aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.52k stars 3.86k forks source link

aws-logs: Allow JSON filter patterns to use regex by removing automatic double quotes around pattern strings #30451

Open sobesim opened 3 months ago

sobesim commented 3 months ago

Describe the feature

FilterPattern.stringValue should allow the user to specify if the value input field is a regex string or not.

Use Case

According to CloudWatch filter pattern documentation, regex expressions must be surrounded by %.

I'm trying to use cdk's FilterPattern.stringValue to match a json document.

CDK automatically adds double quotes in the final filter pattern, which prevents Cloudwatch from recognizing any string as a regex pattern. For example:

Proposed Solution

Reason for this behavior may be this line: https://github.com/aws/aws-cdk/blob/main/packages/aws-cdk-lib/aws-logs/lib/pattern.ts#L399. Perhaps we need another input to stringValue that allows the user to specify if the input string is a regex pattern or not.

Other Information

No response

Acknowledgements

CDK version used

2.144.0

Environment details (OS name and version, etc.)

Linux

brian-stack commented 3 months ago

Also impacted by this issue.

pahud commented 3 months ago

Thank you for the issue report. Please help us prioritize with 👍 and we welcome pull requests.

jan-xyz commented 2 months ago

I created a PR and maybe one of you is willing to also provide some feedback: https://github.com/aws/aws-cdk/pull/30741

studds commented 1 month ago

Is there any workaround for this?

jan-xyz commented 1 month ago

Yes, provide your own function that adds a regex filter pattern, like the one from the PR I linked.

jan-xyz commented 1 month ago

@pahud anything I can do to reopen the PR I made? It was far from abandoned. I requested support weeks ago and never got any response. Honestly, this felt super discouraging and I don't think I will contribute to CDK in the future.

paulhcsun commented 1 month ago

Hi @jan-xyz, apologies for the late response on your PR. I've re-opened and replied on the PR with the information you've requested. Thank you for the contribution!

JevinAnderson commented 1 month ago

Hi @jan-xyz , thanks for raising this! Found this while trying to resolve my own similar problem, and used your solution.

I couldn't comment on the CR, so here are two suggestions:

  1. Add a validator. This would be helpful for all users. How many times have we all written code locally that fails once we try it for real? The syntax here could at least be added to a very simple regex, and if the user inputs something forbidden, i.e., a !, they could be directed to this same documentation with a message like, unsupported char, etc...
  2. For the comparison variable type, you could enforce the options like so: comparison: '=' | '!='
    public static regexValue(jsonField: string, comparison: '=' | '!=', value: string): JsonPattern {
    return new JSONRegexPattern(jsonField, comparison, value);
    }
amrawadk commented 6 days ago

@jan-xyz just wanted to say thank you for taking the time to document your solution, it helped me as well.

jan-xyz commented 6 days ago

Nice! I am also planning to still do the remaining work for the integration test. It's just very busy at the moment and I didn't find the time to get to it again.