kyverno: [Bug] Unable to validate element.securityContext.runAsNonRoot

Kyverno Version

1.11.4

Kubernetes Version

1.26.x

Kubernetes Platform

Bare metal

Kyverno Rule Type

Validate

Description

For some weird reason, spec.initContainers[].securityContext.runAsNonRoot cannot be evaluated properly. There seems to be some bug with admission webhook.

Steps to reproduce

  1. Apply the cluster policy below:
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
  name: rg-validate-pod-policy
spec:
  background: false
  failurePolicy: Ignore
  rules:
  - name: containers-user-privileges-validation
    match:
      resources:
        kinds:
        - Pod
        namespaces:
        - sweechin-test
    validate:
      message: >-
        container user privileges violation
      foreach:
        - list: request.object.spec.initContainers[]
          deny:
            conditions:
              any:
              - key: "{{ element.securityContext.privileged || '' }}"
                operator: Equals
                value: true
                message: >-
                  non-privileged-containers must have securityContext.privileged set as false (violation by init)
              - key: "{{ element.securityContext.allowPrivilegeEscalation || '' }}"
                operator: Equals
                value: true
                message: >-
                  non-privileged-containers must have securityContext.allowPrivilegeEscalation set as false (violation by init)
              - key: "{{ element.securityContext.runAsNonRoot || '' }}"
                operator: Equals
                value: false
                message: >-
                  non-privileged-containers must have securityContext.runAsNonRoot set as true (violation by init)
              - key: "{{ element.securityContext.runAsUser || '' }}"
                operator: Equals
                value: 0
                message: >-
                  non-privileged-containers must have securityContext.runAsUser set as more than 0 (violation by init)
              - key: "{{ element.securityContext.runAsGroup || '' }}"
                operator: Equals
                value: 0
                message: >-
                  non-privileged-containers must have securityContext.runAsGroup set as more than 0 (violation by init)
  1. Apply the pod.yaml below:
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  namespace: sweechin-test
spec:
  initContainers:
    - name: init-nginx
      image: nginx:1.14.2
      securityContext:
        privileged: false
        allowPrivilegeEscalation: false
        runAsNonRoot: false
        runAsUser: 1222
        runAsGroup: 1222
        capabilities:
          drop:
            - all
      ports:
      - containerPort: 80

  containers:
    - name: qp
      image: xxx/knative-qp
      securityContext:
        privileged: false
        allowPrivilegeEscalation: false
        runAsNonRoot: false
        runAsUser: 1222
        runAsGroup: 1222
        capabilities:
          drop:
            - all
      ports:
      - containerPort: 80
  1. Expected pod creation to fail due to runAsNonRoot being set to false. But pod creation was successful instead.

Expected behavior

Expected pod creation to fail due to spec.initContainers[].securityContext.runAsNonRoot being set to false.

Screenshots

No response

Kyverno logs

No logs.

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: closed
  • Created 3 years ago
  • Comments: 24 (17 by maintainers)

Most upvoted comments

to_string() is part of the core JMESPath so it works in Kyverno.

You could do this as another workaround, you’re just converting types here.

@MariamFahmy98 I wish to point out that securityContext.privileged and securityContext.allowPrivilegeEscalation which are both boolean are fine. It is just runAsNonRoot that seems to give me problem.

They aren’t fine, they both are not being substituted internally because they’re both boolean.

@MariamFahmy98, you’re missing request.object in your policy above.