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

* 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>
This commit is contained in:
Jean-Philippe Quéméner 2022-06-27 18:57:47 +02:00 committed by GitHub
parent 0e1f0dd8f5
commit bf255965a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 0 deletions

View File

@ -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

View File

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