diff --git a/cli/config/configfile/file.go b/cli/config/configfile/file.go index f658385e16..383a03228d 100644 --- a/cli/config/configfile/file.go +++ b/cli/config/configfile/file.go @@ -3,6 +3,7 @@ package configfile import ( "encoding/base64" "encoding/json" + "fmt" "io" "io/ioutil" "os" @@ -125,7 +126,7 @@ func (configFile *ConfigFile) LoadFromReader(configData io.Reader) error { ac.ServerAddress = addr configFile.AuthConfigs[addr] = ac } - return nil + return checkKubernetesConfiguration(configFile.Kubernetes) } // ContainsAuth returns whether there is authentication configured @@ -318,3 +319,17 @@ func (configFile *ConfigFile) GetAllCredentials() (map[string]types.AuthConfig, func (configFile *ConfigFile) GetFilename() string { return configFile.Filename } + +func checkKubernetesConfiguration(kubeConfig *KubernetesConfig) error { + if kubeConfig == nil { + return nil + } + switch kubeConfig.AllNamespaces { + case "": + case "enabled": + case "disabled": + default: + return fmt.Errorf("invalid 'kubernetes.allNamespaces' value, should be 'enabled' or 'disabled': %s", kubeConfig.AllNamespaces) + } + return nil +} diff --git a/cli/config/configfile/file_test.go b/cli/config/configfile/file_test.go index c769f53c26..34e3b485b0 100644 --- a/cli/config/configfile/file_test.go +++ b/cli/config/configfile/file_test.go @@ -371,3 +371,45 @@ func TestGetAllCredentialsCredHelperOverridesDefaultStore(t *testing.T) { assert.Check(t, is.Equal(1, testCredsStore.(*mockNativeStore).GetAllCallCount)) assert.Check(t, is.Equal(0, testCredHelper.(*mockNativeStore).GetAllCallCount)) } + +func TestCheckKubernetesConfigurationRaiseAnErrorOnInvalidValue(t *testing.T) { + testCases := []struct { + name string + config *KubernetesConfig + expectError bool + }{ + { + "no kubernetes config is valid", + nil, + false, + }, + { + "enabled is valid", + &KubernetesConfig{AllNamespaces: "enabled"}, + false, + }, + { + "disabled is valid", + &KubernetesConfig{AllNamespaces: "disabled"}, + false, + }, + { + "empty string is valid", + &KubernetesConfig{AllNamespaces: ""}, + false, + }, + { + "other value is invalid", + &KubernetesConfig{AllNamespaces: "unknown"}, + true, + }, + } + for _, test := range testCases { + err := checkKubernetesConfiguration(test.config) + if test.expectError { + assert.Assert(t, err != nil, test.name) + } else { + assert.NilError(t, err, test.name) + } + } +}