All Policies

Add Default securityContext

A Pod securityContext entry defines fields such as the user and group which should be used to run the Pod. Sometimes choosing default values for users rather than blocking is a better alternative to not impede such Pod definitions. This policy will mutate a Pod to set `runAsNonRoot`, `runAsUser`, `runAsGroup`, and `fsGroup` fields within the Pod securityContext if they are not already set.

Policy Definition

/other/add-default-securitycontext/add-default-securitycontext.yaml

 1apiVersion: kyverno.io/v1
 2kind: ClusterPolicy
 3metadata:
 4  name: add-default-securitycontext
 5  annotations:
 6    policies.kyverno.io/title: Add Default securityContext
 7    policies.kyverno.io/category: Sample
 8    policies.kyverno.io/subject: Pod
 9    policies.kyverno.io/minversion: 1.6.0
10    policies.kyverno.io/description: >-
11      A Pod securityContext entry defines fields such as the user and group which should be used to run the Pod.
12      Sometimes choosing default values for users rather than blocking is a better alternative to not impede
13      such Pod definitions. This policy will mutate a Pod to set `runAsNonRoot`, `runAsUser`, `runAsGroup`, and 
14      `fsGroup` fields within the Pod securityContext if they are not already set.
15spec:
16  rules:
17  - name: add-default-securitycontext
18    match:
19      any:
20      - resources:
21          kinds:
22          - Pod
23    mutate:
24      patchStrategicMerge:
25        spec:
26          securityContext:
27            +(runAsNonRoot): true
28            +(runAsUser): 1000
29            +(runAsGroup): 3000
30            +(fsGroup): 2000