diff --git a/.chloggen/strict-unmarshal.yaml b/.chloggen/strict-unmarshal.yaml new file mode 100644 index 000000000000..eef42e520340 --- /dev/null +++ b/.chloggen/strict-unmarshal.yaml @@ -0,0 +1,27 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: 'breaking' + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: opampsupervisor + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: "Enable strict unmarshalling of the OpAMP Supervisor config file. An error will now be returned if an invalid config key is set." + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [35838] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [user] diff --git a/cmd/opampsupervisor/go.mod b/cmd/opampsupervisor/go.mod index 9e990ce8661a..733a8867454f 100644 --- a/cmd/opampsupervisor/go.mod +++ b/cmd/opampsupervisor/go.mod @@ -4,6 +4,7 @@ go 1.22.0 require ( github.com/cenkalti/backoff/v4 v4.3.0 + github.com/go-viper/mapstructure/v2 v2.2.1 github.com/google/uuid v1.6.0 github.com/knadh/koanf/maps v0.1.1 github.com/knadh/koanf/parsers/yaml v0.1.0 @@ -25,7 +26,6 @@ require ( require ( github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/fsnotify/fsnotify v1.8.0 // indirect - github.com/go-viper/mapstructure/v2 v2.2.1 // indirect github.com/google/go-cmp v0.6.0 // indirect github.com/gorilla/websocket v1.5.3 // indirect github.com/mitchellh/copystructure v1.2.0 // indirect diff --git a/cmd/opampsupervisor/supervisor/config/config.go b/cmd/opampsupervisor/supervisor/config/config.go index 68c99fa4c755..4bc88b63fff1 100644 --- a/cmd/opampsupervisor/supervisor/config/config.go +++ b/cmd/opampsupervisor/supervisor/config/config.go @@ -13,6 +13,7 @@ import ( "runtime" "time" + "github.com/go-viper/mapstructure/v2" "github.com/knadh/koanf/parsers/yaml" "github.com/knadh/koanf/providers/file" "github.com/knadh/koanf/v2" @@ -41,11 +42,19 @@ func Load(configFile string) (Supervisor, error) { return Supervisor{}, err } + cfg := DefaultSupervisor() + decodeConf := koanf.UnmarshalConf{ Tag: "mapstructure", + DecoderConfig: &mapstructure.DecoderConfig{ + DecodeHook: mapstructure.ComposeDecodeHookFunc( + mapstructure.StringToTimeDurationHookFunc()), + Result: &cfg, + WeaklyTypedInput: true, + ErrorUnused: true, + }, } - cfg := DefaultSupervisor() if err := k.UnmarshalWithConf("", &cfg, decodeConf); err != nil { return Supervisor{}, fmt.Errorf("cannot parse %s: %w", configFile, err) } diff --git a/cmd/opampsupervisor/supervisor/supervisor_test.go b/cmd/opampsupervisor/supervisor/supervisor_test.go index 3a034bad4113..24301fc19626 100644 --- a/cmd/opampsupervisor/supervisor/supervisor_test.go +++ b/cmd/opampsupervisor/supervisor/supervisor_test.go @@ -1366,3 +1366,32 @@ service: require.NoError(t, err) require.Equal(t, expectedConfig, noopConfig) } + +func TestSupervisor_configStrictUnmarshal(t *testing.T) { + tmpDir, err := os.MkdirTemp(os.TempDir(), "*") + require.NoError(t, err) + + configuration := ` +server: + endpoint: ws://localhost/v1/opamp + tls: + insecure: true + +capabilities: + reports_effective_config: true + invalid_key: invalid_value +` + + cfgPath := filepath.Join(tmpDir, "config.yaml") + err = os.WriteFile(cfgPath, []byte(configuration), 0o600) + require.NoError(t, err) + + _, err = config.Load(cfgPath) + require.Error(t, err) + require.ErrorContains(t, err, "cannot parse") + + t.Cleanup(func() { + require.NoError(t, os.Chmod(tmpDir, 0o700)) + require.NoError(t, os.RemoveAll(tmpDir)) + }) +}