Sub topic 1
How the API server authenticates and authorizes requests
Prerequisites
This chapter assumes you know the following:
- Basic Kubernetes objects: pods, namespaces, deployments, and secrets.
- How to write YAML manifests and apply them with
kubectl. - That the Kubernetes API is a REST API. Resources such as pods are read and changed with verbs such as
getandcreate.
The request pipeline
Every action in a cluster goes through the API server. kubectl, controllers, and pods that call the API all send HTTPS requests to it. Before the server reads or changes anything, each request passes through three stages in order.
- Authentication decides who is calling.
- Authorization decides whether that caller may perform this action.
- Admission control can validate or modify requests that create, update, or delete objects.
RBAC (role-based access control) belongs to the second stage. This chapter covers the first two stages, because RBAC only makes sense once you see what it receives from authentication.
Authentication: who are you?
Authentication turns the credentials on a request into an identity. Kubernetes has no user object, so it does not store people. The API server checks the credential and extracts a username and a list of groups.
Common credential types are:
- Client certificates. The certificate's common name (CN) becomes the username. Its organization (O) fields become groups.
- Bearer tokens, such as service account tokens.
- Tokens from an external OpenID Connect (OIDC) provider.
The server tries each configured authenticator until one accepts the credential. If none does, the request fails with 401 Unauthorized. Requests that carry no credential are treated as the user system:anonymous if anonymous access is enabled.
Authentication does not grant any permission. It only establishes the identity that authorization will examine.
Authorization: what may you do?
Authorization receives the identity plus the details of the request:
| Attribute | Example |
|---|---|
| User | `jane` |
| Groups | `developers` |
| Verb | `list` |
| API group | `""` (core) |
| Resource | `pods` |
| Namespace | `default` |
The API server asks each configured authorizer in turn. In most clusters these are the Node authorizer (for kubelets) and RBAC. If any authorizer allows the request, it proceeds. If none has an opinion that allows it, the server returns 403 Forbidden.
Reading the two error codes
The status code tells you which stage rejected the request.
| Code | Stage | Meaning |
|---|---|---|
| 401 Unauthorized | Authentication | The server could not identify you. |
| 403 Forbidden | Authorization | The server knows who you are, but no rule allows the action. |
A common mistake is to treat a 403 as a login problem. It is not. The identity is valid and the permissions are missing. A message like the following names the identity and the missing permission:
Error from server (Forbidden): pods is forbidden: User "jane" cannot list resource "pods" in API group "" in the namespace "default"Read it as: user jane, verb list, resource pods, API group "", namespace default. That is exactly the set of attributes an RBAC rule must cover.
See your own identity
You can ask the API server what it thinks your identity is. On Kubernetes 1.28 and later, this command works:
kubectl auth whoamiIt prints the username and groups that authentication produced. Use it first when a permission seems wrong, since RBAC bindings match against those exact names.
Exercise
A user runs kubectl get pods and receives Error from server (Forbidden): pods is forbidden: User "sam" cannot list resource "pods" in API group "" in the namespace "default". Which stage rejected the request, and what is the next step?
Show answer
Authorization rejected it, because the 403 Forbidden status shows the identity "sam" was accepted. The next step is to check whether a Role or ClusterRole grants the list verb on pods, and whether a binding connects it to sam or one of sam's groups.