# k8s-admission-control / kubernetes / t0 "Quick start" # # Three Kyverno policies that flag pods which make container escape easy: # privileged mode, host paths, and the host's process, IPC, or network # namespace. They start in Audit mode, so nothing is blocked. Each violation is # recorded in a policy report you can read before deciding to enforce. # # Needs Kyverno 1.13 or later already in the cluster. The Standard version # installs it through Argo CD. # # kubectl apply -f policies.yaml # # Verify: # kubectl run priv-test --image=busybox --overrides='{"spec":{"containers":[{"name":"t","image":"busybox","command":["sleep","60"],"securityContext":{"privileged":true}}]}}' # kubectl get policyreport -A # The report lists priv-test as failing disallow-privileged-containers. --- apiVersion: kyverno.io/v1 kind: ClusterPolicy metadata: name: disallow-privileged-containers annotations: policies.kyverno.io/description: A privileged container can do almost anything the node can. spec: background: true rules: - name: no-privileged match: any: - resources: kinds: [Pod] validate: failureAction: Audit message: "Privileged mode is not allowed." pattern: spec: =(ephemeralContainers): - =(securityContext): =(privileged): "false" =(initContainers): - =(securityContext): =(privileged): "false" containers: - =(securityContext): =(privileged): "false" --- apiVersion: kyverno.io/v1 kind: ClusterPolicy metadata: name: disallow-host-path annotations: policies.kyverno.io/description: A host path mount can expose the node's files, including other pods' secrets. spec: background: true rules: - name: no-host-path match: any: - resources: kinds: [Pod] validate: failureAction: Audit message: "hostPath volumes are not allowed." pattern: spec: =(volumes): - X(hostPath): "null" --- apiVersion: kyverno.io/v1 kind: ClusterPolicy metadata: name: disallow-host-namespaces annotations: policies.kyverno.io/description: Sharing the host's process, IPC, or network namespace lets a pod see and reach the node. spec: background: true rules: - name: no-host-namespaces match: any: - resources: kinds: [Pod] validate: failureAction: Audit message: "hostPID, hostIPC, and hostNetwork are not allowed." pattern: spec: =(hostPID): "false" =(hostIPC): "false" =(hostNetwork): "false"