From 51f37100a176fdd8e5970e47e4f9e3f0010ad3f1 Mon Sep 17 00:00:00 2001 From: Alex Crawford Date: Wed, 22 Oct 2014 11:43:26 -0700 Subject: [PATCH] config: remove config validator --- config/config.go | 86 ------------------------------------------- config/config_test.go | 24 ------------ 2 files changed, 110 deletions(-) diff --git a/config/config.go b/config/config.go index 6dedc2b..9ac6d69 100644 --- a/config/config.go +++ b/config/config.go @@ -18,7 +18,6 @@ package config import ( "fmt" - "log" "reflect" "strings" @@ -68,7 +67,6 @@ func NewCloudConfig(contents string) (*CloudConfig, error) { if err = yaml.Unmarshal(ncontents, &cfg); err != nil { return &cfg, err } - warnOnUnrecognizedKeys(contents, log.Printf) return &cfg, nil } @@ -158,90 +156,6 @@ func isFieldExported(f reflect.StructField) bool { return f.PkgPath == "" } -type warner func(format string, v ...interface{}) - -// warnOnUnrecognizedKeys parses the contents of a cloud-config file and calls -// warn(msg, key) for every unrecognized key (i.e. those not present in CloudConfig) -func warnOnUnrecognizedKeys(contents string, warn warner) { - // Generate a map of all understood cloud config options - var cc map[string]interface{} - b, _ := yaml.Marshal(&CloudConfig{}) - yaml.Unmarshal(b, &cc) - - // Now unmarshal the entire provided contents - var c map[string]interface{} - yaml.Unmarshal([]byte(contents), &c) - - // Check that every key in the contents exists in the cloud config - for k, _ := range c { - if _, ok := cc[k]; !ok { - warn("Warning: unrecognized key %q in provided cloud config - ignoring section", k) - } - } - - // Check for unrecognized coreos options, if any are set - if coreos, ok := c["coreos"]; ok { - if set, ok := coreos.(map[interface{}]interface{}); ok { - known := cc["coreos"].(map[interface{}]interface{}) - for k, _ := range set { - if key, ok := k.(string); ok { - if _, ok := known[key]; !ok { - warn("Warning: unrecognized key %q in coreos section of provided cloud config - ignoring", key) - } - } else { - warn("Warning: unrecognized key %q in coreos section of provided cloud config - ignoring", k) - } - } - } - } - - // Check for any badly-specified users, if any are set - if users, ok := c["users"]; ok { - var known map[string]interface{} - b, _ := yaml.Marshal(&User{}) - yaml.Unmarshal(b, &known) - - if set, ok := users.([]interface{}); ok { - for _, u := range set { - if user, ok := u.(map[interface{}]interface{}); ok { - for k, _ := range user { - if key, ok := k.(string); ok { - if _, ok := known[key]; !ok { - warn("Warning: unrecognized key %q in user section of cloud config - ignoring", key) - } - } else { - warn("Warning: unrecognized key %q in user section of cloud config - ignoring", k) - } - } - } - } - } - } - - // Check for any badly-specified files, if any are set - if files, ok := c["write_files"]; ok { - var known map[string]interface{} - b, _ := yaml.Marshal(&File{}) - yaml.Unmarshal(b, &known) - - if set, ok := files.([]interface{}); ok { - for _, f := range set { - if file, ok := f.(map[interface{}]interface{}); ok { - for k, _ := range file { - if key, ok := k.(string); ok { - if _, ok := known[key]; !ok { - warn("Warning: unrecognized key %q in file section of cloud config - ignoring", key) - } - } else { - warn("Warning: unrecognized key %q in file section of cloud config - ignoring", k) - } - } - } - } - } - } -} - func normalizeConfig(config string) ([]byte, error) { var cfg map[interface{}]interface{} if err := yaml.Unmarshal([]byte(config), &cfg); err != nil { diff --git a/config/config_test.go b/config/config_test.go index ea68947..dbcbc12 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -17,7 +17,6 @@ package config import ( - "fmt" "reflect" "strings" "testing" @@ -146,29 +145,6 @@ hostname: 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{}) { - warnings += fmt.Sprintf(f, v...) - } - - warnOnUnrecognizedKeys(contents, catchWarn) - - if !strings.Contains(warnings, "coreos_unknown") { - t.Errorf("warnings did not catch unrecognized coreos option coreos_unknown") - } - if !strings.Contains(warnings, "bare_unknown") { - t.Errorf("warnings did not catch unrecognized key bare_unknown") - } - 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"