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()