2023-03-31 08:41:48 +02:00
|
|
|
package mutation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2023-05-31 09:27:35 +02:00
|
|
|
json_patch "github.com/evanphx/json-patch/v5"
|
2023-03-31 08:41:48 +02:00
|
|
|
"github.com/go-logr/logr"
|
|
|
|
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
|
|
|
|
"github.com/kyverno/kyverno/pkg/config"
|
|
|
|
engineapi "github.com/kyverno/kyverno/pkg/engine/api"
|
|
|
|
enginecontext "github.com/kyverno/kyverno/pkg/engine/context"
|
|
|
|
"github.com/kyverno/kyverno/pkg/engine/handlers"
|
|
|
|
"github.com/kyverno/kyverno/pkg/engine/internal"
|
2023-05-31 09:27:35 +02:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/mutate/patch"
|
2023-03-31 08:41:48 +02:00
|
|
|
engineutils "github.com/kyverno/kyverno/pkg/engine/utils"
|
|
|
|
"github.com/kyverno/kyverno/pkg/engine/variables"
|
|
|
|
"github.com/kyverno/kyverno/pkg/registryclient"
|
2023-04-03 21:58:58 +02:00
|
|
|
apiutils "github.com/kyverno/kyverno/pkg/utils/api"
|
2023-05-31 09:27:35 +02:00
|
|
|
jsonutils "github.com/kyverno/kyverno/pkg/utils/json"
|
|
|
|
"github.com/mattbaird/jsonpatch"
|
2023-03-31 08:41:48 +02:00
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
|
|
)
|
|
|
|
|
|
|
|
type mutateImageHandler struct {
|
|
|
|
configuration config.Configuration
|
|
|
|
rclient registryclient.Client
|
|
|
|
ivm *engineapi.ImageVerificationMetadata
|
2023-04-03 21:58:58 +02:00
|
|
|
images []apiutils.ImageInfo
|
2023-03-31 08:41:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewMutateImageHandler(
|
2023-04-03 21:58:58 +02:00
|
|
|
policyContext engineapi.PolicyContext,
|
|
|
|
resource unstructured.Unstructured,
|
|
|
|
rule kyvernov1.Rule,
|
2023-03-31 08:41:48 +02:00
|
|
|
configuration config.Configuration,
|
|
|
|
rclient registryclient.Client,
|
|
|
|
ivm *engineapi.ImageVerificationMetadata,
|
2023-04-03 21:58:58 +02:00
|
|
|
) (handlers.Handler, error) {
|
|
|
|
if len(rule.VerifyImages) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
ruleImages, _, err := engineutils.ExtractMatchingImages(resource, policyContext.JSONContext(), rule, configuration)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(ruleImages) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2023-03-31 08:41:48 +02:00
|
|
|
return mutateImageHandler{
|
|
|
|
configuration: configuration,
|
|
|
|
rclient: rclient,
|
|
|
|
ivm: ivm,
|
2023-04-03 21:58:58 +02:00
|
|
|
images: ruleImages,
|
|
|
|
}, nil
|
2023-03-31 08:41:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h mutateImageHandler) Process(
|
|
|
|
ctx context.Context,
|
|
|
|
logger logr.Logger,
|
|
|
|
policyContext engineapi.PolicyContext,
|
|
|
|
resource unstructured.Unstructured,
|
|
|
|
rule kyvernov1.Rule,
|
2023-04-03 06:57:48 +02:00
|
|
|
contextLoader engineapi.EngineContextLoader,
|
2023-03-31 08:41:48 +02:00
|
|
|
) (unstructured.Unstructured, []engineapi.RuleResponse) {
|
|
|
|
jsonContext := policyContext.JSONContext()
|
|
|
|
ruleCopy, err := substituteVariables(rule, jsonContext, logger)
|
|
|
|
if err != nil {
|
2023-04-05 12:35:38 +02:00
|
|
|
return resource, handlers.WithResponses(
|
|
|
|
engineapi.RuleError(rule.Name, engineapi.ImageVerify, "failed to substitute variables", err),
|
2023-03-31 08:41:48 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
iv := internal.NewImageVerifier(logger, h.rclient, policyContext, *ruleCopy, h.ivm)
|
|
|
|
var engineResponses []*engineapi.RuleResponse
|
|
|
|
for _, imageVerify := range ruleCopy.VerifyImages {
|
2023-04-03 21:58:58 +02:00
|
|
|
engineResponses = append(engineResponses, iv.Verify(ctx, imageVerify, h.images, h.configuration)...)
|
2023-03-31 08:41:48 +02:00
|
|
|
}
|
2023-05-31 09:27:35 +02:00
|
|
|
var patches []jsonpatch.JsonPatchOperation
|
|
|
|
for _, response := range engineResponses {
|
|
|
|
patches = append(patches, response.Patches()...)
|
|
|
|
}
|
|
|
|
if len(patches) != 0 {
|
|
|
|
patch := jsonutils.JoinPatches(patch.ConvertPatches(patches...)...)
|
|
|
|
decoded, err := json_patch.DecodePatch(patch)
|
|
|
|
if err != nil {
|
|
|
|
return resource, handlers.WithResponses(
|
|
|
|
engineapi.RuleError(rule.Name, engineapi.ImageVerify, "failed to decode patch", err),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
options := &json_patch.ApplyOptions{SupportNegativeIndices: true, AllowMissingPathOnRemove: true, EnsurePathExistsOnAdd: true}
|
|
|
|
resourceBytes, err := resource.MarshalJSON()
|
|
|
|
if err != nil {
|
|
|
|
return resource, handlers.WithResponses(
|
|
|
|
engineapi.RuleError(rule.Name, engineapi.ImageVerify, "failed to marshal resource", err),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
patchedResourceBytes, err := decoded.ApplyWithOptions(resourceBytes, options)
|
|
|
|
if err != nil {
|
|
|
|
return resource, handlers.WithResponses(
|
|
|
|
engineapi.RuleError(rule.Name, engineapi.ImageVerify, "failed to apply patch", err),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if err := resource.UnmarshalJSON(patchedResourceBytes); err != nil {
|
|
|
|
return resource, handlers.WithResponses(
|
|
|
|
engineapi.RuleError(rule.Name, engineapi.ImageVerify, "failed to unmarshal resource", err),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2023-04-05 12:35:38 +02:00
|
|
|
return resource, handlers.WithResponses(engineResponses...)
|
2023-03-31 08:41:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func substituteVariables(rule kyvernov1.Rule, ctx enginecontext.EvalInterface, logger logr.Logger) (*kyvernov1.Rule, error) {
|
|
|
|
// remove attestations as variables are not substituted in them
|
|
|
|
ruleCopy := *rule.DeepCopy()
|
|
|
|
for i := range ruleCopy.VerifyImages {
|
|
|
|
ruleCopy.VerifyImages[i].Attestations = nil
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
ruleCopy, err = variables.SubstituteAllInRule(logger, ctx, ruleCopy)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// replace attestations
|
|
|
|
for i := range rule.VerifyImages {
|
|
|
|
ruleCopy.VerifyImages[i].Attestations = rule.VerifyImages[i].Attestations
|
|
|
|
}
|
|
|
|
return &ruleCopy, nil
|
|
|
|
}
|