aws-cloudformation / aws-cloudformation-macros

This repository hosts examples of AWS CloudFormation macros.
Apache License 2.0
83 stars 49 forks source link

The String macro does not strip from Right correctly. #18

Closed edenhare closed 4 months ago

edenhare commented 2 years ago

There is a bug in the String macro Lambda function when using StripFrom Right.

In this code block

              elif operation == "MaxLength":
                      length = int(event["params"]["Length"])
                      if len(input) <= length:
                          response["fragment"] = input
                      elif "StripFrom" in event["params"]:
                          if event["params"]["StripFrom"] == "Left":
                              response["fragment"] = input[len(input)-length:]
                          elif event["params"]["StripFrom"] != "Right":
                              response["status"] = "failure"
                      else:
                          response["fragment"] = input[:length]

the else is never executed when StripFrom is present to return the result. The transform lambda successfully executes, but since there is no response['fragment'] is the return to CloudFormation, a template processing error results.

If the code is changed to

                elif operation == "MaxLength":
                      length = int(event["params"]["Length"])
                      if len(input) <= length:
                          response["fragment"] = input
                      elif "StripFrom" in event["params"]:
                          if event["params"]["StripFrom"] == "Left":
                              response["fragment"] = input[len(input)-length:]
                          elif event["params"]["StripFrom"] != "Right":
                              response["status"] = "failure"
                         else:
                              response["fragment"] = input[:length]

the the macros return correctly when using StripFrom Right and the response['fragment'] is in the return back to CloudFormation.

edenhare commented 2 years ago

See PR https://github.com/aws-cloudformation/aws-cloudformation-macros/pull/19

ericzbeard commented 4 months ago

We are archiving this repository in favor of similar content located here: https://github.com/aws-cloudformation/aws-cloudformation-templates/tree/main/aws/services/CloudFormation/MacrosExamples