Skip to content

Commit

Permalink
fix(cognito): imported userpool not retaining environment from arn (#…
Browse files Browse the repository at this point in the history
…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 #13691

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
DaWyz authored Mar 27, 2021
1 parent 6f6e079 commit aa9fd9c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 20 deletions.
37 changes: 27 additions & 10 deletions packages/@aws-cdk/aws-cognito/lib/user-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -660,22 +660,39 @@ export class UserPool extends UserPoolBase {
* Import an existing user pool based on its id.
*/
public static fromUserPoolId(scope: Construct, id: string, userPoolId: string): IUserPool {
class Import extends UserPoolBase {
public readonly userPoolId = userPoolId;
public readonly userPoolArn = Stack.of(this).formatArn({
service: 'cognito-idp',
resource: 'userpool',
resourceName: userPoolId,
});
}
return new Import(scope, id);
let userPoolArn = Stack.of(scope).formatArn({
service: 'cognito-idp',
resource: 'userpool',
resourceName: userPoolId,
});

return UserPool.fromUserPoolArn(scope, id, userPoolArn);
}

/**
* 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!);
const arnParts = Stack.of(scope).parseArn(userPoolArn);

if (!arnParts.resourceName) {
throw new Error('invalid user pool ARN');
}

const userPoolId = arnParts.resourceName;

class ImportedUserPool extends UserPoolBase {
public readonly userPoolArn = userPoolArn;
public readonly userPoolId = userPoolId;
constructor() {
super(scope, id, {
account: arnParts.account,
region: arnParts.region,
});
}
}

return new ImportedUserPool();
}

/**
Expand Down
39 changes: 29 additions & 10 deletions packages/@aws-cdk/aws-cognito/test/user-pool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,17 +211,36 @@ describe('User Pool', () => {
// WHEN
const pool = UserPool.fromUserPoolArn(stack, 'userpool', userPoolArn);
expect(pool.userPoolId).toEqual('test-user-pool');
expect(stack.resolve(pool.userPoolArn)).toEqual({
'Fn::Join': ['', [
'arn:',
{ Ref: 'AWS::Partition' },
':cognito-idp:',
{ Ref: 'AWS::Region' },
':',
{ Ref: 'AWS::AccountId' },
':userpool/test-user-pool',
]],
expect(stack.resolve(pool.userPoolArn)).toEqual('arn:aws:cognito-idp:us-east-1:0123456789012:userpool/test-user-pool');
});

test('import using arn without resourceName fails', () => {
// GIVEN
const stack = new Stack();
const userPoolArn = 'arn:aws:cognito-idp:us-east-1:0123456789012:*';

// WHEN
expect(() => {
UserPool.fromUserPoolArn(stack, 'userpool', userPoolArn);
}).toThrowError(/invalid user pool ARN/);
});

test('import from different account region using arn', () => {
// GIVEN
const userPoolArn = 'arn:aws:cognito-idp:us-east-1:0123456789012:userpool/test-user-pool';

const stack = new Stack(undefined, undefined, {
env: {
account: '111111111111',
region: 'us-east-2',
},
});

// WHEN
const pool = UserPool.fromUserPoolArn(stack, 'userpool', userPoolArn);
expect(pool.env.account).toEqual('0123456789012');
expect(pool.env.region).toEqual('us-east-1');
expect(pool.userPoolArn).toEqual('arn:aws:cognito-idp:us-east-1:0123456789012:userpool/test-user-pool');
});

test('support tags', () => {
Expand Down

0 comments on commit aa9fd9c

Please sign in to comment.