Skip to content

Commit

Permalink
docs(iam): explain restrictions on addCondition (#20165)
Browse files Browse the repository at this point in the history
Multiple calls to `addCondition` aren't as smart as one might want. Clarify that they aren't.

Fixes #20158.


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
rix0rrr committed May 3, 2022
1 parent 443fb2a commit 4fe6717
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions packages/@aws-cdk/aws-iam/lib/policy-statement.ts
Expand Up @@ -310,6 +310,27 @@ export class PolicyStatement {

/**
* Add a condition to the Policy
*
* If multiple calls are made to add a condition with the same operator and field, only
* the last one wins. For example:
*
* ```ts
* declare const stmt: iam.PolicyStatement;
*
* stmt.addCondition('StringEquals', { 'aws:SomeField': '1' });
* stmt.addCondition('StringEquals', { 'aws:SomeField': '2' });
* ```
*
* Will end up with the single condition `StringEquals: { 'aws:SomeField': '2' }`.
*
* If you meant to add a condition to say that the field can be *either* `1` or `2`, write
* this:
*
* ```ts
* declare const stmt: iam.PolicyStatement;
*
* stmt.addCondition('StringEquals', { 'aws:SomeField': ['1', '2'] });
* ```
*/
public addCondition(key: string, value: Condition) {
const existingValue = this.condition[key];
Expand All @@ -318,6 +339,8 @@ export class PolicyStatement {

/**
* Add multiple conditions to the Policy
*
* See the `addCondition` function for a caveat on calling this method multiple times.
*/
public addConditions(conditions: Conditions) {
Object.keys(conditions).map(key => {
Expand All @@ -327,6 +350,8 @@ export class PolicyStatement {

/**
* Add a condition that limits to a given account
*
* This method can only be called once: subsequent calls will overwrite earlier calls.
*/
public addAccountCondition(accountId: string) {
this.addCondition('StringEquals', { 'sts:ExternalId': accountId });
Expand Down

0 comments on commit 4fe6717

Please sign in to comment.