@@ -28,6 +28,7 @@ import (
28
28
"os"
29
29
"path/filepath"
30
30
"reflect"
31
+ "strings"
31
32
"testing"
32
33
"text/template"
33
34
"time"
@@ -542,41 +543,6 @@ func TestWebhook(t *testing.T) {
542
543
}
543
544
}
544
545
545
- type webhookCacheTestCase struct {
546
- attr authorizer.AttributesRecord
547
-
548
- allow bool
549
- statusCode int
550
-
551
- expectedErr bool
552
- expectedAuthorized bool
553
- expectedCalls int
554
- }
555
-
556
- func testWebhookCacheCases (t * testing.T , serv * mockService , wh * WebhookAuthorizer , tests []webhookCacheTestCase ) {
557
- for i , test := range tests {
558
- serv .called = 0
559
- serv .allow = test .allow
560
- serv .statusCode = test .statusCode
561
- authorized , _ , err := wh .Authorize (test .attr )
562
- if test .expectedErr && err == nil {
563
- t .Errorf ("%d: Expected error" , i )
564
- continue
565
- } else if ! test .expectedErr && err != nil {
566
- t .Errorf ("%d: unexpected error: %v" , i , err )
567
- continue
568
- }
569
-
570
- if test .expectedAuthorized != (authorized == authorizer .DecisionAllow ) {
571
- t .Errorf ("%d: expected authorized=%v, got %v" , i , test .expectedAuthorized , authorized )
572
- }
573
-
574
- if test .expectedCalls != serv .called {
575
- t .Errorf ("%d: expected %d calls, got %d" , i , test .expectedCalls , serv .called )
576
- }
577
- }
578
- }
579
-
580
546
// TestWebhookCache verifies that error responses from the server are not
581
547
// cached, but successful responses are.
582
548
func TestWebhookCache (t * testing.T ) {
@@ -595,27 +561,86 @@ func TestWebhookCache(t *testing.T) {
595
561
596
562
aliceAttr := authorizer.AttributesRecord {User : & user.DefaultInfo {Name : "alice" }}
597
563
bobAttr := authorizer.AttributesRecord {User : & user.DefaultInfo {Name : "bob" }}
564
+ aliceRidiculousAttr := authorizer.AttributesRecord {
565
+ User : & user.DefaultInfo {Name : "alice" },
566
+ ResourceRequest : true ,
567
+ Verb : strings .Repeat ("v" , 2000 ),
568
+ APIGroup : strings .Repeat ("g" , 2000 ),
569
+ APIVersion : strings .Repeat ("a" , 2000 ),
570
+ Resource : strings .Repeat ("r" , 2000 ),
571
+ Name : strings .Repeat ("n" , 2000 ),
572
+ }
573
+ bobRidiculousAttr := authorizer.AttributesRecord {
574
+ User : & user.DefaultInfo {Name : "bob" },
575
+ ResourceRequest : true ,
576
+ Verb : strings .Repeat ("v" , 2000 ),
577
+ APIGroup : strings .Repeat ("g" , 2000 ),
578
+ APIVersion : strings .Repeat ("a" , 2000 ),
579
+ Resource : strings .Repeat ("r" , 2000 ),
580
+ Name : strings .Repeat ("n" , 2000 ),
581
+ }
582
+
583
+ type webhookCacheTestCase struct {
584
+ name string
585
+
586
+ attr authorizer.AttributesRecord
587
+
588
+ allow bool
589
+ statusCode int
590
+
591
+ expectedErr bool
592
+ expectedAuthorized bool
593
+ expectedCalls int
594
+ }
598
595
599
596
tests := []webhookCacheTestCase {
600
597
// server error and 429's retry
601
- {attr : aliceAttr , allow : false , statusCode : 500 , expectedErr : true , expectedAuthorized : false , expectedCalls : 5 },
602
- {attr : aliceAttr , allow : false , statusCode : 429 , expectedErr : true , expectedAuthorized : false , expectedCalls : 5 },
598
+ {name : "server errors retry" , attr : aliceAttr , allow : false , statusCode : 500 , expectedErr : true , expectedAuthorized : false , expectedCalls : 5 },
599
+ {name : "429s retry" , attr : aliceAttr , allow : false , statusCode : 429 , expectedErr : true , expectedAuthorized : false , expectedCalls : 5 },
603
600
// regular errors return errors but do not retry
604
- {attr : aliceAttr , allow : false , statusCode : 404 , expectedErr : true , expectedAuthorized : false , expectedCalls : 1 },
605
- {attr : aliceAttr , allow : false , statusCode : 403 , expectedErr : true , expectedAuthorized : false , expectedCalls : 1 },
606
- {attr : aliceAttr , allow : false , statusCode : 401 , expectedErr : true , expectedAuthorized : false , expectedCalls : 1 },
601
+ {name : "404 doesnt retry" , attr : aliceAttr , allow : false , statusCode : 404 , expectedErr : true , expectedAuthorized : false , expectedCalls : 1 },
602
+ {name : "403 doesnt retry" , attr : aliceAttr , allow : false , statusCode : 403 , expectedErr : true , expectedAuthorized : false , expectedCalls : 1 },
603
+ {name : "401 doesnt retry" , attr : aliceAttr , allow : false , statusCode : 401 , expectedErr : true , expectedAuthorized : false , expectedCalls : 1 },
607
604
// successful responses are cached
608
- {attr : aliceAttr , allow : true , statusCode : 200 , expectedErr : false , expectedAuthorized : true , expectedCalls : 1 },
605
+ {name : "alice successful request" , attr : aliceAttr , allow : true , statusCode : 200 , expectedErr : false , expectedAuthorized : true , expectedCalls : 1 },
609
606
// later requests within the cache window don't hit the backend
610
- {attr : aliceAttr , allow : false , statusCode : 500 , expectedErr : false , expectedAuthorized : true , expectedCalls : 0 },
607
+ {name : "alice cached request" , attr : aliceAttr , allow : false , statusCode : 500 , expectedErr : false , expectedAuthorized : true , expectedCalls : 0 },
611
608
612
609
// a request with different attributes doesn't hit the cache
613
- {attr : bobAttr , allow : false , statusCode : 500 , expectedErr : true , expectedAuthorized : false , expectedCalls : 5 },
610
+ {name : "bob failed request" , attr : bobAttr , allow : false , statusCode : 500 , expectedErr : true , expectedAuthorized : false , expectedCalls : 5 },
614
611
// successful response for other attributes is cached
615
- {attr : bobAttr , allow : true , statusCode : 200 , expectedErr : false , expectedAuthorized : true , expectedCalls : 1 },
612
+ {name : "bob unauthorized request" , attr : bobAttr , allow : false , statusCode : 200 , expectedErr : false , expectedAuthorized : false , expectedCalls : 1 },
616
613
// later requests within the cache window don't hit the backend
617
- {attr : bobAttr , allow : false , statusCode : 500 , expectedErr : false , expectedAuthorized : true , expectedCalls : 0 },
614
+ {name : "bob unauthorized cached request" , attr : bobAttr , allow : false , statusCode : 500 , expectedErr : false , expectedAuthorized : false , expectedCalls : 0 },
615
+ // ridiculous unauthorized requests are not cached.
616
+ {name : "ridiculous unauthorized request" , attr : bobRidiculousAttr , allow : false , statusCode : 200 , expectedErr : false , expectedAuthorized : false , expectedCalls : 1 },
617
+ // later ridiculous requests within the cache window still hit the backend
618
+ {name : "ridiculous unauthorized request again" , attr : bobRidiculousAttr , allow : false , statusCode : 200 , expectedErr : false , expectedAuthorized : false , expectedCalls : 1 },
619
+ // ridiculous authorized requests are not cached.
620
+ {name : "ridiculous authorized request" , attr : aliceRidiculousAttr , allow : true , statusCode : 200 , expectedErr : false , expectedAuthorized : true , expectedCalls : 1 },
621
+ // later ridiculous requests within the cache window still hit the backend
622
+ {name : "ridiculous authorized request again" , attr : aliceRidiculousAttr , allow : true , statusCode : 200 , expectedErr : false , expectedAuthorized : true , expectedCalls : 1 },
618
623
}
619
624
620
- testWebhookCacheCases (t , serv , wh , tests )
625
+ for i , test := range tests {
626
+ t .Run (test .name , func (t * testing.T ) {
627
+ serv .called = 0
628
+ serv .allow = test .allow
629
+ serv .statusCode = test .statusCode
630
+ authorized , _ , err := wh .Authorize (test .attr )
631
+ if test .expectedErr && err == nil {
632
+ t .Fatalf ("%d: Expected error" , i )
633
+ } else if ! test .expectedErr && err != nil {
634
+ t .Fatalf ("%d: unexpected error: %v" , i , err )
635
+ }
636
+
637
+ if test .expectedAuthorized != (authorized == authorizer .DecisionAllow ) {
638
+ t .Errorf ("%d: expected authorized=%v, got %v" , i , test .expectedAuthorized , authorized )
639
+ }
640
+
641
+ if test .expectedCalls != serv .called {
642
+ t .Errorf ("%d: expected %d calls, got %d" , i , test .expectedCalls , serv .called )
643
+ }
644
+ })
645
+ }
621
646
}
0 commit comments