All Policies

Remove ServiceAccount Token

Pods running with a ServiceAccount are presented with a volume, containing the token, and volume mounts for all containers in the Pod. Applications that do not need to communicate with the Kubernetes API do not need a ServiceAccount and therefore limiting which Pods have access rights is important. Rather than, or in addition to, requiring that certain Pods disable mounting of a ServiceAccount, it is possible to silently remove this token if it has been presented. This policy ensures that Pods which do not have the label `corp.org/can-use-serviceaccount` and are consuming a ServiceAccount have that stripped away. It should be customized to restrict the scope of its operation as it will not distinguish between an explicitly-defined ServiceAccount or one provided by default.

Policy Definition

/other/remove-serviceaccount-token/remove-serviceaccount-token.yaml

 1apiVersion: kyverno.io/v1
 2kind: ClusterPolicy
 3metadata:
 4  name: remove-serviceaccount-token
 5  annotations:
 6    policies.kyverno.io/title: Remove ServiceAccount Token
 7    policies.kyverno.io/category: Other
 8    policies.kyverno.io/severity: medium
 9    policies.kyverno.io/subject: Pod,ServiceAccount,Volume
10    kyverno.io/kyverno-version: 1.10.0
11    policies.kyverno.io/minversion: 1.10.0
12    kyverno.io/kubernetes-version: "1.25"
13    policies.kyverno.io/description: >-
14      Pods running with a ServiceAccount are presented with a volume, containing
15      the token, and volume mounts for all containers in the Pod. Applications that
16      do not need to communicate with the Kubernetes API do not need a ServiceAccount and
17      therefore limiting which Pods have access rights is important. Rather than, or in addition to,
18      requiring that certain Pods disable mounting of a ServiceAccount, it is possible to silently
19      remove this token if it has been presented. This policy ensures that Pods which do not
20      have the label `corp.org/can-use-serviceaccount` and are consuming a ServiceAccount
21      have that stripped away. It should be customized to restrict the scope of its operation as it
22      will not distinguish between an explicitly-defined ServiceAccount or one provided by default.
23spec:
24  background: false
25  rules:
26    - name: remove-vol-volmount
27      match:
28        any:
29        - resources:
30            kinds:
31            - Pod
32            selector:
33              matchExpressions:
34              - key: corp.org/can-use-serviceaccount
35                operator: DoesNotExist
36      context:
37      - name: tokenvolname
38        variable:
39          jmesPath: request.object.spec.volumes[?projected].name[?starts_with(@,'kube-api-access-')] | [0] || ''
40          default: ''
41      preconditions:
42        all:
43        - key: "{{ tokenvolname }}"
44          operator: Equals
45          value: "?*"
46      mutate:
47        foreach:
48          - list: request.object.spec.volumes[]
49            order: Descending
50            preconditions:
51              all:
52              - key: projected
53                operator: AnyIn
54                value: "{{ element.keys(@) }}"
55              - key: "{{ element.name }}"
56                operator: Equals
57                value: "kube-api-access-*"
58            patchesJson6902: |-
59              - path: /spec/volumes/{{elementIndex}}
60                op: remove
61          - list: request.object.spec.containers[]
62            foreach:
63            - list: element.volumeMounts
64              order: Descending
65              preconditions:
66                all:
67                - key: "{{element.name}}"
68                  operator: AnyIn
69                  value: "{{ tokenvolname }}"
70              patchesJson6902: |-
71                - path: /spec/containers/{{elementIndex0}}/volumeMounts/{{elementIndex1}}
72                  op: remove