Skip to content

Commit 3b3ffb7

Browse files
authored
Merge pull request #23 from suhasgumma/C-0078
ValidatingAdmissionPolicy for C-0078
2 parents e153798 + 7e1afd5 commit 3b3ffb7

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

controls/C-0078/policy.yaml

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
apiVersion: admissionregistration.k8s.io/v1alpha1
2+
kind: ValidatingAdmissionPolicy
3+
metadata:
4+
name: "kubescape-c-0078-only-allow-images-from-allowed-registry"
5+
spec:
6+
failurePolicy: Fail
7+
paramKind:
8+
apiVersion: kubescape.io/v1
9+
kind: ControlConfiguration
10+
matchConstraints:
11+
resourceRules:
12+
- apiGroups: [""]
13+
apiVersions: ["v1"]
14+
operations: ["CREATE", "UPDATE"]
15+
resources: ["pods"]
16+
- apiGroups: ["apps"]
17+
apiVersions: ["v1"]
18+
operations: ["CREATE", "UPDATE"]
19+
resources: ["deployments","replicasets","daemonsets","statefulsets"]
20+
- apiGroups: ["batch"]
21+
apiVersions: ["v1"]
22+
operations: ["CREATE", "UPDATE"]
23+
resources: ["jobs","cronjobs"]
24+
validations:
25+
- expression: >
26+
object.kind != 'Pod' ||
27+
object.spec.containers.all(container, params.settings.imageRepositoryAllowList.exists(registry,
28+
(
29+
(registry == 'docker.io' && !container.image.contains('/')) ||
30+
(container.image.startsWith(registry))
31+
)))
32+
message: "Pods uses an image from a registry that is not in the allow list! (see more at https://hub.armosec.io/docs/c-0078)"
33+
- expression: >
34+
['Deployment','ReplicaSet','DaemonSet','StatefulSet','Job'].all(kind, object.kind != kind) ||
35+
object.spec.template.spec.containers.all(container, params.settings.imageRepositoryAllowList.exists(registry,
36+
(
37+
(registry == 'docker.io' && !container.image.contains('/')) ||
38+
(container.image.startsWith(registry))
39+
)))
40+
message: "Workloads uses an image from a registry that is not in the allow list! (see more at https://hub.armosec.io/docs/c-0078)"
41+
- expression: >
42+
object.kind != 'CronJob' ||
43+
object.spec.jobTemplate.spec.containers.all(container, params.settings.imageRepositoryAllowList.exists(registry,
44+
(
45+
(registry == 'docker.io' && !container.image.contains('/')) ||
46+
(container.image.startsWith(registry))
47+
)))
48+
message: "CronJob uses an image from a registry that is not in the allow list! (see more at https://hub.armosec.io/docs/c-0078)"

controls/C-0078/tests.json

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
[
2+
{
3+
"name": "Pod with image from quay.io is blocked",
4+
"template": "pod.yaml",
5+
"expected": "fail",
6+
"field_change_list": [
7+
"spec.containers.[0].image=quay.io/openshift/origin-cli:latest"
8+
]
9+
},
10+
{
11+
"name": "Pod with image from gcr.io is allowed",
12+
"template": "pod.yaml",
13+
"expected": "pass",
14+
"field_change_list": [
15+
"spec.containers.[0].image=gcr.io/google-containers/busybox"
16+
]
17+
},
18+
{
19+
"name": "Pod with image from docker.io without prefix docker.io/ is allowed",
20+
"template": "pod.yaml",
21+
"expected": "pass",
22+
"field_change_list": [
23+
]
24+
},
25+
{
26+
"name": "Pod with image from docker.io with prefix docker.io/ is allowed",
27+
"template": "pod.yaml",
28+
"expected": "pass",
29+
"field_change_list": [
30+
"spec.containers.[0].image=docker.io/alpine"
31+
]
32+
},
33+
{
34+
"name": "Deployment with image from quay.io is blocked",
35+
"template": "deployment.yaml",
36+
"expected": "fail",
37+
"field_change_list": [
38+
"spec.template.spec.containers.[0].image=quay.io/openshift/origin-cli:latest"
39+
]
40+
},
41+
{
42+
"name": "Deployment with image from gcr.io is allowed",
43+
"template": "deployment.yaml",
44+
"expected": "pass",
45+
"field_change_list": [
46+
"spec.template.spec.containers.[0].image=gcr.io/google-containers/busybox"
47+
]
48+
},
49+
{
50+
"name": "Deployment with image from docker.io without prefix docker.io/ is allowed",
51+
"template": "deployment.yaml",
52+
"expected": "pass",
53+
"field_change_list": [
54+
]
55+
},
56+
{
57+
"name": "Deployment with image from docker.io with prefix docker.io/ is allowed",
58+
"template": "deployment.yaml",
59+
"expected": "pass",
60+
"field_change_list": [
61+
"spec.template.spec.containers.[0].image=docker.io/alpine"
62+
]
63+
}
64+
]

test-resources/default-control-configuration.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ settings:
99
cpuRequestMin: 0.1
1010
imageRepositoryAllowList:
1111
- gcr.io
12+
- docker.io # Not in Kubescape's initial configuration but added to test as this is an edge case.
1213
insecureCapabilities:
1314
- SETPCAP
1415
- NET_ADMIN

0 commit comments

Comments
 (0)