All Policies

Check ServiceAccount

ServiceAccounts with privileges to create Pods may be able to do so and name a ServiceAccount other than the one used to create it. This policy checks the Pod, if created by a ServiceAccount, and ensures the `serviceAccountName` field matches the actual ServiceAccount.

Policy Definition

/other/check-serviceaccount/check-serviceaccount.yaml

 1apiVersion: kyverno.io/v1
 2kind: ClusterPolicy
 3metadata:
 4  name: check-sa
 5  annotations:
 6    policies.kyverno.io/title: Check ServiceAccount
 7    policies.kyverno.io/category: Sample
 8    policies.kyverno.io/subject: Pod,ServiceAccount
 9    kyverno.io/kyverno-version: 1.6.0
10    policies.kyverno.io/minversion: 1.6.0
11    kyverno.io/kubernetes-version: "1.21"
12    policies.kyverno.io/description: >-
13      ServiceAccounts with privileges to create Pods may be able to do so and name
14      a ServiceAccount other than the one used to create it. This policy checks the
15      Pod, if created by a ServiceAccount, and ensures the `serviceAccountName` field
16      matches the actual ServiceAccount.
17spec:
18  validationFailureAction: Audit
19  background: false
20  rules:
21    - name: check-sa
22      match:
23        any:
24        - resources:
25            kinds:
26            - Pod
27      preconditions:
28        all:
29        - key: "{{serviceAccountName}}"
30          operator: Equals
31          value: "*?"
32        - key: "{{request.operation}}"
33          operator: Equals
34          value: CREATE
35      validate:
36        message: "The ServiceAccount used to create this Pod is confined to using the same account when running the Pod."
37        pattern:
38          spec:
39            serviceAccountName: "{{serviceAccountName}}"