From 223c9f0aafb2675210100b7d13c289f58b7b54ed Mon Sep 17 00:00:00 2001 From: Mathieu Tortuyaux Date: Tue, 30 Jan 2024 13:43:15 +0100 Subject: [PATCH] config: use upstream go-yaml Signed-off-by: Mathieu Tortuyaux --- config/config.go | 5 +---- config/validate/node.go | 31 +++++++++++++++++++++++-------- config/validate/validate.go | 12 +++--------- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/config/config.go b/config/config.go index ab1e8a4..f5a6605 100644 --- a/config/config.go +++ b/config/config.go @@ -23,7 +23,7 @@ import ( "strings" "unicode" - "github.com/coreos/yaml" + "gopkg.in/yaml.v3" ) // CloudConfig encapsulates the entire cloud-config configuration file and maps @@ -74,9 +74,6 @@ func IsMultipartMime(userdata string) bool { // string of YAML), returning any error encountered. It will ignore unknown // fields but log encountering them. func NewCloudConfig(contents string) (*CloudConfig, error) { - yaml.UnmarshalMappingKeyTransform = func(nameIn string) (nameOut string) { - return strings.Replace(nameIn, "-", "_", -1) - } var cfg CloudConfig err := yaml.Unmarshal([]byte(contents), &cfg) return &cfg, err diff --git a/config/validate/node.go b/config/validate/node.go index 892be45..51b8058 100644 --- a/config/validate/node.go +++ b/config/validate/node.go @@ -94,15 +94,30 @@ func toNode(v interface{}, c context, n *node) { } case reflect.Map: // Walk over each key in the map and create a node for it. - v := v.(map[interface{}]interface{}) - for k, cv := range v { - cn := node{name: fmt.Sprintf("%s", k)} - c, ok := findKey(cn.name, c) - if ok { - cn.line = c.lineNumber + cpv := v + v, ok := cpv.(map[interface{}]interface{}) + if ok { + for k, cv := range v { + cn := node{name: fmt.Sprintf("%s", k)} + c, ok := findKey(cn.name, c) + if ok { + cn.line = c.lineNumber + } + toNode(cv, c, &cn) + n.children = append(n.children, cn) } - toNode(cv, c, &cn) - n.children = append(n.children, cn) + } else { + sv := cpv.(map[string]interface{}) + for k, cv := range sv { + cn := node{name: fmt.Sprintf("%s", k)} + c, ok := findKey(cn.name, c) + if ok { + cn.line = c.lineNumber + } + toNode(cv, c, &cn) + n.children = append(n.children, cn) + } + } case reflect.Slice: // Walk over each element in the slice and create a node for it. diff --git a/config/validate/validate.go b/config/validate/validate.go index fb0e642..e9496ca 100644 --- a/config/validate/validate.go +++ b/config/validate/validate.go @@ -23,12 +23,12 @@ import ( "github.com/flatcar/coreos-cloudinit/config" - "github.com/coreos/yaml" + "gopkg.in/yaml.v3" ) var ( - yamlLineError = regexp.MustCompile(`^YAML error: line (?P[[:digit:]]+): (?P.*)$`) - yamlError = regexp.MustCompile(`^YAML error: (?P.*)$`) + yamlLineError = regexp.MustCompile(`^yaml: line (?P[[:digit:]]+): (?P.*)$`) + yamlError = regexp.MustCompile(`^yaml: (?P.*)$`) ) // Validate runs a series of validation tests against the given userdata and @@ -77,9 +77,6 @@ func validateCloudConfig(config []byte, rules []rule) (report Report, err error) // any parsing issues into the provided report. Unrecoverable errors are // returned as an error. func parseCloudConfig(cfg []byte, report *Report) (node, error) { - yaml.UnmarshalMappingKeyTransform = func(nameIn string) (nameOut string) { - return nameIn - } // unmarshal the config into an implicitly-typed form. The yaml library // will implicitly convert types into their normalized form // (e.g. 0744 -> 484, off -> false). @@ -108,9 +105,6 @@ func parseCloudConfig(cfg []byte, report *Report) (node, error) { w = normalizeNodeNames(w, report) // unmarshal the config into the explicitly-typed form. - yaml.UnmarshalMappingKeyTransform = func(nameIn string) (nameOut string) { - return strings.Replace(nameIn, "-", "_", -1) - } var strong config.CloudConfig if err := yaml.Unmarshal([]byte(cfg), &strong); err != nil { return node{}, err