Skip to content

Commit 8375058

Browse files
shashank-sachanShashank Sachanbmoffatt
authored
support for HTTP api gateway (#274)
* support for HTTP api gateway * payload keys and struct rename * field adds * APIGatewayV2HTTPRequest struct name update * de-serialization test case for new v2 http * log update * Remove RouteID - it's not present in the payload * test case update * Update apigw.go * Update apigw.go Had a chat with one of the gateway devs, who pointed out that the shape of Authorizer wouldn't be extensible for future shapes of authorizers * Update apigw.go * Update apigw_test.go * Update apigw-v2-request.json * Update apigw.go Co-authored-by: Shashank Sachan <thinkesta@gmail.com> Co-authored-by: Bryan Moffatt <bmoffatt@users.noreply.github.com>
1 parent dd65554 commit 8375058

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

events/apigw.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,48 @@ type APIGatewayProxyRequestContext struct {
4242
APIID string `json:"apiId"` // The API Gateway rest API Id
4343
}
4444

45+
// APIGatewayV2HTTPRequest contains data coming from the new HTTP API Gateway
46+
type APIGatewayV2HTTPRequest struct {
47+
Version string `json:"version"`
48+
RouteKey string `json:"routeKey"`
49+
RawPath string `json:"rawPath"`
50+
RawQueryString string `json:"rawQueryString"`
51+
Cookies []string `json:"cookies"`
52+
Headers map[string]string `json:"headers"`
53+
QueryStringParameters map[string]string `json:"queryStringParameters"`
54+
PathParameters map[string]string `json:"pathParameters"`
55+
RequestContext APIGatewayV2HTTPRequestContext `json:"requestContext"`
56+
StageVariables map[string]string `json:"stageVariables"`
57+
Body string `json:"body"`
58+
IsBase64Encoded bool `json:"isBase64Encoded"`
59+
}
60+
61+
// APIGatewayV2HTTPRequestContext contains the information to identify the AWS account and resources invoking the Lambda function.
62+
type APIGatewayV2HTTPRequestContext struct {
63+
RouteKey string `json:"routeKey"`
64+
AccountID string `json:"accountId"`
65+
Stage string `json:"stage"`
66+
RequestID string `json:"requestId"`
67+
Authorizer struct {
68+
JWT struct {
69+
Claims map[string]string `json:"claims"`
70+
Scopes []string `json:"scopes"`
71+
} `json:"jwt"`
72+
} `json:"authorizer"`
73+
APIID string `json:"apiId"` // The API Gateway HTTP API Id
74+
DomainName string `json:"domainName"`
75+
DomainPrefix string `json:"domainPrefix"`
76+
Time string `json:"time"`
77+
TimeEpoch int64 `json:"timeEpoch"`
78+
HTTP struct {
79+
Method string `json:"method"`
80+
Path string `json:"path"`
81+
Protocol string `json:"protocol"`
82+
SourceIP string `json:"sourceIp"`
83+
UserAgent string `json:"userAgent"`
84+
} `json:"http"`
85+
}
86+
4587
// APIGatewayRequestIdentity contains identity information for the request caller.
4688
type APIGatewayRequestIdentity struct {
4789
CognitoIdentityPoolID string `json:"cognitoIdentityPoolId"`

events/apigw_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,32 @@ func TestApiGatewayRestApiOpenApiRequestMarshaling(t *testing.T) {
209209

210210
assert.JSONEq(t, string(inputJSON), string(outputJSON))
211211
}
212+
213+
func TestApiGatewayV2HTTPRequestMarshaling(t *testing.T) {
214+
215+
// read json from file
216+
inputJSON, err := ioutil.ReadFile("./testdata/apigw-v2-request.json")
217+
if err != nil {
218+
t.Errorf("could not open test file. details: %v", err)
219+
}
220+
221+
// de-serialize into Go object
222+
var inputEvent APIGatewayV2HTTPRequest
223+
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
224+
t.Errorf("could not unmarshal event. details: %v", err)
225+
}
226+
227+
// validate custom authorizer context
228+
authContext := inputEvent.RequestContext.Authorizer
229+
if authContext.JWT.Claims["claim1"] != "value1" {
230+
t.Errorf("could not extract authorizer claim from JWT: %v", authContext)
231+
}
232+
233+
// serialize to json
234+
outputJSON, err := json.Marshal(inputEvent)
235+
if err != nil {
236+
t.Errorf("could not marshal event. details: %v", err)
237+
}
238+
239+
assert.JSONEq(t, string(inputJSON), string(outputJSON))
240+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"version": "2.0",
3+
"routeKey": "$default",
4+
"rawPath": "/my/path",
5+
"rawQueryString": "parameter1=value1&parameter1=value2&parameter2=value",
6+
"cookies": [
7+
"cookie1",
8+
"cookie2"
9+
],
10+
"headers": {
11+
"Header1": "value1",
12+
"Header2": "value2"
13+
},
14+
"queryStringParameters": {
15+
"parameter1": "value1,value2",
16+
"parameter2": "value"
17+
},
18+
"pathParameters": {
19+
"proxy": "hello/world"
20+
},
21+
"requestContext": {
22+
"routeKey": "$default",
23+
"accountId": "123456789012",
24+
"stage": "$default",
25+
"requestId": "id",
26+
"authorizer": {
27+
"jwt": {
28+
"claims": {
29+
"claim1": "value1",
30+
"claim2": "value2"
31+
},
32+
"scopes": [
33+
"scope1",
34+
"scope2"
35+
]
36+
}
37+
},
38+
"apiId": "api-id",
39+
"domainName": "id.execute-api.us-east-1.amazonaws.com",
40+
"domainPrefix": "id",
41+
"time": "12/Mar/2020:19:03:58+0000",
42+
"timeEpoch": 1583348638390,
43+
"http": {
44+
"method": "GET",
45+
"path": "/my/path",
46+
"protocol": "HTTP/1.1",
47+
"sourceIp": "IP",
48+
"userAgent": "agent"
49+
}
50+
},
51+
"stageVariables": {
52+
"stageVariable1": "value1",
53+
"stageVariable2": "value2"
54+
},
55+
"body": "{\r\n\t\"a\": 1\r\n}",
56+
"isBase64Encoded": false
57+
}

0 commit comments

Comments
 (0)