mweagle / Sparta

go microservices, powered by AWS Lambda
https://gosparta.io
MIT License
717 stars 48 forks source link

Parameter translation issue in template mapping #19

Closed ryanwalls closed 8 years ago

ryanwalls commented 8 years ago

Working through some examples and proof-of-concepts and hitting a problem with url form encoding an @ sign in a post parameter.

Our code for our lambda function:

package main

import (
    "encoding/json"
    "fmt"
    "net/http"

    "github.com/Sirupsen/logrus"
    sparta "github.com/mweagle/Sparta"
)

func helloWorld(event *json.RawMessage, context *sparta.LambdaContext, w http.ResponseWriter, logger *logrus.Logger) {
    messageBytes, err := event.MarshalJSON()
    if err != nil {
        panic(err)
    }
    fmt.Fprintf(w, string(messageBytes))
}

func main() {

    var lambdaFunctions []*sparta.LambdaAWSInfo

    helloWorldFn := sparta.NewLambda(sparta.IAMRoleDefinition{},
        helloWorld,
        nil)
    lambdaFunctions = append(lambdaFunctions, helloWorldFn)

    stage := sparta.NewStage("dev")
    apiGateway := sparta.NewAPIGateway("SubscriptionAPI", stage)

    apiGatewayResource, _ := apiGateway.NewResource("/subscriptions", helloWorldFn)
    apiGatewayResource.NewMethod("POST")

    sparta.Main("MyHelloWorldStack",
        "Simple Sparta application that demonstrates core functionality",
        lambdaFunctions,
        apiGateway,
        nil)
}

Our post from postman: image

The response (notice the body is printing foo%!f(MISSING)oo.com instead of foo@foo.com:

{
  "method": "POST",
  "body": "stripeToken=sometoken&customerEmail=foo%!f(MISSING)oo.com",
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "en-US,en;q=0.8",
    "Cache-Control": "no-cache",
    "CloudFront-Forwarded-Proto": "https",
    "CloudFront-Is-Desktop-Viewer": "true",
    "CloudFront-Is-Mobile-Viewer": "false",
    "CloudFront-Is-SmartTV-Viewer": "false",
    "CloudFront-Is-Tablet-Viewer": "false",
    "CloudFront-Viewer-Country": "US",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "sam7set65f.execute-api.us-west-2.amazonaws.com",
    "Origin": "chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop",
    "Postman-Token": "833177fb-ab46-3ca0-f04f-9606e8a984a5",
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36",
    "Via": "1.1 c5a973adb2cf2730cc2e8d8b45a9cbbe.cloudfront.net (CloudFront)",
    "X-Amz-Cf-Id": "D-HzXIDvPy2Y0Q5430vO8XnrsGOzwxbcP1zHRZRdugMDLNxy0Krbdw==",
    "X-Forwarded-For": "216.10.236.190, 205.251.202.200",
    "X-Forwarded-Port": "443",
    "X-Forwarded-Proto": "https"
  },
  "queryParams": {},
  "pathParams": {},
  "context": {
    "apiId": "sam7set65f",
    "method": "POST",
    "requestId": "a2bb18bd-7468-11e6-8ed6-7b72a035e925",
    "resourceId": "n44jz7",
    "resourcePath": "/subscriptions",
    "stage": "dev",
    "identity": {
      "accountId": "",
      "apiKey": "",
      "caller": "",
      "cognitoAuthenticationProvider": "",
      "cognitoAuthenticationType": "",
      "cognitoIdentityId": "",
      "cognitoIdentityPoolId": "",
      "sourceIp": "216.10.236.190",
      "user": "",
      "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36",
      "userArn": ""
    }
  }
}

Any ideas on what is happening? My assumption was that perhaps there is some translation issue in the integration mapping.

mweagle commented 8 years ago

Agreed - I'm guessing that's from some Velocity interaction as part of https://github.com/mweagle/Sparta/blob/master/resources/provision/apigateway/inputmapping_default.vtl. Will take a look.

ryanwalls commented 8 years ago

@mweagle I tried this guy's template for url form encoded data, and it worked: http://stackoverflow.com/a/35068572/411229.

See his gist here: https://gist.github.com/ryanray/668022ad2432e38493df

It would of course need to be adapted to match the format sparta is looking for.

mweagle commented 8 years ago

Thanks very much for tracking this down - will definitely help with the resolution & I'll work to get it included for the 0.9.0 release.

On Tue, Sep 6, 2016 at 2:50 PM, ryanwalls notifications@github.com wrote:

@mweagle https://github.com/mweagle I tried this guy's template for url form encoded data, and it worked: http://stackoverflow.com/a/ 35068572/411229.

See his gist here: https://gist.github.com/ryanray/668022ad2432e38493df

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/mweagle/Sparta/issues/19#issuecomment-245107269, or mute the thread https://github.com/notifications/unsubscribe-auth/AAYr6t0X3Hn9vfZY7GgUDBlN78gOAFznks5qneARgaJpZM4J2NDh .


matt weagle | mweagle@gmail.com

mweagle commented 8 years ago

Should be resolved by https://github.com/mweagle/Sparta/commit/2782cdc0adbf0a3c4f18185f98857475c3f4fe50, available in v0.9.0.