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