Skip to content

Commit

Permalink
use correct annotation key validation pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
bakito committed Nov 6, 2020
1 parent 9e46538 commit b9b0fda
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
28 changes: 20 additions & 8 deletions api/v1/eventlogger_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import (
)

var (
keyPattern = regexp.MustCompile(`^([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]$`)
labelValuePattern = regexp.MustCompile(`^(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?$`)
annotationKeyPattern = regexp.MustCompile(`^([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9](\/([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?$`)
labelKeyPattern = regexp.MustCompile(`^([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]$`)
labelValuePattern = regexp.MustCompile(`^(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?$`)
)

// Custom type for context key, so we don't have to use 'string' directly
Expand Down Expand Up @@ -50,10 +51,21 @@ func k8sLabelValues(_ context.Context, fl validator.FieldLevel) bool {
return true
}

func k8sKeys(_ context.Context, fl validator.FieldLevel) bool {
func k8sLabelKeys(_ context.Context, fl validator.FieldLevel) bool {
if annotations, ok := fl.Field().Interface().(map[string]string); ok {
for k := range annotations {
if !keyPattern.MatchString(k) {
if !labelKeyPattern.MatchString(k) {
return false
}
}
}
return true
}

func k8sAnnotationKeys(_ context.Context, fl validator.FieldLevel) bool {
if annotations, ok := fl.Field().Interface().(map[string]string); ok {
for k := range annotations {
if !annotationKeyPattern.MatchString(k) {
return false
}
}
Expand Down Expand Up @@ -86,9 +98,9 @@ func (err *eventLoggerValidatorError) addError(errStr string) {
func newEventLoggerValidator(spec *EventLoggerSpec) *eventLoggerValidator {
result := validator.New()

_ = result.RegisterValidationCtx("k8s-label-keys", k8sKeys)
_ = result.RegisterValidationCtx("k8s-label-keys", k8sLabelKeys)
_ = result.RegisterValidationCtx("k8s-label-values", k8sLabelValues)
_ = result.RegisterValidationCtx("k8s-annotation-keys", k8sKeys)
_ = result.RegisterValidationCtx("k8s-annotation-keys", k8sAnnotationKeys)

// context
ctx := context.WithValue(context.Background(), specKey, spec)
Expand All @@ -107,15 +119,15 @@ func newEventLoggerValidator(spec *EventLoggerSpec) *eventLoggerValidator {
}{
{
tag: "k8s-label-keys",
translation: fmt.Sprintf("'key in {0}' must match the pattern %s", keyPattern.String()),
translation: fmt.Sprintf("'key in {0}' must match the pattern %s", labelKeyPattern.String()),
},
{
tag: "k8s-label-values",
translation: fmt.Sprintf("'values in {0}' must match the pattern %s", labelValuePattern.String()),
},
{
tag: "k8s-annotation-keys",
translation: fmt.Sprintf("'key in {0}' must match the pattern %s", keyPattern.String()),
translation: fmt.Sprintf("'key in {0}' must match the pattern %s", annotationKeyPattern.String()),
},
}
for _, t := range translations {
Expand Down
7 changes: 5 additions & 2 deletions api/v1/eventlogger_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func Test_Validate_Success(t *testing.T) {
s := &EventLoggerSpec{
Annotations: map[string]string{"valid": "valid"},
Annotations: map[string]string{"valid/valid": "valid", "valid": "valid"},
Labels: map[string]string{"valid": "valid"},
}

Expand All @@ -36,6 +36,9 @@ func Test_Validate_Invalid_AnnotationKey(t *testing.T) {
s := &EventLoggerSpec{
Annotations: map[string]string{"in valid": "valid"},
}

Assert(t, s.Validate() != nil)
s = &EventLoggerSpec{
Annotations: map[string]string{"in/valid/": "valid"},
}
Assert(t, s.Validate() != nil)
}

0 comments on commit b9b0fda

Please sign in to comment.