diff options
author | feistel <6742251-feistel@users.noreply.gitlab.com> | 2022-01-31 20:07:08 +0300 |
---|---|---|
committer | feistel <6742251-feistel@users.noreply.gitlab.com> | 2022-02-01 03:02:44 +0300 |
commit | 17779736478d44af08782dec0e7c892c31b71297 (patch) | |
tree | e523ecc385e5e68e1ba3b031160eefff7fbad602 /internal/config | |
parent | 3470305283c03603ca202c1c246c8e2035faf440 (diff) |
refactor: forbid empty listeners
Diffstat (limited to 'internal/config')
-rw-r--r-- | internal/config/validate.go | 26 | ||||
-rw-r--r-- | internal/config/validate_test.go | 18 |
2 files changed, 42 insertions, 2 deletions
diff --git a/internal/config/validate.go b/internal/config/validate.go index 3039d00a..1b5775af 100644 --- a/internal/config/validate.go +++ b/internal/config/validate.go @@ -2,6 +2,7 @@ package config import ( "errors" + "fmt" "net/url" "github.com/hashicorp/go-multierror" @@ -18,6 +19,7 @@ var ( errAuthNoRedirect = errors.New("auth-redirect-uri must be defined if authentication is supported") errArtifactsServerUnsupportedScheme = errors.New("artifacts-server scheme must be either http:// or https://") errArtifactsServerInvalidTimeout = errors.New("artifacts-server-timeout must be greater than or equal to 1") + errEmptyListener = errors.New("listener must not be empty") ) // Validate values populated in Config @@ -42,7 +44,29 @@ func validateListeners(config *Config) error { return errNoListener } - return nil + var result *multierror.Error + for i, s := range config.ListenHTTPStrings.Split() { + if s == "" { + result = multierror.Append(result, fmt.Errorf("empty http listener at index %d: %w", i, errEmptyListener)) + } + } + for i, s := range config.ListenHTTPSStrings.Split() { + if s == "" { + result = multierror.Append(result, fmt.Errorf("empty https listener at index %d: %w", i, errEmptyListener)) + } + } + for i, s := range config.ListenHTTPSProxyv2Strings.Split() { + if s == "" { + result = multierror.Append(result, fmt.Errorf("empty proxyv2 listener at index %d: %w", i, errEmptyListener)) + } + } + for i, s := range config.ListenProxyStrings.Split() { + if s == "" { + result = multierror.Append(result, fmt.Errorf("empty proxy listener at index %d: %w", i, errEmptyListener)) + } + } + + return result.ErrorOrNil() } func validateAuthConfig(config *Config) error { diff --git a/internal/config/validate_test.go b/internal/config/validate_test.go index 87eb26b3..60e37732 100644 --- a/internal/config/validate_test.go +++ b/internal/config/validate_test.go @@ -61,6 +61,11 @@ func TestConfigValidate(t *testing.T) { cfg: artifactsInvalidTimeout, expectedErr: errArtifactsServerInvalidTimeout, }, + { + name: "empty_listener", + cfg: emptyListeners, + expectedErr: errEmptyListener, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -77,6 +82,13 @@ func TestConfigValidate(t *testing.T) { } } +func emptyListeners(cfg *Config) { + cfg.ListenHTTPSStrings = MultiStringFlag{ + value: []string{"127.0.0.1:8080", "", ":8081"}, + separator: ",", + } +} + func noListeners(cfg *Config) { cfg.ListenHTTPStrings = MultiStringFlag{separator: ","} cfg.ListenHTTPSStrings = MultiStringFlag{separator: ","} @@ -123,7 +135,11 @@ func artifactsInvalidTimeout(cfg *Config) { func validConfig() Config { cfg := Config{ ListenHTTPStrings: MultiStringFlag{ - value: []string{"127.0.0.1:80"}, + value: []string{"127.0.0.1:80", ":8081"}, + separator: ",", + }, + ListenHTTPSStrings: MultiStringFlag{ + value: []string{"127.0.0.1:", ":8082"}, separator: ",", }, ArtifactsServer: ArtifactsServer{ |