mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-06 16:06:56 +00:00
* feat: mutate existing, replace GR by UR in webhook server (#3601) * add attributes for post mutation Signed-off-by: ShutingZhao <shuting@nirmata.com> * add UR informer to webhook server Signed-off-by: ShutingZhao <shuting@nirmata.com> * - replace gr with ur in the webhook server; - create ur for mutateExsiting policies Signed-off-by: ShutingZhao <shuting@nirmata.com> * replace gr by ur across entire packages Signed-off-by: ShutingZhao <shuting@nirmata.com> * add YAMLs Signed-off-by: ShutingZhao <shuting@nirmata.com> * update api docs & fix unit tests Signed-off-by: ShutingZhao <shuting@nirmata.com> * add UR deletion handler Signed-off-by: ShutingZhao <shuting@nirmata.com> * add api docs for v1beta1 Signed-off-by: ShutingZhao <shuting@nirmata.com> * fix clientset method Signed-off-by: ShutingZhao <shuting@nirmata.com> * fix v1beta1 client registration Signed-off-by: ShutingZhao <shuting@nirmata.com> * feat: mutate existing - generates UR for admission requests (#3623) Signed-off-by: ShutingZhao <shuting@nirmata.com> * replace with UR in policy controller generate rules (#3635) Signed-off-by: prateekpandey14 <prateek.pandey@nirmata.com> * - enable mutate engine to process mutateExisting rules; - add unit tests Signed-off-by: ShutingZhao <shuting@nirmata.com> * implemented ur background reconciliation for mutateExisting policies Signed-off-by: ShutingZhao <shuting@nirmata.com> * fix webhook update error Signed-off-by: ShutingZhao <shuting@nirmata.com> * temporary comment out new unit tests Signed-off-by: ShutingZhao <shuting@nirmata.com> * feat: mutate existing, replace GR by UR in webhook server (#3601) * add attributes for post mutation Signed-off-by: ShutingZhao <shuting@nirmata.com> * add UR informer to webhook server Signed-off-by: ShutingZhao <shuting@nirmata.com> * - replace gr with ur in the webhook server; - create ur for mutateExsiting policies Signed-off-by: ShutingZhao <shuting@nirmata.com> * replace gr by ur across entire packages Signed-off-by: ShutingZhao <shuting@nirmata.com> * fix missing policy.kyverno.io/policy-name label (#3599) Signed-off-by: prateekpandey14 <prateek.pandey@nirmata.com> * refactor cli code from pkg to cmd (#3591) * refactor cli code from pkg to cmd Signed-off-by: Mritunjay Sharma <mritunjaysharma394@gmail.com> * fixes in imports Signed-off-by: Mritunjay Sharma <mritunjaysharma394@gmail.com> * fixes tests Signed-off-by: Mritunjay Sharma <mritunjaysharma394@gmail.com> * fixed conflicts Signed-off-by: Mritunjay Sharma <mritunjaysharma394@gmail.com> * moved non-commands to utils Signed-off-by: Mritunjay Sharma <mritunjaysharma394@gmail.com> Co-authored-by: Vyankatesh Kudtarkar <vyankateshkd@gmail.com> * add YAMLs Signed-off-by: ShutingZhao <shuting@nirmata.com> * update api docs & fix unit tests Signed-off-by: ShutingZhao <shuting@nirmata.com> * add UR deletion handler Signed-off-by: ShutingZhao <shuting@nirmata.com> * add api docs for v1beta1 Signed-off-by: ShutingZhao <shuting@nirmata.com> * fix clientset method Signed-off-by: ShutingZhao <shuting@nirmata.com> * add-kms-libraries for cosign (#3603) * add-kms-libraries Signed-off-by: anushkamittal20 <anumittal4641@gmail.com> * Shifted providers to cosign package Signed-off-by: anushkamittal20 <anumittal4641@gmail.com> Signed-off-by: ShutingZhao <shuting@nirmata.com> * Add support for custom image extractors (#3596) Signed-off-by: Sambhav Kothari <skothari44@bloomberg.net> * Update vulnerable dependencies (#3577) Signed-off-by: Shubham Gupta <shubham.gupta2956@gmail.com> Co-authored-by: Jim Bugwadia <jim@nirmata.com> Signed-off-by: ShutingZhao <shuting@nirmata.com> * fix v1beta1 client registration Signed-off-by: ShutingZhao <shuting@nirmata.com> * feat: mutate existing - generates UR for admission requests (#3623) Signed-off-by: ShutingZhao <shuting@nirmata.com> * updating version in Chart.yaml (#3618) * updatimg version in Chart.yaml Signed-off-by: Prateeknandle <prateeknandle@gmail.com> * changes from, make gen-helm Signed-off-by: Prateeknandle <prateeknandle@gmail.com> Co-authored-by: Vyankatesh Kudtarkar <vyankateshkd@gmail.com> Signed-off-by: ShutingZhao <shuting@nirmata.com> * Allow kyverno-policies to have preconditions defined (#3606) * Allow kyverno-policies to have preconditions defined Signed-off-by: Trey Dockendorf <tdockendorf@osc.edu> * Fix docs Signed-off-by: Trey Dockendorf <tdockendorf@osc.edu> Signed-off-by: ShutingZhao <shuting@nirmata.com> * replace with UR in policy controller generate rules (#3635) Signed-off-by: prateekpandey14 <prateek.pandey@nirmata.com> Signed-off-by: ShutingZhao <shuting@nirmata.com> * - enable mutate engine to process mutateExisting rules; - add unit tests Signed-off-by: ShutingZhao <shuting@nirmata.com> * implemented ur background reconciliation for mutateExisting policies Signed-off-by: ShutingZhao <shuting@nirmata.com> * fix webhook update error Signed-off-by: ShutingZhao <shuting@nirmata.com> * temporary comment out new unit tests Signed-off-by: ShutingZhao <shuting@nirmata.com> * Image verify attestors (#3614) * fix logs Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix logs Signed-off-by: Jim Bugwadia <jim@nirmata.com> * support multiple attestors Signed-off-by: Jim Bugwadia <jim@nirmata.com> * rm CLI tests (not currently supported) Signed-off-by: Jim Bugwadia <jim@nirmata.com> * apply attestor repo Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix linter issues Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix entryError assignment Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix tests Signed-off-by: Jim Bugwadia <jim@nirmata.com> * format Signed-off-by: Jim Bugwadia <jim@nirmata.com> * add intermediary certs Signed-off-by: Jim Bugwadia <jim@nirmata.com> * Allow defining imagePullSecrets (#3633) * Allow defining imagePullSecrets Signed-off-by: Trey Dockendorf <tdockendorf@osc.edu> * Use dict for imagePullSecrets Signed-off-by: Trey Dockendorf <tdockendorf@osc.edu> * Simplify how imagePullSecrets is defined Signed-off-by: Trey Dockendorf <tdockendorf@osc.edu> Signed-off-by: ShutingZhao <shuting@nirmata.com> * Fix race condition in pCache (#3632) * fix race condition in pCache Signed-off-by: ShutingZhao <shuting@nirmata.com> * refact: remove unused Run function from generate (#3638) Signed-off-by: prateekpandey14 <prateek.pandey@nirmata.com> * Remove helm mode setting (#3628) Signed-off-by: Charles-Edouard Brétéché <charled.breteche@gmail.com> Signed-off-by: ShutingZhao <shuting@nirmata.com> * refactor: image utils (#3630) Signed-off-by: Charles-Edouard Brétéché <charled.breteche@gmail.com> Signed-off-by: ShutingZhao <shuting@nirmata.com> * -resolve lift comments; -fix informer sync issue Signed-off-by: ShutingZhao <shuting@nirmata.com> * refact the update request cleanup controller Signed-off-by: prateekpandey14 <prateek.pandey@nirmata.com> * - fix delete request for mutateExisting; - fix context variable substitution; - improve logging Signed-off-by: ShutingZhao <shuting@nirmata.com> * - enable events; - add last applied annotation Signed-off-by: ShutingZhao <shuting@nirmata.com> * enable mutate existing on policy creation Signed-off-by: ShutingZhao <shuting@nirmata.com> * update autogen code Signed-off-by: ShutingZhao <shuting@nirmata.com> * merge main Signed-off-by: ShutingZhao <shuting@nirmata.com> * add unit tests Signed-off-by: ShutingZhao <shuting@nirmata.com> * address list comments Signed-off-by: ShutingZhao <shuting@nirmata.com> * update api docs Signed-off-by: ShutingZhao <shuting@nirmata.com> * fix "Implicit memory aliasing in for loop" Signed-off-by: ShutingZhao <shuting@nirmata.com> * remove unused definitions Signed-off-by: ShutingZhao <shuting@nirmata.com> * update api docs Signed-off-by: ShutingZhao <shuting@nirmata.com> Co-authored-by: Prateek Pandey <prateek.pandey@nirmata.com> Co-authored-by: Mritunjay Kumar Sharma <mritunjaysharma394@gmail.com> Co-authored-by: Vyankatesh Kudtarkar <vyankateshkd@gmail.com> Co-authored-by: Anushka Mittal <55237170+anushkamittal20@users.noreply.github.com> Co-authored-by: Sambhav Kothari <sambhavs.email@gmail.com> Co-authored-by: Shubham Gupta <shubham.gupta2956@gmail.com> Co-authored-by: Jim Bugwadia <jim@nirmata.com> Co-authored-by: Prateek Nandle <56027872+Prateeknandle@users.noreply.github.com> Co-authored-by: treydock <tdockendorf@osc.edu> Co-authored-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>
158 lines
6 KiB
Go
158 lines
6 KiB
Go
package webhooks
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
"time"
|
|
|
|
"github.com/go-logr/logr"
|
|
kyverno "github.com/kyverno/kyverno/api/kyverno/v1"
|
|
"github.com/kyverno/kyverno/pkg/common"
|
|
"github.com/kyverno/kyverno/pkg/engine"
|
|
"github.com/kyverno/kyverno/pkg/engine/response"
|
|
"github.com/kyverno/kyverno/pkg/utils"
|
|
engineutils "github.com/kyverno/kyverno/pkg/utils/engine"
|
|
jsonutils "github.com/kyverno/kyverno/pkg/utils/json"
|
|
"github.com/pkg/errors"
|
|
admissionv1 "k8s.io/api/admission/v1"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
)
|
|
|
|
func (ws *WebhookServer) applyMutatePolicies(request *admissionv1.AdmissionRequest, policyContext *engine.PolicyContext, policies []kyverno.PolicyInterface, ts int64, logger logr.Logger) []byte {
|
|
var mutateEngineResponses []*response.EngineResponse
|
|
|
|
mutatePatches, mutateEngineResponses := ws.handleMutation(request, policyContext, policies)
|
|
logger.V(6).Info("", "generated patches", string(mutatePatches))
|
|
|
|
admissionReviewLatencyDuration := int64(time.Since(time.Unix(ts, 0)))
|
|
go ws.registerAdmissionReviewDurationMetricMutate(logger, string(request.Operation), mutateEngineResponses, admissionReviewLatencyDuration)
|
|
go ws.registerAdmissionRequestsMetricMutate(logger, string(request.Operation), mutateEngineResponses)
|
|
|
|
return mutatePatches
|
|
}
|
|
|
|
// handleMutation handles mutating webhook admission request
|
|
// return value: generated patches, triggered policies, engine responses correspdonding to the triggered policies
|
|
func (ws *WebhookServer) handleMutation(
|
|
request *admissionv1.AdmissionRequest,
|
|
policyContext *engine.PolicyContext,
|
|
policies []kyverno.PolicyInterface) ([]byte, []*response.EngineResponse) {
|
|
|
|
if len(policies) == 0 {
|
|
return nil, nil
|
|
}
|
|
|
|
resourceName := request.Kind.Kind + "/" + request.Name
|
|
if request.Namespace != "" {
|
|
resourceName = request.Namespace + "/" + resourceName
|
|
}
|
|
|
|
logger := ws.log.WithValues("action", "mutate", "resource", resourceName, "operation", request.Operation, "gvk", request.Kind.String())
|
|
|
|
patchedResource := request.Object.Raw
|
|
newR, oldR, err := utils.ExtractResources(patchedResource, request)
|
|
if err != nil {
|
|
// as resource cannot be parsed, we skip processing
|
|
logger.Error(err, "failed to extract resource")
|
|
return nil, nil
|
|
}
|
|
var deletionTimeStamp *metav1.Time
|
|
if reflect.DeepEqual(newR, unstructured.Unstructured{}) {
|
|
deletionTimeStamp = newR.GetDeletionTimestamp()
|
|
} else {
|
|
deletionTimeStamp = oldR.GetDeletionTimestamp()
|
|
}
|
|
|
|
if deletionTimeStamp != nil && request.Operation == admissionv1.Update {
|
|
return nil, nil
|
|
}
|
|
var patches [][]byte
|
|
var engineResponses []*response.EngineResponse
|
|
|
|
for _, policy := range policies {
|
|
spec := policy.GetSpec()
|
|
if !spec.HasMutate() {
|
|
continue
|
|
}
|
|
logger.V(3).Info("applying policy mutate rules", "policy", policy.GetName())
|
|
policyContext.Policy = policy
|
|
engineResponse, policyPatches, err := ws.applyMutation(request, policyContext, logger)
|
|
if err != nil {
|
|
// TODO report errors in engineResponse and record in metrics
|
|
logger.Error(err, "mutate error")
|
|
continue
|
|
}
|
|
|
|
if len(policyPatches) > 0 {
|
|
patches = append(patches, policyPatches...)
|
|
rules := engineResponse.GetSuccessRules()
|
|
if len(rules) != 0 {
|
|
logger.Info("mutation rules from policy applied successfully", "policy", policy.GetName(), "rules", rules)
|
|
}
|
|
}
|
|
|
|
policyContext.NewResource = engineResponse.PatchedResource
|
|
engineResponses = append(engineResponses, engineResponse)
|
|
|
|
// registering the kyverno_policy_results_total metric concurrently
|
|
go ws.registerPolicyResultsMetricMutation(logger, string(request.Operation), policy, *engineResponse)
|
|
|
|
// registering the kyverno_policy_execution_duration_seconds metric concurrently
|
|
go ws.registerPolicyExecutionDurationMetricMutate(logger, string(request.Operation), policy, *engineResponse)
|
|
}
|
|
|
|
// generate annotations
|
|
if annPatches := utils.GenerateAnnotationPatches(engineResponses, logger); annPatches != nil {
|
|
patches = append(patches, annPatches...)
|
|
}
|
|
|
|
// REPORTING EVENTS
|
|
// Scenario 1:
|
|
// some/all policies failed to apply on the resource. a policy violation is generated.
|
|
// create an event on the resource and the policy that failed
|
|
// Scenario 2:
|
|
// all policies were applied successfully.
|
|
// create an event on the resource
|
|
// ADD EVENTS
|
|
events := generateEvents(engineResponses, false, request.Operation == admissionv1.Update, logger)
|
|
ws.eventGen.Add(events...)
|
|
|
|
// debug info
|
|
func() {
|
|
if len(patches) != 0 {
|
|
logger.V(4).Info("JSON patches generated")
|
|
}
|
|
|
|
// if any of the policies fails, print out the error
|
|
if !engineutils.IsResponseSuccessful(engineResponses) {
|
|
logger.Error(errors.New(getErrorMsg(engineResponses)), "failed to apply mutation rules on the resource, reporting policy violation")
|
|
}
|
|
}()
|
|
|
|
// patches holds all the successful patches, if no patch is created, it returns nil
|
|
return jsonutils.JoinPatches(patches...), engineResponses
|
|
}
|
|
|
|
func (ws *WebhookServer) applyMutation(request *admissionv1.AdmissionRequest, policyContext *engine.PolicyContext, logger logr.Logger) (*response.EngineResponse, [][]byte, error) {
|
|
if request.Kind.Kind != "Namespace" && request.Namespace != "" {
|
|
policyContext.NamespaceLabels = common.GetNamespaceSelectorsFromNamespaceLister(
|
|
request.Kind.Kind, request.Namespace, ws.nsLister, logger)
|
|
}
|
|
|
|
engineResponse := engine.Mutate(policyContext)
|
|
policyPatches := engineResponse.GetPatches()
|
|
|
|
if !engineResponse.IsSuccessful() && len(engineResponse.GetFailedRules()) > 0 {
|
|
return nil, nil, fmt.Errorf("failed to apply policy %s rules %v", policyContext.Policy.GetName(), engineResponse.GetFailedRules())
|
|
}
|
|
|
|
if engineResponse.PatchedResource.GetKind() != "*" {
|
|
err := ws.openAPIController.ValidateResource(*engineResponse.PatchedResource.DeepCopy(), engineResponse.PatchedResource.GetAPIVersion(), engineResponse.PatchedResource.GetKind())
|
|
if err != nil {
|
|
return nil, nil, errors.Wrapf(err, "failed to validate resource mutated by policy %s", policyContext.Policy.GetName())
|
|
}
|
|
}
|
|
|
|
return engineResponse, policyPatches, nil
|
|
}
|