Skip to content
Tutorials Center
My learning

Topics

Lesson 46 of 78 ยท Security

Authentication and RBAC

Learn how Kubernetes decides who you are and what you may do: users, service accounts, Roles, ClusterRoles, bindings and least privilege, with a Python RBAC evaluator.

Every request to the Kubernetes API server answers two questions before anything happens. First: who are you? That is authentication. Second: are you allowed to do this? That is authorization. In almost every cluster the second question is answered by RBAC, role-based access control. If you have ever seen Error from server (Forbidden): pods is forbidden: User "dana" cannot list resource "pods", you have met RBAC from the wrong side.

This lesson explains who can be a subject, how Roles and bindings grant permissions, how to apply least privilege, and how to check access with kubectl auth can-i. As always, a real cluster cannot run on this site, so the manifests and commands are read-only, and the live examples are a Python model of the rules. If you need a refresher on how requests reach the API server, see Cluster Architecture.

Authentication: who is asking?

Kubernetes has no user database. There is no kubectl create user. The API server accepts proof of identity from several sources and extracts a username and a list of groups from it:

  • Client certificates. The certificate's common name becomes the username and its organization fields become groups. Your kubeconfig on a self-managed cluster often uses this.
  • Bearer tokens. Service account tokens (see Service Accounts and Workload Identity) and static tokens.
  • OpenID Connect (OIDC). An external identity provider such as your company login issues a signed token, and the API server trusts the provider. This is the normal way to give humans access.
  • Cloud integrations. EKS, GKE and AKS map cloud identities to Kubernetes users with their own plugins.

Two kinds of subjects

Users Humans, known only through a certificate or token.
Service accounts Identities for pods, stored as real API objects.

Groups are a third kind of subject. They come from the credential, not from an object.

1 / 2

The important point: users and groups are not objects. Kubernetes trusts whatever name the credential carries. Only ServiceAccounts are real objects you can create with kubectl create serviceaccount.

Roles: a list of allowed verbs

An RBAC permission is only ever an allow. There are no deny rules. A Role lists rules; each rule combines three things: the API groups, the resources, and the verbs.

YAML
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: pod-reader
  namespace: shop
rules:
  - apiGroups: [""]            # "" is the core group
    resources: ["pods", "pods/log"]
    verbs: ["get", "list", "watch"]

Key facts:

  • The core group (pods, services, configmaps, secrets, nodes, namespaces) has an empty string as its API group. Deployments live in apps, Jobs in batch, Ingresses in networking.k8s.io.
  • Verbs map to HTTP operations: get, list, watch, create, update, patch, delete, deletecollection. Note that list and watch also return full object contents, so a role that can list secrets can read every secret in the namespace.
  • Subresources are separate resources: pods/log, pods/exec, deployments/scale. Access to pods does not give you pods/exec.
  • resourceNames narrows a rule to specific objects, for example one ConfigMap called app-config.

A ClusterRole has the same shape but no namespace. It is used for cluster-scoped resources (nodes, namespaces, PersistentVolumes), for non-resource URLs such as /healthz, and for permissions you want to define once and reuse in many namespaces.

Bindings: who gets the role

A role by itself grants nothing. A binding connects it to subjects.

YAML
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: dana-reads-pods
  namespace: shop
subjects:
  - kind: User
    name: [email protected]
    apiGroup: rbac.authorization.k8s.io
  - kind: ServiceAccount
    name: dashboard
    namespace: shop
roleRef:
  kind: Role
  name: pod-reader
  apiGroup: rbac.authorization.k8s.io

A RoleBinding grants the permissions inside its own namespace only, even if it points at a ClusterRole. That is a very useful trick: define a ClusterRole viewer once, then bind it per namespace with RoleBindings. A ClusterRoleBinding grants the permissions in all namespaces and for cluster-scoped resources.

roleRef is immutable. To change which role a binding points to, delete and recreate the binding.

Kubernetes ships useful default ClusterRoles: view (read most things, but not secrets), edit (read and write most things), admin (everything in a namespace including RBAC for that namespace) and cluster-admin (everything, everywhere). Bind cluster-admin rarely and only to a handful of people.

Least privilege in practice

Least privilege means each subject gets the smallest set of verbs on the smallest set of resources that lets it do its job. Some habits:

  • Give each application its own ServiceAccount instead of using default.
  • Prefer namespace-scoped RoleBindings over ClusterRoleBindings.
  • Avoid the wildcard * in resources and verbs. It also covers resources added in future versions.
  • Treat these as sensitive: secrets (any read verb), pods/exec, create pods (a user who can create pods can mount any secret in the namespace), escalate, bind, impersonate. Kubernetes blocks you from granting permissions you do not hold yourself, unless you hold escalate or bind.
  • Review bindings regularly. Stale cluster-admin bindings are one of the most common findings in cluster audits.

Checking access with kubectl

Use kubectl auth can-i to ask the authorizer a question, without doing the action. These commands are read-only here:

CONSOLE
$ kubectl auth can-i list pods -n shop
yes
$ kubectl auth can-i delete deployments -n shop
no
$ kubectl auth can-i create pods --as dana@example.com -n shop
no
$ kubectl auth can-i --list -n shop

The --as flag impersonates another user (you need the impersonate permission for that), which is how admins test a role before handing it out. kubectl auth can-i --list prints everything the current identity may do in a namespace.

A Python model of the evaluator

The real authorizer is Go code inside the API server, but the idea fits in a few lines. This stand-in stores roles and bindings as plain dictionaries and answers "may this subject do this?" by looking for any matching allow rule. It is a simplification: it ignores non-resource URLs and aggregated roles.

Python A tiny RBAC evaluator

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice three things in the output. dana can list pods in shop but not in kitchen, because the binding is namespaced. Delete is refused because nobody granted it: the default answer is always no. And the ci service account may patch deployments but cannot read secrets, which is exactly what a deploy pipeline needs.

The next stand-in shows how a single wildcard silently widens access. It audits a list of rules and flags the risky ones:

Python Auditing rules for risky grants

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Real tools such as kubectl-who-can, rbac-lookup and rakkess do the same thing against a live cluster, answering "who can read secrets here?" or "what can this account do?".

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A RoleBinding in namespace shop points at a ClusterRole named view. Where do the permissions apply?

    A RoleBinding always limits the grant to its own namespace, even when the role it references is a ClusterRole. This lets you define a role once and reuse it per namespace.
  2. 2. How do you create a Kubernetes user account for a new developer?

    Kubernetes has no user objects. The API server trusts the name inside a certificate or token, and RBAC bindings refer to that name. Only ServiceAccounts are real API objects.
  3. 3. Which statement about RBAC rules is true?

    RBAC is purely additive. Access is granted if any rule allows it, and denied otherwise. There is no deny rule.
  4. 4. A role grants get on pods. Can the holder run kubectl exec into a pod?

    Subresources such as pods/exec and pods/log are separate resources, so they must be listed explicitly.

Summary

  • Authentication proves identity (certificates, tokens, OIDC); authorization (RBAC) decides what that identity may do.
  • Users and groups are only names in credentials. ServiceAccounts are the only identity objects.
  • A Role or ClusterRole lists allowed API groups, resources and verbs. RBAC has allow rules only, no deny rules.
  • RoleBindings grant within one namespace; ClusterRoleBindings grant cluster-wide. roleRef cannot be changed after creation.
  • Practice least privilege: one ServiceAccount per app, no wildcards, and extra care around secrets, pods/exec and create pods.
  • Use kubectl auth can-i (with --as and --list) to test access.

Next, learn how pods themselves get an identity: Service Accounts and Workload Identity.