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

Using strict parsing for yaml configs #1433

Merged
merged 1 commit into from
Jan 6, 2020
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
22 changes: 22 additions & 0 deletions pkg/cfg/cfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,25 @@ tls:
},
}, data)
}

func TestParseWithInvalidYAML(t *testing.T) {
yamlSource := dYAML([]byte(`
servers:
ports: 2000
timeoutz: 60h
tls:
keey: YAML
`))

fs := flag.NewFlagSet(t.Name(), flag.PanicOnError)
flagSource := dFlags(fs, []string{"-verbose", "-server.port=21"})

data := Data{}
err := dParse(&data,
dDefaults(fs),
yamlSource,
flagSource,
)
require.Error(t, err)
require.Equal(t, err.Error(), "yaml: unmarshal errors:\n line 2: field servers not found in type cfg.Data\n line 6: field keey not found in type cfg.TLS")
}
2 changes: 1 addition & 1 deletion pkg/cfg/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func YAML(f *string) Source {
// dYAML returns a YAML source and allows dependency injection
func dYAML(y []byte) Source {
return func(dst interface{}) error {
return yaml.Unmarshal(y, dst)
return yaml.UnmarshalStrict(y, dst)
}
}

Expand Down