2019-05-13 21:33:01 +03:00
|
|
|
package webhooks
|
2019-02-07 19:22:04 +02:00
|
|
|
|
|
|
|
import (
|
2019-03-04 20:40:02 +02:00
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"net/http"
|
2022-03-25 10:10:34 +05:30
|
|
|
"sync"
|
2019-03-04 20:40:02 +02:00
|
|
|
"time"
|
|
|
|
|
2020-03-17 11:05:20 -07:00
|
|
|
"github.com/go-logr/logr"
|
2020-04-27 18:38:03 +05:30
|
|
|
"github.com/julienschmidt/httprouter"
|
2022-04-25 20:20:40 +08:00
|
|
|
"github.com/kyverno/kyverno/api/kyverno/v1beta1"
|
2022-04-13 18:15:04 +05:30
|
|
|
"github.com/kyverno/kyverno/pkg/background"
|
2020-10-07 11:12:31 -07:00
|
|
|
kyvernoclient "github.com/kyverno/kyverno/pkg/client/clientset/versioned"
|
|
|
|
kyvernoinformer "github.com/kyverno/kyverno/pkg/client/informers/externalversions/kyverno/v1"
|
2022-04-25 20:20:40 +08:00
|
|
|
urinformer "github.com/kyverno/kyverno/pkg/client/informers/externalversions/kyverno/v1beta1"
|
|
|
|
urlister "github.com/kyverno/kyverno/pkg/client/listers/kyverno/v1beta1"
|
2020-10-07 11:12:31 -07:00
|
|
|
"github.com/kyverno/kyverno/pkg/config"
|
|
|
|
client "github.com/kyverno/kyverno/pkg/dclient"
|
2021-07-20 21:20:37 -07:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine"
|
2020-10-07 11:12:31 -07:00
|
|
|
"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/openapi"
|
|
|
|
"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
|
|
|
tlsutils "github.com/kyverno/kyverno/pkg/tls"
|
2021-10-29 18:13:20 +02:00
|
|
|
"github.com/kyverno/kyverno/pkg/userinfo"
|
2020-10-07 11:12:31 -07:00
|
|
|
"github.com/kyverno/kyverno/pkg/utils"
|
|
|
|
"github.com/kyverno/kyverno/pkg/webhookconfig"
|
2022-03-31 17:34:10 +02:00
|
|
|
"github.com/kyverno/kyverno/pkg/webhooks/handlers"
|
2022-04-25 20:20:40 +08:00
|
|
|
webhookgenerate "github.com/kyverno/kyverno/pkg/webhooks/updaterequest"
|
2021-03-23 10:34:03 -07:00
|
|
|
"github.com/pkg/errors"
|
2022-04-06 22:43:07 +02:00
|
|
|
admissionv1 "k8s.io/api/admission/v1"
|
2021-02-04 02:39:42 +05:30
|
|
|
informers "k8s.io/client-go/informers/core/v1"
|
2020-03-29 07:25:13 +05:30
|
|
|
rbacinformer "k8s.io/client-go/informers/rbac/v1"
|
2021-02-04 02:39:42 +05:30
|
|
|
listerv1 "k8s.io/client-go/listers/core/v1"
|
2019-11-11 15:43:13 -08:00
|
|
|
rbaclister "k8s.io/client-go/listers/rbac/v1"
|
2019-02-07 19:22:04 +02:00
|
|
|
)
|
|
|
|
|
2019-03-04 20:40:02 +02:00
|
|
|
// WebhookServer contains configured TLS server with MutationWebhook.
|
2019-02-07 19:22:04 +02:00
|
|
|
type WebhookServer struct {
|
2022-05-02 22:30:07 +02:00
|
|
|
server *http.Server
|
2020-05-26 10:36:56 -07:00
|
|
|
|
2022-05-02 22:30:07 +02:00
|
|
|
// clients
|
2022-05-03 07:30:04 +02:00
|
|
|
client client.Interface
|
2022-05-02 22:30:07 +02:00
|
|
|
kyvernoClient kyvernoclient.Interface
|
2020-07-10 11:48:27 -07:00
|
|
|
|
2022-05-02 22:30:07 +02:00
|
|
|
// listers
|
|
|
|
urLister urlister.UpdateRequestNamespaceLister
|
|
|
|
rbLister rbaclister.RoleBindingLister
|
|
|
|
rLister rbaclister.RoleLister
|
|
|
|
crLister rbaclister.ClusterRoleLister
|
2020-03-29 07:25:13 +05:30
|
|
|
crbLister rbaclister.ClusterRoleBindingLister
|
2022-05-02 22:30:07 +02:00
|
|
|
nsLister listerv1.NamespaceLister
|
2020-05-26 10:36:56 -07:00
|
|
|
|
2019-11-15 15:59:37 -08:00
|
|
|
// generate events
|
2020-03-29 07:25:13 +05:30
|
|
|
eventGen event.Interface
|
2020-05-26 10:36:56 -07:00
|
|
|
|
2020-07-02 12:49:10 -07:00
|
|
|
// policy cache
|
|
|
|
pCache policycache.Interface
|
|
|
|
|
2019-11-15 15:59:37 -08:00
|
|
|
// webhook registration client
|
2020-11-26 16:07:06 -08:00
|
|
|
webhookRegister *webhookconfig.Register
|
2020-05-26 10:36:56 -07:00
|
|
|
|
2019-10-18 17:38:46 -07:00
|
|
|
// helpers to validate against current loaded configuration
|
2022-05-04 18:05:03 +02:00
|
|
|
configHandler config.Configuration
|
2020-05-26 10:36:56 -07:00
|
|
|
|
2019-10-18 17:38:46 -07:00
|
|
|
// channel for cleanup notification
|
2020-03-29 07:25:13 +05:30
|
|
|
cleanUp chan<- struct{}
|
2020-05-26 10:36:56 -07:00
|
|
|
|
2019-10-30 13:39:19 -07:00
|
|
|
// last request time
|
2020-11-26 16:07:06 -08:00
|
|
|
webhookMonitor *webhookconfig.Monitor
|
2020-05-26 10:36:56 -07:00
|
|
|
|
2020-11-09 11:26:12 -08:00
|
|
|
// policy report generator
|
|
|
|
prGenerator policyreport.GeneratorInterface
|
2020-05-26 10:36:56 -07:00
|
|
|
|
2022-04-29 19:05:49 +08:00
|
|
|
// update request generator
|
|
|
|
urGenerator webhookgenerate.Interface
|
2020-05-26 10:36:56 -07:00
|
|
|
|
2020-07-09 11:48:34 -07:00
|
|
|
auditHandler AuditHandler
|
|
|
|
|
2020-12-03 23:13:43 +05:30
|
|
|
log logr.Logger
|
2020-11-26 16:07:06 -08:00
|
|
|
|
2020-07-09 11:48:34 -07:00
|
|
|
openAPIController *openapi.Controller
|
2020-07-02 12:49:10 -07:00
|
|
|
|
2022-04-29 19:05:49 +08:00
|
|
|
urController *background.Controller
|
2021-01-20 15:25:27 -08:00
|
|
|
|
2021-05-05 00:41:13 +05:30
|
|
|
promConfig *metrics.PromConfig
|
2022-03-25 10:10:34 +05:30
|
|
|
|
2022-03-28 11:31:33 +05:30
|
|
|
mu sync.RWMutex
|
2019-03-04 20:40:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewWebhookServer creates new instance of WebhookServer accordingly to given configuration
|
|
|
|
// Policy Controller and Kubernetes Client should be initialized in configuration
|
2019-05-13 21:27:47 +03:00
|
|
|
func NewWebhookServer(
|
2022-05-02 22:30:07 +02:00
|
|
|
kyvernoClient kyvernoclient.Interface,
|
2022-05-03 07:30:04 +02:00
|
|
|
client client.Interface,
|
2020-11-17 13:07:30 -08:00
|
|
|
tlsPair *tlsutils.PemPair,
|
2022-04-25 20:20:40 +08:00
|
|
|
urInformer urinformer.UpdateRequestInformer,
|
2020-03-29 07:25:13 +05:30
|
|
|
pInformer kyvernoinformer.ClusterPolicyInformer,
|
|
|
|
rbInformer rbacinformer.RoleBindingInformer,
|
|
|
|
crbInformer rbacinformer.ClusterRoleBindingInformer,
|
2020-07-10 11:48:27 -07:00
|
|
|
rInformer rbacinformer.RoleInformer,
|
|
|
|
crInformer rbacinformer.ClusterRoleInformer,
|
2021-02-04 02:39:42 +05:30
|
|
|
namespace informers.NamespaceInformer,
|
2020-03-29 07:25:13 +05:30
|
|
|
eventGen event.Interface,
|
2020-07-02 12:49:10 -07:00
|
|
|
pCache policycache.Interface,
|
2020-11-26 16:07:06 -08:00
|
|
|
webhookRegistrationClient *webhookconfig.Register,
|
|
|
|
webhookMonitor *webhookconfig.Monitor,
|
2022-05-04 18:05:03 +02:00
|
|
|
configHandler config.Configuration,
|
2020-11-09 11:26:12 -08:00
|
|
|
prGenerator policyreport.GeneratorInterface,
|
2022-04-29 19:05:49 +08:00
|
|
|
urGenerator webhookgenerate.Interface,
|
2020-07-09 11:48:34 -07:00
|
|
|
auditHandler AuditHandler,
|
2020-03-17 11:05:20 -07:00
|
|
|
cleanUp chan<- struct{},
|
|
|
|
log logr.Logger,
|
2020-03-27 19:06:06 +05:30
|
|
|
openAPIController *openapi.Controller,
|
2022-04-29 19:05:49 +08:00
|
|
|
urc *background.Controller,
|
2021-05-05 00:41:13 +05:30
|
|
|
promConfig *metrics.PromConfig,
|
2020-03-17 11:05:20 -07:00
|
|
|
) (*WebhookServer, error) {
|
2019-05-13 21:27:47 +03:00
|
|
|
if tlsPair == nil {
|
2019-03-22 22:11:55 +02:00
|
|
|
return nil, errors.New("NewWebhookServer is not initialized properly")
|
2019-03-04 20:40:02 +02:00
|
|
|
}
|
2019-03-22 22:11:55 +02:00
|
|
|
pair, err := tls.X509KeyPair(tlsPair.Certificate, tlsPair.PrivateKey)
|
2019-03-04 20:40:02 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-29 07:25:13 +05:30
|
|
|
ws := &WebhookServer{
|
2022-03-31 17:34:10 +02:00
|
|
|
client: client,
|
|
|
|
kyvernoClient: kyvernoClient,
|
2022-04-25 20:20:40 +08:00
|
|
|
urLister: urInformer.Lister().UpdateRequests(config.KyvernoNamespace),
|
2022-03-31 17:34:10 +02:00
|
|
|
rbLister: rbInformer.Lister(),
|
|
|
|
rLister: rInformer.Lister(),
|
|
|
|
nsLister: namespace.Lister(),
|
2021-03-23 10:34:03 -07:00
|
|
|
crbLister: crbInformer.Lister(),
|
|
|
|
crLister: crInformer.Lister(),
|
|
|
|
eventGen: eventGen,
|
|
|
|
pCache: pCache,
|
|
|
|
webhookRegister: webhookRegistrationClient,
|
|
|
|
configHandler: configHandler,
|
|
|
|
cleanUp: cleanUp,
|
|
|
|
webhookMonitor: webhookMonitor,
|
|
|
|
prGenerator: prGenerator,
|
2022-04-29 19:05:49 +08:00
|
|
|
urGenerator: urGenerator,
|
|
|
|
urController: urc,
|
2021-03-23 10:34:03 -07:00
|
|
|
auditHandler: auditHandler,
|
|
|
|
log: log,
|
|
|
|
openAPIController: openAPIController,
|
2021-05-05 00:41:13 +05:30
|
|
|
promConfig: promConfig,
|
2020-03-29 07:25:13 +05:30
|
|
|
}
|
2020-04-27 18:38:03 +05:30
|
|
|
mux := httprouter.New()
|
2022-03-31 17:34:10 +02:00
|
|
|
mux.HandlerFunc("POST", config.MutatingWebhookServicePath, ws.admissionHandler(true, ws.resourceMutation))
|
|
|
|
mux.HandlerFunc("POST", config.ValidatingWebhookServicePath, ws.admissionHandler(true, ws.resourceValidation))
|
|
|
|
mux.HandlerFunc("POST", config.PolicyMutatingWebhookServicePath, ws.admissionHandler(true, ws.policyMutation))
|
|
|
|
mux.HandlerFunc("POST", config.PolicyValidatingWebhookServicePath, ws.admissionHandler(true, ws.policyValidation))
|
2022-04-25 20:20:40 +08:00
|
|
|
mux.HandlerFunc("POST", config.VerifyMutatingWebhookServicePath, ws.admissionHandler(false, handlers.Verify(ws.webhookMonitor, ws.log.WithName("verifyHandler"))))
|
2022-03-31 17:34:10 +02:00
|
|
|
mux.HandlerFunc("GET", config.LivenessServicePath, handlers.Probe(ws.webhookRegister.Check))
|
|
|
|
mux.HandlerFunc("GET", config.ReadinessServicePath, handlers.Probe(nil))
|
2020-10-22 00:41:25 -07:00
|
|
|
ws.server = &http.Server{
|
|
|
|
Addr: ":9443", // Listen on port for HTTPS requests
|
2022-03-31 19:12:38 +02:00
|
|
|
TLSConfig: &tls.Config{Certificates: []tls.Certificate{pair}, MinVersion: tls.VersionTLS12},
|
2019-03-04 20:40:02 +02:00
|
|
|
Handler: mux,
|
|
|
|
ReadTimeout: 15 * time.Second,
|
|
|
|
WriteTimeout: 15 * time.Second,
|
|
|
|
}
|
|
|
|
return ws, nil
|
2019-02-21 20:31:18 +02:00
|
|
|
}
|
|
|
|
|
2022-04-06 22:43:07 +02:00
|
|
|
func (ws *WebhookServer) buildPolicyContext(request *admissionv1.AdmissionRequest, addRoles bool) (*engine.PolicyContext, error) {
|
2022-04-25 20:20:40 +08:00
|
|
|
userRequestInfo := v1beta1.RequestInfo{
|
2021-03-23 10:34:03 -07:00
|
|
|
AdmissionUserInfo: *request.UserInfo.DeepCopy(),
|
2020-04-15 21:17:14 +05:30
|
|
|
}
|
2020-04-10 23:24:54 +05:30
|
|
|
|
2021-07-09 18:01:46 -07:00
|
|
|
if addRoles {
|
2021-10-29 16:06:03 +01:00
|
|
|
var err error
|
|
|
|
userRequestInfo.Roles, userRequestInfo.ClusterRoles, err = userinfo.GetRoleRef(ws.rbLister, ws.crbLister, request, ws.configHandler)
|
|
|
|
if err != nil {
|
2021-07-09 18:01:46 -07:00
|
|
|
return nil, errors.Wrap(err, "failed to fetch RBAC information for request")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-23 10:34:03 -07:00
|
|
|
ctx, err := newVariablesContext(request, &userRequestInfo)
|
2020-04-10 23:24:54 +05:30
|
|
|
if err != nil {
|
2021-07-09 18:01:46 -07:00
|
|
|
return nil, errors.Wrap(err, "failed to create policy rule context")
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert RAW to unstructured
|
|
|
|
resource, err := utils.ConvertResource(request.Object.Raw, request.Kind.Group, request.Kind.Version, request.Kind.Kind, request.Namespace)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to convert raw resource to unstructured format")
|
2020-04-10 23:24:54 +05:30
|
|
|
}
|
2021-03-23 10:34:03 -07:00
|
|
|
|
2022-04-09 13:52:50 +02:00
|
|
|
if err := ctx.AddImageInfos(&resource); err != nil {
|
2021-07-09 18:01:46 -07:00
|
|
|
return nil, errors.Wrap(err, "failed to add image information to the policy rule context")
|
2020-04-10 23:24:54 +05:30
|
|
|
}
|
|
|
|
|
2022-04-06 22:43:07 +02:00
|
|
|
if request.Kind.Kind == "Secret" && request.Operation == admissionv1.Update {
|
2022-02-22 17:19:03 +05:30
|
|
|
resource, err = utils.NormalizeSecret(&resource)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to convert secret to unstructured format")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-09 18:01:46 -07:00
|
|
|
policyContext := &engine.PolicyContext{
|
|
|
|
NewResource: resource,
|
|
|
|
AdmissionInfo: userRequestInfo,
|
|
|
|
ExcludeGroupRole: ws.configHandler.GetExcludeGroupRole(),
|
|
|
|
ExcludeResourceFunc: ws.configHandler.ToFilter,
|
|
|
|
JSONContext: ctx,
|
|
|
|
Client: ws.client,
|
2022-04-25 20:20:40 +08:00
|
|
|
AdmissionOperation: true,
|
2021-07-09 18:01:46 -07:00
|
|
|
}
|
2020-05-18 17:00:52 -07:00
|
|
|
|
2022-04-06 22:43:07 +02:00
|
|
|
if request.Operation == admissionv1.Update {
|
2021-07-09 18:01:46 -07:00
|
|
|
policyContext.OldResource = resource
|
|
|
|
}
|
2021-05-15 23:33:41 +05:30
|
|
|
|
2021-07-09 18:01:46 -07:00
|
|
|
return policyContext, nil
|
|
|
|
}
|
2021-03-23 10:34:03 -07:00
|
|
|
|
2019-05-14 18:10:25 +03:00
|
|
|
// RunAsync TLS server in separate thread and returns control immediately
|
2019-10-25 16:55:48 -05:00
|
|
|
func (ws *WebhookServer) RunAsync(stopCh <-chan struct{}) {
|
2020-11-09 11:26:12 -08:00
|
|
|
go func() {
|
2022-03-31 17:34:10 +02:00
|
|
|
ws.log.V(3).Info("started serving requests", "addr", ws.server.Addr)
|
2019-08-27 17:00:16 -07:00
|
|
|
if err := ws.server.ListenAndServeTLS("", ""); err != http.ErrServerClosed {
|
2022-03-31 17:34:10 +02:00
|
|
|
ws.log.Error(err, "failed to listen to requests")
|
2019-03-04 20:40:02 +02:00
|
|
|
}
|
2020-10-22 00:41:25 -07:00
|
|
|
}()
|
2022-03-31 17:34:10 +02:00
|
|
|
ws.log.Info("starting service")
|
2019-02-07 19:22:04 +02:00
|
|
|
}
|
|
|
|
|
2019-05-14 18:10:25 +03:00
|
|
|
// Stop TLS server and returns control after the server is shut down
|
2019-11-18 11:41:37 -08:00
|
|
|
func (ws *WebhookServer) Stop(ctx context.Context) {
|
2020-11-26 16:07:06 -08:00
|
|
|
// remove the static webhook configurations
|
|
|
|
go ws.webhookRegister.Remove(ws.cleanUp)
|
2019-11-18 11:41:37 -08:00
|
|
|
// shutdown http.Server with context timeout
|
|
|
|
err := ws.server.Shutdown(ctx)
|
2019-03-04 20:40:02 +02:00
|
|
|
if err != nil {
|
|
|
|
// Error from closing listeners, or context timeout:
|
2022-03-31 17:34:10 +02:00
|
|
|
ws.log.Error(err, "shutting down server")
|
2021-10-11 23:57:43 +02:00
|
|
|
err = ws.server.Close()
|
|
|
|
if err != nil {
|
2022-03-31 17:34:10 +02:00
|
|
|
ws.log.Error(err, "server shut down failed")
|
2021-10-11 23:57:43 +02:00
|
|
|
}
|
2019-03-04 20:40:02 +02:00
|
|
|
}
|
2019-02-07 19:22:04 +02:00
|
|
|
}
|