Skip to content

Commit ad1b797

Browse files
authored
fix(cognito-identitypool-alpha): validation error if provided id is a token (#30882)
### Issue # (if applicable) Closes #29780. Closes #28184. ### Description of changes Skips validations if provided id is an unresolved token. ### Description of how you validated changes Added unit tests not to throw errors even if the resolved value is incorrect. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 8d76778 commit ad1b797

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

packages/@aws-cdk/aws-cognito-identitypool-alpha/lib/identitypool.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
Stack,
1717
ArnFormat,
1818
Lazy,
19+
Token,
1920
} from 'aws-cdk-lib/core';
2021
import {
2122
Construct,
@@ -329,9 +330,15 @@ export class IdentityPool extends Resource implements IIdentityPool {
329330
if (!res) {
330331
throw new Error('Invalid Identity Pool ARN');
331332
}
332-
const idParts = res.split(':');
333-
if (!(idParts.length === 2)) throw new Error('Invalid Identity Pool Id: Identity Pool Ids must follow the format <region>:<id>');
334-
if (idParts[0] !== pool.region) throw new Error('Invalid Identity Pool Id: Region in Identity Pool Id must match stack region');
333+
if (!Token.isUnresolved(res)) {
334+
const idParts = res.split(':');
335+
if (!(idParts.length === 2)) {
336+
throw new Error('Invalid Identity Pool Id: Identity Pool Ids must follow the format <region>:<id>');
337+
}
338+
if (!Token.isUnresolved(pool.region) && idParts[0] !== pool.region) {
339+
throw new Error('Invalid Identity Pool Id: Region in Identity Pool Id must match stack region');
340+
}
341+
}
335342
class ImportedIdentityPool extends Resource implements IIdentityPool {
336343
public readonly identityPoolId = res;
337344
public readonly identityPoolArn = identityPoolArn;

packages/@aws-cdk/aws-cognito-identitypool-alpha/test/identitypool.test.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
} from 'aws-cdk-lib/aws-iam';
2020
import {
2121
Fn,
22+
Lazy,
2223
Stack,
2324
} from 'aws-cdk-lib';
2425
import {
@@ -203,14 +204,28 @@ describe('identity pool', () => {
203204
account: '1234567891011',
204205
},
205206
});
206-
expect(() => IdentityPool.fromIdentityPoolId(stack, 'idPoolIdError', 'idPool')).toThrowError('Invalid Identity Pool Id: Identity Pool Ids must follow the format <region>:<id>');
207-
expect(() => IdentityPool.fromIdentityPoolArn(stack, 'idPoolArnError', 'arn:aws:cognito-identity:my-region:1234567891011:identitypool\/your-region:idPool/')).toThrowError('Invalid Identity Pool Id: Region in Identity Pool Id must match stack region');
207+
expect(() => IdentityPool.fromIdentityPoolId(stack, 'idPoolIdError', 'idPool')).toThrow('Invalid Identity Pool Id: Identity Pool Ids must follow the format <region>:<id>');
208+
expect(() => IdentityPool.fromIdentityPoolId(stack, 'idPoolIdRegionError', 'your-region:idPool')).toThrow('Invalid Identity Pool Id: Region in Identity Pool Id must match stack region');
209+
expect(() => IdentityPool.fromIdentityPoolArn(stack, 'idPoolArnError', 'arn:aws:cognito-identity:my-region:1234567891011:identitypool\/your-region:idPool/')).toThrow('Invalid Identity Pool Id: Region in Identity Pool Id must match stack region');
208210
const idPool = IdentityPool.fromIdentityPoolId(stack, 'staticIdPool', 'my-region:idPool');
209211

210212
expect(idPool.identityPoolId).toEqual('my-region:idPool');
211213
expect(idPool.identityPoolArn).toMatch(/cognito-identity:my-region:1234567891011:identitypool\/my-region:idPool/);
212214
});
213215

216+
test('fromIdentityPoolId accept token', () => {
217+
const stack = new Stack();
218+
expect(() => IdentityPool.fromIdentityPoolId(stack, 'IdPool1', Lazy.string({ produce: () => 'lazy-id' }))).not.toThrow();
219+
expect(() => IdentityPool.fromIdentityPoolId(stack, 'IdPool2', 'id-region:pool-id')).not.toThrow();
220+
});
221+
222+
test('fromIdentityPoolArn accepts token', () => {
223+
const stack = new Stack();
224+
expect(() => IdentityPool.fromIdentityPoolArn(stack, 'IdPool1', Lazy.string({ produce: () => 'lazy-arn' }))).not.toThrow();
225+
expect(() => IdentityPool.fromIdentityPoolArn(stack, 'IdPool2', `arn:aws:cognito-identity:${stack.region}:${stack.account}:identitypool/id-region:pool-id`)).not.toThrow();
226+
expect(() => IdentityPool.fromIdentityPoolArn(stack, 'IdPool3', `arn:aws:cognito-identity:arn-region:${stack.account}:identitypool/${Lazy.string({ produce: () => 'lazy-region' })}:pool-id`)).not.toThrow();
227+
});
228+
214229
test('user pools are properly configured', () => {
215230
const stack = new Stack();
216231
const poolProvider = UserPoolIdentityProvider.fromProviderName(stack, 'poolProvider', 'poolProvider');

0 commit comments

Comments
 (0)