From 5e4ed89c18269d543f73e1f493cb1e7563b2e946 Mon Sep 17 00:00:00 2001 From: Alok Kumar Singh Date: Sat, 2 Nov 2024 19:12:55 +0530 Subject: [PATCH 1/9] Adding new DecoderConfig with required setting instead of using the default provided by koanf Signed-off-by: Alok Kumar Singh --- cmd/opampsupervisor/supervisor/config/config.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/cmd/opampsupervisor/supervisor/config/config.go b/cmd/opampsupervisor/supervisor/config/config.go index 2b77cc56bd62..fbe3cf2b0947 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) } From f5be4e6236b14c672156bfbbb5675e4cf1fabdda Mon Sep 17 00:00:00 2001 From: Alok Kumar Singh Date: Sat, 9 Nov 2024 15:21:13 +0530 Subject: [PATCH 2/9] Test added Signed-off-by: Alok Kumar Singh --- .../supervisor/supervisor_test.go | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/cmd/opampsupervisor/supervisor/supervisor_test.go b/cmd/opampsupervisor/supervisor/supervisor_test.go index 6185ff43c1c1..43176384f203 100644 --- a/cmd/opampsupervisor/supervisor/supervisor_test.go +++ b/cmd/opampsupervisor/supervisor/supervisor_test.go @@ -1372,3 +1372,34 @@ 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 + accepts_restart_command_invalid_key: true +` + + 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.True(t, strings.HasPrefix(err.Error(), "cannot parse")) + + fmt.Println(err.Error()) + + t.Cleanup(func() { + require.NoError(t, os.Chmod(tmpDir, 0o700)) + require.NoError(t, os.RemoveAll(tmpDir)) + }) +} From ec33e81c9c2200d14780020c0342acc903a9e5f4 Mon Sep 17 00:00:00 2001 From: Alok Kumar Singh Date: Sat, 9 Nov 2024 15:38:44 +0530 Subject: [PATCH 3/9] Added chlog Signed-off-by: Alok Kumar Singh --- .chloggen/strict-unmarshal.yaml | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 .chloggen/strict-unmarshal.yaml diff --git a/.chloggen/strict-unmarshal.yaml b/.chloggen/strict-unmarshal.yaml new file mode 100644 index 000000000000..594732bc90f2 --- /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: 'enhancement' + +# 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: "The change enables strict unmarshal of config file for opampsupervisor. Post this change, an error would be returned while unmarshalling a config file where a key exist which doesn't configure anything" + +# 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] From 25ce7eb1210f4413a7863edcd4f28cee9da57797 Mon Sep 17 00:00:00 2001 From: Alok Kumar Singh Date: Sat, 9 Nov 2024 15:42:02 +0530 Subject: [PATCH 4/9] Minor test changes Signed-off-by: Alok Kumar Singh --- cmd/opampsupervisor/supervisor/supervisor_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/opampsupervisor/supervisor/supervisor_test.go b/cmd/opampsupervisor/supervisor/supervisor_test.go index 43176384f203..18af43891017 100644 --- a/cmd/opampsupervisor/supervisor/supervisor_test.go +++ b/cmd/opampsupervisor/supervisor/supervisor_test.go @@ -1385,7 +1385,7 @@ server: capabilities: reports_effective_config: true - accepts_restart_command_invalid_key: true + invalid_key: invalid_value ` cfgPath := filepath.Join(tmpDir, "config.yaml") From ed0fa737da2c62a9d8be0377f2a4490764ff2e10 Mon Sep 17 00:00:00 2001 From: Alok Kumar Singh Date: Sun, 10 Nov 2024 12:07:02 +0530 Subject: [PATCH 5/9] Minor test changes Signed-off-by: Alok Kumar Singh --- cmd/opampsupervisor/supervisor/supervisor_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/opampsupervisor/supervisor/supervisor_test.go b/cmd/opampsupervisor/supervisor/supervisor_test.go index 18af43891017..22c970ae3a5e 100644 --- a/cmd/opampsupervisor/supervisor/supervisor_test.go +++ b/cmd/opampsupervisor/supervisor/supervisor_test.go @@ -1394,7 +1394,7 @@ capabilities: _, err = config.Load(cfgPath) require.Error(t, err) - require.True(t, strings.HasPrefix(err.Error(), "cannot parse")) + require.ErrorContains(t, err, "cannot parse") fmt.Println(err.Error()) From eebec371f6377fb7a96e45c7a0fdbce552873800 Mon Sep 17 00:00:00 2001 From: Alok Kumar Singh Date: Sun, 24 Nov 2024 15:24:01 +0530 Subject: [PATCH 6/9] minor test change Signed-off-by: Alok Kumar Singh --- cmd/opampsupervisor/supervisor/supervisor_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/cmd/opampsupervisor/supervisor/supervisor_test.go b/cmd/opampsupervisor/supervisor/supervisor_test.go index ce2068e1436c..24301fc19626 100644 --- a/cmd/opampsupervisor/supervisor/supervisor_test.go +++ b/cmd/opampsupervisor/supervisor/supervisor_test.go @@ -1390,8 +1390,6 @@ capabilities: require.Error(t, err) require.ErrorContains(t, err, "cannot parse") - fmt.Println(err.Error()) - t.Cleanup(func() { require.NoError(t, os.Chmod(tmpDir, 0o700)) require.NoError(t, os.RemoveAll(tmpDir)) From c2a1903ef78ec903c8973b9e398e2cbda711be99 Mon Sep 17 00:00:00 2001 From: Alok Kumar Singh Date: Mon, 25 Nov 2024 19:16:07 +0530 Subject: [PATCH 7/9] pr checks fix Signed-off-by: Alok Kumar Singh --- cmd/opampsupervisor/go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/opampsupervisor/go.mod b/cmd/opampsupervisor/go.mod index 3ad15a864b84..ed47a34cc97c 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 From 5548a22b7133993ac33206d7731620ca5623533e Mon Sep 17 00:00:00 2001 From: Alok Kumar Singh <62210712+akstron@users.noreply.github.com> Date: Mon, 25 Nov 2024 23:15:31 +0530 Subject: [PATCH 8/9] Update .chloggen/strict-unmarshal.yaml Co-authored-by: Evan Bradley <11745660+evan-bradley@users.noreply.github.com> --- .chloggen/strict-unmarshal.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.chloggen/strict-unmarshal.yaml b/.chloggen/strict-unmarshal.yaml index 594732bc90f2..48670f656254 100644 --- a/.chloggen/strict-unmarshal.yaml +++ b/.chloggen/strict-unmarshal.yaml @@ -7,7 +7,7 @@ change_type: 'enhancement' component: opampsupervisor # A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). -note: "The change enables strict unmarshal of config file for opampsupervisor. Post this change, an error would be returned while unmarshalling a config file where a key exist which doesn't configure anything" +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] From 8d45ba8f7bc286f3beecc69e61a7a5daf5967161 Mon Sep 17 00:00:00 2001 From: Alok Kumar Singh <62210712+akstron@users.noreply.github.com> Date: Mon, 25 Nov 2024 23:16:00 +0530 Subject: [PATCH 9/9] Update .chloggen/strict-unmarshal.yaml Co-authored-by: Evan Bradley <11745660+evan-bradley@users.noreply.github.com> --- .chloggen/strict-unmarshal.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.chloggen/strict-unmarshal.yaml b/.chloggen/strict-unmarshal.yaml index 48670f656254..eef42e520340 100644 --- a/.chloggen/strict-unmarshal.yaml +++ b/.chloggen/strict-unmarshal.yaml @@ -1,7 +1,7 @@ # Use this changelog template to create an entry for release notes. # One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' -change_type: 'enhancement' +change_type: 'breaking' # The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) component: opampsupervisor