diff --git a/CHANGELOG.md b/CHANGELOG.md index afad9d19f4c..e188dc83e4a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,7 @@ * [BUGFIX] Ring: Ignore READONLY ingesters when assigning tokens so the active series remain balanced. #7946 * [BUGFIX] Distributor: Fix a `panic: send on closed channel` crash during shutdown when `-distributor.num-push-workers` or `-distributor.num-query-workers` is greater than 0. #7964 * [BUGFIX] Ring: Fix `DoBatch` counting a key more than once when more replicas than the quorum succeed, which in rare cases could hide a write error from a key that did not reach quorum. A distributor push now always waits until every series reaches quorum, so it returns the error of a series that misses quorum, and takes as long as its slowest series to reach quorum. #7975 +* [BUGFIX] Alertmanager: Fix per-tenant `alertmanager_receivers_firewall_block_cidr_networks` overrides being appended to the global default list instead of replacing it, so CIDRs a tenant left out of its list stayed blocked. #7831 ## 1.22.0 2026-11-19 diff --git a/pkg/util/flagext/cidr.go b/pkg/util/flagext/cidr.go index bb7a19c537e..bc7e91a43e7 100644 --- a/pkg/util/flagext/cidr.go +++ b/pkg/util/flagext/cidr.go @@ -46,17 +46,19 @@ func (c CIDRSliceCSV) String() string { // Set implements flag.Value func (c *CIDRSliceCSV) Set(s string) error { - parts := strings.SplitSeq(s, ",") + // Build into a fresh slice so the value replaces the previous one and a bad entry leaves it untouched. + var values CIDRSliceCSV - for part := range parts { + for part := range strings.SplitSeq(s, ",") { cidr := &CIDR{} if err := cidr.Set(part); err != nil { return errors.Wrapf(err, "cidr: %s", part) } - *c = append(*c, *cidr) + values = append(values, *cidr) } + *c = values return nil } diff --git a/pkg/util/flagext/cidr_test.go b/pkg/util/flagext/cidr_test.go index 1e1209adfc4..270a34d4bcd 100644 --- a/pkg/util/flagext/cidr_test.go +++ b/pkg/util/flagext/cidr_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "gopkg.in/yaml.v2" ) @@ -49,3 +50,20 @@ func Test_CIDRSliceCSV_YamlMarshalling(t *testing.T) { }) } } + +func Test_CIDRSliceCSV_SetReplacesPreviousValue(t *testing.T) { + c := CIDRSliceCSV{} + require.NoError(t, c.Set("10.0.0.0/8,192.168.0.0/16")) + require.NoError(t, c.Set("172.16.0.0/12")) + + assert.Equal(t, "172.16.0.0/12", c.String()) +} + +func Test_CIDRSliceCSV_SetDoesNotMutateOnError(t *testing.T) { + c := CIDRSliceCSV{} + require.NoError(t, c.Set("10.0.0.0/8")) + + // The first entry parses, the second one doesn't: the value must be left untouched. + require.Error(t, c.Set("192.168.0.0/16,not-a-cidr")) + assert.Equal(t, "10.0.0.0/8", c.String()) +} diff --git a/pkg/util/validation/limits_test.go b/pkg/util/validation/limits_test.go index 6f376085b83..24104c0be89 100644 --- a/pkg/util/validation/limits_test.go +++ b/pkg/util/validation/limits_test.go @@ -1376,3 +1376,25 @@ func TestCloseIdleTSDBTimeout_OverridesAccessor(t *testing.T) { assert.Equal(t, time.Duration(0), ov.CloseIdleTSDBTimeout("tenant-with-zero")) assert.Equal(t, time.Duration(0), ov.CloseIdleTSDBTimeout("unknown-tenant")) } + +func TestAlertmanagerReceiversBlockCIDRNetworksPerTenantOverrideReplacesDefault(t *testing.T) { + defaults := Limits{} + require.NoError(t, defaults.AlertmanagerReceiversBlockCIDRNetworks.Set("10.0.0.0/8,192.168.0.0/16")) + SetDefaultLimitsForYAMLUnmarshalling(defaults) + t.Cleanup(func() { SetDefaultLimitsForYAMLUnmarshalling(Limits{}) }) + + tenantLimits := map[string]*Limits{} + require.NoError(t, yaml.Unmarshal([]byte(` +user1: + alertmanager_receivers_firewall_block_cidr_networks: 172.16.0.0/12 +`), &tenantLimits)) + + ov := NewOverrides(defaults, newMockTenantLimits(tenantLimits)) + + blocked := []string{} + for _, c := range ov.AlertmanagerReceiversBlockCIDRNetworks("user1") { + blocked = append(blocked, c.String()) + } + + assert.Equal(t, []string{"172.16.0.0/12"}, blocked) +}