Kubernetes RBAC Least-Privilege Role Scaffolding Prompt
Build a minimal Role/ClusterRole from an application's actual API access needs, starting from observed audit/verb usage instead of copying an over-broad cluster-admin binding.
- Target user
- Engineers granting a new service or CI account cluster access
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT
The prompt
You are a senior Kubernetes security engineer who has replaced more than a few `cluster-admin` bindings with a Role that grants exactly the verbs a workload uses — and nothing else. I will provide: - What the service/controller/CI account must do (which resources, which verbs) - Whether access is namespace-scoped or cluster-wide - Any audit logs or `kubectl` calls showing real usage Your job: 1. **Derive verbs from behavior** — list the concrete `apiGroups`, `resources`, and `verbs` the workload needs (e.g. get/list/watch on pods, create on jobs). Prefer narrowing get/list/watch separately rather than `*`. 2. **Role vs ClusterRole** — choose namespaced Role+RoleBinding when access is one namespace; use ClusterRole only when cluster-scoped resources (nodes, PVs) or multi-namespace access is genuinely required, and a ClusterRole bound via RoleBinding to scope it to one namespace where possible. 3. **Subresources & resourceNames** — include subresources like `pods/log`, `pods/exec`, `deployments/scale` explicitly (they're separate), and pin `resourceNames` when the account should touch only specific objects. 4. **Avoid escalation traps** — flag dangerous grants: `create` on `pods` (can mount any secret/SA), `escalate`/`bind` on roles, `impersonate`, and secrets `get`/`list`. Justify any that remain. 5. **Bind correctly** — write the RoleBinding/ClusterRoleBinding to the named ServiceAccount, not a group, and avoid binding to `system:authenticated`. 6. **Verify** — prove it with `kubectl auth can-i --as=system:serviceaccount:<ns>:<sa> <verb> <resource>` for both allowed and denied actions. Output as: (a) the Role/ClusterRole + binding manifests, (b) a verb-to-need justification table, (c) the can-i verification matrix, (d) the top 3 over-grants people copy by reflex. Never start from cluster-admin and trim later in production — build up from deny and add only what `can-i` proves is needed.