Sub topic 10
Verifying permissions with kubectl auth can-i
This chapter assumes you can apply YAML manifests with kubectl. It also assumes you know what Roles, RoleBindings, and service accounts are from earlier chapters. After you create a binding, you need a way to confirm it grants what you intended. kubectl auth can-i does that by asking the API server's authorizer a question and printing yes or no.
What the command does
The command sends a SelfSubjectAccessReview to the API server. That is a request that asks, "May I perform this verb on this resource?" The server runs the same authorization check it uses for real requests, so the answer reflects the current Roles and bindings. Nothing is created or changed in the cluster.
Checking your own access
The basic form is kubectl auth can-i VERB RESOURCE. Add --namespace to pick a namespace. Without it, kubectl uses the namespace from your current context.
Checking someone else with impersonation
The --as flag makes the request on behalf of another identity. Use --as-group to add group membership. Service accounts use the form system:serviceaccount:NAMESPACE:NAME.
kubectl auth can-i list pods --namespace default
kubectl auth can-i delete pods --namespace default --as jane
kubectl auth can-i list secrets --as system:serviceaccount:default:app-saWith the pod-reader Role and the read-pods binding from earlier chapters, the second command prints no, because pod-reader grants only get, watch, and list. The third prints no because app-sa is bound only within the default namespace and only to pods. It has no rule for secrets.
Useful options
| Option | Purpose |
|---|---|
| `--namespace NS` | Check access in a specific namespace |
| `--all-namespaces` | Check access across all namespaces |
| `--as USER` | Check for another user |
| `--as-group GROUP` | Add group membership to the impersonated identity |
| `--list` | Show every action the identity may perform in a namespace |
| `--subresource NAME` | Check a subresource such as `log` or `exec` |
| `-q` | Print nothing and only set the exit code |
You can name a single resource with RESOURCE/NAME, for example kubectl auth can-i get pods/my-pod. This matters when a rule uses resourceNames. Checking get configmaps can say no while get configmaps/app-config says yes.
Listing all permissions
The --list flag shows the full set of rules that apply to an identity in a namespace.
kubectl auth can-i --list --namespace default --as janeThe output has columns for resources, non-resource URLs, resource names, and verbs. It is a good way to spot grants that are broader than you meant.
Using it in scripts
The command exits with code 0 for yes and 1 for no. With -q, you can use it as a gate in a script.
if kubectl auth can-i create deployments --namespace default --as system:serviceaccount:default:app-sa -q; then
echo "app-sa can create deployments"
else
echo "app-sa cannot create deployments"
fiCommon pitfalls
- Wrong namespace. A RoleBinding applies in one namespace. A
yesindefaultsays nothing aboutkube-system. - Subresources. Permission on
podsdoes not coverpods/execorpods/log. Check them with--subresourceor thepods/logform. - Wrong identity. The user name must match what your authenticator reports. Group names are exact strings.
- Authorizer scope. The command reflects only what the configured authorizers decide. It does not check admission controllers, which can still reject a request that RBAC allowed.
Exercise
A RoleBinding grants the pod-reader Role to the service account app-sa in the default namespace. What command checks whether app-sa can list pods in default, and what does it print if the binding is missing?
Show answer
Run kubectl auth can-i list pods --namespace default --as system:serviceaccount:default:app-sa. It prints no when the binding is missing, and exits with code 1.
Exercise
Why might kubectl auth can-i get configmaps print no while kubectl auth can-i get configmaps/app-config prints yes?
Show answer
The Role likely uses resourceNames to allow get only on the ConfigMap named app-config. The general check asks about all ConfigMaps, which the rule does not cover.