2019-05-14 01:17:28 +00:00
|
|
|
package engine
|
2019-05-13 18:27:47 +00:00
|
|
|
|
|
|
|
import (
|
2020-10-07 18:12:31 +00:00
|
|
|
kyverno "github.com/kyverno/kyverno/pkg/api/kyverno/v1"
|
|
|
|
"github.com/kyverno/kyverno/pkg/engine/response"
|
|
|
|
"github.com/kyverno/kyverno/pkg/engine/variables"
|
2020-03-17 23:25:34 +00:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/log"
|
2020-12-16 20:29:16 +00:00
|
|
|
"time"
|
2019-05-13 18:27:47 +00:00
|
|
|
)
|
|
|
|
|
2020-01-24 20:05:53 +00:00
|
|
|
// Generate checks for validity of generate rule on the resource
|
2020-12-09 06:52:37 +00:00
|
|
|
// 1. validate variables to be substitute in the general ruleInfo (match,exclude,condition)
|
2020-01-10 19:59:05 +00:00
|
|
|
// - the caller has to check the ruleResponse to determine whether the path exist
|
|
|
|
// 2. returns the list of rules that are applicable on this policy and resource, if 1 succeed
|
2020-01-24 20:05:53 +00:00
|
|
|
func Generate(policyContext PolicyContext) (resp response.EngineResponse) {
|
2020-12-16 20:29:16 +00:00
|
|
|
return filterRules(policyContext)
|
|
|
|
}
|
2020-08-08 00:09:24 +00:00
|
|
|
|
2020-12-16 20:29:16 +00:00
|
|
|
func filterRules(policyContext PolicyContext) response.EngineResponse {
|
|
|
|
kind := policyContext.NewResource.GetKind()
|
|
|
|
name := policyContext.NewResource.GetName()
|
|
|
|
namespace := policyContext.NewResource.GetNamespace()
|
|
|
|
|
|
|
|
resp := response.EngineResponse{
|
|
|
|
PolicyResponse: response.PolicyResponse{
|
|
|
|
Policy: policyContext.Policy.Name,
|
|
|
|
Resource: response.ResourceSpec{
|
|
|
|
Kind: kind,
|
|
|
|
Name: name,
|
|
|
|
Namespace: namespace,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if policyContext.ExcludeResourceFunc(kind, namespace, name) {
|
|
|
|
log.Log.WithName("Generate").Info("resource excluded", "kind", kind, "namespace", namespace, "name", name)
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rule := range policyContext.Policy.Spec.Rules {
|
|
|
|
if ruleResp := filterRule(rule, policyContext); ruleResp != nil {
|
|
|
|
resp.PolicyResponse.Rules = append(resp.PolicyResponse.Rules, *ruleResp)
|
|
|
|
}
|
|
|
|
}
|
2020-08-08 00:09:24 +00:00
|
|
|
|
2020-12-16 20:29:16 +00:00
|
|
|
return resp
|
2020-01-07 18:33:28 +00:00
|
|
|
}
|
2019-11-13 23:46:43 +00:00
|
|
|
|
2020-12-16 20:29:16 +00:00
|
|
|
func filterRule(rule kyverno.Rule, policyContext PolicyContext) *response.RuleResponse {
|
2020-01-07 18:33:28 +00:00
|
|
|
if !rule.HasGenerate() {
|
|
|
|
return nil
|
|
|
|
}
|
2020-02-24 14:42:39 +00:00
|
|
|
|
|
|
|
startTime := time.Now()
|
2020-09-22 21:11:49 +00:00
|
|
|
|
2020-12-16 20:29:16 +00:00
|
|
|
policy := policyContext.Policy
|
|
|
|
newResource := policyContext.NewResource
|
|
|
|
oldResource := policyContext.OldResource
|
|
|
|
admissionInfo := policyContext.AdmissionInfo
|
|
|
|
ctx := policyContext.Context
|
|
|
|
resCache := policyContext.ResourceCache
|
|
|
|
jsonContext := policyContext.JSONContext
|
|
|
|
excludeGroupRole := policyContext.ExcludeGroupRole
|
|
|
|
|
|
|
|
logger := log.Log.WithName("Generate").WithValues("policy", policy.Name,
|
|
|
|
"kind", newResource.GetKind(), "namespace", newResource.GetNamespace(), "name", newResource.GetName())
|
|
|
|
|
|
|
|
if err := MatchesResourceDescription(newResource, rule, admissionInfo, excludeGroupRole); err != nil {
|
|
|
|
|
|
|
|
// if the oldResource matched, return "false" to delete GR for it
|
|
|
|
if err := MatchesResourceDescription(oldResource, rule, admissionInfo, excludeGroupRole); err == nil {
|
2020-12-01 20:30:08 +00:00
|
|
|
return &response.RuleResponse{
|
|
|
|
Name: rule.Name,
|
|
|
|
Type: "Generation",
|
|
|
|
Success: false,
|
|
|
|
RuleStats: response.RuleStats{
|
|
|
|
ProcessingTime: time.Since(startTime),
|
|
|
|
},
|
|
|
|
}
|
2020-08-31 18:25:13 +00:00
|
|
|
}
|
2020-12-16 20:29:16 +00:00
|
|
|
|
2020-12-01 20:30:08 +00:00
|
|
|
return nil
|
2020-01-07 18:33:28 +00:00
|
|
|
}
|
2020-10-15 00:39:45 +00:00
|
|
|
|
2020-09-22 21:11:49 +00:00
|
|
|
// add configmap json data to context
|
2020-12-16 20:29:16 +00:00
|
|
|
if err := AddResourceToContext(logger, rule.Context, resCache, jsonContext); err != nil {
|
|
|
|
logger.V(4).Info("cannot add configmaps to context", "reason", err.Error())
|
2020-09-22 21:11:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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-12-16 20:29:16 +00:00
|
|
|
if !variables.EvaluateConditions(logger, ctx, copyConditions) {
|
|
|
|
logger.V(4).Info("preconditions not satisfied, skipping rule", "rule", rule.Name)
|
2020-01-07 23:13:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-12-09 06:17:53 +00:00
|
|
|
|
2020-01-07 18:33:28 +00:00
|
|
|
// build rule Response
|
|
|
|
return &response.RuleResponse{
|
2020-02-24 14:42:39 +00:00
|
|
|
Name: rule.Name,
|
|
|
|
Type: "Generation",
|
|
|
|
Success: true,
|
|
|
|
RuleStats: response.RuleStats{
|
|
|
|
ProcessingTime: time.Since(startTime),
|
|
|
|
},
|
2019-08-19 23:40:10 +00:00
|
|
|
}
|
2020-01-07 18:33:28 +00:00
|
|
|
}
|