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-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-22 14:55:00 +00:00
|
|
|
) engineapi.EngineResponse {
|
2019-09-03 22:48:13 +00:00
|
|
|
startTime := time.Now()
|
2022-04-19 15:35:12 +00:00
|
|
|
logger.V(4).Info("start validate policy processing", "startTime", startTime)
|
2023-02-07 16:51:25 +00:00
|
|
|
policyResponse := e.validateResource(ctx, logger, policyContext)
|
2023-02-10 19:35:55 +00:00
|
|
|
defer logger.V(4).Info("finished policy processing", "processingTime", policyResponse.Stats.ProcessingTime.String(), "validationRulesApplied", policyResponse.Stats.RulesAppliedCount)
|
2023-02-10 14:04:41 +00:00
|
|
|
engineResponse := engineapi.NewEngineResponseFromPolicyContext(policyContext, nil)
|
|
|
|
engineResponse.PolicyResponse = *policyResponse
|
2023-03-23 12:58:52 +00:00
|
|
|
return *internal.BuildResponse(policyContext, &engineResponse, startTime)
|
2020-01-10 01:44:11 +00:00
|
|
|
}
|
|
|
|
|
2023-02-06 12:49:04 +00:00
|
|
|
func (e *engine) validateResource(
|
2023-01-31 14:30:40 +00:00
|
|
|
ctx context.Context,
|
2023-02-09 15:15:51 +00:00
|
|
|
logger logr.Logger,
|
2023-03-23 16:03:40 +00:00
|
|
|
policyContext engineapi.PolicyContext,
|
2023-02-07 16:51:25 +00:00
|
|
|
) *engineapi.PolicyResponse {
|
|
|
|
resp := &engineapi.PolicyResponse{}
|
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-03-23 16:03:40 +00:00
|
|
|
applyRules := policyContext.Policy().GetSpec().GetApplyRules()
|
2022-09-26 04:47:37 +00:00
|
|
|
|
2023-03-28 05:47:53 +00:00
|
|
|
for _, rule := range autogen.ComputeRules(policyContext.Policy()) {
|
|
|
|
logger := internal.LoggerWithRule(logger, rule)
|
2021-09-26 09:12:31 +00:00
|
|
|
startTime := time.Now()
|
2023-03-28 05:47:53 +00:00
|
|
|
hasValidate := rule.HasValidate()
|
|
|
|
hasValidateImage := rule.HasImagesValidationChecks()
|
|
|
|
hasYAMLSignatureVerify := rule.HasYAMLSignatureVerify()
|
|
|
|
if !hasValidate && !hasValidateImage {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var handler handlers.Handler
|
|
|
|
if hasValidate && !hasYAMLSignatureVerify {
|
|
|
|
handler = e.validateResourceHandler
|
|
|
|
} else if hasValidateImage {
|
|
|
|
handler = e.validateImageHandler
|
|
|
|
} else if hasYAMLSignatureVerify {
|
|
|
|
handler = e.validateManifestHandler
|
|
|
|
}
|
|
|
|
if handler != nil {
|
|
|
|
_, ruleResp := e.invokeRuleHandler(ctx, logger, handler, policyContext, policyContext.NewResource(), rule, engineapi.Validation)
|
|
|
|
for _, ruleResp := range ruleResp {
|
|
|
|
ruleResp := ruleResp
|
|
|
|
internal.AddRuleResponse(resp, &ruleResp, startTime)
|
|
|
|
logger.V(4).Info("finished processing rule", "processingTime", ruleResp.Stats.ProcessingTime.String())
|
|
|
|
}
|
2023-02-07 16:51:25 +00:00
|
|
|
}
|
2023-02-10 19:35:55 +00:00
|
|
|
if applyRules == kyvernov1.ApplyOne && resp.Stats.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
|
|
|
}
|
|
|
|
return resp
|
|
|
|
}
|