kyverno: [Bug] foreach with 0 elements is reported as a `skip`

Kyverno Version

1.11.1

Kubernetes Version

1.28.x

Kubernetes Platform

Minikube

Kyverno Rule Type

Validate

Description

A validate rule with a foreach that evaluates to 0 elements is reported as a skip.

Steps to reproduce

Policy:

apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
  name: check-sa-tokens
  annotations:
    pod-policies.kyverno.io/autogen-controllers: none
spec:
  validationFailureAction: Enforce
  background: true
  rules:
  - name: check-token-exp
    match:
      any:
      - resources:
          kinds:
          - Pod
    validate:
      foreach:
      - list: "request.object.spec.volumes[].projected.sources[].serviceAccountToken.expirationSeconds"
        deny:
          conditions:
            any:
            - key: "{{ element }}"
              operator: GreaterThan
              value: 3600
              message: "expirationSeconds must be less than 1 hour"

Resource:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - image: nginx
    name: nginx
    volumeMounts:
    - mountPath: /var/run/secrets/tokens
      name: my-proj-vol
  serviceAccountName: my-service-account #service acount

Apply via CLI:

kubectl-kyverno apply /tmp/policy.yaml -r /tmp/resource.yaml -v 5

Applying 1 policy rule(s) to 1 resource(s)...
I1219 23:26:04.429462   81774 vars.go:375]  "msg"="variable substituted" "path"="/validate/foreach/0/deny/conditions/any/0/key" "value"=null "variable"="{{ element }}"
I1219 23:26:04.430349   81774 context.go:299]  "msg"="updated image info" "images"={"containers":{"nginx":{"registry":"docker.io","name":"nginx","path":"nginx","tag":"latest","jsonPointer":"/spec/containers/0/image"}}}

pass: 0, fail: 0, warn: 0, error: 0, skip: 1

Expected behavior

All result should be a pass as the payload is compliant with the policy

Screenshots

No response

Kyverno logs

No response

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: 16 (15 by maintainers)

Most upvoted comments

@Pushkarm029 - looks like all tests are currently in a containing package:

https://github.com/kyverno/kyverno/blob/main/pkg/engine/validation_test.go#L2427

We can create a test with the sample policy in the issue there.

I see @JimBugwadia , I am sorry for jumping the gun there. @Pushkarm029 I have not yet added tests and I planned to add them today. It would be great if you are willing to collaborate and add the tests for it. Also, should we somehow log to the user that the element was nil and so the rule is returned as a pass and not exactly because of compliance to the rule? what do you guys think @JimBugwadia @Pushkarm029 ?

I’m interested in this bug, can you assign it to me?