Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cognito): imported userpool not retaining environment from arn #13715

Merged
merged 5 commits into from
Mar 27, 2021

Conversation

DaWyz
Copy link
Contributor

@DaWyz DaWyz commented Mar 21, 2021

Importing a UserPool using fromUserPoolArn() method would not retain the account/region from the ARN and would instead use the environment from the scope it is imported to.

Closes #13691


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@gitpod-io
Copy link

gitpod-io bot commented Mar 21, 2021

@github-actions github-actions bot added the @aws-cdk/aws-cognito Related to Amazon Cognito label Mar 21, 2021
Copy link
Contributor

@nija-at nija-at left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DaWyz - Thanks for submitting this PR.

See my comments below.

userPoolArn: string
}

class ImportedUserPool extends UserPoolBase {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you try and retain the original structure of keeping the 'Import' class local to the fromXXX() methods?

You should be able to move the full implementation to within fromUserPoolArn() and call this method from fromUserPoolId(). Something like -

public static fromUserPoolId(...) {
  return fromUserPoolArn(..., Stack.of(scope).formatArn(...));
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nija-at Are we sure we want to do this ?
I'm asking for the two following reasons:

  • I've been asked in the past to extract the Import class (That was for EventBus) and it's done that way in a few other modules (search for class Imported in the codebase).
  • I would need to find a different name for scope and id since I'm getting eslint(@typescript-eslint/no-shadow) eslint error.

Copy link
Contributor

@nija-at nija-at Mar 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer the class to be inline since this acts as an added check to ensure that we don't export this class into the public API. If we extracted this out, it would be easy to just add an 'export' keyword and could easily be missed in reviews.
However if you feel strongly on extracting and keeping this separate, that's fine with me.

The shadow variable issue is minor. We can easily find different variable names.

However, I would either rename of not use the UserPoolAttributes interface. This is reserved for specific types of import APIs - https://github.com/aws/aws-cdk/blob/master/docs/DESIGN_GUIDELINES.md#from-attributes - and I wouldn't want this to be incorrectly used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. I'm moving the Imported class within the fromUserPoolArn. Thanks for the explanation @nija-at

}

/**
* Import an existing user pool based on its ARN.
*/
public static fromUserPoolArn(scope: Construct, id: string, userPoolArn: string): IUserPool {
return UserPool.fromUserPoolId(scope, id, Stack.of(scope).parseArn(userPoolArn).resourceName!);
return new ImportedUserPool(scope, id, {
userPoolId: Stack.of(scope).parseArn(userPoolArn).resourceName!,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid the !.

Instead add a conditional that throws an error is resourceName is undefined.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the code to check for resourceName + added a test.

@nija-at nija-at changed the title fix(cognito): imported userpool not retraining arn environment fix(cognito): imported userpool not retaining environment from arn Mar 22, 2021
@mergify mergify bot dismissed nija-at’s stale review March 23, 2021 02:17

Pull request has been modified.

userPoolArn: string
}

class ImportedUserPool extends UserPoolBase {
Copy link
Contributor

@nija-at nija-at Mar 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer the class to be inline since this acts as an added check to ensure that we don't export this class into the public API. If we extracted this out, it would be easy to just add an 'export' keyword and could easily be missed in reviews.
However if you feel strongly on extracting and keeping this separate, that's fine with me.

The shadow variable issue is minor. We can easily find different variable names.

However, I would either rename of not use the UserPoolAttributes interface. This is reserved for specific types of import APIs - https://github.com/aws/aws-cdk/blob/master/docs/DESIGN_GUIDELINES.md#from-attributes - and I wouldn't want this to be incorrectly used.

let userPoolId = Stack.of(scope).parseArn(userPoolArn).resourceName;

if (!userPoolId) {
throw new Error('UserPool.fromUserPoolArn requires an arn with a resourceName.');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new Error('UserPool.fromUserPoolArn requires an arn with a resourceName.');
throw new Error('invalid user pool ARN');

@mergify mergify bot dismissed nija-at’s stale review March 26, 2021 05:53

Pull request has been modified.

Copy link
Contributor

@nija-at nija-at left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the change @DaWyz. A couple of small suggestions.

Comment on lines 682 to 703
class ImportedUserPool extends UserPoolBase {
public readonly userPoolArn: string;
public readonly userPoolId: string;
constructor(_scope: Construct, _id: string, attrs: {
userPoolId: string;
userPoolArn: string
}) {
const arnParts = Stack.of(_scope).parseArn(attrs.userPoolArn);
super(_scope, _id, {
account: arnParts.account,
region: arnParts.region,
});

this.userPoolArn = attrs.userPoolArn;
this.userPoolId = attrs.userPoolId;
}
}

return new ImportedUserPool(scope, id, {
userPoolId: userPoolId,
userPoolArn: userPoolArn,
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this simpler? Fewer lines of code to achieve the same thing and removes the need for _scope and _id.

Suggested change
class ImportedUserPool extends UserPoolBase {
public readonly userPoolArn: string;
public readonly userPoolId: string;
constructor(_scope: Construct, _id: string, attrs: {
userPoolId: string;
userPoolArn: string
}) {
const arnParts = Stack.of(_scope).parseArn(attrs.userPoolArn);
super(_scope, _id, {
account: arnParts.account,
region: arnParts.region,
});
this.userPoolArn = attrs.userPoolArn;
this.userPoolId = attrs.userPoolId;
}
}
return new ImportedUserPool(scope, id, {
userPoolId: userPoolId,
userPoolArn: userPoolArn,
});
class ImportedUserPool extends UserPoolBase {
public readonly userPoolArn = userPoolArn;
public readonly userPoolId = userPoolId;
constructor() {
const arnParts = Stack.of(scope).parseArn(userPoolArn);
super(scope, id, {
account: arnParts.account,
region: arnParts.region,
});
}
}
return new ImportedUserPool();

// WHEN
const pool = UserPool.fromUserPoolArn(stack, 'userpool', userPoolArn);
expect(pool.env.account).toEqual('0123456789012');
expect(pool.env.region).toEqual('us-east-1');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also verify the arn -

Suggested change
expect(pool.env.region).toEqual('us-east-1');
expect(pool.env.region).toEqual('us-east-1');
expect(pool.userPoolArn).toEqual('arn:aws:cognito-idp:us-east-1:0123456789012:userpool/test-user-pool');

@mergify mergify bot dismissed nija-at’s stale review March 26, 2021 16:59

Pull request has been modified.

@DaWyz
Copy link
Contributor Author

DaWyz commented Mar 26, 2021

Updated the code following the comments !

@DaWyz DaWyz requested a review from nija-at March 26, 2021 23:52
@mergify
Copy link
Contributor

mergify bot commented Mar 27, 2021

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject89A8053A-LhjRyN9kxr8o
  • Commit ID: 677fbb0
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify
Copy link
Contributor

mergify bot commented Mar 27, 2021

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify mergify bot merged commit aa9fd9c into aws:master Mar 27, 2021
@DaWyz DaWyz deleted the DaWyz/fix-imported-userpool-env branch March 27, 2021 14:37
hollanddd pushed a commit to hollanddd/aws-cdk that referenced this pull request Mar 31, 2021
…ws#13715)

Importing a `UserPool` using `fromUserPoolArn()` method would not retain the account/region from the ARN and would instead use the environment from the scope it is imported to.

Closes aws#13691

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
hollanddd pushed a commit to hollanddd/aws-cdk that referenced this pull request Aug 26, 2021
…ws#13715)

Importing a `UserPool` using `fromUserPoolArn()` method would not retain the account/region from the ARN and would instead use the environment from the scope it is imported to.

Closes aws#13691

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-cognito Related to Amazon Cognito
Projects
None yet
Development

Successfully merging this pull request may close these issues.

(aws-appsync): userPoolConfig authorizer ignores the userpool region
3 participants