Skip to content

Commit 4c9ce9b

Browse files
authored
fix(iam): Role.fromRoleName fails on AWS created roles (#25389)
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*
1 parent f2294ba commit 4c9ce9b

File tree

2 files changed

+0
-24
lines changed

2 files changed

+0
-24
lines changed

packages/aws-cdk-lib/aws-iam/lib/role.ts

-13
Original file line numberDiff line numberDiff line change
@@ -320,10 +320,6 @@ export class Role extends Resource implements IRole {
320320
* @param options allow customizing the behavior of the returned role
321321
*/
322322
public static fromRoleName(scope: Construct, id: string, roleName: string, options: FromRoleNameOptions = {}) {
323-
// Validate the role name only if not a token
324-
if (!Token.isUnresolved(roleName)) {
325-
this.validateRoleName(roleName);
326-
}
327323
return Role.fromRoleArn(scope, id, Stack.of(scope).formatArn({
328324
region: '',
329325
service: 'iam',
@@ -373,15 +369,6 @@ export class Role extends Resource implements IRole {
373369
});
374370
}
375371

376-
private static validateRoleName(roleName: string) {
377-
// https://docs.aws.amazon.com/IAM/latest/APIReference/API_CreateRole.html
378-
const regexp: RegExp = /[\w+=,.@-]+/;
379-
const matches = regexp.exec(roleName);
380-
if (!(matches && matches.length === 1 && matches[0] === roleName)) {
381-
throw new Error(`The role name ${roleName} does not match the IAM conventions.`);
382-
}
383-
}
384-
385372
public readonly grantPrincipal: IPrincipal = this;
386373
public readonly principalAccount: string | undefined = this.env.account;
387374

packages/aws-cdk-lib/aws-iam/test/role.test.ts

-11
Original file line numberDiff line numberDiff line change
@@ -1294,14 +1294,3 @@ test('cross-env role ARNs include path', () => {
12941294
},
12951295
});
12961296
});
1297-
1298-
test('fromRoleName should validate role name (only if not a token)', () => {
1299-
const app = new App();
1300-
const stack = new Stack(app, 'MyStack');
1301-
expect(() => {
1302-
Role.fromRoleName(stack, 'Invalid role name', 'arn:aws:iam::***:role/myrole');
1303-
}).toThrow(/does not match the IAM conventions/);
1304-
expect(() => {
1305-
Role.fromRoleName(stack, 'Token', '${Token[TOKEN.26]}');
1306-
}).not.toThrow();
1307-
});

0 commit comments

Comments
 (0)