Skip to content

Commit

Permalink
fix(iam): cannot add multiple conditions using same operator
Browse files Browse the repository at this point in the history
<!--For every issue your PR resolves, add `fixes #<issue>` or `closes #<issue>`-->

<!--Shout out to collaborators.-->

<!--If your PR includes breaking changes, uncomment and fill in the following (notice how multiple breaking changes should be formatted):-->
<!--
BREAKING CHANGE: Description of what broke and how to achieve this behavior now<br>
\* **module-name:** Another breaking change<br>
\* **module-name:** Yet another breaking change
-->

<!--IMPORTANT: This section cannot contain any additional markdown headers (#)-->
  • Loading branch information
benbryant0 authored Mar 10, 2020
1 parent 03df1f1 commit 348a952
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/@aws-cdk/aws-iam/lib/policy-statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ export class PolicyStatement {
* Add a condition to the Policy
*/
public addCondition(key: string, value: any) {
this.condition[key] = value;
const existingValue = this.condition[key];
this.condition[key] = existingValue ? { ...existingValue, ...value } : value;
}

/**
Expand Down
15 changes: 15 additions & 0 deletions packages/@aws-cdk/aws-iam/test/policy-document.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,4 +572,19 @@ describe('IAM polocy document', () => {

expect(stack.resolve(doc1)).toEqual(stack.resolve(doc2));
});

test('adding another condition with the same operator does not delete the original', () => {
const stack = new Stack();

const p = new PolicyStatement();

p.addCondition('StringEquals', { 'kms:ViaService': 'service' });

p.addAccountCondition('12221121221');

expect(stack.resolve(p.toStatementJson())).toEqual({
Effect: 'Allow',
Condition: { StringEquals: { 'kms:ViaService': 'service', 'sts:ExternalId': '12221121221' } }
});
});
});

0 comments on commit 348a952

Please sign in to comment.