@@ -396,7 +396,7 @@ func setupFakeClient(role client.Object) client.Client {
396396func TestPreAuthorize_Success (t * testing.T ) {
397397 t .Run ("preauthorize succeeds with no missing rbac rules" , func (t * testing.T ) {
398398 fakeClient := setupFakeClient (privilegedClusterRole )
399- preAuth := NewRBACPreAuthorizer (fakeClient , WithClusterCollectionVerbs ("list" , "watch" ), WithNamespacedCollectionVerbs ( "create" ) )
399+ preAuth := NewRBACPreAuthorizer (fakeClient , WithClusterCollectionVerbs ("list" , "watch" ))
400400 missingRules , err := preAuth .PreAuthorize (context .TODO (), testUser , strings .NewReader (testManifest ))
401401 require .NoError (t , err )
402402 require .Equal (t , []ScopedPolicyRules {}, missingRules )
@@ -406,7 +406,7 @@ func TestPreAuthorize_Success(t *testing.T) {
406406func TestPreAuthorize_MissingRBAC (t * testing.T ) {
407407 t .Run ("preauthorize fails and finds missing rbac rules" , func (t * testing.T ) {
408408 fakeClient := setupFakeClient (limitedClusterRole )
409- preAuth := NewRBACPreAuthorizer (fakeClient , WithClusterCollectionVerbs ("list" , "watch" ), WithNamespacedCollectionVerbs ( "create" ) )
409+ preAuth := NewRBACPreAuthorizer (fakeClient , WithClusterCollectionVerbs ("list" , "watch" ))
410410 missingRules , err := preAuth .PreAuthorize (context .TODO (), testUser , strings .NewReader (testManifest ))
411411 require .NoError (t , err )
412412 require .Equal (t , expectedSingleNamespaceMissingRules , missingRules )
@@ -416,7 +416,7 @@ func TestPreAuthorize_MissingRBAC(t *testing.T) {
416416func TestPreAuthorizeMultiNamespace_MissingRBAC (t * testing.T ) {
417417 t .Run ("preauthorize fails and finds missing rbac rules in multiple namespaces" , func (t * testing.T ) {
418418 fakeClient := setupFakeClient (limitedClusterRole )
419- preAuth := NewRBACPreAuthorizer (fakeClient , WithClusterCollectionVerbs ("list" , "watch" ), WithNamespacedCollectionVerbs ( "create" ) )
419+ preAuth := NewRBACPreAuthorizer (fakeClient , WithClusterCollectionVerbs ("list" , "watch" ))
420420 missingRules , err := preAuth .PreAuthorize (context .TODO (), testUser , strings .NewReader (testManifestMultiNamespace ))
421421 require .NoError (t , err )
422422 require .Equal (t , expectedMultiNamespaceMissingRules , missingRules )
@@ -426,7 +426,7 @@ func TestPreAuthorizeMultiNamespace_MissingRBAC(t *testing.T) {
426426func TestPreAuthorize_CheckEscalation (t * testing.T ) {
427427 t .Run ("preauthorize succeeds with no missing rbac rules" , func (t * testing.T ) {
428428 fakeClient := setupFakeClient (escalatingClusterRole )
429- preAuth := NewRBACPreAuthorizer (fakeClient , WithClusterCollectionVerbs ("list" , "watch" ), WithNamespacedCollectionVerbs ( "create" ) )
429+ preAuth := NewRBACPreAuthorizer (fakeClient , WithClusterCollectionVerbs ("list" , "watch" ))
430430 missingRules , err := preAuth .PreAuthorize (context .TODO (), testUser , strings .NewReader (testManifest ))
431431 require .NoError (t , err )
432432 require .Equal (t , []ScopedPolicyRules {}, missingRules )
@@ -436,7 +436,7 @@ func TestPreAuthorize_CheckEscalation(t *testing.T) {
436436func TestPreAuthorize_AdditionalRequiredPerms_MissingRBAC (t * testing.T ) {
437437 t .Run ("preauthorize fails and finds missing rbac rules coming from the additional required permissions" , func (t * testing.T ) {
438438 fakeClient := setupFakeClient (escalatingClusterRole )
439- preAuth := NewRBACPreAuthorizer (fakeClient , WithClusterCollectionVerbs ("list" , "watch" ), WithNamespacedCollectionVerbs ( "create" ) )
439+ preAuth := NewRBACPreAuthorizer (fakeClient , WithClusterCollectionVerbs ("list" , "watch" ))
440440 missingRules , err := preAuth .PreAuthorize (context .TODO (), testUser , strings .NewReader (testManifest ), func (user user.Info ) []authorizer.AttributesRecord {
441441 return []authorizer.AttributesRecord {
442442 {
@@ -514,7 +514,7 @@ func TestPreAuthorize_WithClusterCollectionVerbs(t *testing.T) {
514514
515515 t .Run ("no cluster collection verbs option omits cluster-scoped collection rules" , func (t * testing.T ) {
516516 fakeClient := setupFakeClient (limitedClusterRole )
517- preAuth := NewRBACPreAuthorizer (fakeClient , WithNamespacedCollectionVerbs ( "create" ) )
517+ preAuth := NewRBACPreAuthorizer (fakeClient )
518518 missingRules , err := preAuth .PreAuthorize (context .TODO (), testUser , strings .NewReader (testManifest ))
519519 require .NoError (t , err )
520520 // With no cluster collection verbs, there should be no cluster-scoped (namespace="") missing rules
@@ -523,7 +523,7 @@ func TestPreAuthorize_WithClusterCollectionVerbs(t *testing.T) {
523523
524524 t .Run ("cluster verbs option only checks those verbs at cluster scope" , func (t * testing.T ) {
525525 fakeClient := setupFakeClient (limitedClusterRole )
526- preAuth := NewRBACPreAuthorizer (fakeClient , WithClusterCollectionVerbs ("get" , "patch" , "update" ), WithNamespacedCollectionVerbs ( "create" ) )
526+ preAuth := NewRBACPreAuthorizer (fakeClient , WithClusterCollectionVerbs ("get" , "patch" , "update" ))
527527 missingRules , err := preAuth .PreAuthorize (context .TODO (), testUser , strings .NewReader (testManifest ))
528528 require .NoError (t , err )
529529 require .Equal (t , []ScopedPolicyRules {
@@ -557,139 +557,31 @@ func TestPreAuthorize_WithClusterCollectionVerbs(t *testing.T) {
557557
558558 t .Run ("privileged user with no cluster collection verbs succeeds" , func (t * testing.T ) {
559559 fakeClient := setupFakeClient (privilegedClusterRole )
560- preAuth := NewRBACPreAuthorizer (fakeClient , WithNamespacedCollectionVerbs ( "create" ) )
560+ preAuth := NewRBACPreAuthorizer (fakeClient )
561561 missingRules , err := preAuth .PreAuthorize (context .TODO (), testUser , strings .NewReader (testManifest ))
562562 require .NoError (t , err )
563563 require .Equal (t , []ScopedPolicyRules {}, missingRules )
564564 })
565565}
566566
567- func TestPreAuthorize_WithNamespacedCollectionVerbs (t * testing.T ) {
568- // expectedClusterMissingRules are the missing rules expected at cluster scope
569- // when cluster collection verbs are configured as "list", "watch".
570- expectedClusterMissingRules := ScopedPolicyRules {
571- Namespace : "" ,
572- MissingRules : []rbacv1.PolicyRule {
573- {
574- Verbs : []string {"list" , "watch" },
575- APIGroups : []string {"" },
576- Resources : []string {"services" },
577- ResourceNames : []string (nil ),
578- NonResourceURLs : []string (nil )},
579- {
580- Verbs : []string {"list" , "watch" },
581- APIGroups : []string {"rbac.authorization.k8s.io" },
582- Resources : []string {"rolebindings" },
583- ResourceNames : []string (nil ),
584- NonResourceURLs : []string (nil )},
585- {
586- Verbs : []string {"list" , "watch" },
587- APIGroups : []string {"rbac.authorization.k8s.io" },
588- Resources : []string {"roles" },
589- ResourceNames : []string (nil ),
590- NonResourceURLs : []string (nil ),
591- },
592- },
593- }
567+ func TestPreAuthorize_NamespacedCollectionVerbs (t * testing.T ) {
568+ // With namespacedCollectionVerbs now being a fixed variable (containing "create"),
569+ // this test verifies that "create" permissions are always checked at the namespace level.
594570
595- t .Run ("no namespaced collection verbs option omits namespaced collection rules " , func (t * testing.T ) {
571+ t .Run ("create verb is always checked as namespaced collection verb " , func (t * testing.T ) {
596572 fakeClient := setupFakeClient (limitedClusterRole )
597573 preAuth := NewRBACPreAuthorizer (fakeClient , WithClusterCollectionVerbs ("list" , "watch" ))
598574 missingRules , err := preAuth .PreAuthorize (context .TODO (), testUser , strings .NewReader (testManifest ))
599575 require .NoError (t , err )
600- // Without namespaced collection verbs, no "create" rules from collection verbs should appear,
601- // but object verbs (get, patch, update, delete) and escalation checks still apply
602- require .Equal (t , []ScopedPolicyRules {
603- expectedClusterMissingRules ,
604- {
605- Namespace : "test-namespace" ,
606- MissingRules : []rbacv1.PolicyRule {
607- {
608- Verbs : []string {"create" },
609- APIGroups : []string {"*" },
610- Resources : []string {"certificates" }},
611- {
612- Verbs : []string {"delete" , "get" , "patch" , "update" },
613- APIGroups : []string {"" },
614- Resources : []string {"services" },
615- ResourceNames : []string {"test-service" }},
616- {
617- Verbs : []string {"delete" , "get" , "patch" , "update" },
618- APIGroups : []string {"rbac.authorization.k8s.io" },
619- Resources : []string {"rolebindings" },
620- ResourceNames : []string {"test-extension-binding" }},
621- {
622- Verbs : []string {"delete" , "get" , "patch" , "update" },
623- APIGroups : []string {"rbac.authorization.k8s.io" },
624- Resources : []string {"roles" },
625- ResourceNames : []string {"test-extension-role" }},
626- {
627- Verbs : []string {"watch" },
628- APIGroups : []string {"*" },
629- Resources : []string {"serviceaccounts" },
630- },
631- },
632- },
633- }, missingRules )
634- })
635-
636- t .Run ("namespaced collection verbs option checks those verbs per namespace" , func (t * testing.T ) {
637- fakeClient := setupFakeClient (limitedClusterRole )
638- preAuth := NewRBACPreAuthorizer (fakeClient , WithClusterCollectionVerbs ("list" , "watch" ), WithNamespacedCollectionVerbs ("create" , "deletecollection" ))
639- missingRules , err := preAuth .PreAuthorize (context .TODO (), testUser , strings .NewReader (testManifest ))
640- require .NoError (t , err )
641- // Should have cluster-scoped missing rules plus namespaced rules with both create and deletecollection.
642- // Note: "certificates" with apiGroup "*" comes from the escalation check on the Role, not
643- // from namespaced collection verbs, so it only has "create".
644- require .Equal (t , []ScopedPolicyRules {
645- expectedClusterMissingRules ,
646- {
647- Namespace : "test-namespace" ,
648- MissingRules : []rbacv1.PolicyRule {
649- {
650- Verbs : []string {"create" , "deletecollection" },
651- APIGroups : []string {"" },
652- Resources : []string {"services" }},
653- {
654- Verbs : []string {"create" , "deletecollection" },
655- APIGroups : []string {"rbac.authorization.k8s.io" },
656- Resources : []string {"rolebindings" }},
657- {
658- Verbs : []string {"create" , "deletecollection" },
659- APIGroups : []string {"rbac.authorization.k8s.io" },
660- Resources : []string {"roles" }},
661- {
662- Verbs : []string {"create" },
663- APIGroups : []string {"*" },
664- Resources : []string {"certificates" }},
665- {
666- Verbs : []string {"delete" , "get" , "patch" , "update" },
667- APIGroups : []string {"" },
668- Resources : []string {"services" },
669- ResourceNames : []string {"test-service" }},
670- {
671- Verbs : []string {"delete" , "get" , "patch" , "update" },
672- APIGroups : []string {"rbac.authorization.k8s.io" },
673- Resources : []string {"rolebindings" },
674- ResourceNames : []string {"test-extension-binding" }},
675- {
676- Verbs : []string {"delete" , "get" , "patch" , "update" },
677- APIGroups : []string {"rbac.authorization.k8s.io" },
678- Resources : []string {"roles" },
679- ResourceNames : []string {"test-extension-role" }},
680- {
681- Verbs : []string {"watch" },
682- APIGroups : []string {"*" },
683- Resources : []string {"serviceaccounts" },
684- },
685- },
686- },
687- }, missingRules )
576+ // The "create" verb should always appear in missing rules because it's part of the
577+ // namespacedCollectionVerbs. This test verifies expectedSingleNamespaceMissingRules
578+ // which includes "create" verbs for namespace-scoped resources.
579+ require .Equal (t , expectedSingleNamespaceMissingRules , missingRules )
688580 })
689581
690- t .Run ("privileged user with custom namespaced collection verbs succeeds" , func (t * testing.T ) {
582+ t .Run ("privileged user with namespaced collection verbs succeeds" , func (t * testing.T ) {
691583 fakeClient := setupFakeClient (privilegedClusterRole )
692- preAuth := NewRBACPreAuthorizer (fakeClient , WithNamespacedCollectionVerbs ( "create" , "deletecollection" ) )
584+ preAuth := NewRBACPreAuthorizer (fakeClient )
693585 missingRules , err := preAuth .PreAuthorize (context .TODO (), testUser , strings .NewReader (testManifest ))
694586 require .NoError (t , err )
695587 require .Equal (t , []ScopedPolicyRules {}, missingRules )
0 commit comments