2019-05-14 01:17:28 +00:00
|
|
|
package engine
|
2019-05-13 18:27:47 +00:00
|
|
|
|
|
|
|
import (
|
2020-02-24 14:42:39 +00:00
|
|
|
"time"
|
2020-01-10 19:59:05 +00:00
|
|
|
|
2020-03-17 18:05:20 +00:00
|
|
|
"github.com/go-logr/logr"
|
2020-03-18 00:23:18 +00:00
|
|
|
|
2020-10-07 18:12:31 +00:00
|
|
|
kyverno "github.com/kyverno/kyverno/pkg/api/kyverno/v1"
|
|
|
|
"github.com/kyverno/kyverno/pkg/engine/context"
|
|
|
|
"github.com/kyverno/kyverno/pkg/engine/response"
|
|
|
|
"github.com/kyverno/kyverno/pkg/engine/variables"
|
|
|
|
"github.com/kyverno/kyverno/pkg/resourcecache"
|
2020-01-07 18:33: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-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-01-10 19:59:05 +00:00
|
|
|
// 1. validate variables to be susbtitute in the general ruleInfo (match,exclude,condition)
|
|
|
|
// - 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) {
|
2019-11-13 23:46:43 +00:00
|
|
|
policy := policyContext.Policy
|
2020-12-01 20:30:08 +00:00
|
|
|
new := policyContext.NewResource
|
|
|
|
old := policyContext.OldResource
|
2020-01-07 18:33:28 +00:00
|
|
|
admissionInfo := policyContext.AdmissionInfo
|
2020-01-07 23:13:57 +00:00
|
|
|
ctx := policyContext.Context
|
2020-08-08 00:09:24 +00:00
|
|
|
|
2020-09-22 21:11:49 +00:00
|
|
|
resCache := policyContext.ResourceCache
|
|
|
|
jsonContext := policyContext.JSONContext
|
2020-12-01 20:30:08 +00:00
|
|
|
logger := log.Log.WithName("Generate").WithValues("policy", policy.Name, "kind", new.GetKind(), "namespace", new.GetNamespace(), "name", new.GetName())
|
2020-08-08 00:09:24 +00:00
|
|
|
|
2020-12-01 20:30:08 +00:00
|
|
|
return filterRules(policy, new, old, admissionInfo, ctx, logger, policyContext.ExcludeGroupRole, resCache, jsonContext)
|
2020-01-07 18:33:28 +00:00
|
|
|
}
|
2019-11-13 23:46:43 +00:00
|
|
|
|
2020-12-01 20:30:08 +00:00
|
|
|
func filterRule(rule kyverno.Rule, new, old unstructured.Unstructured, admissionInfo kyverno.RequestInfo, ctx context.EvalInterface, log logr.Logger, excludeGroupRole []string, resCache resourcecache.ResourceCacheIface, jsonContext *context.Context) *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-01 20:30:08 +00:00
|
|
|
if err := MatchesResourceDescription(new, rule, admissionInfo, excludeGroupRole); err != nil {
|
|
|
|
if err := MatchesResourceDescription(old, rule, admissionInfo, excludeGroupRole); err == nil {
|
|
|
|
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-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
|
|
|
|
if err := AddResourceToContext(log, rule.Context, resCache, jsonContext); err != nil {
|
2020-11-14 00:36:25 +00:00
|
|
|
log.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-03-17 18:05:20 +00:00
|
|
|
if !variables.EvaluateConditions(log, ctx, copyConditions) {
|
|
|
|
log.V(4).Info("preconditions not satisfied, skipping rule", "rule", rule.Name)
|
2020-01-07 23:13:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2020-12-01 20:30:08 +00:00
|
|
|
func filterRules(policy kyverno.ClusterPolicy, new, old unstructured.Unstructured, admissionInfo kyverno.RequestInfo, ctx context.EvalInterface, log logr.Logger, excludeGroupRole []string, resCache resourcecache.ResourceCacheIface, jsonContext *context.Context) response.EngineResponse {
|
2020-01-07 18:33:28 +00:00
|
|
|
resp := response.EngineResponse{
|
|
|
|
PolicyResponse: response.PolicyResponse{
|
|
|
|
Policy: policy.Name,
|
|
|
|
Resource: response.ResourceSpec{
|
2020-12-01 20:30:08 +00:00
|
|
|
Kind: new.GetKind(),
|
|
|
|
Name: new.GetName(),
|
|
|
|
Namespace: new.GetNamespace(),
|
2020-01-07 18:33:28 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2019-05-20 11:48:38 +00:00
|
|
|
for _, rule := range policy.Spec.Rules {
|
2020-12-01 20:30:08 +00:00
|
|
|
if ruleResp := filterRule(rule, new, old, admissionInfo, ctx, log, excludeGroupRole, resCache, jsonContext); ruleResp != nil {
|
2020-01-07 18:33:28 +00:00
|
|
|
resp.PolicyResponse.Rules = append(resp.PolicyResponse.Rules, *ruleResp)
|
2019-06-26 01:16:02 +00:00
|
|
|
}
|
2019-05-13 18:27:47 +00:00
|
|
|
}
|
2019-12-31 01:08:50 +00:00
|
|
|
return resp
|
2019-07-26 12:11:20 +00:00
|
|
|
}
|