2019-05-14 01:17:28 +00:00
|
|
|
package engine
|
|
|
|
|
2019-05-14 15:10:25 +00:00
|
|
|
import (
|
2019-06-20 15:21:55 +00:00
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2020-12-07 19:26:04 +00:00
|
|
|
"strings"
|
2019-08-19 23:40:10 +00:00
|
|
|
"time"
|
2019-05-16 16:31:02 +00:00
|
|
|
|
2020-03-17 18:05:20 +00:00
|
|
|
"github.com/go-logr/logr"
|
2021-07-07 12:07:44 +00:00
|
|
|
gojmespath "github.com/jmespath/go-jmespath"
|
2020-10-07 18:12:31 +00:00
|
|
|
kyverno "github.com/kyverno/kyverno/pkg/api/kyverno/v1"
|
2020-12-09 06:52:37 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/common"
|
2020-10-07 18:12:31 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/context"
|
|
|
|
"github.com/kyverno/kyverno/pkg/engine/response"
|
|
|
|
"github.com/kyverno/kyverno/pkg/engine/utils"
|
|
|
|
"github.com/kyverno/kyverno/pkg/engine/validate"
|
|
|
|
"github.com/kyverno/kyverno/pkg/engine/variables"
|
2019-08-13 18:32:12 +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-14 15:10:25 +00:00
|
|
|
)
|
|
|
|
|
2019-09-03 22:48:13 +00:00
|
|
|
//Validate applies validation rules from policy on the resource
|
2020-12-23 23:10:07 +00:00
|
|
|
func Validate(policyContext *PolicyContext) (resp *response.EngineResponse) {
|
|
|
|
resp = &response.EngineResponse{}
|
2019-09-03 22:48:13 +00:00
|
|
|
startTime := time.Now()
|
2020-05-19 07:14:23 +00:00
|
|
|
|
2020-12-23 23:10:07 +00:00
|
|
|
logger := buildLogger(policyContext)
|
2021-01-07 19:24:38 +00:00
|
|
|
logger.V(4).Info("start policy processing", "startTime", startTime)
|
2020-05-05 15:28:02 +00:00
|
|
|
defer func() {
|
2020-12-23 23:10:07 +00:00
|
|
|
buildResponse(logger, policyContext, resp, startTime)
|
2021-01-07 19:24:38 +00:00
|
|
|
logger.V(4).Info("finished policy processing", "processingTime", resp.PolicyResponse.ProcessingTime.String(), "validationRulesApplied", resp.PolicyResponse.RulesAppliedCount)
|
2020-05-05 15:28:02 +00:00
|
|
|
}()
|
|
|
|
|
2020-12-24 02:46:12 +00:00
|
|
|
resp = validateResource(logger, policyContext)
|
|
|
|
return
|
2020-12-23 23:10:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func buildLogger(ctx *PolicyContext) logr.Logger {
|
|
|
|
logger := log.Log.WithName("EngineValidate").WithValues("policy", ctx.Policy.Name)
|
|
|
|
if reflect.DeepEqual(ctx.NewResource, unstructured.Unstructured{}) {
|
|
|
|
logger = logger.WithValues("kind", ctx.OldResource.GetKind(), "namespace", ctx.OldResource.GetNamespace(), "name", ctx.OldResource.GetName())
|
|
|
|
} else {
|
|
|
|
logger = logger.WithValues("kind", ctx.NewResource.GetKind(), "namespace", ctx.NewResource.GetNamespace(), "name", ctx.NewResource.GetName())
|
2020-06-24 17:26:04 +00:00
|
|
|
}
|
|
|
|
|
2020-12-23 23:10:07 +00:00
|
|
|
return logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildResponse(logger logr.Logger, ctx *PolicyContext, resp *response.EngineResponse, startTime time.Time) {
|
|
|
|
if reflect.DeepEqual(resp, response.EngineResponse{}) {
|
|
|
|
return
|
2019-09-03 22:48:13 +00:00
|
|
|
}
|
2020-12-23 23:10:07 +00:00
|
|
|
|
|
|
|
if reflect.DeepEqual(resp.PatchedResource, unstructured.Unstructured{}) {
|
|
|
|
// for delete requests patched resource will be oldResource since newResource is empty
|
2020-12-24 02:51:07 +00:00
|
|
|
var resource unstructured.Unstructured = ctx.NewResource
|
2020-12-23 23:10:07 +00:00
|
|
|
if reflect.DeepEqual(ctx.NewResource, unstructured.Unstructured{}) {
|
|
|
|
resource = ctx.OldResource
|
|
|
|
}
|
2020-12-24 02:51:07 +00:00
|
|
|
|
|
|
|
resp.PatchedResource = resource
|
2020-05-05 13:49:47 +00:00
|
|
|
}
|
2020-05-05 15:28:02 +00:00
|
|
|
|
2021-06-29 21:43:11 +00:00
|
|
|
resp.PolicyResponse.Policy.Name = ctx.Policy.GetName()
|
|
|
|
resp.PolicyResponse.Policy.Namespace = ctx.Policy.GetNamespace()
|
2020-12-24 02:51:07 +00:00
|
|
|
resp.PolicyResponse.Resource.Name = resp.PatchedResource.GetName()
|
|
|
|
resp.PolicyResponse.Resource.Namespace = resp.PatchedResource.GetNamespace()
|
|
|
|
resp.PolicyResponse.Resource.Kind = resp.PatchedResource.GetKind()
|
|
|
|
resp.PolicyResponse.Resource.APIVersion = resp.PatchedResource.GetAPIVersion()
|
2020-12-24 02:46:12 +00:00
|
|
|
resp.PolicyResponse.ValidationFailureAction = ctx.Policy.Spec.ValidationFailureAction
|
2020-01-10 01:44:11 +00:00
|
|
|
resp.PolicyResponse.ProcessingTime = time.Since(startTime)
|
2021-05-15 13:45:04 +00:00
|
|
|
resp.PolicyResponse.PolicyExecutionTimestamp = startTime.Unix()
|
2020-01-10 01:44:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func incrementAppliedCount(resp *response.EngineResponse) {
|
|
|
|
resp.PolicyResponse.RulesAppliedCount++
|
|
|
|
}
|
|
|
|
|
2020-12-23 23:10:07 +00:00
|
|
|
func validateResource(log logr.Logger, ctx *PolicyContext) *response.EngineResponse {
|
2019-12-31 01:08:50 +00:00
|
|
|
resp := &response.EngineResponse{}
|
2020-12-23 23:10:07 +00:00
|
|
|
if ManagedPodResource(ctx.Policy, ctx.NewResource) {
|
2021-01-02 09:10:14 +00:00
|
|
|
log.V(5).Info("skip policy as direct changes to pods managed by workload controllers are not allowed", "policy", ctx.Policy.GetName())
|
2020-07-09 18:48:34 +00:00
|
|
|
return resp
|
|
|
|
}
|
2020-12-23 23:10:07 +00:00
|
|
|
|
2021-02-02 07:22:19 +00:00
|
|
|
ctx.JSONContext.Checkpoint()
|
|
|
|
defer ctx.JSONContext.Restore()
|
|
|
|
|
2020-12-23 23:10:07 +00:00
|
|
|
for _, rule := range ctx.Policy.Spec.Rules {
|
2021-04-13 18:44:43 +00:00
|
|
|
var err error
|
|
|
|
|
2019-10-21 21:22:31 +00:00
|
|
|
if !rule.HasValidate() {
|
2019-09-03 22:48:13 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-11-12 05:06:09 +00:00
|
|
|
|
2021-01-07 19:24:38 +00:00
|
|
|
log = log.WithValues("rule", rule.Name)
|
|
|
|
|
2021-02-02 07:22:19 +00:00
|
|
|
if !matches(log, rule, ctx) {
|
2020-09-22 21:11:49 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-02-02 07:22:19 +00:00
|
|
|
ctx.JSONContext.Restore()
|
2021-04-29 17:09:44 +00:00
|
|
|
if err := LoadContext(log, rule.Context, ctx.ResourceCache, ctx, rule.Name); err != nil {
|
2021-07-14 21:50:28 +00:00
|
|
|
if _, ok := err.(gojmespath.NotFoundError); ok {
|
|
|
|
log.V(3).Info("failed to load context", "reason", err.Error())
|
|
|
|
} else {
|
|
|
|
log.Error(err, "failed to load context")
|
|
|
|
}
|
2019-09-03 22:48:13 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-01-07 23:13:57 +00:00
|
|
|
|
2021-01-07 19:24:38 +00:00
|
|
|
log.V(3).Info("matched validate rule")
|
|
|
|
|
2021-07-30 19:07:01 +00:00
|
|
|
ruleCopy := rule.DeepCopy()
|
|
|
|
ruleCopy.AnyAllConditions, err = variables.SubstituteAllInPreconditions(log, ctx.JSONContext, ruleCopy.AnyAllConditions)
|
2021-07-28 16:54:50 +00:00
|
|
|
if err != nil {
|
|
|
|
log.V(4).Info("failed to substitute vars in preconditions, skip current rule", "rule name", rule.Name)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-30 19:07:01 +00:00
|
|
|
preconditions, err := transformConditions(ruleCopy.AnyAllConditions)
|
2021-03-02 04:31:06 +00:00
|
|
|
if err != nil {
|
|
|
|
log.V(2).Info("wrongfully configured data", "reason", err.Error())
|
|
|
|
continue
|
|
|
|
}
|
2021-07-10 01:01:46 +00:00
|
|
|
|
2020-12-23 23:10:07 +00:00
|
|
|
// evaluate pre-conditions
|
2021-07-30 19:07:01 +00:00
|
|
|
if !variables.EvaluateConditions(log, ctx.JSONContext, preconditions) {
|
2020-03-17 18:05:20 +00:00
|
|
|
log.V(4).Info("resource fails the preconditions")
|
2020-01-07 23:13:57 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-07-28 16:54:50 +00:00
|
|
|
if rule.Validation.Pattern != nil || rule.Validation.AnyPattern != nil {
|
2021-07-30 19:07:01 +00:00
|
|
|
if *ruleCopy, err = substituteAll(log, ctx, *ruleCopy, resp); err != nil {
|
2021-07-28 16:54:50 +00:00
|
|
|
continue
|
2021-07-07 12:07:44 +00:00
|
|
|
}
|
2021-04-13 18:44:43 +00:00
|
|
|
|
2021-07-30 19:07:01 +00:00
|
|
|
ruleResponse := validateResourceWithRule(log, ctx, *ruleCopy)
|
2021-01-02 09:10:14 +00:00
|
|
|
if ruleResponse != nil {
|
|
|
|
if !common.IsConditionalAnchorError(ruleResponse.Message) {
|
|
|
|
incrementAppliedCount(resp)
|
|
|
|
resp.PolicyResponse.Rules = append(resp.PolicyResponse.Rules, *ruleResponse)
|
|
|
|
}
|
2020-12-23 23:10:07 +00:00
|
|
|
}
|
|
|
|
} else if rule.Validation.Deny != nil {
|
2021-07-30 19:07:01 +00:00
|
|
|
ruleCopy.Validation.Deny.AnyAllConditions, err = variables.SubstituteAllInPreconditions(log, ctx.JSONContext, ruleCopy.Validation.Deny.AnyAllConditions)
|
2021-07-28 16:54:50 +00:00
|
|
|
if err != nil {
|
|
|
|
log.V(4).Info("failed to substitute vars in preconditions, skip current rule", "rule name", rule.Name)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-07-30 19:07:01 +00:00
|
|
|
if *ruleCopy, err = substituteAll(log, ctx, *ruleCopy, resp); err != nil {
|
2021-07-28 16:54:50 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-07-30 19:07:01 +00:00
|
|
|
denyConditions, err := transformConditions(ruleCopy.Validation.Deny.AnyAllConditions)
|
2021-03-02 04:31:06 +00:00
|
|
|
if err != nil {
|
|
|
|
log.V(2).Info("wrongfully configured data", "reason", err.Error())
|
|
|
|
continue
|
|
|
|
}
|
2021-07-30 19:07:01 +00:00
|
|
|
|
|
|
|
deny := variables.EvaluateConditions(log, ctx.JSONContext, denyConditions)
|
2020-12-23 23:10:07 +00:00
|
|
|
ruleResp := response.RuleResponse{
|
2021-07-30 19:07:01 +00:00
|
|
|
Name: ruleCopy.Name,
|
2020-12-23 23:10:07 +00:00
|
|
|
Type: utils.Validation.String(),
|
2021-07-30 19:07:01 +00:00
|
|
|
Message: ruleCopy.Validation.Message,
|
2020-12-23 23:10:07 +00:00
|
|
|
Success: !deny,
|
2020-04-14 15:17:12 +00:00
|
|
|
}
|
|
|
|
|
2020-12-23 23:10:07 +00:00
|
|
|
incrementAppliedCount(resp)
|
|
|
|
resp.PolicyResponse.Rules = append(resp.PolicyResponse.Rules, ruleResp)
|
|
|
|
}
|
2020-05-05 13:49:47 +00:00
|
|
|
}
|
2020-12-23 23:10:07 +00:00
|
|
|
|
2020-05-05 13:49:47 +00:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2021-01-02 09:10:14 +00:00
|
|
|
func validateResourceWithRule(log logr.Logger, ctx *PolicyContext, rule kyverno.Rule) (resp *response.RuleResponse) {
|
2020-12-23 23:10:07 +00:00
|
|
|
if reflect.DeepEqual(ctx.OldResource, unstructured.Unstructured{}) {
|
2021-01-02 09:10:14 +00:00
|
|
|
resp := validatePatterns(log, ctx.JSONContext, ctx.NewResource, rule)
|
|
|
|
return &resp
|
|
|
|
}
|
|
|
|
|
|
|
|
if reflect.DeepEqual(ctx.NewResource, unstructured.Unstructured{}) {
|
|
|
|
log.V(3).Info("skipping validation on deleted resource")
|
|
|
|
return nil
|
2020-06-24 17:26:04 +00:00
|
|
|
}
|
|
|
|
|
2020-12-23 23:10:07 +00:00
|
|
|
oldResp := validatePatterns(log, ctx.JSONContext, ctx.OldResource, rule)
|
|
|
|
newResp := validatePatterns(log, ctx.JSONContext, ctx.NewResource, rule)
|
2021-01-02 09:10:14 +00:00
|
|
|
if isSameRuleResponse(oldResp, newResp) {
|
|
|
|
log.V(3).Info("skipping modified resource as validation results have not changed")
|
|
|
|
return nil
|
2020-08-08 00:09:24 +00:00
|
|
|
}
|
|
|
|
|
2021-01-02 09:10:14 +00:00
|
|
|
return &newResp
|
2020-12-23 23:10:07 +00:00
|
|
|
}
|
2020-05-05 13:49:47 +00:00
|
|
|
|
2020-12-23 23:10:07 +00:00
|
|
|
// matches checks if either the new or old resource satisfies the filter conditions defined in the rule
|
|
|
|
func matches(logger logr.Logger, rule kyverno.Rule, ctx *PolicyContext) bool {
|
2021-02-03 21:09:42 +00:00
|
|
|
err := MatchesResourceDescription(ctx.NewResource, rule, ctx.AdmissionInfo, ctx.ExcludeGroupRole, ctx.NamespaceLabels)
|
2020-12-23 23:10:07 +00:00
|
|
|
if err == nil {
|
|
|
|
return true
|
|
|
|
}
|
2020-12-09 06:52:37 +00:00
|
|
|
|
2020-12-23 23:10:07 +00:00
|
|
|
if !reflect.DeepEqual(ctx.OldResource, unstructured.Unstructured{}) {
|
2021-02-03 21:09:42 +00:00
|
|
|
err := MatchesResourceDescription(ctx.OldResource, rule, ctx.AdmissionInfo, ctx.ExcludeGroupRole, ctx.NamespaceLabels)
|
2020-12-23 23:10:07 +00:00
|
|
|
if err == nil {
|
|
|
|
return true
|
2019-09-03 22:48:13 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-16 03:56:41 +00:00
|
|
|
|
2021-01-02 09:10:14 +00:00
|
|
|
logger.V(4).Info("resource does not match rule", "reason", err.Error())
|
2020-12-23 23:10:07 +00:00
|
|
|
return false
|
2019-11-13 21:13:07 +00:00
|
|
|
}
|
|
|
|
|
2020-12-23 23:10:07 +00:00
|
|
|
func isSameRuleResponse(r1 response.RuleResponse, r2 response.RuleResponse) bool {
|
|
|
|
if r1.Name != r2.Name {
|
|
|
|
return false
|
|
|
|
}
|
2019-11-13 21:13:07 +00:00
|
|
|
|
2020-12-23 23:10:07 +00:00
|
|
|
if r1.Type != r2.Type {
|
2019-11-13 21:13:07 +00:00
|
|
|
return false
|
|
|
|
}
|
2020-12-23 23:10:07 +00:00
|
|
|
|
|
|
|
if r1.Message != r2.Message {
|
|
|
|
return false
|
2019-11-13 21:13:07 +00:00
|
|
|
}
|
2020-12-23 23:10:07 +00:00
|
|
|
|
|
|
|
if r1.Success != r2.Success {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-11-13 21:13:07 +00:00
|
|
|
return true
|
2019-09-03 22:48:13 +00:00
|
|
|
}
|
2019-08-21 19:38:15 +00:00
|
|
|
|
|
|
|
// validatePatterns validate pattern and anyPattern
|
2020-03-17 18:05:20 +00:00
|
|
|
func validatePatterns(log logr.Logger, ctx context.EvalInterface, resource unstructured.Unstructured, rule kyverno.Rule) (resp response.RuleResponse) {
|
2019-08-24 01:34:23 +00:00
|
|
|
startTime := time.Now()
|
2020-12-07 19:26:04 +00:00
|
|
|
logger := log.WithValues("rule", rule.Name, "name", resource.GetName(), "kind", resource.GetKind())
|
|
|
|
logger.V(5).Info("start processing rule", "startTime", startTime)
|
2019-12-31 01:08:50 +00:00
|
|
|
resp.Name = rule.Name
|
2020-01-08 01:06:17 +00:00
|
|
|
resp.Type = utils.Validation.String()
|
2019-08-24 01:34:23 +00:00
|
|
|
defer func() {
|
2019-12-31 01:08:50 +00:00
|
|
|
resp.RuleStats.ProcessingTime = time.Since(startTime)
|
2021-05-15 13:45:04 +00:00
|
|
|
resp.RuleStats.RuleExecutionTimestamp = startTime.Unix()
|
2020-12-06 19:12:54 +00:00
|
|
|
logger.V(4).Info("finished processing rule", "processingTime", resp.RuleStats.ProcessingTime.String())
|
2019-08-24 01:34:23 +00:00
|
|
|
}()
|
2020-05-26 17:36:56 +00:00
|
|
|
|
2020-02-14 19:59:28 +00:00
|
|
|
validationRule := rule.Validation.DeepCopy()
|
|
|
|
if validationRule.Pattern != nil {
|
|
|
|
pattern := validationRule.Pattern
|
2020-01-10 01:44:11 +00:00
|
|
|
|
2020-03-17 18:05:20 +00:00
|
|
|
if path, err := validate.ValidateResourceWithPattern(logger, resource.Object, pattern); err != nil {
|
2020-12-07 19:26:04 +00:00
|
|
|
logger.V(3).Info("validation failed", "path", path, "error", err.Error())
|
2020-02-14 19:59:28 +00:00
|
|
|
resp.Success = false
|
2020-12-07 19:26:04 +00:00
|
|
|
resp.Message = buildErrorMessage(rule, path)
|
2020-02-14 19:59:28 +00:00
|
|
|
return resp
|
|
|
|
}
|
2020-12-04 20:05:24 +00:00
|
|
|
|
2020-03-17 18:05:20 +00:00
|
|
|
logger.V(4).Info("successfully processed rule")
|
2019-12-31 01:08:50 +00:00
|
|
|
resp.Success = true
|
2020-12-07 19:26:04 +00:00
|
|
|
resp.Message = fmt.Sprintf("validation rule '%s' passed.", rule.Name)
|
2019-12-31 01:08:50 +00:00
|
|
|
return resp
|
2019-05-14 15:10:25 +00:00
|
|
|
}
|
|
|
|
|
2020-02-14 19:59:28 +00:00
|
|
|
if validationRule.AnyPattern != nil {
|
|
|
|
var failedAnyPatternsErrors []error
|
|
|
|
var err error
|
2020-11-14 00:25:51 +00:00
|
|
|
|
|
|
|
anyPatterns, err := rule.Validation.DeserializeAnyPattern()
|
|
|
|
if err != nil {
|
|
|
|
resp.Success = false
|
2020-12-07 19:26:04 +00:00
|
|
|
resp.Message = fmt.Sprintf("failed to deserialize anyPattern, expected type array: %v", err)
|
2020-11-14 00:25:51 +00:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
for idx, pattern := range anyPatterns {
|
2020-12-07 19:26:04 +00:00
|
|
|
path, err := validate.ValidateResourceWithPattern(logger, resource.Object, pattern)
|
2020-02-14 19:59:28 +00:00
|
|
|
if err == nil {
|
2019-12-31 01:08:50 +00:00
|
|
|
resp.Success = true
|
2020-12-07 19:26:04 +00:00
|
|
|
resp.Message = fmt.Sprintf("validation rule '%s' anyPattern[%d] passed.", rule.Name, idx)
|
2019-12-31 01:08:50 +00:00
|
|
|
return resp
|
2019-09-05 19:44:38 +00:00
|
|
|
}
|
2020-12-07 19:26:04 +00:00
|
|
|
|
|
|
|
logger.V(4).Info("validation rule failed", "anyPattern[%d]", idx, "path", path)
|
|
|
|
patternErr := fmt.Errorf("Rule %s[%d] failed at path %s.", rule.Name, idx, path)
|
2020-02-14 19:59:28 +00:00
|
|
|
failedAnyPatternsErrors = append(failedAnyPatternsErrors, patternErr)
|
2019-09-05 19:44:38 +00:00
|
|
|
}
|
2019-11-05 01:54:06 +00:00
|
|
|
|
2020-02-14 19:59:28 +00:00
|
|
|
// Any Pattern validation errors
|
|
|
|
if len(failedAnyPatternsErrors) > 0 {
|
|
|
|
var errorStr []string
|
|
|
|
for _, err := range failedAnyPatternsErrors {
|
|
|
|
errorStr = append(errorStr, err.Error())
|
|
|
|
}
|
2020-12-07 19:26:04 +00:00
|
|
|
|
2020-12-06 19:12:54 +00:00
|
|
|
log.V(4).Info(fmt.Sprintf("Validation rule '%s' failed. %s", rule.Name, errorStr))
|
2020-12-07 19:26:04 +00:00
|
|
|
|
|
|
|
resp.Success = false
|
|
|
|
resp.Message = buildAnyPatternErrorMessage(rule, errorStr)
|
2020-02-14 19:59:28 +00:00
|
|
|
return resp
|
2020-01-10 01:44:11 +00:00
|
|
|
}
|
2019-08-21 19:38:15 +00:00
|
|
|
}
|
2021-01-02 09:10:14 +00:00
|
|
|
|
|
|
|
return resp
|
2019-05-15 16:25:49 +00:00
|
|
|
}
|
2020-12-07 19:26:04 +00:00
|
|
|
|
|
|
|
func buildErrorMessage(rule kyverno.Rule, path string) string {
|
|
|
|
if rule.Validation.Message == "" {
|
|
|
|
return fmt.Sprintf("validation error: rule %s failed at path %s", rule.Name, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasSuffix(rule.Validation.Message, ".") {
|
|
|
|
return fmt.Sprintf("validation error: %s Rule %s failed at path %s", rule.Validation.Message, rule.Name, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("validation error: %s. Rule %s failed at path %s", rule.Validation.Message, rule.Name, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildAnyPatternErrorMessage(rule kyverno.Rule, errors []string) string {
|
|
|
|
errStr := strings.Join(errors, " ")
|
|
|
|
if rule.Validation.Message == "" {
|
|
|
|
return fmt.Sprintf("validation error: %s", errStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasSuffix(rule.Validation.Message, ".") {
|
|
|
|
return fmt.Sprintf("validation error: %s %s", rule.Validation.Message, errStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("validation error: %s. %s", rule.Validation.Message, errStr)
|
|
|
|
}
|
2021-07-28 16:54:50 +00:00
|
|
|
|
|
|
|
func substituteAll(log logr.Logger, ctx *PolicyContext, rule kyverno.Rule, resp *response.EngineResponse) (kyverno.Rule, error) {
|
|
|
|
var err error
|
|
|
|
if rule, err = variables.SubstituteAllInRule(log, ctx.JSONContext, rule); err != nil {
|
|
|
|
ruleResp := response.RuleResponse{
|
|
|
|
Name: rule.Name,
|
|
|
|
Type: utils.Validation.String(),
|
|
|
|
Message: fmt.Sprintf("variable substitution failed for rule %s: %s", rule.Name, err.Error()),
|
|
|
|
Success: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
incrementAppliedCount(resp)
|
|
|
|
resp.PolicyResponse.Rules = append(resp.PolicyResponse.Rules, ruleResp)
|
|
|
|
|
|
|
|
switch err.(type) {
|
|
|
|
case gojmespath.NotFoundError:
|
|
|
|
log.V(2).Info("failed to substitute variables, skip current rule", "info", err.Error(), "rule name", rule.Name)
|
|
|
|
default:
|
|
|
|
log.Error(err, "failed to substitute variables, skip current rule", "rule name", rule.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return rule, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return rule, nil
|
|
|
|
}
|