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>
222 lines
6.7 KiB
Go
222 lines
6.7 KiB
Go
package webhooks
|
|
|
|
import (
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/go-logr/logr"
|
|
"github.com/kyverno/kyverno/api/kyverno/v1beta1"
|
|
"github.com/kyverno/kyverno/pkg/common"
|
|
"github.com/kyverno/kyverno/pkg/config"
|
|
client "github.com/kyverno/kyverno/pkg/dclient"
|
|
"github.com/kyverno/kyverno/pkg/engine"
|
|
"github.com/kyverno/kyverno/pkg/event"
|
|
"github.com/kyverno/kyverno/pkg/metrics"
|
|
"github.com/kyverno/kyverno/pkg/policycache"
|
|
"github.com/kyverno/kyverno/pkg/policyreport"
|
|
"github.com/kyverno/kyverno/pkg/userinfo"
|
|
"github.com/kyverno/kyverno/pkg/utils"
|
|
"github.com/pkg/errors"
|
|
admissionv1 "k8s.io/api/admission/v1"
|
|
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
|
"k8s.io/apimachinery/pkg/util/wait"
|
|
informers "k8s.io/client-go/informers/core/v1"
|
|
rbacinformer "k8s.io/client-go/informers/rbac/v1"
|
|
listerv1 "k8s.io/client-go/listers/core/v1"
|
|
rbaclister "k8s.io/client-go/listers/rbac/v1"
|
|
"k8s.io/client-go/tools/cache"
|
|
"k8s.io/client-go/util/workqueue"
|
|
)
|
|
|
|
const (
|
|
workQueueName = "validate-audit-handler"
|
|
workQueueRetryLimit = 3
|
|
)
|
|
|
|
// AuditHandler applies validate audit policies to the admission request
|
|
// the handler adds the request to the work queue and returns immediately
|
|
// the request is processed in background, with the exact same logic
|
|
// when process the admission request in the webhook
|
|
type AuditHandler interface {
|
|
Add(request *admissionv1.AdmissionRequest)
|
|
Run(workers int, stopCh <-chan struct{})
|
|
}
|
|
|
|
type auditHandler struct {
|
|
client *client.Client
|
|
queue workqueue.RateLimitingInterface
|
|
pCache policycache.Interface
|
|
eventGen event.Interface
|
|
prGenerator policyreport.GeneratorInterface
|
|
|
|
rbLister rbaclister.RoleBindingLister
|
|
rbSynced cache.InformerSynced
|
|
crbLister rbaclister.ClusterRoleBindingLister
|
|
crbSynced cache.InformerSynced
|
|
nsLister listerv1.NamespaceLister
|
|
nsListerSynced cache.InformerSynced
|
|
|
|
log logr.Logger
|
|
configHandler config.Interface
|
|
promConfig *metrics.PromConfig
|
|
}
|
|
|
|
// NewValidateAuditHandler returns a new instance of audit policy handler
|
|
func NewValidateAuditHandler(pCache policycache.Interface,
|
|
eventGen event.Interface,
|
|
prGenerator policyreport.GeneratorInterface,
|
|
rbInformer rbacinformer.RoleBindingInformer,
|
|
crbInformer rbacinformer.ClusterRoleBindingInformer,
|
|
namespaces informers.NamespaceInformer,
|
|
log logr.Logger,
|
|
dynamicConfig config.Interface,
|
|
client *client.Client,
|
|
promConfig *metrics.PromConfig) AuditHandler {
|
|
|
|
return &auditHandler{
|
|
pCache: pCache,
|
|
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), workQueueName),
|
|
eventGen: eventGen,
|
|
rbLister: rbInformer.Lister(),
|
|
rbSynced: rbInformer.Informer().HasSynced,
|
|
crbLister: crbInformer.Lister(),
|
|
crbSynced: crbInformer.Informer().HasSynced,
|
|
nsLister: namespaces.Lister(),
|
|
nsListerSynced: namespaces.Informer().HasSynced,
|
|
log: log,
|
|
prGenerator: prGenerator,
|
|
configHandler: dynamicConfig,
|
|
client: client,
|
|
promConfig: promConfig,
|
|
}
|
|
}
|
|
|
|
func (h *auditHandler) Add(request *admissionv1.AdmissionRequest) {
|
|
h.log.V(4).Info("admission request added", "uid", request.UID, "kind", request.Kind.Kind, "namespace", request.Namespace, "name", request.Name, "operation", request.Operation)
|
|
h.queue.Add(request)
|
|
}
|
|
|
|
func (h *auditHandler) Run(workers int, stopCh <-chan struct{}) {
|
|
h.log.V(4).Info("starting")
|
|
|
|
defer func() {
|
|
utilruntime.HandleCrash()
|
|
h.log.V(4).Info("shutting down")
|
|
}()
|
|
|
|
if !cache.WaitForCacheSync(stopCh, h.rbSynced, h.crbSynced) {
|
|
h.log.Info("failed to sync informer cache")
|
|
}
|
|
|
|
for i := 0; i < workers; i++ {
|
|
go wait.Until(h.runWorker, time.Second, stopCh)
|
|
}
|
|
|
|
<-stopCh
|
|
}
|
|
|
|
func (h *auditHandler) runWorker() {
|
|
for h.processNextWorkItem() {
|
|
}
|
|
}
|
|
|
|
func (h *auditHandler) processNextWorkItem() bool {
|
|
obj, shutdown := h.queue.Get()
|
|
if shutdown {
|
|
return false
|
|
}
|
|
|
|
defer h.queue.Done(obj)
|
|
|
|
request, ok := obj.(*admissionv1.AdmissionRequest)
|
|
if !ok {
|
|
h.queue.Forget(obj)
|
|
h.log.Info("incorrect type: expecting type 'AdmissionRequest'", "object", obj)
|
|
return true
|
|
}
|
|
|
|
err := h.process(request)
|
|
h.handleErr(err, obj, request)
|
|
|
|
return true
|
|
}
|
|
|
|
func (h *auditHandler) process(request *admissionv1.AdmissionRequest) error {
|
|
var roles, clusterRoles []string
|
|
var err error
|
|
// time at which the corresponding the admission request's processing got initiated
|
|
admissionRequestTimestamp := time.Now().Unix()
|
|
logger := h.log.WithName("process")
|
|
|
|
policies := h.pCache.GetPolicies(policycache.ValidateAudit, request.Kind.Kind, request.Namespace)
|
|
|
|
// getRoleRef only if policy has roles/clusterroles defined
|
|
if containsRBACInfo(policies) {
|
|
roles, clusterRoles, err = userinfo.GetRoleRef(h.rbLister, h.crbLister, request, h.configHandler)
|
|
if err != nil {
|
|
logger.Error(err, "failed to get RBAC information for request")
|
|
}
|
|
}
|
|
|
|
userRequestInfo := v1beta1.RequestInfo{
|
|
Roles: roles,
|
|
ClusterRoles: clusterRoles,
|
|
AdmissionUserInfo: request.UserInfo}
|
|
|
|
ctx, err := newVariablesContext(request, &userRequestInfo)
|
|
if err != nil {
|
|
return errors.Wrap(err, "unable to build variable context")
|
|
}
|
|
|
|
namespaceLabels := make(map[string]string)
|
|
if request.Kind.Kind != "Namespace" && request.Namespace != "" {
|
|
namespaceLabels = common.GetNamespaceSelectorsFromNamespaceLister(request.Kind.Kind, request.Namespace, h.nsLister, logger)
|
|
}
|
|
|
|
newResource, oldResource, err := utils.ExtractResources(nil, request)
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed create parse resource")
|
|
}
|
|
|
|
if err := ctx.AddImageInfos(&newResource); err != nil {
|
|
return errors.Wrap(err, "failed add image information to policy rule context\"")
|
|
}
|
|
|
|
policyContext := &engine.PolicyContext{
|
|
NewResource: newResource,
|
|
OldResource: oldResource,
|
|
AdmissionInfo: userRequestInfo,
|
|
ExcludeGroupRole: h.configHandler.GetExcludeGroupRole(),
|
|
ExcludeResourceFunc: h.configHandler.ToFilter,
|
|
JSONContext: ctx,
|
|
Client: h.client,
|
|
AdmissionOperation: true,
|
|
}
|
|
|
|
vh := &validationHandler{
|
|
log: h.log,
|
|
eventGen: h.eventGen,
|
|
prGenerator: h.prGenerator,
|
|
}
|
|
|
|
vh.handleValidation(h.promConfig, request, policies, policyContext, namespaceLabels, admissionRequestTimestamp)
|
|
return nil
|
|
}
|
|
|
|
func (h *auditHandler) handleErr(err error, key interface{}, request *admissionv1.AdmissionRequest) {
|
|
logger := h.log.WithName("handleErr")
|
|
if err == nil {
|
|
h.queue.Forget(key)
|
|
return
|
|
}
|
|
|
|
k := strings.Join([]string{request.Kind.Kind, request.Namespace, request.Name}, "/")
|
|
if h.queue.NumRequeues(key) < workQueueRetryLimit {
|
|
logger.V(3).Info("retrying processing admission request", "key", k, "error", err.Error())
|
|
h.queue.AddRateLimited(key)
|
|
return
|
|
}
|
|
|
|
logger.Error(err, "failed to process admission request", "key", k)
|
|
h.queue.Forget(key)
|
|
}
|