How to make a k8s role that allows all operations on roles and rolebindings in a namespace?

4/6/2020

I want to make a role that allows to do any operation on "Roles" and "RoleBindings" (but not ClusterRoles or ClusterRoleBindings) on a namespace level.

This is the roles YAML I put together but when binding it to a service account it is now applied. What did I do wrong?

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: role-binder
  namespace: foo-namespace
rules:
- apiGroups:
  - rbac.authorization.k8s.io
  resources:
  - Role
  - RoleBinding
  verbs:
  - '*'
-- Hedge
kubernetes
rbac

1 Answer

4/7/2020

You can achieve that with the following rules:

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: role-grantor
rules:
- apiGroups: ["rbac.authorization.k8s.io"]
  resources: ["rolebindings"]
  verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
- apiGroups: ["rbac.authorization.k8s.io"]
  resources: ["roles"]
  verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]

---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: role-grantor-binding
  namespace: office
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: role-grantor
subjects:
- apiGroup: rbac.authorization.k8s.io
  kind: User
  name: employee

I tested it on my lab and it works as you desire:

$ kubectl --context=employee-context get role 
NAME                 AGE
deployment-manager   15m
role-binder          12m

$ kubectl --context=employee-context get rolebindings
NAME                         AGE
deployment-manager-binding   15m
role-grantor-binding         3m37s

$ kubectl --context=employee-context get clusterrolebindings
Error from server (Forbidden): clusterrolebindings.rbac.authorization.k8s.io is forbidden: User "employee" cannot list resource "clusterrolebindings" in API group "rbac.authorization.k8s.io" at the cluster scope

You can read more about this specifically in the documentation.

-- mWatney
Source: StackOverflow