2022-04-25 12:20:40 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2022-12-09 13:45:11 +00:00
|
|
|
"context"
|
2024-07-25 17:36:19 +00:00
|
|
|
"strings"
|
2022-04-25 12:20:40 +00:00
|
|
|
|
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-03-27 08:09:46 +00:00
|
|
|
engineutils "github.com/kyverno/kyverno/pkg/engine/utils"
|
2022-04-25 12:20:40 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/variables"
|
2023-11-13 15:43:25 +00:00
|
|
|
"k8s.io/client-go/tools/cache"
|
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-02-09 15:15:51 +00:00
|
|
|
logger logr.Logger,
|
2023-01-31 15:28:48 +00:00
|
|
|
policyContext engineapi.PolicyContext,
|
2023-03-30 11:59:32 +00:00
|
|
|
) engineapi.PolicyResponse {
|
2024-05-29 23:29:24 +00:00
|
|
|
return e.filterRules(policyContext, logger)
|
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-03-30 11:59:32 +00:00
|
|
|
) engineapi.PolicyResponse {
|
2023-01-31 15:28:48 +00:00
|
|
|
policy := policyContext.Policy()
|
2023-03-30 11:59:32 +00:00
|
|
|
resp := engineapi.NewPolicyResponse()
|
2023-01-31 15:28:48 +00:00
|
|
|
applyRules := policy.GetSpec().GetApplyRules()
|
2024-04-04 08:09:30 +00:00
|
|
|
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 {
|
2023-03-30 11:59:32 +00:00
|
|
|
resp.Rules = append(resp.Rules, *ruleResp)
|
2023-04-05 10:35:38 +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
|
|
|
}
|
|
|
|
}
|
2023-03-23 12:58:52 +00:00
|
|
|
return resp
|
2022-04-25 12:20:40 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2023-12-22 13:07:17 +00:00
|
|
|
if !rule.HasGenerate() && !rule.HasMutateExisting() {
|
2022-04-25 12:20:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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-11-13 15:43:25 +00:00
|
|
|
// get policy exceptions that matches both policy and rule name
|
|
|
|
exceptions, err := e.GetPolicyExceptions(policyContext.Policy(), rule.Name)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err, "failed to get exceptions")
|
|
|
|
return nil
|
|
|
|
}
|
2024-07-25 17:36:19 +00:00
|
|
|
// check if there are policy exceptions that match the incoming resource
|
|
|
|
matchedExceptions := engineutils.MatchesException(exceptions, policyContext, logger)
|
|
|
|
if len(matchedExceptions) > 0 {
|
|
|
|
var keys []string
|
|
|
|
for i, exception := range matchedExceptions {
|
|
|
|
key, err := cache.MetaNamespaceKeyFunc(&matchedExceptions[i])
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err, "failed to compute policy exception key", "namespace", exception.GetNamespace(), "name", exception.GetName())
|
|
|
|
return engineapi.RuleError(rule.Name, ruleType, "failed to compute exception key", err)
|
|
|
|
}
|
|
|
|
keys = append(keys, key)
|
2023-11-13 15:43:25 +00:00
|
|
|
}
|
2024-07-25 17:36:19 +00:00
|
|
|
|
|
|
|
logger.V(3).Info("policy rule is skipped due to policy exceptions", "exceptions", keys)
|
|
|
|
return engineapi.RuleSkip(rule.Name, ruleType, "rule is skipped due to policy exception "+strings.Join(keys, ", ")).WithExceptions(matchedExceptions)
|
2023-02-07 16:51:25 +00:00
|
|
|
}
|
|
|
|
|
2023-01-31 15:28:48 +00:00
|
|
|
newResource := policyContext.NewResource()
|
|
|
|
oldResource := policyContext.OldResource()
|
|
|
|
admissionInfo := policyContext.AdmissionInfo()
|
|
|
|
namespaceLabels := policyContext.NamespaceLabels()
|
2023-03-10 13:55:08 +00:00
|
|
|
policy := policyContext.Policy()
|
2023-03-22 10:18:11 +00:00
|
|
|
gvk, subresource := policyContext.ResourceKind()
|
|
|
|
|
2023-03-29 04:22:21 +00:00
|
|
|
if err := engineutils.MatchesResourceDescription(newResource, rule, admissionInfo, namespaceLabels, policy.GetNamespace(), gvk, subresource, policyContext.Operation()); 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-03-29 04:22:21 +00:00
|
|
|
if err = engineutils.MatchesResourceDescription(oldResource, rule, admissionInfo, namespaceLabels, policy.GetNamespace(), gvk, subresource, policyContext.Operation()); err == nil {
|
2023-04-05 10:35:38 +00:00
|
|
|
return engineapi.RuleFail(rule.Name, ruleType, "")
|
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-04-05 13:51:07 +00:00
|
|
|
contextLoader := e.ContextLoader(policyContext.Policy(), rule)
|
|
|
|
if err := contextLoader(context.TODO(), rule.Context, policyContext.JSONContext()); 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// operate on the copy of the conditions, as we perform variable substitution
|
2023-05-20 21:06:54 +00:00
|
|
|
copyConditions, err := engineutils.TransformConditions(rule.GetAnyAllConditions())
|
2022-04-25 12:20:40 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.V(4).Info("cannot copy AnyAllConditions", "reason", err.Error())
|
2023-05-20 21:06:54 +00:00
|
|
|
return engineapi.RuleError(rule.Name, ruleType, "failed to convert AnyAllConditions", err)
|
2022-04-25 12:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// evaluate pre-conditions
|
2023-06-15 15:32:19 +00:00
|
|
|
pass, msg, err := variables.EvaluateConditions(logger, policyContext.JSONContext(), copyConditions)
|
|
|
|
if err != nil {
|
2023-05-20 21:06:54 +00:00
|
|
|
return engineapi.RuleError(rule.Name, ruleType, "failed to evaluate conditions", err)
|
2022-04-25 12:20:40 +00:00
|
|
|
}
|
|
|
|
|
2023-06-15 15:32:19 +00:00
|
|
|
if pass {
|
|
|
|
return engineapi.RulePass(rule.Name, ruleType, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
if policyContext.OldResource().Object != nil {
|
|
|
|
if err = policyContext.JSONContext().AddResource(policyContext.OldResource().Object); err != nil {
|
|
|
|
return engineapi.RuleError(rule.Name, ruleType, "failed to update JSON context for old resource", err)
|
|
|
|
}
|
|
|
|
if val, msg, err := variables.EvaluateConditions(logger, policyContext.JSONContext(), copyConditions); err != nil {
|
|
|
|
return engineapi.RuleError(rule.Name, ruleType, "failed to evaluate conditions for old resource", err)
|
|
|
|
} else {
|
|
|
|
if val {
|
|
|
|
return engineapi.RuleFail(rule.Name, ruleType, msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.V(4).Info("skip rule as preconditions are not met", "rule", rule.Name, "message", msg)
|
|
|
|
return engineapi.RuleSkip(rule.Name, ruleType, "")
|
2022-04-25 12:20:40 +00:00
|
|
|
}
|