config: remove config validator
This commit is contained in:
		| @@ -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 { | ||||
|   | ||||
| @@ -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" | ||||
|   | ||||
		Reference in New Issue
	
	Block a user