Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(iam): support NotActions/NotResources (#964) #3677

Merged
merged 2 commits into from
Aug 16, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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[];
Copy link
Contributor

@jogold jogold Aug 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be called notActions? Camel case? @eladb

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, it should have been.


/**
* 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[];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be called notResources? Camel case?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, it should have been.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You want to change this before v1.5.0?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hell yes :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can quickly open a PR to fix the casing, The check against combining actions and notActions can wait after v1.5.0 I presume.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK you did it already :)


/**
* 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