Skip to content

Commit 7ad8bc0

Browse files
authored
chore(iam): throw warning if managed policies attached to a group exceeds 10 (#24385)
Throw a warning if more than 10 managed policies are attached to an IAM group. Added [IAM and AWS STS quotas, name requirements, and character limits](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_iam-quotas.html#reference_iam-quotas-entities) to documentation indicating current quotas. Closes #24085. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent c7b71bd commit 7ad8bc0

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

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

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ArnFormat, Lazy, Resource, Stack } from '@aws-cdk/core';
1+
import { Annotations, ArnFormat, Lazy, Resource, Stack } from '@aws-cdk/core';
22
import { Construct } from 'constructs';
33
import { CfnGroup } from './iam.generated';
44
import { IIdentity } from './identity-base';
@@ -200,14 +200,25 @@ export class Group extends GroupBase {
200200
// Removes leading slash from path
201201
resourceName: `${props.path ? props.path.substr(props.path.charAt(0) === '/' ? 1 : 0) : ''}${this.physicalName}`,
202202
});
203+
204+
this.managedPoliciesExceededWarning();
203205
}
204206

205207
/**
206-
* Attaches a managed policy to this group.
208+
* Attaches a managed policy to this group. See [IAM and AWS STS quotas, name requirements, and character limits]
209+
* (https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_iam-quotas.html#reference_iam-quotas-entities)
210+
* for quota of managed policies attached to an IAM group.
207211
* @param policy The managed policy to attach.
208212
*/
209213
public addManagedPolicy(policy: IManagedPolicy) {
210214
if (this.managedPolicies.find(mp => mp === policy)) { return; }
211215
this.managedPolicies.push(policy);
216+
this.managedPoliciesExceededWarning();
217+
}
218+
219+
private managedPoliciesExceededWarning() {
220+
if (this.managedPolicies.length > 10) {
221+
Annotations.of(this).addWarning(`You added ${this.managedPolicies.length} to IAM Group ${this.physicalName}. The maximum number of managed policies attached to an IAM group is 10.`);
222+
}
212223
}
213224
}

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

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

@@ -103,3 +103,44 @@ test('cross-env group ARNs include path', () => {
103103
},
104104
});
105105
});
106+
107+
test('throw warning if attached managed policies exceed 10 in constructor', () => {
108+
// GIVEN
109+
const stack = new Stack();
110+
111+
// WHEN
112+
new Group(stack, 'MyGroup', {
113+
groupName: 'MyGroup',
114+
managedPolicies: [
115+
ManagedPolicy.fromAwsManagedPolicyName('0'),
116+
ManagedPolicy.fromAwsManagedPolicyName('1'),
117+
ManagedPolicy.fromAwsManagedPolicyName('2'),
118+
ManagedPolicy.fromAwsManagedPolicyName('3'),
119+
ManagedPolicy.fromAwsManagedPolicyName('4'),
120+
ManagedPolicy.fromAwsManagedPolicyName('5'),
121+
ManagedPolicy.fromAwsManagedPolicyName('6'),
122+
ManagedPolicy.fromAwsManagedPolicyName('7'),
123+
ManagedPolicy.fromAwsManagedPolicyName('8'),
124+
ManagedPolicy.fromAwsManagedPolicyName('9'),
125+
ManagedPolicy.fromAwsManagedPolicyName('10'),
126+
],
127+
});
128+
129+
Annotations.fromStack(stack).hasWarning('*', 'You added 11 to IAM Group MyGroup. The maximum number of managed policies attached to an IAM group is 10.');
130+
});
131+
132+
test('throw warning if attached managed policies exceed 10 when calling `addManagedPolicy`', () => {
133+
// GIVEN
134+
const stack = new Stack();
135+
136+
// WHEN
137+
const group = new Group(stack, 'MyGroup', {
138+
groupName: 'MyGroup',
139+
});
140+
141+
for (let i = 0; i <= 11; i++) {
142+
group.addManagedPolicy(ManagedPolicy.fromAwsManagedPolicyName(i.toString()));
143+
}
144+
145+
Annotations.fromStack(stack).hasWarning('/Default/MyGroup', 'You added 11 to IAM Group MyGroup. The maximum number of managed policies attached to an IAM group is 10.');
146+
});

0 commit comments

Comments
 (0)