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

assertions: coarseStringLike #29593

Open
1 of 2 tasks
nag0yan opened this issue Mar 24, 2024 · 3 comments
Open
1 of 2 tasks

assertions: coarseStringLike #29593

nag0yan opened this issue Mar 24, 2024 · 3 comments
Labels
@aws-cdk/assertions Related to the @aws-cdk/assertv2 package effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2

Comments

@nag0yan
Copy link

nag0yan commented Mar 24, 2024

Describe the feature

Checking if some string is in specific property JSON string
For example,

BucketPolicy01:
  Type: AWS::S3::BucketPolicy
  Properties:
    Bucket:
      Ref: Bucket01
    PolicyDocument:
      Statement:
        - Action:
            - s3:GetObject
          Effect: Allow
          Principal:
            AWS:
              Fn::Join:
                - ""
                - - "arn:"
                  - Ref: AWS::Partition
                  - :iam::${account-id}:user/some-user
          ...

To check this template, we can check as below.

template.hasResourceProperties("AWS::S3::BucketPolicy", {
  PolicyDocument: {
    Statement: [
      {
        Action: ["s3:GetObject"],
        Effect: "Allow",
        Principal: Match.coarseStringLike("some-user") // new method
      },
    ],
  },
});

Use Case

const principal = new Capture();
template.hasResourceProperties("AWS::S3::BucketPolicy", {
  PolicyDocument: {
    Statement: [
      {
        Action: ["s3:GetObject"],
        Effect: "Allow",
        Principal: principal,
      },
    ],
  },
});
expect(JSON.stringify(principal.asObject())).toMatch(
  "some-user",
);

For many cases, it may be useful to check only partial string in JSON string.

Proposed Solution

I don't dived into assertions module yet, so I don't know this solution is feasible.
However, json utility tools( such as, JSON.stringify, json.dumps, json.Marshal ... ) would be helpful, I thought.

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

CDK version used

2.124.0

Environment details (OS name and version, etc.)

Windows WSL2

@nag0yan nag0yan added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Mar 24, 2024
@github-actions github-actions bot added the @aws-cdk/assertions Related to the @aws-cdk/assertv2 package label Mar 24, 2024
@tim-finnigan tim-finnigan self-assigned this Mar 25, 2024
@tim-finnigan tim-finnigan added investigating This issue is being investigated and/or work is in progress to resolve the issue. and removed needs-triage This issue or PR still needs to be triaged. labels Mar 25, 2024
@tim-finnigan
Copy link

Hi @nag0yan thanks for the feature request. I think what you're trying to do can accomplished by capturing values, for example:

import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { Capture } from 'aws-cdk-lib/assertions';
import { Template } from 'aws-cdk-lib/assertions';

export class AssertionCdkStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    let templateObject = {
      "Resources": {
        "BucketPolicy01": {
          "Type": "AWS::S3::BucketPolicy",
          "Properties": {
            "Bucket": {
              "Ref": "Bucket01"
            },
            "PolicyDocument": {
              "Statement": [
                {
                  "Action": ["s3:GetObject"],
                  "Effect": "Allow",
                  "Principal": {
                    "AWS": {
                      "Fn::Join": [
                        "",
                        [
                          "arn:",
                          {
                            "Ref": "AWS::Partition"
                          },
                          ":iam::${account-id}:user/some-user"
                        ]
                      ]
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }

    const template = Template.fromString(JSON.stringify(templateObject));

    const principalCapture = new Capture();
    template.hasResourceProperties('AWS::S3::BucketPolicy', {
      PolicyDocument: {
        Statement: [
          {
            Action: ['s3:GetObject'],
            Effect: 'Allow',
            Principal: principalCapture,
          },
        ],
      },
    });

    const capturedPrincipal = JSON.stringify(principalCapture.asObject());

    console.log(capturedPrincipal.includes('some-user'));

    if (!capturedPrincipal.includes('some-user')) {
      throw new Error('Expected Principal to contain "some-user"');
    }
  }
}

@tim-finnigan tim-finnigan added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. and removed investigating This issue is being investigated and/or work is in progress to resolve the issue. labels Mar 25, 2024
Copy link

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

@github-actions github-actions bot added the closing-soon This issue will automatically close in 4 days unless further comments are made. label Mar 28, 2024
@nag0yan
Copy link
Author

nag0yan commented Mar 29, 2024

Hello, @tim-finnigan. Thank you for replying.
I already use that method (Capturing and JSON.stringify). I want to do the same thing as other Match static method. (see "Describe the feature")
So, many assertion tests, that checks a template with CloudFormation Built-in Function (such as, Fn::Join) will be more simple.

@github-actions github-actions bot removed closing-soon This issue will automatically close in 4 days unless further comments are made. response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. labels Mar 29, 2024
@tim-finnigan tim-finnigan removed their assignment Mar 29, 2024
@tim-finnigan tim-finnigan added p2 effort/medium Medium work item – several days of effort labels Mar 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/assertions Related to the @aws-cdk/assertv2 package effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2
Projects
None yet
Development

No branches or pull requests

2 participants