Skip to content

Commit

Permalink
[cmd/opampsupervisor] Enable Strict Unmarshal for Supervisor Configur…
Browse files Browse the repository at this point in the history
…ation (#36148)

<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
#### Description
The changes includes providing a custom `DecoderConfig` with
`ErrorUnused` enabled instead of using the default `DecoderConfig`
provided by `koanf`

<!-- Issue number (e.g. #1234) or full URL to issue, if applicable. -->
#### Link to tracking issue
Fixes: #35838 

<!--Describe what testing was performed and which tests were added.-->

<!--Describe the documentation added.-->

<!--Please delete paragraphs that you did not use before submitting.-->

---------

Signed-off-by: Alok Kumar Singh <dev.alok.singh123@gmail.com>
Co-authored-by: Evan Bradley <11745660+evan-bradley@users.noreply.github.com>
  • Loading branch information
akstron and evan-bradley authored Nov 27, 2024
1 parent 4f5671a commit 6c43968
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
27 changes: 27 additions & 0 deletions .chloggen/strict-unmarshal.yaml
Original file line number Diff line number Diff line change
@@ -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]
2 changes: 1 addition & 1 deletion cmd/opampsupervisor/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
11 changes: 10 additions & 1 deletion cmd/opampsupervisor/supervisor/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down
29 changes: 29 additions & 0 deletions cmd/opampsupervisor/supervisor/supervisor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
}

0 comments on commit 6c43968

Please sign in to comment.