@@ -14,14 +14,13 @@ import (
1414
1515 "github.com/aws/aws-sdk-go/aws"
1616 "github.com/aws/aws-sdk-go/service/cognitoidentityprovider"
17+ "github.com/aws/aws-sdk-go/service/cognitoidentityprovider/cognitoidentityprovideriface"
1718 "github.com/aws/aws-sdk-go/service/eks"
1819 "github.com/aws/aws-sdk-go/service/eks/eksiface"
1920
2021 . "github.com/onsi/ginkgo"
2122 . "github.com/onsi/gomega"
2223
23- "github.com/pkg/errors"
24-
2524 // Register the OIDC provider
2625 _ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
2726
@@ -184,12 +183,12 @@ func createOIDCClientset(eksAPI eksiface.EKSAPI, o *OIDCConfig, clusterName stri
184183 Name : aws .String (clusterName ),
185184 })
186185 if err != nil {
187- return nil , errors . Wrap ( err , "describing cluster" )
186+ return nil , fmt . Errorf ( "describing cluster: %w" , err )
188187 }
189188
190189 certData , err := base64 .StdEncoding .DecodeString (* cluster .Cluster .CertificateAuthority .Data )
191190 if err != nil {
192- return nil , errors . Wrap ( err , "unexpected error decoding certificate authority data" )
191+ return nil , fmt . Errorf ( "unexpected error decoding certificate authority data: %w" , err )
193192 }
194193
195194 config := clientcmdapi.Config {
@@ -223,15 +222,58 @@ func createOIDCClientset(eksAPI eksiface.EKSAPI, o *OIDCConfig, clusterName stri
223222
224223 clientConfig , err := clientcmd .NewDefaultClientConfig (config , & clientcmd.ConfigOverrides {}).ClientConfig ()
225224 if err != nil {
226- return nil , errors . Wrap ( err , "creating default client config" )
225+ return nil , fmt . Errorf ( "creating default client config: %w" , err )
227226 }
228227
229228 return kubernetes .NewForConfig (clientConfig )
230229}
231230
231+ type userPoolClient struct {
232+ userPoolID * string
233+ clientID * string
234+ }
235+
232236func setupCognitoProvider (clusterName , region string ) (* OIDCConfig , error ) {
233237 c := cognitoidentityprovider .New (NewSession (region ))
234238
239+ userPoolClient , err := createCognitoUserPoolClient (c , clusterName )
240+ if err != nil {
241+ return nil , err
242+ }
243+
244+ userPass , err := password .Generate (10 , 2 , 3 , false , false )
245+ if err != nil {
246+ return nil , fmt .Errorf ("generating password: %w" , err )
247+ }
248+
249+ clientUsername := aws .String (fmt .Sprintf ("%s@weave.works" , clusterName ))
250+ if err := createCognitoUserGroup (c , userPoolClient .userPoolID , clientUsername , userPass ); err != nil {
251+ return nil , err
252+ }
253+
254+ auth , err := c .AdminInitiateAuth (& cognitoidentityprovider.AdminInitiateAuthInput {
255+ AuthFlow : aws .String (cognitoidentityprovider .AuthFlowTypeAdminUserPasswordAuth ),
256+ AuthParameters : map [string ]* string {
257+ "USERNAME" : clientUsername ,
258+ "PASSWORD" : aws .String (userPass ),
259+ },
260+ ClientId : userPoolClient .clientID ,
261+ UserPoolId : userPoolClient .userPoolID ,
262+ })
263+
264+ if err != nil {
265+ return nil , fmt .Errorf ("initiating auth: %w" , err )
266+ }
267+
268+ return & OIDCConfig {
269+ clientID : * userPoolClient .clientID ,
270+ idToken : * auth .AuthenticationResult .IdToken ,
271+ refreshToken : * auth .AuthenticationResult .RefreshToken ,
272+ idpIssuerURL : fmt .Sprintf ("https://cognito-idp.%s.amazonaws.com/%s" , region , * userPoolClient .userPoolID ),
273+ }, nil
274+ }
275+
276+ func createCognitoUserPoolClient (c cognitoidentityprovideriface.CognitoIdentityProviderAPI , clusterName string ) (* userPoolClient , error ) {
235277 pool , err := c .CreateUserPool (& cognitoidentityprovider.CreateUserPoolInput {
236278 Policies : & cognitoidentityprovider.UserPoolPolicyType {
237279 PasswordPolicy : & cognitoidentityprovider.PasswordPolicyType {
@@ -247,7 +289,7 @@ func setupCognitoProvider(clusterName, region string) (*OIDCConfig, error) {
247289 })
248290
249291 if err != nil {
250- return nil , errors . Wrap ( err , "creating user pool" )
292+ return nil , fmt . Errorf ( "creating user pool: %w" , err )
251293 }
252294
253295 cleanupCognitoResources = func () error {
@@ -282,37 +324,33 @@ func setupCognitoProvider(clusterName, region string) (*OIDCConfig, error) {
282324 })
283325
284326 if err != nil {
285- return nil , errors . Wrap ( err , "creating user pool client" )
327+ return nil , fmt . Errorf ( "creating user pool client: %w" , err )
286328 }
329+ return & userPoolClient {
330+ userPoolID : pool .UserPool .Id ,
331+ clientID : client .UserPoolClient .ClientId ,
332+ }, nil
333+ }
287334
288- var (
289- userPoolID = pool .UserPool .Id
290- clientUsername = aws .String (fmt .Sprintf ("%s@weave.works" , clusterName ))
291- )
292-
293- _ , err = c .AdminCreateUser (& cognitoidentityprovider.AdminCreateUserInput {
335+ func createCognitoUserGroup (c cognitoidentityprovideriface.CognitoIdentityProviderAPI , userPoolID , clientUsername * string , userPass string ) error {
336+ _ , err := c .AdminCreateUser (& cognitoidentityprovider.AdminCreateUserInput {
294337 UserPoolId : userPoolID ,
295338 Username : clientUsername ,
296339 })
297340
298341 if err != nil {
299- return nil , errors .Wrap (err , "creating user" )
300- }
301-
302- pass , err := password .Generate (10 , 2 , 3 , false , false )
303- if err != nil {
304- return nil , errors .Wrap (err , "generating password" )
342+ return fmt .Errorf ("creating user: %w" , err )
305343 }
306344
307345 _ , err = c .AdminSetUserPassword (& cognitoidentityprovider.AdminSetUserPasswordInput {
308346 UserPoolId : userPoolID ,
309347 Username : clientUsername ,
310- Password : aws .String (pass ),
348+ Password : aws .String (userPass ),
311349 Permanent : aws .Bool (true ),
312350 })
313351
314352 if err != nil {
315- return nil , errors . Wrap ( err , "setting user password" )
353+ return fmt . Errorf ( "setting user password: %w" , err )
316354 }
317355
318356 groupName := aws .String (oidcGroupName )
@@ -323,7 +361,7 @@ func setupCognitoProvider(clusterName, region string) (*OIDCConfig, error) {
323361 })
324362
325363 if err != nil {
326- return nil , errors . Wrap ( err , "creating group" )
364+ return fmt . Errorf ( "creating group: %w" , err )
327365 }
328366
329367 _ , err = c .AdminAddUserToGroup (& cognitoidentityprovider.AdminAddUserToGroupInput {
@@ -333,30 +371,9 @@ func setupCognitoProvider(clusterName, region string) (*OIDCConfig, error) {
333371 })
334372
335373 if err != nil {
336- return nil , errors . Wrap ( err , "adding user to group" )
374+ return fmt . Errorf ( "adding user to group: %w" , err )
337375 }
338-
339- auth , err := c .AdminInitiateAuth (& cognitoidentityprovider.AdminInitiateAuthInput {
340- AuthFlow : aws .String (cognitoidentityprovider .AuthFlowTypeAdminUserPasswordAuth ),
341- AuthParameters : map [string ]* string {
342- "USERNAME" : clientUsername ,
343- "PASSWORD" : aws .String (pass ),
344- },
345- ClientId : client .UserPoolClient .ClientId ,
346- UserPoolId : userPoolID ,
347- })
348-
349- if err != nil {
350- return nil , errors .Wrap (err , "initiating auth" )
351- }
352-
353- return & OIDCConfig {
354- clientID : * client .UserPoolClient .ClientId ,
355- idToken : * auth .AuthenticationResult .IdToken ,
356- refreshToken : * auth .AuthenticationResult .RefreshToken ,
357- idpIssuerURL : fmt .Sprintf ("https://cognito-idp.%s.amazonaws.com/%s" , region , * userPoolID ),
358- }, nil
359-
376+ return nil
360377}
361378
362379func makeIdentityProviderClusterConfig (o * OIDCConfig , clusterName , region string ) string {
0 commit comments