Skip to content

Commit 29b379c

Browse files
authoredNov 24, 2021
feat(iam): support fromGroupName() for IAM groups (#17243)
IAM Policies and Users already support import by name. Extending same for Groups ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 5e4a219 commit 29b379c

File tree

3 files changed

+67
-13
lines changed

3 files changed

+67
-13
lines changed
 

‎packages/@aws-cdk/aws-iam/README.md

+24-5
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ Identity Pools Developer Guide].
329329

330330
The following examples defines an OpenID Connect provider. Two client IDs
331331
(audiences) are will be able to send authentication requests to
332-
https://openid/connect.
332+
<https://openid/connect>.
333333

334334
```ts
335335
const provider = new iam.OpenIdConnectProvider(this, 'MyProvider', {
@@ -439,6 +439,26 @@ const user = iam.User.fromUserAttributes(this, 'MyImportedUserByAttributes', {
439439
});
440440
```
441441

442+
## Groups
443+
444+
An IAM user group is a collection of IAM users. User groups let you specify permissions for multiple users.
445+
446+
```ts
447+
const group = new iam.Group(this, 'MyGroup');
448+
```
449+
450+
To import an existing group by ARN:
451+
452+
```ts
453+
const group = iam.Group.fromGroupArn(this, 'MyImportedGroupByArn', 'arn:aws:iam::account-id:group/group-name');
454+
```
455+
456+
To import an existing group by name [with path](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_identifiers.html#identifiers-friendly-names):
457+
458+
```ts
459+
const group = iam.Group.fromGroupName(this, 'MyImportedGroupByName', 'group-name');
460+
```
461+
442462
To add a user to a group (both for a new and imported user/group):
443463

444464
```ts
@@ -450,12 +470,11 @@ user.addToGroup(group);
450470
group.addUser(user);
451471
```
452472

453-
454473
## Features
455474

456-
* Policy name uniqueness is enforced. If two policies by the same name are attached to the same
475+
* Policy name uniqueness is enforced. If two policies by the same name are attached to the same
457476
principal, the attachment will fail.
458-
* Policy names are not required - the CDK logical ID will be used and ensured to be unique.
459-
* Policies are validated during synthesis to ensure that they have actions, and that policies
477+
* Policy names are not required - the CDK logical ID will be used and ensured to be unique.
478+
* Policies are validated during synthesis to ensure that they have actions, and that policies
460479
attached to IAM principals specify relevant resources, while policies attached to resources
461480
specify which IAM principals they apply to.

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

+18
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,24 @@ export class Group extends GroupBase {
156156
return new Import(scope, id);
157157
}
158158

159+
/**
160+
* Import an existing group by given name (with path).
161+
* This method has same caveats of `fromGroupArn`
162+
*
163+
* @param scope construct scope
164+
* @param id construct id
165+
* @param groupName the groupName (path included) of the existing group to import
166+
*/
167+
static fromGroupName(scope: Construct, id: string, groupName: string) {
168+
const groupArn = Stack.of(scope).formatArn({
169+
service: 'iam',
170+
region: '',
171+
resource: 'group',
172+
resourceName: groupName,
173+
});
174+
return Group.fromGroupArn(scope, id, groupArn);
175+
}
176+
159177
public readonly groupName: string;
160178
public readonly groupArn: string;
161179

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

+25-8
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ describe('IAM groups', () => {
2727
{
2828
MyGroupCBA54B1B: { Type: 'AWS::IAM::Group' },
2929
User1E278A736:
30-
{
31-
Type: 'AWS::IAM::User',
32-
Properties: { Groups: [{ Ref: 'MyGroupCBA54B1B' }] },
33-
},
30+
{
31+
Type: 'AWS::IAM::User',
32+
Properties: { Groups: [{ Ref: 'MyGroupCBA54B1B' }] },
33+
},
3434
User21F1486D1:
35-
{
36-
Type: 'AWS::IAM::User',
37-
Properties: { Groups: [{ Ref: 'MyGroupCBA54B1B' }] },
38-
},
35+
{
36+
Type: 'AWS::IAM::User',
37+
Properties: { Groups: [{ Ref: 'MyGroupCBA54B1B' }] },
38+
},
3939
},
4040
});
4141
});
@@ -56,4 +56,21 @@ describe('IAM groups', () => {
5656
],
5757
});
5858
});
59+
60+
test('groups imported by group name have valid arn', () => {
61+
// GIVEN
62+
const stack = new Stack();
63+
64+
// WHEN
65+
const group1 = Group.fromGroupName(stack, 'imported-group1', 'MyGroupName1');
66+
const group2 = Group.fromGroupName(stack, 'imported-group2', 'division/MyGroupName2');
67+
68+
// THEN
69+
expect(stack.resolve(group1.groupArn)).toStrictEqual({
70+
'Fn::Join': ['', ['arn:', { Ref: 'AWS::Partition' }, ':iam::', { Ref: 'AWS::AccountId' }, ':group/MyGroupName1']],
71+
});
72+
expect(stack.resolve(group2.groupArn)).toStrictEqual({
73+
'Fn::Join': ['', ['arn:', { Ref: 'AWS::Partition' }, ':iam::', { Ref: 'AWS::AccountId' }, ':group/division/MyGroupName2']],
74+
});
75+
});
5976
});

0 commit comments

Comments
 (0)
Please sign in to comment.