Skip to content

Commit

Permalink
fix(iam): Role.fromRoleName fails on AWS created roles (#25389)
Browse files Browse the repository at this point in the history
This reverts commit 637fc6a.

Fixes #25360

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
otaviomacedo authored May 2, 2023
1 parent f2294ba commit 4c9ce9b
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 24 deletions.
13 changes: 0 additions & 13 deletions packages/aws-cdk-lib/aws-iam/lib/role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,6 @@ export class Role extends Resource implements IRole {
* @param options allow customizing the behavior of the returned role
*/
public static fromRoleName(scope: Construct, id: string, roleName: string, options: FromRoleNameOptions = {}) {
// Validate the role name only if not a token
if (!Token.isUnresolved(roleName)) {
this.validateRoleName(roleName);
}
return Role.fromRoleArn(scope, id, Stack.of(scope).formatArn({
region: '',
service: 'iam',
Expand Down Expand Up @@ -373,15 +369,6 @@ export class Role extends Resource implements IRole {
});
}

private static validateRoleName(roleName: string) {
// https://docs.aws.amazon.com/IAM/latest/APIReference/API_CreateRole.html
const regexp: RegExp = /[\w+=,.@-]+/;
const matches = regexp.exec(roleName);
if (!(matches && matches.length === 1 && matches[0] === roleName)) {
throw new Error(`The role name ${roleName} does not match the IAM conventions.`);
}
}

public readonly grantPrincipal: IPrincipal = this;
public readonly principalAccount: string | undefined = this.env.account;

Expand Down
11 changes: 0 additions & 11 deletions packages/aws-cdk-lib/aws-iam/test/role.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1294,14 +1294,3 @@ test('cross-env role ARNs include path', () => {
},
});
});

test('fromRoleName should validate role name (only if not a token)', () => {
const app = new App();
const stack = new Stack(app, 'MyStack');
expect(() => {
Role.fromRoleName(stack, 'Invalid role name', 'arn:aws:iam::***:role/myrole');
}).toThrow(/does not match the IAM conventions/);
expect(() => {
Role.fromRoleName(stack, 'Token', '${Token[TOKEN.26]}');
}).not.toThrow();
});

0 comments on commit 4c9ce9b

Please sign in to comment.