RBAC, service account tokens, Pod Security Admission, secrets hygiene, and escalation paths

RBAC is purely additive allow-lists — there is no deny. Combine with a disabled token automount, dropped capabilities and Pod Security admission, or a pod compromise becomes a cluster compromise.

Read the briefing
About 3 min to read 7 drills

What to remember

  1. There is no deny rule in RBAC. Permissions only union. 'Why can they still do that' means another binding allows it.
  2. Namespace-wide 'get secrets' is cluster-wide credentials. It reads every service account token in that namespace.
  3. Turn off automountServiceAccountToken unless it is needed. Most pods never call the API; a mounted token is free escalation for an attacker.
  4. Pod Security admission has three levels. Restricted is the one that blocks privileged escalation paths. Enforce per namespace.
  5. The escalation chain is dependency → pod → token → RBAC → node. Each link has a control; name them and you have the answer to the whole question.

The big picture

Kubernetes security questions in loops come down to four mechanisms: RBAC decides who may do what to which API objects; ServiceAccount tokens are how workloads authenticate; Pod Security Admission decides what a pod is allowed to ask for (privilege, host access); and Secrets plus image policy decide what sensitive material and code enter the cluster. Most real breaches are chains: an over-broad Role, a mounted token, a privileged pod, then the node.

The mental model

Think in two planes. The API plane: identity (users, groups, ServiceAccounts) → RBAC (Roles/ClusterRoles bound by RoleBindings/ClusterRoleBindings) → admission (PSA, webhooks). The node plane: what a container can touch once it runs (privileged mode, hostPath, host network, capabilities). Least privilege in both planes is the answer to almost every design question; the interviewer wants to hear you name the escalation paths you are closing.

In depth

RBAC: roles, bindings, verbs, and the traps

A Role (namespaced) or ClusterRole (cluster-wide) lists rules: API groups, resources, and verbs (get, list, watch, create, update, patch, delete, plus subresources such as pods/exec and pods/log). A RoleBinding grants a Role or ClusterRole within one namespace; a ClusterRoleBinding grants a ClusterRole everywhere. Two traps: list on secrets reveals every secret's contents (list returns full objects), and binding cluster-admin "temporarily" is how most clusters end up with it permanently. kubectl auth can-i --as system:serviceaccount:ns:name <verb> <resource> is how you check what an identity can do.

Escalation paths worth knowing: create pods in a namespace that also allows hostPath lets you mount the node filesystem; pods/exec into a pod with a powerful ServiceAccount inherits its token; create on rolebindings plus a permissive ClusterRole lets you grant yourself more (RBAC prevents granting permissions you do not hold, unless you have bind/escalate verbs); impersonate is cluster-admin in disguise.

ServiceAccount tokens and workload identity

Every pod runs as a ServiceAccount (default: default). Since 1.22, tokens are projected, bound, and short-lived: mounted at /var/run/secrets/kubernetes.io/serviceaccount/token, rotated by the kubelet, bound to the pod, and rejected after the pod is gone. Long-lived Secret-based tokens are no longer auto-created (since 1.24) and exist only if created explicitly but are legacy. Set automountServiceAccountToken: false on pods that never call the API; give each workload its own ServiceAccount with the minimum Role. For cloud access, bind the ServiceAccount to a cloud identity (IRSA on EKS, Workload Identity on GKE) instead of baking cloud keys into Secrets.

Pod Security Admission, Secrets, and images

Pod Security Admission replaced PodSecurityPolicy (removed in 1.25). Namespaces are labelled with a level, privileged, baseline, or restricted, in enforce, audit, or warn modes. restricted requires running as non-root, dropping all capabilities, disallowing privilege escalation, and a seccomp profile; baseline blocks the obvious escalations (privileged, hostPID/hostNetwork, hostPath) while allowing common apps. Roll it out with warn/audit first, then enforce.

Secrets are base64, not encrypted, by default: enable encryption at rest (or a KMS provider), restrict get/list on secrets by RBAC, prefer mounting as files over environment variables (env vars leak into child processes and crash dumps), and use an external secrets operator for anything that rotates. Images: pin by digest or immutable tag, pull from a registry you control, scan, and use imagePullPolicy: Always for mutable tags or not at all; admission policies (Kyverno, Gatekeeper, ValidatingAdmissionPolicy) can enforce signed images and forbid latest.

Myths and facts

Myth: A RoleBinding to a ClusterRole grants cluster-wide access.

Fact: A RoleBinding scopes any role, including a ClusterRole, to the binding's namespace. Only a ClusterRoleBinding is cluster-wide.

Myth: Kubernetes Secrets are encrypted.

Fact: They are base64-encoded and stored in etcd in plaintext unless encryption at rest is configured.

Myth: Giving a ServiceAccount 'get' on secrets is safe because it cannot list them.

Fact: get by name is limited, but list or watch returns full contents of every secret; check which verbs you granted.

Myth: PodSecurityPolicy is how you restrict privileged pods.

Fact: PSP was removed in 1.25; Pod Security Admission (namespace labels) or an admission policy engine does this now.

Myth: Running as root in a container is fine because containers are isolated.

Fact: Root in a container plus a privileged flag, hostPath, or a kernel bug is root on the node; run as non-root and drop capabilities.

How pros do it

  • One ServiceAccount per workload with a namespaced Role; never bind cluster-admin to a workload.
  • Set automountServiceAccountToken: false unless the pod calls the API.
  • Enforce Pod Security restricted (or baseline where needed) per namespace, rolled out via warn → audit → enforce.
  • Encrypt Secrets at rest, mount them as files, restrict list/watch on secrets, and rotate via an external secrets operator.
  • Pin images by digest, scan them, and enforce signature verification and no-latest with an admission policy.

Questions people ask

Are Kubernetes Secrets encrypted?

No — base64 is encoding, not encryption; they sit in etcd in plaintext unless the API server has an EncryptionConfiguration (KMS or AES). The real controls are encryption at rest, RBAC that restricts get, list and watch on secrets, files instead of env vars, and an external secrets operator for anything that rotates.

How do I check what a ServiceAccount is allowed to do?

kubectl auth can-i --list --as=system:serviceaccount:<ns>:<sa> lists it, and kubectl auth can-i create pods --as=… checks one verb and resource. Then read the bindings themselves (kubectl get rolebinding,clusterrolebinding -A -o wide), because can-i tells you the union, not which binding granted it.

What is the minimum I should do to secure a cluster this week?

Audit ClusterRoleBindings for cluster-admin and onboarding-era grants, enable PSA in warn and audit mode everywhere, turn on Secrets encryption at rest, disable token automount on workloads that do not call the API, and require image digests in production. Each one closes a link in the escalation chain.

Why is my pod rejected with 'violates PodSecurity restricted:latest'?

The namespace enforces the restricted level and the pod asks for something it forbids: running as root, added capabilities, privilege escalation, hostPath, or no seccomp profile. The message lists the exact fields; the one thing YAML cannot fix is an image that must run as root.

Should secrets be env vars or files?

Files. Environment variables are visible to every process the container spawns, appear in crash dumps and describe output, and cannot change without a restart; mounted files update in place (unless subPath) and stay out of process listings.

Do NetworkPolicies belong to security?

Yes, as the third plane: without them every pod can talk to every pod and to the node's metadata service. They are covered in the CNI topic; mention them when you list the controls.

Further reading

Drills · 7

Answer in your head, or out loud, before you reveal. Mark each one: "Again" brings it back, "Got it" counts on your desk.

1 Pick the strongest answer

You find a RoleBinding in namespace payments that binds the ClusterRole view to the group developers. What can members of developers actually do?

  1. Read and edit resources in payments, because view includes update. The built-in view role is read-only (get/list/watch).
  2. Read most resources in every namespace, because the role is a ClusterRole. Cluster-wide access requires a ClusterRoleBinding.
  3. Read most resources in the payments namespace only; the ClusterRole's rules are scoped by the namespaced binding. A RoleBinding always scopes to its namespace, even when it references a ClusterRole.
  4. Nothing until an admin also creates a Role named view in the namespace. ClusterRoles are reusable rule sets; binding them locally is the intended pattern.
In short

They can read most resources in the payments namespace only; a RoleBinding limits even a ClusterRole to its namespace.

Why

Roles are rule sets; bindings decide scope. Reusing built-in ClusterRoles (view, edit, admin) through namespaced RoleBindings is the standard way to grant per-namespace access without duplicating rules.

The common mistake

Hearing 'ClusterRole' and answering 'cluster-wide'. The binding kind decides the scope, not the role kind.

Read the docs

2 Answer out loud

A pod gets compromised through a vulnerable dependency. Walk me through how that becomes full cluster compromise, and name the control that breaks each step.

In short

A strong answer names each step, token then RBAC then privileged pod then node, and pairs each step with the control that stops it.

What a strong answer does
  • chain: Projected token → RBAC of its ServiceAccount → list secrets / exec into other pods / create pods with hostPath or privileged → node root → cluster; each step named.
  • controls: No token automount; namespaced minimal Roles; no secrets list/exec; PSA restricted; NetworkPolicy to the API server where possible; encryption at rest; image provenance; audit alerts on sensitive verbs.
  • communication: Step, control, step, control, ending with detection.
Why

Staff-level answers pair each escalation step with the specific control and end with detection; junior answers stop at "use RBAC".

The common mistake

Stopping at 'use RBAC and keep images updated'. The interviewer wants each link of the chain paired with a specific, cheap control.

3 Pick the strongest answer

A monitoring ServiceAccount has a ClusterRole with resources: ["secrets"], verbs: ["list", "watch"] 'so it can see which secrets exist'. What have they actually granted?

  1. None; it can only see metadata. List returns the full object including data.
  2. It can create secrets but not read them. create is not granted; read is.
  3. It can read every Secret's contents cluster-wide: list and watch return full objects, not names. The API has no 'names only' list for secrets; list/watch is equivalent to reading them all.
  4. It can only read secrets in kube-system. With a ClusterRoleBinding the grant is cluster-wide.
In short

It can read every Secret's contents across the cluster, because list and watch return full objects.

Why

Secrets are the one resource where list/watch is as sensitive as get. If a component needs to know names only, use a different mechanism (labels on ConfigMaps, or restrict to a namespace and specific resourceNames with get). Interviewers use this to check whether you think about what a verb returns.

Read the docs

4 Read the output

What does the team have to change to get this pod running?

$ kubectl label ns shop pod-security.kubernetes.io/enforce=restricted --overwrite
$ kubectl -n shop apply -f api.yaml
Error from server (Forbidden): error when creating "api.yaml": pods "api-1" is forbidden: violates PodSecurity "restricted:latest": allowPrivilegeEscalation != false (container "api" must set securityContext.allowPrivilegeEscalation=false), unrestricted capabilities (container "api" must set securityContext.capabilities.drop=["ALL"]), runAsNonRoot != true (pod or container "api" must set securityContext.runAsNonRoot=true), seccompProfile (pod or container "api" must set securityContext.seccompProfile.type to "RuntimeDefault" or "Localhost")
  1. Grant the ServiceAccount cluster-admin so admission passes. Admission applies regardless of RBAC; RBAC decides who may create, PSA decides what may be created.
  2. Add privileged: true so the pod has enough permissions. Privileged is the opposite of what restricted requires.
  3. Add a securityContext that sets runAsNonRoot: true, allowPrivilegeEscalation: false, capabilities.drop: [ALL], and seccompProfile.type: RuntimeDefault (and ensure the image actually runs as a non-root user). The rejection lists each restricted-level requirement the pod fails; the message is the checklist.
  4. Remove the label; restricted is only for kube-system. Restricted is the recommended level for application namespaces; the fix is the pod spec.
In short

Add a securityContext with runAsNonRoot: true, allowPrivilegeEscalation: false, drop ALL capabilities and seccomp RuntimeDefault.

Why

Pod Security Admission messages are precise: each clause names the field to set. The one thing the manifest cannot fix is an image whose process must run as root; that needs an image change (or a non-root user in the Dockerfile).

Read the docs

5 Find what's broken

The reporter workload only needs to read pod logs in its own namespace. A security review found three lines that together make a compromise of this pod a cluster compromise. Which?

apiVersion: v1
kind: ServiceAccount
metadata:
  name: reporter
  namespace: analytics
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: reporter
rules:
  - apiGroups: [""]
    resources: ["pods", "pods/log"]
    verbs: ["get", "list"]
  - apiGroups: [""]
    resources: ["pods/exec"]
    verbs: ["create"]
  - apiGroups: [""]
    resources: ["secrets"]
    verbs: ["get", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: reporter
subjects:
  - kind: ServiceAccount
    name: reporter
    namespace: analytics
roleRef:
  kind: ClusterRole
  name: reporter
  apiGroup: rbac.authorization.k8s.io
In short

The dangerous lines are pods/exec (16), secrets (19) and the ClusterRoleBinding (23); use a namespaced Role with only pod and log reads.

What's broken
  • Line 16: create on pods/exec lets the identity execute commands inside any pod, inheriting that pod's ServiceAccount token and secrets.
  • Line 19: list on secrets returns the contents of every secret the binding reaches.
  • Line 23: A ClusterRoleBinding makes the grant cluster-wide; the workload only needs its own namespace.
Why

Each line alone is a smell; together they are a chain: cluster-wide scope (line 23) × exec into any pod (16) × read any secret (19). Least privilege here is a namespaced Role with get/list on pods and get on pods/log, bound by a RoleBinding. Interviewers love this because the manifest is valid, common, and catastrophic.

Read the docs

6 Scenario: what do you do next?

Security tooling alerts that a pod in the indexer namespace is making API calls to list secrets across all namespaces. The pod runs a third-party chain indexer image pulled by the latest tag. The namespace's default ServiceAccount has a broad ClusterRoleBinding created "during onboarding". Validators and RPC nodes run in the same cluster.

Step 1. First action?

  1. Block the registry in the firewall. The image is already running; the registry is not the active path.
  2. Rotate every secret in the cluster immediately. Possibly needed, but not before you know what was read; blind rotation during an incident causes outages.
  3. Contain: delete the ClusterRoleBinding (or scale the indexer to zero) to cut the API access now, capture the pod's image digest and the audit log entries, and check whether any secret reads succeeded and what they contained. Mitigation first with a reversible, targeted action; evidence preserved for the investigation.
  4. Delete the pod and the namespace. Destroys evidence, and the binding that made it dangerous stays.

Step 2. Audit logs show the pod listed secrets in validators including hot vote-key material 12 minutes ago. What now?

  1. Change the secret names so the attacker cannot find them again. Security through renaming; the values are already exposed.
  2. Treat the validator keys as compromised: rotate vote/identity keys per the runbook (switch to the standby identity), invalidate the token, then investigate the image (digest, SBOM, upstream advisory). Key exposure on a validator is a funds and slashing risk; rotation is the mitigation, investigation follows.
  3. Wait for the vendor to respond. Exposure is now; vendor timelines are later.
  4. Assume the read was benign because the indexer is a well-known project. The latest tag means you do not even know which build ran; trust is not evidence.

Step 3. Systemic fixes?

  1. Move validators to a separate cluster and stop there. Isolation helps and is worth doing, but the RBAC and image controls are needed wherever workloads run.
  2. Ban third-party images. Unworkable; the fix is provenance and least privilege, not prohibition.
  3. Per-workload ServiceAccounts with namespaced Roles and no default-SA bindings, automountServiceAccountToken false, images pinned by digest with signature verification via admission policy, validator keys in a remote signer or HSM, and an alert on secrets list/watch by workloads. Breaks every link in the chain and detects the next attempt.
In short

Cut the API access first by deleting the ClusterRoleBinding, keep the evidence, then rotate any keys the pod could read.

Why

A supply-chain or compromised image is only as dangerous as the identity it runs with and the material it can reach. The interview shape: contain reversibly, assess exposure from audit logs, rotate what was exposed, then rebuild the chain of controls (identity, admission, provenance, key custody).

Read the docs

7 Quick recall

PodSecurityPolicy was removed in Kubernetes 1.25. What do you use now to stop privileged pods being created in a namespace?

In short

Pod Security Admission, which enforces the Pod Security Standards with namespace labels.

Accepted

Pod Security Admission · pod security admission · PSA

Why

Pod Security Admission enforces the Pod Security Standards (privileged, baseline, restricted) via namespace labels; policy engines like Kyverno or Gatekeeper add custom rules.

Next lesson Services and kube-proxy: ClusterIP to LoadBalancer, endpoints, and traffic policy
Drill · RBAC, service account tokens, Pod Security Admission, secrets hygiene, and escalation paths
0:00

Loading…