K8s RBAC serviceaccount with extended permissions

12/18/2019

I am looking for option to have serviceaccount (which is not cluster admins) to be able to create new namespace and automatically get admin permission on it (while all system namespaces are not editable by this serviceaccount).

Currently my serviceaccount binded to clusterrole contains

- apiGroups:
  - ""
  resources:
  - namespaces
  verbs:
  - create

However it can't do anything on namespace it creates. Would like to get any suggestions, recommendations.

-- Michael
kubernetes
rbac

1 Answer

12/19/2019

Unfortunately, it is not possible to do using RBAC objects only.

In the RBAC API, a role contains rules that represent a set of permissions. Permissions are purely additive (there are no “deny” rules).

However there is a possible workaround:

  • you add a pod with Kubernetes python/go/java client inside, using service account with create-Roles+RoleBindings-permissions on a cluster level

  • then you run a simple code that monitor api-server for new namespaces and create RoleBinding for the namespace-admin in the new namespace

  • this RoleBinding refers to cluster-admin role, which gives the user all permission in the specific namespace

  • to differentiate new namespaces from existing ones, the script can also put a label on the namespace: namespace-admin=true or similar.

Also, you could consider using RBAC Manager. Dynamic Namespaces and Labels to be more specific.

RBAC Definitions can now include namespaceSelectors in place of namespace attributes when specifying Role Binding configuration. This can be incredibly helpful when working with dynamically provisioned namespaces.

Please let me know if that helped.

-- OhHiMark
Source: StackOverflow