2

We're creating dynamic test environments for our developers. Each environment goes into one namespace called test-<something>, where <something> is entered by the developer when creating the environment (we use Gitlab-CI for the automation).

We want to grant them limited access to the K8s API to see deployments, exec into pods for instance. So the plan is to apply a (cluster)role (yet to decide) like this.

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: "{{ ns }}"
  name: "test-{{ ns }}"
rules:
- apiGroups: ["apps"]
  resources: ["deploys"]
  verbs: ["get", "list"]
- apiGroups: [""]
  resources: ["pods", "pods/log"]
  verbs: ["get", "list"]
- apiGroups: [""]
  resources: ["pods/exec"]
  verbs: ["create"]

However we preferably don't want to apply it for all namespaces but only the test-* ones.

We could add the creation of the namespaced role and rolebinding during the app deploy, but that would mean granting our Gitlab-CI runner the permission to create and delete roles and rolebindings. We're concerned by the security implications of this and the possible privilege escalations.

  • Is it possible to create a clusterrolebinding limited to a regexp-ed set of namespaces?
  • Alternatively, if we want to grant the permissions via the automation, is it possible to limit the namespaces where the rolebindings can be created by the runner?

We looked at the docs but couldn't find such things

Worst case scenario is probably to go for the clusterrolebinding route and not give too many privileges to the automation. So asking if we can find a better way

Thanks in advance

dev93
  • 337
  • 4
  • 14

1 Answers1

1

I also stumbled into this problem and Hierarchical Namespaces seem like a decent solution, as you can give the permissions to a single "static" namespace. Every namespace afterwards will inherit the permissions. Hope it helps.

dsax7
  • 1,333
  • 22
  • 36
  • Thanks for that. We went for the perms on all namespaces for now but will keep that in mind should we want to refactor – dev93 Nov 09 '22 at 19:57