Skip to content

Commit

Permalink
fix(iam): support NotActions/NotResources (#964) (#3677)
Browse files Browse the repository at this point in the history
Signed-off-by: Elliot Murphy <statik@users.noreply.github.com>
  • Loading branch information
statik authored and mergify[bot] committed Aug 16, 2019
1 parent 6eabe6d commit a8ee987
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
30 changes: 29 additions & 1 deletion packages/@aws-cdk/aws-iam/lib/policy-statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,20 @@ export class PolicyStatement {
public effect: Effect;

private action = new Array<any>();
private notaction = new Array<any>();
private principal: { [key: string]: any[] } = {};
private resource = new Array<any>();
private notresource = new Array<any>();
private condition: { [key: string]: any } = { };

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

this.addActions(...props.actions || []);
this.addNotActions(...props.notactions || []);
this.addPrincipals(...props.principals || []);
this.addResources(...props.resources || []);
this.addNotResources(...props.notresources || []);
if (props.conditions !== undefined) {
this.addConditions(props.conditions);
}
Expand All @@ -37,6 +41,10 @@ export class PolicyStatement {
this.action.push(...actions);
}

public addNotActions(...notactions: string[]) {
this.notaction.push(...notactions);
}

//
// Principal
//
Expand Down Expand Up @@ -98,6 +106,10 @@ export class PolicyStatement {
this.resource.push(...arns);
}

public addNotResources(...arns: string[]) {
this.notresource.push(...arns);
}

/**
* Adds a ``"*"`` resource to this statement.
*/
Expand Down Expand Up @@ -142,10 +154,12 @@ export class PolicyStatement {
public toStatementJson(): any {
return noUndef({
Action: _norm(this.action),
NotAction: _norm(this.notaction),
Condition: _norm(this.condition),
Effect: _norm(this.effect),
Principal: _normPrincipal(this.principal),
Resource: _norm(this.resource),
NotResource: _norm(this.notresource),
Sid: _norm(this.sid),
});

Expand Down Expand Up @@ -229,6 +243,13 @@ export interface PolicyStatementProps {
*/
readonly actions?: string[];

/**
* List of not actions to add to the statement
*
* @default - no actions
*/
readonly notactions?: string[];

/**
* List of principals to add to the statement
*
Expand All @@ -239,10 +260,17 @@ export interface PolicyStatementProps {
/**
* Resource ARNs to add to the statement
*
* @default - no principals
* @default - no resources
*/
readonly resources?: string[];

/**
* NotResource ARNs to add to the statement
*
* @default - no resources
*/
readonly notresources?: string[];

/**
* Conditions to add to the statement
*
Expand Down
11 changes: 9 additions & 2 deletions packages/@aws-cdk/aws-iam/test/test.policy-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,26 @@ export = {
const p1 = new PolicyStatement();
p1.addActions('sqs:SendMessage');
p1.addResources('*');
p1.addNotResources('arn:aws:sqs:us-east-1:123456789012:forbidden_queue');

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

const p3 = new PolicyStatement();
p3.effect = Effect.ALLOW;
p3.addNotActions('cloudformation:UpdateTerminationProtection');

doc.addStatements(p1);
doc.addStatements(p2);
doc.addStatements(p3);

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

test.done();
},
Expand Down

0 comments on commit a8ee987

Please sign in to comment.