From 98f5ead7300023eabf07c0341d03f898c2ce9c26 Mon Sep 17 00:00:00 2001 From: Jonathan Boulle Date: Mon, 12 May 2014 16:33:28 -0700 Subject: [PATCH] fix(*): catch more unknown keys in user and file sections --- initialize/config.go | 55 ++++++++++++++++++++++++++++++++------- initialize/config_test.go | 20 ++++++++++++++ 2 files changed, 66 insertions(+), 9 deletions(-) diff --git a/initialize/config.go b/initialize/config.go index 423d4e1..5f5c89b 100644 --- a/initialize/config.go +++ b/initialize/config.go @@ -64,17 +64,54 @@ func warnOnUnrecognizedKeys(contents string, warn warner) { } } - // Finally, check for unrecognized coreos options, if any are set + // Check for unrecognized coreos options, if any are set coreos, ok := c["coreos"] - if !ok { - return + if ok { + set := coreos.(map[interface{}]interface{}) + known := cc["coreos"].(map[interface{}]interface{}) + for k, _ := range set { + key := k.(string) + if _, ok := known[key]; !ok { + warn("Warning: unrecognized key %q in coreos section of provided cloud config - ignoring", key) + } + } } - set := coreos.(map[interface{}]interface{}) - known := cc["coreos"].(map[interface{}]interface{}) - for k, _ := range set { - key := k.(string) - if _, ok := known[key]; !ok { - warn("Warning: unrecognized key %q in coreos section of provided cloud config - ignoring", key) + + // Check for any badly-specified users, if any are set + users, ok := c["users"] + if ok { + var known map[string]interface{} + b, _ := goyaml.Marshal(&system.User{}) + goyaml.Unmarshal(b, &known) + + set := users.([]interface{}) + for _, u := range set { + user := u.(map[interface{}]interface{}) + for k, _ := range user { + key := k.(string) + if _, ok := known[key]; !ok { + warn("Warning: unrecognized key %q in user section of cloud config - ignoring", key) + } + } + } + } + + // Check for any badly-specified files, if any are set + files, ok := c["write_files"] + if ok { + var known map[string]interface{} + b, _ := goyaml.Marshal(&system.File{}) + goyaml.Unmarshal(b, &known) + + set := files.([]interface{}) + for _, f := range set { + file := f.(map[interface{}]interface{}) + for k, _ := range file { + key := k.(string) + if _, ok := known[key]; !ok { + warn("Warning: unrecognized key %q in file section of cloud config - ignoring", key) + } + } } } } diff --git a/initialize/config_test.go b/initialize/config_test.go index 51c228b..6db2949 100644 --- a/initialize/config_test.go +++ b/initialize/config_test.go @@ -18,6 +18,14 @@ section_unknown: something bare_unknown: bar +write_files: + - content: fun + path: /var/party + file_unknown: nofun +users: + - name: fry + passwd: somehash + user_unknown: philip hostname: foo ` @@ -31,6 +39,12 @@ hostname: if len(cfg.Coreos.Etcd) < 1 { t.Fatalf("etcd section not correctly set when invalid keys are present") } + if len(cfg.WriteFiles) < 1 || cfg.WriteFiles[0].Content != "fun" || cfg.WriteFiles[0].Path != "/var/party" { + t.Fatalf("write_files section not correctly set when invalid keys are present") + } + if len(cfg.Users) < 1 || cfg.Users[0].Name != "fry" || cfg.Users[0].PasswordHash != "somehash" { + t.Fatalf("users section not correctly set when invalid keys are present") + } var warnings string catchWarn := func(f string, v ...interface{}) { @@ -48,6 +62,12 @@ hostname: if !strings.Contains(warnings, "section_unknown") { t.Errorf("warnings did not catch unrecognized key section_unknown") } + if !strings.Contains(warnings, "user_unknown") { + t.Errorf("warnings did not catch unrecognized user key user_unknown") + } + if !strings.Contains(warnings, "file_unknown") { + t.Errorf("warnings did not catch unrecognized file key file_unknown") + } } // Assert that the parsing of a cloud config file "generally works"