All Policies

Restrict Service Account

Users may be able to specify any ServiceAccount which exists in their Namespace without restrictions. Confining Pods to a list of authorized ServiceAccounts can be useful to ensure applications in those Pods do not have more privileges than they should. This policy verifies that in the `staging` Namespace the ServiceAccount being specified is matched based on the image and name of the container. For example: 'sa-name: ["registry/image-name"]'

Policy Definition

/other/restrict_service_account.yaml

 1apiVersion: kyverno.io/v1
 2kind: ClusterPolicy
 3metadata:
 4  name: restrict-service-account
 5  annotations:
 6    policies.kyverno.io/title: Restrict Service Account
 7    policies.kyverno.io/category: Sample
 8    policies.kyverno.io/severity: medium
 9    policies.kyverno.io/subject: Pod,ServiceAccount
10    policies.kyverno.io/minversion: 1.3.5
11    policies.kyverno.io/description: >-
12      Users may be able to specify any ServiceAccount which exists in their Namespace without
13      restrictions. Confining Pods to a list of authorized ServiceAccounts can be useful to
14      ensure applications in those Pods do not have more privileges than they should.
15      This policy verifies that in the `staging` Namespace the ServiceAccount being
16      specified is matched based on the image and name of the container. For example:
17      'sa-name: ["registry/image-name"]'      
18spec:
19  validationFailureAction: audit
20  background: true
21  rules:
22  - name: validate-service-account
23    context:
24    - name: saMap
25      configMap:
26        name: sa-map
27        namespace: staging
28    match:
29      resources:
30        kinds:
31        - Pod
32        namespaces:
33        - staging
34    validate:
35      message: "Invalid service account {{ request.object.spec.serviceAccountName }} for image {{ images.containers.*.registry | [0] }}/{{ images.containers.*.name | [0] }}"
36      deny:
37        conditions:
38          any:
39          - key: "{{ images.containers.*.registry | [0] }}/{{ images.containers.*.name | [0] }}"
40            operator: NotIn
41            value: "{{ saMap.data.\"{{ request.object.spec.serviceAccountName }}\" }}"
42