mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-06 16:06:56 +00:00
* add image verification * inline policy list Signed-off-by: Jim Bugwadia <jim@nirmata.com> * cosign version and dependencies updates Signed-off-by: Jim Bugwadia <jim@nirmata.com> * add registry initialization Signed-off-by: Jim Bugwadia <jim@nirmata.com> * add build tag to exclude k8schain for cloud providers Signed-off-by: Jim Bugwadia <jim@nirmata.com> * add build tag to exclude k8schain for cloud providers Signed-off-by: Jim Bugwadia <jim@nirmata.com> * generate deep copy and other fixtures Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix deep copy issues Signed-off-by: Jim Bugwadia <jim@nirmata.com> * mutate images to add digest Signed-off-by: Jim Bugwadia <jim@nirmata.com> * add certificates to Kyverno container for HTTPS lookups Signed-off-by: Jim Bugwadia <jim@nirmata.com> * align flag syntax Signed-off-by: Jim Bugwadia <jim@nirmata.com> * update docs Signed-off-by: Jim Bugwadia <jim@nirmata.com> * update dependencies Signed-off-by: Jim Bugwadia <jim@nirmata.com> * update dependencies Signed-off-by: Jim Bugwadia <jim@nirmata.com> * patch image with digest and fix checks Signed-off-by: Jim Bugwadia <jim@nirmata.com> * hardcode image for demos Signed-off-by: Jim Bugwadia <jim@nirmata.com> * add default registry (docker.io) before calling reference.Parse Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix definition Signed-off-by: Jim Bugwadia <jim@nirmata.com> * increase webhook timeout Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix args Signed-off-by: Jim Bugwadia <jim@nirmata.com> * run gofmt Signed-off-by: Jim Bugwadia <jim@nirmata.com> * rename for clarity Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix HasImageVerify check Signed-off-by: Jim Bugwadia <jim@nirmata.com> * align make test commands Signed-off-by: Jim Bugwadia <jim@nirmata.com> * align make test commands Signed-off-by: Jim Bugwadia <jim@nirmata.com> * align make test commands Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix linter error Signed-off-by: Jim Bugwadia <jim@nirmata.com> * format Signed-off-by: Jim Bugwadia <jim@nirmata.com> * handle API conflict and retry Signed-off-by: Jim Bugwadia <jim@nirmata.com> * format Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix reviewdog issues Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix make for unit tests Signed-off-by: Jim Bugwadia <jim@nirmata.com> * improve error message Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix durations Signed-off-by: Jim Bugwadia <jim@nirmata.com> * handle errors in tests Signed-off-by: Jim Bugwadia <jim@nirmata.com> * print policy name Signed-off-by: Jim Bugwadia <jim@nirmata.com> * update tests Signed-off-by: Jim Bugwadia <jim@nirmata.com> * add retries and duration to error log Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix time check in tests Signed-off-by: Jim Bugwadia <jim@nirmata.com> * round creation times in test Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix retry loop Signed-off-by: Jim Bugwadia <jim@nirmata.com> * remove timing check for policy creation Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix e2e error - policy not found Signed-off-by: Shuting Zhao <shutting06@gmail.com> * update string comparison method Signed-off-by: Shuting Zhao <shutting06@gmail.com> * fix test Generate_Namespace_Label_Actions Signed-off-by: Shuting Zhao <shutting06@gmail.com> * add debug info for e2e tests Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix error Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix generate bug Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix format Signed-off-by: Jim Bugwadia <jim@nirmata.com> * add check for update operations Signed-off-by: Jim Bugwadia <jim@nirmata.com> * increase time for deleteing a resource Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix check Signed-off-by: Jim Bugwadia <jim@nirmata.com> Co-authored-by: Shuting Zhao <shutting06@gmail.com>
151 lines
5.2 KiB
Go
151 lines
5.2 KiB
Go
package engine
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/go-logr/logr"
|
|
kyverno "github.com/kyverno/kyverno/pkg/api/kyverno/v1"
|
|
"github.com/kyverno/kyverno/pkg/engine/mutate"
|
|
"github.com/kyverno/kyverno/pkg/engine/response"
|
|
"github.com/kyverno/kyverno/pkg/engine/utils"
|
|
"github.com/kyverno/kyverno/pkg/engine/variables"
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
"sigs.k8s.io/controller-runtime/pkg/log"
|
|
)
|
|
|
|
const (
|
|
// PodControllerCronJob represent CronJob string
|
|
PodControllerCronJob = "CronJob"
|
|
//PodControllers stores the list of Pod-controllers in csv string
|
|
PodControllers = "DaemonSet,Deployment,Job,StatefulSet,CronJob"
|
|
//PodControllersAnnotation defines the annotation key for Pod-Controllers
|
|
PodControllersAnnotation = "pod-policies.kyverno.io/autogen-controllers"
|
|
)
|
|
|
|
// Mutate performs mutation. Overlay first and then mutation patches
|
|
func Mutate(policyContext *PolicyContext) (resp *response.EngineResponse) {
|
|
resp = &response.EngineResponse{}
|
|
startTime := time.Now()
|
|
policy := policyContext.Policy
|
|
patchedResource := policyContext.NewResource
|
|
ctx := policyContext.JSONContext
|
|
|
|
resCache := policyContext.ResourceCache
|
|
logger := log.Log.WithName("EngineMutate").WithValues("policy", policy.Name, "kind", patchedResource.GetKind(),
|
|
"namespace", patchedResource.GetNamespace(), "name", patchedResource.GetName())
|
|
|
|
logger.V(4).Info("start policy processing", "startTime", startTime)
|
|
|
|
startMutateResultResponse(resp, policy, patchedResource)
|
|
defer endMutateResultResponse(logger, resp, startTime)
|
|
|
|
if ManagedPodResource(policy, patchedResource) {
|
|
logger.V(5).Info("changes to pods managed by workload controllers are not permitted", "policy", policy.GetName())
|
|
resp.PatchedResource = patchedResource
|
|
return
|
|
}
|
|
|
|
policyContext.JSONContext.Checkpoint()
|
|
defer policyContext.JSONContext.Restore()
|
|
|
|
for _, rule := range policy.Spec.Rules {
|
|
if !rule.HasMutate() {
|
|
continue
|
|
}
|
|
|
|
var ruleResponse response.RuleResponse
|
|
logger := logger.WithValues("rule", rule.Name)
|
|
|
|
excludeResource := []string{}
|
|
if len(policyContext.ExcludeGroupRole) > 0 {
|
|
excludeResource = policyContext.ExcludeGroupRole
|
|
}
|
|
|
|
if err := MatchesResourceDescription(patchedResource, rule, policyContext.AdmissionInfo, excludeResource, policyContext.NamespaceLabels); err != nil {
|
|
logger.V(4).Info("rule not matched", "reason", err.Error())
|
|
continue
|
|
}
|
|
|
|
logger.V(3).Info("matched mutate rule")
|
|
|
|
policyContext.JSONContext.Restore()
|
|
if err := LoadContext(logger, rule.Context, resCache, policyContext, rule.Name); err != nil {
|
|
logger.Error(err, "failed to load context")
|
|
continue
|
|
}
|
|
|
|
// operate on the copy of the conditions, as we perform variable substitution
|
|
copyConditions, err := copyConditions(rule.AnyAllConditions)
|
|
if err != nil {
|
|
logger.V(2).Info("failed to load context", "reason", err.Error())
|
|
continue
|
|
}
|
|
// evaluate pre-conditions
|
|
// - handle variable substitutions
|
|
if !variables.EvaluateConditions(logger, ctx, copyConditions, true) {
|
|
logger.V(3).Info("resource fails the preconditions")
|
|
continue
|
|
}
|
|
|
|
if rule, err = variables.SubstituteAllInRule(logger, policyContext.JSONContext, rule); err != nil {
|
|
ruleResp := response.RuleResponse{
|
|
Name: rule.Name,
|
|
Type: utils.Validation.String(),
|
|
Message: fmt.Sprintf("variable substitution failed for rule %s: %s", rule.Name, err.Error()),
|
|
Success: true,
|
|
}
|
|
|
|
incrementAppliedCount(resp)
|
|
resp.PolicyResponse.Rules = append(resp.PolicyResponse.Rules, ruleResp)
|
|
|
|
logger.Error(err, "failed to substitute variables, skip current rule", "rule name", rule.Name)
|
|
continue
|
|
}
|
|
|
|
mutation := rule.Mutation.DeepCopy()
|
|
mutateHandler := mutate.CreateMutateHandler(rule.Name, mutation, patchedResource, ctx, logger)
|
|
ruleResponse, patchedResource = mutateHandler.Handle()
|
|
if ruleResponse.Success {
|
|
// - overlay pattern does not match the resource conditions
|
|
if ruleResponse.Patches == nil {
|
|
continue
|
|
}
|
|
|
|
logger.V(4).Info("mutate rule applied successfully", "ruleName", rule.Name)
|
|
}
|
|
|
|
resp.PolicyResponse.Rules = append(resp.PolicyResponse.Rules, ruleResponse)
|
|
incrementAppliedRuleCount(resp)
|
|
}
|
|
|
|
resp.PatchedResource = patchedResource
|
|
return resp
|
|
}
|
|
|
|
func incrementAppliedRuleCount(resp *response.EngineResponse) {
|
|
resp.PolicyResponse.RulesAppliedCount++
|
|
}
|
|
|
|
func startMutateResultResponse(resp *response.EngineResponse, policy kyverno.ClusterPolicy, resource unstructured.Unstructured) {
|
|
if resp == nil {
|
|
return
|
|
}
|
|
|
|
resp.PolicyResponse.Policy.Name = policy.GetName()
|
|
resp.PolicyResponse.Policy.Namespace = policy.GetNamespace()
|
|
resp.PolicyResponse.Resource.Name = resource.GetName()
|
|
resp.PolicyResponse.Resource.Namespace = resource.GetNamespace()
|
|
resp.PolicyResponse.Resource.Kind = resource.GetKind()
|
|
resp.PolicyResponse.Resource.APIVersion = resource.GetAPIVersion()
|
|
}
|
|
|
|
func endMutateResultResponse(logger logr.Logger, resp *response.EngineResponse, startTime time.Time) {
|
|
if resp == nil {
|
|
return
|
|
}
|
|
|
|
resp.PolicyResponse.ProcessingTime = time.Since(startTime)
|
|
resp.PolicyResponse.PolicyExecutionTimestamp = startTime.Unix()
|
|
logger.V(5).Info("finished processing policy", "processingTime", resp.PolicyResponse.ProcessingTime.String(), "mutationRulesApplied", resp.PolicyResponse.RulesAppliedCount)
|
|
}
|