Skip to content

Commit 67e840e

Browse files
authored
Check runAsRoot on both pod and container levels (#94)
If it is not defined on the pod level, we should still allow a pod that specifies `runAsNonRoot: true` on all containers
1 parent f0dc712 commit 67e840e

File tree

2 files changed

+133
-32
lines changed

2 files changed

+133
-32
lines changed

policy/kubernetes/deny_run_as_root.rego

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,15 @@ exception[rules] {
1414
rules = ["run_container_as_root"]
1515
}
1616

17-
# TODO also check the containers security context, as that takes precedence
17+
podOrContainerRunningAsRoot(pod) {
18+
not pod.spec.securityContext.runAsNonRoot
19+
containers := kubernetes.pod_containers(pod)
20+
container := containers[_]
21+
not container.securityContext.runAsNonRoot
22+
}
23+
1824
deny_run_container_as_root[msg] {
1925
kubernetes.pods[pod]
20-
not pod.spec.securityContext.runAsNonRoot
26+
podOrContainerRunningAsRoot(pod)
2127
msg = sprintf("%s: %s %s is running as root. More info: %s", [check02, kubernetes.kind, kubernetes.name, l.get_url(check02)])
2228
}

policy/kubernetes/deny_run_as_root_test.rego

Lines changed: 125 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,135 @@ package kubernetes
22

33
import data.testing as t
44

5-
basic_deployment := {
6-
"kind": "Deployment",
7-
"metadata": {
8-
"name": "sample",
9-
"namespace":"test",
10-
},
11-
"spec": {
12-
"selector": {
13-
"matchLabels": {
14-
"app": "app",
15-
"release": "release"
16-
}
17-
},
18-
"template": {
19-
"spec": {
20-
"serviceAccountName": "sample",
21-
}
22-
}
23-
}
5+
test_deny_deployment_without_security_context {
6+
input := {
7+
"kind": "Deployment",
8+
"metadata": {
9+
"name": "sample",
10+
"namespace":"test",
11+
},
12+
"spec": {
13+
"selector": {
14+
"matchLabels": {
15+
"app": "app",
16+
"release": "release"
17+
}
18+
},
19+
"template": {
20+
"spec": {
21+
"serviceAccountName": "sample",
22+
"containers": [
23+
{
24+
"name": "test",
25+
"image": "test",
26+
}
27+
]
28+
}
29+
}
30+
}
31+
}
32+
t.error_count(deny_run_container_as_root, 1) with input as input
2433
}
2534

26-
securityContext_patch := {
27-
"op": "add",
28-
"path": "/spec/template/spec/securityContext",
29-
"value": {
30-
"runAsNonRoot": true
31-
}
35+
test_allow_deployment_with_pod_security_context {
36+
input := {
37+
"kind": "Deployment",
38+
"metadata": {
39+
"name": "sample",
40+
"namespace":"test",
41+
},
42+
"spec": {
43+
"selector": {
44+
"matchLabels": {
45+
"app": "app",
46+
"release": "release"
47+
}
48+
},
49+
"template": {
50+
"spec": {
51+
"serviceAccountName": "sample",
52+
"securityContext": {
53+
"runAsNonRoot": true,
54+
},
55+
"containers": [
56+
{
57+
"name": "test",
58+
"image": "test",
59+
}
60+
]
61+
}
62+
}
63+
}
64+
}
65+
t.no_errors(deny_run_container_as_root) with input as input
3266
}
3367

34-
test_deny_deployment_without_security_context {
35-
t.error_count(deny_run_container_as_root, 1) with input as basic_deployment
68+
test_allow_deployment_with_container_security_context {
69+
input := {
70+
"kind": "Deployment",
71+
"metadata": {
72+
"name": "sample",
73+
"namespace":"test",
74+
},
75+
"spec": {
76+
"selector": {
77+
"matchLabels": {
78+
"app": "app",
79+
"release": "release"
80+
}
81+
},
82+
"template": {
83+
"spec": {
84+
"serviceAccountName": "sample",
85+
"containers": [
86+
{
87+
"name": "test",
88+
"image": "test",
89+
"securityContext": {
90+
"runAsNonRoot": true,
91+
},
92+
}
93+
]
94+
}
95+
}
96+
}
97+
}
98+
t.no_errors(deny_run_container_as_root) with input as input
3699
}
37100

38-
test_allow_deployment_with_security_context {
39-
withRunAsNonRoot := json.patch(basic_deployment, [securityContext_patch])
40-
t.no_errors(deny_run_container_as_root) with input as withRunAsNonRoot
101+
test_deny_deployment_with_partial_container_security_context {
102+
input := {
103+
"kind": "Deployment",
104+
"metadata": {
105+
"name": "sample",
106+
"namespace":"test",
107+
},
108+
"spec": {
109+
"selector": {
110+
"matchLabels": {
111+
"app": "app",
112+
"release": "release"
113+
}
114+
},
115+
"template": {
116+
"spec": {
117+
"serviceAccountName": "sample",
118+
"containers": [
119+
{
120+
"name": "test",
121+
"image": "test",
122+
"securityContext": {
123+
"runAsNonRoot": true,
124+
},
125+
},
126+
{
127+
"name": "test",
128+
"image": "test",
129+
}
130+
]
131+
}
132+
}
133+
}
134+
}
135+
t.error_count(deny_run_container_as_root, 1) with input as input
41136
}

0 commit comments

Comments
 (0)