Skip to content

Commit d0163f8

Browse files
authored
fix(iam): AccountPrincipal accepts values which aren't account IDs (#20292)
Changed the type of accountId in AccountPrincipal constructor to string from any fixes #20288 ---- ### All Submissions: * [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/master/INTEGRATION_TESTS.md)? * [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent f7693e3 commit d0163f8

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

Diff for: packages/@aws-cdk/aws-iam/lib/principals.ts

+3
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,9 @@ export class AccountPrincipal extends ArnPrincipal {
394394
*/
395395
constructor(public readonly accountId: any) {
396396
super(new StackDependentToken(stack => `arn:${stack.partition}:iam::${accountId}:root`).toString());
397+
if (!cdk.Token.isUnresolved(accountId) && typeof accountId !== 'string') {
398+
throw new Error('accountId should be of type string');
399+
}
397400
this.principalAccount = accountId;
398401
}
399402

Diff for: packages/@aws-cdk/aws-iam/test/principals.test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,10 @@ test('AccountPrincipal can specify an organization', () => {
294294
});
295295
});
296296

297+
test('Passing non-string as accountId parameter in AccountPrincipal constructor should throw error', () => {
298+
expect(() => new iam.AccountPrincipal(1234)).toThrowError('accountId should be of type string');
299+
});
300+
297301
test('ServicePrincipal in agnostic stack generates lookup table', () => {
298302
// GIVEN
299303
const stack = new Stack();

0 commit comments

Comments
 (0)