Skip to content

Commit b831883

Browse files
committed
Adding new safeguards for Draft validate
1 parent ccd76b0 commit b831883

File tree

32 files changed

+1598
-0
lines changed

32 files changed

+1598
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: constraints.gatekeeper.sh/v1beta1
2+
kind: K8sAzureV2ContainerAllowedImages
3+
metadata:
4+
name: v2-container-allowed-images
5+
spec:
6+
match:
7+
kinds:
8+
- apiGroups: [""]
9+
kinds: ["Pod"]
10+
parameters:
11+
imageRegex: .*
12+
excludedContainers: []
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
apiVersion: templates.gatekeeper.sh/v1beta1
2+
kind: ConstraintTemplate
3+
metadata:
4+
name: k8sazurev2containerallowedimages
5+
spec:
6+
crd:
7+
spec:
8+
names:
9+
kind: K8sAzureV2ContainerAllowedImages
10+
validation:
11+
# Schema for the `parameters` field
12+
openAPIV3Schema:
13+
properties:
14+
imageRegex:
15+
type: string
16+
excludedContainers:
17+
type: array
18+
items:
19+
type: string
20+
targets:
21+
- target: admission.k8s.gatekeeper.sh
22+
rego: |
23+
package k8sazurev2containerallowedimages
24+
25+
violation[{"msg": msg}] {
26+
container := input_containers[_]
27+
not input_container_excluded(container.name)
28+
not regex.match(input.parameters.imageRegex, container.image)
29+
msg := sprintf("Container image %v for container %v has not been allowed.", [container.image, container.name])
30+
}
31+
32+
input_containers[c] {
33+
c := input.review.object.spec.containers[_]
34+
}
35+
input_containers[c] {
36+
c := input.review.object.spec.initContainers[_]
37+
}
38+
input_containers[c] {
39+
c := input.review.object.spec.ephemeralContainers[_]
40+
}
41+
input_container_excluded(field) {
42+
field == input.parameters.excludedContainers[_]
43+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: constraints.gatekeeper.sh/v1beta1
2+
kind: K8sAzureV2ContainerEnforceProbes
3+
metadata:
4+
name: v2-container-enforce-probes
5+
spec:
6+
match:
7+
kinds:
8+
- apiGroups: [""]
9+
kinds: ["Pod"]
10+
parameters:
11+
enforceProbes : ["readinessProbe","livenessProbe"]
12+
excludedContainers: []
13+
excludedImages: []
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
apiVersion: templates.gatekeeper.sh/v1beta1
2+
kind: ConstraintTemplate
3+
metadata:
4+
name: k8sazurev2containerenforceprobes
5+
spec:
6+
crd:
7+
spec:
8+
names:
9+
kind: K8sAzureV2ContainerEnforceProbes
10+
validation:
11+
openAPIV3Schema:
12+
properties:
13+
enforceProbes:
14+
type: array
15+
items:
16+
type: string
17+
excludedContainers:
18+
type: array
19+
items:
20+
type: string
21+
excludedImages:
22+
description: >-
23+
Any container that uses an image that matches an entry in this list will be excluded
24+
from enforcement. Prefix-matching can be signified with `*`. For example: `my-image-*`.
25+
It is recommended that users use the fully-qualified Docker image name (e.g. start with a domain name)
26+
in order to avoid unexpectedly excluding images from an untrusted repository.
27+
type: array
28+
items:
29+
type: string
30+
targets:
31+
- target: admission.k8s.gatekeeper.sh
32+
rego: |
33+
package k8sazurev2containerenforceprobes
34+
35+
import data.lib.exclude_container_image.is_excluded
36+
37+
# Rule:
38+
## Parameter enforceProbes is one string array that will define which kinds of probes to be enforced for all the containers (init container excludes). The allowed values could be livenessProbes and readinessProbes for now
39+
## Once certain probe is enforces, e.g. livenessProbes, the policy will check on all containers(except init) if they have livenessProbes field. Besides, the probes should at least have defined one of the probe_types, "tcpSocket", "httpGet" or "exec"
40+
41+
probe_type_set = probe_types {
42+
probe_types := {type | type := ["tcpSocket", "httpGet", "exec"][_]}
43+
}
44+
violation[{"msg": msg}] {
45+
container := input_containers[_]
46+
not input_container_excluded(container.name)
47+
not is_excluded(container)
48+
probe := input.parameters.enforceProbes[_]
49+
probe_is_missing(container, probe)
50+
msg := get_violation_message(container, input.review, probe)
51+
}
52+
probe_is_missing(ctr, probe) = true {
53+
not ctr[probe]
54+
}
55+
probe_is_missing(ctr, probe) = true {
56+
probe_field_empty(ctr, probe)
57+
}
58+
probe_field_empty(ctr, probe) = true {
59+
probe_fields := {field | ctr[probe][field]}
60+
diff_fields := probe_type_set - probe_fields
61+
count(diff_fields) == count(probe_type_set)
62+
}
63+
get_violation_message(container, review, probe) = msg {
64+
msg := sprintf("Container <%v> in your Pod <%v> has no <%v>. Required probes: %v", [container.name, review.object.metadata.name, probe, input.parameters.enforceProbes])
65+
}
66+
67+
input_containers[c] {
68+
c := input.review.object.spec.containers[_]
69+
}
70+
input_container_excluded(field) {
71+
field == input.parameters.excludedContainers[_]
72+
}
73+
libs:
74+
- |
75+
package lib.exclude_container_image
76+
is_excluded(container) {
77+
exclude_images := object.get(object.get(input, "parameters", {}), "excludedImages", [])
78+
img := container.image
79+
exclusion := exclude_images[_]
80+
_matches_exclusion(img, exclusion)
81+
}
82+
_matches_exclusion(img, exclusion) {
83+
not endswith(exclusion, "*")
84+
exclusion == img
85+
}
86+
_matches_exclusion(img, exclusion) {
87+
endswith(exclusion, "*")
88+
prefix := trim_suffix(exclusion, "*")
89+
startswith(img, prefix)
90+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
apiVersion: constraints.gatekeeper.sh/v1beta1
2+
kind: K8sAzureV3ContainerLimits
3+
metadata:
4+
name: v3-container-limits
5+
spec:
6+
match:
7+
kinds:
8+
- apiGroups: [""]
9+
kinds: ["Pod"]
10+
parameters:
11+
cpuLimit : "200m"
12+
memoryLimit: "1Gi"
13+
excludedContainers: []
14+
excludedImages: []

0 commit comments

Comments
 (0)