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/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 kyverno.io/kyverno-version: 1.6.2
12 kyverno.io/kubernetes-version: "1.23"
13 policies.kyverno.io/description: >-
14 Users may be able to specify any ServiceAccount which exists in their Namespace without
15 restrictions. Confining Pods to a list of authorized ServiceAccounts can be useful to
16 ensure applications in those Pods do not have more privileges than they should.
17 This policy verifies that in the `staging` Namespace the ServiceAccount being
18 specified is matched based on the image and name of the container. For example:
19 'sa-name: ["registry/image-name"]'
20spec:
21 validationFailureAction: audit
22 background: true
23 rules:
24 - name: validate-service-account
25 context:
26 - name: saMap
27 configMap:
28 name: sa-map
29 namespace: staging
30 match:
31 resources:
32 kinds:
33 - Pod
34 namespaces:
35 - staging
36 validate:
37 message: "Invalid service account {{ request.object.spec.serviceAccountName }} for image {{ images.containers.*.registry | [0] }}/{{ images.containers.*.name | [0] }}"
38 deny:
39 conditions:
40 any:
41 - key: "{{ images.containers.*.registry | [0] }}/{{ images.containers.*.name | [0] }}"
42 operator: NotIn
43 value: "{{ saMap.data.\"{{ request.object.spec.serviceAccountName }}\" }}"