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

Least-Privilege Rollout

mediumLeast-Privilege Access DesignAccess Review & Reporting—No data yet

Problem statement

Namespace access-release goes least-privilege in one release. Today a single binding hands a vendor account full control of it, and everybody else - the app team, the release automation, the compliance auditors - operates through the same door. On release night each of them gets exactly the row the access matrix gives them, the auditors get read access across the estate, the vendor's god-mode is provably gone, and an access report signed off by compliance says so. access-archive is the second namespace the auditors have to cover, and the one place the archive auditor is allowed to look.

About this exercise

This brings together the topic's three exercises:

  • Namespaced permission - Roles as rule sets, RoleBindings as the thing that hands a rule set to a User, a Group or a ServiceAccount, and what a subject is refused because no rule mentions it (Roles & RoleBindings)
  • Scope - one ClusterRole bound cluster-wide by a ClusterRoleBinding and bound into a single namespace by a RoleBinding, and the cluster-scoped resource only the first of those can grant (ClusterRoles & Scope)
  • Identity - ServiceAccounts as identities that live in the cluster, tokens minted for them, and the pod that runs as one (ServiceAccounts & Tokens)

One idea ties them together: the rules say nothing about who; the bindings say nothing about what. Every question about access is answered by joining those two, per request, at the API server. Which is why a least-privilege rollout is not a document - it is a set of objects, and the honest way to check it is to ask the API server itself, subject by subject, verb by verb. Your own kubeconfig is cluster-admin and would succeed at everything, so an access review on someone else's behalf, and a call carrying an account's own minted token, are the only checks worth anything.

That is also why the report has to re-derive. An access report written by hand is a snapshot of what somebody believed on the day they wrote it. A report that reads the bindings and asks the API server every time it runs is the only kind that stays true after the next binding is added.

How this project is graded

The grader reads the live Roles, ClusterRole and bindings, asks the API server as every subject in the matrix whether each operation is allowed, mints its own tokens for the accounts you created and calls the API with them, and reads your pod's spec. It then runs access-report.sh, compares both sections against what the cluster says right now, adds a binding of its own to access-release, runs the report again and requires the new subject to appear, and removes that binding afterwards. Anything the grader creates to test a write is its own throwaway object, removed after the check; nothing you created is changed. Both namespaces are re-seeded before each run, so the workloads and the settings come back if you remove them; the legacy binding, once revoked, stays revoked.

What you will build

Two namespaces are seeded. access-release runs checkout-api and ledger-worker, and holds the ConfigMap release-settings and the Secret release-db-credentials. access-archive runs archive-indexer and holds the ConfigMap archive-settings.

1. The app team's rule sets and bindings, in access-release:

Role release-editor     configmaps  get, list, create, update, delete
                        pods        get, list
Role release-viewer     pods        get, list
                        configmaps  get, list

RoleBinding release-editors roleRef Role release-editor User priya ServiceAccount build-bot (in access-release) RoleBinding release-viewers roleRef Role release-viewer Group release-observers ServiceAccount log-shipper (in access-release)

Neither Role mentions secrets, and neither grants delete on pods.

2. The auditors' shared rule set and its two bindings:

ClusterRole compliance-auditor-read    pods, configmaps  get, list, watch
                                       nodes             get, list

ClusterRoleBinding compliance-auditor-everywhere roleRef ClusterRole compliance-auditor-read User sam RoleBinding compliance-auditor-archive in namespace access-archive roleRef ClusterRole compliance-auditor-read User toby

Both bindings reference that one ClusterRole. There is no second copy of the rules anywhere.

3. The automation's identities, in access-release: ServiceAccounts build-bot and log-shipper, bound as above, and pod release-runner - container runner, image busybox:1.36, staying alive, running as build-bot.

4. The legacy binding revoked. One binding in access-release still gives the user vendor-ops full control of the namespace. Remove it. The account keeps existing; everything it could do here goes.

5. access-report.sh - the report compliance signs off. Two sections, each introduced by its name on a line of its own:

GRANTS
<subject> <namespace> <roleKind>/<roleName>
     one line per subject of every RoleBinding in access-release and in
     access-archive, plus one line per subject of the ClusterRoleBinding
     compliance-auditor-everywhere with * as its namespace. Sorted.

CHECKS <subject> <verb> <resource> <namespace> <yes|no> one line per row of the access matrix below, in the order listed. The verdict is the API server's own answer for that subject, not yours. For a cluster-scoped resource the namespace field is -

A subject is written one of three ways, and the same spelling is used in both sections:

user/<name>                     a User subject
group/<name>                    a Group subject
sa/<namespace>:<name>           a ServiceAccount subject

The access matrix - the sixteen rows of the CHECKS section, in this order:

user/priya                     list    configmaps  access-release
user/priya                     delete  configmaps  access-release
user/priya                     delete  pods        access-release
user/priya                     list    secrets     access-release
group/release-observers        list    pods        access-release
group/release-observers        create  configmaps  access-release
sa/access-release:build-bot    update  configmaps  access-release
sa/access-release:log-shipper  create  configmaps  access-release
user/sam                       list    pods        access-release
user/sam                       list    pods        access-archive
user/sam                       list    nodes       -
user/toby                      list    pods        access-archive
user/toby                      list    pods        access-release
user/toby                      list    nodes       -
user/vendor-ops                delete  pods        access-release
user/vendor-ops                list    secrets     access-release

The report prints whatever the API server answers. Getting the objects right is what makes those answers the ones compliance expects.

The tests

  • Roles_app_team_grants_exactly_the_matrix_verbs - the editor rule set covers the settings and reads the workloads, and nothing more
  • Roles_binding_subject_lists_match_the_matrix - each binding names exactly its matrix subjects, with the right kinds
  • Roles_viewers_are_denied_every_write - the read-only row is read-only, for the group and for the account alike
  • Scope_auditors_read_every_namespace - the cluster-bound auditor reads both namespaces through one binding
  • Scope_namespace_bound_auditor_reads_only_its_namespace - the same rules, scoped to the archive, stop at its edge
  • Scope_cluster_scoped_read_requires_the_cluster_binding - the nodes row separates the two bindings, and both point at one rule set
  • Tokens_minted_tokens_authenticate_as_their_accounts - the API server names each account as the owner of its token
  • Tokens_api_calls_stay_inside_the_bindings - calls carrying those tokens succeed and are refused exactly per the matrix
  • Tokens_pod_runs_as_its_assigned_account - the runner's every API call is made as build-bot
  • Composition_legacy_binding_is_revoked - the vendor account has lost what it used to have
  • Composition_report_matches_the_access_matrix - both sections agree with the live cluster, cell for cell
  • Composition_report_is_rederived_on_every_run - a binding added after your last run turns up in the next report

When every test passes

You have:

  • An access matrix that exists as objects, with every cell answered by the API server rather than by a spreadsheet
  • Humans, a group and two automation accounts each holding one row and refused the rest
  • One auditor rule set written once, reaching the whole estate through one binding and one namespace through another
  • Tokens that prove which account made a call, and a pod that makes its calls as one
  • A god-mode binding revoked, and the proof that the account behind it can do nothing
  • A report that re-reads the cluster every time it runs, so it is still true the day after it is signed
  • The next topic unlocked

Verify

Click Test.

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 yetmediumClusterRoles & 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 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.