-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Conversation
There was a problem hiding this 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 { |
There was a problem hiding this comment.
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(...));
}
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
userPoolArn: string | ||
} | ||
|
||
class ImportedUserPool extends UserPoolBase { |
There was a problem hiding this comment.
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.'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
throw new Error('UserPool.fromUserPoolArn requires an arn with a resourceName.'); | |
throw new Error('invalid user pool ARN'); |
There was a problem hiding this 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.
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, | ||
}); |
There was a problem hiding this comment.
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
.
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'); |
There was a problem hiding this comment.
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 -
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'); |
Updated the code following the comments ! |
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 CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
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). |
…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*
…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*
Importing a
UserPool
usingfromUserPoolArn()
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