mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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:
parent
0e1f0dd8f5
commit
bf255965a2
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user