Skip to content

Commit

Permalink
Merge pull request #32 from raspbernetes/feature/#26-cis-benchmark-5-1
Browse files Browse the repository at this point in the history
CIS Policy 5.1.1 -  disallow clusterrole binding to cluster-admin
  • Loading branch information
saurabhpandit authored Jun 12, 2020
2 parents eb88cd0 + 694c5b5 commit b3219e1
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
20 changes: 20 additions & 0 deletions policies/CIS.5.1.1.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cis_5_1_1

import data.lib.kubernetes

violation[msg] {
kubernetes.clusterrolebindings[clusterrolebinding]
is_clusterrole_admin(clusterrolebinding)
msg = kubernetes.format(sprintf("ClusterRoleBinding %v - Binding to cluster-admin role is not allowed", [clusterrolebinding.metadata.name]))
}

violation[msg] {
kubernetes.rolebindings[rolebinding]
is_clusterrole_admin(rolebinding)
msg = kubernetes.format(sprintf("RoleBinding %v - Binding to cluster-admin role is not allowed", [rolebinding.metadata.name]))
}

is_clusterrole_admin(rolebinding) {
rolebinding.roleRef.name == "cluster-admin"
startswith(rolebinding.metadata.name, "system:") == false
}
47 changes: 47 additions & 0 deletions policies/CIS.5.1.1_test.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cis_5_1_1

import data.lib.test

test_violation {
test.violations(violation) with input as policy_input("ClusterRoleBinding", "example:view:binding", "cluster-admin")
}

test_violation_2 {
test.violations(violation) with input as policy_input("RoleBinding", "example:view:binding", "cluster-admin")
}

test_no_violation {
test.no_violations(violation) with input as policy_input("ClusterRoleBinding", "system:cluster-admin", "cluster-admin")
}

test_no_violation_2 {
test.no_violations(violation) with input as policy_input("RoleBinding", "system:cluster-admin", "cluster-admin")
}

test_no_violation_3 {
test.no_violations(violation) with input as policy_input("ClusterRoleBinding", "stackdriver:fluentd-gcp", "stackdriver:fluentd-gcp")
}

test_no_violation_4 {
test.no_violations(violation) with input as policy_input("RoleBinding", "stackdriver:fluentd-gcp", "stackdriver:fluentd-gcp")
}

policy_input(rolebindingkind, name, ref) = {
"apiVersion": "rbac.authorization.k8s.io/v1",
"kind": rolebindingkind,
"metadata": {
"name": name
},
"roleRef": {
"apiGroup": "rbac.authorization.k8s.io",
"kind": "ClusterRole",
"name": ref
},
"subjects": [
{
"apiGroup": "rbac.authorization.k8s.io",
"kind": "Group",
"name": "system:masters"
}
]
}
13 changes: 13 additions & 0 deletions policies/lib/kubernetes.rego
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,19 @@ clusterroles[clusterrole] {
clusterrole = object
}

is_clusterrole_binding {
kind = "ClusterRoleBinding"
}

is_clusterrole_binding {
kind = "ClusterRoleBindings"
}

clusterrolebindings[clusterrolebinding] {
is_clusterrole_binding
clusterrolebinding = object
}

pod_containers(pod) = all_containers {
keys = {"containers", "initContainers"}
all_containers = [c | keys[k]; c = pod.spec[k][_]]
Expand Down

0 comments on commit b3219e1

Please sign in to comment.