2023-02-02 10:58:34 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-03-27 11:22:54 +00:00
|
|
|
"fmt"
|
2023-03-30 11:59:32 +00:00
|
|
|
"time"
|
2023-02-02 10:58:34 +00:00
|
|
|
|
2023-03-27 11:22:54 +00:00
|
|
|
"github.com/go-logr/logr"
|
2023-02-08 05:55:03 +00:00
|
|
|
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
|
2023-02-07 15:09:15 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/clients/dclient"
|
2023-02-02 10:58:34 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/config"
|
|
|
|
engineapi "github.com/kyverno/kyverno/pkg/engine/api"
|
2023-02-08 05:55:03 +00:00
|
|
|
enginecontext "github.com/kyverno/kyverno/pkg/engine/context"
|
2023-03-24 16:24:00 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/handlers"
|
2023-03-27 08:09:46 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/handlers/mutation"
|
2023-03-27 13:53:42 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/handlers/validation"
|
2023-02-09 15:15:51 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/internal"
|
2023-03-27 11:22:54 +00:00
|
|
|
engineutils "github.com/kyverno/kyverno/pkg/engine/utils"
|
2023-02-09 15:15:51 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/logging"
|
2023-02-02 10:58:34 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/registryclient"
|
2023-03-27 11:22:54 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/tracing"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
2023-02-02 10:58:34 +00:00
|
|
|
)
|
|
|
|
|
2023-02-03 05:01:11 +00:00
|
|
|
type engine struct {
|
2023-03-28 05:47:53 +00:00
|
|
|
configuration config.Configuration
|
|
|
|
client dclient.Interface
|
|
|
|
rclient registryclient.Client
|
|
|
|
engineContextLoaderFactory engineapi.EngineContextLoaderFactory
|
|
|
|
exceptionSelector engineapi.PolicyExceptionSelector
|
2023-03-30 09:51:16 +00:00
|
|
|
validateResourceHandler handlers.Handler
|
2023-03-28 05:47:53 +00:00
|
|
|
validateManifestHandler handlers.Handler
|
2023-03-30 09:51:16 +00:00
|
|
|
validatePssHandler handlers.Handler
|
2023-04-03 04:57:48 +00:00
|
|
|
validateImageHandler handlers.Handler
|
2023-03-28 05:47:53 +00:00
|
|
|
mutateResourceHandler handlers.Handler
|
|
|
|
mutateExistingHandler handlers.Handler
|
2023-02-03 05:01:11 +00:00
|
|
|
}
|
2023-02-02 10:58:34 +00:00
|
|
|
|
2023-02-03 05:01:11 +00:00
|
|
|
func NewEngine(
|
|
|
|
configuration config.Configuration,
|
2023-02-07 15:09:15 +00:00
|
|
|
client dclient.Interface,
|
2023-02-08 05:55:03 +00:00
|
|
|
rclient registryclient.Client,
|
2023-02-03 05:01:11 +00:00
|
|
|
contextLoader engineapi.ContextLoaderFactory,
|
2023-02-06 05:49:47 +00:00
|
|
|
exceptionSelector engineapi.PolicyExceptionSelector,
|
2023-02-03 05:01:11 +00:00
|
|
|
) engineapi.Engine {
|
2023-03-28 05:47:53 +00:00
|
|
|
engineContextLoaderFactory := func(policy kyvernov1.PolicyInterface, rule kyvernov1.Rule) engineapi.EngineContextLoader {
|
|
|
|
loader := contextLoader(policy, rule)
|
|
|
|
return func(ctx context.Context, contextEntries []kyvernov1.ContextEntry, jsonContext enginecontext.Interface) error {
|
|
|
|
return loader.Load(
|
|
|
|
ctx,
|
|
|
|
client,
|
|
|
|
rclient,
|
|
|
|
contextEntries,
|
|
|
|
jsonContext,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &engine{
|
|
|
|
configuration: configuration,
|
|
|
|
client: client,
|
|
|
|
rclient: rclient,
|
|
|
|
engineContextLoaderFactory: engineContextLoaderFactory,
|
|
|
|
exceptionSelector: exceptionSelector,
|
2023-04-03 04:57:48 +00:00
|
|
|
validateResourceHandler: validation.NewValidateResourceHandler(),
|
2023-03-30 09:51:16 +00:00
|
|
|
validateManifestHandler: validation.NewValidateManifestHandler(client),
|
|
|
|
validatePssHandler: validation.NewValidatePssHandler(),
|
2023-04-03 04:57:48 +00:00
|
|
|
validateImageHandler: validation.NewValidateImageHandler(configuration),
|
|
|
|
mutateResourceHandler: mutation.NewMutateResourceHandler(),
|
|
|
|
mutateExistingHandler: mutation.NewMutateExistingHandler(client),
|
2023-02-03 05:01:11 +00:00
|
|
|
}
|
2023-02-02 10:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *engine) Validate(
|
|
|
|
ctx context.Context,
|
|
|
|
policyContext engineapi.PolicyContext,
|
2023-03-22 14:55:00 +00:00
|
|
|
) engineapi.EngineResponse {
|
2023-03-30 11:59:32 +00:00
|
|
|
response := engineapi.NewEngineResponseFromPolicyContext(policyContext, time.Now())
|
2023-02-09 15:15:51 +00:00
|
|
|
logger := internal.LoggerWithPolicyContext(logging.WithName("engine.validate"), policyContext)
|
2023-03-30 11:59:32 +00:00
|
|
|
if internal.MatchPolicyContext(logger, policyContext, e.configuration) {
|
|
|
|
policyResponse := e.validate(ctx, logger, policyContext)
|
|
|
|
response = response.WithPolicyResponse(policyResponse)
|
2023-03-23 16:03:40 +00:00
|
|
|
}
|
2023-03-30 11:59:32 +00:00
|
|
|
return response.Done(time.Now())
|
2023-02-02 10:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *engine) Mutate(
|
|
|
|
ctx context.Context,
|
|
|
|
policyContext engineapi.PolicyContext,
|
2023-03-22 14:55:00 +00:00
|
|
|
) engineapi.EngineResponse {
|
2023-03-30 11:59:32 +00:00
|
|
|
response := engineapi.NewEngineResponseFromPolicyContext(policyContext, time.Now())
|
2023-02-09 15:15:51 +00:00
|
|
|
logger := internal.LoggerWithPolicyContext(logging.WithName("engine.mutate"), policyContext)
|
2023-03-30 11:59:32 +00:00
|
|
|
if internal.MatchPolicyContext(logger, policyContext, e.configuration) {
|
|
|
|
policyResponse, patchedResource := e.mutate(ctx, logger, policyContext)
|
|
|
|
response = response.
|
|
|
|
WithPolicyResponse(policyResponse).
|
|
|
|
WithPatchedResource(patchedResource)
|
2023-03-23 16:03:40 +00:00
|
|
|
}
|
2023-03-30 11:59:32 +00:00
|
|
|
return response.Done(time.Now())
|
2023-02-02 10:58:34 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 04:57:48 +00:00
|
|
|
func (e *engine) Generate(
|
|
|
|
ctx context.Context,
|
|
|
|
policyContext engineapi.PolicyContext,
|
|
|
|
) engineapi.EngineResponse {
|
|
|
|
response := engineapi.NewEngineResponseFromPolicyContext(policyContext, time.Now())
|
|
|
|
logger := internal.LoggerWithPolicyContext(logging.WithName("engine.generate"), policyContext)
|
|
|
|
if internal.MatchPolicyContext(logger, policyContext, e.configuration) {
|
|
|
|
policyResponse := e.generateResponse(ctx, logger, policyContext)
|
|
|
|
response = response.WithPolicyResponse(policyResponse)
|
|
|
|
}
|
|
|
|
return response.Done(time.Now())
|
|
|
|
}
|
|
|
|
|
2023-02-02 10:58:34 +00:00
|
|
|
func (e *engine) VerifyAndPatchImages(
|
|
|
|
ctx context.Context,
|
|
|
|
policyContext engineapi.PolicyContext,
|
2023-03-23 12:58:52 +00:00
|
|
|
) (engineapi.EngineResponse, engineapi.ImageVerificationMetadata) {
|
2023-03-30 11:59:32 +00:00
|
|
|
response := engineapi.NewEngineResponseFromPolicyContext(policyContext, time.Now())
|
|
|
|
ivm := engineapi.ImageVerificationMetadata{}
|
2023-02-09 15:15:51 +00:00
|
|
|
logger := internal.LoggerWithPolicyContext(logging.WithName("engine.verify"), policyContext)
|
2023-03-30 11:59:32 +00:00
|
|
|
if internal.MatchPolicyContext(logger, policyContext, e.configuration) {
|
|
|
|
policyResponse, innerIvm := e.verifyAndPatchImages(ctx, logger, policyContext)
|
|
|
|
response, ivm = response.WithPolicyResponse(policyResponse), innerIvm
|
2023-03-23 16:03:40 +00:00
|
|
|
}
|
2023-03-30 11:59:32 +00:00
|
|
|
return response.Done(time.Now()), ivm
|
2023-02-03 05:01:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *engine) ApplyBackgroundChecks(
|
2023-02-08 05:55:03 +00:00
|
|
|
ctx context.Context,
|
2023-02-03 05:01:11 +00:00
|
|
|
policyContext engineapi.PolicyContext,
|
2023-03-22 14:55:00 +00:00
|
|
|
) engineapi.EngineResponse {
|
2023-03-30 11:59:32 +00:00
|
|
|
response := engineapi.NewEngineResponseFromPolicyContext(policyContext, time.Now())
|
2023-02-09 15:15:51 +00:00
|
|
|
logger := internal.LoggerWithPolicyContext(logging.WithName("engine.background"), policyContext)
|
2023-03-30 11:59:32 +00:00
|
|
|
if internal.MatchPolicyContext(logger, policyContext, e.configuration) {
|
|
|
|
policyResponse := e.applyBackgroundChecks(ctx, logger, policyContext)
|
|
|
|
response = response.WithPolicyResponse(policyResponse)
|
2023-03-23 16:03:40 +00:00
|
|
|
}
|
2023-03-30 11:59:32 +00:00
|
|
|
return response.Done(time.Now())
|
2023-02-03 05:01:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *engine) ContextLoader(
|
2023-02-08 05:55:03 +00:00
|
|
|
policy kyvernov1.PolicyInterface,
|
|
|
|
rule kyvernov1.Rule,
|
|
|
|
) engineapi.EngineContextLoader {
|
2023-03-28 05:47:53 +00:00
|
|
|
return e.engineContextLoaderFactory(policy, rule)
|
|
|
|
}
|
|
|
|
|
|
|
|
// matches checks if either the new or old resource satisfies the filter conditions defined in the rule
|
|
|
|
func matches(
|
|
|
|
rule kyvernov1.Rule,
|
|
|
|
policyContext engineapi.PolicyContext,
|
|
|
|
resource unstructured.Unstructured,
|
|
|
|
) error {
|
|
|
|
gvk, subresource := policyContext.ResourceKind()
|
|
|
|
err := engineutils.MatchesResourceDescription(
|
|
|
|
resource,
|
|
|
|
rule,
|
|
|
|
policyContext.AdmissionInfo(),
|
|
|
|
policyContext.NamespaceLabels(),
|
|
|
|
policyContext.Policy().GetNamespace(),
|
|
|
|
gvk,
|
|
|
|
subresource,
|
2023-03-29 04:22:21 +00:00
|
|
|
policyContext.Operation(),
|
2023-03-28 05:47:53 +00:00
|
|
|
)
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
oldResource := policyContext.OldResource()
|
|
|
|
if oldResource.Object != nil {
|
|
|
|
err := engineutils.MatchesResourceDescription(
|
|
|
|
policyContext.OldResource(),
|
|
|
|
rule,
|
|
|
|
policyContext.AdmissionInfo(),
|
|
|
|
policyContext.NamespaceLabels(),
|
|
|
|
policyContext.Policy().GetNamespace(),
|
|
|
|
gvk,
|
|
|
|
subresource,
|
2023-03-29 04:22:21 +00:00
|
|
|
policyContext.Operation(),
|
2023-02-08 05:55:03 +00:00
|
|
|
)
|
2023-03-28 05:47:53 +00:00
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2023-02-08 05:55:03 +00:00
|
|
|
}
|
2023-03-28 05:47:53 +00:00
|
|
|
return err
|
2023-02-02 10:58:34 +00:00
|
|
|
}
|
2023-03-27 11:22:54 +00:00
|
|
|
|
|
|
|
func (e *engine) invokeRuleHandler(
|
|
|
|
ctx context.Context,
|
|
|
|
logger logr.Logger,
|
2023-04-03 04:57:48 +00:00
|
|
|
handler handlers.Handler,
|
2023-03-27 11:22:54 +00:00
|
|
|
policyContext engineapi.PolicyContext,
|
|
|
|
resource unstructured.Unstructured,
|
|
|
|
rule kyvernov1.Rule,
|
2023-03-28 05:47:53 +00:00
|
|
|
ruleType engineapi.RuleType,
|
2023-03-27 11:22:54 +00:00
|
|
|
) (unstructured.Unstructured, []engineapi.RuleResponse) {
|
|
|
|
return tracing.ChildSpan2(
|
|
|
|
ctx,
|
|
|
|
"pkg/engine",
|
|
|
|
fmt.Sprintf("RULE %s", rule.Name),
|
|
|
|
func(ctx context.Context, span trace.Span) (unstructured.Unstructured, []engineapi.RuleResponse) {
|
|
|
|
// check if resource and rule match
|
2023-03-28 05:47:53 +00:00
|
|
|
if err := matches(rule, policyContext, resource); err != nil {
|
2023-03-27 11:22:54 +00:00
|
|
|
logger.V(4).Info("rule not matched", "reason", err.Error())
|
|
|
|
return resource, nil
|
|
|
|
}
|
|
|
|
// check if there's an exception
|
2023-03-28 05:47:53 +00:00
|
|
|
if ruleResp := e.hasPolicyExceptions(logger, ruleType, policyContext, rule); ruleResp != nil {
|
2023-03-27 11:22:54 +00:00
|
|
|
return resource, handlers.RuleResponses(ruleResp)
|
|
|
|
}
|
2023-04-03 04:57:48 +00:00
|
|
|
// process handler
|
|
|
|
return handler.Process(ctx, logger, policyContext, resource, rule, e.ContextLoader(policyContext.Policy(), rule))
|
2023-03-27 11:22:54 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|