Skip to content

Commit 447c4e5

Browse files
authored
Merge pull request #16 from suhasgumma/C-0061
ValidatingAdmissionPolicy for C-0061
2 parents de600fa + 53e5992 commit 447c4e5

File tree

3 files changed

+79
-2
lines changed

3 files changed

+79
-2
lines changed

controls/C-0061/policy.yaml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
apiVersion: admissionregistration.k8s.io/v1alpha1
2+
kind: ValidatingAdmissionPolicy
3+
metadata:
4+
name: "kubescape-c-0061-deny-workloads-in-default-namespace"
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: "['Pod','Deployment','ReplicaSet','DaemonSet','StatefulSet','Job', 'CronJob'].all(kind, object.kind != kind) || (has(object.metadata.namespace) && object.metadata.namespace != 'default')"
23+
message: "Workloads with privileged containers are not allowed! (see more at https://hub.armosec.io/docs/c-0016)"

controls/C-0061/tests.json

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
[
2+
{
3+
"name": "Pod with namespace not set is denied",
4+
"template": "pod.yaml",
5+
"expected": "fail",
6+
"field_change_list": [
7+
]
8+
},
9+
{
10+
"name": "Pod with namespace set to default is denied",
11+
"template": "pod.yaml",
12+
"expected": "fail",
13+
"field_change_list": [
14+
"metadata.namespace=default"
15+
]
16+
},
17+
{
18+
"name": "Pod with namespace set to value other than default is allowed",
19+
"template": "pod.yaml",
20+
"expected": "pass",
21+
"field_change_list": [
22+
"metadata.namespace=test-namespace"
23+
]
24+
},
25+
{
26+
"name": "Deployment with namespace not set is denied",
27+
"template": "deployment.yaml",
28+
"expected": "fail",
29+
"field_change_list": [
30+
]
31+
},
32+
{
33+
"name": "Deployment with namespace set to default is denied",
34+
"template": "deployment.yaml",
35+
"expected": "fail",
36+
"field_change_list": [
37+
"metadata.namespace=default"
38+
]
39+
},
40+
{
41+
"name": "Deployment with namespace set to value other than default is allowed",
42+
"template": "deployment.yaml",
43+
"expected": "pass",
44+
"field_change_list": [
45+
"metadata.namespace=test-namespace"
46+
]
47+
}
48+
]

scripts/run-control-tests.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
with open('tests.json', 'r') as f:
3030
tests = json.load(f)
3131

32+
# Create a test namespace
33+
subprocess.check_call(['kubectl', 'create', 'namespace','test-namespace'])
34+
3235
# Apply the configuraton CRD
3336
subprocess.check_call(['kubectl', 'apply', '-f', os.path.join(CONFIGURATION_DIR, 'policy-configuration-definition.yaml')])
3437

@@ -102,7 +105,7 @@
102105
print(colored('Test passed!','green'))
103106

104107
print(colored('Cleaning up...', 'yellow'))
105-
# Run kubectl delete on the policy and policy binding
108+
# Run kubectl delete on the policy and policy binding.
106109
try:
107110
subprocess.check_call(['kubectl', 'delete', '-f', 'policy.yaml'],stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
108111
subprocess.check_call(['kubectl', 'delete', '-f', policy_bind_temp_file_name],stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
@@ -127,7 +130,10 @@
127130
print('-'*120)
128131
print('')
129132

130-
133+
134+
# Delete the test namespace
135+
subprocess.check_call(['kubectl', 'delete', 'namespaces', 'test-namespace'],stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
136+
131137
if all_tests_passed:
132138
print(colored('Control tests passed!','green'))
133139
sys.exit(0)

0 commit comments

Comments
 (0)