Skip to content

Commit ec1c5b7

Browse files
rix0rrrElad Ben-Israel
authored and
Elad Ben-Israel
committedJul 6, 2019
fix(iam): fix managed policies for User (#3221)
Fix adding managed policies to a User upon creation. Rename the property for `Group`s. Fixes #2557. BREAKING CHANGE: `aws-iam.User` and `Group`: `managedPolicyArns` => `managedPolicies`.
1 parent d60d673 commit ec1c5b7

File tree

7 files changed

+60
-12
lines changed

7 files changed

+60
-12
lines changed
 

‎allowed-breaking-changes.txt

+2
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ removed:@aws-cdk/aws-elasticloadbalancingv2.ApplicationLoadBalancer.metricIPv6Pr
88
removed:@aws-cdk/aws-elasticloadbalancingv2.ApplicationLoadBalancer.metricIPv6RequestCount
99
removed:@aws-cdk/aws-elasticloadbalancingv2.ApplicationTargetGroup.metricIPv6RequestCount
1010
removed:@aws-cdk/core.Fn.getAZs
11+
removed:@aws-cdk/aws-iam.UserProps.managedPolicyArns
12+
removed:@aws-cdk/aws-iam.GroupProps.managedPolicyArns

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,13 @@ export interface GroupProps {
4040
readonly groupName?: string;
4141

4242
/**
43-
* A list of ARNs for managed policies associated with group.
43+
* A list managed policies associated with this role.
44+
*
45+
* You can add managed policies later using `attachManagedPolicy(policy)`.
4446
*
4547
* @default - No managed policies.
4648
*/
47-
readonly managedPolicyArns?: any[];
49+
readonly managedPolicies?: IManagedPolicy[];
4850

4951
/**
5052
* The path to the group. For more information about paths, see [IAM
@@ -130,7 +132,7 @@ export class Group extends GroupBase {
130132
physicalName: props.groupName,
131133
});
132134

133-
this.managedPolicies.push(...props.managedPolicyArns || []);
135+
this.managedPolicies.push(...props.managedPolicies || []);
134136

135137
const group = new CfnGroup(this, 'Resource', {
136138
groupName: this.physicalName,

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ export interface RoleProps {
3030
readonly externalId?: string;
3131

3232
/**
33-
* A list of ARNs for managed policies associated with this role.
33+
* A list of managed policies associated with this role.
34+
*
3435
* You can add managed policies later using `attachManagedPolicy(arn)`.
3536
*
3637
* @default - No managed policies.

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ export interface UserProps {
2424
readonly groups?: IGroup[];
2525

2626
/**
27-
* A list of ARNs for managed policies attacherd to this user.
28-
* You can use `addManagedPolicy(arn)` to attach a managed policy to this user.
27+
* A list managed policies associated with this role.
28+
*
29+
* You can add managed policies later using `attachManagedPolicy(policy)`.
2930
*
3031
* @default - No managed policies.
3132
*/
32-
readonly managedPolicyArns?: any[];
33+
readonly managedPolicies?: IManagedPolicy[];
3334

3435
/**
3536
* The path for the user name. For more information about paths, see IAM
@@ -108,6 +109,8 @@ export class User extends Resource implements IIdentity {
108109
physicalName: props.userName,
109110
});
110111

112+
this.managedPolicies.push(...props.managedPolicies || []);
113+
111114
const user = new CfnUser(this, 'Resource', {
112115
userName: this.physicalName,
113116
groups: undefinedIfEmpty(() => this.groups),

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

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { expect } from '@aws-cdk/assert';
1+
import { expect, haveResource } from '@aws-cdk/assert';
22
import { App, Stack } from '@aws-cdk/core';
33
import { Test } from 'nodeunit';
4-
import { Group, User } from '../lib';
4+
import { Group, ManagedPolicy, User } from '../lib';
55

66
export = {
77
'default group'(test: Test) {
@@ -35,4 +35,23 @@ export = {
3535
Properties: { Groups: [ { Ref: 'MyGroupCBA54B1B' } ] } } } });
3636
test.done();
3737
},
38+
39+
'create with managed policy'(test: Test) {
40+
// GIVEN
41+
const stack = new Stack();
42+
43+
// WHEN
44+
new Group(stack, 'MyGroup', {
45+
managedPolicies: [ManagedPolicy.fromAwsManagedPolicyName('asdf')]
46+
});
47+
48+
// THEN
49+
expect(stack).to(haveResource('AWS::IAM::Group', {
50+
ManagedPolicyArns: [
51+
{ "Fn::Join": [ "", [ "arn:", { Ref: "AWS::Partition" }, ":iam::aws:policy/asdf" ] ] }
52+
]
53+
}));
54+
55+
test.done();
56+
}
3857
};

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -288,5 +288,6 @@ export = {
288288
Roles: [ "MyRole" ]
289289
}));
290290
test.done();
291-
}
291+
},
292+
292293
};

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

+22-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { expect } from '@aws-cdk/assert';
1+
import { expect, haveResource } from '@aws-cdk/assert';
22
import { App, SecretValue, Stack } from '@aws-cdk/core';
33
import { Test } from 'nodeunit';
4-
import { User } from '../lib';
4+
import { ManagedPolicy, User } from '../lib';
55

66
export = {
77
'default user'(test: Test) {
@@ -32,6 +32,26 @@ export = {
3232
const app = new App();
3333
const stack = new Stack(app, 'MyStack');
3434
test.throws(() => new User(stack, 'MyUser', { passwordResetRequired: true }));
35+
test.done();
36+
},
37+
38+
'create with managed policy'(test: Test) {
39+
// GIVEN
40+
const app = new App();
41+
const stack = new Stack(app, 'MyStack');
42+
43+
// WHEN
44+
new User(stack, 'MyUser', {
45+
managedPolicies: [ManagedPolicy.fromAwsManagedPolicyName('asdf')]
46+
});
47+
48+
// THEN
49+
expect(stack).to(haveResource('AWS::IAM::User', {
50+
ManagedPolicyArns: [
51+
{ "Fn::Join": [ "", [ "arn:", { Ref: "AWS::Partition" }, ":iam::aws:policy/asdf" ] ] }
52+
]
53+
}));
54+
3555
test.done();
3656
}
3757
};

0 commit comments

Comments
 (0)