diff --git a/pkg/engine/handlers/mutation/mutate_image.go b/pkg/engine/handlers/mutation/mutate_image.go index 49de7ed6b7..4698d4cad2 100644 --- a/pkg/engine/handlers/mutation/mutate_image.go +++ b/pkg/engine/handlers/mutation/mutate_image.go @@ -74,12 +74,11 @@ func (h mutateImageHandler) Process( } iv := internal.NewImageVerifier(logger, h.rclient, policyContext, *ruleCopy, h.ivm, h.imageSignatureRepository) var engineResponses []*engineapi.RuleResponse - for _, imageVerify := range ruleCopy.VerifyImages { - engineResponses = append(engineResponses, iv.Verify(ctx, imageVerify, h.images, h.configuration)...) - } var patches []jsonpatch.JsonPatchOperation - for _, response := range engineResponses { - patches = append(patches, response.DeprecatedPatches()...) + for _, imageVerify := range ruleCopy.VerifyImages { + patch, ruleResponse := iv.Verify(ctx, imageVerify, h.images, h.configuration) + patches = append(patches, patch...) + engineResponses = append(engineResponses, ruleResponse...) } if len(patches) != 0 { patch := jsonutils.JoinPatches(patch.ConvertPatches(patches...)...) diff --git a/pkg/engine/image_verify_test.go b/pkg/engine/image_verify_test.go index 5bfe61ec1e..7ad5dc5b78 100644 --- a/pkg/engine/image_verify_test.go +++ b/pkg/engine/image_verify_test.go @@ -480,9 +480,13 @@ func Test_SignatureGoodSigned(t *testing.T) { engineResp, _ := testVerifyAndPatchImages(context.TODO(), registryclient.NewOrDie(), nil, policyContext, cfg) assert.Equal(t, len(engineResp.PolicyResponse.Rules), 1) assert.Equal(t, engineResp.PolicyResponse.Rules[0].Status(), engineapi.RuleStatusPass, engineResp.PolicyResponse.Rules[0].Message()) - assert.Equal(t, len(engineResp.PolicyResponse.Rules[0].DeprecatedPatches()), 1) - patch := engineResp.PolicyResponse.Rules[0].DeprecatedPatches()[0] - assert.Equal(t, patch.Json(), "{\"op\":\"replace\",\"path\":\"/spec/containers/0/image\",\"value\":\"ghcr.io/kyverno/test-verify-image:signed@sha256:b31bfb4d0213f254d361e0079deaaebefa4f82ba7aa76ef82e90b4935ad5b105\"}") + constainers, found, err := unstructured.NestedSlice(engineResp.PatchedResource.UnstructuredContent(), "spec", "containers") + assert.NilError(t, err) + assert.Equal(t, true, found) + image, found, err := unstructured.NestedString(constainers[0].(map[string]interface{}), "image") + assert.NilError(t, err) + assert.Equal(t, true, found) + assert.Equal(t, "ghcr.io/kyverno/test-verify-image:signed@sha256:b31bfb4d0213f254d361e0079deaaebefa4f82ba7aa76ef82e90b4935ad5b105", image) } func Test_SignatureUnsigned(t *testing.T) { diff --git a/pkg/engine/internal/imageverifier.go b/pkg/engine/internal/imageverifier.go index c03b53ad1c..00cf346237 100644 --- a/pkg/engine/internal/imageverifier.go +++ b/pkg/engine/internal/imageverifier.go @@ -187,8 +187,9 @@ func (iv *ImageVerifier) Verify( imageVerify kyvernov1.ImageVerification, matchedImageInfos []apiutils.ImageInfo, cfg config.Configuration, -) []*engineapi.RuleResponse { +) ([]jsonpatch.JsonPatchOperation, []*engineapi.RuleResponse) { var responses []*engineapi.RuleResponse + var patches []jsonpatch.JsonPatchOperation // for backward compatibility imageVerify = *imageVerify.Convert() @@ -226,7 +227,7 @@ func (iv *ImageVerifier) Verify( if ruleResp == nil { ruleResp = engineapi.RulePass(iv.rule.Name, engineapi.ImageVerify, "mutated image digest") } - ruleResp = ruleResp.WithPatches(*patch) + patches = append(patches, *patch) imageInfo.Digest = retrievedDigest image = imageInfo.String() } @@ -239,7 +240,7 @@ func (iv *ImageVerifier) Verify( responses = append(responses, ruleResp) } } - return responses + return patches, responses } func (iv *ImageVerifier) verifyImage(