fix(*): catch more unknown keys in user and file sections

This commit is contained in:
Jonathan Boulle 2014-05-12 16:33:28 -07:00
parent 81fe0dc9e0
commit 98f5ead730
2 changed files with 66 additions and 9 deletions

View File

@ -64,11 +64,9 @@ 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"] coreos, ok := c["coreos"]
if !ok { if ok {
return
}
set := coreos.(map[interface{}]interface{}) set := coreos.(map[interface{}]interface{})
known := cc["coreos"].(map[interface{}]interface{}) known := cc["coreos"].(map[interface{}]interface{})
for k, _ := range set { for k, _ := range set {
@ -79,6 +77,45 @@ func warnOnUnrecognizedKeys(contents string, warn warner) {
} }
} }
// 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)
}
}
}
}
}
// NewCloudConfig instantiates a new CloudConfig from the given contents (a // NewCloudConfig instantiates a new CloudConfig from the given contents (a
// string of YAML), returning any error encountered. It will ignore unknown // string of YAML), returning any error encountered. It will ignore unknown
// fields but log encountering them. // fields but log encountering them.

View File

@ -18,6 +18,14 @@ section_unknown:
something something
bare_unknown: bare_unknown:
bar bar
write_files:
- content: fun
path: /var/party
file_unknown: nofun
users:
- name: fry
passwd: somehash
user_unknown: philip
hostname: hostname:
foo foo
` `
@ -31,6 +39,12 @@ hostname:
if len(cfg.Coreos.Etcd) < 1 { if len(cfg.Coreos.Etcd) < 1 {
t.Fatalf("etcd section not correctly set when invalid keys are present") 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 var warnings string
catchWarn := func(f string, v ...interface{}) { catchWarn := func(f string, v ...interface{}) {
@ -48,6 +62,12 @@ hostname:
if !strings.Contains(warnings, "section_unknown") { if !strings.Contains(warnings, "section_unknown") {
t.Errorf("warnings did not catch unrecognized key 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" // Assert that the parsing of a cloud config file "generally works"