Skip to content

Commit

Permalink
Merge pull request #22 from suhasgumma/C-0077
Browse files Browse the repository at this point in the history
ValidatingAdmissionPolicy for  C-0077
  • Loading branch information
slashben committed Jan 16, 2023
2 parents 1cd23ff + 214e0e3 commit 4c51d98
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 0 deletions.
61 changes: 61 additions & 0 deletions controls/C-0077/policy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
apiVersion: admissionregistration.k8s.io/v1alpha1
kind: ValidatingAdmissionPolicy
metadata:
name: "kubescape-c-0077-deny-resources-without-configured-list-of-k8s-common-labels-not-set"
spec:
failurePolicy: Fail
paramKind:
apiVersion: kubescape.io/v1
kind: ControlConfiguration
matchConstraints:
resourceRules:
- apiGroups: [""]
apiVersions: ["v1"]
operations: ["CREATE", "UPDATE"]
resources: ["pods"]
- apiGroups: ["apps"]
apiVersions: ["v1"]
operations: ["CREATE", "UPDATE"]
resources: ["deployments","replicasets","daemonsets","statefulsets"]
- apiGroups: ["batch"]
apiVersions: ["v1"]
operations: ["CREATE", "UPDATE"]
resources: ["jobs","cronjobs"]
validations:
- expression: >
object.kind != 'Pod' ||
(
has(object.metadata.labels) &&
!(object.metadata.labels.all(label, params.settings.k8sRecommendedLabels.all(
labelInList, labelInList != label
)))
)
message: "Pod doesn't have any k8s common label from the configured list! (see more at https://hub.armosec.io/docs/c-0077)"
- expression: >
['Deployment','ReplicaSet','DaemonSet','StatefulSet','Job'].all(kind, object.kind != kind) ||
(
has(object.metadata.labels) &&
!(object.metadata.labels.all(label, params.settings.k8sRecommendedLabels.all(
labelInList, labelInList != label
))) &&
has(object.spec.template.metadata) &&
has(object.spec.template.metadata.labels) &&
!(object.spec.template.metadata.labels.all(label, params.settings.k8sRecommendedLabels.all(
labelInList, labelInList != label
)))
)
message: "Workload or Pod in workload doesn't have any k8s common label from the configured list! (see more at https://hub.armosec.io/docs/c-0077)"
- expression: >
object.kind != 'CronJob' ||
(
has(object.metadata.labels) &&
!(object.metadata.labels.all(label, params.settings.k8sRecommendedLabels.all(
labelInList, labelInList != label
))) &&
has(object.spec.jobTemplate.metadata) &&
has(object.spec.jobTemplate.metadata.labels) &&
!(object.spec.jobTemplate.metadata.labels.all(label, params.settings.k8sRecommendedLabels.all(
labelInList, labelInList != label
)))
)
message: "CronJob or Pod in workload doesn't have any k8s common label from the configured list! (see more at https://hub.armosec.io/docs/c-0077)"
45 changes: 45 additions & 0 deletions controls/C-0077/tests.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[
{
"name": "Pod without one of configured common labels is blocked",
"template": "pod.yaml",
"expected": "fail",
"field_change_list": [
]
},
{
"name": "Pod with label \"app.kubernetes.io/name\" is allowed",
"template": "pod-for-list-items.yaml",
"expected": "pass",
"field_change_list": [
]
},
{
"name": "Deployment and its PodSpec without one of configured common labels is blocked",
"template": "deployment.yaml",
"expected": "fail",
"field_change_list": [
]
},
{
"name": "Deployment with label \"app.kubernetes.io/name\" and its PodSpec without one of configured common labels is blocked",
"template": "deployment-with-common-label-1.yaml",
"expected": "fail",
"field_change_list": [
]
},
{
"name": "Deployment without one of configured common labels and its PodSpec with label \"app.kubernetes.io/name\" is blocked",
"template": "deployment-with-common-label-2.yaml",
"expected": "fail",
"field_change_list": [
]
},
{
"name": "Deployment and PodSpec with label \"app.kubernetes.io/name\" is allowed",
"template": "deployment-for-list-items.yaml",
"expected": "pass",
"field_change_list": [
]
}

]
2 changes: 2 additions & 0 deletions test-resources/deployment-for-list-items.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ metadata:
name: test-deployment
labels:
admission-policy-test: abc
app.kubernetes.io/name: myApp
spec:
replicas: 1
selector:
Expand All @@ -13,6 +14,7 @@ spec:
metadata:
labels:
app: test-deployment
app.kubernetes.io/name: myApp
spec:
containers:
- name: sleep
Expand Down
26 changes: 26 additions & 0 deletions test-resources/deployment-with-common-label-1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-deployment
labels:
admission-policy-test: abc
app.kubernetes.io/name: myApp
spec:
replicas: 1
selector:
matchLabels:
app: test-deployment
template:
metadata:
labels:
app: test-deployment
spec:
containers:
- name: sleep
image: alpine
command: ["sudo","sh"]
args: ["-c", "while true; do sleep 1; done"]
securityContext:
capabilities:
add:
- SYS_ADM
26 changes: 26 additions & 0 deletions test-resources/deployment-with-common-label-2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-deployment
labels:
admission-policy-test: abc
spec:
replicas: 1
selector:
matchLabels:
app: test-deployment
template:
metadata:
labels:
app: test-deployment
app.kubernetes.io/name: myApp
spec:
containers:
- name: sleep
image: alpine
command: ["sudo","sh"]
args: ["-c", "while true; do sleep 1; done"]
securityContext:
capabilities:
add:
- SYS_ADM
1 change: 1 addition & 0 deletions test-resources/pod-for-list-items.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ metadata:
name: test-pod
labels:
admission-policy-test: abc
app.kubernetes.io/name: myApp
spec:
containers:
- name: sleep
Expand Down

0 comments on commit 4c51d98

Please sign in to comment.