2019-05-14 01:17:28 +00:00
|
|
|
package engine
|
2019-05-10 05:26:22 +00:00
|
|
|
|
|
|
|
import (
|
2019-08-19 23:40:10 +00:00
|
|
|
"time"
|
2020-11-18 22:31:43 +00:00
|
|
|
|
2020-03-17 18:05:20 +00:00
|
|
|
"github.com/go-logr/logr"
|
2020-10-07 18:12:31 +00:00
|
|
|
kyverno "github.com/kyverno/kyverno/pkg/api/kyverno/v1"
|
|
|
|
"github.com/kyverno/kyverno/pkg/engine/mutate"
|
|
|
|
"github.com/kyverno/kyverno/pkg/engine/response"
|
|
|
|
"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
|
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
|
|
|
|
2020-12-23 23:10:07 +00:00
|
|
|
if ManagedPodResource(policy, patchedResource) {
|
2020-12-09 22:41:20 +00:00
|
|
|
logger.V(5).Info("skip applying policy as direct changes to pods managed by workload controllers are not allowed", "policy", policy.GetName())
|
2020-06-24 17:26:04 +00:00
|
|
|
resp.PatchedResource = patchedResource
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-02 07:22:19 +00:00
|
|
|
policyContext.JSONContext.Checkpoint()
|
|
|
|
defer policyContext.JSONContext.Restore()
|
|
|
|
|
2019-08-24 01:34:23 +00:00
|
|
|
for _, rule := range policy.Spec.Rules {
|
2020-02-14 19:59:28 +00:00
|
|
|
var ruleResponse response.RuleResponse
|
2020-03-17 18:05:20 +00:00
|
|
|
logger := logger.WithValues("rule", rule.Name)
|
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
|
|
|
|
2019-08-24 01:34:23 +00:00
|
|
|
// check if the resource satisfies the filter conditions defined in the rule
|
|
|
|
//TODO: this needs to be extracted, to filter the resource so that we can avoid passing resources that
|
2020-05-19 20:04:06 +00:00
|
|
|
// dont satisfy a policy rule resource description
|
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-02-03 21:09:42 +00:00
|
|
|
if err := MatchesResourceDescription(patchedResource, rule, policyContext.AdmissionInfo, excludeResource, policyContext.NamespaceLabels); err != nil {
|
2021-01-07 19:24:38 +00:00
|
|
|
logger.V(4).Info("rule not matched", "reason", err.Error())
|
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-02-02 07:22:19 +00:00
|
|
|
policyContext.JSONContext.Restore()
|
2021-02-01 20:59:13 +00:00
|
|
|
if err := LoadContext(logger, rule.Context, resCache, policyContext); err != nil {
|
2021-02-22 20:08:26 +00:00
|
|
|
logger.Error(err, "failed to load context")
|
2020-09-22 21:11:49 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-01-07 23:13:57 +00:00
|
|
|
|
2020-02-04 20:13:41 +00:00
|
|
|
// operate on the copy of the conditions, as we perform variable substitution
|
|
|
|
copyConditions := copyConditions(rule.Conditions)
|
2020-01-07 23:13:57 +00:00
|
|
|
// evaluate pre-conditions
|
2020-06-30 18:53:27 +00:00
|
|
|
// - handle variable substitutions
|
2020-03-17 18:05:20 +00:00
|
|
|
if !variables.EvaluateConditions(logger, ctx, copyConditions) {
|
2020-05-26 17:36:56 +00:00
|
|
|
logger.V(3).Info("resource fails the preconditions")
|
2020-01-07 23:13:57 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-02-14 19:59:28 +00:00
|
|
|
mutation := rule.Mutation.DeepCopy()
|
2020-08-05 16:11:23 +00:00
|
|
|
mutateHandler := mutate.CreateMutateHandler(rule.Name, mutation, patchedResource, ctx, logger)
|
|
|
|
ruleResponse, patchedResource = mutateHandler.Handle()
|
|
|
|
if ruleResponse.Success {
|
|
|
|
// - overlay pattern does not match the resource conditions
|
|
|
|
if ruleResponse.Patches == nil {
|
|
|
|
continue
|
2019-08-24 01:34:23 +00:00
|
|
|
}
|
2020-12-23 23:10:07 +00:00
|
|
|
|
2020-08-05 16:11:23 +00:00
|
|
|
logger.V(4).Info("mutate rule applied successfully", "ruleName", rule.Name)
|
2019-08-24 01:34:23 +00:00
|
|
|
}
|
|
|
|
|
2020-08-05 16:11:23 +00:00
|
|
|
resp.PolicyResponse.Rules = append(resp.PolicyResponse.Rules, ruleResponse)
|
2020-12-23 23:10:07 +00:00
|
|
|
incrementAppliedRuleCount(resp)
|
2020-06-26 00:24:10 +00:00
|
|
|
}
|
|
|
|
|
2019-12-12 23:02:59 +00:00
|
|
|
resp.PatchedResource = patchedResource
|
|
|
|
return resp
|
2019-08-24 01:34:23 +00:00
|
|
|
}
|
2020-04-04 17:09:21 +00:00
|
|
|
|
2020-02-14 19:59:28 +00:00
|
|
|
func incrementAppliedRuleCount(resp *response.EngineResponse) {
|
|
|
|
resp.PolicyResponse.RulesAppliedCount++
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2020-01-16 19:57:28 +00:00
|
|
|
resp.PolicyResponse.Policy = policy.Name
|
|
|
|
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-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
|
|
|
}
|