Skip to content

assertions: coarseStringLike #29593

Open
Open
@nag0yan

Description

@nag0yan

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    @aws-cdk/assertionsRelated to the @aws-cdk/assertv2 packageeffort/mediumMedium work item – several days of effortfeature-requestA feature should be added or improved.p2

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions