Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate-event sns adds in " html entities #755

Closed
ste00martin opened this issue Nov 9, 2018 · 4 comments
Closed

generate-event sns adds in " html entities #755

ste00martin opened this issue Nov 9, 2018 · 4 comments
Assignees
Labels
area/local/generate-event sam local generate-event command stage/waiting-for-release Fix has been merged to develop and is waiting for a release type/bug

Comments

@ste00martin
Copy link

ste00martin commented Nov 9, 2018

On aws-cli version 0.6.2

Expected output of sam local generate-event sns notification --message "$(cat event.json)"

{
    "Records": [
        {
            "EventVersion": "1.0",
            "EventSource": "aws:sns",
            "EventSubscriptionArn": "arn:aws:sns:EXAMPLE",
            "Sns": {
                "MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e",
                "Signature": "EXAMPLE",
                "Type": "Notification",
                "TopicArn": "arn:aws:sns:us-east-1:111122223333:ExampleTopic",
                "MessageAttributes": {
                    "Test": {
                        "Type": "String",
                        "Value": "TestString"
                    },
                    "TestBinary": {
                        "Type": "Binary",
                        "Value": "TestBinary"
                    }
                },
                "SignatureVersion": "1",
                "Timestamp": "1970-01-01T00:00:00.000Z",
                "SigningCertUrl": "EXAMPLE",
                "Message": "{\n  \"foo\": \"bar\"\n}",
                "UnsubscribeUrl": "EXAMPLE",
                "Subject": "example subject"
            }
        }
    ]
}

Actual output:

{
  "Records": [
    {
      "EventVersion": "1.0",
      "EventSubscriptionArn": "arn:aws:sns:us-east-1::ExampleTopic",
      "EventSource": "aws:sns",
      "Sns": {
        "SignatureVersion": "1",
        "Timestamp": "1970-01-01T00:00:00.000Z",
        "Signature": "EXAMPLE",
        "SigningCertUrl": "EXAMPLE",
        "MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e",
        "Message": "{
  "foo": "bar"
}",
        "MessageAttributes": {
          "Test": {
            "Type": "String",
            "Value": "TestString"
          },
          "TestBinary": {
            "Type": "Binary",
            "Value": "TestBinary"
          }
        },
        "Type": "Notification",
        "UnsubscribeUrl": "EXAMPLE",
        "TopicArn": "arn:aws:sns:us-east-1:123456789012:ExampleTopic",
        "Subject": "example subject"
      }
    }
  ]
}

My event.json file:

{
  "foo": "bar"
}

On aws-cli version 0.3.0 it works correctly. However, the way you have to invoke generate-event is different between versions. The way to call generate-event on v0.3.0 with the correctly formatted response is this way: sam local generate-event sns --message "$(cat event.json)" which outputs the first json blob.

For now my workaround is to use the older version; but when will this be fixed going forward so:
a) newer versions of aws-cli has backwards compatibility
b) json files are parsed correctly
c) you can pass in a json file directly into the generate-event command without the cat command?

@ghost
Copy link

ghost commented Nov 26, 2018

It looks like dcd65f4 is the commit that introduced this issue. With this commit, sam uses a templating engine. I think that the solution would be to change {{message}} to {{{message}}} in this file https://github.com/awslabs/aws-sam-cli/blob/develop/samcli/commands/local/lib/generated_sample_events/events/sns/Sns.json#L12 (and ideally doing the same for all events).

You can look at https://github.com/noahmorrison/chevron/blob/master/tests/test.mustache for more details about how the templating engine renders variables.

@jfuss
Copy link
Contributor

jfuss commented Nov 26, 2018

@ste00martin Thanks for the report. @jackdbernier is correct. {{message}} will be html echoed by default and that using {{{message}}} instead is the correct behavior we want across all events. I have a patch for this in my fork. Hoping to get this out for a PR after I make a second pass through all the events.

More details on how mustache behaves in the variable section here

@jfuss jfuss added type/bug area/local/generate-event sam local generate-event command labels Nov 26, 2018
@jfuss jfuss self-assigned this Nov 26, 2018
@ghost
Copy link

ghost commented Nov 27, 2018

The combo of triple braces and json escaping works.
For instance,

sam local generate-event sns notification --message "$(jq '.|tojson' event.json)"

will output

{
  "Records": [
    {
      "EventSource": "aws:sns",
      "EventVersion": "1.0",
      "EventSubscriptionArn": "arn:aws:sns:us-east-1::ExampleTopic",
      "Sns": {
        "Type": "Notification",
        "MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e",
        "TopicArn": "arn:aws:sns:us-east-1:123456789012:ExampleTopic",
        "Subject": "example subject",
        "Message": ""{\"name\":\"Jacques\"}"",
        "Timestamp": "1970-01-01T00:00:00.000Z",
        "SignatureVersion": "1",
        "Signature": "EXAMPLE",
        "SigningCertUrl": "EXAMPLE",
        "UnsubscribeUrl": "EXAMPLE",
        "MessageAttributes": {
          "Test": {
            "Type": "String",
            "Value": "TestString"
          },
          "TestBinary": {
            "Type": "Binary",
            "Value": "TestBinary"
          }
        }
      }
    }
  ]
}

if you omit the jq part you will end up with something like "Message": "{"name": "Jacques"}", and that is not valid. It used to work before and I think it is because it was using https://docs.python.org/3/library/json.html to generate the event. So --message "$(cat event.json)" needs to become something like --message "$(jq '.|tojson' event.json)". This is not as straightforward as it used to be but with 1 or 2 examples, it will definitely work out just fine.

Thanks for jumping on that.

@jfuss jfuss added the stage/waiting-for-release Fix has been merged to develop and is waiting for a release label Dec 5, 2018
@jfuss
Copy link
Contributor

jfuss commented Dec 13, 2018

This was released in v0.9.0

Closing

@jfuss jfuss closed this as completed Dec 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/local/generate-event sam local generate-event command stage/waiting-for-release Fix has been merged to develop and is waiting for a release type/bug
Projects
None yet
Development

No branches or pull requests

2 participants