Testing RBAC Permissions
What You'll Learn
You'll learn how to verify whether a user, group, or ServiceAccount has specific permissions in your Kubernetes cluster using the built-in authorization check command.
Why Test RBAC Permissions?
After creating Roles, ClusterRoles, and their bindings, you need to confirm they work as intended. Rather than attempting an action and watching it fail, Kubernetes provides a safe way to check permissions beforehand.
Think of it like checking if you have the right key for a door before trying to unlock it—you can verify access without actually performing the operation.
Using kubectl auth can-i
The kubectl auth can-i command lets you test whether a specific action is allowed. You can check your own permissions or impersonate another subject to verify their access.
Check Your Own Permissions
# Can I create pods in the default namespace?
kubectl auth can-i create pods
# Can I delete services in the production namespace?
kubectl auth can-i delete services -n production
# Can I get nodes (cluster-scoped)?
kubectl auth can-i get nodes
The command returns yes or no, making it scriptable and perfect for troubleshooting.
Check Another Subject's Permissions
# Can the ServiceAccount 'myapp' create deployments?
kubectl auth can-i create deployments --as=system:serviceaccount:default:myapp
# Can user 'jane' list secrets in 'dev' namespace?
kubectl auth can-i list secrets --as=jane -n dev
Common Testing Patterns
# Test wildcard permissions
kubectl auth can-i '*' '*'
# Test specific resource and subresource
kubectl auth can-i get pods/log -n kube-system
Key Takeaway: Use kubectl auth can-i to safely verify RBAC permissions before deployment, troubleshoot access issues, and validate security configurations without risking unauthorized actions.