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"
|
|
|
|
|
2023-02-09 15:15:51 +00:00
|
|
|
"github.com/go-logr/logr"
|
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-02-07 04:30:15 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/internal"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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-02-08 05:55:03 +00:00
|
|
|
ctx context.Context,
|
2023-02-09 15:15:51 +00:00
|
|
|
logger logr.Logger,
|
2023-01-31 15:28:48 +00:00
|
|
|
policyContext engineapi.PolicyContext,
|
2023-01-31 14:30:40 +00:00
|
|
|
) (resp *engineapi.EngineResponse) {
|
2023-02-09 15:15:51 +00:00
|
|
|
return e.filterRules(policyContext, logger, time.Now())
|
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-02-09 15:15:51 +00:00
|
|
|
logger logr.Logger,
|
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()
|
2023-02-10 14:04:41 +00:00
|
|
|
resp := engineapi.NewEngineResponseFromPolicyContext(policyContext, nil)
|
2023-02-10 08:11:21 +00:00
|
|
|
resp.PolicyResponse = engineapi.PolicyResponse{
|
2023-02-10 19:35:55 +00:00
|
|
|
Stats: engineapi.PolicyStats{
|
2023-02-10 08:11:21 +00:00
|
|
|
ExecutionStats: engineapi.ExecutionStats{
|
|
|
|
Timestamp: startTime.Unix(),
|
2022-04-25 12:20:40 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-02-06 12:49:04 +00:00
|
|
|
if e.configuration.ToFilter(kind, namespace, name) {
|
2023-02-09 15:15:51 +00:00
|
|
|
logger.Info("resource excluded")
|
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-09 15:15:51 +00:00
|
|
|
logger := internal.LoggerWithRule(logger, rule)
|
|
|
|
if ruleResp := e.filterRule(rule, logger, 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-02-09 15:15:51 +00:00
|
|
|
logger logr.Logger,
|
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
|
|
|
|
}
|
|
|
|
|
2023-01-05 06:23:44 +00:00
|
|
|
kindsInPolicy := append(rule.MatchResources.GetKinds(), rule.ExcludeResources.GetKinds()...)
|
2023-02-07 15:09:15 +00:00
|
|
|
subresourceGVKToAPIResource := GetSubresourceGVKToAPIResourceMap(e.client, kindsInPolicy, policyContext)
|
2023-01-05 06:23:44 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-02-07 16:51:25 +00:00
|
|
|
// check if there is a corresponding policy exception
|
|
|
|
ruleResp := hasPolicyExceptions(logger, ruleType, e.exceptionSelector, policyContext, &rule, subresourceGVKToAPIResource, e.configuration)
|
|
|
|
if ruleResp != nil {
|
|
|
|
return ruleResp
|
|
|
|
}
|
|
|
|
|
2022-04-25 12:20:40 +00:00
|
|
|
startTime := time.Now()
|
|
|
|
|
2023-01-31 15:28:48 +00:00
|
|
|
newResource := policyContext.NewResource()
|
|
|
|
oldResource := policyContext.OldResource()
|
|
|
|
admissionInfo := policyContext.AdmissionInfo()
|
|
|
|
ctx := policyContext.JSONContext()
|
2023-02-22 10:08:41 +00:00
|
|
|
excludeGroupRole := e.configuration.GetExcludedGroups()
|
2023-01-31 15:28:48 +00:00
|
|
|
namespaceLabels := policyContext.NamespaceLabels()
|
2022-04-25 12:20:40 +00:00
|
|
|
|
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-02-10 19:35:55 +00:00
|
|
|
Stats: engineapi.ExecutionStats{
|
2023-01-30 14:49:44 +00:00
|
|
|
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-08 05:55:03 +00:00
|
|
|
if err := internal.LoadContext(context.TODO(), e, policyContext, rule); 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-02-07 16:51:25 +00:00
|
|
|
return internal.RuleSkip(ruleCopy, ruleType, "")
|
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-02-10 19:35:55 +00:00
|
|
|
Stats: engineapi.ExecutionStats{
|
2023-01-30 14:49:44 +00:00
|
|
|
ProcessingTime: time.Since(startTime),
|
|
|
|
Timestamp: startTime.Unix(),
|
2022-04-25 12:20:40 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|