A controller in the shop namespace starts and logs configmaps is forbidden: User "system:serviceaccount:shop:api" cannot list resource "configmaps" in API group "" in the namespace "shop". Someone suggests a ClusterRoleBinding to cluster-admin. The error goes away, and every pod that runs as that service account now controls the whole cluster.
Kubernetes RBAC answers one question for each API request: may this user or group perform this verb on this resource, in this namespace? This post covers how the API server decides, the four RBAC objects, subjects and service accounts, the built-in roles, the permissions that lead to privilege escalation and how to test access, with a diagram you can copy.
How the API server authorizes a request
Every request passes authentication, then authorization; requests that create, change, delete or connect to objects then pass admission. Authentication turns a credential into a username and a list of groups: the common name and organization fields of a client certificate, the claims of an OIDC token, or a service account token. Kubernetes has no User or Group objects; both are plain strings.
The authorizers then run in order. The first one that allows or denies the request decides, and if none has an opinion, the request is denied with HTTP 403. kubeadm, for example, configures Node,RBAC: the Node authorizer handles kubelets, and RBAC handles everyone else.
RBAC sees only a few attributes of a request: the user and groups from authentication, and the verb, API group, resource (with any subresource, such as pods/log), namespace and object name from the request itself. It cannot consider object labels, the time of day or relationships between objects, as ABAC and ReBAC models can. For non-resource paths such as /healthz, it sees just the path and the verb, and access to them can be granted only through a ClusterRole's nonResourceURLs. The verb follows the HTTP method: GET maps to get, list or watch, POST to create, PUT to update, PATCH to patch, and DELETE to delete or deletecollection.
The four RBAC objects
The RBAC API, rbac.authorization.k8s.io/v1, has four kinds. Roles say what is allowed; bindings say who gets it, and where.
| Object | Scope | Holds | Grants access in |
|---|---|---|---|
| Role | One namespace | Rules | Its own namespace |
| ClusterRole | Cluster | Rules, or an aggregation rule | Wherever it is bound |
| RoleBinding | One namespace | Subjects and a roleRef to a Role or ClusterRole |
Its own namespace |
| ClusterRoleBinding | Cluster | Subjects and a roleRef to a ClusterRole |
All namespaces, plus cluster-scoped resources |
Permissions are purely additive, and there are no deny rules: a subject may do anything that at least one binding naming it, directly or through a group, allows. A binding's roleRef cannot be changed; to point it at another role, delete and recreate the binding, which kubectl auth reconcile does for you.
A rule lists API groups, resources and verbs. This Role lets its subjects read ConfigMaps in shop:
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: config-reader
namespace: shop
rules:
- apiGroups: [""] # "" is the core API group
resources: ["configmaps"]
verbs: ["get", "list", "watch"]
- Resources use their URL names, and subresources take a slash:
pods/log,pods/exec. resourceNameslimits a rule to named objects, but it cannot restrictdeletecollectionor top-levelcreate, so a rule with names never grants them.get,listandwatchall return full objects, soliston Secrets reveals their data.- Wildcards (
*) also match resources added later, including custom resources. Spell them out.
Subjects: users, groups and service accounts
| Subject kind | Comes from | Example | apiGroup in the binding |
|---|---|---|---|
| User | Certificate common name, OIDC claim, webhook | alice@example.com |
rbac.authorization.k8s.io |
| Group | Certificate organization fields, OIDC groups claim | dev-team |
rbac.authorization.k8s.io |
| ServiceAccount | A ServiceAccount object | api, with namespace: shop |
Empty |
A service account authenticates as the user system:serviceaccount:<namespace>:<name> (singular) and belongs to the groups system:serviceaccounts and system:serviceaccounts:<namespace> (plural). The system: prefix is reserved, so keep it out of your own user and group names.
Because users are only names, deleting a person in your identity provider leaves their bindings in place, and a new account with the same name inherits them. Prefer groups, and remove bindings when people leave. The OpenID Connect flow diagram shows where a groups claim comes from.
Binding a ClusterRole inside one namespace
A RoleBinding can reference a ClusterRole. The rules come from the ClusterRole, but they apply only in the binding's namespace. That is how you define a role once and reuse it in many namespaces, and how the built-in admin, edit and view roles are meant to be granted. This is the dev-team binding from the diagram below:
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: dev-team-edit
namespace: shop
subjects:
- kind: Group
name: dev-team
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: ClusterRole
name: edit
apiGroup: rbac.authorization.k8s.io
roleRef |
Binding | Effective scope |
|---|---|---|
| Role | RoleBinding in the same namespace | That namespace |
| ClusterRole | RoleBinding | The binding's namespace only |
| ClusterRole | ClusterRoleBinding | Every namespace, plus cluster-scoped resources such as nodes |
| Role | ClusterRoleBinding | Not possible: a ClusterRoleBinding references only ClusterRoles |
The binding lives with the resources. To let a service account in dev list Jobs in maintenance, create the RoleBinding in maintenance and name the dev service account as its subject.
The built-in roles: cluster-admin, admin, edit and view
| ClusterRole | Bind it with | Allows | Watch out for |
|---|---|---|---|
cluster-admin |
ClusterRoleBinding; bound to the system:masters group by default |
Everything | In a RoleBinding, full control of that namespace, including the namespace object |
admin |
RoleBinding | Read and write most objects, including Roles and RoleBindings in the namespace | No write access to ResourceQuota or the namespace |
edit |
RoleBinding | Read and write most objects | Can read Secrets and run pods as any service account in the namespace |
view |
RoleBinding, or a ClusterRoleBinding for read-only access to every namespace | Read most objects | No Secrets, Roles or RoleBindings |
Because edit can read Secrets and start pods as any service account, it gains the API access of every service account in the namespace. To extend these roles to custom resources, label a ClusterRole rbac.authorization.k8s.io/aggregate-to-edit: "true" (or -admin, -view), and the control plane merges its rules in.
Members of system:masters bypass RBAC entirely, and no binding change can revoke that. Do not issue credentials with that group; bind administrators to cluster-admin instead.
Service accounts and their tokens
Service accounts are namespaced identities for workloads. Every namespace gets a default service account with no permissions beyond API discovery. A pod that sets no serviceAccountName runs as default, so a role bound to default reaches every such pod.
- Tokens are short-lived. Since Kubernetes 1.22, the kubelet mounts a token from the TokenRequest API, a JWT signed by the API server, in a projected volume at
/var/run/secrets/kubernetes.io/serviceaccount. By default the kubelet requests a one-hour token that is also invalidated when the pod is deleted; the API server's default--service-account-extend-token-expirationsetting can keep such a mounted token valid for up to a year. The kubelet replaces it once it is older than 80% of its lifetime or 24 hours, so applications must reread the file. - Long-lived token Secrets are legacy. Since 1.24, Kubernetes no longer creates them automatically; since 1.29, auto-generated ones unused for a year are marked invalid and later deleted.
- Pods that never call the API need no token. Set
automountServiceAccountToken: falseon the service account, andtruein the pod spec of the workloads that do call it; the pod setting wins.
Permissions that lead to privilege escalation
RBAC blocks the obvious path: you can create a role only with permissions you already hold, and bind a role only if you hold its permissions, unless you have the escalate or bind verb. The RBAC good practices page lists permissions that still lead further:
| Permission | Why it is powerful |
|---|---|
get, list or watch on secrets |
Returns Secret data, including any long-lived tokens |
create on pods, or on workloads that create pods |
Runs as any service account in the namespace and mounts any Secret in it |
escalate or bind on roles |
Skips the checks on creating roles and bindings |
impersonate on users, groups or service accounts |
Acts as someone else |
create on serviceaccounts/token |
Issues tokens for existing service accounts |
get on nodes/proxy |
Reaches the kubelet API, including exec, without audit logging or admission control |
| Write access to webhook configurations | Sees every admitted object; a mutating webhook can also change it |
patch on namespaces |
Changes the labels that Pod Security admission and NetworkPolicies select on |
The same page treats boundaries within a namespace as weak: put workloads that need different levels of trust in different namespaces. Several of these permissions expose Secret values, so prefer short-lived credentials issued by Vault to long-lived passwords stored in Secrets.
Testing access with kubectl auth can-i
kubectl auth can-i asks the API server for a decision through the SelfSubjectAccessReview API, whichever authorizers you run. --list uses SelfSubjectRulesReview instead, which can be incomplete when you also run webhook authorizers. With --as, which requires the impersonate verb, it answers for someone else:
kubectl auth whoami
kubectl auth can-i --list --namespace shop
# Check a service account before you deploy
kubectl auth can-i list configmaps -n shop --as system:serviceaccount:shop:api
kubectl auth can-i list secrets -n shop --as system:serviceaccount:shop:api
# Check a group member
kubectl auth can-i delete deployments -n shop --as alice --as-group dev-team
Keep RBAC manifests in Git and review bindings like code, as in a GitOps workflow.
The Kubernetes RBAC diagram
Diagram contents
- Cluster scope: no namespace
- sre-view
- view (built-in)
- edit (built-in)
- namespace: shop
- dev-team-edit
- api-7d9f4
- db-credentials
- api
- api-config-reader
- config-reader
- app-config
- Group: sre
- Group: dev-team
- subject
- roleRef
- serviceAccountName
- get, list, watch
- list secrets → 403 Forbidden
How to read it:
- Arrows run subject → binding → role. A grant needs both a binding and a role.
- Blue, at the top: the
sregroup is bound cluster-wide to the built-inviewrole, so it reads most objects in every namespace, but no Secrets. - Amber:
dev-team-editlives inshopand references theeditClusterRole. The role is cluster-scoped; the grant is not. - Teal: pod
api-7d9f4runs as service accountapi, whichapi-config-readerbinds to theconfig-readerRole: get, list and watch on ConfigMaps inshop. - Red, dashed: the same pod lists Secrets and gets 403 Forbidden. No rule allows it, and that is all a denial needs.
- Bottom note: the
kubectl auth can-icheck, with both results.
Drawing Kubernetes RBAC diagrams clearly
- Separate the scopes: a frame for cluster-scoped objects and one per namespace, so a reader sees whether a grant is cluster-wide.
- Keep one direction: subject, binding, role, resource, with edges labeled
subjectandroleRef. - Show identities as they authenticate: groups from your identity provider, service accounts with their namespace.
- Write the rules on each role, and color by grant rather than by object kind.
- Draw one denial: a dashed edge with the 403 makes "additive only" concrete.
- Leave out the
system:roles unless you audit them; the Kubernetes control plane that uses them has its own diagram.
Common mistakes are a ClusterRole inside a namespace, an arrow from a role straight to a subject, a "deny" box and service accounts without a namespace. RBAC governs API calls only; traffic between pods is NetworkPolicy, covered in the Kubernetes networking diagram.
Build it in Archboard
- Sign in with Google or GitHub and create a diagram. The free Kubernetes YAML to diagram tool turns existing manifests into a first draft.
- Add a Frame from the Core pack named "Cluster scope: no namespace", and below it a Namespace from the Kubernetes pack named shop.
- In the frame, add a ClusterRoleBinding (sre-view) and two ClusterRole nodes, view and edit, with their rules.
- In the namespace, add a RoleBinding (dev-team-edit) with the role ref ClusterRole edit, and connect it up to edit.
- Add a Pod, a ServiceAccount (api), a second RoleBinding (api-config-reader), a Role (config-reader), a ConfigMap and a Secret, all from the Kubernetes pack. Put the Pod above the ServiceAccount, chain ServiceAccount, RoleBinding, Role and ConfigMap left to right, and place the Secret on the Pod's row.
- Add two Person nodes from the C4 Model pack for the sre and dev-team groups, and connect each to its binding with a subject edge.
- Draw a dashed red edge from the pod to the Secret labeled 403 Forbidden, and add Text notes from the Core pack for the scopes and the
kubectl auth can-icheck.
The Kubernetes use case page lists the rest of the pack. The example uses 14 diagram nodes, within the Free plan's limit of 15 per diagram.
FAQ
What is the difference between a Role and a ClusterRole? A Role holds rules for one namespace. A ClusterRole is not namespaced: it can cover cluster-scoped resources such as nodes, and RoleBindings can reuse it in many namespaces.
Can a RoleBinding reference a ClusterRole? Yes. The permissions then apply only in the RoleBinding's namespace, which is how the built-in admin, edit and view roles are meant to be granted.
How do I deny a permission in Kubernetes RBAC? You cannot: RBAC has no deny rules. Remove or narrow the binding that grants it. To reject requests based on their content, use admission control, such as a ValidatingAdmissionPolicy.
What permissions does the default service account have?
None beyond API discovery. Pods without a serviceAccountName use it, so bind roles to a service account per application instead.
How do I see what a service account can do?
Run kubectl auth can-i --list -n <namespace> --as system:serviceaccount:<namespace>:<name>. Impersonation requires the impersonate verb, which cluster administrators have.