2019-05-14 01:17:28 +00:00
|
|
|
package engine
|
|
|
|
|
2019-05-14 15:10:25 +00:00
|
|
|
import (
|
2022-12-09 13:45:11 +00:00
|
|
|
"context"
|
2019-08-19 23:40:10 +00:00
|
|
|
"time"
|
2019-05-16 16:31:02 +00:00
|
|
|
|
2022-05-17 05:56:48 +00:00
|
|
|
"github.com/go-logr/logr"
|
2022-05-17 11:12:43 +00:00
|
|
|
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
|
2022-03-28 14:01:27 +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-03-27 08:09:46 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/handlers"
|
2023-04-03 19:58:58 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/handlers/validation"
|
2023-02-07 04:30:15 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/internal"
|
2019-05-14 15:10:25 +00:00
|
|
|
)
|
|
|
|
|
2023-02-06 12:49:04 +00:00
|
|
|
func (e *engine) validate(
|
2023-01-31 14:30:40 +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-03-30 11:59:32 +00:00
|
|
|
) engineapi.PolicyResponse {
|
2023-03-31 06:41:48 +00:00
|
|
|
resp := engineapi.NewPolicyResponse()
|
2023-04-03 04:57:48 +00:00
|
|
|
policy := policyContext.Policy()
|
|
|
|
matchedResource := policyContext.NewResource()
|
|
|
|
applyRules := policy.GetSpec().GetApplyRules()
|
2020-12-23 23:10:07 +00:00
|
|
|
|
2023-03-23 16:03:40 +00:00
|
|
|
policyContext.JSONContext().Checkpoint()
|
|
|
|
defer policyContext.JSONContext().Restore()
|
2021-02-02 07:22:19 +00:00
|
|
|
|
2023-04-03 04:57:48 +00:00
|
|
|
for _, rule := range autogen.ComputeRules(policy) {
|
2021-09-26 09:12:31 +00:00
|
|
|
startTime := time.Now()
|
2023-04-03 04:57:48 +00:00
|
|
|
logger := internal.LoggerWithRule(logger, rule)
|
2023-04-03 19:58:58 +00:00
|
|
|
handlerFactory := func() (handlers.Handler, error) {
|
|
|
|
hasValidate := rule.HasValidate()
|
|
|
|
hasVerifyImageChecks := rule.HasVerifyImageChecks()
|
|
|
|
if !hasValidate && !hasVerifyImageChecks {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if hasValidate {
|
|
|
|
hasVerifyManifest := rule.HasVerifyManifests()
|
|
|
|
hasValidatePss := rule.HasValidatePodSecurity()
|
2023-05-31 21:30:55 +00:00
|
|
|
hasValidateCEL := rule.HasValidateCEL()
|
2023-04-03 19:58:58 +00:00
|
|
|
if hasVerifyManifest {
|
|
|
|
return validation.NewValidateManifestHandler(
|
|
|
|
policyContext,
|
|
|
|
e.client,
|
|
|
|
)
|
|
|
|
} else if hasValidatePss {
|
|
|
|
return validation.NewValidatePssHandler()
|
2023-05-31 21:30:55 +00:00
|
|
|
} else if hasValidateCEL {
|
|
|
|
return validation.NewValidateCELHandler(e.client)
|
2023-04-03 19:58:58 +00:00
|
|
|
} else {
|
|
|
|
return validation.NewValidateResourceHandler()
|
|
|
|
}
|
|
|
|
} else if hasVerifyImageChecks {
|
|
|
|
return validation.NewValidateImageHandler(
|
|
|
|
policyContext,
|
|
|
|
policyContext.NewResource(),
|
|
|
|
rule,
|
|
|
|
e.configuration,
|
|
|
|
)
|
2023-03-30 09:51:16 +00:00
|
|
|
}
|
2023-04-03 19:58:58 +00:00
|
|
|
return nil, nil
|
2023-03-28 05:47:53 +00:00
|
|
|
}
|
2023-04-03 04:57:48 +00:00
|
|
|
resource, ruleResp := e.invokeRuleHandler(
|
|
|
|
ctx,
|
|
|
|
logger,
|
2023-04-03 19:58:58 +00:00
|
|
|
handlerFactory,
|
2023-04-03 04:57:48 +00:00
|
|
|
policyContext,
|
|
|
|
matchedResource,
|
|
|
|
rule,
|
|
|
|
engineapi.Validation,
|
|
|
|
)
|
|
|
|
matchedResource = resource
|
2023-04-05 22:55:42 +00:00
|
|
|
resp.Add(engineapi.NewExecutionStats(startTime, time.Now()), ruleResp...)
|
2023-04-12 16:20:42 +00:00
|
|
|
if applyRules == kyvernov1.ApplyOne && resp.RulesAppliedCount() > 0 {
|
2023-02-07 16:51:25 +00:00
|
|
|
break
|
2019-09-03 22:48:13 +00:00
|
|
|
}
|
2021-09-26 09:12:31 +00:00
|
|
|
}
|
2023-03-31 06:41:48 +00:00
|
|
|
return resp
|
2021-09-26 09:12:31 +00:00
|
|
|
}
|