forked from knative/serving
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwebhook.go
More file actions
408 lines (343 loc) · 13.2 KB
/
webhook.go
File metadata and controls
408 lines (343 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
Copyright 2017 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package webhook
import (
"context"
"crypto/tls"
"errors"
"fmt"
"html"
"log"
"net"
"net/http"
"time"
// Injection stuff
"knative.dev/pkg/controller"
kubeinformerfactory "knative.dev/pkg/injection/clients/namespacedkube/informers/factory"
"knative.dev/pkg/network"
"knative.dev/pkg/network/handlers"
"knative.dev/pkg/observability/semconv"
knativetls "knative.dev/pkg/tls"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
admissionv1 "k8s.io/api/admission/v1"
"knative.dev/pkg/logging"
"knative.dev/pkg/system"
)
// Options contains the configuration for the webhook.
//
// TLS fields (TLSMinVersion, TLSMaxVersion, TLSCipherSuites, TLSCurvePreferences)
// are resolved with the following precedence:
// 1. Values set explicitly in Options (programmatic).
// 2. WEBHOOK_TLS_* environment variables (WEBHOOK_TLS_MIN_VERSION,
// WEBHOOK_TLS_MAX_VERSION, WEBHOOK_TLS_CIPHER_SUITES, WEBHOOK_TLS_CURVE_PREFERENCES).
// 3. Defaults (TLS 1.3 minimum version; zero values for the rest, meaning the
// Go standard library picks its defaults).
type Options struct {
// TLSMinVersion contains the minimum TLS version that is acceptable to communicate with the API server.
// TLS 1.3 is the minimum version if not specified otherwise.
TLSMinVersion uint16
// TLSMaxVersion contains the maximum TLS version that is acceptable.
// If not set (0), the maximum version supported by the implementation will be used.
// This is useful for enforcing Modern profile (TLS 1.3 only) by setting both
// TLSMinVersion and TLSMaxVersion to tls.VersionTLS13.
TLSMaxVersion uint16
// TLSCipherSuites specifies the list of enabled cipher suites.
// If empty, a default list of secure cipher suites will be used.
// Note: Cipher suites are not configurable in TLS 1.3; they are determined by the implementation.
TLSCipherSuites []uint16
// TLSCurvePreferences specifies the elliptic curves that will be used in an ECDHE handshake.
// If empty, the default curves will be used.
TLSCurvePreferences []tls.CurveID
// ServiceName is the service name of the webhook.
ServiceName string
// SecretName is the name of k8s secret that contains the webhook
// server key/cert and corresponding CA cert that signed them. The
// server key/cert are used to serve the webhook and the CA cert
// is provided to k8s apiserver during admission controller
// registration.
// If no SecretName is provided, then the webhook serves without TLS.
SecretName string
// ServerPrivateKeyName is the name for the webhook secret's data key e.g. `tls.key`.
// Default value is `server-key.pem` if no value is passed.
ServerPrivateKeyName string
// ServerCertificateName is the name for the webhook secret's ca data key e.g. `tls.crt`.
// Default value is `server-cert.pem` if no value is passed.
ServerCertificateName string
// Port where the webhook is served. Per k8s admission
// registration requirements this should be 443 unless there is
// only a single port for the service.
Port int
// GracePeriod is how long to wait after failing readiness probes
// before shutting down.
GracePeriod time.Duration
// DisableNamespaceOwnership configures if the SYSTEM_NAMESPACE is added as an owner reference to the
// webhook configuration resources. Overridden by the WEBHOOK_DISABLE_NAMESPACE_OWNERSHIP environment variable.
// Disabling can be useful to avoid breaking systems that expect ownership to indicate a true controller
// relationship: https://github.com/knative/serving/issues/15483
DisableNamespaceOwnership bool
// ControllerOptions encapsulates options for creating a new controller,
// including throttling and stats behavior.
ControllerOptions *controller.ControllerOptions
// EnableHTTP2 enables HTTP2 for webhooks.
// Mitigate CVE-2023-44487 by disabling HTTP2 by default until the Go
// standard library and golang.org/x/net are fully fixed.
// Right now, it is possible for authenticated and unauthenticated users to
// hold open HTTP2 connections and consume huge amounts of memory.
// See:
// * https://github.com/kubernetes/kubernetes/pull/121120
// * https://github.com/kubernetes/kubernetes/issues/121197
// * https://github.com/golang/go/issues/63417#issuecomment-1758858612
EnableHTTP2 bool
// MeterProvider is used to configure the MeterProvider used by the webhook
// If nil it will use the global meter provider
MeterProvider metric.MeterProvider
// TracerProvider is used to config the TracerProvider used by the webhook
// if nil it will use the global tracer provider
TracerProvider trace.TracerProvider
// TextMapPropagator is used to configure the TextMapPropagator used by the webhook
// if nil it will use the global text map propagator
TextMapPropagator propagation.TextMapPropagator
}
// Operation is the verb being operated on
// it is aliased in Validation from the k8s admission package
type Operation = admissionv1.Operation
// Operation types
const (
Create Operation = admissionv1.Create
Update Operation = admissionv1.Update
Delete Operation = admissionv1.Delete
Connect Operation = admissionv1.Connect
)
// Webhook implements the external webhook for validation of
// resources and configuration.
type Webhook struct {
Options Options
Logger *zap.SugaredLogger
// synced is function that is called when the informers have been synced.
synced context.CancelFunc
mux http.ServeMux
// The TLS configuration to use for serving (or nil for non-TLS)
tlsConfig *tls.Config
// testListener is only used in testing so we don't get port conflicts
testListener net.Listener
metrics *metrics
}
// New constructs a Webhook
func New(
ctx context.Context,
controllers []interface{},
) (webhook *Webhook, err error) {
// ServeMux.Handle panics on duplicate paths
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("error creating webhook %v", r)
}
}()
opts := GetOptions(ctx)
if opts == nil {
return nil, errors.New("context must have Options specified")
}
logger := logging.FromContext(ctx)
tlsCfg, err := knativetls.DefaultConfigFromEnv("WEBHOOK_")
if err != nil {
return nil, fmt.Errorf("reading TLS configuration from environment: %w", err)
}
if opts.TLSMinVersion != 0 {
tlsCfg.MinVersion = opts.TLSMinVersion
}
if opts.TLSMaxVersion != 0 {
tlsCfg.MaxVersion = opts.TLSMaxVersion
}
if opts.TLSCipherSuites != nil {
tlsCfg.CipherSuites = opts.TLSCipherSuites
}
if opts.TLSCurvePreferences != nil {
tlsCfg.CurvePreferences = opts.TLSCurvePreferences
}
if tlsCfg.MinVersion != tls.VersionTLS12 && tlsCfg.MinVersion != tls.VersionTLS13 {
return nil, fmt.Errorf("unsupported TLS minimum version %d: must be TLS 1.2 or TLS 1.3", tlsCfg.MinVersion)
}
if tlsCfg.MaxVersion != 0 && tlsCfg.MinVersion > tlsCfg.MaxVersion {
return nil, fmt.Errorf("TLS minimum version (%#x) is greater than maximum version (%#x)", tlsCfg.MinVersion, tlsCfg.MaxVersion)
}
syncCtx, cancel := context.WithCancel(context.Background())
webhook = &Webhook{
Options: *opts,
Logger: logger,
synced: cancel,
metrics: newMetrics(*opts),
}
if opts.SecretName != "" {
// Injection is too aggressive for this case because by simply linking this
// library we force consumers to have secret access. If we require that one
// of the admission controllers' informers *also* require the secret
// informer, then we can fetch the shared informer factory here and produce
// a new secret informer from it.
secretInformer := kubeinformerfactory.Get(ctx).Core().V1().Secrets()
// If we return (nil, error) the client sees - 'tls: internal error'
// If we return (nil, nil) the client sees - 'tls: no certificates configured'
//
// We'll return (nil, nil) when we don't find a certificate
tlsCfg.GetCertificate = func(*tls.ClientHelloInfo) (*tls.Certificate, error) {
secret, err := secretInformer.Lister().Secrets(system.Namespace()).Get(opts.SecretName)
if err != nil {
logger.Errorw("failed to fetch secret", zap.Error(err))
return nil, nil
}
webOpts := GetOptions(ctx)
sKey, sCert := getSecretDataKeyNamesOrDefault(webOpts.ServerPrivateKeyName, webOpts.ServerCertificateName)
serverKey, ok := secret.Data[sKey]
if !ok {
logger.Warn("server key missing")
return nil, nil
}
serverCert, ok := secret.Data[sCert]
if !ok {
logger.Warn("server cert missing")
return nil, nil
}
cert, err := tls.X509KeyPair(serverCert, serverKey)
if err != nil {
return nil, err
}
return &cert, nil
}
webhook.tlsConfig = tlsCfg
}
webhook.mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Error(w, fmt.Sprint("no controller registered for: ", html.EscapeString(r.URL.Path)), http.StatusBadRequest)
})
for _, controller := range controllers {
switch c := controller.(type) {
case AdmissionController:
handler := admissionHandler(webhook, c, syncCtx.Done())
webhook.mux.Handle(c.Path(), handler)
case ConversionController:
handler := conversionHandler(webhook, c)
webhook.mux.Handle(c.Path(), handler)
default:
return nil, fmt.Errorf("unknown webhook controller type: %T", controller)
}
}
return webhook, err
}
// InformersHaveSynced is called when the informers have all been synced, which allows any outstanding
// admission webhooks through.
func (wh *Webhook) InformersHaveSynced() {
wh.synced()
wh.Logger.Info("Informers have been synced, unblocking admission webhooks.")
}
type zapWrapper struct {
logger *zap.SugaredLogger
}
func (z *zapWrapper) Write(p []byte) (n int, err error) {
z.logger.Errorw(string(p))
return len(p), nil
}
// Run implements the admission controller run loop.
func (wh *Webhook) Run(stop <-chan struct{}) error {
logger := wh.Logger
ctx := logging.WithLogger(context.Background(), logger)
drainer := &handlers.Drainer{
Inner: wh,
QuietPeriod: wh.Options.GracePeriod,
}
otelHandler := otelhttp.NewHandler(
drainer,
wh.Options.ServiceName, // Note this service is k8s service name
otelhttp.WithMeterProvider(wh.Options.MeterProvider),
otelhttp.WithTracerProvider(wh.Options.TracerProvider),
otelhttp.WithPropagators(wh.Options.TextMapPropagator),
otelhttp.WithMetricAttributesFn(func(r *http.Request) []attribute.KeyValue {
if r.URL.Path == "" {
return nil
}
return []attribute.KeyValue{
semconv.HTTPRoute(r.URL.Path),
}
}),
otelhttp.WithFilter(func(r *http.Request) bool {
// Don't trace kubelet probes
return !network.IsKubeletProbe(r)
}),
otelhttp.WithSpanNameFormatter(func(operation string, r *http.Request) string {
if r.URL.Path == "" {
return r.Method + " /"
}
return fmt.Sprintf("%s %s", r.Method, r.URL.Path)
}),
)
// If TLSNextProto is not nil, HTTP/2 support is not enabled automatically.
nextProto := map[string]func(*http.Server, *tls.Conn, http.Handler){}
if wh.Options.EnableHTTP2 {
nextProto = nil
}
server := &http.Server{
ErrorLog: log.New(&zapWrapper{logger}, "", 0),
Handler: otelHandler,
Addr: fmt.Sprint(":", wh.Options.Port),
TLSConfig: wh.tlsConfig,
ReadHeaderTimeout: time.Minute, // https://medium.com/a-journey-with-go/go-understand-and-mitigate-slowloris-attack-711c1b1403f6
TLSNextProto: nextProto,
}
serve := server.ListenAndServe
if server.TLSConfig != nil && wh.testListener != nil {
serve = func() error {
return server.ServeTLS(wh.testListener, "", "")
}
} else if server.TLSConfig != nil {
serve = func() error {
return server.ListenAndServeTLS("", "")
}
} else if wh.testListener != nil {
serve = func() error {
return server.Serve(wh.testListener)
}
}
eg, ctx := errgroup.WithContext(ctx)
eg.Go(func() error {
if err := serve(); err != nil && !errors.Is(err, http.ErrServerClosed) {
logger.Errorw("ListenAndServe for admission webhook returned error", zap.Error(err))
return err
}
return nil
})
select {
case <-stop:
eg.Go(func() error {
// Start failing readiness probes immediately.
logger.Info("Starting to fail readiness probes...")
drainer.Drain()
return server.Shutdown(context.Background())
})
// Wait for all outstanding go routined to terminate, including our new one.
return eg.Wait()
case <-ctx.Done():
return fmt.Errorf("webhook server bootstrap failed %w", ctx.Err())
}
}
func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Verify the content type is accurate.
contentType := r.Header.Get("Content-Type")
if contentType != "application/json" {
http.Error(w, "invalid Content-Type, want `application/json`", http.StatusUnsupportedMediaType)
return
}
wh.mux.ServeHTTP(w, r)
}