Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CIS Policy 5.1.1 - disallow clusterrole binding to cluster-admin #32

Merged
merged 3 commits into from
Jun 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -149,6 +149,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