Securing Exec and Attach Access
Required knowledge for the CKS certification.
The pods/exec and pods/attach subresources provide powerful debugging capabilities but also represent significant security risks. Unrestricted access allows attackers to execute arbitrary commands inside containers, steal credentials, and pivot to connected systems.
Issue: By default, many RBAC roles grant broad exec and attach permissions, allowing users to access any container in authorized namespaces and extract sensitive data.
Fix: Apply strict RBAC controls, implement admission policies, enable audit logging, and use alternative debugging methods that don't require exec access.
1. Restrict RBAC Permissions for Exec and Attach
Issue: Overly permissive roles grant exec and attach access to all pods, enabling credential theft from any container.
Fix: Grant exec/attach permissions only when necessary and restrict to specific pods or namespaces.
Deny Exec and Attach by Default
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: developer-readonly
namespace: production
rules:
- apiGroups: [""]
resources: ["pods", "pods/log"]
verbs: ["get", "list", "watch"]
- apiGroups: ["apps"]
resources: ["deployments", "replicasets"]
verbs: ["get", "list"]
# Explicitly exclude pods/exec and pods/attach
Grant Exec Access to Specific Pods Only
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: debug-specific-app
namespace: production
rules:
- apiGroups: [""]
resources: ["pods/exec"]
verbs: ["create"]
resourceNames:
- "debug-pod-12345"
- "troubleshooting-pod-67890"
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: debug-binding
namespace: production
subjects:
- kind: User
name: operator@company.com
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: debug-specific-app
apiGroup: rbac.authorization.k8s.io