Admission policy exemptions
This guide is for cluster admins. It shows how to allow the DebugHero debug container in clusters with Pod Security Admission, Kyverno, or OPA Gatekeeper, with the smallest possible exemption.
An exemption lets the debug container trace any process in the target container. Give it only for the namespaces and the users that need it. Test each change on a non-production cluster first.
What the debug container needs
debugd adds this ephemeral container to the target pod:
ephemeralContainers:
- name: debughero-x7k2p9 # debughero- and 6 random characters, new for each session
image: madhavhugar/debughero:0.0.9
targetContainerName: <target container>
securityContext:
runAsUser: 0
allowPrivilegeEscalation: true
capabilities:
add: ["SYS_PTRACE"]
seccompProfile:
type: RuntimeDefault
The request is an update on the pods/ephemeralcontainers subresource, made with the credentials of the developer.
| Setting | Why | Blocked by |
|---|---|---|
capabilities.add: SYS_PTRACE | Delve uses ptrace to attach to a process of another user. | PSA baseline and restricted, capability policies |
runAsUser: 0 | Delve and the debug server read /proc/<pid>/root of the target. | PSA restricted, non-root policies |
allowPrivilegeEscalation: true | Needed together with the added capability. | PSA restricted, privilege-escalation policies |
Pod Security Admission
Pod Security Admission also checks ephemeral containers. baseline rejects SYS_PTRACE. restricted also rejects the root user and privilege escalation.
Option 1: set the namespace to privileged
kubectl label namespace <namespace> \
pod-security.kubernetes.io/enforce=privileged --overwrite
Keep the warn and audit labels at baseline, so that you still see the pods that would break the policy:
kubectl label namespace <namespace> \
pod-security.kubernetes.io/warn=baseline \
pod-security.kubernetes.io/audit=baseline --overwrite
This option is simple, but every pod in the namespace can then run privileged. Use it for development and staging namespaces.
Option 2: exempt the developers
Pod Security Admission can ignore the requests of named users. This needs access to the API server configuration, so it is possible only on self-managed clusters. EKS, GKE and AKS do not let you change it.
apiVersion: apiserver.config.k8s.io/v1
kind: AdmissionConfiguration
plugins:
- name: PodSecurity
configuration:
apiVersion: pod-security.admission.config.k8s.io/v1
kind: PodSecurityConfiguration
defaults:
enforce: baseline
enforce-version: latest
exemptions:
usernames: ["alice@example.com", "bob@example.com"]
Exemptions apply to user names only, not to groups. An exempt user is exempt for all the pods that the user creates or changes.
Kyverno
If your policies use the Kyverno podSecurity rule, exclude the debug image from the controls that it breaks:
validate:
podSecurity:
level: baseline
version: latest
exclude:
- controlName: Capabilities
images:
- "madhavhugar/debughero*"
For restricted, also exclude the Privilege Escalation, Running as Non-root and Running as Non-root user controls for the same image.
If your policies use separate rules, such as disallow-capabilities from the Kyverno policy library, create a PolicyException for the namespace:
apiVersion: kyverno.io/v2
kind: PolicyException
metadata:
name: debughero
namespace: <exceptions namespace>
spec:
exceptions:
- policyName: disallow-capabilities
ruleNames:
- adding-capabilities
- autogen-adding-capabilities
match:
any:
- resources:
kinds: ["Pod"]
namespaces: ["<namespace>"]
Policy exceptions must be enabled in Kyverno, and Kyverno must accept exceptions from the namespace of the PolicyException. A PolicyException applies to all the pods in the namespace, not only to the debug container. The podSecurity exclude above is narrower.
OPA Gatekeeper
The pod security templates of the Gatekeeper library have an exemptImages parameter. Add the debug image to each constraint that rejects the debug container:
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sPSPCapabilities
metadata:
name: capabilities
spec:
match:
kinds:
- apiGroups: [""]
kinds: ["Pod"]
parameters:
allowedCapabilities: []
requiredDropCapabilities: []
exemptImages:
- "madhavhugar/debughero:*"
Do the same for the constraints that use K8sPSPAllowPrivilegeEscalationContainer and K8sPSPAllowedUsers, if you use them.
Check the result
To see if your user can add the debug container, start a session and run debugctl get sessions. If an admission controller rejects the container, the session status is FAILED. LAST_ERRORS starts with the name of the admission controller, for example blocked by Pod Security Admission: or blocked by Kyverno:. Then it contains the error from the Kubernetes API, with the message of the admission controller, and a link to this page. The debugd log contains debug session failed: debug container blocked by an admission policy.
If LAST_ERRORS starts with missing kubernetes permissions, the problem is RBAC, not an admission policy. See Cluster requirements: Permissions.
If you use an image mirror
Use the image name of your mirror in the exemptions. Set the same image for debugd with DEBUG_IMAGE. See Environment variables.