k8s rbac role, rolebinding and testing
In AKS we can setup rbac for service acount. Let's create a service account and then using the following yaml definition to setup it access to list pods.
Next we will create a role that only allows this service account to list and watch pods in dev namespace, Role is reusable.
Here we can see the common apiGroups that you can use and please ensure you do not omit the (s) - plural as will NOT work
| API Group | Common Resources |
"" (Core) | pods, services, nodes, namespaces, configmaps, secrets, persistentvolumeclaims |
| apps | deployments, statefulsets, daemonsets, replicasets |
| batch | jobs, cronjobs |
| networking.k8s.io | ingresses, networkpolicies |
| autoscaling | horizontalpodautoscalers |
| storage.k8s.io | storageclasses, csinodes |
for example, if we would like to specifiy that it can list cronjob, it will be like this
Next we will tied the user to the role using rolebinding as shown here. Please note, rolebinding won't check if the "roleRef" target let's say group exist or not.
And to test it out we can use kubectl auth can-i command
# YES
kubectl auth can-i list cronjob --as system:serviceaccount:dev:pod-watcher -n dev
# NO
kubectl auth can-i delete cronjob --as system:serviceaccount:dev:pod-watcher -n dev
Comments