StaffCoder
Kubernetes
15 modules · 60 challengesSign up
Module 15 · RBAC & ServiceAccounts

ClusterRoles & Scope

mediumClusterRoles & BindingsAuthorization Scope—No data yet

Problem statement

The audit team needs to read every workload in the estate, and the way that happens today is a Role called estate-local-reader copy-pasted into forty namespaces, one identical rule set at a time. Three of those namespaces are here - estate-payments, estate-search and estate-archive - each with its own copy and its own binding. When the auditors asked to also see the nodes, it turned out no copy can grant that at all. Write the rule set once, outside namespaces, and bind it two different ways.

About this exercise

A ClusterRole holds exactly the same kind of rules a Role holds - API groups, resources, verbs - but it is not a namespaced object. That buys two things a Role cannot do.

First, a ClusterRole can name cluster-scoped resources: nodes, namespaces, persistent volumes, and the RBAC objects themselves. These live outside every namespace, so no namespaced Role can ever grant them, no matter how many namespaces you paste it into.

Second, a ClusterRole is bound in two different ways, and the binding decides the scope, not the rules:

  • A ClusterRoleBinding grants the rule set across the whole cluster: every namespace that exists, every namespace created later, and the cluster-scoped resources the rules name.
  • A RoleBinding in one namespace can also point its roleRef at a ClusterRole. Then the same rules apply inside that namespace only - and the cluster-scoped parts of the rule set grant nothing at all, because a namespaced binding has no cluster scope to grant.

That is why one ClusterRole replaces forty pasted Roles. The rules are written once and referenced; a rule that changes changes everywhere, and the readback tells you which bindings share it.

Your own kubeconfig is cluster-admin, so trying an operation yourself proves nothing - you would succeed either way. The honest check is an access review asking the API server whether a named other subject would be allowed. kubectl auth can-i --help covers asking on someone else's behalf, and kubectl create clusterrole --help, kubectl create clusterrolebinding --help and kubectl create rolebinding --help list what each object needs.

How this project is graded

The grader reads the ClusterRole's rules and both bindings' roleRef fields from the live cluster, then asks the API server, as each auditor, what it may read in each of the three namespaces and on the cluster-scoped resource. It confirms the pasted copies are gone and that the group they were bound to has lost its access. Nothing you created is changed. The namespaces and their workloads are re-seeded before each run; the copies you retire stay retired.

What you will build

1. ClusterRole estate-auditor-read - the single shared rule set, read-only:

resources   pods, configmaps          (core API group)
verbs       get, list, watch

resources nodes (core API group, cluster-scoped) verbs get, list

2. ClusterRoleBinding estate-auditor-everywhere - the estate-wide grant:

roleRef    ClusterRole estate-auditor-read
subject    User nina

3. RoleBinding estate-auditor-archive in namespace estate-archive - the same rule set, one namespace only:

roleRef    ClusterRole estate-auditor-read      (the same object, not a copy)
subject    User raj

4. The pasted copies retired. In each of estate-payments, estate-search and estate-archive, the Role estate-local-reader and the RoleBinding estate-local-reader-binding that grants it to the group estate-audit-legacy are removed. When you are done, no Role by that name exists in any of the three namespaces and that group can read nothing.

Step 1 - one rule set, written once

Define the ClusterRole. It is a rule set with no scope of its own - creating it grants nobody anything yet.

cluster_role_defines_the_shared_read_rule_set should pass

Step 2 - the estate-wide grant

Bind it cluster-wide to nina. She then reads the workloads and the settings in all three namespaces, and the nodes as well.

cluster_bound_auditor_reads_every_namespace should pass

Step 3 - the same rules, one namespace

Bind the same ClusterRole inside estate-archive only, to raj. He reads what nina reads, but nowhere else - and the cluster-scoped part of the rule set gives him nothing, because the binding that carries it has no cluster scope.

namespace_bound_auditor_reads_only_its_namespace, cluster_scoped_read_requires_the_cluster_binding and both_bindings_reference_the_same_rule_set should pass

Step 4 - remove the copies

Retire estate-local-reader and its binding in all three namespaces. The auditors keep their access through the shared rule set; the old group loses everything.

pasted_per_namespace_copies_are_retired should pass

When every test passes

You have:

  • One rule set that forty namespaces can reference instead of forty copies to keep in step
  • An auditor reading every namespace in the estate through a single cluster-wide binding
  • The same rules scoped down to one namespace by the binding alone, with no second rule set anywhere
  • A cluster-scoped read that no namespaced binding can hand out, proved on both auditors
  • The pasted copies gone, and the group behind them holding nothing

You do not have yet:

  • Any identity for the automation. Accounts that live in the cluster, the tokens they authenticate with, and the pods that run as them are the next exercise.

Verify

Click Test after every step.

More in RBAC & ServiceAccounts

See the module
Roles & RoleBindingsReplace namespace-wide admin with two namespaced Roles, bind them to a user, a group and a ServiceAccount, and prove with access reviews what is granted, what is refused and what a revoked binding takes away.—No data yetmediumServiceAccounts & TokensGive release automation its own ServiceAccounts, mint tokens for them, call the API as each account to prove where its binding stops, run a pod as one of them, and revoke an inherited account's access.—No data yetmediumLeast-Privilege RolloutPut a namespace on least privilege in one release - Roles for the app team, one auditor ClusterRole bound cluster-wide and namespace-wide, ServiceAccount tokens for the automation, a revoked god-mode binding, and an access report re-derived from the cluster on every run.—No data yetmedium

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.