* validate alert notification UID length
* simplify-ish
* remove unused field
* whoopsie
* delete newline
* remove check
* apply feedback
(cherry picked from commit 635fa4ab0f)
Co-authored-by: Will Browne <wbrowne@users.noreply.github.com>
This commit is contained in:
co-authored by
Will Browne
parent
636cb95bc9
commit
80a3be920c
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/notifications"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
)
|
||||
|
||||
type AlertNotificationService struct {
|
||||
@@ -33,6 +34,10 @@ func (s *AlertNotificationService) GetAlertNotifications(ctx context.Context, qu
|
||||
}
|
||||
|
||||
func (s *AlertNotificationService) CreateAlertNotificationCommand(ctx context.Context, cmd *models.CreateAlertNotificationCommand) error {
|
||||
if util.IsShortUIDTooLong(cmd.Uid) {
|
||||
return ValidationError{Reason: "Invalid UID: Must be 40 characters or less"}
|
||||
}
|
||||
|
||||
var err error
|
||||
cmd.EncryptedSecureSettings, err = s.EncryptionService.EncryptJsonData(ctx, cmd.SecureSettings, setting.SecretKey)
|
||||
if err != nil {
|
||||
@@ -53,6 +58,10 @@ func (s *AlertNotificationService) CreateAlertNotificationCommand(ctx context.Co
|
||||
}
|
||||
|
||||
func (s *AlertNotificationService) UpdateAlertNotification(ctx context.Context, cmd *models.UpdateAlertNotificationCommand) error {
|
||||
if util.IsShortUIDTooLong(cmd.Uid) {
|
||||
return ValidationError{Reason: "Invalid UID: Must be 40 characters or less"}
|
||||
}
|
||||
|
||||
var err error
|
||||
cmd.EncryptedSecureSettings, err = s.EncryptionService.EncryptJsonData(ctx, cmd.SecureSettings, setting.SecretKey)
|
||||
if err != nil {
|
||||
@@ -99,6 +108,10 @@ func (s *AlertNotificationService) GetAlertNotificationsWithUid(ctx context.Cont
|
||||
}
|
||||
|
||||
func (s *AlertNotificationService) UpdateAlertNotificationWithUid(ctx context.Context, cmd *models.UpdateAlertNotificationWithUidCommand) error {
|
||||
if util.IsShortUIDTooLong(cmd.Uid) || util.IsShortUIDTooLong(cmd.NewUid) {
|
||||
return ValidationError{Reason: "Invalid UID: Must be 40 characters or less"}
|
||||
}
|
||||
|
||||
return s.SQLStore.UpdateAlertNotificationWithUid(ctx, cmd)
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package alerting
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
@@ -112,6 +113,26 @@ func TestService(t *testing.T) {
|
||||
err = s.DeleteAlertNotification(context.Background(), &delCmd)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("create alert notification should reject an invalid command", func(t *testing.T) {
|
||||
uid := strings.Repeat("A", 41)
|
||||
|
||||
err := s.CreateAlertNotificationCommand(context.Background(), &models.CreateAlertNotificationCommand{Uid: uid})
|
||||
require.ErrorIs(t, err, ValidationError{Reason: "Invalid UID: Must be 40 characters or less"})
|
||||
})
|
||||
|
||||
t.Run("update alert notification should reject an invalid command", func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
uid := strings.Repeat("A", 41)
|
||||
expectedErr := ValidationError{Reason: "Invalid UID: Must be 40 characters or less"}
|
||||
|
||||
err := s.UpdateAlertNotification(ctx, &models.UpdateAlertNotificationCommand{Uid: uid})
|
||||
require.ErrorIs(t, err, expectedErr)
|
||||
|
||||
err = s.UpdateAlertNotificationWithUid(ctx, &models.UpdateAlertNotificationWithUidCommand{NewUid: uid})
|
||||
require.ErrorIs(t, err, expectedErr)
|
||||
})
|
||||
}
|
||||
|
||||
func registerTestNotifier(notifierType string) {
|
||||
|
||||
Reference in New Issue
Block a user