RBAC and Cluster Security

Topics Covered

Roles and ClusterRoles

Authentication, then authorisation

The structure of a Role

Verbs

Subresources and resource names

Creating Roles imperatively

ClusterRoles

Aggregated ClusterRoles

RBAC is additive

Reading an existing role quickly

Wildcards and their risks

Roles for non resource URLs

Role based access control decides what an authenticated identity may do. On the administrator exam it appears in many small tasks: create a Role that lets a user read pods, bind a ClusterRole to a service account, check whether an identity can perform an action, or find why a request is denied. The tasks are quick if you know the objects and the imperative commands, and slow if you write the YAML from memory. This section covers the permission objects; the next one covers bindings and checks.

Users created from client certificates were introduced with kubeconfig files in the cluster install lesson, and service accounts were introduced with pods in the workloads lessons. Here they appear as RBAC subjects.

Authentication, then authorisation

Every request to the API server passes three stages. Authentication establishes who is calling: a client certificate, a bearer token, a service account token, or an external identity provider. Authorisation decides whether that identity may perform the requested verb on the requested resource; on kubeadm clusters the API server runs with --authorization-mode=Node,RBAC, so the Node authoriser handles kubelets and RBAC handles everyone else. Admission control then validates or mutates the object, which is where Pod Security Admission sits.

Kubernetes has no user objects. A user is just a name that an authenticator asserted, plus a list of groups. RBAC rules refer to those names and groups, and to service accounts, which are real objects in a namespace. If you see a task asking you to create a user, it means create credentials for a name, usually a client certificate, and then grant that name permissions.

The structure of a Role

A Role is namespaced and holds a list of rules. Each rule names API groups, resources and verbs, and optionally specific resource names.

yaml
1apiVersion: rbac.authorization.k8s.io/v1
2kind: Role
3metadata:
4  name: pod-reader
5  namespace: dev
6rules:
7- apiGroups: [""]
8  resources: ["pods", "pods/log"]
9  verbs: ["get", "list", "watch"]
10- apiGroups: ["apps"]
11  resources: ["deployments"]
12  verbs: ["get", "list", "update", "patch"]

The empty string in apiGroups is the core group, which holds pods, services, configmaps, secrets, namespaces and nodes. Deployments, ReplicaSets, StatefulSets and DaemonSets are in apps. Jobs and CronJobs are in batch. Ingresses and NetworkPolicies are in networking.k8s.io. If you get the group wrong, the rule silently matches nothing, and the request is denied. kubectl api-resources shows the group of every resource, which settles the question in seconds.

bash
1kubectl api-resources | grep -E 'deployments|cronjobs|ingresses'
2# deployments   deploy   apps/v1               true   Deployment
3# cronjobs      cj       batch/v1              true   CronJob
4# ingresses     ing      networking.k8s.io/v1  true   Ingress

Verbs

The verbs map to HTTP methods and to what kubectl does. get reads one object, list reads a collection, watch streams changes, create posts a new object, update replaces it, patch modifies part of it, delete removes one, and deletecollection removes many. kubectl get pods needs list, kubectl get pod web needs get, kubectl get pods -w needs watch, kubectl apply needs get plus create or patch, and kubectl edit needs get plus update or patch. A wildcard verb grants everything, which is rarely what a task wants.

A few verbs are special. escalate and bind on roles let a subject create roles or bindings granting more than it holds. impersonate lets a subject act as another user, group or service account. use on PodSecurityPolicy existed in older releases and is gone.

Subresources and resource names

Some actions are subresources with their own names. Reading container logs needs get on pods/log. kubectl exec needs create on pods/exec, and kubectl port-forward needs create on pods/portforward. Scaling a Deployment through the scale endpoint needs update or patch on deployments/scale. A Role that grants pods but not pods/exec does not let the user exec, which is a common deliberate design and a common exam check.

resourceNames limits a rule to specific objects by name.

yaml
1- apiGroups: [""]
2  resources: ["configmaps"]
3  resourceNames: ["app-config"]
4  verbs: ["get", "update"]

resourceNames cannot restrict list, watch or create in a useful way, because those requests do not name a single object at authorisation time. A rule with resourceNames and verb list does not let the user list the collection.

Creating Roles imperatively

Writing Role YAML by hand is slow. kubectl create role does it in one line, and accepts repeated --verb and --resource flags or comma separated lists.

bash
1kubectl -n dev create role pod-reader --verb=get,list,watch --resource=pods,pods/log
2kubectl -n dev create role deploy-editor --verb=get,list,update,patch --resource=deployments.apps
3kubectl -n dev create role cm-one --verb=get,update --resource=configmaps --resource-name=app-config
4kubectl -n dev describe role pod-reader

The resource.group form, such as deployments.apps, sets the API group for you. Without it, kubectl looks the resource up and fills in the group, which also works; being explicit avoids surprises with resources whose name exists in more than one group.

ClusterRoles

A ClusterRole has the same rules but is not namespaced. It is needed for three things. Cluster scoped resources such as nodes, namespaces, persistentvolumes, storageclasses and CSRs can only be granted through a ClusterRole. Non resource URLs such as /healthz and /metrics can only be granted through a ClusterRole. And a ClusterRole can be defined once and reused in many namespaces through RoleBindings, which the next section shows.

bash
kubectl create clusterrole node-reader --verb=get,list,watch --resource=nodes
kubectl create clusterrole pv-admin --verb='*' --resource=persistentvolumes
kubectl get clusterrole view -o yaml | head -30

Clusters ship with default ClusterRoles worth knowing. cluster-admin can do anything. admin gives broad rights inside a namespace when bound with a RoleBinding, including creating Roles and RoleBindings there. edit allows reading and writing most namespaced objects but not Roles or bindings. view is read only and deliberately excludes Secrets. Many tasks are answered fastest by binding one of these rather than writing a new role.

Aggregated ClusterRoles

The admin, edit and view roles are aggregated: their rules are assembled from any ClusterRole carrying a matching label, such as rbac.authorization.k8s.io/aggregate-to-view: "true". A custom resource operator can ship a ClusterRole with that label, and every user bound to view automatically gains read access to the new resource. You will rarely write one on the exam, but recognising aggregationRule in a ClusterRole explains why its rules list looks empty in the manifest yet is full when you get it from the cluster.

RBAC is additive

There are no deny rules. Every rule grants something, and a subject's permissions are the union of every Role and ClusterRole bound to it or to any of its groups. To take away a permission, you must remove the binding or edit the role that grants it. If a user can do something you did not expect, look for a binding to one of their groups, especially system:authenticated, which every authenticated user belongs to.

Reading an existing role quickly

Troubleshooting tasks often hand you a role someone else wrote. kubectl describe role prints one line per resource with its verbs and resource names, which is easier to scan than YAML. Look for three things in order: whether the API group column is right for each resource, whether the verbs cover what the client actually does, and whether a resourceNames restriction quietly blocks list or create. Then compare with kubectl auth can-i for the exact request. A role that grants update but not patch is a frequent find, because kubectl apply and kubectl edit use patch, while some client libraries use update, so the same role works from one tool and fails from another.

Wildcards and their risks

A rule with resources set to an asterisk grants every resource in the listed groups, including ones added later by custom resource definitions. A rule with apiGroups set to an asterisk covers every group, present and future. Combining both with verb asterisk is effectively cluster-admin within the binding's scope. Wildcards make roles short to write and hard to reason about, and they tend to survive long after the reason for them is forgotten. On the exam, never use a wildcard unless the task says all resources or all verbs. In reviews, treat any custom wildcard rule as a finding to justify or replace.

Roles for non resource URLs

The API server also serves paths that are not resources, such as /healthz, /livez, /readyz, /version and /metrics. Access to them is granted with nonResourceURLs in a ClusterRole rule, using verbs such as get. Monitoring systems that scrape API server metrics need such a rule, bound with a ClusterRoleBinding. kubectl create clusterrole metrics-reader --verb=get --non-resource-url=/metrics does it in one line.

The API group is part of the permission

A rule for deployments in the core group matches nothing, because deployments live in apps. When a Role looks right but the request is denied, check the group with kubectl api-resources before anything else.