2022-04-25 12:20:40 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2022-12-09 13:45:11 +00:00
|
|
|
"context"
|
2022-04-25 12:20:40 +00:00
|
|
|
"time"
|
|
|
|
|
2022-05-17 11:12:43 +00:00
|
|
|
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
|
2022-04-25 12:20:40 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/autogen"
|
2023-01-30 11:41:09 +00:00
|
|
|
engineapi "github.com/kyverno/kyverno/pkg/engine/api"
|
2023-01-30 18:26:35 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/utils"
|
2022-04-25 12:20:40 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/variables"
|
2022-10-02 19:45:03 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/logging"
|
2022-04-25 12:20:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ApplyBackgroundChecks checks for validity of generate and mutateExisting rules on the resource
|
|
|
|
// 1. validate variables to be substitute in the general ruleInfo (match,exclude,condition)
|
2022-08-24 13:08:24 +00:00
|
|
|
// - the caller has to check the ruleResponse to determine whether the path exist
|
|
|
|
//
|
2022-04-25 12:20:40 +00:00
|
|
|
// 2. returns the list of rules that are applicable on this policy and resource, if 1 succeed
|
2023-02-06 12:49:04 +00:00
|
|
|
func (e *engine) applyBackgroundChecks(
|
2023-01-31 15:28:48 +00:00
|
|
|
policyContext engineapi.PolicyContext,
|
2023-01-31 14:30:40 +00:00
|
|
|
) (resp *engineapi.EngineResponse) {
|
2022-04-25 12:20:40 +00:00
|
|
|
policyStartTime := time.Now()
|
2023-02-06 12:49:04 +00:00
|
|
|
return e.filterRules(policyContext, policyStartTime)
|
2022-04-25 12:20:40 +00:00
|
|
|
}
|
|
|
|
|
2023-02-06 12:49:04 +00:00
|
|
|
func (e *engine) filterRules(
|
2023-01-31 15:28:48 +00:00
|
|
|
policyContext engineapi.PolicyContext,
|
2023-01-31 14:30:40 +00:00
|
|
|
startTime time.Time,
|
|
|
|
) *engineapi.EngineResponse {
|
2023-01-31 15:28:48 +00:00
|
|
|
newResource := policyContext.NewResource()
|
|
|
|
policy := policyContext.Policy()
|
|
|
|
kind := newResource.GetKind()
|
|
|
|
name := newResource.GetName()
|
|
|
|
namespace := newResource.GetNamespace()
|
|
|
|
apiVersion := newResource.GetAPIVersion()
|
2023-01-30 11:41:09 +00:00
|
|
|
resp := &engineapi.EngineResponse{
|
|
|
|
PolicyResponse: engineapi.PolicyResponse{
|
|
|
|
Policy: engineapi.PolicySpec{
|
2023-01-31 15:28:48 +00:00
|
|
|
Name: policy.GetName(),
|
|
|
|
Namespace: policy.GetNamespace(),
|
2022-04-25 12:20:40 +00:00
|
|
|
},
|
2023-01-30 11:41:09 +00:00
|
|
|
PolicyStats: engineapi.PolicyStats{
|
2023-01-30 14:49:44 +00:00
|
|
|
ExecutionStats: engineapi.ExecutionStats{
|
|
|
|
Timestamp: startTime.Unix(),
|
|
|
|
},
|
2022-04-25 12:20:40 +00:00
|
|
|
},
|
2023-01-30 11:41:09 +00:00
|
|
|
Resource: engineapi.ResourceSpec{
|
2022-04-25 12:20:40 +00:00
|
|
|
Kind: kind,
|
|
|
|
Name: name,
|
|
|
|
Namespace: namespace,
|
|
|
|
APIVersion: apiVersion,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-02-06 12:49:04 +00:00
|
|
|
if e.configuration.ToFilter(kind, namespace, name) {
|
2022-10-02 19:45:03 +00:00
|
|
|
logging.WithName("ApplyBackgroundChecks").Info("resource excluded", "kind", kind, "namespace", namespace, "name", name)
|
2022-04-25 12:20:40 +00:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2023-01-31 15:28:48 +00:00
|
|
|
applyRules := policy.GetSpec().GetApplyRules()
|
|
|
|
for _, rule := range autogen.ComputeRules(policy) {
|
2023-02-06 12:49:04 +00:00
|
|
|
if ruleResp := e.filterRule(rule, policyContext); ruleResp != nil {
|
2022-04-25 12:20:40 +00:00
|
|
|
resp.PolicyResponse.Rules = append(resp.PolicyResponse.Rules, *ruleResp)
|
2023-01-30 11:41:09 +00:00
|
|
|
if applyRules == kyvernov1.ApplyOne && ruleResp.Status != engineapi.RuleStatusSkip {
|
2022-07-29 07:02:26 +00:00
|
|
|
break
|
|
|
|
}
|
2022-04-25 12:20:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2023-02-06 12:49:04 +00:00
|
|
|
func (e *engine) filterRule(
|
2023-01-31 14:30:40 +00:00
|
|
|
rule kyvernov1.Rule,
|
2023-01-31 15:28:48 +00:00
|
|
|
policyContext engineapi.PolicyContext,
|
2023-01-31 14:30:40 +00:00
|
|
|
) *engineapi.RuleResponse {
|
2022-04-25 12:20:40 +00:00
|
|
|
if !rule.HasGenerate() && !rule.IsMutateExisting() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-21 04:35:26 +00:00
|
|
|
logger := logging.WithName("exception")
|
2023-01-05 06:23:44 +00:00
|
|
|
|
|
|
|
kindsInPolicy := append(rule.MatchResources.GetKinds(), rule.ExcludeResources.GetKinds()...)
|
|
|
|
subresourceGVKToAPIResource := GetSubresourceGVKToAPIResourceMap(kindsInPolicy, policyContext)
|
|
|
|
|
2022-12-21 04:35:26 +00:00
|
|
|
// check if there is a corresponding policy exception
|
2023-02-06 12:49:04 +00:00
|
|
|
ruleResp := hasPolicyExceptions(logger, e.exceptionSelector, policyContext, &rule, subresourceGVKToAPIResource, e.configuration)
|
2022-12-21 04:35:26 +00:00
|
|
|
if ruleResp != nil {
|
|
|
|
return ruleResp
|
|
|
|
}
|
|
|
|
|
2023-01-30 11:41:09 +00:00
|
|
|
ruleType := engineapi.Mutation
|
2022-04-25 12:20:40 +00:00
|
|
|
if rule.HasGenerate() {
|
2023-01-30 11:41:09 +00:00
|
|
|
ruleType = engineapi.Generation
|
2022-04-25 12:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
startTime := time.Now()
|
|
|
|
|
2023-01-31 15:28:48 +00:00
|
|
|
policy := policyContext.Policy()
|
|
|
|
newResource := policyContext.NewResource()
|
|
|
|
oldResource := policyContext.OldResource()
|
|
|
|
admissionInfo := policyContext.AdmissionInfo()
|
|
|
|
ctx := policyContext.JSONContext()
|
2023-02-06 12:49:04 +00:00
|
|
|
excludeGroupRole := e.configuration.GetExcludeGroupRole()
|
2023-01-31 15:28:48 +00:00
|
|
|
namespaceLabels := policyContext.NamespaceLabels()
|
2022-04-25 12:20:40 +00:00
|
|
|
|
2022-12-21 04:35:26 +00:00
|
|
|
logger = logging.WithName(string(ruleType)).WithValues("policy", policy.GetName(),
|
2022-04-25 12:20:40 +00:00
|
|
|
"kind", newResource.GetKind(), "namespace", newResource.GetNamespace(), "name", newResource.GetName())
|
|
|
|
|
2023-01-31 15:28:48 +00:00
|
|
|
if err := MatchesResourceDescription(subresourceGVKToAPIResource, newResource, rule, admissionInfo, excludeGroupRole, namespaceLabels, "", policyContext.SubResource()); err != nil {
|
2023-01-30 11:41:09 +00:00
|
|
|
if ruleType == engineapi.Generation {
|
2022-04-25 12:20:40 +00:00
|
|
|
// if the oldResource matched, return "false" to delete GR for it
|
2023-01-31 15:28:48 +00:00
|
|
|
if err = MatchesResourceDescription(subresourceGVKToAPIResource, oldResource, rule, admissionInfo, excludeGroupRole, namespaceLabels, "", policyContext.SubResource()); err == nil {
|
2023-01-30 11:41:09 +00:00
|
|
|
return &engineapi.RuleResponse{
|
2022-04-25 12:20:40 +00:00
|
|
|
Name: rule.Name,
|
|
|
|
Type: ruleType,
|
2023-01-30 11:41:09 +00:00
|
|
|
Status: engineapi.RuleStatusFail,
|
2023-01-30 14:49:44 +00:00
|
|
|
ExecutionStats: engineapi.ExecutionStats{
|
|
|
|
ProcessingTime: time.Since(startTime),
|
|
|
|
Timestamp: startTime.Unix(),
|
2022-04-25 12:20:40 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-25 14:26:22 +00:00
|
|
|
logger.V(4).Info("rule not matched", "reason", err.Error())
|
2022-04-25 12:20:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-31 15:28:48 +00:00
|
|
|
policyContext.JSONContext().Checkpoint()
|
|
|
|
defer policyContext.JSONContext().Restore()
|
2022-04-25 12:20:40 +00:00
|
|
|
|
2023-02-06 12:49:04 +00:00
|
|
|
if err := LoadContext(context.TODO(), e.contextLoader, rule.Context, policyContext, rule.Name); err != nil {
|
2022-04-25 12:20:40 +00:00
|
|
|
logger.V(4).Info("cannot add external data to the context", "reason", err.Error())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ruleCopy := rule.DeepCopy()
|
|
|
|
if after, err := variables.SubstituteAllInPreconditions(logger, ctx, ruleCopy.GetAnyAllConditions()); err != nil {
|
|
|
|
logger.V(4).Info("failed to substitute vars in preconditions, skip current rule", "rule name", ruleCopy.Name)
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
ruleCopy.SetAnyAllConditions(after)
|
|
|
|
}
|
|
|
|
|
|
|
|
// operate on the copy of the conditions, as we perform variable substitution
|
2023-01-30 18:26:35 +00:00
|
|
|
copyConditions, err := utils.TransformConditions(ruleCopy.GetAnyAllConditions())
|
2022-04-25 12:20:40 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.V(4).Info("cannot copy AnyAllConditions", "reason", err.Error())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// evaluate pre-conditions
|
|
|
|
if !variables.EvaluateConditions(logger, ctx, copyConditions) {
|
|
|
|
logger.V(4).Info("skip rule as preconditions are not met", "rule", ruleCopy.Name)
|
2023-01-30 11:41:09 +00:00
|
|
|
return &engineapi.RuleResponse{
|
2022-11-10 15:10:29 +00:00
|
|
|
Name: ruleCopy.Name,
|
|
|
|
Type: ruleType,
|
2023-01-30 11:41:09 +00:00
|
|
|
Status: engineapi.RuleStatusSkip,
|
2023-01-30 14:49:44 +00:00
|
|
|
ExecutionStats: engineapi.ExecutionStats{
|
|
|
|
ProcessingTime: time.Since(startTime),
|
|
|
|
Timestamp: startTime.Unix(),
|
2022-11-10 15:10:29 +00:00
|
|
|
},
|
|
|
|
}
|
2022-04-25 12:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// build rule Response
|
2023-01-30 11:41:09 +00:00
|
|
|
return &engineapi.RuleResponse{
|
2022-04-25 12:20:40 +00:00
|
|
|
Name: ruleCopy.Name,
|
|
|
|
Type: ruleType,
|
2023-01-30 11:41:09 +00:00
|
|
|
Status: engineapi.RuleStatusPass,
|
2023-01-30 14:49:44 +00:00
|
|
|
ExecutionStats: engineapi.ExecutionStats{
|
|
|
|
ProcessingTime: time.Since(startTime),
|
|
|
|
Timestamp: startTime.Unix(),
|
2022-04-25 12:20:40 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|