2019-05-14 01:17:28 +00:00
|
|
|
package engine
|
2019-05-10 05:26:22 +00:00
|
|
|
|
|
|
|
import (
|
2021-04-13 18:44:43 +00:00
|
|
|
"fmt"
|
2021-10-13 18:03:25 +00:00
|
|
|
"time"
|
|
|
|
|
2021-10-29 16:13:20 +00:00
|
|
|
kyverno "github.com/kyverno/kyverno/api/kyverno/v1"
|
2021-10-07 01:31:20 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/context"
|
|
|
|
"github.com/pkg/errors"
|
2020-11-18 22:31:43 +00:00
|
|
|
|
2020-03-17 18:05:20 +00:00
|
|
|
"github.com/go-logr/logr"
|
2021-07-14 21:50:28 +00:00
|
|
|
gojmespath "github.com/jmespath/go-jmespath"
|
2020-10-07 18:12:31 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/mutate"
|
|
|
|
"github.com/kyverno/kyverno/pkg/engine/response"
|
2021-04-13 18:44:43 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/utils"
|
2020-10-07 18:12:31 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/variables"
|
2020-01-16 19:57:28 +00:00
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
2020-03-17 23:25:34 +00:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/log"
|
2019-05-10 05:26:22 +00:00
|
|
|
)
|
|
|
|
|
2019-12-26 23:34:19 +00:00
|
|
|
const (
|
2020-09-01 16:11:20 +00:00
|
|
|
// PodControllerCronJob represent CronJob string
|
|
|
|
PodControllerCronJob = "CronJob"
|
2020-01-24 20:05:53 +00:00
|
|
|
//PodControllers stores the list of Pod-controllers in csv string
|
2020-09-01 16:11:20 +00:00
|
|
|
PodControllers = "DaemonSet,Deployment,Job,StatefulSet,CronJob"
|
2020-01-24 20:05:53 +00:00
|
|
|
//PodControllersAnnotation defines the annotation key for Pod-Controllers
|
2019-12-26 23:34:19 +00:00
|
|
|
PodControllersAnnotation = "pod-policies.kyverno.io/autogen-controllers"
|
|
|
|
)
|
|
|
|
|
2019-05-13 18:27:47 +00:00
|
|
|
// Mutate performs mutation. Overlay first and then mutation patches
|
2020-12-23 23:10:07 +00:00
|
|
|
func Mutate(policyContext *PolicyContext) (resp *response.EngineResponse) {
|
|
|
|
resp = &response.EngineResponse{}
|
2019-08-24 01:34:23 +00:00
|
|
|
startTime := time.Now()
|
2019-11-09 02:57:27 +00:00
|
|
|
policy := policyContext.Policy
|
2020-07-09 18:48:34 +00:00
|
|
|
patchedResource := policyContext.NewResource
|
2020-12-23 23:10:07 +00:00
|
|
|
ctx := policyContext.JSONContext
|
2021-12-28 16:37:41 +00:00
|
|
|
var name []string
|
2020-09-22 21:11:49 +00:00
|
|
|
|
|
|
|
resCache := policyContext.ResourceCache
|
2020-07-09 18:48:34 +00:00
|
|
|
logger := log.Log.WithName("EngineMutate").WithValues("policy", policy.Name, "kind", patchedResource.GetKind(),
|
|
|
|
"namespace", patchedResource.GetNamespace(), "name", patchedResource.GetName())
|
|
|
|
|
2020-05-26 17:36:56 +00:00
|
|
|
logger.V(4).Info("start policy processing", "startTime", startTime)
|
2020-01-16 19:57:28 +00:00
|
|
|
|
2020-12-23 23:10:07 +00:00
|
|
|
startMutateResultResponse(resp, policy, patchedResource)
|
|
|
|
defer endMutateResultResponse(logger, resp, startTime)
|
2020-06-24 17:26:04 +00:00
|
|
|
|
2021-02-02 07:22:19 +00:00
|
|
|
policyContext.JSONContext.Checkpoint()
|
|
|
|
defer policyContext.JSONContext.Restore()
|
|
|
|
|
2021-07-28 16:54:50 +00:00
|
|
|
var err error
|
|
|
|
|
2021-10-13 18:03:25 +00:00
|
|
|
for i, rule := range policy.Spec.Rules {
|
2020-08-05 16:11:23 +00:00
|
|
|
if !rule.HasMutate() {
|
2019-08-24 01:34:23 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-11-09 02:57:27 +00:00
|
|
|
|
2021-07-10 01:01:46 +00:00
|
|
|
logger := logger.WithValues("rule", rule.Name)
|
2020-08-14 19:21:06 +00:00
|
|
|
excludeResource := []string{}
|
2020-08-08 00:09:24 +00:00
|
|
|
if len(policyContext.ExcludeGroupRole) > 0 {
|
|
|
|
excludeResource = policyContext.ExcludeGroupRole
|
|
|
|
}
|
2020-12-23 23:10:07 +00:00
|
|
|
|
2021-10-01 08:46:33 +00:00
|
|
|
if err = MatchesResourceDescription(patchedResource, rule, policyContext.AdmissionInfo, excludeResource, policyContext.NamespaceLabels, policyContext.Policy.Namespace); err != nil {
|
2021-01-07 19:24:38 +00:00
|
|
|
logger.V(4).Info("rule not matched", "reason", err.Error())
|
2021-12-28 16:37:41 +00:00
|
|
|
name = append(name, rule.Name)
|
2019-08-24 01:34:23 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-10-15 00:39:45 +00:00
|
|
|
|
2021-01-07 19:24:38 +00:00
|
|
|
logger.V(3).Info("matched mutate rule")
|
|
|
|
|
2021-07-22 21:23:03 +00:00
|
|
|
// Restore() is meant for restoring context loaded from external lookup (APIServer & ConfigMap)
|
|
|
|
// while we need to keep updated resource in the JSON context as rules can be chained
|
|
|
|
resource, err := policyContext.JSONContext.Query("request.object")
|
2021-09-26 09:12:31 +00:00
|
|
|
policyContext.JSONContext.Reset()
|
2021-07-22 21:23:03 +00:00
|
|
|
if err == nil && resource != nil {
|
|
|
|
if err := ctx.AddResourceAsObject(resource.(map[string]interface{})); err != nil {
|
2021-10-07 01:31:20 +00:00
|
|
|
logger.Error(err, "unable to update resource object")
|
2021-07-22 21:23:03 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-10-07 01:31:20 +00:00
|
|
|
logger.Error(err, "failed to query resource object")
|
2021-07-22 21:23:03 +00:00
|
|
|
}
|
|
|
|
|
2021-04-29 17:09:44 +00:00
|
|
|
if err := LoadContext(logger, rule.Context, resCache, policyContext, rule.Name); err != nil {
|
2021-07-14 21:50:28 +00:00
|
|
|
if _, ok := err.(gojmespath.NotFoundError); ok {
|
|
|
|
logger.V(3).Info("failed to load context", "reason", err.Error())
|
|
|
|
} else {
|
|
|
|
logger.Error(err, "failed to load context")
|
|
|
|
}
|
2020-09-22 21:11:49 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-01-07 23:13:57 +00:00
|
|
|
|
2021-07-30 19:07:01 +00:00
|
|
|
ruleCopy := rule.DeepCopy()
|
2021-10-07 01:31:20 +00:00
|
|
|
var ruleResp *response.RuleResponse
|
|
|
|
if rule.Mutation.ForEachMutation != nil {
|
|
|
|
ruleResp, patchedResource = mutateForEachResource(ruleCopy, policyContext, patchedResource, logger)
|
|
|
|
} else {
|
2021-10-29 15:06:03 +00:00
|
|
|
mutateResp, err := mutateResource(ruleCopy, policyContext.JSONContext, patchedResource, logger, 0)
|
2021-10-07 01:31:20 +00:00
|
|
|
if err != nil {
|
2021-10-07 04:50:26 +00:00
|
|
|
if mutateResp.skip {
|
2021-10-13 18:03:25 +00:00
|
|
|
ruleResp = ruleResponse(&policy.Spec.Rules[i], utils.Mutation, err.Error(), response.RuleStatusSkip)
|
2021-10-07 01:31:20 +00:00
|
|
|
} else {
|
2021-10-13 18:03:25 +00:00
|
|
|
ruleResp = ruleResponse(&policy.Spec.Rules[i], utils.Mutation, err.Error(), response.RuleStatusError)
|
2021-10-07 01:31:20 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-10-07 04:50:26 +00:00
|
|
|
if mutateResp.message == "" {
|
|
|
|
mutateResp.message = "mutated resource"
|
|
|
|
}
|
|
|
|
|
2021-10-13 18:03:25 +00:00
|
|
|
ruleResp = ruleResponse(&policy.Spec.Rules[i], utils.Mutation, mutateResp.message, response.RuleStatusPass)
|
2021-10-07 01:31:20 +00:00
|
|
|
ruleResp.Patches = mutateResp.patches
|
|
|
|
patchedResource = mutateResp.patchedResource
|
|
|
|
}
|
2021-07-28 16:54:50 +00:00
|
|
|
}
|
|
|
|
|
2021-10-07 01:31:20 +00:00
|
|
|
if ruleResp != nil {
|
|
|
|
resp.PolicyResponse.Rules = append(resp.PolicyResponse.Rules, *ruleResp)
|
|
|
|
if ruleResp.Status == response.RuleStatusError {
|
|
|
|
incrementErrorCount(resp)
|
|
|
|
} else {
|
|
|
|
incrementAppliedCount(resp)
|
|
|
|
}
|
2020-01-07 23:13:57 +00:00
|
|
|
}
|
2021-10-07 01:31:20 +00:00
|
|
|
}
|
2020-01-07 23:13:57 +00:00
|
|
|
|
2021-12-28 16:37:41 +00:00
|
|
|
for _, r := range resp.PolicyResponse.Rules {
|
|
|
|
for _, n := range name {
|
|
|
|
if r.Name == n {
|
|
|
|
r.Status = response.RuleStatusSkip
|
|
|
|
logger.V(4).Info("rule Status set as skip", "rule name", r.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-07 01:31:20 +00:00
|
|
|
resp.PatchedResource = patchedResource
|
|
|
|
return resp
|
|
|
|
}
|
2021-04-13 18:44:43 +00:00
|
|
|
|
2021-10-07 01:31:20 +00:00
|
|
|
func mutateForEachResource(rule *kyverno.Rule, ctx *PolicyContext, resource unstructured.Unstructured, logger logr.Logger) (*response.RuleResponse, unstructured.Unstructured) {
|
2021-10-14 07:20:52 +00:00
|
|
|
foreachList := rule.Mutation.ForEachMutation
|
|
|
|
if foreachList == nil {
|
2021-10-07 01:31:20 +00:00
|
|
|
return nil, resource
|
|
|
|
}
|
2021-04-16 00:33:34 +00:00
|
|
|
|
2021-10-07 01:31:20 +00:00
|
|
|
applyCount := 0
|
|
|
|
patchedResource := resource
|
|
|
|
allPatches := make([][]byte, 0)
|
|
|
|
|
2021-10-14 07:20:52 +00:00
|
|
|
for foreachIndex, foreach := range foreachList {
|
|
|
|
|
|
|
|
if err := LoadContext(logger, foreach.Context, ctx.ResourceCache, ctx, rule.Name); err != nil {
|
|
|
|
logger.Error(err, "failed to load context")
|
|
|
|
return ruleError(rule, utils.Mutation, "failed to load context", err), resource
|
2021-10-07 01:31:20 +00:00
|
|
|
}
|
|
|
|
|
2021-12-22 14:20:40 +00:00
|
|
|
preconditionsPassed, err := checkPreconditions(logger, ctx, rule.AnyAllConditions)
|
|
|
|
|
2021-10-14 07:20:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return ruleError(rule, utils.Mutation, "failed to evaluate preconditions", err), resource
|
|
|
|
} else if !preconditionsPassed {
|
|
|
|
return ruleResponse(rule, utils.Mutation, "preconditions not met", response.RuleStatusSkip), resource
|
2019-08-24 01:34:23 +00:00
|
|
|
}
|
|
|
|
|
2021-10-14 07:20:52 +00:00
|
|
|
elements, err := evaluateList(foreach.List, ctx.JSONContext)
|
|
|
|
if err != nil {
|
|
|
|
msg := fmt.Sprintf("failed to evaluate list %s", foreach.List)
|
|
|
|
return ruleError(rule, utils.Mutation, msg, err), resource
|
2021-07-22 21:37:09 +00:00
|
|
|
}
|
|
|
|
|
2021-10-14 07:20:52 +00:00
|
|
|
ctx.JSONContext.Checkpoint()
|
|
|
|
defer ctx.JSONContext.Restore()
|
|
|
|
|
|
|
|
for _, e := range elements {
|
|
|
|
ctx.JSONContext.Reset()
|
|
|
|
|
|
|
|
ctx := ctx.Copy()
|
|
|
|
if err := addElementToContext(ctx, e); err != nil {
|
|
|
|
logger.Error(err, "failed to add element to context")
|
|
|
|
return ruleError(rule, utils.Mutation, "failed to process foreach", err), resource
|
|
|
|
}
|
|
|
|
|
|
|
|
var skip = false
|
2021-10-29 15:06:03 +00:00
|
|
|
mutateResp, err := mutateResource(rule, ctx.JSONContext, patchedResource, logger, foreachIndex)
|
2021-10-14 07:20:52 +00:00
|
|
|
if err != nil && !skip {
|
|
|
|
return ruleResponse(rule, utils.Mutation, err.Error(), response.RuleStatusError), resource
|
|
|
|
}
|
|
|
|
|
|
|
|
patchedResource = mutateResp.patchedResource
|
|
|
|
if len(mutateResp.patches) > 0 {
|
|
|
|
allPatches = append(allPatches, mutateResp.patches...)
|
|
|
|
}
|
|
|
|
|
|
|
|
applyCount++
|
|
|
|
}
|
2020-06-26 00:24:10 +00:00
|
|
|
}
|
|
|
|
|
2021-10-07 01:31:20 +00:00
|
|
|
if applyCount == 0 {
|
|
|
|
return ruleResponse(rule, utils.Mutation, "0 elements processed", response.RuleStatusSkip), resource
|
|
|
|
}
|
|
|
|
|
|
|
|
r := ruleResponse(rule, utils.Mutation, fmt.Sprintf("%d elements processed", applyCount), response.RuleStatusPass)
|
|
|
|
r.Patches = allPatches
|
|
|
|
return r, patchedResource
|
|
|
|
}
|
|
|
|
|
|
|
|
type mutateResponse struct {
|
2021-10-07 05:05:28 +00:00
|
|
|
skip bool
|
2021-10-07 01:31:20 +00:00
|
|
|
patchedResource unstructured.Unstructured
|
2021-10-07 05:05:28 +00:00
|
|
|
patches [][]byte
|
|
|
|
message string
|
2019-08-24 01:34:23 +00:00
|
|
|
}
|
2020-04-04 17:09:21 +00:00
|
|
|
|
2021-10-29 15:06:03 +00:00
|
|
|
func mutateResource(rule *kyverno.Rule, ctx *context.Context, resource unstructured.Unstructured, logger logr.Logger, foreachIndex int) (*mutateResponse, error) {
|
2021-10-07 04:50:26 +00:00
|
|
|
mutateResp := &mutateResponse{false, unstructured.Unstructured{}, nil, ""}
|
2021-10-14 07:20:52 +00:00
|
|
|
|
|
|
|
// Pre-conditions checks for the list of foreach rules should ideally be performed once.
|
2021-10-18 22:42:34 +00:00
|
|
|
// Currently, they are performed for each entry in the foreach list.
|
2021-10-14 07:20:52 +00:00
|
|
|
// Also, the foreach index parameter should be removed and a set of patches should be passed in.
|
2021-10-07 01:31:20 +00:00
|
|
|
anyAllConditions, err := variables.SubstituteAllInPreconditions(logger, ctx, rule.AnyAllConditions)
|
|
|
|
if err != nil {
|
2021-10-29 15:06:03 +00:00
|
|
|
return mutateResp, errors.Wrapf(err, "failed to substitute vars in preconditions")
|
2021-10-07 01:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
copyConditions, err := transformConditions(anyAllConditions)
|
|
|
|
if err != nil {
|
2021-10-29 15:06:03 +00:00
|
|
|
return mutateResp, errors.Wrapf(err, "failed to load context")
|
2021-10-07 01:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !variables.EvaluateConditions(logger, ctx, copyConditions) {
|
2021-10-18 22:42:34 +00:00
|
|
|
mutateResp.skip = true
|
2021-10-29 15:06:03 +00:00
|
|
|
return mutateResp, fmt.Errorf("preconditions mismatch")
|
2021-10-07 01:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
updatedRule, err := variables.SubstituteAllInRule(logger, ctx, *rule)
|
|
|
|
if err != nil {
|
2021-10-29 15:06:03 +00:00
|
|
|
return mutateResp, errors.Wrapf(err, "variable substitution failed")
|
2021-10-07 01:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mutation := updatedRule.Mutation.DeepCopy()
|
2021-10-14 07:20:52 +00:00
|
|
|
mutateHandler := mutate.CreateMutateHandler(updatedRule.Name, mutation, resource, ctx, logger, foreachIndex)
|
2021-10-07 01:31:20 +00:00
|
|
|
resp, patchedResource := mutateHandler.Handle()
|
2021-10-14 07:20:52 +00:00
|
|
|
|
2021-10-07 01:31:20 +00:00
|
|
|
if resp.Status == response.RuleStatusPass {
|
|
|
|
// - overlay pattern does not match the resource conditions
|
|
|
|
if resp.Patches == nil {
|
|
|
|
mutateResp.skip = true
|
2021-10-29 15:06:03 +00:00
|
|
|
return mutateResp, fmt.Errorf("resource does not match pattern")
|
2021-10-07 01:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mutateResp.skip = false
|
|
|
|
mutateResp.patchedResource = patchedResource
|
|
|
|
mutateResp.patches = resp.Patches
|
2021-10-07 04:50:26 +00:00
|
|
|
mutateResp.message = resp.Message
|
2021-10-07 01:31:20 +00:00
|
|
|
logger.V(4).Info("mutate rule applied successfully", "ruleName", rule.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ctx.AddResourceAsObject(patchedResource.Object); err != nil {
|
|
|
|
logger.Error(err, "failed to update resource in the JSON context")
|
|
|
|
}
|
|
|
|
|
2021-10-29 15:06:03 +00:00
|
|
|
return mutateResp, nil
|
2020-02-14 19:59:28 +00:00
|
|
|
}
|
2019-12-26 23:34:19 +00:00
|
|
|
|
2020-01-16 19:57:28 +00:00
|
|
|
func startMutateResultResponse(resp *response.EngineResponse, policy kyverno.ClusterPolicy, resource unstructured.Unstructured) {
|
2020-12-23 23:10:07 +00:00
|
|
|
if resp == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-29 21:43:11 +00:00
|
|
|
resp.PolicyResponse.Policy.Name = policy.GetName()
|
|
|
|
resp.PolicyResponse.Policy.Namespace = policy.GetNamespace()
|
2020-01-16 19:57:28 +00:00
|
|
|
resp.PolicyResponse.Resource.Name = resource.GetName()
|
|
|
|
resp.PolicyResponse.Resource.Namespace = resource.GetNamespace()
|
|
|
|
resp.PolicyResponse.Resource.Kind = resource.GetKind()
|
|
|
|
resp.PolicyResponse.Resource.APIVersion = resource.GetAPIVersion()
|
|
|
|
}
|
|
|
|
|
2020-03-17 18:05:20 +00:00
|
|
|
func endMutateResultResponse(logger logr.Logger, resp *response.EngineResponse, startTime time.Time) {
|
2020-12-23 23:10:07 +00:00
|
|
|
if resp == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-16 19:57:28 +00:00
|
|
|
resp.PolicyResponse.ProcessingTime = time.Since(startTime)
|
2021-05-15 13:45:04 +00:00
|
|
|
resp.PolicyResponse.PolicyExecutionTimestamp = startTime.Unix()
|
2021-01-07 19:24:38 +00:00
|
|
|
logger.V(5).Info("finished processing policy", "processingTime", resp.PolicyResponse.ProcessingTime.String(), "mutationRulesApplied", resp.PolicyResponse.RulesAppliedCount)
|
2019-12-26 23:34:19 +00:00
|
|
|
}
|