Skip to content

Conversation

vishal-chdhry
Copy link
Member

@vishal-chdhry vishal-chdhry commented Nov 19, 2023

Explanation

The resource is not blocked when setting the failurePolicy to Ignore for a policy that was set to Enforce .

When testing using the provided proof manifest I found out that ac.KeysAreMissing() returns true here which results in the rule returning an error and not a failure. Due to this failurePolicy=Ignore consumes the error response and the resource is not blocked.

The resource should be blocked and ac.KeysAreMissing() should not return an error when it does not encounters an negation anchor key in resource.

Related issue

Closes: #8916

Milestone of this PR

/milestone 1.11.1

What type of PR is this

/kind bug

Proposed Changes

Proof Manifests

apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
  name: disallow-annotations
spec:
  background: true
  failurePolicy: Ignore
  rules:
  - match:
      all:
      - resources:
          kinds:
          - Pod
    name: disallow-annotations
    validate:
      message: One or more annotations is not allowed per the policies disallowed
        values list.
      pattern:
        metadata:
          =(annotations):
            =(kyverno-policies-test/key): '!disallowed'
            X(kyverno-policies-test/disallowed): ?*
  validationFailureAction: Enforce
  webhookTimeoutSeconds: 30

---
apiVersion: v1
kind: Pod
metadata:
  name: disallow-annotations-example
  namespace: default
  annotations:
    kyverno-policies-test/key: disallowed
spec:
  containers:
  - name: example
    image: busybox
    args: ["sleep", "infinity"]

Checklist

  • I have read the contributing guidelines.
  • I have read the PR documentation guide and followed the process including adding proof manifests to this PR.
  • This is a bug fix and I have added unit tests that prove my fix is effective.
  • This is a feature and I have added CLI tests that are applicable.
  • My PR needs to be cherry picked to a specific release branch which is release 1.11.1
  • My PR contains new or altered behavior to Kyverno and
    • CLI support should be added and my PR doesn't contain that functionality.
    • I have added or changed the documentation myself in an existing PR and the link is:
    • I have raised an issue in kyverno/website to track the documentation update and the link is:

Further Comments

KeysAreMissing() checks if a key is missing in a resource, since a negation should not be present in the resource, it should not count as a missing key

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>
Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>
Copy link

codecov bot commented Nov 19, 2023

Codecov Report

Attention: 2 lines in your changes are missing coverage. Please review.

Comparison is base (f4b18b6) 33.60% compared to head (f9da9d7) 33.60%.

Files Patch % Lines
pkg/engine/anchor/anchormap.go 50.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8953      +/-   ##
==========================================
- Coverage   33.60%   33.60%   -0.01%     
==========================================
  Files         315      315              
  Lines       24951    24953       +2     
==========================================
  Hits         8386     8386              
- Misses      15768    15769       +1     
- Partials      797      798       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>
@realshuting realshuting enabled auto-merge (squash) November 21, 2023 09:29
@realshuting
Copy link
Member

/cherry-pick release-1.11

Copy link
Member

@realshuting realshuting left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May not be related directly to this fix, do we need a doc issue to clarify behaviors for the policy failure vs error ?

@realshuting
Copy link
Member

/cherry-pick release-1.11

@realshuting realshuting merged commit 72524c7 into kyverno:main Nov 22, 2023
gcp-cherry-pick-bot bot pushed a commit that referenced this pull request Nov 22, 2023
* fix: update KeysAreMissing() to ignore negations in resource

KeysAreMissing() checks if a key is missing in a resource, since a negation should not be present in the resource, it should not count as a missing key

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

* feat: add tests

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

* fix: pod is supposed to fail

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

---------

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>
@realshuting realshuting added the cherry-pick-completed The PR was cherry-picked (or merged) to required release branches label Nov 22, 2023
realshuting pushed a commit that referenced this pull request Nov 22, 2023
…8982)

* fix: update KeysAreMissing() to ignore negations in resource

KeysAreMissing() checks if a key is missing in a resource, since a negation should not be present in the resource, it should not count as a missing key



* feat: add tests



* fix: pod is supposed to fail



---------

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>
Co-authored-by: Vishal Choudhary <sendtovishalchoudhary@gmail.com>
vishal-chdhry added a commit to vishal-chdhry/kyverno that referenced this pull request Jan 5, 2024
…#8953)

* fix: update KeysAreMissing() to ignore negations in resource

KeysAreMissing() checks if a key is missing in a resource, since a negation should not be present in the resource, it should not count as a missing key

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

* feat: add tests

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

* fix: pod is supposed to fail

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

---------

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>
vishal-chdhry added a commit to vishal-chdhry/kyverno that referenced this pull request Jan 5, 2024
…#8953)

* fix: update KeysAreMissing() to ignore negations in resource

KeysAreMissing() checks if a key is missing in a resource, since a negation should not be present in the resource, it should not count as a missing key

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

* feat: add tests

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

* fix: pod is supposed to fail

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

---------

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>
vishal-chdhry added a commit to vishal-chdhry/kyverno that referenced this pull request Jan 6, 2024
…#8953)

* fix: update KeysAreMissing() to ignore negations in resource

KeysAreMissing() checks if a key is missing in a resource, since a negation should not be present in the resource, it should not count as a missing key

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

* feat: add tests

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

* fix: pod is supposed to fail

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

---------

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cherry-pick-completed The PR was cherry-picked (or merged) to required release branches cherry-pick-required milestone 1.12.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug] failurePolicy of Ignore causes some invalid resources to be admitted
2 participants