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.
Client cert, JWT, OIDC, proxy Authentication invalid 401 Unauthorized user, groups, extra UserInfo no credential :system:anonymous verb, resource, ns Authorizer chainNode, RBAC, Webhook no allow 403 Forbidden first allow wins Admission saved etcd

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

KindScopeHoldsEffect
RoleNamespacedrules[]Permissions usable only inside its namespace
ClusterRoleClusterrules[] or aggregationRuleReusable. The only way to cover nodes, nonResourceURLs or all namespaces
RoleBindingNamespacedsubjects[] + roleRef (Role or ClusterRole)Grants the role only in the binding's namespace
ClusterRoleBindingClustersubjects[] + 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.
User Group ServiceAccount subjects[] RoleBindingone namespace ClusterRoleBindingwhole cluster roleRef reuse Role ClusterRole labelledClusterRoles aggregation
In short : Authentication turns a credential into strings. Roles list what is allowed, bindings hand a role to subjects in one namespace or the whole cluster, permissions only add up, and anything no rule allows gets 403.

1.ServiceAccounts

The only identity stored as an API object. Namespaced, core v1.

  • Every namespace gets a default SA. It may only do discovery and read ClusterTrustBundles.
  • A Pod picks one with serviceAccountName (else default). A missing SA gets the Pod rejected.
  • automountServiceAccountToken: false on the SA or the Pod skips the token. The Pod wins. Use it when the app never calls the API.
Singular vs plural
  • 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.
Pod specserviceAccountName SA admissionadds the volume kube-api-accesstoken, ca.crt, namespace reads Appre-reads, file rotates TokenRequestbound to this Pod asks kubeletrenews at 80% of TTL writes token kube-apiserversig, exp, aud, Pod UID Bearer RBACas system:serviceaccount:ns:sa Pod deleted :token rejected

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.authorization.k8s.io/aggregate-to-edit: "true" (or -admin, -view), so CRDs can extend them. view flows into edit, edit into admin.

4.Risky permissions

PermissionWhy
get / list / watch secretsAll three return the data, legacy SA tokens too
create podsMount any Secret, run as any SA in the namespace. See Pod Security
escalate, bindWrite or bind roles you do not hold. Without them the RBAC API blocks that
impersonateAct as anyone, even group system:masters
get nodes/proxyExec in every Pod on the node, no audit
create serviceaccounts/tokenMint tokens for existing SAs

5.Commands

CommandDoes
kubectl auth whoami -o jsonUsername, groups and extra, as the cluster sees you
kubectl auth can-i get pods --subresource=log -n shopAsk the authorizer. The exit code is the answer
kubectl auth can-i list pods -n prod --as=system:serviceaccount:dev:fooAsk as someone else. Needs impersonation rights
kubectl auth can-i --list -n shopEverything you may do in a namespace
kubectl create rolebinding log-reader -n shop --role=pod-log-reader --serviceaccount=shop:log-readerGrant a Role (or --clusterrole) in one namespace
kubectl create token log-reader -n shop --duration 10mShort-lived token through TokenRequest
In short : A Pod runs as a ServiceAccount with a short-lived token bound to that Pod. Grant the least it needs, and treat the risky permissions like admin rights.