Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kubernetes RBAC role verbs to exec to pod

Tags:

kubernetes

I my 1.9 cluster created this deployment role for the dev user. Deployment works as expected. Now I want to give exec and logs access to developer. What role I need to add for exec to the pod?

kind: Role
name: deployment-manager
  rules:
  - apiGroups: ["", "extensions", "apps"]
    resources: ["deployments", "replicasets", "pods"]
    verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]

Error message:

kubectl exec nginx -it -- sh  

Error from server (Forbidden): pods "nginx" is forbidden: User "dev" cannot create pods/exec in the namespace "dev"

Thanks SR

like image 468
sfgroups Avatar asked Jan 05 '18 17:01

sfgroups


People also ask

How do I restrict kubectl exec?

To limit the ability to kubectl exec to pods what you want to do is create a custom Role & RoleBinding that removes the create verb for the pods/exec resource. An easy approach to this might be to copy the default RBAC policies, and then make the appropriate edit and rename.


1 Answers

The RBAC docs say that

Most resources are represented by a string representation of their name, such as “pods”, just as it appears in the URL for the relevant API endpoint. However, some Kubernetes APIs involve a “subresource”, such as the logs for a pod. [...] To represent this in an RBAC role, use a slash to delimit the resource and subresource.

To allow a subject to read both pods and pod logs, and be able to exec into the pod, you would write:

kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  namespace: default
  name: pod-and-pod-logs-reader
rules:
- apiGroups: [""]
  resources: ["pods", "pods/log"]
  verbs: ["get", "list"]
- apiGroups: [""]
  resources: ["pods/exec"]
  verbs: ["create"]

Some client libraries may do an http GET to negotiate a websocket first, which would require the "get" verb. kubectl sends an http POST instead, that's why it requires the "create" verb in that case.

like image 181
Jose Armesto Avatar answered Sep 19 '22 23:09

Jose Armesto