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"
|
|
|
|
"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"
|
2023-04-04 12:23:20 +02:00
|
|
|
"github.com/kyverno/kyverno/pkg/clients/dclient"
|
2020-10-07 11:12:31 -07:00
|
|
|
"github.com/kyverno/kyverno/pkg/config"
|
2022-10-19 15:12:55 +02:00
|
|
|
"github.com/kyverno/kyverno/pkg/logging"
|
2022-11-09 11:52:20 +01:00
|
|
|
"github.com/kyverno/kyverno/pkg/metrics"
|
2022-09-06 17:43:04 +02:00
|
|
|
"github.com/kyverno/kyverno/pkg/toggle"
|
2023-01-12 12:47:40 +08:00
|
|
|
"github.com/kyverno/kyverno/pkg/utils"
|
2022-10-12 08:52:42 +02:00
|
|
|
controllerutils "github.com/kyverno/kyverno/pkg/utils/controller"
|
|
|
|
runtimeutils "github.com/kyverno/kyverno/pkg/utils/runtime"
|
2022-03-31 17:34:10 +02:00
|
|
|
"github.com/kyverno/kyverno/pkg/webhooks/handlers"
|
2022-04-06 22:43:07 +02:00
|
|
|
admissionv1 "k8s.io/api/admission/v1"
|
2022-10-12 08:52:42 +02:00
|
|
|
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
|
|
|
|
coordinationv1 "k8s.io/api/coordination/v1"
|
|
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2023-02-15 10:37:36 +01:00
|
|
|
rbacv1listers "k8s.io/client-go/listers/rbac/v1"
|
2019-02-07 19:22:04 +02:00
|
|
|
)
|
|
|
|
|
2022-10-21 17:17:49 +01:00
|
|
|
// DebugModeOptions holds the options to configure debug mode
|
|
|
|
type DebugModeOptions struct {
|
|
|
|
// DumpPayload is used to activate/deactivate debug mode.
|
|
|
|
DumpPayload bool
|
|
|
|
}
|
|
|
|
|
2022-05-16 16:36:21 +02:00
|
|
|
type Server interface {
|
|
|
|
// Run TLS server in separate thread and returns control immediately
|
|
|
|
Run(<-chan struct{})
|
|
|
|
// Stop TLS server and returns control after the server is shut down
|
2023-01-12 12:47:40 +08:00
|
|
|
Stop()
|
2022-05-16 16:36:21 +02:00
|
|
|
}
|
|
|
|
|
2022-12-15 10:53:22 +01:00
|
|
|
type ExceptionHandlers interface {
|
|
|
|
// Validate performs the validation check on exception resources
|
2023-04-04 07:11:18 +02:00
|
|
|
Validate(context.Context, logr.Logger, handlers.AdmissionRequest, time.Time) admissionv1.AdmissionResponse
|
2022-12-15 10:53:22 +01:00
|
|
|
}
|
|
|
|
|
2022-09-26 17:55:46 +02:00
|
|
|
type PolicyHandlers interface {
|
2022-05-13 07:33:20 +02:00
|
|
|
// Mutate performs the mutation of policy resources
|
2023-04-04 07:11:18 +02:00
|
|
|
Mutate(context.Context, logr.Logger, handlers.AdmissionRequest, time.Time) admissionv1.AdmissionResponse
|
2022-05-13 07:33:20 +02:00
|
|
|
// Validate performs the validation check on policy resources
|
2023-04-04 07:11:18 +02:00
|
|
|
Validate(context.Context, logr.Logger, handlers.AdmissionRequest, time.Time) admissionv1.AdmissionResponse
|
2022-05-13 07:33:20 +02:00
|
|
|
}
|
|
|
|
|
2022-09-26 17:55:46 +02:00
|
|
|
type ResourceHandlers interface {
|
|
|
|
// Mutate performs the mutation of kube resources
|
2023-04-04 07:11:18 +02:00
|
|
|
Mutate(context.Context, logr.Logger, handlers.AdmissionRequest, string, time.Time) admissionv1.AdmissionResponse
|
2022-09-26 17:55:46 +02:00
|
|
|
// Validate performs the validation check on kube resources
|
2023-04-04 07:11:18 +02:00
|
|
|
Validate(context.Context, logr.Logger, handlers.AdmissionRequest, string, time.Time) admissionv1.AdmissionResponse
|
2022-09-26 17:55:46 +02:00
|
|
|
}
|
|
|
|
|
2022-05-16 16:36:21 +02:00
|
|
|
type server struct {
|
2022-10-12 08:52:42 +02:00
|
|
|
server *http.Server
|
|
|
|
runtime runtimeutils.Runtime
|
2023-01-12 12:47:40 +08:00
|
|
|
mwcClient controllerutils.DeleteCollectionClient[*admissionregistrationv1.MutatingWebhookConfiguration]
|
|
|
|
vwcClient controllerutils.DeleteCollectionClient[*admissionregistrationv1.ValidatingWebhookConfiguration]
|
2022-10-12 08:52:42 +02:00
|
|
|
leaseClient controllerutils.DeleteClient[*coordinationv1.Lease]
|
2019-03-04 20:40:02 +02:00
|
|
|
}
|
|
|
|
|
2022-05-16 16:36:21 +02:00
|
|
|
type TlsProvider func() ([]byte, []byte, error)
|
|
|
|
|
|
|
|
// NewServer creates new instance of server accordingly to given configuration
|
|
|
|
func NewServer(
|
2022-09-26 17:55:46 +02:00
|
|
|
policyHandlers PolicyHandlers,
|
|
|
|
resourceHandlers ResourceHandlers,
|
2022-12-15 10:53:22 +01:00
|
|
|
exceptionHandlers ExceptionHandlers,
|
2022-05-16 16:36:21 +02:00
|
|
|
configuration config.Configuration,
|
2022-11-30 14:37:53 +01:00
|
|
|
metricsConfig metrics.MetricsConfigManager,
|
2022-10-21 17:17:49 +01:00
|
|
|
debugModeOpts DebugModeOptions,
|
2022-10-12 08:52:42 +02:00
|
|
|
tlsProvider TlsProvider,
|
2023-01-12 12:47:40 +08:00
|
|
|
mwcClient controllerutils.DeleteCollectionClient[*admissionregistrationv1.MutatingWebhookConfiguration],
|
|
|
|
vwcClient controllerutils.DeleteCollectionClient[*admissionregistrationv1.ValidatingWebhookConfiguration],
|
2022-10-12 08:52:42 +02:00
|
|
|
leaseClient controllerutils.DeleteClient[*coordinationv1.Lease],
|
|
|
|
runtime runtimeutils.Runtime,
|
2023-02-15 10:37:36 +01:00
|
|
|
rbLister rbacv1listers.RoleBindingLister,
|
|
|
|
crbLister rbacv1listers.ClusterRoleBindingLister,
|
2023-04-04 12:23:20 +02:00
|
|
|
discovery dclient.IDiscovery,
|
2022-05-16 16:36:21 +02:00
|
|
|
) Server {
|
2020-04-27 18:38:03 +05:30
|
|
|
mux := httprouter.New()
|
2022-05-16 16:36:21 +02:00
|
|
|
resourceLogger := logger.WithName("resource")
|
|
|
|
policyLogger := logger.WithName("policy")
|
2022-12-15 09:34:44 +01:00
|
|
|
exceptionLogger := logger.WithName("exception")
|
2022-05-16 16:36:21 +02:00
|
|
|
verifyLogger := logger.WithName("verify")
|
2022-11-23 07:50:55 +01:00
|
|
|
registerWebhookHandlers(
|
|
|
|
mux,
|
|
|
|
"MUTATE",
|
|
|
|
config.MutatingWebhookServicePath,
|
|
|
|
resourceHandlers.Mutate,
|
2022-11-25 09:58:13 +01:00
|
|
|
func(handler handlers.AdmissionHandler) handlers.HttpHandler {
|
|
|
|
return handler.
|
|
|
|
WithFilter(configuration).
|
|
|
|
WithProtection(toggle.ProtectManagedResources.Enabled()).
|
2023-04-04 12:23:20 +02:00
|
|
|
WithDump(debugModeOpts.DumpPayload).
|
|
|
|
WithTopLevelGVK(discovery).
|
|
|
|
WithRoles(rbLister, crbLister).
|
2022-11-25 09:58:13 +01:00
|
|
|
WithOperationFilter(admissionv1.Create, admissionv1.Update, admissionv1.Connect).
|
2022-11-30 14:37:53 +01:00
|
|
|
WithMetrics(resourceLogger, metricsConfig.Config(), metrics.WebhookMutating).
|
2022-11-25 09:58:13 +01:00
|
|
|
WithAdmission(resourceLogger.WithName("mutate"))
|
|
|
|
},
|
2022-11-23 07:50:55 +01:00
|
|
|
)
|
|
|
|
registerWebhookHandlers(
|
|
|
|
mux,
|
|
|
|
"VALIDATE",
|
|
|
|
config.ValidatingWebhookServicePath,
|
|
|
|
resourceHandlers.Validate,
|
2022-11-25 09:58:13 +01:00
|
|
|
func(handler handlers.AdmissionHandler) handlers.HttpHandler {
|
|
|
|
return handler.
|
|
|
|
WithFilter(configuration).
|
|
|
|
WithProtection(toggle.ProtectManagedResources.Enabled()).
|
2023-04-04 12:23:20 +02:00
|
|
|
WithDump(debugModeOpts.DumpPayload).
|
|
|
|
WithTopLevelGVK(discovery).
|
|
|
|
WithRoles(rbLister, crbLister).
|
2022-11-30 14:37:53 +01:00
|
|
|
WithMetrics(resourceLogger, metricsConfig.Config(), metrics.WebhookValidating).
|
2022-11-25 09:58:13 +01:00
|
|
|
WithAdmission(resourceLogger.WithName("validate"))
|
|
|
|
},
|
2022-11-23 07:50:55 +01:00
|
|
|
)
|
2022-11-09 11:52:20 +01:00
|
|
|
mux.HandlerFunc(
|
|
|
|
"POST",
|
|
|
|
config.PolicyMutatingWebhookServicePath,
|
2022-11-23 07:50:55 +01:00
|
|
|
handlers.FromAdmissionFunc("MUTATE", policyHandlers.Mutate).
|
2023-04-04 12:23:20 +02:00
|
|
|
WithDump(debugModeOpts.DumpPayload).
|
2022-11-30 14:37:53 +01:00
|
|
|
WithMetrics(policyLogger, metricsConfig.Config(), metrics.WebhookMutating).
|
2022-11-23 07:50:55 +01:00
|
|
|
WithAdmission(policyLogger.WithName("mutate")).
|
|
|
|
ToHandlerFunc(),
|
2022-11-09 11:52:20 +01:00
|
|
|
)
|
|
|
|
mux.HandlerFunc(
|
|
|
|
"POST",
|
|
|
|
config.PolicyValidatingWebhookServicePath,
|
2022-11-23 07:50:55 +01:00
|
|
|
handlers.FromAdmissionFunc("VALIDATE", policyHandlers.Validate).
|
2023-04-04 12:23:20 +02:00
|
|
|
WithDump(debugModeOpts.DumpPayload).
|
2022-11-25 09:58:13 +01:00
|
|
|
WithSubResourceFilter().
|
2022-11-30 14:37:53 +01:00
|
|
|
WithMetrics(policyLogger, metricsConfig.Config(), metrics.WebhookValidating).
|
2022-11-23 07:50:55 +01:00
|
|
|
WithAdmission(policyLogger.WithName("validate")).
|
|
|
|
ToHandlerFunc(),
|
2022-11-09 11:52:20 +01:00
|
|
|
)
|
2022-12-15 09:34:44 +01:00
|
|
|
mux.HandlerFunc(
|
|
|
|
"POST",
|
|
|
|
config.ExceptionValidatingWebhookServicePath,
|
2022-12-15 10:53:22 +01:00
|
|
|
handlers.FromAdmissionFunc("VALIDATE", exceptionHandlers.Validate).
|
2023-04-04 12:23:20 +02:00
|
|
|
WithDump(debugModeOpts.DumpPayload).
|
2022-12-15 09:34:44 +01:00
|
|
|
WithSubResourceFilter().
|
|
|
|
WithMetrics(exceptionLogger, metricsConfig.Config(), metrics.WebhookValidating).
|
|
|
|
WithAdmission(exceptionLogger.WithName("validate")).
|
|
|
|
ToHandlerFunc(),
|
|
|
|
)
|
2022-11-09 11:52:20 +01:00
|
|
|
mux.HandlerFunc(
|
|
|
|
"POST",
|
|
|
|
config.VerifyMutatingWebhookServicePath,
|
2022-11-23 07:50:55 +01:00
|
|
|
handlers.FromAdmissionFunc("VERIFY", handlers.Verify).
|
|
|
|
WithAdmission(verifyLogger.WithName("mutate")).
|
|
|
|
ToHandlerFunc(),
|
2022-11-09 11:52:20 +01:00
|
|
|
)
|
2022-10-12 08:52:42 +02:00
|
|
|
mux.HandlerFunc("GET", config.LivenessServicePath, handlers.Probe(runtime.IsLive))
|
|
|
|
mux.HandlerFunc("GET", config.ReadinessServicePath, handlers.Probe(runtime.IsReady))
|
2022-05-16 16:36:21 +02:00
|
|
|
return &server{
|
|
|
|
server: &http.Server{
|
|
|
|
Addr: ":9443",
|
|
|
|
TLSConfig: &tls.Config{
|
|
|
|
GetCertificate: func(*tls.ClientHelloInfo) (*tls.Certificate, error) {
|
|
|
|
certPem, keyPem, err := tlsProvider()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pair, err := tls.X509KeyPair(certPem, keyPem)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &pair, nil
|
|
|
|
},
|
|
|
|
MinVersion: tls.VersionTLS12,
|
2023-05-26 15:15:12 +02:00
|
|
|
CipherSuites: []uint16{
|
|
|
|
// AEADs w/ ECDHE
|
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
|
|
|
|
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
|
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
|
|
|
|
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
|
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
|
|
|
|
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
|
|
|
|
},
|
2022-05-10 11:55:39 +02:00
|
|
|
},
|
2022-08-24 15:08:24 +02:00
|
|
|
Handler: mux,
|
|
|
|
ReadTimeout: 30 * time.Second,
|
|
|
|
WriteTimeout: 30 * time.Second,
|
|
|
|
ReadHeaderTimeout: 30 * time.Second,
|
2022-10-19 14:09:04 +02:00
|
|
|
IdleTimeout: 5 * time.Minute,
|
2022-10-19 15:12:55 +02:00
|
|
|
ErrorLog: logging.StdLogger(logger.WithName("server"), ""),
|
2022-05-10 11:55:39 +02:00
|
|
|
},
|
2022-10-12 08:52:42 +02:00
|
|
|
mwcClient: mwcClient,
|
|
|
|
vwcClient: vwcClient,
|
|
|
|
leaseClient: leaseClient,
|
|
|
|
runtime: runtime,
|
2019-03-04 20:40:02 +02:00
|
|
|
}
|
2019-02-21 20:31:18 +02:00
|
|
|
}
|
|
|
|
|
2022-05-16 16:36:21 +02:00
|
|
|
func (s *server) Run(stopCh <-chan struct{}) {
|
|
|
|
go func() {
|
|
|
|
logger.V(3).Info("started serving requests", "addr", s.server.Addr)
|
|
|
|
if err := s.server.ListenAndServeTLS("", ""); err != http.ErrServerClosed {
|
|
|
|
logger.Error(err, "failed to listen to requests")
|
2022-05-05 14:06:18 -07:00
|
|
|
}
|
2022-05-16 16:36:21 +02:00
|
|
|
}()
|
|
|
|
logger.Info("starting service")
|
2023-01-12 12:47:40 +08:00
|
|
|
|
|
|
|
<-stopCh
|
|
|
|
s.Stop()
|
2021-07-09 18:01:46 -07:00
|
|
|
}
|
2021-03-23 10:34:03 -07:00
|
|
|
|
2023-01-12 12:47:40 +08:00
|
|
|
func (s *server) Stop() {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2022-07-27 16:45:06 +08:00
|
|
|
s.cleanup(ctx)
|
2022-05-16 16:36:21 +02:00
|
|
|
err := s.server.Shutdown(ctx)
|
2022-05-05 14:06:18 -07:00
|
|
|
if err != nil {
|
2022-05-16 16:36:21 +02:00
|
|
|
logger.Error(err, "shutting down server")
|
|
|
|
err = s.server.Close()
|
2022-05-05 14:06:18 -07:00
|
|
|
if err != nil {
|
2022-05-16 16:36:21 +02:00
|
|
|
logger.Error(err, "server shut down failed")
|
2022-05-05 14:06:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-27 16:45:06 +08:00
|
|
|
func (s *server) cleanup(ctx context.Context) {
|
2022-10-12 08:52:42 +02:00
|
|
|
if s.runtime.IsGoingDown() {
|
|
|
|
deleteLease := func(name string) {
|
|
|
|
if err := s.leaseClient.Delete(ctx, name, metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) {
|
|
|
|
logger.Error(err, "failed to clean up lease", "name", name)
|
|
|
|
}
|
|
|
|
}
|
2023-01-12 12:47:40 +08:00
|
|
|
deleteVwc := func() {
|
|
|
|
if err := s.vwcClient.DeleteCollection(ctx, metav1.DeleteOptions{}, metav1.ListOptions{
|
|
|
|
LabelSelector: utils.ManagedByLabel,
|
|
|
|
}); err != nil && !apierrors.IsNotFound(err) {
|
|
|
|
logger.Error(err, "failed to clean up validating webhook configuration", "label", utils.ManagedByLabel)
|
2022-10-12 08:52:42 +02:00
|
|
|
}
|
|
|
|
}
|
2023-01-12 12:47:40 +08:00
|
|
|
deleteMwc := func() {
|
|
|
|
if err := s.mwcClient.DeleteCollection(ctx, metav1.DeleteOptions{}, metav1.ListOptions{
|
|
|
|
LabelSelector: utils.ManagedByLabel,
|
|
|
|
}); err != nil && !apierrors.IsNotFound(err) {
|
|
|
|
logger.Error(err, "failed to clean up mutating webhook configuration", "label", utils.ManagedByLabel)
|
2022-10-12 08:52:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
deleteLease("kyvernopre-lock")
|
2022-11-03 10:19:38 +00:00
|
|
|
deleteLease("kyverno-health")
|
2023-01-12 12:47:40 +08:00
|
|
|
deleteVwc()
|
|
|
|
deleteMwc()
|
2022-10-12 08:52:42 +02:00
|
|
|
}
|
2022-07-27 16:45:06 +08:00
|
|
|
}
|
|
|
|
|
2022-09-26 17:55:46 +02:00
|
|
|
func registerWebhookHandlers(
|
|
|
|
mux *httprouter.Router,
|
2022-11-23 07:50:55 +01:00
|
|
|
name string,
|
2022-09-26 17:55:46 +02:00
|
|
|
basePath string,
|
2023-04-04 07:11:18 +02:00
|
|
|
handlerFunc func(context.Context, logr.Logger, handlers.AdmissionRequest, string, time.Time) admissionv1.AdmissionResponse,
|
2022-11-25 09:58:13 +01:00
|
|
|
builder func(handler handlers.AdmissionHandler) handlers.HttpHandler,
|
2022-09-26 17:55:46 +02:00
|
|
|
) {
|
2022-11-25 09:58:13 +01:00
|
|
|
all := handlers.FromAdmissionFunc(
|
|
|
|
name,
|
2023-04-04 07:11:18 +02:00
|
|
|
func(ctx context.Context, logger logr.Logger, request handlers.AdmissionRequest, startTime time.Time) admissionv1.AdmissionResponse {
|
2022-11-25 09:58:13 +01:00
|
|
|
return handlerFunc(ctx, logger, request, "all", startTime)
|
|
|
|
},
|
2022-09-26 17:55:46 +02:00
|
|
|
)
|
2022-11-25 09:58:13 +01:00
|
|
|
ignore := handlers.FromAdmissionFunc(
|
|
|
|
name,
|
2023-04-04 07:11:18 +02:00
|
|
|
func(ctx context.Context, logger logr.Logger, request handlers.AdmissionRequest, startTime time.Time) admissionv1.AdmissionResponse {
|
2022-11-25 09:58:13 +01:00
|
|
|
return handlerFunc(ctx, logger, request, "ignore", startTime)
|
|
|
|
},
|
2022-09-26 17:55:46 +02:00
|
|
|
)
|
2022-11-25 09:58:13 +01:00
|
|
|
fail := handlers.FromAdmissionFunc(
|
|
|
|
name,
|
2023-04-04 07:11:18 +02:00
|
|
|
func(ctx context.Context, logger logr.Logger, request handlers.AdmissionRequest, startTime time.Time) admissionv1.AdmissionResponse {
|
2022-11-25 09:58:13 +01:00
|
|
|
return handlerFunc(ctx, logger, request, "fail", startTime)
|
|
|
|
},
|
2022-09-26 17:55:46 +02:00
|
|
|
)
|
2022-11-25 09:58:13 +01:00
|
|
|
mux.HandlerFunc("POST", basePath, builder(all).ToHandlerFunc())
|
|
|
|
mux.HandlerFunc("POST", basePath+"/ignore", builder(ignore).ToHandlerFunc())
|
|
|
|
mux.HandlerFunc("POST", basePath+"/fail", builder(fail).ToHandlerFunc())
|
2022-09-26 17:55:46 +02:00
|
|
|
}
|