-
Notifications
You must be signed in to change notification settings - Fork 58
/
webhook.go
573 lines (485 loc) · 19.6 KB
/
webhook.go
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
package main
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"reflect"
"strconv"
"strings"
"time"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
admissionV1 "k8s.io/api/admission/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)
type webhookOperation string
type gmsaResourceKind string
const (
contentTypeHeader = "Content-Type"
jsonContentType = "application/json"
validate webhookOperation = "VALIDATE"
mutate webhookOperation = "MUTATE"
podKind gmsaResourceKind = "pod"
containerKind gmsaResourceKind = "container"
)
type webhook struct {
server *http.Server
client kubeClientInterface
config *WebhookConfig
}
type podAdmissionError struct {
error
code int
pod *corev1.Pod
}
type WebhookConfig struct {
EnableCertReload bool
EnableRandomHostName bool
}
type WebhookOption func(*WebhookConfig)
func WithCertReload(enabled bool) WebhookOption {
return func(cfg *WebhookConfig) {
cfg.EnableCertReload = enabled
}
}
func WithRandomHostname(enabled bool) WebhookOption {
return func(cfg *WebhookConfig) {
cfg.EnableRandomHostName = enabled
}
}
func newWebhook(client kubeClientInterface) *webhook {
return newWebhookWithOptions(client)
}
func newWebhookWithOptions(client kubeClientInterface, options ...WebhookOption) *webhook {
config := &WebhookConfig{EnableCertReload: false, EnableRandomHostName: false}
for _, option := range options {
option(config)
}
return &webhook{
client: client,
config: config,
}
}
// start is a blocking call.
// If passed a listeningChan, it will close it when it's started listening
func (webhook *webhook) start(port int, tlsConfig *tlsConfig, listeningChan chan interface{}) error {
if webhook.server != nil {
return fmt.Errorf("webhook already started")
}
webhook.server = &http.Server{
Addr: ":" + strconv.Itoa(port),
Handler: webhook,
}
logrus.Infof("starting webhook server at port %v", port)
listener, err := net.Listen("tcp", webhook.server.Addr)
if err != nil {
return err
}
defer listener.Close()
keepAliveListener := tcpKeepAliveListener{listener.(*net.TCPListener)}
if listeningChan != nil {
close(listeningChan)
}
if tlsConfig == nil {
err = webhook.server.Serve(keepAliveListener)
} else {
if webhook.config.EnableCertReload {
logrus.Infof("Webhook certificate reload enabled")
certReloader := NewCertReloader(tlsConfig.crtPath, tlsConfig.keyPath)
_, err = certReloader.LoadCertificate()
if err != nil {
return err
}
go watchCertFiles(context.Background(), certReloader)
webhook.server.TLSConfig = &tls.Config{
GetCertificate: certReloader.GetCertificateFunc(),
}
err = webhook.server.ServeTLS(keepAliveListener, "", "")
} else {
err = webhook.server.ServeTLS(keepAliveListener, tlsConfig.crtPath, tlsConfig.keyPath)
}
}
if err != nil {
if err == http.ErrServerClosed {
logrus.Infof("server closed")
} else {
return err
}
}
return nil
}
// stop stops the HTTP server.
func (webhook *webhook) stop() error {
if webhook.server == nil {
return fmt.Errorf("webhook server not started yet")
}
return webhook.server.Shutdown(context.Background())
}
// ServeHTTP makes this object a http.Handler; its job is handling the HTTP routing: paths,
// methods and content-type headers.
// Since we only have a handful of endpoints, there's no need for a full-fledged router here.
func (webhook *webhook) ServeHTTP(responseWriter http.ResponseWriter, request *http.Request) {
var operation webhookOperation
switch request.URL.Path {
case "/validate":
operation = validate
case "/mutate":
operation = mutate
case "/info":
writeJSONBody(responseWriter, map[string]string{"version": getVersion()})
return
case "/health":
responseWriter.WriteHeader(http.StatusNoContent)
return
default:
abortHTTPRequest(responseWriter, http.StatusNotFound, "received %s request for unknown path %s", request.Method, request.URL.Path)
return
}
// should be a POST request
if strings.ToUpper(request.Method) != "POST" {
abortHTTPRequest(responseWriter, http.StatusMethodNotAllowed, "expected POST HTTP request, got a %s %s request", request.Method, operation)
return
}
// verify the content type is JSON
if contentType := request.Header.Get(contentTypeHeader); contentType != jsonContentType {
abortHTTPRequest(responseWriter, http.StatusUnsupportedMediaType, "expected JSON content-type header for %s request, got %q", operation, contentType)
return
}
admissionResponse := webhook.httpRequestToAdmissionResponse(request, operation)
responseAdmissionReview := admissionV1.AdmissionReview{
TypeMeta: metav1.TypeMeta{
Kind: "AdmissionReview",
APIVersion: "admission.k8s.io/v1",
},
Response: admissionResponse,
}
writeJSONBody(responseWriter, responseAdmissionReview)
}
// abortHTTPRequest is called for low-level HTTP errors (routing or writing the body)
func abortHTTPRequest(responseWriter http.ResponseWriter, httpCode int, logMsg string, logArs ...interface{}) {
logrus.Infof(logMsg, logArs...)
responseWriter.WriteHeader(httpCode)
}
// writeJsonBody writes a JSON object to an HTTP response.
func writeJSONBody(responseWriter http.ResponseWriter, jsonBody interface{}) {
if responseBytes, err := json.Marshal(jsonBody); err == nil {
logrus.Debugf("sending response: %s", responseBytes)
responseWriter.Header().Set(contentTypeHeader, jsonContentType)
if _, err = responseWriter.Write(responseBytes); err != nil {
abortHTTPRequest(responseWriter, http.StatusInternalServerError, "error when writing response JSON %s: %v", responseBytes, err)
}
} else {
abortHTTPRequest(responseWriter, http.StatusInternalServerError, "error when marshalling response %v: %v", jsonBody, err)
}
}
// httpRequestToAdmissionResponse turns a raw HTTP request into an AdmissionResponse struct.
func (webhook *webhook) httpRequestToAdmissionResponse(request *http.Request, operation webhookOperation) *admissionV1.AdmissionResponse {
// read the body
if request.Body == nil {
deniedAdmissionResponse(fmt.Errorf("no request body"), http.StatusBadRequest)
}
body, err := ioutil.ReadAll(request.Body)
if err != nil {
return deniedAdmissionResponse(fmt.Errorf("couldn't read request body: %v", err), http.StatusBadRequest)
}
defer request.Body.Close()
logrus.Debugf("handling %s request: %s", operation, body)
// unmarshall the request
admissionReview := admissionV1.AdmissionReview{}
if err = json.Unmarshal(body, &admissionReview); err != nil {
return deniedAdmissionResponse(fmt.Errorf("unable to unmarshall JSON body as an admission review: %v", err), http.StatusBadRequest)
}
if admissionReview.Request == nil {
return deniedAdmissionResponse(fmt.Errorf("no 'Request' field in JSON body"), http.StatusBadRequest)
}
admissionResponse, admissionError := webhook.validateOrMutate(request.Context(), admissionReview.Request, operation)
if admissionError != nil {
admissionResponse = deniedAdmissionResponse(admissionError)
}
// return the same UID
admissionResponse.UID = admissionReview.Request.UID
return admissionResponse
}
// validateOrMutate is where the non-HTTP-related work happens.
func (webhook *webhook) validateOrMutate(ctx context.Context, request *admissionV1.AdmissionRequest, operation webhookOperation) (*admissionV1.AdmissionResponse, *podAdmissionError) {
if request.Kind.Kind != "Pod" {
return nil, &podAdmissionError{error: fmt.Errorf("expected a Pod object, got a %v", request.Kind.Kind), code: http.StatusBadRequest}
}
pod, err := unmarshallPod(request.Object)
if err != nil {
return nil, err
}
switch request.Operation {
case admissionV1.Create:
switch operation {
case validate:
return webhook.validateCreateRequest(ctx, pod, request.Namespace)
case mutate:
return webhook.mutateCreateRequest(ctx, pod)
default:
// shouldn't happen, but needed so that all paths in the function have a return value
panic(fmt.Errorf("unexpected webhook operation: %v", operation))
}
case admissionV1.Update:
if operation == validate {
oldPod, err := unmarshallPod(request.OldObject)
if err != nil {
return nil, err
}
return validateUpdateRequest(pod, oldPod)
}
// we only do validation on updates, no mutation
return &admissionV1.AdmissionResponse{Allowed: true}, nil
default:
return nil, &podAdmissionError{error: fmt.Errorf("unpexpected operation %s", request.Operation), pod: pod, code: http.StatusBadRequest}
}
}
// unmarshallPod unmarshalls a pod object from its raw JSON representation.
func unmarshallPod(object runtime.RawExtension) (*corev1.Pod, *podAdmissionError) {
pod := &corev1.Pod{}
if err := json.Unmarshal(object.Raw, pod); err != nil {
return nil, &podAdmissionError{error: fmt.Errorf("unable to unmarshall pod JSON object: %v", err), code: http.StatusBadRequest}
}
return pod, nil
}
// validateCreateRequest ensures that the GMSA contents set in the pod's spec
// match the corresponding GMSA names, and that the pod's service account
// is authorized to `use` the requested GMSA's.
func (webhook *webhook) validateCreateRequest(ctx context.Context, pod *corev1.Pod, namespace string) (*admissionV1.AdmissionResponse, *podAdmissionError) {
if err := iterateOverWindowsSecurityOptions(pod, func(windowsOptions *corev1.WindowsSecurityContextOptions, resourceKind gmsaResourceKind, resourceName string, _ int) *podAdmissionError {
if credSpecName := windowsOptions.GMSACredentialSpecName; credSpecName != nil {
// let's check that the associated service account can read the relevant cred spec CRD
if authorized, reason := webhook.client.isAuthorizedToUseCredSpec(ctx, pod.Spec.ServiceAccountName, namespace, *credSpecName); !authorized {
msg := fmt.Sprintf("service account %q is not authorized to `use` GMSA cred spec %q", pod.Spec.ServiceAccountName, *credSpecName)
if reason != "" {
msg += fmt.Sprintf(", reason: %q", reason)
}
return &podAdmissionError{error: fmt.Errorf(msg), pod: pod, code: http.StatusForbidden}
}
// and the contents should match the ones contained in the GMSA resource with that name
if credSpecContents := windowsOptions.GMSACredentialSpec; credSpecContents != nil {
if expectedContents, code, retrieveErr := webhook.client.retrieveCredSpecContents(ctx, *credSpecName); retrieveErr != nil {
return &podAdmissionError{error: retrieveErr, pod: pod, code: code}
} else if specsEqual, compareErr := compareCredSpecContents(*credSpecContents, expectedContents); !specsEqual || compareErr != nil {
msg := fmt.Sprintf("the GMSA cred spec contents for %s %q does not match the contents of GMSA resource %q", resourceKind, resourceName, *credSpecName)
if compareErr != nil {
msg += fmt.Sprintf(": %v", compareErr)
}
return &podAdmissionError{error: fmt.Errorf(msg), pod: pod, code: http.StatusUnprocessableEntity}
}
}
} else if windowsOptions.GMSACredentialSpec != nil {
// the GMSA's name is not set, but the contents are
msg := fmt.Sprintf("%s %q has a GMSA cred spec set, but does not define the name of the corresponding resource", resourceKind, resourceName)
return &podAdmissionError{error: fmt.Errorf(msg), pod: pod, code: http.StatusUnprocessableEntity}
}
return nil
}); err != nil {
return nil, err
}
return &admissionV1.AdmissionResponse{Allowed: true}, nil
}
// compareCredSpecContents returns true iff the two strings represent the same credential spec contents.
func compareCredSpecContents(fromResource, fromCRD string) (bool, error) {
// this is actually what happens almost all the time, when users don't set the GMSA contents directly
// but instead rely on the mutating webhook to do that for them; and in that case no need for a slow
// JSON parsing and comparison
if fromResource == fromCRD {
return true, nil
}
var (
jsonObjectFromResource map[string]interface{}
jsonObjectFromCRD map[string]interface{}
)
if err := json.Unmarshal([]byte(fromResource), &jsonObjectFromResource); err != nil {
return false, fmt.Errorf("unable to parse %q as a JSON object: %v", fromResource, err)
}
if err := json.Unmarshal([]byte(fromCRD), &jsonObjectFromCRD); err != nil {
return false, fmt.Errorf("unable to parse CRD %q as a JSON object: %v", fromCRD, err)
}
return reflect.DeepEqual(jsonObjectFromResource, jsonObjectFromCRD), nil
}
// mutateCreateRequest inlines the requested GMSA's into the pod's and containers' `WindowsSecurityOptions` structs.
func (webhook *webhook) mutateCreateRequest(ctx context.Context, pod *corev1.Pod) (*admissionV1.AdmissionResponse, *podAdmissionError) {
var patches []map[string]string
hasGMSA := false
if err := iterateOverWindowsSecurityOptions(pod, func(windowsOptions *corev1.WindowsSecurityContextOptions, resourceKind gmsaResourceKind, resourceName string, containerIndex int) *podAdmissionError {
if credSpecName := windowsOptions.GMSACredentialSpecName; credSpecName != nil {
hasGMSA = true
// if the user has pre-set the GMSA's contents, we won't override it - it'll be down
// to the validation endpoint to make sure the contents actually are what they should
if credSpecContents := windowsOptions.GMSACredentialSpec; credSpecContents == nil {
contents, code, retrieveErr := webhook.client.retrieveCredSpecContents(ctx, *credSpecName)
if retrieveErr != nil {
return &podAdmissionError{error: retrieveErr, pod: pod, code: code}
}
partialPath := ""
if resourceKind == containerKind {
partialPath = fmt.Sprintf("/containers/%d", containerIndex)
}
// worth noting that this JSON patch is guaranteed to work since we know at this point
// that the resource comprises a `windowsOptions` object, and and that it doesn't have a
// "gmsaCredentialSpec" field
patches = append(patches, map[string]string{
"op": "add",
"path": fmt.Sprintf("/spec%s/securityContext/windowsOptions/gmsaCredentialSpec", partialPath),
"value": contents,
})
}
}
return nil
}); err != nil {
return nil, err
}
if hasGMSA && webhook.config.EnableRandomHostName {
// Pods are GMSA related, Env enabled, patch the hostname only if it is empty
hostName := pod.Spec.Hostname
if hostName == "" {
hostName = generateUUID()
patches = append(patches, map[string]string{
"op": "add",
"path": "/spec/hostname",
"value": hostName,
})
} else {
// Will honor the hostname set in the spec, print out a message
logrus.Warnf("hostname is set in spec and will be hornored instead of being randomized")
}
}
admissionResponse := &admissionV1.AdmissionResponse{Allowed: true}
if len(patches) != 0 {
patchesBytes, err := json.Marshal(patches)
if err != nil {
return nil, &podAdmissionError{error: fmt.Errorf("unable to marshall patch JSON %v: %v", patches, err), pod: pod, code: http.StatusInternalServerError}
}
admissionResponse.Patch = patchesBytes
patchType := admissionV1.PatchTypeJSONPatch
admissionResponse.PatchType = &patchType
}
return admissionResponse, nil
}
// validateUpdateRequest ensures that there are no updates to any of the GMSA names or contents.
func validateUpdateRequest(pod, oldPod *corev1.Pod) (*admissionV1.AdmissionResponse, *podAdmissionError) {
var oldPodContainerOptions map[string]*corev1.WindowsSecurityContextOptions
if err := iterateOverWindowsSecurityOptions(pod, func(windowsOptions *corev1.WindowsSecurityContextOptions, resourceKind gmsaResourceKind, resourceName string, _ int) *podAdmissionError {
var oldWindowsOptions *corev1.WindowsSecurityContextOptions
if resourceKind == podKind {
if oldPod.Spec.SecurityContext != nil {
oldWindowsOptions = oldPod.Spec.SecurityContext.WindowsOptions
}
} else {
// it's a container; look for the same container in the old pod,
// lazily building the map of container names to security options if needed
if oldPodContainerOptions == nil {
oldPodContainerOptions = make(map[string]*corev1.WindowsSecurityContextOptions)
iterateOverWindowsSecurityOptions(oldPod, func(winOpts *corev1.WindowsSecurityContextOptions, rsrcKind gmsaResourceKind, rsrcName string, _ int) *podAdmissionError {
if rsrcKind == containerKind {
oldPodContainerOptions[rsrcName] = winOpts
}
return nil
})
}
oldWindowsOptions = oldPodContainerOptions[resourceName]
}
if oldWindowsOptions == nil {
oldWindowsOptions = &corev1.WindowsSecurityContextOptions{}
}
var modifiedFieldNames []string
if !equalStringPointers(windowsOptions.GMSACredentialSpecName, oldWindowsOptions.GMSACredentialSpecName) {
modifiedFieldNames = append(modifiedFieldNames, "name")
}
if !equalStringPointers(windowsOptions.GMSACredentialSpec, oldWindowsOptions.GMSACredentialSpec) {
modifiedFieldNames = append(modifiedFieldNames, "contents")
}
if len(modifiedFieldNames) != 0 {
msg := fmt.Errorf("cannot update an existing pod's GMSA settings (GMSA %s modified on %s %q)", strings.Join(modifiedFieldNames, " and "), resourceKind, resourceName)
return &podAdmissionError{error: msg, pod: pod, code: http.StatusForbidden}
}
return nil
}); err != nil {
return nil, err
}
return &admissionV1.AdmissionResponse{Allowed: true}, nil
}
func equalStringPointers(s1, s2 *string) bool {
if s1 == nil {
return s2 == nil
}
if s2 == nil {
return false
}
return *s1 == *s2
}
// iterateOverWindowsSecurityOptions calls `f` on the pod's `.Spec.SecurityContext.WindowsOptions` field,
// as well as over each of its container's `.SecurityContext.WindowsOptions` field.
// `f` can assume it only gets called with non-nil `WindowsSecurityOptions` pointers; the other
// arguments give information on the resource owning that pointer - in particular, if that
// resource is a container, `containerIndex` is the index of the container in the spec's list (-1 for pods).
// If `f` returns an error, that breaks the loop, and the error is bubbled up.
func iterateOverWindowsSecurityOptions(pod *corev1.Pod, f func(windowsOptions *corev1.WindowsSecurityContextOptions, resourceKind gmsaResourceKind, resourceName string, containerIndex int) *podAdmissionError) *podAdmissionError {
if pod.Spec.SecurityContext != nil && pod.Spec.SecurityContext.WindowsOptions != nil {
if err := f(pod.Spec.SecurityContext.WindowsOptions, podKind, pod.Name, -1); err != nil {
return err
}
}
for i, container := range pod.Spec.Containers {
if container.SecurityContext != nil && container.SecurityContext.WindowsOptions != nil {
if err := f(container.SecurityContext.WindowsOptions, containerKind, container.Name, i); err != nil {
return err
}
}
}
return nil
}
// deniedAdmissionResponse is a helper function to create an AdmissionResponse
// with an embedded error.
func deniedAdmissionResponse(err error, httpCode ...int) *admissionV1.AdmissionResponse {
var code int
logMsg := "refusing to admit"
if admissionError, ok := err.(*podAdmissionError); ok {
code = admissionError.code
if admissionError.pod != nil {
logMsg += fmt.Sprintf(" pod %+v", admissionError.pod)
}
}
if len(httpCode) > 0 {
code = httpCode[0]
}
if code != 0 {
logMsg += fmt.Sprintf(" with code %v", code)
}
logrus.Infof("%s: %v", logMsg, err)
return &admissionV1.AdmissionResponse{
Allowed: false,
Result: &metav1.Status{
Message: err.Error(),
Code: int32(code),
},
}
}
// stolen from https://github.com/golang/go/blob/go1.12/src/net/http/server.go#L3255-L3271
type tcpKeepAliveListener struct {
*net.TCPListener
}
func (ln tcpKeepAliveListener) Accept() (net.Conn, error) {
tc, err := ln.AcceptTCP()
if err != nil {
return nil, err
}
tc.SetKeepAlive(true)
tc.SetKeepAlivePeriod(3 * time.Minute)
return tc, nil
}
func generateUUID() string {
// Generate a new UUID
id := uuid.New()
// Convert to string and get the first 15 characters in lower case
shortUUID := strings.ToLower(id.String()[:15])
return shortUUID
}