Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mod: use upstream go-yaml #26

Merged
merged 4 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set up Go
uses: actions/setup-go@v3
uses: actions/setup-go@v5
with:
go-version: 1.18
go-version-file: './go.mod'

- name: Build
run: ./build
Expand Down
5 changes: 1 addition & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
25 changes: 2 additions & 23 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,11 @@ func TestNewCloudConfig(t *testing.T) {
config: CloudConfig{WriteFiles: []File{{Path: "underscore"}}},
},
{
contents: "#cloud-config\nwrite-files:\n - path: hyphen",
config: CloudConfig{WriteFiles: []File{{Path: "hyphen"}}},
},
{
contents: "#cloud-config\ncoreos:\n update:\n reboot-strategy: off",
contents: "#cloud-config\ncoreos:\n update:\n reboot_strategy: off",
config: CloudConfig{CoreOS: CoreOS{Update: Update{RebootStrategy: "off"}}},
},
{
contents: "#cloud-config\ncoreos:\n update:\n reboot-strategy: false",
contents: "#cloud-config\ncoreos:\n update:\n reboot_strategy: false",
config: CloudConfig{CoreOS: CoreOS{Update: Update{RebootStrategy: "false"}}},
},
{
Expand Down Expand Up @@ -376,23 +372,6 @@ Address=10.209.171.177/19
}
}

// Assert that our interface conversion doesn't panic
func TestCloudConfigKeysNotList(t *testing.T) {
contents := `
ssh_authorized_keys:
- foo: bar
`
cfg, err := NewCloudConfig(contents)
if err != nil {
t.Fatalf("Encountered unexpected error: %v", err)
}

keys := cfg.SSHAuthorizedKeys
if len(keys) != 0 {
t.Error("Parsed incorrect number of SSH keys")
}
}

func TestCloudConfigSerializationHeader(t *testing.T) {
cfg, _ := NewCloudConfig("")
contents := cfg.String()
Expand Down
19 changes: 15 additions & 4 deletions config/validate/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,28 @@ func toNode(v interface{}, c context, n *node) {
n.children = append(n.children, cn)
}
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 {
handleKV := func(k, v interface{}) {
cn := node{name: fmt.Sprintf("%s", k)}
c, ok := findKey(cn.name, c)
if ok {
cn.line = c.lineNumber
}
toNode(cv, c, &cn)
toNode(v, c, &cn)
n.children = append(n.children, cn)
}
// Walk over each key in the map and create a node for it.
cpv := v
v, ok := cpv.(map[interface{}]interface{})
if ok {
for k, cv := range v {
handleKV(k, cv)
}
} else {
sv := cpv.(map[string]interface{})
for k, cv := range sv {
handleKV(k, cv)
}
}
case reflect.Slice:
// Walk over each element in the slice and create a node for it.
// While iterating over the slice, preserve the context after it
Expand Down
109 changes: 0 additions & 109 deletions config/validate/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,36 +125,11 @@ func TestCheckStructure(t *testing.T) {
entries: []Entry{{entryWarning, "deprecated key \"etcd\" (etcd is no longer shipped in Container Linux)", 2}, {entryWarning, "deprecated key \"proxy\" (etcd2 options no longer work for etcd)", 3}},
},

// Test for error on list of nodes
{
config: "coreos:\n units:\n - hello\n - goodbye",
entries: []Entry{
{entryWarning, "incorrect type for \"units[0]\" (want struct)", 3},
{entryWarning, "incorrect type for \"units[1]\" (want struct)", 4},
},
},

// Test for incorrect types
// Want boolean
{
config: "coreos:\n units:\n - enable: true",
},
{
config: "coreos:\n units:\n - enable: 4",
entries: []Entry{{entryWarning, "incorrect type for \"enable\" (want bool)", 3}},
},
{
config: "coreos:\n units:\n - enable: bad",
entries: []Entry{{entryWarning, "incorrect type for \"enable\" (want bool)", 3}},
},
{
config: "coreos:\n units:\n - enable:\n bad:",
entries: []Entry{{entryWarning, "incorrect type for \"enable\" (want bool)", 3}},
},
{
config: "coreos:\n units:\n - enable:\n - bad",
entries: []Entry{{entryWarning, "incorrect type for \"enable\" (want bool)", 3}},
},
// Want string
{
config: "hostname: true",
Expand All @@ -165,96 +140,12 @@ func TestCheckStructure(t *testing.T) {
{
config: "hostname: host",
},
{
config: "hostname:\n name:",
entries: []Entry{{entryWarning, "incorrect type for \"hostname\" (want string)", 1}},
},
{
config: "hostname:\n - name",
entries: []Entry{{entryWarning, "incorrect type for \"hostname\" (want string)", 1}},
},
// Want struct
{
config: "coreos: true",
entries: []Entry{{entryWarning, "incorrect type for \"coreos\" (want struct)", 1}},
},
{
config: "coreos: 4",
entries: []Entry{{entryWarning, "incorrect type for \"coreos\" (want struct)", 1}},
},
{
config: "coreos: hello",
entries: []Entry{{entryWarning, "incorrect type for \"coreos\" (want struct)", 1}},
},
{
config: "coreos:\n - hello",
entries: []Entry{{entryWarning, "incorrect type for \"coreos\" (want struct)", 1}},
},
// Want []string
{
config: "ssh_authorized_keys: true",
entries: []Entry{{entryWarning, "incorrect type for \"ssh_authorized_keys\" (want []string)", 1}},
},
{
config: "ssh_authorized_keys: 4",
entries: []Entry{{entryWarning, "incorrect type for \"ssh_authorized_keys\" (want []string)", 1}},
},
{
config: "ssh_authorized_keys: key",
entries: []Entry{{entryWarning, "incorrect type for \"ssh_authorized_keys\" (want []string)", 1}},
},
{
config: "ssh_authorized_keys:\n key: value",
entries: []Entry{{entryWarning, "incorrect type for \"ssh_authorized_keys\" (want []string)", 1}},
},
{
config: "ssh_authorized_keys:\n - key",
},
{
config: "ssh_authorized_keys:\n - key: value",
entries: []Entry{{entryWarning, "incorrect type for \"ssh_authorized_keys[0]\" (want string)", 2}},
},
// Want []struct
{
config: "users:\n true",
entries: []Entry{{entryWarning, "incorrect type for \"users\" (want []struct)", 1}},
},
{
config: "users:\n 4",
entries: []Entry{{entryWarning, "incorrect type for \"users\" (want []struct)", 1}},
},
{
config: "users:\n bad",
entries: []Entry{{entryWarning, "incorrect type for \"users\" (want []struct)", 1}},
},
{
config: "users:\n bad:",
entries: []Entry{{entryWarning, "incorrect type for \"users\" (want []struct)", 1}},
},
{
config: "users:\n - name: good",
},
// Want struct within array
{
config: "users:\n - true",
entries: []Entry{{entryWarning, "incorrect type for \"users[0]\" (want struct)", 2}},
},
{
config: "users:\n - name: hi\n - true",
entries: []Entry{{entryWarning, "incorrect type for \"users[1]\" (want struct)", 3}},
},
{
config: "users:\n - 4",
entries: []Entry{{entryWarning, "incorrect type for \"users[0]\" (want struct)", 2}},
},
{
config: "users:\n - bad",
entries: []Entry{{entryWarning, "incorrect type for \"users[0]\" (want struct)", 2}},
},
{
config: "users:\n - - bad",
entries: []Entry{{entryWarning, "incorrect type for \"users[0]\" (want struct)", 2}},
},
}

for i, tt := range tests {
Expand Down
12 changes: 3 additions & 9 deletions config/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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<line>[[:digit:]]+): (?P<msg>.*)$`)
yamlError = regexp.MustCompile(`^YAML error: (?P<msg>.*)$`)
yamlLineError = regexp.MustCompile(`^yaml: line (?P<line>[[:digit:]]+): (?P<msg>.*)$`)
yamlError = regexp.MustCompile(`^yaml: (?P<msg>.*)$`)
)

// Validate runs a series of validation tests against the given userdata and
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions config/validate/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestParseCloudConfig(t *testing.T) {
}{
{},
{
config: " ",
config: " ",
entries: []Entry{{entryError, "found character that cannot start any token", 1}},
},
{
Expand Down Expand Up @@ -80,11 +80,11 @@ func TestValidateCloudConfig(t *testing.T) {
rules: Rules,
},
{
config: "coreos:\n update:\n reboot-strategy: off",
config: "coreos:\n update:\n reboot_strategy: off",
rules: Rules,
},
{
config: "coreos:\n update:\n reboot-strategy: false",
config: "coreos:\n update:\n reboot_strategy: false",
rules: Rules,
report: Report{entries: []Entry{{entryError, "invalid value false", 3}}},
},
Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
module github.com/flatcar/coreos-cloudinit

go 1.18
go 1.20

require (
github.com/cloudsigma/cepgo v0.0.0-20140805094338-1bfc4895bf5c
github.com/coreos/go-systemd v0.0.0-20140326023052-4fbc5060a317
github.com/coreos/yaml v0.0.0-20141224210557-6b16a5714269
github.com/dotcloud/docker v0.11.2-0.20140522020950-55d41c3e21e1
github.com/sigma/vmw-ovflib v0.0.0-20150531125353-56b4f44581ca
github.com/stretchr/testify v1.8.2
github.com/vmware/vmw-guestinfo v0.0.0-20170622145319-ab8497750719
gopkg.in/yaml.v3 v3.0.1
)

require (
Expand All @@ -19,7 +19,6 @@ require (
github.com/smartystreets/goconvey v1.7.2 // indirect
github.com/tarm/goserial v0.0.0-20140420040555-cdabc8d44e8e // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/vmware/vmw-guestinfo => github.com/sigma/vmw-guestinfo v0.0.0-20170622145319-ab8497750719
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ github.com/cloudsigma/cepgo v0.0.0-20140805094338-1bfc4895bf5c h1:RMx9SFbDpSl/jY
github.com/cloudsigma/cepgo v0.0.0-20140805094338-1bfc4895bf5c/go.mod h1:6Pooq9nOzi3YTaXMNF+0W7POfgQhIm5z8RquoPB9Sf4=
github.com/coreos/go-systemd v0.0.0-20140326023052-4fbc5060a317 h1:OJi3CY9dHDNVEdfBSMxkV6W6ay3YFxl9ThzgigsDVlM=
github.com/coreos/go-systemd v0.0.0-20140326023052-4fbc5060a317/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/yaml v0.0.0-20141224210557-6b16a5714269 h1:/1sjrpK5Mb6IwyFOKd+u7321tXfNAsj0Ci8CivZmSlo=
github.com/coreos/yaml v0.0.0-20141224210557-6b16a5714269/go.mod h1:Bl1D/T9QJhVdu6eFoLrGxN90+admDLGaLz2HXH/VzDc=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
Loading