From c8d706bb91d265d3e940589d47e78d3c4dcc472b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Sun, 27 Sep 2020 19:40:32 +0100 Subject: [PATCH] feat(backend): fail on unknown keys in the config file --- .../022_auth_basicAuth_no_credentials.txt | 2 +- .../testscript/075_config_extra_fileds.txt | 26 +++++++++++++++++++ internal/config/config.go | 22 ++++++++++++++++ internal/config/config_test.go | 7 +++++ 4 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 cmd/karma/tests/testscript/075_config_extra_fileds.txt diff --git a/cmd/karma/tests/testscript/022_auth_basicAuth_no_credentials.txt b/cmd/karma/tests/testscript/022_auth_basicAuth_no_credentials.txt index 29d2e5122..5373e5974 100644 --- a/cmd/karma/tests/testscript/022_auth_basicAuth_no_credentials.txt +++ b/cmd/karma/tests/testscript/022_auth_basicAuth_no_credentials.txt @@ -11,4 +11,4 @@ alertmanager: authentication: basicAuth: users: - - foo: bar + - username: bar diff --git a/cmd/karma/tests/testscript/075_config_extra_fileds.txt b/cmd/karma/tests/testscript/075_config_extra_fileds.txt new file mode 100644 index 000000000..3e59596e0 --- /dev/null +++ b/cmd/karma/tests/testscript/075_config_extra_fileds.txt @@ -0,0 +1,26 @@ +# Raises an error if --config.file points to a file that contains unknown keys +karma.bin-should-fail --log.format=text --log.config=false --check-config +! stdout . +! stderr 'msg="Configuration is valid"' +stderr 'msg="Failed to parse configuration file \\"karma.yaml\\": yaml: unmarshal errors:\\n line 5: field authorizationFoo not found in type config.configSchema"' + +-- karma.yaml -- +authentication: + header: + name: "X-User" + value_re: "(.+)" +authorizationFoo: + groups: + - name: admins + members: + - alice + - bob + - name: users + members: + - john + acl: + silences: acl.yaml +alertmanager: + servers: + - name: default + uri: https://localhost:9093 diff --git a/internal/config/config.go b/internal/config/config.go index 41d8002d4..71b0bcee8 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -4,6 +4,7 @@ import ( "bufio" "bytes" "fmt" + "io/ioutil" "os" "strings" "time" @@ -123,6 +124,21 @@ func SetupFlags(f *pflag.FlagSet) { f.String("ui.collapseGroups", "collapsedOnMobile", "Default state for alert groups") } +func validateConfigFile(path string) error { + f, err := ioutil.ReadFile(path) + if err != nil { + return err + } + + cfg := configSchema{} + err = yaml.UnmarshalStrict(f, &cfg) + if err != nil { + return err + } + + return nil +} + func readConfigFile(k *koanf.Koanf, flags *pflag.FlagSet) (string, error) { var configFile string @@ -243,6 +259,12 @@ func (config *configSchema) Read(flags *pflag.FlagSet) (string, error) { return "", fmt.Errorf("Failed to unmarshal configuration: %v", err) } + if configFileUsed != "" { + if err := validateConfigFile(configFileUsed); err != nil { + return "", fmt.Errorf("Failed to parse configuration file %q: %v", configFileUsed, err) + } + } + if config.Authentication.Header.Name != "" && len(config.Authentication.BasicAuth.Users) > 0 { return "", fmt.Errorf("Both authentication.basicAuth.users and authentication.header.name is set, only one can be enabled") } diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 00543cbc2..b248aa1e0 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -330,3 +330,10 @@ func TestDefaultConfig(t *testing.T) { t.Errorf("Wrong silence form config returned (-want +got):\n%s", diff) } } + +func TestValidateConfigMissingFile(t *testing.T) { + err := validateConfigFile(("/foo/bar/xxx/yyy.yaml")) + if err == nil { + t.Errorf("validateConfigFile didn't return any error") + } +}