Skip to content

Commit a8ee987

Browse files
statikmergify[bot]
authored andcommitted
fix(iam): support NotActions/NotResources (#964) (#3677)
Signed-off-by: Elliot Murphy <statik@users.noreply.github.com>
1 parent 6eabe6d commit a8ee987

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

packages/@aws-cdk/aws-iam/lib/policy-statement.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,20 @@ export class PolicyStatement {
1414
public effect: Effect;
1515

1616
private action = new Array<any>();
17+
private notaction = new Array<any>();
1718
private principal: { [key: string]: any[] } = {};
1819
private resource = new Array<any>();
20+
private notresource = new Array<any>();
1921
private condition: { [key: string]: any } = { };
2022

2123
constructor(props: PolicyStatementProps = {}) {
2224
this.effect = props.effect || Effect.ALLOW;
2325

2426
this.addActions(...props.actions || []);
27+
this.addNotActions(...props.notactions || []);
2528
this.addPrincipals(...props.principals || []);
2629
this.addResources(...props.resources || []);
30+
this.addNotResources(...props.notresources || []);
2731
if (props.conditions !== undefined) {
2832
this.addConditions(props.conditions);
2933
}
@@ -37,6 +41,10 @@ export class PolicyStatement {
3741
this.action.push(...actions);
3842
}
3943

44+
public addNotActions(...notactions: string[]) {
45+
this.notaction.push(...notactions);
46+
}
47+
4048
//
4149
// Principal
4250
//
@@ -98,6 +106,10 @@ export class PolicyStatement {
98106
this.resource.push(...arns);
99107
}
100108

109+
public addNotResources(...arns: string[]) {
110+
this.notresource.push(...arns);
111+
}
112+
101113
/**
102114
* Adds a ``"*"`` resource to this statement.
103115
*/
@@ -142,10 +154,12 @@ export class PolicyStatement {
142154
public toStatementJson(): any {
143155
return noUndef({
144156
Action: _norm(this.action),
157+
NotAction: _norm(this.notaction),
145158
Condition: _norm(this.condition),
146159
Effect: _norm(this.effect),
147160
Principal: _normPrincipal(this.principal),
148161
Resource: _norm(this.resource),
162+
NotResource: _norm(this.notresource),
149163
Sid: _norm(this.sid),
150164
});
151165

@@ -229,6 +243,13 @@ export interface PolicyStatementProps {
229243
*/
230244
readonly actions?: string[];
231245

246+
/**
247+
* List of not actions to add to the statement
248+
*
249+
* @default - no actions
250+
*/
251+
readonly notactions?: string[];
252+
232253
/**
233254
* List of principals to add to the statement
234255
*
@@ -239,10 +260,17 @@ export interface PolicyStatementProps {
239260
/**
240261
* Resource ARNs to add to the statement
241262
*
242-
* @default - no principals
263+
* @default - no resources
243264
*/
244265
readonly resources?: string[];
245266

267+
/**
268+
* NotResource ARNs to add to the statement
269+
*
270+
* @default - no resources
271+
*/
272+
readonly notresources?: string[];
273+
246274
/**
247275
* Conditions to add to the statement
248276
*

packages/@aws-cdk/aws-iam/test/test.policy-document.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,26 @@ export = {
4343
const p1 = new PolicyStatement();
4444
p1.addActions('sqs:SendMessage');
4545
p1.addResources('*');
46+
p1.addNotResources('arn:aws:sqs:us-east-1:123456789012:forbidden_queue');
4647

4748
const p2 = new PolicyStatement();
4849
p2.effect = Effect.DENY;
4950
p2.addActions('cloudformation:CreateStack');
5051

52+
const p3 = new PolicyStatement();
53+
p3.effect = Effect.ALLOW;
54+
p3.addNotActions('cloudformation:UpdateTerminationProtection');
55+
5156
doc.addStatements(p1);
5257
doc.addStatements(p2);
58+
doc.addStatements(p3);
5359

5460
test.deepEqual(stack.resolve(doc), {
5561
Version: '2012-10-17',
5662
Statement:
57-
[ { Effect: 'Allow', Action: 'sqs:SendMessage', Resource: '*' },
58-
{ Effect: 'Deny', Action: 'cloudformation:CreateStack' } ] });
63+
[{ Effect: 'Allow', Action: 'sqs:SendMessage', Resource: '*', NotResource: 'arn:aws:sqs:us-east-1:123456789012:forbidden_queue' },
64+
{ Effect: 'Deny', Action: 'cloudformation:CreateStack' },
65+
{ Effect: 'Allow', NotAction: 'cloudformation:UpdateTerminationProtection' } ] });
5966

6067
test.done();
6168
},

0 commit comments

Comments
 (0)