From ec171c787776cb72df4ffd302ed6a26fc8090ff1 Mon Sep 17 00:00:00 2001 From: Matteo Ridolfi Date: Fri, 11 Jan 2019 15:17:11 +0100 Subject: [PATCH 01/12] apigw websocket event testdata --- events/testdata/apigw-websocket-request.json | 71 ++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 events/testdata/apigw-websocket-request.json diff --git a/events/testdata/apigw-websocket-request.json b/events/testdata/apigw-websocket-request.json new file mode 100644 index 00000000..f4ba0657 --- /dev/null +++ b/events/testdata/apigw-websocket-request.json @@ -0,0 +1,71 @@ +{ + "headers": { + "Host": "*.execute-api.eu-central-1.amazonaws.com", + "Sec-WebSocket-Extensions": "permessage-deflate; client_max_window_bits", + "Sec-WebSocket-Key": "*", + "Sec-WebSocket-Version": "13", + "X-Amzn-Trace-Id": "*", + "X-Forwarded-For": "*.*.*.*", + "X-Forwarded-Port": "443", + "X-Forwarded-Proto": "https" + }, + "isBase64Encoded": false, + "multiValueHeaders": { + "Host": [ + "*.execute-api.eu-central-1.amazonaws.com" + ], + "Sec-WebSocket-Extensions": [ + "permessage-deflate; client_max_window_bits" + ], + "Sec-WebSocket-Key": [ + "*" + ], + "Sec-WebSocket-Version": [ + "13" + ], + "X-Amzn-Trace-Id": [ + "Root=*" + ], + "X-Forwarded-For": [ + "*.*.*.*" + ], + "X-Forwarded-Port": [ + "443" + ], + "X-Forwarded-Proto": [ + "https" + ] + }, + "requestContext": { + "apiId": "*", + "authorizer": "", + "connectedAt": "*", + "connectionId": "*", + "domainName": "*.execute-api.eu-central-1.amazonaws.com", + "error": "", + "eventType": "CONNECT", + "extendedRequestId": "*", + "identity": { + "accessKey": null, + "accountId": null, + "caller": null, + "cognitoAuthenticationProvider": null, + "cognitoAuthenticationType": null, + "cognitoIdentityId": null, + "cognitoIdentityPoolId": null, + "sourceIp": "*.*.*.*", + "user": null, + "userAgent": null, + "userArn": null + }, + "integrationLatency": "", + "messageDirection": "IN", + "messageId": null, + "requestId": "*", + "requestTime": "07/Jan/2019:09:20:57 +0000", + "requestTimeEpoch": 1546852857724, + "routeKey": "$connect", + "stage": "test", + "status": "" + } +} From 0070d0d5d80c3975383381aa5ad4395119cbb0f3 Mon Sep 17 00:00:00 2001 From: Matteo Ridolfi Date: Fri, 11 Jan 2019 15:18:18 +0100 Subject: [PATCH 02/12] Add event for ApiGW Websocket integration --- events/apigw.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ events/apigw_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/events/apigw.go b/events/apigw.go index 72e68c62..4ca0e8cf 100644 --- a/events/apigw.go +++ b/events/apigw.go @@ -57,6 +57,50 @@ type APIGatewayRequestIdentity struct { User string `json:"user"` } +// APIGatewayWebsocketProxyRequest contains data coming from the API Gateway proxy +type APIGatewayWebsocketProxyRequest struct { + Resource string `json:"resource"` // The resource path defined in API Gateway + Path string `json:"path"` // The url path for the caller + HTTPMethod string `json:"httpMethod"` + Headers map[string]string `json:"headers"` + MultiValueHeaders map[string][]string `json:"multiValueHeaders"` + QueryStringParameters map[string]string `json:"queryStringParameters"` + MultiValueQueryStringParameters map[string][]string `json:"multiValueQueryStringParameters"` + PathParameters map[string]string `json:"pathParameters"` + StageVariables map[string]string `json:"stageVariables"` + RequestContext APIGatewayWebsocketProxyRequestContext `json:"requestContext"` + Body string `json:"body"` + IsBase64Encoded bool `json:"isBase64Encoded,omitempty"` +} + +// APIGatewayWebsocketProxyRequestContext contains the information to identify +// the AWS account and resources invoking the Lambda function. It also includes +// Cognito identity information for the caller. +type APIGatewayWebsocketProxyRequestContext struct { + AccountID string `json:"accountId"` + ResourceID string `json:"resourceId"` + Stage string `json:"stage"` + RequestID string `json:"requestId"` + Identity APIGatewayRequestIdentity `json:"identity"` + ResourcePath string `json:"resourcePath"` + Authorizer string `json:"authorizer"` + HTTPMethod string `json:"httpMethod"` + APIID string `json:"apiId"` // The API Gateway rest API Id + ConnectedAt int64 `json:"connectedAt"` + ConnectionID string `json:"connectionId"` + DomainName string `json:"domainName"` + Error string `json:"error"` + EventType string `json:"eventType"` + ExtendedRequestId string `json:"extendedRequestId"` + IntegrationLatency string `json:"integrationLatency"` + MessageDirection string `json:"messageDirection"` + MessageID interface{} `json:"messageId"` + RequestTime string `json:"requestTime"` + RequestTimeEpoch int64 `json:"requestTime"` + RouteKey string `json:"routeKey"` + Status string `json:"status"` +} + // APIGatewayCustomAuthorizerRequestTypeRequestIdentity contains identity information for the request caller. type APIGatewayCustomAuthorizerRequestTypeRequestIdentity struct { APIKey string `json:"apiKey"` diff --git a/events/apigw_test.go b/events/apigw_test.go index ac1f0766..300aafba 100644 --- a/events/apigw_test.go +++ b/events/apigw_test.go @@ -127,6 +127,33 @@ func TestApiGatewayCustomAuthorizerRequestTypeRequestMalformedJson(t *testing.T) test.TestMalformedJson(t, APIGatewayCustomAuthorizerRequestTypeRequest{}) } +func TestApiGatewayWebsocketRequestMarshaling(t *testing.T) { + + // read json from file + inputJSON, err := ioutil.ReadFile("./testdata/apigw-websocket-request.json") + if err != nil { + t.Errorf("could not open test file. details: %v", err) + } + + // de-serialize into Go object + var inputEvent APIGatewayProxyWebsocketRequest + if err := json.Unmarshal(inputJSON, &inputEvent); err != nil { + t.Errorf("could not unmarshal event. details: %v", err) + } + + // serialize to json + outputJSON, err := json.Marshal(inputEvent) + if err != nil { + t.Errorf("could not marshal event. details: %v", err) + } + + assert.JSONEq(t, string(inputJSON), string(outputJSON)) +} + +func TestApiGatewayWebsocketRequestMalformedJson(t *testing.T) { + test.TestMalformedJson(t, APIGatewayProxyWebsocketRequest{}) +} + func TestApiGatewayCustomAuthorizerResponseMarshaling(t *testing.T) { // read json from file From 323ed311e3e1c801019b380fc91d567c8f9bf8c9 Mon Sep 17 00:00:00 2001 From: Matteo Ridolfi Date: Fri, 11 Jan 2019 19:08:25 +0100 Subject: [PATCH 03/12] typo in struct name --- events/apigw_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/events/apigw_test.go b/events/apigw_test.go index 300aafba..3749bdb4 100644 --- a/events/apigw_test.go +++ b/events/apigw_test.go @@ -136,7 +136,7 @@ func TestApiGatewayWebsocketRequestMarshaling(t *testing.T) { } // de-serialize into Go object - var inputEvent APIGatewayProxyWebsocketRequest + var inputEvent APIGatewayWebsocketProxyRequest if err := json.Unmarshal(inputJSON, &inputEvent); err != nil { t.Errorf("could not unmarshal event. details: %v", err) } @@ -151,7 +151,7 @@ func TestApiGatewayWebsocketRequestMarshaling(t *testing.T) { } func TestApiGatewayWebsocketRequestMalformedJson(t *testing.T) { - test.TestMalformedJson(t, APIGatewayProxyWebsocketRequest{}) + test.TestMalformedJson(t, APIGatewayWebsocketProxyRequest{}) } func TestApiGatewayCustomAuthorizerResponseMarshaling(t *testing.T) { From c65fb6dd5cfc4493c0b48167bd89bb319b402804 Mon Sep 17 00:00:00 2001 From: Matteo Ridolfi Date: Fri, 11 Jan 2019 19:20:49 +0100 Subject: [PATCH 04/12] fixed test event --- events/testdata/apigw-websocket-request.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/events/testdata/apigw-websocket-request.json b/events/testdata/apigw-websocket-request.json index f4ba0657..ca0e60b4 100644 --- a/events/testdata/apigw-websocket-request.json +++ b/events/testdata/apigw-websocket-request.json @@ -39,12 +39,12 @@ "requestContext": { "apiId": "*", "authorizer": "", - "connectedAt": "*", - "connectionId": "*", + "connectedAt": 1547230720092, + "connectionId": "TWegAcC4EowCHnA=", "domainName": "*.execute-api.eu-central-1.amazonaws.com", "error": "", "eventType": "CONNECT", - "extendedRequestId": "*", + "extendedRequestId": "TWegAcC4EowCHnA=", "identity": { "accessKey": null, "accountId": null, @@ -63,7 +63,7 @@ "messageId": null, "requestId": "*", "requestTime": "07/Jan/2019:09:20:57 +0000", - "requestTimeEpoch": 1546852857724, + "requestTimeEpoch": 0, "routeKey": "$connect", "stage": "test", "status": "" From 810d07107ced48a180ccb5e9573f3bdee0e0fd5b Mon Sep 17 00:00:00 2001 From: Matteo Ridolfi Date: Fri, 11 Jan 2019 19:31:57 +0100 Subject: [PATCH 05/12] matching apigw testdata --- events/testdata/apigw-websocket-request.json | 55 ++++++++++++++++---- 1 file changed, 44 insertions(+), 11 deletions(-) diff --git a/events/testdata/apigw-websocket-request.json b/events/testdata/apigw-websocket-request.json index ca0e60b4..7bf2f6d7 100644 --- a/events/testdata/apigw-websocket-request.json +++ b/events/testdata/apigw-websocket-request.json @@ -1,11 +1,25 @@ { + "resource": "", + "path": "", + "httpMethod": "", "headers": { - "Host": "*.execute-api.eu-central-1.amazonaws.com", - "Sec-WebSocket-Extensions": "permessage-deflate; client_max_window_bits", - "Sec-WebSocket-Key": "*", - "Sec-WebSocket-Version": "13", - "X-Amzn-Trace-Id": "*", - "X-Forwarded-For": "*.*.*.*", + "Accept": "*/*", + "Accept-Encoding": "gzip, deflate", + "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/json", + "headerName": "headerValue", + "Host": "gy415nuibc.execute-api.us-east-1.amazonaws.com", + "Postman-Token": "9f583ef0-ed83-4a38-aef3-eb9ce3f7a57f", + "User-Agent": "PostmanRuntime/2.4.5", + "Via": "1.1 d98420743a69852491bbdea73f7680bd.cloudfront.net (CloudFront)", + "X-Amz-Cf-Id": "pn-PWIJc6thYnZm5P0NMgOUglL1DYtl0gdeJky8tqsg8iS_sgsKD1A==", + "X-Forwarded-For": "54.240.196.186, 54.182.214.83", "X-Forwarded-Port": "443", "X-Forwarded-Proto": "https" }, @@ -37,7 +51,24 @@ ] }, "requestContext": { - "apiId": "*", + "accountId": "12345678912", + "resourceId": "roq9wj", + "stage": "testStage", + "requestId": "deef4878-7910-11e6-8f14-25afc3e9ae33", + "identity": { + "cognitoIdentityPoolId": "theCognitoIdentityPoolId", + "accountId": "theAccountId", + "cognitoIdentityId": "theCognitoIdentityId", + "caller": "theCaller", + "apiKey": "theApiKey", + "accessKey": "ANEXAMPLEOFACCESSKEY", + "sourceIp": "192.168.196.186", + "cognitoAuthenticationType": "theCognitoAuthenticationType", + "cognitoAuthenticationProvider": "theCognitoAuthenticationProvider", + "userArn": "theUserArn", + "userAgent": "PostmanRuntime/2.4.5", + "user": "theUser" + }, "authorizer": "", "connectedAt": 1547230720092, "connectionId": "TWegAcC4EowCHnA=", @@ -61,11 +92,13 @@ "integrationLatency": "", "messageDirection": "IN", "messageId": null, - "requestId": "*", "requestTime": "07/Jan/2019:09:20:57 +0000", "requestTimeEpoch": 0, "routeKey": "$connect", - "stage": "test", - "status": "" - } + "status": "", + "resourcePath": "/{proxy+}", + "httpMethod": "POST", + "apiId": "gy415nuibc" + }, + "body": "{\r\n\t\"a\": 1\r\n}" } From d63a7eb691177d00bcd57c4cb98e9c7be8a3552a Mon Sep 17 00:00:00 2001 From: Matteo Ridolfi Date: Fri, 11 Jan 2019 19:34:58 +0100 Subject: [PATCH 06/12] duplicated identidy data --- events/testdata/apigw-websocket-request.json | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/events/testdata/apigw-websocket-request.json b/events/testdata/apigw-websocket-request.json index 7bf2f6d7..6b18abd7 100644 --- a/events/testdata/apigw-websocket-request.json +++ b/events/testdata/apigw-websocket-request.json @@ -76,19 +76,6 @@ "error": "", "eventType": "CONNECT", "extendedRequestId": "TWegAcC4EowCHnA=", - "identity": { - "accessKey": null, - "accountId": null, - "caller": null, - "cognitoAuthenticationProvider": null, - "cognitoAuthenticationType": null, - "cognitoIdentityId": null, - "cognitoIdentityPoolId": null, - "sourceIp": "*.*.*.*", - "user": null, - "userAgent": null, - "userArn": null - }, "integrationLatency": "", "messageDirection": "IN", "messageId": null, From 2f08d7adfe0208b3479ffa5eab1e188921103216 Mon Sep 17 00:00:00 2001 From: Matteo Ridolfi Date: Fri, 11 Jan 2019 19:48:20 +0100 Subject: [PATCH 07/12] duplicated json field requestTimeEpoch --- events/apigw.go | 2 +- events/testdata/apigw-websocket-request.json | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/events/apigw.go b/events/apigw.go index 4ca0e8cf..91b3e649 100644 --- a/events/apigw.go +++ b/events/apigw.go @@ -96,7 +96,7 @@ type APIGatewayWebsocketProxyRequestContext struct { MessageDirection string `json:"messageDirection"` MessageID interface{} `json:"messageId"` RequestTime string `json:"requestTime"` - RequestTimeEpoch int64 `json:"requestTime"` + RequestTimeEpoch int64 `json:"requestTimeEpoch"` RouteKey string `json:"routeKey"` Status string `json:"status"` } diff --git a/events/testdata/apigw-websocket-request.json b/events/testdata/apigw-websocket-request.json index 6b18abd7..f6217dba 100644 --- a/events/testdata/apigw-websocket-request.json +++ b/events/testdata/apigw-websocket-request.json @@ -1,7 +1,7 @@ { - "resource": "", - "path": "", - "httpMethod": "", + "resource": "/{proxy+}", + "path": "/hello/world", + "httpMethod": "POST", "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", @@ -69,20 +69,20 @@ "userAgent": "PostmanRuntime/2.4.5", "user": "theUser" }, - "authorizer": "", + "authorizer": "*", "connectedAt": 1547230720092, "connectionId": "TWegAcC4EowCHnA=", "domainName": "*.execute-api.eu-central-1.amazonaws.com", - "error": "", + "error": "*", "eventType": "CONNECT", "extendedRequestId": "TWegAcC4EowCHnA=", - "integrationLatency": "", + "integrationLatency": "123", "messageDirection": "IN", "messageId": null, "requestTime": "07/Jan/2019:09:20:57 +0000", "requestTimeEpoch": 0, "routeKey": "$connect", - "status": "", + "status": "*", "resourcePath": "/{proxy+}", "httpMethod": "POST", "apiId": "gy415nuibc" From 7f74a83356ef5ff1352a44e498f5a0c7b2cc8fee Mon Sep 17 00:00:00 2001 From: Matteo Ridolfi Date: Fri, 11 Jan 2019 19:58:41 +0100 Subject: [PATCH 08/12] fixed test request json order --- events/testdata/apigw-websocket-request.json | 21 +++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/events/testdata/apigw-websocket-request.json b/events/testdata/apigw-websocket-request.json index f6217dba..cba53e63 100644 --- a/events/testdata/apigw-websocket-request.json +++ b/events/testdata/apigw-websocket-request.json @@ -23,7 +23,6 @@ "X-Forwarded-Port": "443", "X-Forwarded-Proto": "https" }, - "isBase64Encoded": false, "multiValueHeaders": { "Host": [ "*.execute-api.eu-central-1.amazonaws.com" @@ -50,6 +49,18 @@ "https" ] }, + "queryStringParameters": { + "name": "me" + }, + "multiValueQueryStringParameters": { + "name": ["me"] + }, + "pathParameters": { + "proxy": "hello/world" + }, + "stageVariables": { + "stageVariableName": "stageVariableValue" + }, "requestContext": { "accountId": "12345678912", "resourceId": "roq9wj", @@ -69,7 +80,10 @@ "userAgent": "PostmanRuntime/2.4.5", "user": "theUser" }, + "resourcePath": "/{proxy+}", "authorizer": "*", + "httpMethod": "POST", + "apiId": "gy415nuibc", "connectedAt": 1547230720092, "connectionId": "TWegAcC4EowCHnA=", "domainName": "*.execute-api.eu-central-1.amazonaws.com", @@ -82,10 +96,7 @@ "requestTime": "07/Jan/2019:09:20:57 +0000", "requestTimeEpoch": 0, "routeKey": "$connect", - "status": "*", - "resourcePath": "/{proxy+}", - "httpMethod": "POST", - "apiId": "gy415nuibc" + "status": "*" }, "body": "{\r\n\t\"a\": 1\r\n}" } From 28cdadc59e871640686a8924c024d09383b6e215 Mon Sep 17 00:00:00 2001 From: Dino Korah Date: Thu, 24 Jan 2019 12:56:05 +0000 Subject: [PATCH 09/12] Fixes suggestions from pr: aws/aws-lambda-go#159 issue: aws/aws-lambda-go#154 --- events/apigw.go | 2 +- events/testdata/apigw-websocket-request.json | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/events/apigw.go b/events/apigw.go index 91b3e649..3161bf30 100644 --- a/events/apigw.go +++ b/events/apigw.go @@ -83,7 +83,7 @@ type APIGatewayWebsocketProxyRequestContext struct { RequestID string `json:"requestId"` Identity APIGatewayRequestIdentity `json:"identity"` ResourcePath string `json:"resourcePath"` - Authorizer string `json:"authorizer"` + Authorizer map[string]interface{} `json:"authorizer"` HTTPMethod string `json:"httpMethod"` APIID string `json:"apiId"` // The API Gateway rest API Id ConnectedAt int64 `json:"connectedAt"` diff --git a/events/testdata/apigw-websocket-request.json b/events/testdata/apigw-websocket-request.json index cba53e63..821ffc66 100644 --- a/events/testdata/apigw-websocket-request.json +++ b/events/testdata/apigw-websocket-request.json @@ -81,7 +81,11 @@ "user": "theUser" }, "resourcePath": "/{proxy+}", - "authorizer": "*", + "authorizer": { + "principalId": "admin", + "clientId": 1, + "clientName": "Exata" + }, "httpMethod": "POST", "apiId": "gy415nuibc", "connectedAt": 1547230720092, From 0671d2393bae6894f7da59a5a9ee7dd69169dbdd Mon Sep 17 00:00:00 2001 From: Bryan Moffatt Date: Thu, 24 Jan 2019 20:03:56 +0000 Subject: [PATCH 10/12] ExtendedRequestId -> ExtendedRequestID --- events/apigw.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/events/apigw.go b/events/apigw.go index 3161bf30..67a78ef1 100644 --- a/events/apigw.go +++ b/events/apigw.go @@ -91,7 +91,7 @@ type APIGatewayWebsocketProxyRequestContext struct { DomainName string `json:"domainName"` Error string `json:"error"` EventType string `json:"eventType"` - ExtendedRequestId string `json:"extendedRequestId"` + ExtendedRequestID string `json:"extendedRequestId"` IntegrationLatency string `json:"integrationLatency"` MessageDirection string `json:"messageDirection"` MessageID interface{} `json:"messageId"` From e486b6c420baa63538c98d15d8e21c1e766ac931 Mon Sep 17 00:00:00 2001 From: Bryan Moffatt Date: Wed, 20 Feb 2019 19:20:54 +0000 Subject: [PATCH 11/12] Change type of Authorizer to interface{} --- events/apigw.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/events/apigw.go b/events/apigw.go index 67a78ef1..485afc29 100644 --- a/events/apigw.go +++ b/events/apigw.go @@ -36,7 +36,7 @@ type APIGatewayProxyRequestContext struct { RequestID string `json:"requestId"` Identity APIGatewayRequestIdentity `json:"identity"` ResourcePath string `json:"resourcePath"` - Authorizer map[string]interface{} `json:"authorizer"` + Authorizer interface{} `json:"authorizer"` HTTPMethod string `json:"httpMethod"` APIID string `json:"apiId"` // The API Gateway rest API Id } From dc65ae8a3c5a7aa9c6a12ab4f04f6dfb4af6b994 Mon Sep 17 00:00:00 2001 From: Bryan Moffatt Date: Wed, 20 Feb 2019 19:22:47 +0000 Subject: [PATCH 12/12] Update apigw.go changed wrong `Authorizer` --- events/apigw.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/events/apigw.go b/events/apigw.go index 485afc29..520a02c8 100644 --- a/events/apigw.go +++ b/events/apigw.go @@ -36,7 +36,7 @@ type APIGatewayProxyRequestContext struct { RequestID string `json:"requestId"` Identity APIGatewayRequestIdentity `json:"identity"` ResourcePath string `json:"resourcePath"` - Authorizer interface{} `json:"authorizer"` + Authorizer map[string]interface{} `json:"authorizer"` HTTPMethod string `json:"httpMethod"` APIID string `json:"apiId"` // The API Gateway rest API Id } @@ -83,7 +83,7 @@ type APIGatewayWebsocketProxyRequestContext struct { RequestID string `json:"requestId"` Identity APIGatewayRequestIdentity `json:"identity"` ResourcePath string `json:"resourcePath"` - Authorizer map[string]interface{} `json:"authorizer"` + Authorizer interface{} `json:"authorizer"` HTTPMethod string `json:"httpMethod"` APIID string `json:"apiId"` // The API Gateway rest API Id ConnectedAt int64 `json:"connectedAt"`