SecurityLesson 2 of 6
  1. Authentication and service accounts, not reconciled
  2. RBAC, not reconciled
    1. Four objects
    2. Rules
    3. Subjects
    4. Default roles
    5. Checking access
    6. Escalation paths
    7. Least privilege
  3. Pod security: securityContext and Pod Security Admission, not reconciled
  4. Admission control and policy, not reconciled
  5. Image supply chain, not reconciled
  6. Audit logging and runtime security, not reconciled

RBAC

Four objects decide what every identity may do. Rules only add permissions, bindings set the scope, and a few verbs and resources quietly grant far more than they appear to.

  • 30 min
  • 4 objectives
  • 7 scenes
  • Includes an On AWS layer

Shows the lesson one block at a time. Arrow keys step, number keys jump to a scene, and Escape returns to the page where you were.

Authentication turns a request into a username and groups. Authorization decides whether that identity may perform this verb on this resource in this namespace. Most clusters answer with the Node authorizer for kubelets and RBAC for everyone else, configured with --authorization-mode=Node,RBAC or, stable from Kubernetes 1.32, an AuthorizationConfiguration file passed to --authorization-config. RBAC denies by default and its rules only ever add permissions. There is no deny rule, so the question for any request is whether some binding grants it.

Reconcile, in 7 scenes

Four objects

RBAC is four kinds in rbac.authorization.k8s.io/v1. Two hold rules and two attach rules to identities:

ObjectScopeGrants
RoleA namespaced set of RBAC rules: which verbs are allowed on which resources, in the Role’s namespace only.NamespaceNothing until bound
ClusterRoleClusterNothing until bound; can also cover cluster-scoped resources such as nodes, and non-resource URLs such as /healthz
RoleBindingA namespaced object that grants the rules of one Role or ClusterRole to a list of subjects, inside the binding’s namespace only.NamespaceIts role’s rules, in its own namespace
ClusterRoleBindingClusterA ClusterRole’s rules in every namespace and on cluster-scoped resources

The binding sets the scope, not the role. A RoleBinding in shop that references a ClusterRole grants that ClusterRole’s rules in shop only, which is how one rule set, such as the built-in view, is reused across namespaces without copying it. A ClusterRoleBinding to the same ClusterRole grants it everywhere.

The pairings are not symmetric. A RoleBinding can reference a Role in its own namespace or any ClusterRole; a ClusterRoleBinding can reference only a ClusterRole, because a Role has no meaning outside its namespace. Rules about cluster-scoped resources, such as nodes, PersistentVolumes, Namespaces or CRDs, and about non-resource URLs, take effect only through a ClusterRoleBinding: a RoleBinding to a ClusterRole that mentions nodes grants nothing, since no node lives in the binding’s namespace. Asking for list pods across all namespaces, as kubectl get pods -A does, is a cluster-scoped request too, so it needs a ClusterRoleBinding even when the user can list pods in every namespace one RoleBinding at a time.

A binding’s roleRef is immutable. Permission to update a binding is meant to let someone manage who holds a role, not to swap the role for a different one, and a different role is a different grant. To change it, delete and recreate the binding. Pointing the RoleBinding support-pod-reader, which grants the Role pod-reader, at the ClusterRole view is refused:

roleRef cannot change
kubectl -n shop patch rolebinding support-pod-reader -p '{"roleRef":{"name":"view","kind":"ClusterRole"}}'
The RoleBinding "reporter-configmap-reader" is invalid: roleRef: Invalid value: rbac.RoleRef{APIGroup:"rbac.authorization.k8s.io", Kind:"ClusterRole", Name:"view"}: cannot change roleRef

kubectl auth reconcile -f rbac.yaml handles this for manifests kept in Git: it creates missing objects, adds missing rules and subjects, and recreates bindings whose roleRef changed. Extra rules and subjects survive unless you pass --remove-extra-permissions and --remove-extra-subjects.

Rules

Each rule lists API groups, resources and verbs, and a request is allowed when one rule matches all three:

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: deployer
  namespace: shop
rules:
  - apiGroups: ["apps"]
    resources: ["deployments"]
    verbs: ["get", "list", "watch", "patch"]
  - apiGroups: [""]
    resources: ["pods", "pods/log"]
    verbs: ["get", "list"]
  - apiGroups: [""]
    resources: ["configmaps"]
    resourceNames: ["release-config"]
    verbs: ["get", "update"]

"" is the core group, where pods, Services, ConfigMaps and Secrets live; Deployments are in apps, Jobs in batch, Ingresses in networking.k8s.io. RBAC does not check names against discovery, so deployments under "" is stored happily and matches nothing. The forbidden error prints the group it needed, and kubectl api-resources lists every resource’s group.

Verbs come from the HTTP request: a GET on one object is get, a GET on a collection is list, and a GET with ?watch=true is watch; POST is create, PUT is update, PATCH is patch, and DELETE is delete on one object or deletecollection on a collection. Tools often need more verbs than their name suggests. kubectl get with -w needs watch as well as list, and client-side kubectl apply reads the object before it creates or patches it, so it needs get too. Four special verbs gate RBAC’s own safety checks rather than HTTP methods: bind and escalate on roles and ClusterRoles, impersonate on users, groups, ServiceAccounts and UIDs, and use, which PodSecurityPolicy relied on before its removal in 1.25.

A subresourceA path below an object, such as pods/log, pods/exec or deployments/scale, that RBAC authorizes as its own resource name. needs its own entry, written <resource>/<subresource>. A rule on pods does not cover pods/log or pods/exec. kubectl exec needs create on pods/exec. Clients that open exec, attach or port-forward over a WebSocket send a GET, which used to need only get; from 1.35 the AuthorizePodWebsocketUpgradeCreatePermission gate (beta, on by default) makes the API server require create on pods/exec, pods/attach and pods/portforward for those upgrades too.

resourceNames narrows a rule to named objects, with two limits: it cannot restrict create (the name is not known when the request is authorized) or deletecollection, and list and watch match only when the request carries a metadata.name field selector. Wildcards ("*") in groups, resources or verbs match resources that do not exist yet, including every CRD installed later.

ClusterRoles can also grant non-resource URLs, paths such as /healthz, /metrics or /version that are not API objects. A rule there lists nonResourceURLs with the verb get (or post and friends) instead of groups and resources, and a trailing * matches a prefix. Prometheus scraping the API server’s /metrics, for example, needs a ClusterRole with nonResourceURLs: ["/metrics"] bound by a ClusterRoleBinding.

Subjects

A binding’s subjects name users, groups and ServiceAccounts. Users and groups are the strings an authenticator produced; nothing checks that they exist. A ServiceAccount subject names a namespace, and that namespace can differ from the binding’s, which is how a CI account in ci gets access to shop:

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: deployer
  namespace: shop
subjects:
  - kind: ServiceAccount
    name: deployer
    namespace: ci
  - kind: Group
    name: shop-oncall
    apiGroup: rbac.authorization.k8s.io
  - kind: User
    name: release-manager
    apiGroup: rbac.authorization.k8s.io
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: deployer

The subject’s namespace is the field people get wrong. With a namespace that does not match where the ServiceAccount lives, the binding grants access to an account that does not exist, and the real one is still refused with exactly the error it had before, because nothing validates subjects. kubectl describe rolebinding prints a Namespace column for each ServiceAccount subject; compare it with the namespace in the username of the forbidden error. The groups system:serviceaccounts:<namespace> and system:serviceaccounts grant to every ServiceAccount in a namespace or the cluster, and system:authenticated to everyone with any credential; bindings to those are rarely what you want.

A binding grants to a ServiceAccount, not to a credential, so rotating tokens never touches RBAC. The credentials themselves are short-lived: TokenRequest issues them for an hour by default and the kubelet refreshes them, --service-account-extend-token-expiration (default true) stretches admission-injected ones up to a year for old clients, token Secrets have not been created automatically since 1.24, and the legacy token cleaner, stable since 1.30, removes auto-generated ones unused for a year. Managed platforms map their own identities onto the user and group strings that bindings name.

Default roles

The API server creates default ClusterRoles and bindings at startup, labelled kubernetes.io/bootstrapping=rbac-defaults, and reconciles them on every restart, restoring missing permissions and subjects. An annotation rbac.authorization.kubernetes.io/autoupdate: "false" on one opts it out. A system: prefix marks a role or binding the control plane manages directly; changing one can break the cluster.

The discovery roles let any identity learn what the API serves: system:basic-user and system:discovery are bound to system:authenticated, and system:public-info-viewer to both system:authenticated and system:unauthenticated. Every ServiceAccount also gets system:service-account-issuer-discovery, bound to the group system:serviceaccounts, which reads the token issuer’s OIDC discovery document. That is all a ServiceAccount outside kube-system gets by default, and it does not include creating tokens for itself.

The user-facing roles have no system: prefix. cluster-admin is meant for ClusterRoleBindings; admin, edit and view are meant to be granted per namespace with RoleBindings:

  • cluster-admin allows everything. Its default binding is to the group system:masters; through a RoleBinding it grants everything in that one namespace.
  • admin allows nearly everything in a namespace, including Roles and RoleBindings there, but not writing ResourceQuotas or the Namespace object.
  • edit adds writes to most namespaced objects, including Secrets and pods. It excludes Roles and RoleBindings, yet it can run pods as any ServiceAccount in the namespace, so it is close to admin in practice.
  • view reads most namespaced objects but not Secrets, because reading Secrets can expose ServiceAccount credentials and so the API access of every account in the namespace.

kubectl describe clusterrole view prints what a role grants, resource by resource, which is quicker than guessing from its name. admin, edit and view are aggregated. A ClusterRole with an aggregationRule gets its rules from every ClusterRole whose labels match, and the controller overwrites any rules you write on it. A CRD’s installer extends the built-in roles by labelling a ClusterRole:

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: backups-view
  labels:
    rbac.authorization.k8s.io/aggregate-to-view: "true"
rules:
  - apiGroups: ["backup.example.com"]
    resources: ["backups"]
    verbs: ["get", "list", "watch"]

Checking access

The fastest way to understand a denial is to read it. A workload that is refused logs the API server’s error, and the error names the user, verb, resource, API group and namespace: everything a rule needs.

From forbidden to allowed
kubectl -n shop logs deploy/reporter | tail -1
{"time":"2026-11-19T09:00:47.739Z","level":"ERROR","msg":"list configmaps","error":"configmaps is forbidden: User \"system:serviceaccount:shop:reporter\" cannot list resource \"configmaps\" in API group \"\" in the namespace \"shop\""}
kubectl -n shop create role configmap-reader --verb=get,list,watch --resource=configmaps
role.rbac.authorization.k8s.io/configmap-reader created
kubectl -n shop create rolebinding reporter-configmap-reader --role=configmap-reader --serviceaccount=shop:reporter
rolebinding.rbac.authorization.k8s.io/reporter-configmap-reader created
kubectl auth can-i list configmaps -n shop --as system:serviceaccount:shop:reporter
yes
kubectl auth can-i list secrets -n shop --as system:serviceaccount:shop:reporter
no

kubectl auth can-i sends a SelfSubjectAccessReview, impersonating another identity when you add --as, and prints yes or no, exiting non-zero on no. --list prints every rule that applies in a namespace. kubectl auth whoami shows the username and groups the API server sees for your own credential.

The Node authorizer answers for kubelets before RBAC is consulted, so can-i as system:node:<name> reflects the pods bound to that node, not a ClusterRole. The same question travels in the other direction too: with the Webhook authorization mode the API server sends a SubjectAccessReview to an external service and acts on its answer, and components that must check another identity’s access, such as an aggregated API server, create a SubjectAccessReview against the API server.

For the reverse question, who holds a permission, list bindings with kubectl get rolebindings,clusterrolebindings -A -o wide. rbac-lookup is maintained and summarises bindings per subject; kubectl-who-can and rakkess have had no release since 2022.

Escalation paths

RBAC protects itself with escalation prevention. Creating or updating a Role or ClusterRole succeeds only if you already hold every permission in it, or hold escalate on roles. Creating or updating a binding succeeds only if you hold every permission in the referenced role, or hold bind on that role. So a user with write access to RBAC objects cannot grant themselves more than they have, and bootstrapping the first admin needs a credential in system:masters.

The permissions that defeat that protection are the ones to treat as admin-equivalent:

  • Creating workloads. Anyone who can create pods, or a Deployment, Job, CronJob or any other template, in a namespace can mount every Secret, ConfigMap and claim there and run as every ServiceAccount there.
  • Reading Secrets. list and watch return full objects, so they reveal every value, not just names.
  • bind, escalate and impersonate. Each is a direct way past escalation prevention.
  • create on serviceaccounts/token. It mints tokens for any account in scope.
  • Node and cluster surfaces. get on nodes/proxy reaches the kubelet API and can run commands in any pod on the node; creating PersistentVolumes can mount host paths; creating and approving CertificateSigningRequests can issue client certificates; writing webhook configurations can read or change every object admitted.
  • Patching Namespaces. Changing the Pod Security labels can relax what pods may do.

Workload creation deserves the most care because it is so common. A CI job allowed to create Deployments in payments looks harmless, but if payments also holds a ServiceAccount bound to admin for an operator, the CI job can start a pod that runs as that account and inherit admin there. Pod Security Admission limits what such a pod can do on the node; it does nothing about which ServiceAccount the pod uses. Keep powerful ServiceAccounts in namespaces where few identities can create workloads.

system:masters sits outside all of this: its members bypass RBAC, and no binding exists to remove. Keep people out of it and grant cluster-admin through a ClusterRoleBinding that can be deleted.

Least privilege

  • One ServiceAccount per workload, with a Role and RoleBinding in its namespace. Reach for a ClusterRoleBinding only for genuinely cluster-wide needs.
  • Prefer RoleBindings to the built-in view, edit and admin over hand-written copies, and remember that edit includes Secrets and pod creation.
  • Avoid wildcards, which grant resources installed later, and use resourceNames where the object is known.
  • Keep bind, escalate, impersonate, Secrets access and workload creation to the few identities that need them.
  • Review with kubectl auth can-i --list --as and a binding inventory, and manage RBAC manifests with kubectl auth reconcile so removed permissions are actually removed.

To build a Role for a workload, start from nothing. Run it with its own ServiceAccount and no bindings, read each forbidden error it logs, and add exactly the rule the error names: the verb, the resource with its group, and the namespace. A Role built this way is usually a handful of lines, and each line has a reason.

Grant, check and hit escalation prevention

  1. kubectl --context kind-kx create namespace ci
  2. kubectl --context kind-kx create namespace app
  3. kubectl --context kind-kx -n ci create serviceaccount deployer
  4. kubectl --context kind-kx auth can-i patch deployments -n app --as system:serviceaccount:ci:deployer
  5. kubectl --context kind-kx -n app create role deploy-manager --verb=get,list,patch --resource=deployments.apps
  6. kubectl --context kind-kx -n app create rolebinding deployer --role=deploy-manager --serviceaccount=ci:deployer
  7. kubectl --context kind-kx auth can-i patch deployments -n app --as system:serviceaccount:ci:deployer
  8. kubectl --context kind-kx auth can-i get pods --subresource=log -n app --as system:serviceaccount:ci:deployer
  9. kubectl --context kind-kx -n app create role role-writer --verb=create --resource=roles.rbac.authorization.k8s.io
  10. kubectl --context kind-kx -n app create rolebinding deployer-role-writer --role=role-writer --serviceaccount=ci:deployer
  11. kubectl --context kind-kx -n app create role sneaky --verb=get --resource=secrets --as system:serviceaccount:ci:deployer
  12. kubectl --context kind-kx -n app get rolebindings -o wide
  13. kubectl --context kind-kx delete namespace app ci

For your own kind cluster kx: copy a command and paste it into your terminal. Run in the simulator runs the same command in this page's simulated cluster instead; nothing on this page touches your machine. The can-i checks print no, yes and no. The deployer may create Roles, yet creating sneaky fails as forbidden because the Role would grant get on secrets, which the deployer does not hold; that is escalation prevention at work.

Actual state

Check your understanding

5 checks

Your first answer to each check is the one that counts. After a miss you can answer again; passing every check reconciles the lesson.

  1. Check 1 of 5

    A RoleBinding points at the Role log-reader, and you want it to grant the ClusterRole view instead. What works?

  2. Check 2 of 5

    A ServiceAccount's only rule in payments is create on deployments in apps. Which of these can it not do, directly or indirectly?

  3. Check 3 of 5

  4. Check 4 of 5

    A user holds create on roles and rolebindings in shop and nothing else. They create a Role granting get on secrets. What happens?

  5. Check 5 of 5

    A ServiceAccount's Role grants get on pods only. kubectl auth can-i get pods/log --as <it> prints yes. Can it read pod logs?

Lesson not yet reconciled

Desired: 4 objectives, 0 metReconciled: 4 of 4 objectives met

  • Write Roles and ClusterRoles with the right API groups, resources, subresources and verbs. (not yet met)
  • Bind them at the right scope to users, groups and ServiceAccounts, including ServiceAccounts from other namespaces. (not yet met)
  • Check what an identity can do with kubectl auth can-i --as and read a forbidden error back into the missing rule. (not yet met)
  • Name the escalation paths, pod creation, bind, escalate, impersonate and reading Secrets, and how escalation prevention limits them. (not yet met)

Pass every check above to reconcile this lesson.

Every check has passed, so the desired state is met. First-try accuracy: 0 of 5. This lesson's review cards are in your queue.

Practice

Put this lesson to work on the simulated cluster. Everything runs in your browser, with nothing to install.

Changed since the notes

This lesson draws on an earlier set of study notes. Where the documentation contradicts a claim in them, qualifies it or has moved past it, the claim is listed here with the corrected statement and the source it was checked against.

Show 7 corrections
  1. Stale, true once, no longerKubernetes RBAC Deep Dive

    The notes saidaws-auth ConfigMap maps IAM entities; EKS Access Entries (newer feature) are an alternative.

    CorrectedAWS documents aws-auth as deprecated and access entries plus access policies (AmazonEKSClusterAdminPolicy, AmazonEKSAdminPolicy, AmazonEKSEditPolicy, AmazonEKSViewPolicy) as the preferred mechanism. Authentication modes are CONFIG_MAP, API_AND_CONFIG_MAP and API (recommended); changes are one-way. Access-entry usernames cannot start with system:, eks:, aws:, amazon: or iam:, and eksctl will not migrate mappings to system:-prefixed groups other than EKS-specific ones; whether a STANDARD entry can list system:masters as a group was not confirmed here (use AmazonEKSClusterAdminPolicy).

    Sourcedocs.aws.amazon.com/eks/latest/best-practices/identity-and-access-management.html%20;%20https://docs.aws.amazon.com/eks/latest/userguide/creating-access-entries.html%20;%20https://docs.aws.amazon.com/eks/latest/eksctl/access-entries.html

  2. Nuance, true with conditionsKubernetes RBAC Deep Dive

    The notes saidProjected service account tokens: short-lived, audience-scoped, automatically mounted.

    CorrectedCorrect, with specifics: TokenRequest-issued tokens default to a 1-hour lifespan and the kubelet refreshes them before expiry; kube-apiserver --service-account-extend-token-expiration (default true) extends admission-injected tokens up to 1 year for legacy clients. Secret-based tokens are not auto-created since 1.24, and the legacy token cleaner (stable since 1.30) deletes auto-generated secret tokens unused for one year by default.

    Sourcekubernetes.io/docs/reference/access-authn-authz/service-accounts-admin/%20;%20https://kubernetes.io/docs/reference/command-line-tools-reference/kube-apiserver

  3. Nuance, true with conditionsKubernetes RBAC Deep Dive

    The notes saidTools like rbac-lookup, kubectl-who-can, and rakkess provide RBAC analysis.

    Correctedrbac-lookup is maintained (v0.10.3, 2025-08-01). kubectl-who-can's last release is v0.4.0 (2022-02-15, built on client-go v0.20) and rakkess's is v0.5.1 (2022-11-18), so both are effectively unmaintained. Built-in kubectl auth can-i --list covers the common case.

    Sourcegithub.com/FairwindsOps/rbac-lookup/releases/tag/v0.10.3%20;%20https://github.com/aquasecurity/kubectl-who-can/releases/tag/v0.4.0%20;%20https://github.com/corneliusweig/rakkess/releases/tag/v0.5.1

  4. Wrong, not trueKubernetes RBAC Deep Dive

    The notes saidDefault service account has 'no permissions beyond the minimum (creating tokens for itself)'.

    CorrectedDefault RBAC grants no permissions to service accounts outside kube-system beyond the API discovery roles (system:basic-user, system:discovery, system:public-info-viewer via system:authenticated). A service account cannot create its own tokens; serviceaccounts/token create is not granted. The kubelet obtains tokens for pods.

    Sourcekubernetes.io/docs/reference/access-authn-authz/rbac

  5. Wrong, not trueKubernetes RBAC Deep Dive

    The notes said'Roles that can create or modify bindings can grant themselves more permissions'.

    CorrectedRBAC escalation prevention lets a user create/update a role binding only if they already hold every permission in the referenced role, or have the bind verb on it; creating/updating a Role/ClusterRole with permissions they lack requires the escalate verb. The real escalation paths are bind, escalate, impersonate, create pods (run as any service account in the namespace), and reading secrets.

    Sourcekubernetes.io/docs/reference/access-authn-authz/rbac

  6. Wrong, not trueKubernetes RBAC Deep Dive

    The notes saidExample IAM ARNs use a 9-digit account ID (123456789).

    CorrectedAWS account IDs are 12 digits; use the AWS documentation placeholder 111122223333 (for example arn:aws:iam::111122223333:role/my-role).

    Sourcedocs.aws.amazon.com/accounts/latest/reference/manage-acct-identifiers.html

  7. Nuance, true with conditionsKubernetes RBAC Deep Dive

    The notes saidVerb list omits bind, escalate and impersonate.

    CorrectedBesides get, list, watch, create, update, patch, delete and deletecollection, RBAC has special verbs: bind and escalate (on roles/clusterroles), impersonate (on users, groups, serviceaccounts, uids) and use (e.g. legacy PSP). These are the verbs the escalation discussion depends on.

    Sourcekubernetes.io/docs/reference/access-authn-authz/rbac

Every correction across the guide, by note and topic.

Sources

Checked against kubernetes.io and AWS documentation for Kubernetes 1.37 on .

  1. kubernetes.io/docs/reference/access-authn-authz/rbac
  2. kubernetes.io/docs/concepts/security/rbac-good-practices
  3. kubernetes.io/docs/reference/access-authn-authz/authorization
  4. kubernetes.io/docs/reference/access-authn-authz/user-impersonation
  5. kubernetes.io/docs/reference/access-authn-authz/service-accounts-admin
  6. kubernetes.io/docs/reference/kubectl/generated/kubectl_auth/kubectl_auth_can-i
  7. kubernetes.io/docs/reference/kubectl/generated/kubectl_auth/kubectl_auth_reconcile
  8. kubernetes.io/docs/reference/command-line-tools-reference/feature-gates
  9. docs.aws.amazon.com/eks/latest/userguide/access-entries.html
  10. github.com/FairwindsOps/rbac-lookup

RBAC

↓ ↑ step, 1 to 7 scene, Esc leave

kind cluster kxLast command: Nothing run yet