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