What does a Kubernetes RBAC generator do?
Kubernetes decides who may do what with RBAC, role-based access control. You describe permissions in a Role (or a ClusterRole for the whole cluster), then attach them to people or programs with a RoleBinding (or ClusterRoleBinding). Programs running in the cluster, such as a CI job, usually need a ServiceAccount as well. That is three or four objects in YAML, and the small details are easy to get wrong: the API group of each resource, the exact verbs, the roleRef that must point to the right kind, the namespace of a ServiceAccount subject.
This tool writes those objects from a form. You choose resources and verbs, add the subjects, and it produces YAML in rbac.authorization.k8s.io/v1 that you can apply with kubectl apply -f. The API groups are filled in for you from the resource names, so deployments gets apps and jobs gets batch. It also warns when a rule is risky: a star, reading Secrets, running commands in pods, or verbs that let a user raise their own permissions.
Everything is computed in your browser. The names of your roles, users and namespaces are not uploaded. The result is plain YAML you can check with the YAML validator and keep in version control.
How to generate a Role and a RoleBinding
- Optionally pick a starting point under "Start from": read pods and logs, deploy apps, read ConfigMaps, read nodes (cluster), read /metrics (cluster) or "CI deploys one app". A preset fills in the kind, the name and the rules, and you can change anything afterwards.
- In the Role card, choose the kind, Role or ClusterRole, and type the role name and the namespace. If a role already exists, use "Role to bind" instead: pick
view,edit,adminorcluster-adminand no new role is written, only the binding. - For a ClusterRole, decide with the toggle "Bind in the whole cluster (ClusterRoleBinding)". Off, the ClusterRole is bound with a RoleBinding and only applies in that namespace.
- In Rules, type the resources separated by commas or spaces, for example
pods, pods/log. The hint under the field shows the API groups it found. Tick the verbs you need: get, list, watch, create, update, patch, delete and so on. Use "Only these names" to limit a rule to named objects, or "Non-resource URLs" such as/metricsin a ClusterRole. "Add a rule" adds another. - Under "Who gets it" add subjects: a User, a Group or a ServiceAccount. A ServiceAccount takes a namespace; if you leave it empty, the namespace of the role is used. Turn on "Also write the ServiceAccount" to include the account in the output.
- Read the yellow warnings, then copy "All in one file" or choose a single object from the file list. Apply it with
kubectl apply -f rbac.yaml.
When you are done, test the result with the kubectl cheatsheet at hand, for example kubectl auth can-i list pods --as=jane -n dev. If you start from a container and need the Deployment too, the Docker to Kubernetes converter writes the workload.
Examples
Read pods in one namespace
The first Role of the Kubernetes documentation: a user may read pods in dev. Kind Role, name pod-reader, namespace dev, resource pods, verbs get, list and watch, subject User jane.
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: pod-reader
namespace: dev
rules:
- apiGroups:
- ""
resources:
- pods
verbs:
- get
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: pod-reader-binding
namespace: dev
subjects:
- kind: User
name: jane
apiGroup: rbac.authorization.k8s.io
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: pod-readerA CI account that can update deployments
Role deployer in apps, resource deployments, verbs get, list, patch and update, subject ServiceAccount ci-bot, with "Also write the ServiceAccount" on. The API group apps appears by itself, and the ServiceAccount takes the namespace of the role.
apiVersion: v1
kind: ServiceAccount
metadata:
name: ci-bot
namespace: apps
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: deployer
namespace: apps
rules:
- apiGroups:
- apps
resources:
- deployments
verbs:
- get
- list
- patch
- updateThe RoleBinding that follows in the real output binds ci-bot in apps to this role.
Metrics for the whole cluster
A ClusterRole with the non-resource URL /metrics and the verb get, bound with a ClusterRoleBinding. Non-resource URLs are written as a rule of their own.
rules:
- nonResourceURLs:
- /metrics
verbs:
- getBind a role that already exists
Choose cluster-admin under "Role to bind", turn on the cluster-wide toggle and add a user. The output is one ClusterRoleBinding named cluster-admin-binding, and a warning says it gives full control of the cluster.
Common errors and how to fix them
The tool stops and names the problem instead of writing wrong YAML.
| Message | What to do |
|---|---|
| The role name must be lowercase letters, digits, dashes and dots, for example pod-reader. | Remove spaces, capitals and underscores from the name. |
| The namespace must be lowercase letters, digits and dashes, for example default. | Use a valid namespace name; underscores are not allowed. |
| Add at least one rule. | A role without rules grants nothing. Add a rule. |
| Rule N has no verb. Choose at least one. | Tick at least one verb in that rule. |
| Rule N needs resources or a non-resource URL. | Type a resource such as pods, or a URL such as /metrics. |
| Rule N has both resources and non-resource URLs. Use one or the other. | Split them into two rules. |
| Add at least one subject. / Subject N has no name. | Give the binding someone to bind to, and fill in the name. |
Warnings do not block the output, but read them. "A ClusterRole bound with a RoleBinding is limited to that namespace" is often what you want, and sometimes a surprise. "Non-resource URLs only work in a ClusterRole bound with a ClusterRoleBinding" means your /metrics rule would be ignored inside a Role. A warning about resource names says that names do not limit create, list, watch or deletecollection, so do not rely on them for those verbs.
FAQ
What is the difference between a Role and a ClusterRole?
A Role lives in one namespace and only grants access there. A ClusterRole is not namespaced: it can grant access to cluster-scoped resources such as nodes, to non-resource URLs, or to a resource in every namespace. A ClusterRole bound with a RoleBinding is limited to the namespace of the binding.
Why does the YAML contain an empty string in apiGroups?
The empty string is the core API group, where pods, services, configmaps, secrets and nodes live. Resources like deployments belong to apps and jobs to batch. The tool picks the group from the resource name; the hint under the resources field shows the result, and the core group is shown as core ("").
How do I check that the permissions work?
Apply the YAML, then ask the cluster as that user: kubectl auth can-i list pods --as=jane -n dev. For a ServiceAccount use --as=system:serviceaccount:apps:ci-bot. Try an action that should be denied as well, to be sure the role is not too wide.
Should I use a star to save time?
Avoid it. A star in verbs, resources or API groups also covers things added in the future. List what is needed, and give the least access that works.
Is my data sent anywhere?
No. The YAML is built in your browser and nothing is uploaded.