aws-cloudformation/aws-cloudformation-macros

The String macro does not strip from Right correctly.

Closed this issue · 2 comments

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.

See PR #19