@@ -534,39 +534,44 @@ func TestAuthzCoverage(t *testing.T) {
534
534
fw := newServerTest ()
535
535
defer fw .testHTTPServer .Close ()
536
536
537
- // method:path -> has coverage
538
- expectedCases := map [string ]bool {}
539
-
540
- // Test all the non-web-service handlers
541
- for _ , path := range fw .serverUnderTest .restfulCont .RegisteredHandlePaths () {
542
- expectedCases ["GET:" + path ] = false
543
- expectedCases ["POST:" + path ] = false
544
- }
537
+ for _ , fineGrained := range []bool {false , true } {
538
+ t .Run (fmt .Sprintf ("fineGrained=%v" , fineGrained ), func (t * testing.T ) {
539
+ featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .KubeletFineGrainedAuthz , fineGrained )
540
+ // method:path -> has coverage
541
+ expectedCases := map [string ]bool {}
542
+
543
+ // Test all the non-web-service handlers
544
+ for _ , path := range fw .serverUnderTest .restfulCont .RegisteredHandlePaths () {
545
+ expectedCases ["GET:" + path ] = false
546
+ expectedCases ["POST:" + path ] = false
547
+ }
545
548
546
- // Test all the generated web-service paths
547
- for _ , ws := range fw .serverUnderTest .restfulCont .RegisteredWebServices () {
548
- for _ , r := range ws .Routes () {
549
- expectedCases [r .Method + ":" + r .Path ] = false
550
- }
551
- }
549
+ // Test all the generated web-service paths
550
+ for _ , ws := range fw .serverUnderTest .restfulCont .RegisteredWebServices () {
551
+ for _ , r := range ws .Routes () {
552
+ expectedCases [r .Method + ":" + r .Path ] = false
553
+ }
554
+ }
552
555
553
- // This is a sanity check that the Handle->HandleWithFilter() delegation is working
554
- // Ideally, these would move to registered web services and this list would get shorter
555
- expectedPaths := []string {"/healthz" , "/metrics" , "/metrics/cadvisor" }
556
- for _ , expectedPath := range expectedPaths {
557
- if _ , expected := expectedCases ["GET:" + expectedPath ]; ! expected {
558
- t .Errorf ("Expected registered handle path %s was missing" , expectedPath )
559
- }
560
- }
556
+ // This is a sanity check that the Handle->HandleWithFilter() delegation is working
557
+ // Ideally, these would move to registered web services and this list would get shorter
558
+ expectedPaths := []string {"/healthz" , "/metrics" , "/metrics/cadvisor" }
559
+ for _ , expectedPath := range expectedPaths {
560
+ if _ , expected := expectedCases ["GET:" + expectedPath ]; ! expected {
561
+ t .Errorf ("Expected registered handle path %s was missing" , expectedPath )
562
+ }
563
+ }
561
564
562
- for _ , tc := range AuthzTestCases (false ) {
563
- expectedCases [tc .Method + ":" + tc .Path ] = true
564
- }
565
+ for _ , tc := range AuthzTestCases (fineGrained ) {
566
+ expectedCases [tc .Method + ":" + tc .Path ] = true
567
+ }
565
568
566
- for tc , found := range expectedCases {
567
- if ! found {
568
- t .Errorf ("Missing authz test case for %s" , tc )
569
- }
569
+ for tc , found := range expectedCases {
570
+ if ! found {
571
+ t .Errorf ("Missing authz test case for %s" , tc )
572
+ }
573
+ }
574
+ })
570
575
}
571
576
}
572
577
@@ -580,43 +585,47 @@ func TestAuthFilters(t *testing.T) {
580
585
581
586
attributesGetter := NewNodeAuthorizerAttributesGetter (authzTestNodeName )
582
587
583
- for _ , tc := range AuthzTestCases (false ) {
584
- t .Run (tc .Method + ":" + tc .Path , func (t * testing.T ) {
585
- var (
586
- expectedUser = AuthzTestUser ()
587
-
588
- calledAuthenticate = false
589
- calledAuthorize = false
590
- calledAttributes = false
591
- )
592
-
593
- fw .fakeAuth .authenticateFunc = func (req * http.Request ) (* authenticator.Response , bool , error ) {
594
- calledAuthenticate = true
595
- return & authenticator.Response {User : expectedUser }, true , nil
596
- }
597
- fw .fakeAuth .attributesFunc = func (u user.Info , req * http.Request ) []authorizer.Attributes {
598
- calledAttributes = true
599
- require .Equal (t , expectedUser , u )
600
- return attributesGetter .GetRequestAttributes (u , req )
601
- }
602
- fw .fakeAuth .authorizeFunc = func (a authorizer.Attributes ) (decision authorizer.Decision , reason string , err error ) {
603
- calledAuthorize = true
604
- tc .AssertAttributes (t , []authorizer.Attributes {a })
605
- return authorizer .DecisionNoOpinion , "" , nil
606
- }
588
+ for _ , fineGraned := range []bool {false , true } {
589
+ featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .KubeletFineGrainedAuthz , fineGraned )
590
+ for _ , tc := range AuthzTestCases (fineGraned ) {
591
+ t .Run (fmt .Sprintf ("method=%v:path=%v:fineGrained=%v" , tc .Method , tc .Method , fineGraned ), func (t * testing.T ) {
592
+ var (
593
+ expectedUser = AuthzTestUser ()
594
+
595
+ calledAuthenticate = false
596
+ calledAuthorize = false
597
+ calledAttributes = false
598
+ )
599
+
600
+ fw .fakeAuth .authenticateFunc = func (req * http.Request ) (* authenticator.Response , bool , error ) {
601
+ calledAuthenticate = true
602
+ return & authenticator.Response {User : expectedUser }, true , nil
603
+ }
604
+ fw .fakeAuth .attributesFunc = func (u user.Info , req * http.Request ) []authorizer.Attributes {
605
+ calledAttributes = true
606
+ require .Equal (t , expectedUser , u )
607
+ attrs := attributesGetter .GetRequestAttributes (u , req )
608
+ tc .AssertAttributes (t , attrs )
609
+ return attrs
610
+ }
611
+ fw .fakeAuth .authorizeFunc = func (a authorizer.Attributes ) (decision authorizer.Decision , reason string , err error ) {
612
+ calledAuthorize = true
613
+ return authorizer .DecisionNoOpinion , "" , nil
614
+ }
607
615
608
- req , err := http .NewRequest (tc .Method , fw .testHTTPServer .URL + tc .Path , nil )
609
- require .NoError (t , err )
616
+ req , err := http .NewRequest (tc .Method , fw .testHTTPServer .URL + tc .Path , nil )
617
+ require .NoError (t , err )
610
618
611
- resp , err := http .DefaultClient .Do (req )
612
- require .NoError (t , err )
613
- defer resp .Body .Close ()
619
+ resp , err := http .DefaultClient .Do (req )
620
+ require .NoError (t , err )
621
+ defer resp .Body .Close () //nolint:errcheck
614
622
615
- assert .Equal (t , http .StatusForbidden , resp .StatusCode )
616
- assert .True (t , calledAuthenticate , "Authenticate was not called" )
617
- assert .True (t , calledAttributes , "Attributes were not called" )
618
- assert .True (t , calledAuthorize , "Authorize was not called" )
619
- })
623
+ assert .Equal (t , http .StatusForbidden , resp .StatusCode )
624
+ assert .True (t , calledAuthenticate , "Authenticate was not called" )
625
+ assert .True (t , calledAttributes , "Attributes were not called" )
626
+ assert .True (t , calledAuthorize , "Authorize was not called" )
627
+ })
628
+ }
620
629
}
621
630
}
622
631
0 commit comments