@@ -49,8 +49,14 @@ const (
49
49
svcBaseAddr = "http://svc-name.svc-ns.svc"
50
50
51
51
customPath = "/custom-path"
52
+
53
+ userAgentHeader = "User-Agent"
54
+ userAgentCtxKey agentCtxKey = "UserAgent"
55
+ userAgentValue = "test"
52
56
)
53
57
58
+ type agentCtxKey string
59
+
54
60
var _ = Describe ("webhook" , func () {
55
61
Describe ("New" , func () {
56
62
Context ("v1 AdmissionReview" , func () {
@@ -315,6 +321,9 @@ func runTests(admissionReviewVersion string) {
315
321
WithLogConstructor (func (base logr.Logger , req * admission.Request ) logr.Logger {
316
322
return admission .DefaultLogConstructor (testingLogger , req )
317
323
}).
324
+ WithContextFunc (func (ctx context.Context , request * http.Request ) context.Context {
325
+ return context .WithValue (ctx , userAgentCtxKey , request .Header .Get (userAgentHeader ))
326
+ }).
318
327
Complete ()
319
328
ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
320
329
svr := m .GetWebhookServer ()
@@ -344,6 +353,30 @@ func runTests(admissionReviewVersion string) {
344
353
}
345
354
}
346
355
}` )
356
+ readerWithCxt := strings .NewReader (admissionReviewGV + admissionReviewVersion + `",
357
+ "request":{
358
+ "uid":"07e52e8d-4513-11e9-a716-42010a800270",
359
+ "kind":{
360
+ "group":"foo.test.org",
361
+ "version":"v1",
362
+ "kind":"TestValidator"
363
+ },
364
+ "resource":{
365
+ "group":"foo.test.org",
366
+ "version":"v1",
367
+ "resource":"testvalidator"
368
+ },
369
+ "namespace":"default",
370
+ "name":"foo",
371
+ "operation":"UPDATE",
372
+ "object":{
373
+ "replica":1
374
+ },
375
+ "oldObject":{
376
+ "replica":1
377
+ }
378
+ }
379
+ }` )
347
380
348
381
ctx , cancel := context .WithCancel (specCtx )
349
382
cancel ()
@@ -373,6 +406,20 @@ func runTests(admissionReviewVersion string) {
373
406
ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"allowed":false` ))
374
407
ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"code":403` ))
375
408
EventuallyWithOffset (1 , logBuffer ).Should (gbytes .Say (`"msg":"Validating object","object":{"name":"foo","namespace":"default"},"namespace":"default","name":"foo","resource":{"group":"foo.test.org","version":"v1","resource":"testvalidator"},"user":"","requestID":"07e52e8d-4513-11e9-a716-42010a800270"` ))
409
+
410
+ By ("sending a request to a validating webhook with context header validation" )
411
+ path = generateValidatePath (testValidatorGVK )
412
+ _ , err = readerWithCxt .Seek (0 , 0 )
413
+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
414
+ req = httptest .NewRequest ("POST" , svcBaseAddr + path , readerWithCxt )
415
+ req .Header .Add ("Content-Type" , "application/json" )
416
+ req .Header .Add (userAgentHeader , userAgentValue )
417
+ w = httptest .NewRecorder ()
418
+ svr .WebhookMux ().ServeHTTP (w , req )
419
+ ExpectWithOffset (1 , w .Code ).To (Equal (http .StatusOK ))
420
+ By ("sanity checking the response contains reasonable field" )
421
+ ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"allowed":true` ))
422
+ ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"code":200` ))
376
423
})
377
424
378
425
It ("should scaffold a custom validating webhook with a custom path" , func (specCtx SpecContext ) {
@@ -1009,6 +1056,7 @@ func (*TestCustomDefaulter) Default(ctx context.Context, obj runtime.Object) err
1009
1056
if d .Replica < 2 {
1010
1057
d .Replica = 2
1011
1058
}
1059
+
1012
1060
return nil
1013
1061
}
1014
1062
@@ -1035,6 +1083,7 @@ func (*TestCustomValidator) ValidateCreate(ctx context.Context, obj runtime.Obje
1035
1083
if v .Replica < 0 {
1036
1084
return nil , errors .New ("number of replica should be greater than or equal to 0" )
1037
1085
}
1086
+
1038
1087
return nil , nil
1039
1088
}
1040
1089
@@ -1056,6 +1105,12 @@ func (*TestCustomValidator) ValidateUpdate(ctx context.Context, oldObj, newObj r
1056
1105
if v .Replica < old .Replica {
1057
1106
return nil , fmt .Errorf ("new replica %v should not be fewer than old replica %v" , v .Replica , old .Replica )
1058
1107
}
1108
+
1109
+ userAgent , ok := ctx .Value (userAgentCtxKey ).(string )
1110
+ if ok && userAgent != userAgentValue {
1111
+ return nil , fmt .Errorf ("expected %s value is %q in TestCustomValidator got %q" , userAgentCtxKey , userAgentValue , userAgent )
1112
+ }
1113
+
1059
1114
return nil , nil
1060
1115
}
1061
1116
0 commit comments