Skip to content

Commit

Permalink
implement image checks using json schema
Browse files Browse the repository at this point in the history
  • Loading branch information
rbren committed Dec 23, 2019
1 parent 8b20fd9 commit 0f2e5e0
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 28 deletions.
13 changes: 13 additions & 0 deletions checks/pullPolicyNotAlways.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: PullPolicyNotAlways
id: pullPolicyNotAlways
successMessage: Image pull policy is "Always"
failureMessage: Image pull policy should be "Always"
category: Images
target: Container
schema:
'$schema': http://json-schema.org/draft-07/schema
required:
- imagePullPolicy
properties:
imagePullPolicy:
const: Always
18 changes: 18 additions & 0 deletions checks/tagNotSpecified.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: TagNotSpecified
id: tagNotSpecified
successMessage: Image tag is specified
failureMessage: Image tag should be specified
category: Images
target: Container
schema:
'$schema': http://json-schema.org/draft-07/schema
required:
- image
allOf:
- properties:
image:
pattern: ^.+:.+$
- properties:
image:
not:
pattern: ^.+:latest$
27 changes: 0 additions & 27 deletions pkg/validator/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package validator

import (
"fmt"
"strings"

"github.com/fairwindsops/polaris/pkg/config"
"github.com/fairwindsops/polaris/pkg/validator/messages"
Expand Down Expand Up @@ -65,7 +64,6 @@ func ValidateContainer(container *corev1.Container, parentPodResult *PodResult,
panic(err)
}

cv.validateImage(conf, controllerName)
cv.validateNetworking(conf, controllerName)
cv.validateSecurity(conf, controllerName)

Expand Down Expand Up @@ -157,31 +155,6 @@ func (cv *ContainerValidation) validateResourceRange(id, resourceName string, ra
}
}

func (cv *ContainerValidation) validateImage(conf *config.Configuration, controllerName string) {
category := messages.CategoryImages

name := "PullPolicyNotAlways"
if conf.IsActionable(conf.Images, name, controllerName) {
id := config.GetIDFromField(conf.Images, name)
if cv.Container.ImagePullPolicy != corev1.PullAlways {
cv.addFailure(messages.ImagePullPolicyFailure, conf.Images.PullPolicyNotAlways, category, id)
} else {
cv.addSuccess(messages.ImagePullPolicySuccess, category, id)
}
}

name = "TagNotSpecified"
if conf.IsActionable(conf.Images, name, controllerName) {
id := config.GetIDFromField(conf.Images, name)
img := strings.Split(cv.Container.Image, ":")
if len(img) == 1 || img[1] == "latest" {
cv.addFailure(messages.ImageTagFailure, conf.Images.TagNotSpecified, category, id)
} else {
cv.addSuccess(messages.ImageTagSuccess, category, id)
}
}
}

func (cv *ContainerValidation) validateNetworking(conf *config.Configuration, controllerName string) {
category := messages.CategoryNetworking

Expand Down
5 changes: 4 additions & 1 deletion pkg/validator/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,10 @@ func TestValidateImage(t *testing.T) {
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
tt.cv = resetCV(tt.cv)
tt.cv.validateImage(&conf.Configuration{Images: tt.image}, "")
err := applyContainerSchemaChecks(&conf.Configuration{Images: tt.image}, tt.cv.Container, "", conf.Deployments, tt.cv.IsInitContainer, &tt.cv)
if err != nil {
panic(err)
}
assert.Len(t, tt.cv.Errors, len(tt.expected))
assert.ElementsMatch(t, tt.cv.Errors, tt.expected)
})
Expand Down
2 changes: 2 additions & 0 deletions pkg/validator/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ var (
// Container checks
"readinessProbe",
"livenessProbe",
"pullPolicyNotAlways",
"tagNotSpecified",
}
)

Expand Down

0 comments on commit 0f2e5e0

Please sign in to comment.