DianaIonita / serverless-api-gateway-caching

A plugin for the Serverless framework which helps with configuring caching for API Gateway endpoints.
ISC License
136 stars 35 forks source link

Proxy integration type header should not be specified. #41

Closed jonathanalberghini closed 5 years ago

jonathanalberghini commented 5 years ago

When using your plugin with an api that is set to use lambda Proxy integration true. The API call will fail only sometimes depending on what headers you are using and if there is authorization on it. I have confirmed with AWS support the integration request cannot add query parameters or headers when the type is lambda proxy. And it usually ignores these except when you have an authorizer. Function definition sampleProtectedRoute: handler: handler.sampleProtectedRoute events:

If I go into API gateway uncheck proxy integration and then remove the header it works. I also verified this with Jared at serverless. He made a quick fix for me that works "serverless-api-gateway-caching": "git://github.com/shortjared/serverless-api-gateway-caching.git#develop",

But he is not check the function is Proxy integration. Did he reach out to you? And if you use the console to make an api the minute you check Proxy integration check box the ability to list url path, query strings, http headers and mapping template goes away.

This only breaks when authorization is on.

The results are { "message": "" } Execution log for request 9f4d355f-2c6f-11e9-8d0a-9dcbaa5e4066 Sat Feb 09 13:35:53 UTC 2019 : Starting execution for request: 9f4d355f-2c6f-11e9-8d0a-9dcbaa5e4066 Sat Feb 09 13:35:53 UTC 2019 : HTTP Method: GET, Resource Path: /sampleprotectedroute Sat Feb 09 13:35:53 UTC 2019 : Method request path: {} Sat Feb 09 13:35:53 UTC 2019 : Method request query string: {} Sat Feb 09 13:35:53 UTC 2019 : Method request headers: {Authorization=**e2nE3E} Sat Feb 09 13:35:53 UTC 2019 : Method request body before transformations: Sat Feb 09 13:35:53 UTC 2019 : Request validation succeeded for content type application/json Sat Feb 09 13:35:53 UTC 2019 : Endpoint request URI: https://lambda.us-east-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:us-east-1:924149985142:function:iam-auth-api-dev-sampleProtectedRoute/invocations Sat Feb 09 13:35:53 UTC 2019 : Endpoint request headers: {Authorization=**e2nE3E,**262cc1, x-amzn-lambda-integration-tag=9f4d355f-2c6f-11e9-8d0a-9dcbaa5e4066, X-Amz-Date=20190209T133553Z, x-amzn-apigateway-api-id=7f16zarwv7, X-Amz-Source-Arn=arn:aws:execute-api:us-east-1:924149985142:7f16zarwv7/test-invoke-stage/GET/sampleprotectedroute, Accept=application/json, User-Agent=AmazonAPIGateway_7f16zarwv7, X-Amz-Security-Token=FQoGZXIvYXdzEB0aD [TRUNCATED] Sat Feb 09 13:35:53 UTC 2019 : Endpoint request body after transformations: {"resource":"/sampleprotectedroute","path":"/sampleprotectedroute","httpMethod":"GET","headers":{"Authorization":" eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHBpcmVzSW4iOiIxIGRheSIsImlzc3VlciI6ImxlZ2FjeSByZXNlYXJjaCIsImF1ZGllbmNlIjoic3Vic2NyaWJlcnMiLCJzdWJqZWN0Ijp7ImN1c3RvbWVyTnVtYmVyIjoiU0FDMDAxMTc0MzU2OSIsInVzZXJJcCI6IjE3Mi42OC41NC4xMjIifSwiaWF0IjoxNTQ4NTQyODg1fQ.Uromv03zOk03wY7ilS3fA5rFulxRGRNGKAfvle2nE3E"},"multiValueHeaders":{"Authorization":[" eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHBpcmVzSW4iOiIxIGRheSIsImlzc3VlciI6ImxlZ2FjeSByZXNlYXJjaCIsImF1ZGllbmNlIjoic3Vic2NyaWJlcnMiLCJzdWJqZWN0Ijp7ImN1c3RvbWVyTnVtYmVyIjoiU0FDMDAxMTc0MzU2OSIsInVzZXJJcCI6IjE3Mi42OC41NC4xMjIifSwiaWF0IjoxNTQ4NTQyODg1fQ.Uromv03zOk03wY7ilS3fA5rFulxRGRNGKAfvle2nE3E"]},"queryStringParameters":null,"multiValueQueryStringParameters":null,"pathParameters":null,"stageVariables":null,"requestContext":{"path":"/sampleprotectedroute","accountId":"924149985142","resourceId":"678gti","stage":"test-inv [TRUNCATED] Sat Feb 09 13:35:53 UTC 2019 : Sending request to https://lambda.us-east-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:us-east-1:924149985142:function:iam-auth-api-dev-sampleProtectedRoute/invocations Sat Feb 09 13:35:53 UTC 2019 : Received response. Integration latency: 2 ms Sat Feb 09 13:35:53 UTC 2019 : Endpoint response body before transformations:

400 Bad Request

400 Bad Request

Sat Feb 09 13:35:53 UTC 2019 : Endpoint response headers: {Server=awselb/2.0, Date=Sat, 09 Feb 2019 13:35:53 GMT, Content-Type=text/html, Content-Length=138, Connection=close} Sat Feb 09 13:35:53 UTC 2019 : Lambda invocation failed with status: 400. Lambda request id: N/A Sat Feb 09 13:35:53 UTC 2019 : Execution failed: Sat Feb 09 13:35:53 UTC 2019 : Method completed with status: 400

jonathanalberghini commented 5 years ago

Hi Jonathan,

Mariam here from AWS Premium Support with a follow up to our call earlier.

During our chat, I confirmed with you that an API Gateway with a proxy integration (AWS Proxy or HTTP proxy) does not give you the option to set request parameters for the method's Integration Request as the full HTTP request is passed through to the backend.

You pointed me in the direction of this document [1] which states that in the value set for "CacheKeyParameters" must be set as a request parameter.

However in the meantime, I will be in touch with the internal API Gateway experts with regards to the documentation and get back to you tomorrow with our findings and some official wording.

Please do let me know if you have any additional questions. In the meantime, I will give you a call tomorrow at about 12pm UTC. Could you please confirm that +18023234558 is the best number to reach you on and I will be in touch.

Have a great day and chat soon.

[1] https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-integration-settings-integration-request.html

Best regards,

Mariam N. Amazon Web Services

jonathanalberghini commented 5 years ago

The code should be Should be If ( method.Properties.Integration.Type !== ‘AWS_PROXY’) method.Properties.Integration.RequestParameters[integration.${cacheKeyParameter.name}] = method.${cacheKeyParameter.name};

DianaIonita commented 5 years ago

Thanks @jonathanalberghini, I've just released v1.3.7 to fix this.