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"
|
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"
|
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"
|
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
|
|
|
|
Stop(context.Context)
|
2022-09-30 16:13:29 +02:00
|
|
|
// Cleanup returns the chanel used to wait for the server to clean up resources
|
|
|
|
Cleanup() <-chan struct{}
|
2022-05-16 16:36:21 +02: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
|
2022-11-17 16:17:52 +01:00
|
|
|
Mutate(context.Context, logr.Logger, *admissionv1.AdmissionRequest, time.Time) *admissionv1.AdmissionResponse
|
2022-05-13 07:33:20 +02:00
|
|
|
// Validate performs the validation check on policy resources
|
2022-11-17 16:17:52 +01:00
|
|
|
Validate(context.Context, logr.Logger, *admissionv1.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
|
2022-11-17 16:17:52 +01:00
|
|
|
Mutate(context.Context, logr.Logger, *admissionv1.AdmissionRequest, string, time.Time) *admissionv1.AdmissionResponse
|
2022-09-26 17:55:46 +02:00
|
|
|
// Validate performs the validation check on kube resources
|
2022-11-17 16:17:52 +01:00
|
|
|
Validate(context.Context, logr.Logger, *admissionv1.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
|
|
|
|
mwcClient controllerutils.DeleteClient[*admissionregistrationv1.MutatingWebhookConfiguration]
|
|
|
|
vwcClient controllerutils.DeleteClient[*admissionregistrationv1.ValidatingWebhookConfiguration]
|
|
|
|
leaseClient controllerutils.DeleteClient[*coordinationv1.Lease]
|
|
|
|
cleanUp chan struct{}
|
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-05-16 16:36:21 +02:00
|
|
|
configuration config.Configuration,
|
2022-11-09 11:52:20 +01:00
|
|
|
metricsConfig *metrics.MetricsConfig,
|
2022-10-21 17:17:49 +01:00
|
|
|
debugModeOpts DebugModeOptions,
|
2022-10-12 08:52:42 +02:00
|
|
|
tlsProvider TlsProvider,
|
|
|
|
mwcClient controllerutils.DeleteClient[*admissionregistrationv1.MutatingWebhookConfiguration],
|
|
|
|
vwcClient controllerutils.DeleteClient[*admissionregistrationv1.ValidatingWebhookConfiguration],
|
|
|
|
leaseClient controllerutils.DeleteClient[*coordinationv1.Lease],
|
|
|
|
runtime runtimeutils.Runtime,
|
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")
|
|
|
|
verifyLogger := logger.WithName("verify")
|
2022-11-09 11:52:20 +01:00
|
|
|
registerWebhookHandlers(resourceLogger.WithName("mutate"), mux, config.MutatingWebhookServicePath, configuration, metricsConfig, resourceHandlers.Mutate, debugModeOpts)
|
|
|
|
registerWebhookHandlers(resourceLogger.WithName("validate"), mux, config.ValidatingWebhookServicePath, configuration, metricsConfig, resourceHandlers.Validate, debugModeOpts)
|
|
|
|
mux.HandlerFunc(
|
|
|
|
"POST",
|
|
|
|
config.PolicyMutatingWebhookServicePath,
|
|
|
|
handlers.AdmissionHandler(policyHandlers.Mutate).
|
|
|
|
WithFilter(configuration).
|
|
|
|
WithDump(debugModeOpts.DumpPayload).
|
|
|
|
WithMetrics(metricsConfig).
|
|
|
|
WithAdmission(policyLogger.WithName("mutate")),
|
|
|
|
)
|
|
|
|
mux.HandlerFunc(
|
|
|
|
"POST",
|
|
|
|
config.PolicyValidatingWebhookServicePath,
|
|
|
|
handlers.AdmissionHandler(policyHandlers.Validate).
|
|
|
|
WithFilter(configuration).
|
|
|
|
WithDump(debugModeOpts.DumpPayload).
|
|
|
|
WithMetrics(metricsConfig).
|
|
|
|
WithAdmission(policyLogger.WithName("validate")),
|
|
|
|
)
|
|
|
|
mux.HandlerFunc(
|
|
|
|
"POST",
|
|
|
|
config.VerifyMutatingWebhookServicePath,
|
|
|
|
handlers.Verify().
|
|
|
|
WithAdmission(verifyLogger.WithName("mutate")),
|
|
|
|
)
|
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,
|
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,
|
|
|
|
cleanUp: make(chan struct{}),
|
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")
|
2021-07-09 18:01:46 -07:00
|
|
|
}
|
2021-03-23 10:34:03 -07:00
|
|
|
|
2022-05-16 16:36:21 +02:00
|
|
|
func (s *server) Stop(ctx context.Context) {
|
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-09-30 16:13:29 +02:00
|
|
|
func (s *server) Cleanup() <-chan struct{} {
|
|
|
|
return s.cleanUp
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
deleteVwc := func(name string) {
|
|
|
|
if err := s.vwcClient.Delete(ctx, name, metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) {
|
|
|
|
logger.Error(err, "failed to clean up validating webhook configuration", "name", name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
deleteMwc := func(name string) {
|
|
|
|
if err := s.mwcClient.Delete(ctx, name, metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) {
|
|
|
|
logger.Error(err, "failed to clean up mutating webhook configuration", "name", name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
deleteLease("kyvernopre-lock")
|
2022-11-03 10:19:38 +00:00
|
|
|
deleteLease("kyverno-health")
|
2022-10-12 08:52:42 +02:00
|
|
|
deleteVwc(config.ValidatingWebhookConfigurationName)
|
|
|
|
deleteVwc(config.PolicyValidatingWebhookConfigurationName)
|
|
|
|
deleteMwc(config.MutatingWebhookConfigurationName)
|
|
|
|
deleteMwc(config.PolicyMutatingWebhookConfigurationName)
|
|
|
|
deleteMwc(config.VerifyMutatingWebhookConfigurationName)
|
|
|
|
}
|
2022-07-27 16:45:06 +08:00
|
|
|
close(s.cleanUp)
|
|
|
|
}
|
|
|
|
|
2022-09-26 17:55:46 +02:00
|
|
|
func registerWebhookHandlers(
|
|
|
|
logger logr.Logger,
|
|
|
|
mux *httprouter.Router,
|
|
|
|
basePath string,
|
|
|
|
configuration config.Configuration,
|
2022-11-09 11:52:20 +01:00
|
|
|
metricsConfig *metrics.MetricsConfig,
|
2022-11-17 16:17:52 +01:00
|
|
|
handlerFunc func(context.Context, logr.Logger, *admissionv1.AdmissionRequest, string, time.Time) *admissionv1.AdmissionResponse,
|
2022-10-21 17:17:49 +01:00
|
|
|
debugModeOpts DebugModeOptions,
|
2022-09-26 17:55:46 +02:00
|
|
|
) {
|
2022-11-09 11:52:20 +01:00
|
|
|
mux.HandlerFunc(
|
|
|
|
"POST",
|
|
|
|
basePath,
|
2022-11-17 16:17:52 +01:00
|
|
|
handlers.AdmissionHandler(func(ctx context.Context, logger logr.Logger, request *admissionv1.AdmissionRequest, startTime time.Time) *admissionv1.AdmissionResponse {
|
|
|
|
return handlerFunc(ctx, logger, request, "all", startTime)
|
2022-11-09 11:52:20 +01:00
|
|
|
}).
|
|
|
|
WithFilter(configuration).
|
|
|
|
WithProtection(toggle.ProtectManagedResources.Enabled()).
|
|
|
|
WithDump(debugModeOpts.DumpPayload).
|
|
|
|
WithMetrics(metricsConfig).
|
|
|
|
WithAdmission(logger),
|
2022-09-26 17:55:46 +02:00
|
|
|
)
|
2022-11-09 11:52:20 +01:00
|
|
|
mux.HandlerFunc(
|
|
|
|
"POST",
|
|
|
|
basePath+"/fail",
|
2022-11-17 16:17:52 +01:00
|
|
|
handlers.AdmissionHandler(func(ctx context.Context, logger logr.Logger, request *admissionv1.AdmissionRequest, startTime time.Time) *admissionv1.AdmissionResponse {
|
|
|
|
return handlerFunc(ctx, logger, request, "fail", startTime)
|
2022-11-09 11:52:20 +01:00
|
|
|
}).
|
|
|
|
WithFilter(configuration).
|
|
|
|
WithProtection(toggle.ProtectManagedResources.Enabled()).
|
|
|
|
WithDump(debugModeOpts.DumpPayload).
|
|
|
|
WithMetrics(metricsConfig).
|
|
|
|
WithAdmission(logger),
|
2022-09-26 17:55:46 +02:00
|
|
|
)
|
2022-11-09 11:52:20 +01:00
|
|
|
mux.HandlerFunc(
|
|
|
|
"POST",
|
|
|
|
basePath+"/ignore",
|
2022-11-17 16:17:52 +01:00
|
|
|
handlers.AdmissionHandler(func(ctx context.Context, logger logr.Logger, request *admissionv1.AdmissionRequest, startTime time.Time) *admissionv1.AdmissionResponse {
|
|
|
|
return handlerFunc(ctx, logger, request, "ignore", startTime)
|
2022-11-09 11:52:20 +01:00
|
|
|
}).
|
|
|
|
WithFilter(configuration).
|
|
|
|
WithProtection(toggle.ProtectManagedResources.Enabled()).
|
|
|
|
WithDump(debugModeOpts.DumpPayload).
|
|
|
|
WithMetrics(metricsConfig).
|
|
|
|
WithAdmission(logger),
|
2022-09-26 17:55:46 +02:00
|
|
|
)
|
|
|
|
}
|