From 3cd7eac1373a19f4f91c1e44ee169bf3ffbda377 Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Mon, 27 Jun 2022 13:11:41 -0400 Subject: [PATCH] Alerting: prevent the use of the same uid across all contact points (#51440) (#51458) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Alerting: prevent the use of the same uid across all contact points * Update pkg/services/ngalert/provisioning/contactpoints.go Co-authored-by: Yuriy Tseretyan Co-authored-by: Yuriy Tseretyan (cherry picked from commit bf255965a2e8575bb6ca07b492a1d0e8e0e5e826) Co-authored-by: Jean-Philippe Quéméner --- pkg/services/ngalert/provisioning/contactpoints.go | 9 +++++++++ .../ngalert/provisioning/contactpoints_test.go | 13 +++++++++++++ 2 files changed, 22 insertions(+) diff --git a/pkg/services/ngalert/provisioning/contactpoints.go b/pkg/services/ngalert/provisioning/contactpoints.go index 411ca8ef2b9..879666d5f4c 100644 --- a/pkg/services/ngalert/provisioning/contactpoints.go +++ b/pkg/services/ngalert/provisioning/contactpoints.go @@ -146,6 +146,15 @@ func (ecp *ContactPointService) CreateContactPoint(ctx context.Context, orgID in receiverFound := false for _, receiver := range revision.cfg.AlertmanagerConfig.Receivers { + // check if uid is already used in receiver + for _, rec := range receiver.PostableGrafanaReceivers.GrafanaManagedReceivers { + if grafanaReceiver.UID == rec.UID { + return apimodels.EmbeddedContactPoint{}, fmt.Errorf( + "receiver configuration with UID '%s' already exist in contact point '%s'. Please use unique identifiers for receivers across all contact points", + rec.UID, + rec.Name) + } + } if receiver.Name == contactPoint.Name { receiver.PostableGrafanaReceivers.GrafanaManagedReceivers = append(receiver.PostableGrafanaReceivers.GrafanaManagedReceivers, grafanaReceiver) receiverFound = true diff --git a/pkg/services/ngalert/provisioning/contactpoints_test.go b/pkg/services/ngalert/provisioning/contactpoints_test.go index 730408b7df0..17a24735a34 100644 --- a/pkg/services/ngalert/provisioning/contactpoints_test.go +++ b/pkg/services/ngalert/provisioning/contactpoints_test.go @@ -58,6 +58,19 @@ func TestContactPointService(t *testing.T) { require.Equal(t, customUID, cps[1].UID) }) + t.Run("it's not possbile to use the same uid twice", func(t *testing.T) { + customUID := "1337" + sut := createContactPointServiceSut(secretsService) + newCp := createTestContactPoint() + newCp.UID = customUID + + _, err := sut.CreateContactPoint(context.Background(), 1, newCp, models.ProvenanceAPI) + require.NoError(t, err) + + _, err = sut.CreateContactPoint(context.Background(), 1, newCp, models.ProvenanceAPI) + require.Error(t, err) + }) + t.Run("create rejects contact points that fail validation", func(t *testing.T) { sut := createContactPointServiceSut(secretsService) newCp := createTestContactPoint()