Alerting: prevent the use of the same uid across all contact points (#51440) (#51458)

* Alerting: prevent the use of the same uid across all contact points

* Update pkg/services/ngalert/provisioning/contactpoints.go

Co-authored-by: Yuriy Tseretyan <yuriy.tseretyan@grafana.com>

Co-authored-by: Yuriy Tseretyan <yuriy.tseretyan@grafana.com>
(cherry picked from commit bf255965a2)

Co-authored-by: Jean-Philippe Quéméner <JohnnyQQQQ@users.noreply.github.com>
This commit is contained in:
Grot (@grafanabot)
2022-06-27 13:11:41 -04:00
committed by GitHub
co-authored by Jean-Philippe Quéméner
parent c23fc1c84e
commit 3cd7eac137
2 changed files with 22 additions and 0 deletions
@@ -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
@@ -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()