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.72k stars 3.94k forks source link

elasticloadbalancingv2: add validation for path parameter of `ListenerAction.redirect()` #31201

Closed badmintoncryer closed 2 months ago

badmintoncryer commented 3 months ago

Describe the feature

When setting the path property for ListenerAction.redirect(), CDK will validate whether it starts with a /.

Use Case

If we set a string without a leading / for the path property, deployment will fail as shown below.

Given:

httpListener.addAction('RedirectAction', {
        conditions: [elbv2.ListenerCondition.pathPatterns(['/*'])],
        action: elbv2.ListenerAction.redirect({
          protocol: elbv2.ApplicationProtocol.HTTPS,
          path: 'example/path? ',
          permanent: true
        }),
      })

Result:

Failed resources:
MainStack-develop | 1:34:02 AM | CREATE_FAILED | AWS::ElasticLoadBalancingV2::ListenerRule | Alb/HttpListener/RedirectActionRule (AlbHttpListenerRedirectActionRule1D930694) Internal error reported from downstream service during operation 'The Path parameter must be a valid path, should start with a '/', and may contain up to one of each of these placeholders: '#{path}', '#{host}', '#{port}'. (Service: ElasticLoadBalancingV2, Status Code: 400, Request ID: fd4c7f01-9c97-44c1-a177-30ac04b2db26)'.

Related docs: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-elasticloadbalancingv2-listenerrule-redirectconfig.html#cfn-elasticloadbalancingv2-listenerrule-redirectconfig-path

Proposed Solution

Add validation for path prop like below.

    if (options.path && !options.path.startsWith('/')) {
      throw new Error('Redirect path must start with a \'/\'');
    }

Other Information

No response

Acknowledgements

CDK version used

2.150.0

Environment details (OS name and version, etc.)

irrelevant

pahud commented 3 months ago

Thank you for the PR as always @badmintoncryer !

github-actions[bot] commented 2 months ago

Comments on closed issues and PRs are hard for our team to see. If you need help, please open a new issue that references this one.

github-actions[bot] commented 2 months ago

Comments on closed issues and PRs are hard for our team to see. If you need help, please open a new issue that references this one.