@@ -19,7 +19,6 @@ package authentication
1919import (
2020 "context"
2121 "fmt"
22- "math/rand"
2322 "testing"
2423 "time"
2524
@@ -39,7 +38,7 @@ import (
3938 tenancyv1alpha1 "github.com/kcp-dev/kcp/sdk/apis/tenancy/v1alpha1"
4039 kcpclientset "github.com/kcp-dev/kcp/sdk/client/clientset/versioned/cluster"
4140 kcptesting "github.com/kcp-dev/kcp/sdk/testing"
42- "github.com/kcp-dev/kcp/sdk/testing/third_party/library-go/crypto "
41+ "github.com/kcp-dev/kcp/test/e2e/fixtures/authfixtures "
4342 "github.com/kcp-dev/kcp/test/e2e/framework"
4443)
4544
@@ -61,19 +60,19 @@ func TestWorkspaceOIDC(t *testing.T) {
6160
6261 // start a two mock OIDC servers that will listen on random ports
6362 // (only for discovery and keyset handling, no actual login workflows)
64- mockA , ca := startMockOIDC (t , server )
65- mockB , _ := startMockOIDC (t , server )
63+ mockA , ca := authfixtures . StartMockOIDC (t , server )
64+ mockB , _ := authfixtures . StartMockOIDC (t , server )
6665
6766 // setup a new workspace auth config that uses mockoidc's server, one for
6867 // each of our mockoidc servers
69- authConfigA := createWorkspaceAuthentication (t , ctx , kcpClusterClient , baseWsPath , mockA , ca )
70- authConfigB := createWorkspaceAuthentication (t , ctx , kcpClusterClient , baseWsPath , mockB , ca )
68+ authConfigA := authfixtures . CreateWorkspaceOIDCAuthentication (t , ctx , kcpClusterClient , baseWsPath , mockA , ca )
69+ authConfigB := authfixtures . CreateWorkspaceOIDCAuthentication (t , ctx , kcpClusterClient , baseWsPath , mockB , ca )
7170
7271 // use these configs in new WorkspaceTypes and create one extra workspace type that allows
7372 // both mockoidc issuers
74- wsTypeA := createWorkspaceType (t , ctx , kcpClusterClient , baseWsPath , authConfigA )
75- wsTypeB := createWorkspaceType (t , ctx , kcpClusterClient , baseWsPath , authConfigB )
76- wsTypeC := createWorkspaceType (t , ctx , kcpClusterClient , baseWsPath , authConfigA , authConfigB )
73+ wsTypeA := authfixtures . CreateWorkspaceType (t , ctx , kcpClusterClient , baseWsPath , "with-oidc-a" , authConfigA )
74+ wsTypeB := authfixtures . CreateWorkspaceType (t , ctx , kcpClusterClient , baseWsPath , "with-oidc-b" , authConfigB )
75+ wsTypeC := authfixtures . CreateWorkspaceType (t , ctx , kcpClusterClient , baseWsPath , "with-oidc-c" , authConfigA , authConfigB )
7776
7877 // create a new workspace with our new type
7978 t .Log ("Creating Workspaces..." )
@@ -89,15 +88,15 @@ func TestWorkspaceOIDC(t *testing.T) {
8988 }
9089
9190 // grant permissions to random users and groups
92- grantWorkspaceAccess (t , ctx , kubeClusterClient , teamAPath , []rbacv1.Subject {{
91+ authfixtures . GrantWorkspaceAccess (t , ctx , kubeClusterClient , teamAPath , "grant-oidc-user" , "cluster-admin" , []rbacv1.Subject {{
9392 Kind : "User" ,
94939594 }, {
9695 Kind : "Group" ,
9796 Name : "oidc:developers" ,
9897 }})
9998
100- grantWorkspaceAccess (t , ctx , kubeClusterClient , teamBPath , []rbacv1.Subject {{
99+ authfixtures . GrantWorkspaceAccess (t , ctx , kubeClusterClient , teamBPath , "grant-oidc-user" , "cluster-admin" , []rbacv1.Subject {{
101100 Kind : "User" ,
102101103102 }, {
@@ -108,7 +107,7 @@ func TestWorkspaceOIDC(t *testing.T) {
108107 Name : "oidc:developers" ,
109108 }})
110109
111- grantWorkspaceAccess (t , ctx , kubeClusterClient , teamCPath , []rbacv1.Subject {{
110+ authfixtures . GrantWorkspaceAccess (t , ctx , kubeClusterClient , teamCPath , "grant-oidc-user" , "cluster-admin" , []rbacv1.Subject {{
112111 Kind : "User" ,
113112114113 }, {
@@ -224,7 +223,7 @@ func TestWorkspaceOIDC(t *testing.T) {
224223 t .Run (testcase .name , func (t * testing.T ) {
225224 t .Parallel ()
226225
227- token := createOIDCToken (t , testcase .mock , testcase .username , testcase .email , testcase .groups )
226+ token := authfixtures . CreateOIDCToken (t , testcase .mock , testcase .username , testcase .email , testcase .groups )
228227
229228 client , err := kcpkubernetesclientset .NewForConfig (framework .ConfigWithToken (token , kcpConfig ))
230229 require .NoError (t , err )
@@ -268,9 +267,9 @@ func TestUserScope(t *testing.T) {
268267 kcpClusterClient , err := kcpclientset .NewForConfig (kcpConfig )
269268 require .NoError (t , err )
270269
271- mock , ca := startMockOIDC (t , server )
272- authConfig := createWorkspaceAuthentication (t , ctx , kcpClusterClient , baseWsPath , mock , ca )
273- wsType := createWorkspaceType (t , ctx , kcpClusterClient , baseWsPath , authConfig )
270+ mock , ca := authfixtures . StartMockOIDC (t , server )
271+ authConfig := authfixtures . CreateWorkspaceOIDCAuthentication (t , ctx , kcpClusterClient , baseWsPath , mock , ca )
272+ wsType := authfixtures . CreateWorkspaceType (t , ctx , kcpClusterClient , baseWsPath , "with-oidc" , authConfig )
274273
275274 // create a new workspace with our new type
276275 t .Log ("Creating Workspaces..." )
@@ -287,12 +286,12 @@ func TestUserScope(t *testing.T) {
287286 expectedGroups = append (expectedGroups , "oidc:" + group )
288287 }
289288
290- grantWorkspaceAccess (t , ctx , kubeClusterClient , teamPath , []rbacv1.Subject {{
289+ authfixtures . GrantWorkspaceAccess (t , ctx , kubeClusterClient , teamPath , "grant-oidc-user" , "cluster-admin" , []rbacv1.Subject {{
291290 Kind : "User" ,
292291 Name : "oidc:" + userEmail ,
293292 }})
294293
295- token := createOIDCToken (t , mock , userName , userEmail , userGroups )
294+ token := authfixtures . CreateOIDCToken (t , mock , userName , userEmail , userGroups )
296295
297296 peterClient , err := kcpkubernetesclientset .NewForConfig (framework .ConfigWithToken (token , kcpConfig ))
298297 require .NoError (t , err )
@@ -334,7 +333,7 @@ func TestForbiddenSystemAccess(t *testing.T) {
334333 kcpClusterClient , err := kcpclientset .NewForConfig (kcpConfig )
335334 require .NoError (t , err )
336335
337- mock , ca := startMockOIDC (t , server )
336+ mock , ca := authfixtures . StartMockOIDC (t , server )
338337
339338 // create an evil AuthConfig that would not prefix OIDC-provided groups, theoretically allowing
340339 // users to become part of system groups.
@@ -345,7 +344,7 @@ func TestForbiddenSystemAccess(t *testing.T) {
345344 },
346345 Spec : tenancyv1alpha1.WorkspaceAuthenticationConfigurationSpec {
347346 JWT : []tenancyv1alpha1.JWTAuthenticator {
348- mockJWTAuthenticator (t , mock , ca , "" , "" ),
347+ authfixtures . MockJWTAuthenticator (t , mock , ca , "" , "" ),
349348 },
350349 },
351350 }
@@ -354,20 +353,20 @@ func TestForbiddenSystemAccess(t *testing.T) {
354353 _ , err = kcpClusterClient .Cluster (baseWsPath ).TenancyV1alpha1 ().WorkspaceAuthenticationConfigurations ().Create (ctx , authConfig , metav1.CreateOptions {})
355354 require .NoError (t , err )
356355
357- wsType := createWorkspaceType (t , ctx , kcpClusterClient , baseWsPath , authConfig .Name )
356+ wsType := authfixtures . CreateWorkspaceType (t , ctx , kcpClusterClient , baseWsPath , "with-oidc" , authConfig .Name )
358357
359358 // create a new workspace with our new type
360359 t .Log ("Creating Workspaces..." )
361360 teamPath , _ := kcptesting .NewWorkspaceFixture (t , server , baseWsPath , kcptesting .WithName ("team-a" ), kcptesting .WithType (baseWsPath , tenancyv1alpha1 .WorkspaceTypeName (wsType )))
362361
363362 // give a dummy user access
364- grantWorkspaceAccess (t , ctx , kubeClusterClient , teamPath , []rbacv1.Subject {{
363+ authfixtures . GrantWorkspaceAccess (t , ctx , kubeClusterClient , teamPath , "grant-oidc-user" , "cluster-admin" , []rbacv1.Subject {{
365364 Kind : "User" ,
366365367366 }})
368367
369368 // wait until the authenticator is ready
370- token := createOIDCToken (
t ,
mock ,
"dummy" ,
"[email protected] " ,
nil )
369+ token := authfixtures . CreateOIDCToken (
t ,
mock ,
"dummy" ,
"[email protected] " ,
nil )
371370
372371 client , err := kcpkubernetesclientset .NewForConfig (framework .ConfigWithToken (token , kcpConfig ))
373372 require .NoError (t , err )
@@ -406,7 +405,7 @@ func TestForbiddenSystemAccess(t *testing.T) {
406405 t .Run (testcase .name , func (t * testing.T ) {
407406 t .Parallel ()
408407
409- token := createOIDCToken (t , mock , testcase .username , testcase .email , testcase .groups )
408+ token := authfixtures . CreateOIDCToken (t , mock , testcase .username , testcase .email , testcase .groups )
410409
411410 client , err := kcpkubernetesclientset .NewForConfig (framework .ConfigWithToken (token , kcpConfig ))
412411 require .NoError (t , err )
@@ -545,69 +544,3 @@ func TestAcceptableWorkspaceAuthenticationConfigurations(t *testing.T) {
545544 })
546545 }
547546}
548-
549- func createWorkspaceAuthentication (t * testing.T , ctx context.Context , client kcpclientset.ClusterInterface , workspace logicalcluster.Path , mock * mockoidc.MockOIDC , ca * crypto.CA ) string {
550- name := fmt .Sprintf ("mockoidc-%d" , rand .Int ())
551-
552- // setup a new workspace auth config that uses mockoidc's server
553- authConfig := & tenancyv1alpha1.WorkspaceAuthenticationConfiguration {
554- ObjectMeta : metav1.ObjectMeta {
555- Name : name ,
556- },
557- Spec : tenancyv1alpha1.WorkspaceAuthenticationConfigurationSpec {
558- JWT : []tenancyv1alpha1.JWTAuthenticator {
559- mockJWTAuthenticator (t , mock , ca , "oidc:" , "oidc:" ),
560- },
561- },
562- }
563-
564- t .Logf ("Creating WorkspaceAuthenticationConfguration %s..." , name )
565- _ , err := client .Cluster (workspace ).TenancyV1alpha1 ().WorkspaceAuthenticationConfigurations ().Create (ctx , authConfig , metav1.CreateOptions {})
566- require .NoError (t , err )
567-
568- return name
569- }
570-
571- func createWorkspaceType (t * testing.T , ctx context.Context , client kcpclientset.ClusterInterface , workspace logicalcluster.Path , authConfigNames ... string ) string {
572- name := fmt .Sprintf ("with-oidc-%d" , rand .Int ())
573-
574- configs := []tenancyv1alpha1.AuthenticationConfigurationReference {}
575- for _ , name := range authConfigNames {
576- configs = append (configs , tenancyv1alpha1.AuthenticationConfigurationReference {
577- Name : name ,
578- })
579- }
580-
581- // setup a new workspace auth config that uses mockoidc's server
582- wsType := & tenancyv1alpha1.WorkspaceType {
583- ObjectMeta : metav1.ObjectMeta {
584- Name : name ,
585- },
586- Spec : tenancyv1alpha1.WorkspaceTypeSpec {
587- AuthenticationConfigurations : configs ,
588- },
589- }
590-
591- t .Logf ("Creating WorkspaceType %s..." , name )
592- _ , err := client .Cluster (workspace ).TenancyV1alpha1 ().WorkspaceTypes ().Create (ctx , wsType , metav1.CreateOptions {})
593- require .NoError (t , err )
594-
595- return name
596- }
597-
598- func grantWorkspaceAccess (t * testing.T , ctx context.Context , client kcpkubernetesclientset.ClusterInterface , workspace logicalcluster.Path , subjects []rbacv1.Subject ) {
599- crb := & rbacv1.ClusterRoleBinding {
600- ObjectMeta : metav1.ObjectMeta {
601- Name : "allow-oidc-user" ,
602- },
603- RoleRef : rbacv1.RoleRef {
604- Kind : "ClusterRole" ,
605- Name : "cluster-admin" ,
606- },
607- Subjects : subjects ,
608- }
609-
610- t .Log ("Creating ClusterRoleBinding..." )
611- _ , err := client .Cluster (workspace ).RbacV1 ().ClusterRoleBindings ().Create (ctx , crb , metav1.CreateOptions {})
612- require .NoError (t , err )
613- }
0 commit comments