Skip to content

Commit ef2b480

Browse files
authored
fix(iam): role/group/user's path not included in ARN (#13258)
Solution to #13156 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 7d0e7ee commit ef2b480

File tree

6 files changed

+98
-7
lines changed

6 files changed

+98
-7
lines changed

packages/@aws-cdk/aws-iam/lib/group.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ export class Group extends GroupBase {
197197
region: '', // IAM is global in each partition
198198
service: 'iam',
199199
resource: 'group',
200-
resourceName: this.physicalName,
200+
// Removes leading slash from path
201+
resourceName: `${props.path ? props.path.substr(props.path.charAt(0) === '/' ? 1 : 0) : ''}${this.physicalName}`,
201202
});
202203
}
203204

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,8 @@ export class Role extends Resource implements IRole {
371371
region: '', // IAM is global in each partition
372372
service: 'iam',
373373
resource: 'role',
374-
resourceName: this.physicalName,
374+
// Removes leading slash from path
375+
resourceName: `${props.path ? props.path.substr(props.path.charAt(0) === '/' ? 1 : 0) : ''}${this.physicalName}`,
375376
});
376377
this.roleName = this.getResourceNameAttribute(role.ref);
377378
this.policyFragment = new ArnPrincipal(this.roleArn).policyFragment;

packages/@aws-cdk/aws-iam/lib/user.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,8 @@ export class User extends Resource implements IIdentity, IUser {
276276
region: '', // IAM is global in each partition
277277
service: 'iam',
278278
resource: 'user',
279-
resourceName: this.physicalName,
279+
// Removes leading slash from path
280+
resourceName: `${props.path ? props.path.substr(props.path.charAt(0) === '/' ? 1 : 0) : ''}${this.physicalName}`,
280281
});
281282

282283
this.policyFragment = new ArnPrincipal(this.userArn).policyFragment;

packages/@aws-cdk/aws-iam/test/group.test.ts

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Template } from '@aws-cdk/assertions';
2-
import { App, Stack } from '@aws-cdk/core';
2+
import { App, CfnResource, Stack } from '@aws-cdk/core';
33
import { Group, ManagedPolicy, User } from '../lib';
44

55
describe('IAM groups', () => {
@@ -74,3 +74,32 @@ describe('IAM groups', () => {
7474
});
7575
});
7676
});
77+
78+
test('cross-env group ARNs include path', () => {
79+
const app = new App();
80+
const groupStack = new Stack(app, 'group-stack', { env: { account: '123456789012', region: 'us-east-1' } });
81+
const referencerStack = new Stack(app, 'referencer-stack', { env: { region: 'us-east-2' } });
82+
const group = new Group(groupStack, 'Group', {
83+
path: '/sample/path/',
84+
groupName: 'sample-name',
85+
});
86+
new CfnResource(referencerStack, 'Referencer', {
87+
type: 'Custom::GroupReferencer',
88+
properties: { GroupArn: group.groupArn },
89+
});
90+
91+
Template.fromStack(referencerStack).hasResourceProperties('Custom::GroupReferencer', {
92+
GroupArn: {
93+
'Fn::Join': [
94+
'',
95+
[
96+
'arn:',
97+
{
98+
Ref: 'AWS::Partition',
99+
},
100+
':iam::123456789012:group/sample/path/sample-name',
101+
],
102+
],
103+
},
104+
});
105+
});

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

+32-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Template } from '@aws-cdk/assertions';
22
import { testDeprecated } from '@aws-cdk/cdk-build-tools';
3-
import { Duration, Stack, App } from '@aws-cdk/core';
3+
import { Duration, Stack, App, CfnResource } from '@aws-cdk/core';
44
import { AnyPrincipal, ArnPrincipal, CompositePrincipal, FederatedPrincipal, ManagedPolicy, PolicyStatement, Role, ServicePrincipal, User, Policy, PolicyDocument } from '../lib';
55

66
describe('IAM role', () => {
@@ -569,4 +569,34 @@ test('managed policy ARNs are deduplicated', () => {
569569
},
570570
],
571571
});
572-
});
572+
});
573+
574+
test('cross-env role ARNs include path', () => {
575+
const app = new App();
576+
const roleStack = new Stack(app, 'role-stack', { env: { account: '123456789012', region: 'us-east-1' } });
577+
const referencerStack = new Stack(app, 'referencer-stack', { env: { region: 'us-east-2' } });
578+
const role = new Role(roleStack, 'Role', {
579+
assumedBy: new ServicePrincipal('sns.amazonaws.com'),
580+
path: '/sample/path/',
581+
roleName: 'sample-name',
582+
});
583+
new CfnResource(referencerStack, 'Referencer', {
584+
type: 'Custom::RoleReferencer',
585+
properties: { RoleArn: role.roleArn },
586+
});
587+
588+
Template.fromStack(referencerStack).hasResourceProperties('Custom::RoleReferencer', {
589+
RoleArn: {
590+
'Fn::Join': [
591+
'',
592+
[
593+
'arn:',
594+
{
595+
Ref: 'AWS::Partition',
596+
},
597+
':iam::123456789012:role/sample/path/sample-name',
598+
],
599+
],
600+
},
601+
});
602+
});

packages/@aws-cdk/aws-iam/test/user.test.ts

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Template } from '@aws-cdk/assertions';
2-
import { App, SecretValue, Stack, Token } from '@aws-cdk/core';
2+
import { App, CfnResource, SecretValue, Stack, Token } from '@aws-cdk/core';
33
import { Group, ManagedPolicy, Policy, PolicyStatement, User } from '../lib';
44

55
describe('IAM user', () => {
@@ -289,3 +289,32 @@ describe('IAM user', () => {
289289
});
290290
});
291291
});
292+
293+
test('cross-env user ARNs include path', () => {
294+
const app = new App();
295+
const userStack = new Stack(app, 'user-stack', { env: { account: '123456789012', region: 'us-east-1' } });
296+
const referencerStack = new Stack(app, 'referencer-stack', { env: { region: 'us-east-2' } });
297+
const user = new User(userStack, 'User', {
298+
path: '/sample/path/',
299+
userName: 'sample-name',
300+
});
301+
new CfnResource(referencerStack, 'Referencer', {
302+
type: 'Custom::UserReferencer',
303+
properties: { UserArn: user.userArn },
304+
});
305+
306+
Template.fromStack(referencerStack).hasResourceProperties('Custom::UserReferencer', {
307+
UserArn: {
308+
'Fn::Join': [
309+
'',
310+
[
311+
'arn:',
312+
{
313+
Ref: 'AWS::Partition',
314+
},
315+
':iam::123456789012:user/sample/path/sample-name',
316+
],
317+
],
318+
},
319+
});
320+
});

0 commit comments

Comments
 (0)