question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Minimized IAM Statements produced are inconsistent

See original GitHub issue

Describe the bug

aws-cdk version: 2.27.0 (build 8e89048) aws-cdk-lib version: 2.27.0

I have a Policy attached to a User which has been granted S3 access as such:

        bucket.grant_delete(iam_user)
        bucket.grant_put_acl(iam_user)

When doing cdk synth this results in the following statement block being added onto an IAM policy tied to the user:

      {
       "Action": [
        "s3:DeleteObject*",
        "s3:PutObjectAcl",
        "s3:PutObjectVersionAcl"
       ],
       "Effect": "Allow",
       "Resource": {
        "Fn::Join": [
         "",
         [
          {
           "Fn::GetAtt": [
            "bucket43879C71",
            "Arn"
           ]
          },
          "/*"
         ]
        ]
       }
      }

However, when I try to assert that this statement is on the policy in the template it fails because in the template generated by the test it has split it into 2 statements.

Expected Behavior

I expect that the output of cdk synth would be the same as what aws_cdk.assertions.Template.from_stack(stack) produces when it comes to IAM policy statements.

Current Behavior

What actually happens is the aws_cdk.assertions.Template.from_stack(stack) produces 2 individual statements like so:

E                     {
E                       "Action": "s3:DeleteObject*",
E                       "Effect": "Allow",
E                       "Resource": {
E                         "Fn::Join": [
E                           "",
E                           [
E                             {
E                               "Fn::GetAtt": [
E                                 "bucket43879C71",
E                                 "Arn"
E                               ]
E                             },
E                             "/*"
E                           ]
E                         ]
E                       }
E                     },
E                     {
E                       "Action": [
E                         "s3:PutObjectAcl",
E                         "s3:PutObjectVersionAcl"
E                       ],
E                       "Effect": "Allow",
E                       "Resource": {
E                         "Fn::Join": [
E                           "",
E                           [
E                             {
E                               "Fn::GetAtt": [
E                                 "bucket43879C71",
E                                 "Arn"
E                               ]
E                             },
E                             "/*"
E                           ]
E                         ]
E                       }
E                     },

(pardon the E’s, taken straight from my CLI)

Reproduction Steps

import aws_cdk
from constructs import Construct

class TestStack(aws_cdk.Stack):
    def __init__(
        self,
        scope: Construct,
        construct_id: str,
        **kwargs
    ) -> None:
        super().__init__(scope, construct_id, **kwargs)

        bucket = aws_cdk.aws_s3.Bucket(self, 'bucket')
        iam_user = aws_cdk.aws_iam.User(self, 'iam-user')
        bucket.grant_delete(iam_user)
        bucket.grant_put_acl(iam_user)

app = aws_cdk.App()
stack = TestStack(app, 'test')
template = aws_cdk.assertions.Template.from_stack(stack)

user = template.find_resources("AWS::IAM::User")
bucket = template.find_resources("AWS::S3::Bucket")
template.has_resource_properties(
    "AWS::IAM::Policy",
    aws_cdk.assertions.Match.object_like(
        {
            "PolicyDocument": {
                "Statement": aws_cdk.assertions.Match.array_with(
                    [
                        aws_cdk.assertions.Match.object_like(
                            {
                                "Action": [
                                    "s3:DeleteObject*",
                                    "s3:PutObjectAcl",
                                    "s3:PutObjectVersionAcl",
                                ],
                                "Effect": "Allow",
                                "Resource": {
                                    "Fn::Join": [
                                        "",
                                        [
                                            {
                                                "Fn::GetAtt": [
                                                    aws_cdk.assertions.Match.any_value(),
                                                    "Arn",
                                                ]
                                            },
                                            "/*",
                                        ],
                                    ]
                                },
                            }
                        ),
                    ]
                )
            },
        }
    ),
)

Possible Solution

It seems that the synth is doing some extra smarts to merge the 2 statements for grant_delete and grant_put_acl into 1 statement?

It would be great for the output of synth and aws_cdk.assertions.Template.from_stack() to be as similar as possible so that a developer can write their tests based on the output of synth and what ends up getting deployed into CF

Additional Information/Context

No response

CDK CLI Version

2.27.0 (build 8e89048)

Framework Version

No response

Node.js Version

v18.0.0

OS

Mac

Language

Python

Language Version

3.8.5

Other information

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
joekeilty-oubcommented, Jun 10, 2022

@polothy That worked perfectly, thank you! ❤️

0reactions
github-actions[bot]commented, Jun 8, 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

scipy.optimize minimize inconsistent results - Stack Overflow
I am getting some very weird results when running the minimize function from scipy optimize. Here is the code from scipy.optimize import ...
Read more >
optimization - Inconsistency in optimize.minimize - Computational ...
I am trying to fit a time-dependent curve at each time step. I do so in minimizing along xc the quadratic error between...
Read more >
First Circuit Rules Inconsistent SSDI Statements Doom ADA Claim ...
In Pena v. Honeywell International, Inc., issued on July 22, 2019, the U.S. Court of Appeals for the First Circuit denied a former...
Read more >
Issues with data and analyses: Errors, underlying themes, and ...
Errors we have seen (and sometimes made) are the result not of repeating ... there are inconsistencies between P values and test statistics, ......
Read more >
Cognitive dissonance - Wikipedia
In the field of psychology, cognitive dissonance is the perception of contradictory information, and the mental toll of it. Relevant items of information ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found