2022-09-09 12:48:29 +02:00
|
|
|
package mutation
|
|
|
|
|
|
|
|
import (
|
2022-11-28 11:30:14 +01:00
|
|
|
"context"
|
2022-09-09 12:48:29 +02:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-logr/logr"
|
|
|
|
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
|
|
|
|
"github.com/kyverno/kyverno/pkg/engine"
|
2023-01-30 12:41:09 +01:00
|
|
|
engineapi "github.com/kyverno/kyverno/pkg/engine/api"
|
2023-05-13 10:56:54 +02:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/mutate/patch"
|
2022-09-09 12:48:29 +02:00
|
|
|
"github.com/kyverno/kyverno/pkg/event"
|
|
|
|
"github.com/kyverno/kyverno/pkg/metrics"
|
|
|
|
"github.com/kyverno/kyverno/pkg/openapi"
|
2022-12-12 21:32:11 +01:00
|
|
|
"github.com/kyverno/kyverno/pkg/tracing"
|
2022-09-09 12:48:29 +02:00
|
|
|
engineutils "github.com/kyverno/kyverno/pkg/utils/engine"
|
|
|
|
jsonutils "github.com/kyverno/kyverno/pkg/utils/json"
|
|
|
|
webhookutils "github.com/kyverno/kyverno/pkg/webhooks/utils"
|
2022-12-12 21:32:11 +01:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
2023-06-08 12:23:20 +02:00
|
|
|
"gomodules.xyz/jsonpatch/v2"
|
2022-09-09 12:48:29 +02:00
|
|
|
admissionv1 "k8s.io/api/admission/v1"
|
|
|
|
corev1listers "k8s.io/client-go/listers/core/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MutationHandler interface {
|
|
|
|
// HandleMutation handles validating webhook admission request
|
|
|
|
// If there are no errors in validating rule we apply generation rules
|
|
|
|
// patchedResource is the (resource + patches) after applying mutation rules
|
2023-04-04 07:11:18 +02:00
|
|
|
HandleMutation(context.Context, admissionv1.AdmissionRequest, []kyvernov1.PolicyInterface, *engine.PolicyContext, time.Time) ([]byte, []string, error)
|
2022-09-09 12:48:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewMutationHandler(
|
|
|
|
log logr.Logger,
|
2023-02-02 11:58:34 +01:00
|
|
|
engine engineapi.Engine,
|
2022-09-09 12:48:29 +02:00
|
|
|
eventGen event.Interface,
|
2022-10-12 18:54:16 +02:00
|
|
|
openApiManager openapi.ValidateInterface,
|
2022-09-09 12:48:29 +02:00
|
|
|
nsLister corev1listers.NamespaceLister,
|
2022-12-09 14:45:11 +01:00
|
|
|
metrics metrics.MetricsConfigManager,
|
2022-09-09 12:48:29 +02:00
|
|
|
) MutationHandler {
|
|
|
|
return &mutationHandler{
|
2022-10-12 18:54:16 +02:00
|
|
|
log: log,
|
2023-02-02 11:58:34 +01:00
|
|
|
engine: engine,
|
2022-10-12 18:54:16 +02:00
|
|
|
eventGen: eventGen,
|
|
|
|
openApiManager: openApiManager,
|
|
|
|
nsLister: nsLister,
|
2022-12-09 14:45:11 +01:00
|
|
|
metrics: metrics,
|
2022-09-09 12:48:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type mutationHandler struct {
|
2022-10-12 18:54:16 +02:00
|
|
|
log logr.Logger
|
2023-02-02 11:58:34 +01:00
|
|
|
engine engineapi.Engine
|
2022-10-12 18:54:16 +02:00
|
|
|
eventGen event.Interface
|
|
|
|
openApiManager openapi.ValidateInterface
|
|
|
|
nsLister corev1listers.NamespaceLister
|
2022-12-09 14:45:11 +01:00
|
|
|
metrics metrics.MetricsConfigManager
|
2022-09-09 12:48:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *mutationHandler) HandleMutation(
|
2022-12-09 14:45:11 +01:00
|
|
|
ctx context.Context,
|
2023-04-04 07:11:18 +02:00
|
|
|
request admissionv1.AdmissionRequest,
|
2022-09-09 12:48:29 +02:00
|
|
|
policies []kyvernov1.PolicyInterface,
|
|
|
|
policyContext *engine.PolicyContext,
|
|
|
|
admissionRequestTimestamp time.Time,
|
|
|
|
) ([]byte, []string, error) {
|
2022-12-09 14:45:11 +01:00
|
|
|
mutatePatches, mutateEngineResponses, err := h.applyMutations(ctx, request, policies, policyContext)
|
2022-09-09 12:48:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
h.log.V(6).Info("", "generated patches", string(mutatePatches))
|
|
|
|
return mutatePatches, webhookutils.GetWarningMessages(mutateEngineResponses), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// applyMutations handles mutating webhook admission request
|
|
|
|
// return value: generated patches, triggered policies, engine responses correspdonding to the triggered policies
|
|
|
|
func (v *mutationHandler) applyMutations(
|
2022-12-09 14:45:11 +01:00
|
|
|
ctx context.Context,
|
2023-04-04 07:11:18 +02:00
|
|
|
request admissionv1.AdmissionRequest,
|
2022-09-09 12:48:29 +02:00
|
|
|
policies []kyvernov1.PolicyInterface,
|
|
|
|
policyContext *engine.PolicyContext,
|
2023-03-23 13:58:52 +01:00
|
|
|
) ([]byte, []engineapi.EngineResponse, error) {
|
2022-09-09 12:48:29 +02:00
|
|
|
if len(policies) == 0 {
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
|
2023-05-13 10:56:54 +02:00
|
|
|
var patches []jsonpatch.JsonPatchOperation
|
2023-03-23 13:58:52 +01:00
|
|
|
var engineResponses []engineapi.EngineResponse
|
2022-09-09 12:48:29 +02:00
|
|
|
|
|
|
|
for _, policy := range policies {
|
|
|
|
spec := policy.GetSpec()
|
|
|
|
if !spec.HasMutate() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-12-12 21:32:11 +01:00
|
|
|
err := tracing.ChildSpan1(
|
|
|
|
ctx,
|
|
|
|
"",
|
|
|
|
fmt.Sprintf("POLICY %s/%s", policy.GetNamespace(), policy.GetName()),
|
|
|
|
func(ctx context.Context, span trace.Span) error {
|
|
|
|
v.log.V(3).Info("applying policy mutate rules", "policy", policy.GetName())
|
|
|
|
currentContext := policyContext.WithPolicy(policy)
|
|
|
|
engineResponse, policyPatches, err := v.applyMutation(ctx, request, currentContext)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("mutation policy %s error: %v", policy.GetName(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(policyPatches) > 0 {
|
|
|
|
patches = append(patches, policyPatches...)
|
|
|
|
rules := engineResponse.GetSuccessRules()
|
|
|
|
if len(rules) != 0 {
|
|
|
|
v.log.Info("mutation rules from policy applied successfully", "policy", policy.GetName(), "rules", rules)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-23 13:58:52 +01:00
|
|
|
if engineResponse != nil {
|
|
|
|
policyContext = currentContext.WithNewResource(engineResponse.PatchedResource)
|
|
|
|
engineResponses = append(engineResponses, *engineResponse)
|
|
|
|
}
|
2022-12-12 21:32:11 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2022-09-09 12:48:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-13 15:00:50 +08:00
|
|
|
events := webhookutils.GenerateEvents(engineResponses, false)
|
|
|
|
v.eventGen.Add(events...)
|
2022-09-09 12:48:29 +02:00
|
|
|
|
|
|
|
logMutationResponse(patches, engineResponses, v.log)
|
|
|
|
|
|
|
|
// patches holds all the successful patches, if no patch is created, it returns nil
|
2023-05-13 10:56:54 +02:00
|
|
|
return jsonutils.JoinPatches(patch.ConvertPatches(patches...)...), engineResponses, nil
|
2022-09-09 12:48:29 +02:00
|
|
|
}
|
|
|
|
|
2023-05-13 10:56:54 +02:00
|
|
|
func (h *mutationHandler) applyMutation(ctx context.Context, request admissionv1.AdmissionRequest, policyContext *engine.PolicyContext) (*engineapi.EngineResponse, []jsonpatch.JsonPatchOperation, error) {
|
2022-09-09 12:48:29 +02:00
|
|
|
if request.Kind.Kind != "Namespace" && request.Namespace != "" {
|
2022-12-21 21:30:45 +01:00
|
|
|
policyContext = policyContext.WithNamespaceLabels(engineutils.GetNamespaceSelectorsFromNamespaceLister(request.Kind.Kind, request.Namespace, h.nsLister, h.log))
|
2022-09-09 12:48:29 +02:00
|
|
|
}
|
|
|
|
|
2023-02-03 06:01:11 +01:00
|
|
|
engineResponse := h.engine.Mutate(ctx, policyContext)
|
2022-09-09 12:48:29 +02:00
|
|
|
policyPatches := engineResponse.GetPatches()
|
|
|
|
|
2022-11-08 12:36:13 +01:00
|
|
|
if !engineResponse.IsSuccessful() {
|
2023-03-17 16:36:06 +08:00
|
|
|
return nil, nil, fmt.Errorf("failed to apply policy %s rules %v", policyContext.Policy().GetName(), engineResponse.GetFailedRulesWithErrors())
|
2022-09-09 12:48:29 +02:00
|
|
|
}
|
|
|
|
|
2022-12-02 09:14:23 +01:00
|
|
|
if policyContext.Policy().ValidateSchema() && engineResponse.PatchedResource.GetKind() != "*" {
|
2022-10-12 18:54:16 +02:00
|
|
|
err := h.openApiManager.ValidateResource(*engineResponse.PatchedResource.DeepCopy(), engineResponse.PatchedResource.GetAPIVersion(), engineResponse.PatchedResource.GetKind())
|
2022-09-09 12:48:29 +02:00
|
|
|
if err != nil {
|
2023-02-01 14:38:04 +08:00
|
|
|
return nil, nil, fmt.Errorf("failed to validate resource mutated by policy %s: %w", policyContext.Policy().GetName(), err)
|
2022-09-09 12:48:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-22 15:55:00 +01:00
|
|
|
return &engineResponse, policyPatches, nil
|
2022-09-09 12:48:29 +02:00
|
|
|
}
|
|
|
|
|
2023-05-13 10:56:54 +02:00
|
|
|
func logMutationResponse(patches []jsonpatch.JsonPatchOperation, engineResponses []engineapi.EngineResponse, logger logr.Logger) {
|
2022-09-09 12:48:29 +02:00
|
|
|
if len(patches) != 0 {
|
|
|
|
logger.V(4).Info("created patches", "count", len(patches))
|
|
|
|
}
|
|
|
|
|
|
|
|
// if any of the policies fails, print out the error
|
|
|
|
if !engineutils.IsResponseSuccessful(engineResponses) {
|
2023-02-01 14:38:04 +08:00
|
|
|
logger.Error(fmt.Errorf(webhookutils.GetErrorMsg(engineResponses)), "failed to apply mutation rules on the resource, reporting policy violation")
|
2022-09-09 12:48:29 +02:00
|
|
|
}
|
|
|
|
}
|