RBAC and access
Which RBAC object to use, how to write and test rules, and how users and ServiceAccounts get credentials.
Which object
| Need | Rules in | Binding | Reach |
|---|---|---|---|
| One namespace | Role | RoleBinding | That namespace |
| Same rules in several namespaces | ClusterRole | RoleBinding per namespace | Each bound namespace |
| All namespaces | ClusterRole | ClusterRoleBinding | Cluster |
| Cluster-scoped kinds (nodes, PVs, namespaces) | ClusterRole | ClusterRoleBinding | Cluster |
Non-resource URLs such as /healthz | ClusterRole with nonResourceURLs | ClusterRoleBinding | Cluster |
RBAC is additive. There is no deny rule, and the binding's roleRef cannot be changed after creation.
Imperative forms
k create role r -n ns --verb=get,list,watch --resource=pods,pods/log
k create clusterrole cr --verb=get,list --resource=nodes
k create rolebinding rb -n ns --role=r --user=jana --group=dev --serviceaccount=ns:sa1
k create clusterrolebinding crb --clusterrole=cr --user=jana
k create rolebinding rb2 -n ns --clusterrole=view --serviceaccount=ns:sa1
k auth can-i list pods -n ns --as=jana
k auth can-i get pods --subresource=log -n ns --as=system:serviceaccount:ns:sa1
k auth can-i --list -n ns --as=janaVerbs and subresources
| Verbs | get list watch create update patch delete deletecollection |
|---|---|
| Subresources | pods/log, pods/exec, deployments/scale, nodes/status |
| API groups | "" core, apps, batch, networking.k8s.io, rbac.authorization.k8s.io |
| Names | resourceNames: ["x"] limits to named objects. It does not work with list and create |
Default ClusterRoles
cluster-admin (everything), admin (namespace admin including RBAC), edit (read and write most objects, no RBAC),
view (read most, no Secrets). Aggregation labels rbac.authorization.k8s.io/aggregate-to-view, -edit and -admin extend them.
Identities
| Identity | Created by | Appears as |
|---|---|---|
| User | Certificate CN signed by the cluster CA | jana |
| Group | Certificate O fields, or an OIDC claim | developers |
| ServiceAccount | k create sa | system:serviceaccount:<ns>:<name> |
New user flow: openssl genrsa, openssl req -new -subj "/CN=u/O=g", a CertificateSigningRequest with
signerName: kubernetes.io/kube-apiserver-client and usages: [client auth], k certificate approve, read
.status.certificate, k config set-credentials and set-context, then bind a role.
ServiceAccount tokens: k create token sa -n ns --duration=1h. Pods get a projected, expiring token. Set
automountServiceAccountToken: false when the Pod never calls the API.