Skip to content
Hands-on IT IT Drills

Day 5: Minimizing RBAC and ServiceAccounts

Goal: You find overly broad permissions, cut them back to what is needed, and give Pods an API token only when they need one – and then a short-lived one. · Duration: approx. 75 min · Prerequisite: Day 1

Briefing

CKS: Cluster Hardening (15 %) – “Use Role Based Access Controls to minimize exposure” and “Exercise caution in using service accounts e.g. disable defaults, minimize permissions on newly created ones”.

  • Role/RoleBinding apply within a namespace, ClusterRole/ClusterRoleBinding cluster-wide. A ClusterRole bound via a RoleBinding applies only in the binding’s namespace.
  • RBAC only knows permissions, no denials. Fewer permissions means: remove rules or bindings.
  • Tool number one: k auth can-i … --as=<user> or --as=system:serviceaccount:<ns>:<name>.
mkdir -p ~/cks/tag05 && cd ~/cks/tag05
k create ns tag05
k config set-context --current --namespace=tag05

Drill 1: Find overly broad permissions

Recreate two typical mistakes: the CI pipeline (deployer) got edit cluster-wide, the monitoring (monitor) a wildcard role.

k create sa deployer
k create clusterrolebinding tag05-deployer --clusterrole=edit \
  --serviceaccount=tag05:deployer
k create sa monitor
k apply -f - <<'EOF'
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: wild
rules:
- apiGroups: ["*"]
  resources: ["*"]
  verbs: ["*"]
EOF
k create rolebinding monitor-wild --role=wild --serviceaccount=tag05:monitor
k create secret generic app-key --from-literal=api-key=lab-key-123
k create secret generic db-pass --from-literal=password=lab-db-pw-123

lab-key-123 and lab-db-pw-123 are lab values only. Now ask from the point of view of the two accounts:

D=system:serviceaccount:tag05:deployer
M=system:serviceaccount:tag05:monitor
k auth can-i get secrets -A --as=$D
k auth can-i create deployments -n kube-system --as=$D
k auth can-i --list --as=$D | wc -l
k auth can-i --list --as=$M | head -2 | tr -s " "
k auth can-i create rolebindings --as=$M
k auth can-i get secrets -n kube-system --as=$M

Expected:

yes
yes
98
Resources Non-Resource URLs Resource Names Verbs
*.* [] [] [*]
yes
no

deployer reads Secrets in all namespaces and may deploy into kube-system. monitor may do everything in tag05 (*.*), including granting roles – but nothing outside.

Save your progress? Sign up for free

Drill 2: Where do the permissions come from?

k get clusterrolebindings \
  -o jsonpath='{range .items[*]}{.metadata.name} {.subjects[*].name}{"\n"}{end}' \
  | grep tag05
k get clusterrolebinding tag05-deployer \
  -o jsonpath='{.roleRef.kind}/{.roleRef.name}{"\n"}'
k get rolebindings -o wide
k get role wild -o jsonpath='{.rules}{"\n"}'

Expected:

tag05-deployer deployer
ClusterRole/edit
NAME           ROLE        AGE   USERS   GROUPS   SERVICEACCOUNTS
monitor-wild   Role/wild   3s                     tag05/monitor
[{"apiGroups":["*"],"resources":["*"],"verbs":["*"]}]

ClusterRoleBindings do not show up in k get rolebindings -A – always check both kinds. k get clusterrolebindings -o wide shows the same, but is very wide.

Save your progress? Sign up for free

Drill 3: The wildcard escalates

Play monitor with a real token. Watch out: with your kubeconfig, kubectl still sends your admin certificate along, and that wins:

k config view --raw \
  -o jsonpath='{.clusters[0].cluster.certificate-authority-data}' | base64 -d > ca.crt
SA="--kubeconfig=/dev/null --server=https://172.16.0.126:6443"
SA="$SA --certificate-authority=ca.crt"
TOKEN=$(k create token monitor --duration=10m)
k --token=$TOKEN auth whoami | grep Username
k $SA --token=$TOKEN auth whoami | grep Username
k $SA --token=$TOKEN -n tag05 create rolebinding pwn --clusterrole=cluster-admin \
  --serviceaccount=tag05:default
k auth can-i get secrets --as=system:serviceaccount:tag05:default

Expected:

Username                                            kubernetes-admin
Username                                            system:serviceaccount:tag05:monitor
rolebinding.rbac.authorization.k8s.io/pwn created
yes

* includes the verbs bind and escalate: monitor grants cluster-admin to the default ServiceAccount, and with that every Pod without its own account has full rights in the namespace. Without these verbs, the API server refuses: “is attempting to grant RBAC permissions not currently held”.

Save your progress? Sign up for free

Drill 4: ClusterRole via RoleBinding

monitor should only read, and only in tag05. The built-in ClusterRole view fits – bound via a RoleBinding:

k delete rolebinding pwn monitor-wild
k delete role wild
k create rolebinding monitor-view --clusterrole=view --serviceaccount=tag05:monitor
k auth can-i list pods --as=$M
k auth can-i list pods -n kube-system --as=$M
k auth can-i get secrets --as=$M
k auth can-i create rolebindings --as=$M

Expected:

rolebinding.rbac.authorization.k8s.io "pwn" deleted from tag05 namespace
rolebinding.rbac.authorization.k8s.io "monitor-wild" deleted from tag05 namespace
role.rbac.authorization.k8s.io "wild" deleted from tag05 namespace
rolebinding.rbac.authorization.k8s.io/monitor-view created
yes
no
no
no

view deliberately leaves out Secrets. edit and admin include them – that is why the permissions of deployer were so dangerous.

Save your progress? Sign up for free

Drill 5: Least privilege for the pipeline

The pipeline only updates the image of existing Deployments in tag05:

k delete clusterrolebinding tag05-deployer
k create role deploy-updater --verb=get,list,watch,patch,update --resource=deployments
k create rolebinding deployer --role=deploy-updater --serviceaccount=tag05:deployer
k auth can-i --list --as=$D | grep deployments | tr -s " "
k auth can-i patch deployments --as=$D
k auth can-i create deployments --as=$D
k auth can-i get secrets --as=$D
k auth can-i patch deployments -n kube-system --as=$D

Expected (shortened):

deployments.apps [] [] [get list watch patch update]
yes
no
no
no

With --resource=deployments, kubectl sets the API group apps on its own. Always also check what should not work – “yes” alone proves no hardening.

Save your progress? Sign up for free

Drill 6: Secrets with resourceNames

notes-api needs exactly one Secret, app-key:

k create sa notes
k create role read-app-key --verb=get --resource=secrets --resource-name=app-key
k get role read-app-key -o jsonpath='{.rules}{"\n"}'
k create rolebinding notes-app-key --role=read-app-key --serviceaccount=tag05:notes
N=system:serviceaccount:tag05:notes
k auth can-i get secret/app-key --as=$N
k auth can-i get secret/db-pass --as=$N
k auth can-i list secrets --as=$N

Expected (shortened):

[{"apiGroups":[""],"resourceNames":["app-key"],"resources":["secrets"],"verbs":["get"]}]
yes
no
no

list and watch return Secrets with their content – whoever may list Secrets can read them all. resourceNames cannot be applied to create (the name is not yet known at creation time); a list of all Secrets stays forbidden here.

Save your progress? Sign up for free

Drill 7: Default ServiceAccount and automount

A Pod without serviceAccountName gets default – token included:

k run plain --image=busybox:1.37 -- sleep 3600
k wait --for=condition=Ready pod/plain
k get pod plain -o jsonpath='{.spec.serviceAccountName}{"\n"}'
k exec plain -- ls /var/run/secrets/kubernetes.io/serviceaccount

Expected (shortened):

default
ca.crt
namespace
token

Switch it off on the ServiceAccount (for all Pods that use it) and on the Pod (always wins):

k patch sa default -p '{"automountServiceAccountToken":false}'
k create sa web $do > web-sa.yaml
echo "automountServiceAccountToken: false" >> web-sa.yaml
k apply -f web-sa.yaml
k apply -f - <<'EOF'
apiVersion: v1
kind: Pod
metadata:
  name: web
spec:
  serviceAccountName: web
  containers:
  - name: c
    image: busybox:1.37
    command: ["sleep", "3600"]
---
apiVersion: v1
kind: Pod
metadata:
  name: api-client
spec:
  serviceAccountName: web
  automountServiceAccountToken: true
  containers:
  - name: c
    image: busybox:1.37
    command: ["sleep", "3600"]
EOF
k run plain2 --image=busybox:1.37 -- sleep 3600
k wait --for=condition=Ready pod/web pod/api-client pod/plain2
k exec plain2 -- ls /var/run/secrets/kubernetes.io/serviceaccount
k exec web -- ls /var/run/secrets/kubernetes.io/serviceaccount
k exec api-client -- ls /var/run/secrets/kubernetes.io/serviceaccount

Expected (shortened):

ls: /var/run/secrets/kubernetes.io/serviceaccount: No such file or directory
command terminated with exit code 1
ls: /var/run/secrets/kubernetes.io/serviceaccount: No such file or directory
command terminated with exit code 1
ca.crt
namespace
token

On the ServiceAccount, automountServiceAccountToken sits at the top level (next to metadata), on the Pod under spec. Running Pods keep their token – plain still has it; only a new Pod shows the effect.

Save your progress? Sign up for free

Drill 8: Short-lived tokens

k create token deployer --duration=1m
TOKEN=$(k create token deployer --duration=10m)
echo $TOKEN | cut -d. -f2 | base64 -d 2>/dev/null | jq '{sub, exp, iat}'
k get secrets -A --field-selector type=kubernetes.io/service-account-token
k create deployment web --image=nginx:1.29
k $SA --token=$TOKEN -n tag05 set image deploy/web nginx=nginx:1.29-alpine
k $SA --token=$TOKEN -n tag05 delete deploy web

Expected (shortened):

error: failed to create token: TokenRequest.authentication.k8s.io "" is invalid:
spec.expirationSeconds: Invalid value: 60: may not specify a duration less than
10 minutes
{
  "sub": "system:serviceaccount:tag05:deployer",
  "exp": 1795023955,
  "iat": 1795023355
}
No resources found
deployment.apps/web created
deployment.apps/web image updated
Error from server (Forbidden): deployments.apps "web" is forbidden: User
"system:serviceaccount:tag05:deployer" cannot delete resource "deployments" ...

exp - iat = 600: exactly 10 minutes, the minimum. Without --duration, the token is valid for one hour. Such tokens are not stored anywhere and expire on their own; there are no long-lived Secrets of type service-account-token in the cluster – do not create any either.

Save your progress? Sign up for free

Exercise: Speed Drills

  1. User jane may read Pods in tag05 (get, list, watch), nothing else; also check another namespace. Target time: 1 min
  2. ServiceAccount batch without an automatically mounted token. Target time: 30 s
  3. Create a token for batch that is valid for 2 hours. Target time: 15 s
  4. Which ServiceAccounts in tag05 may read the Secret app-key? Target time: 1 min

Solution

k create role pod-reader --verb=get,list,watch --resource=pods
k create rolebinding jane-pod-reader --role=pod-reader --user=jane
k auth can-i list pods --as=jane
k auth can-i list pods -n default --as=jane
k create sa batch
k patch sa batch -p '{"automountServiceAccountToken":false}'
k create token batch --duration=2h
for s in $(k get sa -o name | cut -d/ -f2); do
  echo "$s $(k auth can-i get secret/app-key --as=system:serviceaccount:tag05:$s)"
done

Expected (shortened):

yes
no
serviceaccount/batch created
serviceaccount/batch patched
eyJhbGciOiJSUzI1NiIs...
batch no
default no
deployer no
monitor no
notes yes
web no

Save your progress? Sign up for free

Checkpoint

k get clusterrolebindings \
  -o jsonpath='{range .items[*]}{.metadata.name} {.subjects[*].name}{"\n"}{end}' \
  | grep -c tag05

Expected: 0

k auth can-i patch deployments --as=system:serviceaccount:tag05:deployer

Expected: yes

k auth can-i get secrets --as=system:serviceaccount:tag05:deployer

Expected: no

k auth can-i get secret/db-pass --as=system:serviceaccount:tag05:notes

Expected: no

k get sa default -o jsonpath='{.automountServiceAccountToken}{"\n"}'

Expected: false

Challenge

A colleague set something up “just for a moment”. Since then, every ServiceAccount in tag05 – even a brand-new one – may read Secrets in all namespaces. Find the cause and remove it.

Preparation (plays the colleague):

k create clusterrole tag05-secret-reader --verb=get,list --resource=secrets
k create clusterrolebinding tag05-sa-secrets --clusterrole=tag05-secret-reader \
  --group=system:serviceaccounts:tag05

Hint: Bindings can point to a group instead of a ServiceAccount.

Solution

k create sa newbie
k auth can-i list secrets -n kube-system --as=system:serviceaccount:tag05:newbie
k get rolebindings -o wide | grep -c secret
k get clusterrolebindings \
  -o jsonpath='{range .items[*]}{.metadata.name} {.subjects[*].name}{"\n"}{end}' \
  | grep tag05
k get clusterrolebinding tag05-sa-secrets -o jsonpath='{.roleRef.name}{"\n"}'
k delete clusterrolebinding tag05-sa-secrets
k delete clusterrole tag05-secret-reader
k auth can-i list secrets -n kube-system --as=system:serviceaccount:tag05:newbie

Expected (shortened):

yes
0
tag05-sa-secrets system:serviceaccounts:tag05
tag05-secret-reader
clusterrolebinding.rbac.authorization.k8s.io "tag05-sa-secrets" deleted
clusterrole.rbac.authorization.k8s.io "tag05-secret-reader" deleted
no

Every ServiceAccount belongs to the groups system:serviceaccounts and system:serviceaccounts:<namespace>; --as=system:serviceaccount:… includes them when checking. Bindings to these groups (or to system:authenticated) are especially broad.

Clean up:

k config set-context --current --namespace=default
k delete ns tag05

Save your progress? Sign up for free

Exam Tip

  • First k auth can-i --list --as=…, then look for the binding – ClusterRoleBindings and RoleBindings. Moving an account from a ClusterRoleBinding into a RoleBinding is the most common minimization.
  • Imperative is faster: k create role … --verb --resource --resource-name, k create rolebinding … --clusterrole|--role --serviceaccount=<ns>:<name>. Watch out: for a ServiceAccount always <namespace>:<name>.
  • Docs: kubernetes.io → Reference → Access Authn/Authz → “Using RBAC Authorization” and Tasks → “Configure Service Accounts for Pods”.

Enterprise Note

  • Manage RBAC via GitOps and evaluate it regularly (e.g. kubectl who-can or rbac-tool plugins, policy engines like Kyverno against wildcards and cluster-admin).
  • People get permissions via groups from the identity provider (OIDC), not via individual certificates – certificates cannot be revoked.
  • automountServiceAccountToken: false on the default ServiceAccount of every namespace as part of the namespace template; every app gets its own ServiceAccount.
  • External systems (CI, Vault) use short-lived, audience-bound tokens instead of long-lived token Secrets.
  • escalate, bind and impersonate are as critical as cluster-admin itself.