Security
RBAC and Service Accounts
1.Two questions per request
Every API call is checked twice. Authentication asks who you are. Authorization asks if that identity may do this verb on this resource.
- Authentication returns only strings : username, UID, groups, extra.
- Bad credential : 401. Nobody allows : 403, the default deny.
- Authorizers run in order, e.g. Node, RBAC, Webhook. The first allow or deny wins.
- RBAC allows or has no opinion. It never denies.
- Admission runs only after an allow.
Members of system:masters skip RBAC and webhook authorizers.
2.Users come from outside
There is no User object. You cannot add a normal user with an API call.
- X.509 certCN = username, each O = a group
- OIDC / JWTtoken from an identity provider
- Webhookan outside service checks the token
- Auth proxya proxy in front sets the user
- Nothingsystem:anonymous, group system:unauthenticated
Every authenticated request also gets the group system:authenticated.
3.Inside a rule
- apiGroups
""= the core group - resourcespods, secrets, pods/log (get), pods/exec (create)
- verbsget, list, watch, create, update, patch, delete, deletecollection
- specialescalate, bind, approve, sign, attest, impersonate (beta : impersonate-on:MODE:VERB)
- resourceNamesonly these objects
- nonResourceURLs/healthz, ClusterRoleBinding only
4.Four RBAC kinds
| Kind | Scope | Holds | Effect |
|---|---|---|---|
| Role | Namespaced | rules[] | Permissions usable only inside its namespace |
| ClusterRole | Cluster | rules[] or aggregationRule | Reusable. The only way to cover nodes, nonResourceURLs or all namespaces |
| RoleBinding | Namespaced | subjects[] + roleRef (Role or ClusterRole) | Grants the role only in the binding's namespace |
| ClusterRoleBinding | Cluster | subjects[] + roleRef (ClusterRole only) | Grants the role in every namespace and cluster-wide |
All in rbac.authorization.k8s.io/v1. No deny rules, permissions only add up. Turn it on with --authorization-mode=...,RBAC or an --authorization-config file (stable since v1.32).
5.Subjects, bindings, roles
- A RoleBinding can reuse one ClusterRole in many namespaces, granting only in its own.
- roleRef is immutable. Delete and recreate, or
kubectl auth reconcile. - A ServiceAccount subject uses apiGroup
"". A ClusterRoleBinding must set its namespace. - Users are just strings. Recreate a deleted name and it inherits its old bindings.
1.ServiceAccounts
The only identity stored as an API object. Namespaced, core v1.
- Every namespace gets a
defaultSA. It may only do discovery and read ClusterTrustBundles. - A Pod picks one with
serviceAccountName(else default). A missing SA gets the Pod rejected. automountServiceAccountToken: falseon the SA or the Pod skips the token. The Pod wins. Use it when the app never calls the API.
- usersystem:serviceaccount:shop:log-reader
- groupssystem:serviceaccounts and system:serviceaccounts:shop
kind: Pod
spec:
serviceAccountName: log-reader
automountServiceAccountToken: false # no token
Identity outside the cluster, with mTLS : see SPIFFE, SPIRE and Pod Certificates.
2.Bound tokens in a Pod
- Mount : /var/run/secrets/kubernetes.io/serviceaccount holds token, ca.crt and namespace.
- Lifetime : asks for 3607s, and the API server may extend it to a year. Renewed past 80% of TTL or 24h.
- Revoke : not per token. Delete the Pod or the SA; it still works up to 10 s (cache).
- Legacy Secret tokens : not auto-made since v1.24. A hand-made one never expires.
3.Default roles
- cluster-admin : everything. Bound to system:masters and kubeadm:cluster-admins.
- admin : most namespaced objects, Roles too. Cannot write ResourceQuota or the Namespace.
- edit : read / write most objects and Secrets, run Pods as any SA. Cannot see Roles.
- view : read-only. No Secrets, no Roles.
admin, edit and view have no default binding. They aggregate any ClusterRole labelled rbac. (or -admin, -view), so CRDs can extend them. view flows into edit, edit into admin.
4.Risky permissions
| Permission | Why |
|---|---|
| get / list / watch secrets | All three return the data, legacy SA tokens too |
| create pods | Mount any Secret, run as any SA in the namespace. See Pod Security |
| escalate, bind | Write or bind roles you do not hold. Without them the RBAC API blocks that |
| impersonate | Act as anyone, even group system:masters |
| get nodes/proxy | Exec in every Pod on the node, no audit |
| create serviceaccounts/ | Mint tokens for existing SAs |
5.Commands
| Command | Does |
|---|---|
kubectl auth whoami -o json | Username, groups and extra, as the cluster sees you |
kubectl auth can-i get pods --subresource=log -n shop | Ask the authorizer. The exit code is the answer |
kubectl auth can-i list pods -n prod --as=system:serviceaccount:dev:foo | Ask as someone else. Needs impersonation rights |
kubectl auth can-i --list -n shop | Everything you may do in a namespace |
kubectl create rolebinding log-reader -n shop --role=pod-log-reader --serviceaccount=shop:log-reader | Grant a Role (or --clusterrole) in one namespace |
kubectl create token log-reader -n shop --duration 10m | Short-lived token through TokenRequest |