Sub topic 14
Troubleshooting Forbidden errors
This chapter assumes you know Roles, RoleBindings, ClusterRoles, ClusterRoleBindings, and service accounts, and that you can use kubectl auth can-i. A Forbidden error means the API server knew who you are (authentication succeeded) but no RBAC rule allows the action (authorization failed). Because RBAC is deny-by-default, the cause is always a missing grant or a grant that does not match the request.
Read the error message
The message tells you almost everything you need:
Error from server (Forbidden): pods is forbidden: User "jane" cannot delete resource "pods" in API group "" in the namespace "default"Break it into five parts:
- Subject:
User "jane". For a workload this readsserviceaccount "app-sa" in namespace "default", orsystem:serviceaccount:default:app-sa. - Verb:
delete. - Resource:
pods. A subresource appears aspods/logorpods/exec. - API group:
""is the core group. - Scope: namespace
default, orat the cluster scope.
A missing or wrong value in any of these five is the bug.
Reproduce with can-i
Use impersonation to confirm the failure without logging in as the subject:
kubectl auth can-i delete pods --namespace default --as jane
kubectl auth can-i get pods/log --namespace default --as jane
kubectl auth can-i list secrets --as system:serviceaccount:default:app-saImpersonating requires your own account to hold the impersonate permission. Cluster admins have it by default.
Find the missing rule or binding
List what exists, then compare it to the five parts of the message:
kubectl get rolebindings,clusterrolebindings -A -o wide | grep -E 'jane|app-sa'
kubectl describe rolebinding read-pods --namespace default
kubectl describe role pod-reader --namespace default
kubectl auth can-i --list --namespace default --as janekubectl auth can-i --list prints every verb and resource the subject may use in that namespace. It is the quickest way to see the gap.
Common causes
| Symptom | Likely cause | Fix |
|---|---|---|
| Works in one namespace, fails in another | A RoleBinding only applies in its own namespace | Add a RoleBinding there, or use a ClusterRoleBinding |
| `pods` allowed but `pods/log` or `pods/exec` denied | Subresources are separate resources | Add `pods/log` or `pods/exec` to `resources` |
| Rule exists but still denied | Wrong `apiGroups`, such as `""` used for `deployments` | Use `apps` for deployments |
| Binding exists but has no effect | `roleRef` names a Role that does not exist, or the subject name is misspelled | Check names with `kubectl describe` |
| Service account denied | Binding lists the wrong `namespace` for the service account | Set the subject namespace to the account's own namespace |
| Pod uses the wrong identity | Pod runs as the `default` service account | Set `serviceAccountName` in the pod spec |
| Group grant ignored | The identity provider does not send that group | Check the groups in the user's token or certificate |
| Access limited to some objects | `resourceNames` restricts the rule, and it does not work for `list` or `create` | Remove `resourceNames` for those verbs |
Fix example
Suppose app-sa gets deployments is forbidden when listing deployments. The Role pod-reader only covers pods in the core group. Add a rule with the right group:
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: default
name: pod-reader
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "watch", "list"]
- apiGroups: ["apps"]
resources: ["deployments"]
verbs: ["get", "list"]Apply it, then rerun the same can-i check. Do not fix a Forbidden error by granting cluster-admin. Add only the missing rule.
Exercise
A pod running as app-sa in namespace default can list pods in default but gets "pods is forbidden ... in the namespace "staging"". A RoleBinding named app-sa-read-pods exists in default. What is wrong, and what is the least-privilege fix?
Show answer
A RoleBinding grants access only in its own namespace, so the binding in default does nothing in staging. Create a RoleBinding in staging that references the pod-reader Role (created in staging) or a ClusterRole, with the subject app-sa and namespace default. Do not use a ClusterRoleBinding unless access is needed in every namespace.
Exercise
A user can get pods but kubectl logs mypod returns Forbidden. Which rule is missing?
Show answer
A rule for the subresource: resources: ["pods/log"] with verb get, in the correct API group ("").