Sub topic 9
RBAC and Security Considerations
This chapter assumes you have used kubectl debug to attach an ephemeral container to a pod. It also assumes you know basic Kubernetes RBAC (Role, RoleBinding, verbs, resources). Debug containers can read process memory, files, and network traffic, so you need to control who can create them and what they can do.
Why debugging needs its own permissions
Adding an ephemeral container does not go through the ordinary pod update path. The API server exposes it as a separate subresource, pods/ephemeralcontainers. A user who can update or patch pods cannot add an ephemeral container unless they also hold rights on that subresource.
This split lets you give some people permission to change pods, such as editing labels, without letting them inject a container. It also lets you give debug access without letting people edit pod specs in general.
A debug session involves several API calls, and each needs its own permission:
| Step | API resource | Verb |
|---|---|---|
| Find the pod | `pods` | `get`, `list` |
| Add the ephemeral container | `pods/ephemeralcontainers` | `patch`, `update` |
| Open the interactive session | `pods/attach` | `create` |
| Read the new container's output | `pods/log` | `get` |
The pods/log permission matters only if the user runs kubectl logs -c against the debug container. An interactive kubectl debug -it session uses attach.
A minimal Role
The following Role allows debugging pods in the dev namespace and nothing more:
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: pod-debugger
namespace: dev
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list"]
- apiGroups: [""]
resources: ["pods/ephemeralcontainers"]
verbs: ["patch", "update"]
- apiGroups: [""]
resources: ["pods/attach"]
verbs: ["create"]Bind it to a user or group with a RoleBinding:
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: pod-debugger-binding
namespace: dev
subjects:
- kind: Group
name: dev-oncall
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: pod-debugger
apiGroup: rbac.authorization.k8s.ioYou can check the result without logging in as that user:
kubectl auth can-i patch pods/ephemeralcontainers -n dev --as-group=dev-oncall --as=jane
kubectl auth can-i create pods -n dev --as-group=dev-oncall --as=janeThe first command should print yes. The second should print no, which confirms the Role grants debugging without general pod creation.
What the other techniques need
The permissions above cover only ephemeral containers. Other kubectl debug modes need more:
--copy-tocreates a new pod, so the user needscreateonpods. The copy also runs with the original pod's service account and mounts, so the user gains whatever that pod can reach.kubectl debug node/NAMEcreates a pod on a node. The user needscreateonpodsin the target namespace and the right to read nodes (getonnodes).
Because these modes need broader rights, keep them out of a default debugging Role.
Security implications of a debug container
An ephemeral container is a real container in the pod's sandbox. Keep these facts in mind when you decide who gets access.
- Shared network. All containers in a pod share one network namespace. A debug container can reach
localhostports, and with the right capabilities it can capture traffic. - Shared volumes and secrets. A debug container does not automatically mount the application's volumes. With
--targetand a shared process namespace, though, it can read the application's filesystem through/proc/<pid>/root. This can expose mounted Secrets and tokens. - Service account token. The pod's service account token is available to processes that can read the application's files. Debug access is therefore a path to that account's permissions.
- Process visibility. Environment variables of the target process can be read from
/proc/<pid>/environ, which often holds credentials.
Treat pods/ephemeralcontainers as nearly equivalent to read access to the secrets of every pod the user can debug.
Limiting privilege with profiles
The --profile flag sets the security context of the debug container. Pick the weakest profile that works.
kubectl debug -it web-7d4b9c-abcde --image=busybox:1.36 --target=app --profile=generalProfiles range from restricted settings to broad ones such as sysadmin, which runs privileged. A privileged debug container can escape most isolation on the node. RBAC does not distinguish between profiles, because the profile is a client-side setting. A user with the permissions above can ask for any profile that the cluster's admission rules allow.
To enforce limits, use cluster-side controls:
- Pod Security Admission. Namespaces labeled
pod-security.kubernetes.io/enforce=restrictedreject ephemeral containers that request privileged mode or extra capabilities, since the checks apply to the pod spec after the update. - Admission policies. A ValidatingAdmissionPolicy or tools such as Kyverno or OPA Gatekeeper can restrict which images ephemeral containers may use. For example, allow only an approved registry.
- Audit logging. Requests to
pods/ephemeralcontainersappear in the audit log with the user, pod, and image. Alert on them in production namespaces.
Common errors
pods "web-..." is forbidden: User "jane" cannot patch resource "pods/ephemeralcontainers". The Role lacks the subresource rule. Addingpodswithpatchdoes not fix it.- The session adds the container but cannot attach. The Role lacks
createonpods/attach. - The request is rejected with a Pod Security violation. Use a less privileged
--profile, or ask a cluster admin whether the namespace policy allows the profile you need.
Exercise
A developer can run kubectl edit pod in the dev namespace but gets a "forbidden" error from kubectl debug -it on the same pod. Which rule is missing from their Role, and why does the existing pod permission not cover it?
Show answer
The Role needs patch and update on the pods/ephemeralcontainers subresource, plus create on pods/attach for the interactive session. Ephemeral containers are added through a separate subresource, so permissions on the pods resource do not apply to it.
Exercise
Why is enforcing a Pod Security Admission level a better control than RBAC for preventing a privileged debug container?
Show answer
RBAC authorizes access to the subresource but does not inspect which profile or security context the request asks for. Pod Security Admission checks the resulting pod spec and rejects ephemeral containers that violate the namespace's policy.