diff --git a/go.mod b/go.mod index daab4218c91..576f6c0614f 100644 --- a/go.mod +++ b/go.mod @@ -197,7 +197,6 @@ require ( go.uber.org/goleak v1.3.0 // @grafana/grafana-search-and-storage go.uber.org/mock v0.6.0 // @grafana/grafana-operator-experience-squad go.uber.org/zap v1.27.0 // @grafana/identity-access-team - go.yaml.in/yaml/v2 v2.4.3 // @grafana/alerting-backend go.yaml.in/yaml/v3 v3.0.4 // @grafana/alerting-backend gocloud.dev v0.43.0 // @grafana/grafana-app-platform-squad gocloud.dev/secrets/hashivault v0.43.0 // @grafana/grafana-operator-experience-squad @@ -628,6 +627,7 @@ require ( go.opentelemetry.io/otel/sdk/metric v1.38.0 // indirect go.opentelemetry.io/proto/otlp v1.7.1 // indirect go.uber.org/multierr v1.11.0 // indirect + go.yaml.in/yaml/v2 v2.4.3 // indirect go4.org/netipx v0.0.0-20230125063823-8449b0a6169f // indirect golang.org/x/sys v0.38.0 // indirect golang.org/x/telemetry v0.0.0-20251111182119-bc8e575c7b54 // indirect diff --git a/pkg/services/ngalert/sender/notifier_test.go b/pkg/services/ngalert/sender/notifier_test.go index e7e9aedec96..a8b452437c1 100644 --- a/pkg/services/ngalert/sender/notifier_test.go +++ b/pkg/services/ngalert/sender/notifier_test.go @@ -28,6 +28,7 @@ import ( "net/http/httptest" "net/url" "strconv" + "strings" "testing" "time" @@ -38,7 +39,7 @@ import ( "github.com/prometheus/common/promslog" "github.com/stretchr/testify/require" "go.uber.org/atomic" - "go.yaml.in/yaml/v2" + "go.yaml.in/yaml/v3" "github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/discovery" @@ -660,11 +661,10 @@ alerting: alertmanagers: - static_configs: ` - err := yaml.UnmarshalStrict([]byte(s), cfg) - require.NoError(t, err, "Unable to load YAML config.") + mustStrictlyDecodeConfig(t, strings.NewReader(s), cfg) require.Len(t, cfg.AlertingConfig.AlertmanagerConfigs, 1) - err = n.ApplyConfig(cfg, nil) + err := n.ApplyConfig(cfg, nil) require.NoError(t, err, "Error applying the config.") tgs := make(map[string][]*targetgroup.Group) @@ -711,11 +711,10 @@ alerting: regex: 'alertmanager:9093' action: drop ` - err := yaml.UnmarshalStrict([]byte(s), cfg) - require.NoError(t, err, "Unable to load YAML config.") + mustStrictlyDecodeConfig(t, strings.NewReader(s), cfg) require.Len(t, cfg.AlertingConfig.AlertmanagerConfigs, 1) - err = n.ApplyConfig(cfg, nil) + err := n.ApplyConfig(cfg, nil) require.NoError(t, err, "Error applying the config.") tgs := make(map[string][]*targetgroup.Group) @@ -1091,7 +1090,7 @@ alerting: - foo.json ` // 1. Ensure known alertmanagers are not dropped during ApplyConfig. - require.NoError(t, yaml.UnmarshalStrict([]byte(s), cfg)) + mustStrictlyDecodeConfig(t, strings.NewReader(s), cfg) require.Len(t, cfg.AlertingConfig.AlertmanagerConfigs, 1) // First, apply the config and reload. @@ -1117,7 +1116,7 @@ alerting: - files: - foo.json ` - require.NoError(t, yaml.UnmarshalStrict([]byte(s), cfg)) + mustStrictlyDecodeConfig(t, strings.NewReader(s), cfg) require.Len(t, cfg.AlertingConfig.AlertmanagerConfigs, 2) require.NoError(t, n.ApplyConfig(cfg, nil)) @@ -1140,7 +1139,7 @@ alerting: - files: - foo.json ` - require.NoError(t, yaml.UnmarshalStrict([]byte(s), cfg)) + mustStrictlyDecodeConfig(t, strings.NewReader(s), cfg) require.Len(t, cfg.AlertingConfig.AlertmanagerConfigs, 2) require.NoError(t, n.ApplyConfig(cfg, nil)) @@ -1167,9 +1166,22 @@ alerting: regex: 'doesntmatter:1234' action: drop ` - require.NoError(t, yaml.UnmarshalStrict([]byte(s), cfg)) + mustStrictlyDecodeConfig(t, strings.NewReader(s), cfg) require.Len(t, cfg.AlertingConfig.AlertmanagerConfigs, 2) require.NoError(t, n.ApplyConfig(cfg, nil)) require.Empty(t, n.Alertmanagers()) } + +// Maintain strict yaml decode behavior from v2: https://github.com/go-yaml/yaml/issues/639#issuecomment-666935833 +func mustStrictlyDecodeConfig(t testing.TB, r io.Reader, cfg *config.Config) { + t.Helper() + + dec := yaml.NewDecoder(r) + dec.KnownFields(true) + + err := dec.Decode(cfg) + if err != nil { + require.Equal(t, io.EOF, err, "Unable to load YAML config.") + } +}