move networking checks over to json schema

This commit is contained in:
Robert Brennan
2019-12-23 17:04:44 +00:00
parent 0f2e5e0def
commit 95c04b1e9d
4 changed files with 29 additions and 24 deletions

24
checks/hostPortSet.yaml Normal file
View File

@@ -0,0 +1,24 @@
name: HostPortSet
id: hostPortSet
successMessage: Host port is not configured
failureMessage: Host port should not be configured
category: Networking
controllers:
exclude:
- Job
- CronJob
containers:
exclude:
- initContainer
target: Container
schema:
'$schema': http://json-schema.org/draft-07/schema
type: object
required:
properties:
ports:
type: array
items:
properties:
hostPort:
const: 0

View File

@@ -64,7 +64,6 @@ func ValidateContainer(container *corev1.Container, parentPodResult *PodResult,
panic(err)
}
cv.validateNetworking(conf, controllerName)
cv.validateSecurity(conf, controllerName)
cRes := ContainerResult{
@@ -155,28 +154,6 @@ func (cv *ContainerValidation) validateResourceRange(id, resourceName string, ra
}
}
func (cv *ContainerValidation) validateNetworking(conf *config.Configuration, controllerName string) {
category := messages.CategoryNetworking
name := "HostPortSet"
if conf.IsActionable(conf.Networking, name, controllerName) {
hostPortSet := false
for _, port := range cv.Container.Ports {
if port.HostPort != 0 {
hostPortSet = true
break
}
}
id := config.GetIDFromField(conf.Networking, name)
if hostPortSet {
cv.addFailure(messages.HostPortFailure, conf.Networking.HostPortSet, category, id)
} else {
cv.addSuccess(messages.HostPortSuccess, category, id)
}
}
}
func (cv *ContainerValidation) validateSecurity(conf *config.Configuration, controllerName string) {
category := messages.CategorySecurity
securityContext := cv.Container.SecurityContext

View File

@@ -650,7 +650,10 @@ func TestValidateNetworking(t *testing.T) {
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
tt.cv = resetCV(tt.cv)
tt.cv.validateNetworking(&conf.Configuration{Networking: tt.networkConf}, "")
err := applyContainerSchemaChecks(&conf.Configuration{Networking: tt.networkConf}, tt.cv.Container, "", conf.Deployments, tt.cv.IsInitContainer, &tt.cv)
if err != nil {
panic(err)
}
assert.Len(t, tt.cv.messages(), len(tt.expectedMessages))
assert.ElementsMatch(t, tt.cv.messages(), tt.expectedMessages)
})

View File

@@ -57,6 +57,7 @@ var (
"livenessProbe",
"pullPolicyNotAlways",
"tagNotSpecified",
"hostPortSet",
}
)