Skip to content

Commit 31311bf

Browse files
authored
Merge pull request #12 from suhasgumma/C-0045
ValidatingAdmissionPolicy for C-0045
2 parents 1f85e8c + cecdf8c commit 31311bf

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

controls/C-0045/policy.yaml

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
apiVersion: admissionregistration.k8s.io/v1alpha1
2+
kind: ValidatingAdmissionPolicy
3+
metadata:
4+
name: "kubescape-c-0045-deny-workloads-with-hostpath-volumes-readonly-not-false"
5+
spec:
6+
failurePolicy: Fail
7+
matchConstraints:
8+
resourceRules:
9+
- apiGroups: [""]
10+
apiVersions: ["v1"]
11+
operations: ["CREATE", "UPDATE"]
12+
resources: ["pods"]
13+
- apiGroups: ["apps"]
14+
apiVersions: ["v1"]
15+
operations: ["CREATE", "UPDATE"]
16+
resources: ["deployments","replicasets","daemonsets","statefulsets"]
17+
- apiGroups: ["batch"]
18+
apiVersions: ["v1"]
19+
operations: ["CREATE", "UPDATE"]
20+
resources: ["jobs","cronjobs"]
21+
validations:
22+
- expression: >
23+
object.kind != 'Pod' || object.spec.volumes.all(vol, !(has(vol.hostPath)) ||
24+
object.spec.containers.all(container, !(has(container.volumeMounts)) || container.volumeMounts.all(
25+
containerVol, containerVol.name != vol.name ||
26+
(has(containerVol.readOnly) && containerVol.readOnly == true)
27+
)))
28+
message: "One or more hostPath Volumes in the Pod has readOnly not set to false! (see more at https://hub.armosec.io/docs/c-0045)"
29+
- expression: >
30+
['Deployment','ReplicaSet','DaemonSet','StatefulSet','Job'].all(kind, object.kind != kind) || object.spec.template.spec.volumes.all(vol, !(has(vol.hostPath)) ||
31+
object.spec.template.spec.containers.all(container, !(has(container.volumeMounts)) || container.volumeMounts.all(
32+
containerVol, containerVol.name != vol.name ||
33+
(has(containerVol.readOnly) && containerVol.readOnly == true)
34+
)))
35+
message: "One or more hostPath Volumes in the Workload has readOnly not set to false! (see more at https://hub.armosec.io/docs/c-0045)"
36+
- expression: >
37+
object.kind != 'CronJob' || object.spec.jobTemplate.spec.volumes.all(vol, !(has(vol.hostPath)) ||
38+
object.spec.jobTemplate.spec.containers.all(container, !(has(container.volumeMounts)) || container.volumeMounts.all(
39+
containerVol, containerVol.name != vol.name ||
40+
(has(containerVol.readOnly) && containerVol.readOnly == true)
41+
)))
42+
message: "One or more hostPath Volumes in the CronJob has readOnly not set to false! (see more at https://hub.armosec.io/docs/c-0045)"

controls/C-0045/tests.json

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
[
2+
{
3+
"name": "Pod with no hostPath Volumes allowed",
4+
"template": "pod.yaml",
5+
"expected": "pass",
6+
"field_change_list": [
7+
]
8+
},
9+
{
10+
"name": "Pod with hostPath volume and readOnly is not set for corresponding volumeMount is denied",
11+
"template": "pod.yaml",
12+
"expected": "fail",
13+
"field_change_list": [
14+
"spec.volumes.[0].hostPath.path=/var"
15+
]
16+
},
17+
{
18+
"name": "Pod with hostPath volume and readOnly is set to false for corresponding volumeMount is denied",
19+
"template": "pod.yaml",
20+
"expected": "fail",
21+
"field_change_list": [
22+
"spec.volumes.[0].hostPath.path=/var",
23+
"spec.containers.[0].volumeMounts.[0].readOnly=false"
24+
]
25+
},
26+
{
27+
"name": "Pod with hostPath volume and readOnly is set to true for corresponding volumeMount is allowed",
28+
"template": "pod.yaml",
29+
"expected": "pass",
30+
"field_change_list": [
31+
"spec.volumes.[0].hostPath.path=/var",
32+
"spec.containers.[0].volumeMounts.[0].readOnly=true"
33+
]
34+
},
35+
{
36+
"name": "Deployment with no hostPath Volumes allowed",
37+
"template": "deployment.yaml",
38+
"expected": "pass",
39+
"field_change_list": [
40+
]
41+
},
42+
{
43+
"name": "Deployment with hostPath volume and readOnly is not set for corresponding volumeMount is denied",
44+
"template": "deployment.yaml",
45+
"expected": "fail",
46+
"field_change_list": [
47+
"spec.template.spec.volumes.[0].hostPath.path=/var"
48+
]
49+
},
50+
{
51+
"name": "Deployment with hostPath volume and readOnly is set to false for corresponding volumeMount is denied",
52+
"template": "deployment.yaml",
53+
"expected": "fail",
54+
"field_change_list": [
55+
"spec.template.spec.volumes.[0].hostPath.path=/var",
56+
"spec.template.spec.containers.[0].volumeMounts.[0].readOnly=false"
57+
]
58+
},
59+
{
60+
"name": "Deployment with hostPath volume and readOnly is set to true for corresponding volumeMount is allowed",
61+
"template": "deployment.yaml",
62+
"expected": "pass",
63+
"field_change_list": [
64+
"spec.template.spec.volumes.[0].hostPath.path=/var",
65+
"spec.template.spec.containers.[0].volumeMounts.[0].readOnly=true"
66+
]
67+
}
68+
]

0 commit comments

Comments
 (0)