From 4ffd47f84ae2511bd3f5dd68f18280ba675a1bca Mon Sep 17 00:00:00 2001 From: MakMuftic Date: Tue, 14 May 2024 16:49:19 +0200 Subject: [PATCH 1/2] add basic auth --- README.md | 20 ++++++++++++++++++++ main.go | 19 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/README.md b/README.md index 45bb528..f879eb1 100644 --- a/README.md +++ b/README.md @@ -105,3 +105,23 @@ Each JSON configuration file for the gateways can specify detailed settings for ] } ``` + +## Authentication +Basic authentication has been added to the RPC Gateway. You need to provide a username and password for access. These can be configured using CLI flags: + +- `--username`: The username for basic authentication. +- `--password`: The password for basic authentication. + +**The password must be provided; otherwise, the application will throw an error.** + +### Running the Application +To run the application with authentication: + +``` +DEBUG=true go run . --config config.json --username myuser --password mypass +``` +To use configuration from an environment variable: + +``` +DEBUG=true go run . --env --username myuser --password mypass +``` \ No newline at end of file diff --git a/main.go b/main.go index a9c28f3..0e7b6ac 100644 --- a/main.go +++ b/main.go @@ -58,6 +58,16 @@ func main() { Usage: "Load configuration from environment variable named GATEWAY_CONFIG.", Value: false, }, + &cli.StringFlag{ + Name: "username", + Usage: "The username for basic authentication.", + Value: "sygma", + }, + &cli.StringFlag{ + Name: "password", + Usage: "The password for basic authentication.", + Value: "", + }, }, Action: func(cc *cli.Context) error { configPath := resolveConfigPath(cc.String("config"), cc.Bool("env")) @@ -73,6 +83,15 @@ func main() { r.Use(httplog.RequestLogger(logger)) r.Use(middleware.Recoverer) r.Use(middleware.Heartbeat("/health")) + // Add basic auth middleware + pass := cc.String("password") + if pass == "" { + return errors.New("password for basic authentication is required") + } + r.Use(middleware.BasicAuth("API Realm", map[string]string{ + cc.String("username"): pass, + })) + server := &http.Server{ Addr: fmt.Sprintf(":%d", config.Port), Handler: r, From 2a293c5a7f34ec3578f709c4055b173a2e82d3bd Mon Sep 17 00:00:00 2001 From: MakMuftic Date: Wed, 15 May 2024 11:12:49 +0200 Subject: [PATCH 2/2] move to env --- README.md | 14 ++------------ main.go | 28 +++++++++++++--------------- 2 files changed, 15 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index f879eb1..7dbd9e0 100644 --- a/README.md +++ b/README.md @@ -107,21 +107,11 @@ Each JSON configuration file for the gateways can specify detailed settings for ``` ## Authentication -Basic authentication has been added to the RPC Gateway. You need to provide a username and password for access. These can be configured using CLI flags: - -- `--username`: The username for basic authentication. -- `--password`: The password for basic authentication. - -**The password must be provided; otherwise, the application will throw an error.** +Basic authentication can be enabled using the `--basic-auth` flag. The username and password should be set through environment variables `GATEWAY_USERNAME` and `GATEWAY_PASSWORD`, respectively. ### Running the Application To run the application with authentication: ``` -DEBUG=true go run . --config config.json --username myuser --password mypass -``` -To use configuration from an environment variable: - +DEBUG=true GATEWAY_USERNAME=myuser GATEWAY_PASSWORD=mypass go run . --config config.json --basic-auth ``` -DEBUG=true go run . --env --username myuser --password mypass -``` \ No newline at end of file diff --git a/main.go b/main.go index 0e7b6ac..470e544 100644 --- a/main.go +++ b/main.go @@ -58,15 +58,10 @@ func main() { Usage: "Load configuration from environment variable named GATEWAY_CONFIG.", Value: false, }, - &cli.StringFlag{ - Name: "username", - Usage: "The username for basic authentication.", - Value: "sygma", - }, - &cli.StringFlag{ - Name: "password", - Usage: "The password for basic authentication.", - Value: "", + &cli.BoolFlag{ + Name: "basic-auth", + Usage: "Enable basic authentication.", + Value: false, }, }, Action: func(cc *cli.Context) error { @@ -84,13 +79,16 @@ func main() { r.Use(middleware.Recoverer) r.Use(middleware.Heartbeat("/health")) // Add basic auth middleware - pass := cc.String("password") - if pass == "" { - return errors.New("password for basic authentication is required") + if cc.Bool("basic-auth") { + username := os.Getenv("GATEWAY_USERNAME") + password := os.Getenv("GATEWAY_PASSWORD") + if username == "" || password == "" { + return errors.New("both GATEWAY_USERNAME and GATEWAY_PASSWORD environment variables must be set for basic authentication") + } + r.Use(middleware.BasicAuth("API Realm", map[string]string{ + username: password, + })) } - r.Use(middleware.BasicAuth("API Realm", map[string]string{ - cc.String("username"): pass, - })) server := &http.Server{ Addr: fmt.Sprintf(":%d", config.Port),