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): Merge multiple principals correctly #983

Merged
merged 6 commits into from
Oct 23, 2018
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
36 changes: 30 additions & 6 deletions packages/@aws-cdk/aws-iam/lib/policy-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export abstract class PolicyPrincipal {
*/
export class PrincipalPolicyFragment {
constructor(
public readonly principalJson: any,
public readonly principalJson: { [key: string]: any },
public readonly conditions: {[key: string]: any} = {}) {
}
}
Expand Down Expand Up @@ -155,7 +155,7 @@ export class Anyone extends PolicyPrincipal {
public readonly accountId = '*';

public policyFragment(): PrincipalPolicyFragment {
return new PrincipalPolicyFragment('*');
return new PrincipalPolicyFragment({ AWS: this.accountId });
RomainMuller marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand All @@ -164,7 +164,7 @@ export class Anyone extends PolicyPrincipal {
*/
export class PolicyStatement extends Token {
private action = new Array<any>();
private principal = new Array<any>();
private principal: { [key: string]: any[] } = {};
private resource = new Array<any>();
private condition: { [key: string]: any } = { };
private effect?: PolicyStatementEffect;
Expand Down Expand Up @@ -197,12 +197,20 @@ export class PolicyStatement extends Token {
* Indicates if this permission has a "Principal" section.
*/
public get hasPrincipal() {
return this.principal && this.principal.length > 0;
return Object.keys(this.principal).length > 0;
}

public addPrincipal(principal: PolicyPrincipal): PolicyStatement {
const fragment = principal.policyFragment();
this.principal.push(fragment.principalJson);
for (const key of Object.keys(fragment.principalJson)) {
RomainMuller marked this conversation as resolved.
Show resolved Hide resolved
this.principal[key] = this.principal[key] || [];
const value = fragment.principalJson[key];
if (Array.isArray(value)) {
this.principal[key].push(...value);
RomainMuller marked this conversation as resolved.
Show resolved Hide resolved
} else {
this.principal[key].push(value);
}
}
this.addConditions(fragment.conditions);
return this;
}
Expand Down Expand Up @@ -330,7 +338,7 @@ export class PolicyStatement extends Token {
Action: _norm(this.action),
Condition: _norm(this.condition),
Effect: _norm(this.effect),
Principal: _norm(this.principal),
Principal: _normPrincipal(this.principal),
Resource: _norm(this.resource),
Sid: _norm(this.sid),
};
Expand Down Expand Up @@ -361,6 +369,22 @@ export class PolicyStatement extends Token {

return values;
}

function _normPrincipal(principal: { [key: string]: any[] }) {
const keys = Object.keys(principal);
if (keys.length === 0) { return undefined; }
const result: any = {};
for (const key of keys) {
const normVal = _norm(principal[key]);
if (normVal) {
result[key] = normVal;
}
}
if (Object.keys(result).length === 1 && result.AWS === '*') {
RomainMuller marked this conversation as resolved.
Show resolved Hide resolved
return '*';
}
return result;
}
}
}

Expand Down
16 changes: 16 additions & 0 deletions packages/@aws-cdk/aws-iam/test/test.policy-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,22 @@ export = {
test.done();
},

'addAwsAccountPrincipal can be used multiple times'(test: Test) {
const p = new PolicyStatement();
p.addAwsAccountPrincipal('1234');
p.addAwsAccountPrincipal('5678');
test.deepEqual(resolve(p), {
Effect: 'Allow',
Principal: {
AWS: [
{ 'Fn::Join': ['', ['arn:', { Ref: 'AWS::Partition' }, ':iam::1234:root']] },
{ 'Fn::Join': ['', ['arn:', { Ref: 'AWS::Partition' }, ':iam::5678:root']] }
]
}
});
test.done();
},

'hasResource': {
'false if there are no resources'(test: Test) {
test.equal(new PolicyStatement().hasResource, false, 'hasResource should be false for an empty permission');
Expand Down