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

ServiceAccounts & Tokens

mediumServiceAccounts & TokensWorkload Identity—No data yet

Problem statement

The release automation in namespace pipeline-ops authenticates as an engineer who left the company, out of a kubeconfig somebody copied onto a shared drive. Nobody can rotate it, nobody can narrow it, and the audit log says a person did every deploy. Give the automation identities of its own, scoped to what each job actually does, and take the inherited account's power away.

About this exercise

Users are not objects in Kubernetes. A ServiceAccount is - a namespaced object whose whole purpose is to be an identity that lives in the cluster, so it can be created, bound, rotated and deleted like anything else. The API server sees a ServiceAccount as the username system:serviceaccount:<namespace>:<name>, and that string is what appears in bindings, in access reviews and in the audit log.

An account authenticates with a token. Modern tokens are minted on demand through the TokenRequest API: you ask for a token for a named account, the API server signs one with an expiry, and it is never stored as an object. A client that presents that token instead of your own credential is that account for the life of the request - which is what makes the check honest. Your kubeconfig is cluster-admin, so anything you try yourself succeeds; a call carrying the account's token succeeds or is refused exactly as the account's bindings say.

A ServiceAccount by itself can do almost nothing. It becomes useful when a RoleBinding names it as a subject - kind ServiceAccount, with the name and the namespace it lives in - and it becomes an identity a workload runs as when a pod names it in its spec's serviceAccountName. From then on every API call the pod makes carries that account's mounted token, and the account's bindings decide what it can do.

Because the API server evaluates bindings per request, revocation is immediate and needs nothing restarted: delete the binding and the same token, still valid and still authenticating as the same account, is refused everything it used to be allowed.

kubectl create token --help covers minting, kubectl auth whoami --help covers asking the API server who a credential belongs to, kubectl options lists the client flags that select a server and a credential, and kubectl explain pod.spec.serviceAccountName is the field reference.

How this project is graded

The grader mints its own token for each account you created, calls the API server with it, and records what came back: the identity the server reports, the calls that succeed and the calls that are refused. It reads your bindings and your pod's spec from the live cluster, and it compares your saved answers against the API server's own responses. Everything it creates to test a write is its own throwaway object, removed afterwards; nothing you created is changed. The namespace is re-seeded before each run, so the workloads and the settings come back if you remove them; the inherited account's binding, once revoked, stays revoked.

What you will build

Everything lives in namespace pipeline-ops, which already runs deploy-console, holds the ConfigMap pipeline-settings and the Secret pipeline-registry-credentials, and carries the ServiceAccount retired-agent.

1. ServiceAccount deploy-bot with Role pipeline-deployer and RoleBinding pipeline-deployer-binding:

resources   configmaps    verbs   get, list, create, update, delete
resources   pods          verbs   get, list
subject     ServiceAccount deploy-bot (in namespace pipeline-ops)

2. ServiceAccount log-shipper with Role pipeline-observer and RoleBinding pipeline-observer-binding:

resources   pods          verbs   get, list
subject     ServiceAccount log-shipper (in namespace pipeline-ops)

Neither Role mentions secrets. The registry credentials stay out of reach of both accounts.

3. Three answers in answers/, all taken from calls that carry a freshly minted deploy-bot token rather than your own credential:

identity.txt   the username the API server reports for that token, exactly that
               string and nothing else
allowed.txt    what comes back when that token lists the configmaps of
               pipeline-ops - the listing itself
denied.txt     what comes back when that token tries to list the secrets of
               pipeline-ops - the refusal itself, naming the account

4. Pod pipeline-runner - container runner, image busybox:1.36, staying alive, running as deploy-bot rather than the namespace default.

5. The inherited account revoked. retired-agent still exists as an account, and one binding in pipeline-ops still gives it broad write access to the namespace. Remove that binding. The account stays; everything it could do goes.

Step 1 - identities that live in the cluster

Create the two accounts, define their Roles, and bind each account to its own.

serviceaccounts_exist_with_their_bound_roles should pass

Step 2 - who the token is

Mint a token for deploy-bot and ask the API server whose it is. The answer is the account's full username, and it is what belongs in answers/identity.txt.

minted_token_authenticates_as_the_service_account should pass

Step 3 - what the token may and may not do

Call the API with that token twice: once for the configmaps it is allowed, once for the secrets it is not. Keep both responses.

token_calls_succeed_inside_the_bound_permissions and token_calls_are_denied_outside_the_bound_permissions should pass

Step 4 - a workload with an identity

Create pipeline-runner running as deploy-bot. Confirm the observer account is read-only while you are here: it lists pods and can write nothing.

observer_account_is_read_only and pod_runs_as_the_assigned_service_account should pass

Step 5 - revoke the inherited access

Find the binding that still gives retired-agent broad write access to the namespace and delete it. A token for that account will still authenticate - a token proves who you are, not what you may do - and it will be refused everything.

revoked_binding_stops_the_retired_accounts_token should pass

When every test passes

You have:

  • Two identities that exist as objects, each scoped to one job, instead of one borrowed human credential
  • Tokens minted on demand and proved by the API server to belong to those accounts
  • The API's own answers for a call inside the grant and a call outside it
  • A pod whose every API call is made as the account you assigned it
  • An inherited account that still exists and can no longer do anything

You do not have yet:

  • The whole namespace on least privilege at once. Humans, automation and auditors each held to their own matrix, with a report proving it, is 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 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 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.