kyverno: [Bug] policyException not skipping consistently

Kyverno Version

1.12.2

Description

Hello!

I am running Kyverno v1.12.3, and enforcing Pod Security Standards with Kyverno. I am working on creating policyException objects for addressing known/accepted Policy Violations. After creating the PolicyExceptions object, the PolicyReports are not reflecting that policies are not skipping as expected.

Here is the setup:

I am enforcing the following Pod Security Standards with Kyverno:

ClusterPolicy Object for Pod Security Standards
---
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
  name: pod-security-standards
spec:
  admission: true
  background: false
  rules:
  - match:
      any:
      - resources:
          kinds:
          - Pod
    name: restricted
    skipBackgroundRequests: true
    validate:
      podSecurity:
        level: restricted
        version: latest
  validationFailureAction: Audit

It is currently set to Audit mode.

Here is a DeamonSet for demonstration:

promtail deamonset
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: promtail
  namespace: promtail
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: promtail
  template:
    metadata:
      labels:
        app.kubernetes.io/name: promtail
    spec:
      containers:
      - args:
        - -config.file=/etc/promtail/promtail.yaml
        - -config.expand-env=true
        env:
        - name: HOSTNAME
          valueFrom:
            fieldRef:
              apiVersion: v1
              fieldPath: spec.nodeName
        image: grafana/promtail:2.9.3
        imagePullPolicy: IfNotPresent
        name: promtail
        ports:
        - containerPort: 3101
          name: http-metrics
          protocol: TCP
        readinessProbe:
          failureThreshold: 5
          httpGet:
            path: /ready
            port: http-metrics
            scheme: HTTP
          initialDelaySeconds: 10
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 1
        resources:
          limits:
            cpu: 250m
            memory: 450Mi
          requests:
            cpu: 100m
            memory: 250Mi
        securityContext:
          allowPrivilegeEscalation: false
          capabilities:
            drop:
            - ALL
          readOnlyRootFilesystem: true
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: /etc/promtail
          name: config
        - mountPath: /run/promtail
          name: run
        - mountPath: /var/lib/docker/containers
          name: containers
          readOnly: true
        - mountPath: /var/log/pods
          name: pods
          readOnly: true
      dnsPolicy: ClusterFirst
      enableServiceLinks: true
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext:
        runAsGroup: 0
        runAsUser: 0
        seccompProfile:
          type: RuntimeDefault
      serviceAccount: promtail
      serviceAccountName: promtail
      terminationGracePeriodSeconds: 30
      tolerations:
      - effect: NoSchedule
        key: node-role.kubernetes.io/master
        operator: Exists
      - effect: NoSchedule
        key: node-role.kubernetes.io/control-plane
        operator: Exists
      volumes:
      - name: config
        secret:
          defaultMode: 420
          secretName: promtail
      - hostPath:
          path: /run/promtail
          type: ""
        name: run
      - hostPath:
          path: /var/lib/docker/containers
          type: ""
        name: containers
      - hostPath:
          path: /var/log/pods
          type: ""
        name: pods
  updateStrategy:
    rollingUpdate:
      maxSurge: 0
      maxUnavailable: 1
    type: RollingUpdate

This DS creates six pods Here is an example of a PolicyReport object for a Pod created by the DS:

Policy Report
---
apiVersion: wgpolicyk8s.io/v1alpha2
kind: PolicyReport
metadata:
  creationTimestamp: "2024-05-30T19:56:28Z"
  generation: 100
  labels:
    app.kubernetes.io/managed-by: kyverno
  name: 04f708b4-41fc-4d82-9c68-deb83922413c
  namespace: promtail
  ownerReferences:
  - apiVersion: v1
    kind: Pod
    name: promtail-dl4xx
    uid: 04f708b4-41fc-4d82-9c68-deb83922413c
  resourceVersion: "1943898329"
  uid: 07930712-62b3-42e7-b032-822deb60f651
results:
- message: 'Validation rule ''restricted'' failed. It violates PodSecurity "restricted:latest":
    (Forbidden reason: hostPath volumes, field error list: [spec.volumes[1].hostPath
    is forbidden, forbidden values found: /run/promtail, spec.volumes[2].hostPath
    is forbidden, forbidden values found: /var/lib/docker/containers, spec.volumes[3].hostPath
    is forbidden, forbidden values found: /var/log/pods])(Forbidden reason: restricted
    volume types, field error list: [spec.volumes[1].hostPath: Forbidden, spec.volumes[2].hostPath:
    Forbidden, spec.volumes[3].hostPath: Forbidden])'
  policy: pod-security-standards
  properties:
    controls: hostPathVolumes,restrictedVolumes,runAsNonRoot,runAsUser
    standard: restricted
    version: latest
  result: fail
  rule: restricted
  scored: true
  source: kyverno
  timestamp:
    nanos: 0
    seconds: 1717425175
scope:
  apiVersion: v1
  kind: Pod
  name: promtail-dl4xx
  namespace: promtail
  uid: 04f708b4-41fc-4d82-9c68-deb83922413c
summary:
  error: 0
  fail: 1
  pass: 0
  skip: 0
  warn: 0

Here is the PolicyException object for policy violations. This object is created in the promtail namespace:

PolicyException
---
apiVersion: kyverno.io/v2
kind: PolicyException
metadata:
  name: promtail-exceptions
  namespace: promtail
spec:
  background: true
  exceptions:
  - policyName: pod-security-standards
    ruleNames:
    - restricted
  match:
    any:
    - resources:
        kinds:
        - Pod
  podSecurity:
  - controlName: HostPath Volumes
  - controlName: Volume Types
  - controlName: Running as Non-root
  - controlName: Running as Non-root user

After applying the PolicyException object, I removed all the Policy Reports, and restarted the DS. The result is that, the policies are skipped for 3 Pods: image

I have other examples of this with deployments in other namespaces.

Am I misunderstanding/misusing the PolicyExceptions objects?

EDIT: added list of policy Reports for completeness

❯ k get policyreports.wgpolicyk8s.io -n promtail
NAME                                   KIND        NAME             PASS   FAIL   WARN   ERROR   SKIP   AGE
04f708b4-41fc-4d82-9c68-deb83922413c   Pod         promtail-dl4xx   0      1      0      0       0      3d19h
0955dc80-6f98-481d-8c12-a0d0c710abfc   Pod         promtail-jwnrj   0      0      0      0       1      3d19h
5425ea2a-7bb2-4ab9-835a-d63d93ef1ece   DaemonSet   promtail         0      1      0      0       0      3d19h
6edf5c44-a885-45ba-8ff5-0f153c4e7de0   Pod         promtail-hnvmq   0      0      0      0       1      3d19h
7308b1b6-d5eb-40cd-87e2-82c2fc8fd18b   Pod         promtail-7hzlk   0      1      0      0       0      3d19h
80355914-7ff9-4554-bb72-2df0d2525d69   Pod         promtail-9l2br   0      1      0      0       0      3d19h
a5bae873-1758-4a30-8a0f-ef9c0b794acc   Pod         promtail-htrjc   0      0      0      0       1      3d19h

Slack discussion

No response

Troubleshooting

  • I have read and followed the documentation AND the troubleshooting guide.
  • I have searched other issues in this repository and mine is not recorded.

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 1
  • Comments: 16 (7 by maintainers)

Most upvoted comments

I think this was fixed from 1.13.3, by #11780 . This issue existed not only for DaemonSets, it didn’t exempt any PodControllers as per the exceptions till 1.13.2. When I checked with 1.13.3, it skipped the pod controllers as expected. Would be great if somebody could re-confirm.

As of 1.13.4, this issue seems to be still present, and can be replicated with the example I have posted in the issue description.

Thank you for your response @MariamFahmy98. I can confirm this is still occuring on 1.12.5.

What is also bizarre is that, pods have different policy reports:

❯ k get policyreports.wgpolicyk8s.io -n promtail
NAME                                   KIND        NAME             PASS   FAIL   WARN   ERROR   SKIP   AGE
010ed6d6-ee43-4e15-978e-56fcaa40e602   Pod         promtail-brznn   0      0      0      0       1      14h
52b89fa4-9000-4d1a-b669-ef7566dd9dfd   Pod         promtail-pr4jt   0      1      0      0       0      14h
5425ea2a-7bb2-4ab9-835a-d63d93ef1ece   DaemonSet   promtail         0      1      0      0       0      14h
61f63e60-7451-4fd1-a5fd-5dd1b2dcbaf1   Pod         promtail-2tlnp   0      1      0      0       0      14h
bf3d6bb3-0643-457c-a55e-cb2c6ef665d4   Pod         promtail-qhgwd   0      0      0      0       1      14h
e88691e2-0783-41ae-a74d-e63cd9b8e945   Pod         promtail-hzl9l   0      1      0      0       0      14h
f35f028e-8d8a-431b-b3de-977e90d8d3e6   Pod         promtail-xhfks   0      0      0      0       1      14h

Some fail, some skip.