From a20197229e833ea8c75877d8f5bf9a46cc2a4755 Mon Sep 17 00:00:00 2001 From: Matthew Jacobson Date: Thu, 18 Apr 2024 21:08:38 -0400 Subject: [PATCH] Alerting: Prevent simplified routing zero duration GroupInterval and RepeatInterval (#86561) Prevent zero duration GroupInterval and RepeatInterval --- pkg/services/ngalert/models/notifications.go | 8 ++++---- pkg/services/ngalert/models/notifications_test.go | 10 ++++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/pkg/services/ngalert/models/notifications.go b/pkg/services/ngalert/models/notifications.go index 8d40c5d88b1..895a70da0a6 100644 --- a/pkg/services/ngalert/models/notifications.go +++ b/pkg/services/ngalert/models/notifications.go @@ -81,11 +81,11 @@ func (s *NotificationSettings) Validate() error { if s.GroupWait != nil && *s.GroupWait < 0 { return errors.New("group wait must be a positive duration") } - if s.GroupInterval != nil && *s.GroupInterval < 0 { - return errors.New("group interval must be a positive duration") + if s.GroupInterval != nil && *s.GroupInterval <= 0 { + return errors.New("group interval must be greater than zero") } - if s.RepeatInterval != nil && *s.RepeatInterval < 0 { - return errors.New("repeat interval must be a positive duration") + if s.RepeatInterval != nil && *s.RepeatInterval <= 0 { + return errors.New("repeat interval must be greater than zero") } return nil } diff --git a/pkg/services/ngalert/models/notifications_test.go b/pkg/services/ngalert/models/notifications_test.go index 14e3a3e89bb..4cdf589614c 100644 --- a/pkg/services/ngalert/models/notifications_test.go +++ b/pkg/services/ngalert/models/notifications_test.go @@ -74,6 +74,11 @@ func TestValidate(t *testing.T) { notificationSettings: CopyNotificationSettings(validNotificationSettings(), NSMuts.WithGroupInterval(util.Pointer(-1*time.Second))), expErrorContains: "group interval", }, + { + name: "group interval zero is invalid", + notificationSettings: CopyNotificationSettings(validNotificationSettings(), NSMuts.WithGroupInterval(util.Pointer(0*time.Second))), + expErrorContains: "group interval", + }, { name: "repeat interval empty is valid", notificationSettings: CopyNotificationSettings(validNotificationSettings(), NSMuts.WithRepeatInterval(nil)), @@ -87,6 +92,11 @@ func TestValidate(t *testing.T) { notificationSettings: CopyNotificationSettings(validNotificationSettings(), NSMuts.WithRepeatInterval(util.Pointer(-1*time.Second))), expErrorContains: "repeat interval", }, + { + name: "repeat interval zero is invalid", + notificationSettings: CopyNotificationSettings(validNotificationSettings(), NSMuts.WithRepeatInterval(util.Pointer(0*time.Second))), + expErrorContains: "repeat interval", + }, } for _, tt := range testCases {