2020-07-09 11:48:34 -07:00
|
|
|
package webhooks
|
|
|
|
|
|
|
|
import (
|
2021-07-09 18:01:46 -07:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine"
|
|
|
|
"github.com/kyverno/kyverno/pkg/utils"
|
2021-01-07 16:26:59 -08:00
|
|
|
"strings"
|
2020-12-23 17:48:00 -08:00
|
|
|
"time"
|
|
|
|
|
2021-07-09 18:01:46 -07:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2021-02-04 02:39:42 +05:30
|
|
|
"github.com/kyverno/kyverno/pkg/common"
|
|
|
|
client "github.com/kyverno/kyverno/pkg/dclient"
|
|
|
|
|
2020-07-09 11:48:34 -07:00
|
|
|
"github.com/go-logr/logr"
|
2020-10-07 11:12:31 -07:00
|
|
|
v1 "github.com/kyverno/kyverno/pkg/api/kyverno/v1"
|
|
|
|
"github.com/kyverno/kyverno/pkg/config"
|
|
|
|
"github.com/kyverno/kyverno/pkg/event"
|
2021-05-05 00:41:13 +05:30
|
|
|
"github.com/kyverno/kyverno/pkg/metrics"
|
2020-10-07 11:12:31 -07:00
|
|
|
"github.com/kyverno/kyverno/pkg/policycache"
|
2020-11-09 11:26:12 -08:00
|
|
|
"github.com/kyverno/kyverno/pkg/policyreport"
|
2020-10-07 11:12:31 -07:00
|
|
|
"github.com/kyverno/kyverno/pkg/resourcecache"
|
|
|
|
"github.com/kyverno/kyverno/pkg/userinfo"
|
2020-07-09 11:48:34 -07:00
|
|
|
"k8s.io/api/admission/v1beta1"
|
|
|
|
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
|
|
|
"k8s.io/apimachinery/pkg/util/wait"
|
2021-02-04 02:39:42 +05:30
|
|
|
informers "k8s.io/client-go/informers/core/v1"
|
2020-07-09 11:48:34 -07:00
|
|
|
rbacinformer "k8s.io/client-go/informers/rbac/v1"
|
2021-02-04 02:39:42 +05:30
|
|
|
listerv1 "k8s.io/client-go/listers/core/v1"
|
2020-07-09 11:48:34 -07:00
|
|
|
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
|
|
|
|
)
|
|
|
|
|
2020-11-17 13:07:30 -08:00
|
|
|
// AuditHandler applies validate audit policies to the admission request
|
2020-07-09 11:48:34 -07:00
|
|
|
// 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 *v1beta1.AdmissionRequest)
|
|
|
|
Run(workers int, stopCh <-chan struct{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type auditHandler struct {
|
2021-07-14 12:18:59 -07:00
|
|
|
client *client.Client
|
|
|
|
queue workqueue.RateLimitingInterface
|
|
|
|
pCache policycache.Interface
|
|
|
|
eventGen event.Interface
|
|
|
|
prGenerator policyreport.GeneratorInterface
|
2020-07-09 11:48:34 -07:00
|
|
|
|
2021-02-04 02:39:42 +05:30
|
|
|
rbLister rbaclister.RoleBindingLister
|
|
|
|
rbSynced cache.InformerSynced
|
|
|
|
crbLister rbaclister.ClusterRoleBindingLister
|
|
|
|
crbSynced cache.InformerSynced
|
|
|
|
nsLister listerv1.NamespaceLister
|
|
|
|
nsListerSynced cache.InformerSynced
|
2020-07-09 11:48:34 -07:00
|
|
|
|
2020-08-14 12:21:06 -07:00
|
|
|
log logr.Logger
|
2020-08-07 17:09:24 -07:00
|
|
|
configHandler config.Interface
|
2021-01-29 17:38:23 -08:00
|
|
|
resCache resourcecache.ResourceCache
|
2021-05-05 00:41:13 +05:30
|
|
|
promConfig *metrics.PromConfig
|
2020-07-09 11:48:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewValidateAuditHandler returns a new instance of audit policy handler
|
|
|
|
func NewValidateAuditHandler(pCache policycache.Interface,
|
|
|
|
eventGen event.Interface,
|
2020-11-09 11:26:12 -08:00
|
|
|
prGenerator policyreport.GeneratorInterface,
|
2020-07-09 11:48:34 -07:00
|
|
|
rbInformer rbacinformer.RoleBindingInformer,
|
|
|
|
crbInformer rbacinformer.ClusterRoleBindingInformer,
|
2021-02-04 02:39:42 +05:30
|
|
|
namespaces informers.NamespaceInformer,
|
2020-08-07 17:09:24 -07:00
|
|
|
log logr.Logger,
|
2020-09-23 02:41:49 +05:30
|
|
|
dynamicConfig config.Interface,
|
2021-02-01 12:59:13 -08:00
|
|
|
resCache resourcecache.ResourceCache,
|
2021-05-05 00:41:13 +05:30
|
|
|
client *client.Client,
|
|
|
|
promConfig *metrics.PromConfig) AuditHandler {
|
2020-07-09 11:48:34 -07:00
|
|
|
|
|
|
|
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,
|
2021-02-04 02:39:42 +05:30
|
|
|
nsLister: namespaces.Lister(),
|
|
|
|
nsListerSynced: namespaces.Informer().HasSynced,
|
2020-07-09 11:48:34 -07:00
|
|
|
log: log,
|
2020-11-09 11:26:12 -08:00
|
|
|
prGenerator: prGenerator,
|
2020-08-14 12:21:06 -07:00
|
|
|
configHandler: dynamicConfig,
|
2020-09-23 02:41:49 +05:30
|
|
|
resCache: resCache,
|
2021-02-01 12:59:13 -08:00
|
|
|
client: client,
|
2021-05-05 00:41:13 +05:30
|
|
|
promConfig: promConfig,
|
2020-07-09 11:48:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *auditHandler) Add(request *v1beta1.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) {
|
2021-06-10 07:46:26 +05:30
|
|
|
h.log.Info("failed to sync informer cache")
|
2020-07-09 11:48:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < workers; i++ {
|
2020-12-23 17:48:00 -08:00
|
|
|
go wait.Until(h.runWorker, time.Second, stopCh)
|
2020-07-09 11:48:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
<-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.(*v1beta1.AdmissionRequest)
|
|
|
|
if !ok {
|
|
|
|
h.queue.Forget(obj)
|
2021-06-10 07:46:26 +05:30
|
|
|
h.log.Info("incorrect type: expecting type 'AdmissionRequest'", "object", obj)
|
2020-12-23 17:48:00 -08:00
|
|
|
return true
|
2020-07-09 11:48:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
err := h.process(request)
|
2021-01-07 16:26:59 -08:00
|
|
|
h.handleErr(err, obj, request)
|
2020-07-09 11:48:34 -07:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *auditHandler) process(request *v1beta1.AdmissionRequest) error {
|
|
|
|
var roles, clusterRoles []string
|
|
|
|
var err error
|
2021-05-15 19:15:04 +05:30
|
|
|
// time at which the corresponding the admission request's processing got initiated
|
|
|
|
admissionRequestTimestamp := time.Now().Unix()
|
2020-07-09 11:48:34 -07:00
|
|
|
logger := h.log.WithName("process")
|
2021-07-09 18:01:46 -07:00
|
|
|
|
|
|
|
policies := h.pCache.GetPolicies(policycache.ValidateAudit, request.Kind.Kind, request.Namespace)
|
|
|
|
|
2020-07-09 11:48:34 -07:00
|
|
|
// getRoleRef only if policy has roles/clusterroles defined
|
2021-07-09 18:01:46 -07:00
|
|
|
if containsRBACInfo(policies) {
|
2020-08-14 12:21:06 -07:00
|
|
|
roles, clusterRoles, err = userinfo.GetRoleRef(h.rbLister, h.crbLister, request, h.configHandler)
|
2020-07-09 11:48:34 -07:00
|
|
|
if err != nil {
|
|
|
|
logger.Error(err, "failed to get RBAC information for request")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
userRequestInfo := v1.RequestInfo{
|
|
|
|
Roles: roles,
|
|
|
|
ClusterRoles: clusterRoles,
|
|
|
|
AdmissionUserInfo: request.UserInfo}
|
|
|
|
|
2021-03-23 10:34:03 -07:00
|
|
|
ctx, err := newVariablesContext(request, &userRequestInfo)
|
2020-07-09 11:48:34 -07:00
|
|
|
if err != nil {
|
2021-07-09 18:01:46 -07:00
|
|
|
return errors.Wrap(err, "unable to build variable context")
|
2020-07-09 11:48:34 -07:00
|
|
|
}
|
|
|
|
|
2021-02-04 02:39:42 +05:30
|
|
|
namespaceLabels := make(map[string]string)
|
|
|
|
if request.Kind.Kind != "Namespace" && request.Namespace != "" {
|
|
|
|
namespaceLabels = common.GetNamespaceSelectorsFromNamespaceLister(request.Kind.Kind, request.Namespace, h.nsLister, logger)
|
|
|
|
}
|
|
|
|
|
2021-07-09 18:01:46 -07:00
|
|
|
newResource, oldResource, err := utils.ExtractResources(nil, request)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed create parse resource")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ctx.AddImageInfo(&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,
|
|
|
|
ResourceCache: h.resCache,
|
|
|
|
JSONContext: ctx,
|
|
|
|
Client: h.client,
|
|
|
|
}
|
|
|
|
|
|
|
|
vh := &validationHandler{
|
2021-07-14 12:18:59 -07:00
|
|
|
log: h.log,
|
|
|
|
eventGen: h.eventGen,
|
|
|
|
prGenerator: h.prGenerator,
|
2021-07-09 18:01:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
vh.handleValidation(h.promConfig, request, policies, policyContext, namespaceLabels, admissionRequestTimestamp)
|
2020-07-09 11:48:34 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-07 16:26:59 -08:00
|
|
|
func (h *auditHandler) handleErr(err error, key interface{}, request *v1beta1.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
|
|
|
|
}
|
2020-07-09 11:48:34 -07:00
|
|
|
|
2021-01-07 16:26:59 -08:00
|
|
|
logger.Error(err, "failed to process admission request", "key", k)
|
|
|
|
h.queue.Forget(key)
|
2020-07-09 11:48:34 -07:00
|
|
|
}
|