apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
  name: deny-privilege-escalation
  annotations:
    policies.kyverno.io/category: Pod Security Standards (Restricted)
    policies.kyverno.io/severity: medium
    policies.kyverno.io/subject: Pod
    policies.kyverno.io/description: >-
      Privilege escalation, such as via set-user-ID or set-group-ID file mode, should not be allowed.
      This policy ensures the `allowPrivilegeEscalation` fields are either undefined
      or set to `false`.      
spec:
  background: true
  validationFailureAction: enforce
  rules:
  - name: deny-privilege-escalation
    match:
      resources:
        kinds:
        - Pod
    exclude:
      clusterRoles:
      # add litmus cluster roles here
      - litmus-admin
      roles:
      # add litmus roles here
      - litmus-roles
      subjects:
      # add serviceAccount name here
      - kind: ServiceAccount
        name: pod-network-loss-sa
    validate:
      message: >-
        Privilege escalation is disallowed. The fields
        spec.containers[*].securityContext.allowPrivilegeEscalation, and
        spec.initContainers[*].securityContext.allowPrivilegeEscalation must
        be undefined or set to `false`.        
      pattern:
        spec:
          =(initContainers):
          - =(securityContext):
              =(allowPrivilegeEscalation): "false"
          containers:
          - =(securityContext):
              =(allowPrivilegeEscalation): "false"