2021-07-09 18:01:46 -07:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2022-12-07 16:08:37 +01:00
|
|
|
"context"
|
2021-09-29 00:49:26 +05:30
|
|
|
"time"
|
|
|
|
|
2021-07-09 18:01:46 -07:00
|
|
|
"github.com/go-logr/logr"
|
2022-05-17 13:12:43 +02:00
|
|
|
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
|
2022-04-11 10:30:38 +01:00
|
|
|
"github.com/kyverno/kyverno/pkg/autogen"
|
2023-01-30 12:41:09 +01:00
|
|
|
engineapi "github.com/kyverno/kyverno/pkg/engine/api"
|
2023-04-03 21:58:58 +02:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/handlers"
|
2023-03-31 08:41:48 +02:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/handlers/mutation"
|
2023-02-07 05:30:15 +01:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/internal"
|
2021-07-09 18:01:46 -07:00
|
|
|
)
|
|
|
|
|
2023-02-06 13:49:04 +01:00
|
|
|
func (e *engine) verifyAndPatchImages(
|
2022-12-09 14:45:11 +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, engineapi.ImageVerificationMetadata) {
|
|
|
|
resp := engineapi.NewPolicyResponse()
|
2023-04-03 06:57:48 +02:00
|
|
|
policy := policyContext.Policy()
|
|
|
|
matchedResource := policyContext.NewResource()
|
|
|
|
applyRules := policy.GetSpec().GetApplyRules()
|
|
|
|
ivm := engineapi.ImageVerificationMetadata{}
|
2021-07-09 18:01:46 -07:00
|
|
|
|
2023-01-31 16:28:48 +01:00
|
|
|
policyContext.JSONContext().Checkpoint()
|
|
|
|
defer policyContext.JSONContext().Restore()
|
2021-07-09 18:01:46 -07:00
|
|
|
|
2023-04-03 06:57:48 +02:00
|
|
|
for _, rule := range autogen.ComputeRules(policy) {
|
|
|
|
startTime := time.Now()
|
|
|
|
logger := internal.LoggerWithRule(logger, rule)
|
2023-04-03 21:58:58 +02:00
|
|
|
handlerFactory := func() (handlers.Handler, error) {
|
|
|
|
if !rule.HasVerifyImages() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return mutation.NewMutateImageHandler(
|
|
|
|
policyContext,
|
|
|
|
matchedResource,
|
|
|
|
rule,
|
|
|
|
e.configuration,
|
|
|
|
e.rclient,
|
|
|
|
&ivm,
|
|
|
|
)
|
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.ImageVerify,
|
|
|
|
)
|
|
|
|
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-04-03 06:57:48 +02:00
|
|
|
break
|
2022-07-29 00:02:26 -07:00
|
|
|
}
|
2021-07-09 18:01:46 -07:00
|
|
|
}
|
2023-04-03 21:58:58 +02:00
|
|
|
// TODO: it doesn't make sense to not return the patched resource here
|
2022-05-05 14:06:18 -07:00
|
|
|
return resp, ivm
|
2021-07-09 18:01:46 -07:00
|
|
|
}
|