All Policies

Block Pod Exec by Pod Label

The `exec` command may be used to gain shell access, or run other commands, in a Pod's container. While this can be useful for troubleshooting purposes, it could represent an attack vector and is discouraged. This policy blocks Pod exec commands to Pods having the label `exec=false`.

Policy Definition

/other/block-pod-exec-by-pod-label.yaml

 1apiVersion: kyverno.io/v1
 2kind: ClusterPolicy
 3metadata:
 4  name: deny-exec-by-pod-label
 5  annotations:
 6    policies.kyverno.io/title: Block Pod Exec by Pod Label
 7    policies.kyverno.io/category: Sample
 8    policies.kyverno.io/minversion: 1.4.2
 9    policies.kyverno.io/subject: Pod
10    policies.kyverno.io/description: >-
11      The `exec` command may be used to gain shell access, or run other commands, in a Pod's container. While this can
12      be useful for troubleshooting purposes, it could represent an attack vector and is discouraged.
13      This policy blocks Pod exec commands to Pods having the label `exec=false`.      
14spec:
15  validationFailureAction: audit
16  background: false
17  rules:
18  - name: deny-exec-by-label
19    match:
20      resources:
21        kinds:
22        - PodExecOptions
23    context:
24    - name: podexeclabel
25      apiCall:
26        urlPath: "/api/v1/namespaces/{{request.namespace}}/pods/{{request.name}}"
27        jmesPath: "metadata.labels.exec"   
28    preconditions:
29    - key: "{{ request.operation }}"
30      operator: Equals
31      value: CONNECT
32    validate:
33      message: Exec'ing into Pods protected with the label `exec=false` is forbidden.
34      deny:
35        conditions:
36          - key: "{{ podexeclabel }}"
37            operator: Equals
38            value: "false"
39