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

[cmd/opampsupervisor] Enable Strict Unmarshal for Supervisor Configuration #36148

Merged
merged 11 commits into from
Nov 27, 2024
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: 'enhancement'
akstron marked this conversation as resolved.
Show resolved Hide resolved

# 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"
akstron marked this conversation as resolved.
Show resolved Hide resolved

# 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))
})
}