Skip to content

assertions: Allow to use wildcard when specifying constructPath of Annotations #25803

Open
@tmokmss

Description

@tmokmss

Describe the feature

Currently we can use methods like Annotations.hasError for either 1. all the constructs or 2. one specific construct.

// 1. Find errors in every construct
Annotations.fromStack(stack).findError('*', 'foo');

// 2. Find errors in the specific path
Annotations.fromStack(stack).findError('/Stack/Some/Resource', 'foo');

Now it will be much more useful if we can specify more granular match like prefix match.

// Find errors under the specific path
Annotations.fromStack(stack).findError('/Stack/Some/Resource/*', 'foo');

Use Case

I'd like to do more granular test on a specific construct. It would be ideal if we can use something like glob pattern to specify the construct path.

For example, when using with cdk-nag, we can check the vulnerabilities only for a specific construct and its children.

Proposed Solution

Use minimatch library here instead of v.id === path. minimatch is already used in aws-cdk-lib so it should not affect bundle size.

return Object.entries(section ?? {})
.filter(([_, v]) => v.id === path)
.reduce((agg, [k, v]) => { return { ...agg, [k]: v }; }, {});

However, because we can use special characters such as * or + in a construct id, the syntax of minimatch conflicts with them. We have to consider a workaround to introduce this feature without breaking changes.

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.81.0

Environment details (OS name and version, etc.)

macOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    @aws-cdk/assertionsRelated to the @aws-cdk/assertv2 packageeffort/smallSmall work item – less than a day 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