@@ -2543,6 +2543,20 @@ func TestBindingRemoval(t *testing.T) {
2543
2543
// Test_ValidateSecondaryAuthorization tests a ValidatingAdmissionPolicy that performs secondary authorization checks
2544
2544
// for both users and service accounts.
2545
2545
func Test_ValidateSecondaryAuthorization (t * testing.T ) {
2546
+ generic .PolicyRefreshInterval = 10 * time .Millisecond
2547
+ featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , genericfeatures .ValidatingAdmissionPolicy , true )
2548
+ server , err := apiservertesting .StartTestServer (t , nil , []string {
2549
+ "--enable-admission-plugins" , "ValidatingAdmissionPolicy" ,
2550
+ "--authorization-mode=RBAC" ,
2551
+ "--anonymous-auth" ,
2552
+ }, framework .SharedEtcd ())
2553
+ if err != nil {
2554
+ t .Fatal (err )
2555
+ }
2556
+ defer server .TearDownFn ()
2557
+
2558
+ // For test set up such as creating policies, bindings and RBAC rules.
2559
+ adminClient := clientset .NewForConfigOrDie (server .ClientConfig )
2546
2560
testcases := []struct {
2547
2561
name string
2548
2562
rbac * rbacv1.PolicyRule
@@ -2595,7 +2609,7 @@ func Test_ValidateSecondaryAuthorization(t *testing.T) {
2595
2609
},
2596
2610
}
2597
2611
2598
- for _ , testcase := range testcases {
2612
+ for i , testcase := range testcases {
2599
2613
t .Run (testcase .name , func (t * testing.T ) {
2600
2614
clients := map [string ]func (t * testing.T , adminClient * clientset.Clientset , clientConfig * rest.Config , rules []rbacv1.PolicyRule ) * clientset.Clientset {
2601
2615
"user" : secondaryAuthorizationUserClient ,
@@ -2604,20 +2618,6 @@ func Test_ValidateSecondaryAuthorization(t *testing.T) {
2604
2618
2605
2619
for clientName , clientFn := range clients {
2606
2620
t .Run (clientName , func (t * testing.T ) {
2607
- featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , genericfeatures .ValidatingAdmissionPolicy , true )
2608
- server , err := apiservertesting .StartTestServer (t , nil , []string {
2609
- "--enable-admission-plugins" , "ValidatingAdmissionPolicy" ,
2610
- "--authorization-mode=RBAC" ,
2611
- "--anonymous-auth" ,
2612
- }, framework .SharedEtcd ())
2613
- if err != nil {
2614
- t .Fatal (err )
2615
- }
2616
- defer server .TearDownFn ()
2617
-
2618
- // For test set up such as creating policies, bindings and RBAC rules.
2619
- adminClient := clientset .NewForConfigOrDie (server .ClientConfig )
2620
-
2621
2621
// Principal is always allowed to create and update namespaces so that the admission requests to test
2622
2622
// authorization expressions can be sent by the principal.
2623
2623
rules := []rbacv1.PolicyRule {{
@@ -2639,21 +2639,24 @@ func Test_ValidateSecondaryAuthorization(t *testing.T) {
2639
2639
testcase .extraAccountFn (t , adminClient , server .ClientConfig , extraRules )
2640
2640
}
2641
2641
2642
+ policyName := fmt .Sprintf ("%s-%s-%d" , "validate-authz" , clientName , i )
2642
2643
policy := withWaitReadyConstraintAndExpression (withValidations ([]admissionregistrationv1.Validation {
2643
2644
{
2644
2645
Expression : testcase .expression ,
2645
2646
},
2646
- }, withFailurePolicy (admissionregistrationv1 .Fail , withNamespaceMatch (makePolicy ("validate-authz" )))))
2647
+ }, withFailurePolicy (admissionregistrationv1 .Fail , withNamespaceMatch (makePolicy (policyName )))))
2647
2648
if _ , err := adminClient .AdmissionregistrationV1 ().ValidatingAdmissionPolicies ().Create (context .TODO (), policy , metav1.CreateOptions {}); err != nil {
2648
2649
t .Fatal (err )
2649
2650
}
2650
- if err := createAndWaitReady (t , adminClient , makeBinding ("validate-authz-binding" , "validate-authz" , "" ), nil ); err != nil {
2651
+ policyBindingName := fmt .Sprintf ("%s-%s" , policyName , "binding" )
2652
+ policyBinding := makeBinding (policyBindingName , policyName , "" )
2653
+ if err := createAndWaitReady (t , adminClient , policyBinding , nil ); err != nil {
2651
2654
t .Fatal (err )
2652
2655
}
2653
2656
2654
2657
ns := & v1.Namespace {
2655
2658
ObjectMeta : metav1.ObjectMeta {
2656
- Name : " test-authz" ,
2659
+ Name : fmt . Sprintf ( "%s-%s-%d" , " test-authz", clientName , i ) ,
2657
2660
},
2658
2661
}
2659
2662
_ , err = client .CoreV1 ().Namespaces ().Create (context .TODO (), ns , metav1.CreateOptions {})
@@ -2663,6 +2666,9 @@ func Test_ValidateSecondaryAuthorization(t *testing.T) {
2663
2666
expected = metav1 .StatusReasonInvalid
2664
2667
}
2665
2668
checkFailureReason (t , err , expected )
2669
+ if err := cleanupPolicy (t , adminClient , policy , policyBinding ); err != nil {
2670
+ t .Fatalf ("error while cleaning up policy and its bindings: %v" , err )
2671
+ }
2666
2672
})
2667
2673
}
2668
2674
})
@@ -2839,7 +2845,7 @@ func serviceAccountClient(namespace, name string) clientFn {
2839
2845
return func (t * testing.T , adminClient * clientset.Clientset , clientConfig * rest.Config , rules []rbacv1.PolicyRule ) * clientset.Clientset {
2840
2846
clientConfig = rest .CopyConfig (clientConfig )
2841
2847
sa , err := adminClient .CoreV1 ().ServiceAccounts (namespace ).Create (context .TODO (), & v1.ServiceAccount {ObjectMeta : metav1.ObjectMeta {Name : name }}, metav1.CreateOptions {})
2842
- if err != nil {
2848
+ if err != nil && ! apierrors . IsAlreadyExists ( err ) {
2843
2849
t .Fatal (err )
2844
2850
}
2845
2851
uid := sa .UID
0 commit comments