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

IAM: addCondition overwrites previous conditions #20158

Closed
llgerard opened this issue May 1, 2022 · 3 comments · Fixed by #20165
Closed

IAM: addCondition overwrites previous conditions #20158

llgerard opened this issue May 1, 2022 · 3 comments · Fixed by #20165
Labels
@aws-cdk/aws-iam Related to AWS Identity and Access Management bug This issue is a bug. cause/not-a-bug Not a bug (might still be a documentation issue, might still need work) effort/small Small work item – less than a day of effort p2

Comments

@llgerard
Copy link

llgerard commented May 1, 2022

Describe the bug

Looking at #19114 the code will The following code will forget conditions when the only difference is in the value tested, example:

const p = new PolicyStatement;
p.addCondition("StringEquals", {'sts:ExternalId': '12345678901'});
p.addCondition("StringEquals", {'sts:ExternalId': '01234567890'});
console.log(p.toStatementJson());

will forget the first condition, generating:

Condition: { StringEquals: { 'sts:ExternalId': '01234567890' } },
Effect: 'Allow'

instead of

Condition: { StringEquals: { 'sts:ExternalId': ['12345678901', '01234567890'] } },
Effect: 'Allow'

Expected Behavior

Not forget conditions

Current Behavior

Forget conditions

Reproduction Steps

import {
    PolicyStatement,
} from "monocdk/aws-iam";
const p = new PolicyStatement;
p.addCondition("StringEquals", {'sts:ExternalId': '12345678901'});
p.addCondition("StringEquals", {'sts:ExternalId': '01234567890'});
console.log(p.toStatementJson());

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

1.124

Framework Version

No response

Node.js Version

17.4

OS

macos

Language

Typescript

Language Version

No response

Other information

No response

@llgerard llgerard added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels May 1, 2022
@github-actions github-actions bot added the @aws-cdk/aws-iam Related to AWS Identity and Access Management label May 1, 2022
@rix0rrr
Copy link
Contributor

rix0rrr commented May 2, 2022

Phew, this headline stressed me out when I saw it, but I'm glad it has nothing to do with merging.

addCondition doesn't do what you think it does:

  • it doesn't go: "oh well StringEquals/sts:ExternalId already exists so I guess I'd better turn that into a list and append this new value."
  • instead, it goes: "set value in dict, done"

We don't recommend using the addXyz function anymore, really. You should try to create the Statement fully-formed

rix0rrr added a commit that referenced this issue May 2, 2022
Multiple calls to `addCondition` aren't as smart as one might want. Clarify that they aren't.

Fixes #20158.
@rix0rrr rix0rrr changed the title IAM: merge policy statements loosing conditions IAM: addCondition overwrites previous conditions May 2, 2022
@rix0rrr rix0rrr added effort/small Small work item – less than a day of effort p2 and removed needs-triage This issue or PR still needs to be triaged. labels May 2, 2022
@rix0rrr rix0rrr removed their assignment May 2, 2022
@rix0rrr rix0rrr added the cause/not-a-bug Not a bug (might still be a documentation issue, might still need work) label May 2, 2022
@llgerard
Copy link
Author

llgerard commented May 2, 2022

Is it intended? Because the semantics is a disjunction instead of "adding" (conjunction) a condition?

@mergify mergify bot closed this as completed in #20165 May 3, 2022
mergify bot pushed a commit that referenced this issue May 3, 2022
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*
@github-actions
Copy link

github-actions bot commented May 3, 2022

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

wphilipw pushed a commit to wphilipw/aws-cdk that referenced this issue May 23, 2022
Multiple calls to `addCondition` aren't as smart as one might want. Clarify that they aren't.

Fixes aws#20158.


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-iam Related to AWS Identity and Access Management bug This issue is a bug. cause/not-a-bug Not a bug (might still be a documentation issue, might still need work) effort/small Small work item – less than a day of effort p2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants