Sub topic 15
Least-privilege practices and auditing
This chapter assumes you know Roles, ClusterRoles, bindings, service accounts, and kubectl auth can-i. It covers how to keep grants small and how to check them over time.
What least privilege means in RBAC
Least privilege means every subject gets only the verbs on the resources it needs, in the namespaces where it needs them. RBAC is additive, so the only way to reduce access is to remove or narrow grants. Start with nothing and add rules until the workload works.
The main risks come from the topics in earlier chapters:
cluster-adminand wildcard rules (*) grant everything.- Read access to
secretsexposes credentials, even with onlyget,list, orwatch. - The
escalate,bind, andimpersonateverbs let a subject gain permissions it was never given.
Practices to apply
- Prefer a Role and RoleBinding over a ClusterRole and ClusterRoleBinding. A ClusterRoleBinding applies in every namespace. If one namespace is enough, use a RoleBinding.
- List verbs and resources explicitly. Avoid
"*"inverbs,resources, orapiGroups. UseresourceNameswhen a rule applies to named objects only. - Give each workload its own service account. Do not reuse
default, because every pod in the namespace that does not set a service account shares it. - Bind groups, not individual users. Group membership comes from your identity provider, so access changes when people change teams.
- Limit who can create bindings and roles. Whoever can create RoleBindings can hand out any permission they already hold.
- Turn off token mounting when a pod does not call the API. Set
automountServiceAccountToken: false.
apiVersion: v1
kind: ServiceAccount
metadata:
name: batch-worker
namespace: default
automountServiceAccountToken: falseA pod that never talks to the API server then holds no credential to steal.
Auditing existing permissions
Audit on a schedule, not only after an incident. Three checks cover most of the ground.
First, list what a subject can do. The --list flag prints every allowed action in a namespace:
kubectl auth can-i --list --namespace default --as system:serviceaccount:default:app-saSecond, find who holds powerful roles. List bindings and look for cluster-admin:
kubectl get clusterrolebindings -o wide
kubectl get rolebindings,clusterrolebindings --all-namespaces \
-o custom-columns='KIND:kind,NAMESPACE:metadata.namespace,NAME:metadata.name,ROLE:roleRef.name,SUBJECTS:subjects[*].name'Third, look for wildcards and sensitive verbs in the rules themselves:
kubectl get clusterroles,roles --all-namespaces -o yaml | grep -n -E '"\*"|- \*|escalate|bind|impersonate'Each hit needs an owner and a reason. Remove bindings whose subjects no longer exist.
Review the flow
Use audit logs for actual use
The commands above show what is allowed. The API server audit log shows what was done. If audit logging is enabled, search it for Forbidden responses (code 403) to find missing rules, and for activity by powerful subjects. Permissions that never appear in the logs over a long period are candidates for removal. Audit logging is configured on the API server with an audit policy. Managed clusters usually expose it through the provider's logging service.
Exercise
Exercise
A deployment's pods use the default service account in the shop namespace. A ClusterRoleBinding grants that account the edit ClusterRole. The pods only need to read ConfigMaps in shop. Describe how to fix this and how to check the result.
Show answer
Create a dedicated service account for the deployment and set it in the pod spec. Create a Role in shop with get, list, and watch on configmaps, and bind it with a RoleBinding. Remove the ClusterRoleBinding that gives default the edit role. Verify with kubectl auth can-i list configmaps -n shop --as system:serviceaccount:shop:<new-sa> (expect yes) and kubectl auth can-i delete pods -n shop --as system:serviceaccount:shop:<new-sa> (expect no). Also run can-i --list for the default account to confirm it no longer has broad access.