From 83730015d3462eb60e3038be010879c73d5afd79 Mon Sep 17 00:00:00 2001 From: Fabian Vowie Date: Sun, 23 Jan 2022 18:13:12 +0100 Subject: [PATCH 1/2] Add enabled field to rate limiter settings --- main.go | 5 ++++- settings/settings.go | 6 ++++-- settings/settings_test.go | 1 + 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 030aa5b..3d2f59d 100644 --- a/main.go +++ b/main.go @@ -71,7 +71,10 @@ func main() { r := mux.NewRouter() r.Use(authMiddleware.Middleware) - r.Use(rateLimiterMiddleware.Middleware) + + if appSettings.RateLimiter.Enabled { + r.Use(rateLimiterMiddleware.Middleware) + } r.HandleFunc("/", IndexHandler) diff --git a/settings/settings.go b/settings/settings.go index 6e33efe..8b0264d 100644 --- a/settings/settings.go +++ b/settings/settings.go @@ -27,8 +27,9 @@ type StorageSettings struct { } type RateLimiterSettings struct { - RequestsPerMinute int `json:"requests_per_minute"` - AllowedBurst int `json:"allowed_burst"` + Enabled bool `json:"enabled"` + RequestsPerMinute int `json:"requests_per_minute"` + AllowedBurst int `json:"allowed_burst"` } func parseSettings(data []byte) Settings { @@ -58,6 +59,7 @@ func LoadSettings(fileSystem afero.Fs) (Settings, error) { Endpoint: "127.0.0.1:8000", Token: "changeme", RateLimiter: RateLimiterSettings{ + Enabled: true, RequestsPerMinute: 20, AllowedBurst: 5, }, diff --git a/settings/settings_test.go b/settings/settings_test.go index fa801ff..8f91392 100644 --- a/settings/settings_test.go +++ b/settings/settings_test.go @@ -14,6 +14,7 @@ func TestSettingsParsing(t *testing.T) { "endpoint": "0.0.0.0:8000", "token": "foobar", "rate_limiter": { + "enabled": true, "requests_per_minute": 20, "allowed_burst": 5 }, From 959432c842268be751f43ba9d4bbad35ff0c89f5 Mon Sep 17 00:00:00 2001 From: Fabian Vowie Date: Sun, 23 Jan 2022 18:16:55 +0100 Subject: [PATCH 2/2] Add enabled field to authentication settings --- main.go | 18 +++++++++++------- settings/settings.go | 18 +++++++++++++----- settings/settings_test.go | 7 +++++-- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index 3d2f59d..049a604 100644 --- a/main.go +++ b/main.go @@ -63,16 +63,20 @@ func main() { pipes := pipelines.LoadPipelines() - authMiddleware := middlewares.CreateAuthenticationMiddleware(appSettings.Token) - rateLimiterMiddleware, err := middlewares.CreateRateLimiterMiddleware(appSettings.RateLimiter.RequestsPerMinute, appSettings.RateLimiter.AllowedBurst) - if err != nil { - panic(err) - } - r := mux.NewRouter() - r.Use(authMiddleware.Middleware) + + if appSettings.Authentication.Enabled { + authMiddleware := middlewares.CreateAuthenticationMiddleware(appSettings.Authentication.Token) + + r.Use(authMiddleware.Middleware) + } if appSettings.RateLimiter.Enabled { + rateLimiterMiddleware, err := middlewares.CreateRateLimiterMiddleware(appSettings.RateLimiter.RequestsPerMinute, appSettings.RateLimiter.AllowedBurst) + if err != nil { + panic(err) + } + r.Use(rateLimiterMiddleware.Middleware) } diff --git a/settings/settings.go b/settings/settings.go index 8b0264d..81cd6e4 100644 --- a/settings/settings.go +++ b/settings/settings.go @@ -15,10 +15,10 @@ const ( type FileSystemType int type Settings struct { - Endpoint string `json:"endpoint"` - Token string `json:"token"` - RateLimiter RateLimiterSettings `json:"rate_limiter"` - StorageProvider StorageSettings `json:"storage_provider"` + Endpoint string `json:"endpoint"` + Authentication AuthenticationSettings `json:"authentication"` + RateLimiter RateLimiterSettings `json:"rate_limiter"` + StorageProvider StorageSettings `json:"storage_provider"` } type StorageSettings struct { @@ -26,6 +26,11 @@ type StorageSettings struct { BasePath string `json:"base_path"` } +type AuthenticationSettings struct { + Enabled bool `json:"enabled"` + Token string `json:"token"` +} + type RateLimiterSettings struct { Enabled bool `json:"enabled"` RequestsPerMinute int `json:"requests_per_minute"` @@ -57,7 +62,10 @@ func LoadSettings(fileSystem afero.Fs) (Settings, error) { // If file does not exist, create default settings defaultSettings := Settings{ Endpoint: "127.0.0.1:8000", - Token: "changeme", + Authentication: AuthenticationSettings{ + Enabled: true, + Token: "changeme", + }, RateLimiter: RateLimiterSettings{ Enabled: true, RequestsPerMinute: 20, diff --git a/settings/settings_test.go b/settings/settings_test.go index 8f91392..287734e 100644 --- a/settings/settings_test.go +++ b/settings/settings_test.go @@ -12,7 +12,10 @@ import ( func TestSettingsParsing(t *testing.T) { const file string = `{ "endpoint": "0.0.0.0:8000", - "token": "foobar", + "authentication": { + "enabled": true, + "token": "foobar" + }, "rate_limiter": { "enabled": true, "requests_per_minute": 20, @@ -28,7 +31,7 @@ func TestSettingsParsing(t *testing.T) { settings := parseSettings([]byte(file)) assert.Equal(t, "0.0.0.0:8000", settings.Endpoint) - assert.Equal(t, "foobar", settings.Token) + assert.Equal(t, "foobar", settings.Authentication.Token) assert.Equal(t, "assets", settings.StorageProvider.BasePath) }) }