Alerting: Update provisioning to validate user-defined UID on create (#73793)
* add ValidateUID to util * provisioning to validate UID on rule creation --------- Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com> Co-authored-by: Alexander Weaver <weaver.alex.d@gmail.com>
This commit is contained in:
co-authored by
brendamuir
Alexander Weaver
parent
9c50296a07
commit
99fd7b8141
@@ -1,6 +1,8 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"regexp"
|
||||
"sync"
|
||||
@@ -9,10 +11,18 @@ import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const MaxUIDLength = 40
|
||||
|
||||
var uidrand = rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
var alphaRunes = []rune("abcdefghijklmnopqrstuvwxyz")
|
||||
var hexLetters = []rune("abcdef")
|
||||
|
||||
var (
|
||||
ErrUIDTooLong = fmt.Errorf("UID is longer than %d symbols", MaxUIDLength)
|
||||
ErrUIDFormatInvalid = errors.New("invalid format of UID. Only letters, numbers, '-' and '_' are allowed")
|
||||
ErrUIDEmpty = fmt.Errorf("UID is empty")
|
||||
)
|
||||
|
||||
// We want to protect our number generator as they are not thread safe. Not using
|
||||
// the mutex could result in panics in certain cases where UIDs would be generated
|
||||
// at the same time.
|
||||
@@ -29,7 +39,7 @@ func IsValidShortUID(uid string) bool {
|
||||
|
||||
// IsShortUIDTooLong checks if short unique identifier is too long
|
||||
func IsShortUIDTooLong(uid string) bool {
|
||||
return len(uid) > 40
|
||||
return len(uid) > MaxUIDLength
|
||||
}
|
||||
|
||||
// GenerateShortUID will generate a UUID that can also be a k8s name
|
||||
@@ -51,3 +61,17 @@ func GenerateShortUID() string {
|
||||
}
|
||||
return uuid
|
||||
}
|
||||
|
||||
// ValidateUID checks the format and length of the string and returns error if it does not pass the condition
|
||||
func ValidateUID(uid string) error {
|
||||
if len(uid) == 0 {
|
||||
return ErrUIDEmpty
|
||||
}
|
||||
if IsShortUIDTooLong(uid) {
|
||||
return ErrUIDTooLong
|
||||
}
|
||||
if !IsValidShortUID(uid) {
|
||||
return ErrUIDFormatInvalid
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"cuelang.org/go/pkg/strings"
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/require"
|
||||
"k8s.io/apimachinery/pkg/util/validation"
|
||||
@@ -84,3 +85,47 @@ func TestIsShortUIDTooLong(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateUID(t *testing.T) {
|
||||
var tests = []struct {
|
||||
name string
|
||||
uid string
|
||||
expected error
|
||||
}{
|
||||
{
|
||||
name: "no error when string is of correct length",
|
||||
uid: "f8cc010c-ee72-4681-89d2-d46e1bd47d33",
|
||||
expected: nil,
|
||||
},
|
||||
{
|
||||
name: "error when string is empty",
|
||||
uid: "",
|
||||
expected: ErrUIDEmpty,
|
||||
},
|
||||
{
|
||||
name: "error when string is too long",
|
||||
uid: strings.Repeat("1", MaxUIDLength+1),
|
||||
expected: ErrUIDTooLong,
|
||||
},
|
||||
{
|
||||
name: "error when string has invalid characters",
|
||||
uid: "f8cc010c.ee72.4681;89d2+d46e1bd47d33",
|
||||
expected: ErrUIDFormatInvalid,
|
||||
},
|
||||
{
|
||||
name: "error when string has only whitespaces",
|
||||
uid: " ",
|
||||
expected: ErrUIDFormatInvalid,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := ValidateUID(tt.uid)
|
||||
if tt.expected == nil {
|
||||
require.NoError(t, err)
|
||||
} else {
|
||||
require.ErrorIs(t, err, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user