Skip to content

Commit

Permalink
fix: validation mode lower case
Browse files Browse the repository at this point in the history
The check on the validation mode is done by lower casing the input and comparing it to a constant. Unfortunately the constant wasn't entirely lower case. Instead, no lower casing is done anymore, but an exact matching is required.

fixes #1816
  • Loading branch information
phbelitz committed Nov 15, 2024
1 parent 031f034 commit 9c95014
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
3 changes: 1 addition & 2 deletions internal/handler/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"connaisseur/internal/utils"
"context"
"fmt"
"strings"

"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -161,7 +160,7 @@ func ValidateImage(ctx context.Context, in ValidationInput, out chan<- Validatio
logrus.Debugf("validator: %s", validatorName)

// get validation mode
switch strings.ToLower(rule.With.ValidationMode) {
switch rule.With.ValidationMode {
case constants.MutateMode:
validationMode = constants.MutateMode
case constants.ValidateMode:
Expand Down
52 changes: 34 additions & 18 deletions internal/handler/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ func TestValidateWorkloadObject(t *testing.T) {
var testCases = []struct {
newWLO kubernetes.WorkloadObject
out map[string]struct {
img string
err error
img string
validationMode string
err error
}
}{
// test case with one image
Expand All @@ -43,10 +44,11 @@ func TestValidateWorkloadObject(t *testing.T) {
InitContainers: []core.Container{{Image: "nginx"}},
},
map[string]struct {
img string
err error
img string
validationMode string
err error
}{
"nginx": {"index.docker.io/library/nginx:latest", nil},
"nginx": {"index.docker.io/library/nginx:latest", constants.MutateMode, nil},
},
},
// test case with validationMode set to mutate
Expand All @@ -56,11 +58,13 @@ func TestValidateWorkloadObject(t *testing.T) {
InitContainers: []core.Container{{Image: "docker.io/securesystemsengineering/sample"}},
},
map[string]struct {
img string
err error
img string
validationMode string
err error
}{
"docker.io/securesystemsengineering/sample": {
"index.docker.io/securesystemsengineering/sample:latest",
constants.MutateMode,
nil,
},
},
Expand All @@ -72,11 +76,13 @@ func TestValidateWorkloadObject(t *testing.T) {
InitContainers: []core.Container{{Image: "docker.io/securesystemsengineering/sample:v1"}},
},
map[string]struct {
img string
err error
img string
validationMode string
err error
}{
"docker.io/securesystemsengineering/sample:v1": {
"index.docker.io/securesystemsengineering/sample:v1",
constants.ValidateMode,
nil,
},
},
Expand All @@ -89,11 +95,12 @@ func TestValidateWorkloadObject(t *testing.T) {
EphemeralContainers: []core.EphemeralContainer{{EphemeralContainerCommon: core.EphemeralContainerCommon{Image: "debian"}}},
},
map[string]struct {
img string
err error
img string
validationMode string
err error
}{
"nginx": {"index.docker.io/library/nginx:latest", nil},
"debian": {"index.docker.io/library/debian:latest", nil},
"nginx": {"index.docker.io/library/nginx:latest", constants.MutateMode, nil},
"debian": {"index.docker.io/library/debian:latest", constants.MutateMode, nil},
},
},
}
Expand All @@ -108,16 +115,18 @@ func TestValidateWorkloadObject(t *testing.T) {
for idx, tc := range testCases {
voChannel := ValidateWorkloadObject(ctx, &tc.newWLO, &kubernetes.WorkloadObject{})
validatedImages := map[string]struct {
img string
err error
img string
mode string
err error
}{}
containers := tc.newWLO.ConsolidatedContainers()
for range containers {
vo := <-voChannel
validatedImages[vo.RawImage] = struct {
img string
err error
}{vo.NewImage, vo.Error}
img string
mode string
err error
}{vo.NewImage, vo.ValidationMode, vo.Error}
}
assert.Equalf(t, len(tc.out), len(validatedImages), "test case %i", idx+1)
for expectedValidatedImg := range tc.out {
Expand All @@ -130,6 +139,13 @@ func TestValidateWorkloadObject(t *testing.T) {
"test case %i",
idx+1,
)
assert.Equalf(
t,
tc.out[expectedValidatedImg].validationMode,
actualValidatedImg.mode,
"test case %i",
idx+1,
)
assert.Equalf(
t,
tc.out[expectedValidatedImg].err,
Expand Down

0 comments on commit 9c95014

Please sign in to comment.