Skip to content

Commit e153798

Browse files
authored
Merge pull request #19 from suhasgumma/C-0062
ValidatingAdmissionPolicy for C-0062
2 parents 52852c1 + 0f38820 commit e153798

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-2
lines changed

controls/C-0062/policy.yaml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
apiVersion: admissionregistration.k8s.io/v1alpha1
2+
kind: ValidatingAdmissionPolicy
3+
metadata:
4+
name: "kubescape-c-0062-deny-resources-having-containers-with-sudo-in-entrypoint"
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: "object.kind != 'Pod' || object.spec.containers.all(container, !(has(container.command)) || container.command.all(cmd, cmd != 'sudo'))"
23+
message: "Pod has a container/s having sudo in entrypoint! (see more at https://hub.armosec.io/docs/c-0062)"
24+
- expression: "['Deployment','ReplicaSet','DaemonSet','StatefulSet','Job'].all(kind, object.kind != kind) || object.spec.template.spec.containers.all(container, !(has(container.command)) || container.command.all(cmd, cmd != 'sudo'))"
25+
message: "Workload has a container/s having sudo in entrypoint! (see more at https://hub.armosec.io/docs/c-0062)"
26+
- expression: "object.kind != 'CronJob' || object.spec.jobTemplate.spec.containers.all(container, !(has(container.command)) || container.command.all(cmd, cmd != 'sudo'))"
27+
message: "CronJob has a container/s having sudo in entrypoint! (see more at https://hub.armosec.io/docs/c-0062)"

controls/C-0062/tests.json

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[
2+
{
3+
"name": "Pod with container having sudo in command is blocked",
4+
"template": "pod-for-list-items.yaml",
5+
"expected": "fail",
6+
"field_change_list": [
7+
]
8+
},
9+
{
10+
"name": "Pod with container not having sudo in command is allowed",
11+
"template": "pod.yaml",
12+
"expected": "pass",
13+
"field_change_list": [
14+
]
15+
},
16+
{
17+
"name": "Deployment with container having sudo in command is blocked",
18+
"template": "deployment-for-list-items.yaml",
19+
"expected": "fail",
20+
"field_change_list": [
21+
]
22+
},
23+
{
24+
"name": "Deployment with container not having sudo in command is allowed",
25+
"template": "deployment.yaml",
26+
"expected": "pass",
27+
"field_change_list": [
28+
]
29+
}
30+
]

test-resources/deployment-for-list-items.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ spec:
1717
containers:
1818
- name: sleep
1919
image: alpine
20-
command: ["sh"]
20+
command: ["sudo","sh"]
2121
args: ["-c", "while true; do sleep 1; done"]
2222
securityContext:
2323
capabilities:

test-resources/pod-for-list-items.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ spec:
88
containers:
99
- name: sleep
1010
image: alpine
11-
command: ["sh"]
11+
command: ["sudo", sh"]
1212
args: ["-c", "while true; do sleep 1; done"]
1313
securityContext:
1414
capabilities:

0 commit comments

Comments
 (0)