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"
|
2022-01-05 01:36:33 +00:00
|
|
|
"reflect"
|
2021-10-13 18:03:25 +00:00
|
|
|
"time"
|
|
|
|
|
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"
|
2022-03-25 08:40:25 +00:00
|
|
|
kyverno "github.com/kyverno/kyverno/api/kyverno/v1"
|
2022-04-25 16:36:31 +00:00
|
|
|
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/store"
|
2022-03-28 14:01:27 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/autogen"
|
2022-03-25 08:40:25 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/mutate"
|
2020-10-07 18:12:31 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/response"
|
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-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) {
|
2019-08-24 01:34:23 +00:00
|
|
|
startTime := time.Now()
|
2019-11-09 02:57:27 +00:00
|
|
|
policy := policyContext.Policy
|
2022-03-31 12:25:54 +00:00
|
|
|
resp = &response.EngineResponse{
|
|
|
|
Policy: policy,
|
|
|
|
}
|
2022-04-25 12:20:40 +00:00
|
|
|
matchedResource := policyContext.NewResource
|
2020-12-23 23:10:07 +00:00
|
|
|
ctx := policyContext.JSONContext
|
2022-01-25 09:00:18 +00:00
|
|
|
var skippedRules []string
|
2020-09-22 21:11:49 +00:00
|
|
|
|
2022-04-25 12:20:40 +00:00
|
|
|
logger := log.Log.WithName("EngineMutate").WithValues("policy", policy.GetName(), "kind", matchedResource.GetKind(),
|
|
|
|
"namespace", matchedResource.GetNamespace(), "name", matchedResource.GetName())
|
2020-07-09 18:48:34 +00:00
|
|
|
|
2022-04-19 15:35:12 +00:00
|
|
|
logger.V(4).Info("start mutate policy processing", "startTime", startTime)
|
2020-01-16 19:57:28 +00:00
|
|
|
|
2022-04-25 12:20:40 +00:00
|
|
|
startMutateResultResponse(resp, policy, matchedResource)
|
2020-12-23 23:10:07 +00:00
|
|
|
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
|
|
|
|
|
2022-03-30 13:04:30 +00:00
|
|
|
for _, rule := range autogen.ComputeRules(policy) {
|
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)
|
2022-01-05 01:36:33 +00:00
|
|
|
var 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
|
|
|
|
2022-04-25 12:20:40 +00:00
|
|
|
if err = MatchesResourceDescription(matchedResource, rule, policyContext.AdmissionInfo, excludeResource, policyContext.NamespaceLabels, policyContext.Policy.GetNamespace()); err != nil {
|
2021-01-07 19:24:38 +00:00
|
|
|
logger.V(4).Info("rule not matched", "reason", err.Error())
|
2022-01-25 09:00:18 +00:00
|
|
|
skippedRules = append(skippedRules, 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
|
|
|
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 {
|
2022-04-09 11:52:50 +00:00
|
|
|
if err := ctx.AddResource(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
|
|
|
}
|
|
|
|
|
2022-01-18 12:59:35 +00:00
|
|
|
if err := LoadContext(logger, rule.Context, 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()
|
2022-04-25 12:20:40 +00:00
|
|
|
var patchedResources []unstructured.Unstructured
|
|
|
|
if !policyContext.AdmissionOperation && rule.IsMutateExisting() {
|
2022-05-06 05:46:36 +00:00
|
|
|
targets, err := loadTargets(ruleCopy.Mutation.Targets, policyContext, logger)
|
2022-04-25 12:20:40 +00:00
|
|
|
if err != nil {
|
|
|
|
rr := ruleResponse(rule, response.Mutation, err.Error(), response.RuleStatusError, nil)
|
|
|
|
resp.PolicyResponse.Rules = append(resp.PolicyResponse.Rules, *rr)
|
|
|
|
} else {
|
|
|
|
patchedResources = append(patchedResources, targets...)
|
|
|
|
}
|
2021-10-07 01:31:20 +00:00
|
|
|
} else {
|
2022-04-25 12:20:40 +00:00
|
|
|
patchedResources = append(patchedResources, matchedResource)
|
2021-07-28 16:54:50 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 12:20:40 +00:00
|
|
|
for _, patchedResource := range patchedResources {
|
|
|
|
if reflect.DeepEqual(patchedResource, unstructured.Unstructured{}) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.V(4).Info("apply rule to resource", "rule", rule.Name, "resource namespace", patchedResource.GetNamespace(), "resource name", patchedResource.GetName())
|
|
|
|
var ruleResp *response.RuleResponse
|
|
|
|
if rule.Mutation.ForEachMutation != nil {
|
|
|
|
ruleResp, patchedResource = mutateForEach(ruleCopy, policyContext, patchedResource, logger)
|
2021-10-07 01:31:20 +00:00
|
|
|
} else {
|
2022-04-25 12:20:40 +00:00
|
|
|
ruleResp, patchedResource = mutateResource(ruleCopy, policyContext, patchedResource, logger)
|
|
|
|
}
|
|
|
|
|
|
|
|
matchedResource = patchedResource
|
|
|
|
|
|
|
|
if ruleResp != nil {
|
|
|
|
resp.PolicyResponse.Rules = append(resp.PolicyResponse.Rules, *ruleResp)
|
|
|
|
if ruleResp.Status == response.RuleStatusError {
|
|
|
|
incrementErrorCount(resp)
|
|
|
|
} else {
|
|
|
|
incrementAppliedCount(resp)
|
|
|
|
}
|
2021-10-07 01:31:20 +00:00
|
|
|
}
|
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 {
|
2022-01-25 09:00:18 +00:00
|
|
|
for _, n := range skippedRules {
|
2021-12-28 16:37:41 +00:00
|
|
|
if r.Name == n {
|
|
|
|
r.Status = response.RuleStatusSkip
|
2022-01-25 09:00:18 +00:00
|
|
|
logger.V(4).Info("rule Status set as skip", "rule skippedRules", r.Name)
|
2021-12-28 16:37:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-25 12:20:40 +00:00
|
|
|
resp.PatchedResource = matchedResource
|
2021-10-07 01:31:20 +00:00
|
|
|
return resp
|
|
|
|
}
|
2021-04-13 18:44:43 +00:00
|
|
|
|
2022-01-05 01:36:33 +00:00
|
|
|
func mutateResource(rule *kyverno.Rule, ctx *PolicyContext, resource unstructured.Unstructured, logger logr.Logger) (*response.RuleResponse, unstructured.Unstructured) {
|
2022-03-06 19:07:51 +00:00
|
|
|
preconditionsPassed, err := checkPreconditions(logger, ctx, rule.GetAnyAllConditions())
|
2022-01-05 01:36:33 +00:00
|
|
|
if err != nil {
|
2022-04-06 19:04:08 +00:00
|
|
|
return ruleError(rule, response.Mutation, "failed to evaluate preconditions", err), resource
|
2022-01-05 01:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !preconditionsPassed {
|
2022-04-25 12:20:40 +00:00
|
|
|
return ruleResponse(*rule, response.Mutation, "preconditions not met", response.RuleStatusSkip, &resource), resource
|
2022-01-05 01:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mutateResp := mutate.Mutate(rule, ctx.JSONContext, resource, logger)
|
2022-04-25 12:20:40 +00:00
|
|
|
ruleResp := buildRuleResponse(rule, mutateResp, &mutateResp.PatchedResource)
|
2022-01-05 01:36:33 +00:00
|
|
|
return ruleResp, mutateResp.PatchedResource
|
|
|
|
}
|
|
|
|
|
|
|
|
func mutateForEach(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
|
|
|
patchedResource := resource
|
2022-01-05 01:36:33 +00:00
|
|
|
var applyCount int
|
2021-10-07 01:31:20 +00:00
|
|
|
allPatches := make([][]byte, 0)
|
|
|
|
|
2022-01-05 01:36:33 +00:00
|
|
|
for _, foreach := range foreachList {
|
2022-01-18 12:59:35 +00:00
|
|
|
if err := LoadContext(logger, rule.Context, ctx, rule.Name); err != nil {
|
2021-10-14 07:20:52 +00:00
|
|
|
logger.Error(err, "failed to load context")
|
2022-04-06 19:04:08 +00:00
|
|
|
return ruleError(rule, response.Mutation, "failed to load context", err), resource
|
2021-10-07 01:31:20 +00:00
|
|
|
}
|
|
|
|
|
2022-03-06 19:07:51 +00:00
|
|
|
preconditionsPassed, err := checkPreconditions(logger, ctx, rule.GetAnyAllConditions())
|
2021-10-14 07:20:52 +00:00
|
|
|
if err != nil {
|
2022-04-06 19:04:08 +00:00
|
|
|
return ruleError(rule, response.Mutation, "failed to evaluate preconditions", err), resource
|
2022-01-05 01:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !preconditionsPassed {
|
2022-04-25 12:20:40 +00:00
|
|
|
return ruleResponse(*rule, response.Mutation, "preconditions not met", response.RuleStatusSkip, &patchedResource), 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)
|
2022-04-06 19:04:08 +00:00
|
|
|
return ruleError(rule, response.Mutation, msg, err), resource
|
2021-07-22 21:37:09 +00:00
|
|
|
}
|
|
|
|
|
2022-01-25 09:00:18 +00:00
|
|
|
mutateResp := mutateElements(rule.Name, foreach, ctx, elements, patchedResource, logger)
|
2022-01-05 01:36:33 +00:00
|
|
|
if mutateResp.Status == response.RuleStatusError {
|
|
|
|
logger.Error(err, "failed to mutate elements")
|
2022-04-25 12:20:40 +00:00
|
|
|
return buildRuleResponse(rule, mutateResp, nil), resource
|
2022-01-05 01:36:33 +00:00
|
|
|
}
|
2021-10-14 07:20:52 +00:00
|
|
|
|
2022-01-05 01:36:33 +00:00
|
|
|
if mutateResp.Status != response.RuleStatusSkip {
|
2021-10-14 07:20:52 +00:00
|
|
|
applyCount++
|
2022-01-05 01:36:33 +00:00
|
|
|
if len(mutateResp.Patches) > 0 {
|
|
|
|
patchedResource = mutateResp.PatchedResource
|
|
|
|
allPatches = append(allPatches, mutateResp.Patches...)
|
|
|
|
}
|
2021-10-14 07:20:52 +00:00
|
|
|
}
|
2020-06-26 00:24:10 +00:00
|
|
|
}
|
|
|
|
|
2021-10-07 01:31:20 +00:00
|
|
|
if applyCount == 0 {
|
2022-04-25 12:20:40 +00:00
|
|
|
return ruleResponse(*rule, response.Mutation, "0 elements processed", response.RuleStatusSkip, &resource), resource
|
2021-10-07 01:31:20 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 12:20:40 +00:00
|
|
|
r := ruleResponse(*rule, response.Mutation, fmt.Sprintf("%d elements processed", applyCount), response.RuleStatusPass, &patchedResource)
|
2021-10-07 01:31:20 +00:00
|
|
|
r.Patches = allPatches
|
|
|
|
return r, patchedResource
|
|
|
|
}
|
|
|
|
|
2022-04-28 04:41:10 +00:00
|
|
|
func mutateElements(name string, foreach kyverno.ForEachMutation, ctx *PolicyContext, elements []interface{}, resource unstructured.Unstructured, logger logr.Logger) *mutate.Response {
|
2022-01-05 01:36:33 +00:00
|
|
|
ctx.JSONContext.Checkpoint()
|
|
|
|
defer ctx.JSONContext.Restore()
|
2020-04-04 17:09:21 +00:00
|
|
|
|
2022-01-05 01:36:33 +00:00
|
|
|
patchedResource := resource
|
|
|
|
var allPatches [][]byte
|
2022-03-25 15:15:31 +00:00
|
|
|
if foreach.RawPatchStrategicMerge != nil {
|
|
|
|
invertedElement(elements)
|
|
|
|
}
|
2021-10-14 07:20:52 +00:00
|
|
|
|
2022-01-05 01:36:33 +00:00
|
|
|
for i, e := range elements {
|
|
|
|
ctx.JSONContext.Reset()
|
|
|
|
ctx := ctx.Copy()
|
2022-04-25 16:36:31 +00:00
|
|
|
store.SetForeachElement(i)
|
2022-05-02 16:39:37 +00:00
|
|
|
falseVar := false
|
|
|
|
if err := addElementToContext(ctx, e, i, &falseVar); err != nil {
|
2022-01-05 01:36:33 +00:00
|
|
|
return mutateError(err, fmt.Sprintf("failed to add element to mutate.foreach[%d].context", i))
|
|
|
|
}
|
2021-10-07 01:31:20 +00:00
|
|
|
|
2022-01-18 12:59:35 +00:00
|
|
|
if err := LoadContext(logger, foreach.Context, ctx, name); err != nil {
|
2022-01-05 01:36:33 +00:00
|
|
|
return mutateError(err, fmt.Sprintf("failed to load to mutate.foreach[%d].context", i))
|
|
|
|
}
|
|
|
|
|
|
|
|
preconditionsPassed, err := checkPreconditions(logger, ctx, foreach.AnyAllConditions)
|
|
|
|
if err != nil {
|
|
|
|
return mutateError(err, fmt.Sprintf("failed to evaluate mutate.foreach[%d].preconditions", i))
|
|
|
|
}
|
|
|
|
|
|
|
|
if !preconditionsPassed {
|
|
|
|
logger.Info("mutate.foreach.preconditions not met", "elementIndex", i)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
mutateResp := mutate.ForEach(name, foreach, ctx.JSONContext, patchedResource, logger)
|
|
|
|
if mutateResp.Status == response.RuleStatusFail || mutateResp.Status == response.RuleStatusError {
|
|
|
|
return mutateResp
|
|
|
|
}
|
2021-10-07 01:31:20 +00:00
|
|
|
|
2022-01-05 01:36:33 +00:00
|
|
|
if len(mutateResp.Patches) > 0 {
|
|
|
|
patchedResource = mutateResp.PatchedResource
|
|
|
|
allPatches = append(allPatches, mutateResp.Patches...)
|
|
|
|
}
|
2021-10-07 01:31:20 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 01:36:33 +00:00
|
|
|
return &mutate.Response{
|
|
|
|
Status: response.RuleStatusPass,
|
|
|
|
PatchedResource: patchedResource,
|
|
|
|
Patches: allPatches,
|
|
|
|
Message: "foreach mutation applied",
|
2021-10-07 01:31:20 +00:00
|
|
|
}
|
2022-01-05 01:36:33 +00:00
|
|
|
}
|
2021-10-07 01:31:20 +00:00
|
|
|
|
2022-01-05 01:36:33 +00:00
|
|
|
func mutateError(err error, message string) *mutate.Response {
|
|
|
|
return &mutate.Response{
|
|
|
|
Status: response.RuleStatusFail,
|
|
|
|
PatchedResource: unstructured.Unstructured{},
|
|
|
|
Patches: nil,
|
|
|
|
Message: fmt.Sprintf("failed to add element to context: %v", err),
|
|
|
|
}
|
|
|
|
}
|
2021-10-14 07:20:52 +00:00
|
|
|
|
2022-04-25 12:20:40 +00:00
|
|
|
func buildRuleResponse(rule *kyverno.Rule, mutateResp *mutate.Response, patchedResource *unstructured.Unstructured) *response.RuleResponse {
|
|
|
|
resp := ruleResponse(*rule, response.Mutation, mutateResp.Message, mutateResp.Status, patchedResource)
|
2021-10-07 01:31:20 +00:00
|
|
|
if resp.Status == response.RuleStatusPass {
|
2022-01-05 01:36:33 +00:00
|
|
|
resp.Patches = mutateResp.Patches
|
|
|
|
resp.Message = buildSuccessMessage(mutateResp.PatchedResource)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp
|
|
|
|
}
|
2021-10-07 01:31:20 +00:00
|
|
|
|
2022-01-05 01:36:33 +00:00
|
|
|
func buildSuccessMessage(r unstructured.Unstructured) string {
|
|
|
|
if reflect.DeepEqual(unstructured.Unstructured{}, r) {
|
|
|
|
return "mutated resource"
|
2021-10-07 01:31:20 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 01:36:33 +00:00
|
|
|
ns := r.GetNamespace()
|
|
|
|
if ns == "" {
|
|
|
|
return fmt.Sprintf("mutated %s/%s", r.GetKind(), r.GetName())
|
2021-10-07 01:31:20 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 01:36:33 +00:00
|
|
|
return fmt.Sprintf("mutated %s/%s in namespace %s", r.GetKind(), r.GetName(), r.GetNamespace())
|
2020-02-14 19:59:28 +00:00
|
|
|
}
|
2019-12-26 23:34:19 +00:00
|
|
|
|
2022-03-30 13:04:30 +00:00
|
|
|
func startMutateResultResponse(resp *response.EngineResponse, policy kyverno.PolicyInterface, 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
|
|
|
}
|