forked from kubernetes/cel-admission-webhook
/
server.go
501 lines (427 loc) · 12.7 KB
/
server.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
package webhook
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"reflect"
"regexp"
"strings"
"sync"
"time"
"github.com/kubescape/kubeenforcer/pkg/alertmanager"
admissionv1 "k8s.io/api/admission/v1"
authenticationv1 "k8s.io/api/authentication/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apiserver/pkg/admission"
"k8s.io/apiserver/pkg/authentication/user"
"k8s.io/klog/v2"
)
var logger klog.Logger = klog.LoggerWithName(klog.Background(), "webhook")
type Interface interface {
// Runs the webhook server until the passed context is cancelled, or it
// experiences an internal error.
//
// Error is always non-nil and will always be one of:
// deadline exceeded
// context cancelled
// or http listen error
Run(ctx context.Context) error
}
func New(addr string, certFile, keyFile string, alertmanagerHost string, scheme *runtime.Scheme, validator admission.ValidationInterface) Interface {
codecs := serializer.NewCodecFactory(scheme)
return &webhook{
objectInferfaces: admission.NewObjectInterfacesFromScheme(scheme),
decoder: codecs.UniversalDeserializer(),
validator: validator,
addr: addr,
certFile: certFile,
keyFile: keyFile,
alertmanagerHost: alertmanagerHost,
}
}
type webhook struct {
lock sync.Mutex
port int
validator admission.ValidationInterface
objectInferfaces admission.ObjectInterfaces
decoder runtime.Decoder
addr string
alertmanagerHost string
certFile, keyFile string
}
func notifyChanges(ctx context.Context, paths ...string) <-chan struct{} {
type info struct {
modTime time.Time
err string
}
infos := map[string]info{}
getInfos := func() map[string]info {
res := map[string]info{}
for _, v := range paths {
fileInfo, err := os.Stat(v)
if err != nil {
infos[v] = info{err: err.Error()}
} else {
infos[v] = info{modTime: fileInfo.ModTime()}
}
}
return res
}
lastInfos := getInfos()
res := make(chan struct{})
go func() {
defer close(res)
for {
select {
case <-ctx.Done():
// context cancelled, stop watching
return
case <-time.After(2 * time.Second):
newInfos := getInfos()
if reflect.DeepEqual(lastInfos, newInfos) {
continue
}
lastInfos = newInfos
// skip event if client has not read last change
select {
case res <- struct{}{}:
default:
}
}
}
}()
return res
}
func (wh *webhook) Run(ctx context.Context) error {
var serverError error
var wg sync.WaitGroup
logger.Info("starting webhook HTTP server")
defer logger.Info("stopped webhook HTTP server")
defer wg.Wait()
wg.Add(1)
defer wg.Done()
launchServer := func() (*http.Server, <-chan error) {
mux := http.NewServeMux()
mux.HandleFunc("/health", wh.handleHealth)
mux.HandleFunc("/validate", wh.handleWebhookValidate)
srv := &http.Server{}
srv.Handler = mux
srv.Addr = wh.addr
errChan := make(chan error)
wg.Add(1)
go func() {
defer wg.Done()
defer close(errChan)
err := srv.ListenAndServeTLS(wh.certFile, wh.keyFile)
errChan <- err
// ListenAndServeTLS always returns non-nil error
}()
return srv, errChan
}
watchCtx, cancelWatches := context.WithCancel(ctx)
defer cancelWatches()
keyWatch := notifyChanges(watchCtx, wh.certFile, wh.keyFile)
currentServer, currentErrorChannel := launchServer()
loop:
for {
select {
case <-ctx.Done():
// If the caller closed their context, rather than the server having errored,
// close the server. srv.Close() is safe to call on an already-closed server
//
// note: should we prefer to use Shutdown with a deadline for graceful close
// rather than Close?
if err := currentServer.Close(); err != nil {
// Errors with gracefully shutting down connections. Not fatal. Server
// is still closed.
logger.Error(err, "shutting down webhook")
}
serverError = ctx.Err()
break loop
case serverError, _ = <-currentErrorChannel:
// Server was closed independently of being restarted
break loop
case _, ok := <-keyWatch:
if !ok {
serverError = watchCtx.Err()
break loop
}
logger.Info("TLS input has changed, restarting HTTP server")
// Graceful shutdown, ignore any errors
wg.Add(1)
q := currentServer
go func() {
defer wg.Done()
//!TOOD: add shutdown timeout, requests to a webhook should
// not be long-lived
shutdownCtx, shutdownCancel := context.WithTimeout(watchCtx, 5*time.Second)
defer shutdownCancel()
q.Shutdown(shutdownCtx)
}()
currentServer, currentErrorChannel = launchServer()
}
}
return serverError
}
func (wh *webhook) handleHealth(w http.ResponseWriter, req *http.Request) {
fmt.Fprint(w, "OK")
}
func (wh *webhook) handleWebhookValidate(w http.ResponseWriter, req *http.Request) {
parsed, err := parseRequest(req)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// logger.Info(
// "review request",
// "user",
// parsed.Request.UserInfo.String(),
// "resource",
// parsed.Request.Resource.String(),
// "operation",
// parsed.Request.Operation,
// "uid",
// parsed.Request.UID,
// )
failure := func(err error, status int) {
http.Error(w, err.Error(), status)
logger.Error(err, "review response", "uid", parsed.Request.UID, "status", status)
}
err = nil
var attrs admission.Attributes
if wh.validator.Handles(admission.Operation(parsed.Request.Operation)) {
var object runtime.Object
var oldObject runtime.Object
if len(parsed.Request.OldObject.Raw) > 0 {
obj, gvk, err := wh.decoder.Decode(parsed.Request.OldObject.Raw, nil, nil)
switch {
case gvk == nil || *gvk != schema.GroupVersionKind(parsed.Request.Kind):
// GVK case first. If object type is unknown it is parsed to
// unstructured, but
failure(fmt.Errorf("unexpected GVK %v. Expected %v", gvk, parsed.Request.Kind), http.StatusBadRequest)
return
case err != nil && runtime.IsNotRegisteredError(err):
var oldUnstructured unstructured.Unstructured
err = json.Unmarshal(parsed.Request.OldObject.Raw, &oldUnstructured)
if err != nil {
failure(err, http.StatusInternalServerError)
return
}
oldObject = &oldUnstructured
case err != nil:
failure(err, http.StatusBadRequest)
return
default:
oldObject = obj
}
}
if len(parsed.Request.Object.Raw) > 0 {
obj, gvk, err := wh.decoder.Decode(parsed.Request.Object.Raw, nil, nil)
switch {
case gvk == nil || *gvk != schema.GroupVersionKind(parsed.Request.Kind):
// GVK case first. If object type is unknown it is parsed to
// unstructured, but
failure(fmt.Errorf("unexpected GVK %v. Expected %v", gvk, parsed.Request.Kind), http.StatusBadRequest)
return
case err != nil && runtime.IsNotRegisteredError(err):
var objUnstructured unstructured.Unstructured
err = json.Unmarshal(parsed.Request.Object.Raw, &objUnstructured)
if err != nil {
failure(err, http.StatusInternalServerError)
return
}
object = &objUnstructured
case err != nil:
failure(err, http.StatusBadRequest)
return
default:
object = obj
}
}
// Parse into native types if possible
convertExtra := func(input map[string]authenticationv1.ExtraValue) map[string][]string {
if input == nil {
return nil
}
res := map[string][]string{}
for k, v := range input {
var converted []string
for _, s := range v {
converted = append(converted, string(s))
}
res[k] = converted
}
return res
}
//!TODO: Parse options as v1.CreateOptions, v1.DeleteOptions, or v1.PatchOptions
attrs = admission.NewAttributesRecord(
object,
oldObject,
schema.GroupVersionKind(parsed.Request.Kind),
parsed.Request.Namespace,
parsed.Request.Name,
schema.GroupVersionResource{
Group: parsed.Request.Resource.Group,
Version: parsed.Request.Resource.Version,
Resource: parsed.Request.Resource.Resource,
},
parsed.Request.SubResource,
admission.Operation(parsed.Request.Operation),
nil, // operation options?
false,
&user.DefaultInfo{
Name: parsed.Request.UserInfo.Username,
UID: parsed.Request.UserInfo.UID,
Groups: parsed.Request.UserInfo.Groups,
Extra: convertExtra(parsed.Request.UserInfo.Extra),
})
err = wh.validator.Validate(context.TODO(), attrs, wh.objectInferfaces)
}
response := reviewResponse(
parsed.Request.UID,
err,
wh.alertmanagerHost,
parsed.Request.Resource.Resource,
parsed.Request.Name,
parsed.Request.Namespace,
attrs,
&parsed.Request.UserInfo,
)
out, err := json.Marshal(response)
if err != nil {
failure(err, http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(out)
// logger.Info(
// "review response",
// "resource",
// parsed.Request.Resource.String(),
// "namespace",
// parsed.Request.Namespace,
// "name",
// parsed.Request.Name,
// "allowed",
// response.Response.Allowed,
// "msg",
// response.Response.Result.Message,
// "reason",
// response.Response.Result.Reason,
// "uid",
// parsed.Request.UID,
// )
}
func getValidationAnnotations(attrs admission.Attributes) (audit bool, deny bool) {
validationActionsPattern := `validationActions":\[(.*?)\]`
regex, _ := regexp.Compile(validationActionsPattern)
match := regex.FindStringSubmatch(fmt.Sprintf("%+v", attrs))
if len(match) >= 2 {
actions := match[1]
audit = strings.Contains(actions, "Audit")
deny = strings.Contains(actions, "Deny")
}
// logger.Info("The actions are", "audit", audit, "deny", deny)
return audit, deny
}
func getMessage(attrs admission.Attributes) (message string) {
validationMessagePattern := `message":"(.*?)"`
regex, _ := regexp.Compile(validationMessagePattern)
match := regex.FindStringSubmatch(fmt.Sprintf("%+v", attrs))
if len(match) >= 2 {
message = match[1]
}
// logger.Info("The message is", "message", message)
return message
}
func getPolicy(attrs admission.Attributes) (policy string) {
policyPattern := `policy":"(.*?)"`
regex, _ := regexp.Compile(policyPattern)
match := regex.FindStringSubmatch(fmt.Sprintf("%+v", attrs))
if len(match) >= 2 {
policy = match[1]
}
// logger.Info("The policy is", "policy", policy)
return policy
}
func reviewResponse(uid types.UID, err error, aletmanagerHost string, resource string, name string, namespace string, attrs admission.Attributes, requestingUser *authenticationv1.UserInfo) *admissionv1.AdmissionReview {
allowed := err == nil
var status int32 = http.StatusAccepted
if err != nil {
status = http.StatusForbidden
}
reason := metav1.StatusReasonUnknown
message := "valid"
if err != nil {
message = err.Error()
}
var statusErr *k8serrors.StatusError
if ok := errors.As(err, &statusErr); ok {
reason = statusErr.ErrStatus.Reason
message = statusErr.ErrStatus.Message
status = statusErr.ErrStatus.Code
}
audit, deny := getValidationAnnotations(attrs)
if audit || deny {
if aletmanagerHost != "" {
policyName := getPolicy(attrs)
alerter := alertmanager.New(aletmanagerHost, "")
alertInfo := alertmanager.AlertInfo{
Name: fmt.Sprintf("Failed Policy: %v", policyName),
Severity: string(reason),
Resource: resource,
Instance: name,
Namespace: namespace,
RequestingUser: requestingUser.Username,
Description: getMessage(attrs),
}
alerter.Alert(&alertInfo)
}
}
return &admissionv1.AdmissionReview{
TypeMeta: metav1.TypeMeta{
Kind: "AdmissionReview",
APIVersion: "admission.k8s.io/v1",
},
Response: &admissionv1.AdmissionResponse{
UID: uid,
Allowed: allowed,
Result: &metav1.Status{
Code: status,
Message: message,
Reason: reason,
},
},
}
}
// parseRequest extracts an AdmissionReview from an http.Request if possible
func parseRequest(r *http.Request) (*admissionv1.AdmissionReview, error) {
if r.Header.Get("Content-Type") != "application/json" {
return nil, fmt.Errorf("Content-Type: %q should be %q",
r.Header.Get("Content-Type"), "application/json")
}
bodybuf := new(bytes.Buffer)
bodybuf.ReadFrom(r.Body)
body := bodybuf.Bytes()
if len(body) == 0 {
return nil, fmt.Errorf("admission request body is empty")
}
var a admissionv1.AdmissionReview
if err := json.Unmarshal(body, &a); err != nil {
return nil, fmt.Errorf("could not parse admission review request: %v", err)
}
if a.Request == nil {
return nil, fmt.Errorf("admission review can't be used: Request field is nil")
}
return &a, nil
}