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

Roles & RoleBindings

mediumRoles & RoleBindingsAccess Reviews & Least Privilege—No data yet

Problem statement

Namespace billing-ops runs the invoice API, the settlement worker, the billing settings and the database credentials - and every person and script that touches it holds cluster-admin, because no finer permission was ever defined. A support account belonging to a vendor who finished their contract last quarter still has full control of the namespace. Define the permissions the namespace actually needs, hand them to the right subjects, and take the god-mode away.

About this exercise

Kubernetes authorization is built from two objects that do separate jobs, and keeping them separate is the whole idea.

A Role is a namespaced list of rules, and nothing else. Each rule says: for these API groups, on these resources, allow these verbs - get, list, watch, create, update, patch, delete. Core resources such as pods, configmaps and secrets live in the API group whose name is the empty string. A Role names no people at all; it is a capability sitting on a shelf, and creating one grants nobody anything.

A RoleBinding is what hands that capability out. It has a roleRef - which Role - and a list of subjects. A subject is one of three kinds: a User, a Group, or a ServiceAccount (the only one of the three that is a real object in the cluster; users and groups arrive from whatever authenticates the request, and Kubernetes never stores them). One Role can be handed to many bindings, and one binding can carry many subjects.

Two consequences follow. Permission is additive: there is no deny rule, so a subject can do exactly the union of what its bindings grant, and nothing else is allowed. And permission is live: the API server evaluates the bindings on every request, so deleting a binding removes the access immediately, everywhere, with nothing to restart.

Because your own kubeconfig is cluster-admin, you cannot test any of this by trying it yourself - you would succeed at everything. The honest check is an access review: asking the API server whether a named other subject would be allowed a verb on a resource. kubectl auth can-i --help is the reference for asking on someone else's behalf, and kubectl create role --help and kubectl create rolebinding --help list what each object needs.

How this project is graded

The grader reads the live Role and RoleBinding objects - their rules, their roleRef and their subject lists - and then asks the API server, as each subject you bound, whether the granted operations are allowed and whether the operations you were not asked to grant are refused. It never authenticates as you, and it never changes anything you created. The namespace is re-seeded before each run, so the workloads and the settings come back if you remove them; the vendor binding, once revoked, stays revoked.

What you will build

Everything lives in namespace billing-ops. Nothing here needs a manifest file - the objects in the cluster are what is graded.

1. Role billing-viewer - read-only over the workloads and the settings:

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

2. RoleBinding billing-viewers - hands billing-viewer to two subjects:

User    dana
Group   billing-oncall

3. Role billing-config-editor - full control of the settings and nothing else:

resources   configmaps            (core API group)
verbs       get, list, create, update, delete

4. RoleBinding billing-config-editors - hands billing-config-editor to one subject:

ServiceAccount   report-runner    (in namespace billing-ops)

Neither Role mentions secrets, and neither mentions delete on pods. That absence is the exercise: no rule means no access.

5. answers/revoked.txt - the name of the binding in billing-ops that grants the departed vendor account full control, and that name only. Find it by reading the namespace, then remove the binding.

Step 1 - the read-only capability

Define billing-viewer and bind it to the two subjects. When it is done, dana and anyone in billing-oncall can list the pods and read the settings.

viewer_role_grants_exactly_the_defined_verbs_and_resources and viewer_binding_lists_the_defined_subjects should pass

Step 2 - what the rules do not say

Nothing you granted covers deleting a pod, writing a configmap or touching the database credentials, and nothing you granted reaches another namespace. Confirm the API server refuses all of it for dana.

viewer_subjects_are_denied_ungranted_verbs_and_resources and namespaced_grant_does_not_reach_other_namespaces should pass

Step 3 - a capability for an account

Define billing-config-editor and bind it to the report-runner ServiceAccount, so the reporting job can rewrite the settings without being able to read the credentials beside them.

editor_role_and_binding_grant_configmap_writes_to_the_account should pass

Step 4 - take the god-mode away

Record the vendor binding's name and delete it. The account keeps existing wherever it came from; what disappears is everything it could do here.

revoked_binding_removes_the_former_admin_access should pass

When every test passes

You have:

  • Two capabilities written as rules, each covering exactly what its holder needs
  • A user, a group and a ServiceAccount holding permissions, each through a binding you can read
  • Proof from the API server itself that the operations you did not grant are refused
  • A namespace where the departed vendor account can no longer do anything at all

You do not have yet:

  • Any way to grant the same thing in every namespace at once. A rule set that lives outside namespaces, and the two ways of binding it, is the next exercise.

Verify

Click Test after every step.

More in RBAC & ServiceAccounts

See the module
ClusterRoles & ScopeReplace a Role pasted into every namespace with one ClusterRole, bind it cluster-wide and again inside a single namespace, and prove where each grant reaches and where the cluster-scoped rule stops.—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.