Skip to content

Commit 387ef29

Browse files
committed
Support for HTTP API
1 parent f13ad14 commit 387ef29

File tree

4 files changed

+146
-4
lines changed

4 files changed

+146
-4
lines changed

src/eventToRequestOptions.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,12 @@ const eventToRequestOptions = (event: APIGatewayEvent, ctx?: LambdaContext): InP
4545
} else {
4646
// api gateway request
4747
ssl = true;
48-
remoteAddress = event.requestContext && event.requestContext.identity && event.requestContext.identity.sourceIp
48+
const remoteAddressList = event.requestContext && event.requestContext.identity && event.requestContext.identity.sourceIp
49+
if (remoteAddressList) {
50+
// HTTP API includes the full x-forwarder for chain here and the remote ip is the last element
51+
const items = remoteAddressList.split(',').map(s => s.trim());
52+
remoteAddress = items[items.length - 1];
53+
}
4954
}
5055
return {
5156
method: event.httpMethod,

test/app/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const app = express();
77

88
app.set('view engine', 'ejs');
99
app.set('views', path.join(__dirname, 'views'));
10+
app.set('trust proxy', 2);
1011

1112
app.use(cookieParser());
1213
app.use(express.json());

test/fixtures/event-http-api.json

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
{
2+
"version": 2,
3+
"path": "/inspect",
4+
"httpMethod": "GET",
5+
"headers": {
6+
"Content-Length": "0",
7+
"Host": "apiid.execute-api.ap-southeast-2.amazonaws.com",
8+
"User-Agent": "curl/7.54.0",
9+
"X-Amzn-Trace-Id": "Root=1-5de881d0-2a206d74d6702f28f7e78eb0",
10+
"X-Forwarded-For": "1.2.3.4, 4.5.6.7, 9.9.9.9",
11+
"X-Forwarded-Port": "443",
12+
"X-Forwarded-Proto": "https",
13+
"accept": "*/*",
14+
"cookie": "cookie1=value1; cookie2=value2; cookie3=value3"
15+
},
16+
"multiValueHeaders": {
17+
"Content-Length": [
18+
"0"
19+
],
20+
"Host": [
21+
"apiid.execute-api.ap-southeast-2.amazonaws.com"
22+
],
23+
"User-Agent": [
24+
"curl/7.54.0"
25+
],
26+
"X-Amzn-Trace-Id": [
27+
"Root=1-5de881d0-2a206d74d6702f28f7e78eb0"
28+
],
29+
"X-Forwarded-For": [
30+
"1.2.3.4, 4.5.6.7, 9.9.9.9"
31+
],
32+
"X-Forwarded-Port": [
33+
"443"
34+
],
35+
"X-Forwarded-Proto": [
36+
"https"
37+
],
38+
"accept": [
39+
"*/*"
40+
],
41+
"cookie": [
42+
"cookie1=value1; cookie2=value2; cookie3=value3"
43+
]
44+
},
45+
"queryStringParameters": {
46+
"param": "ab cd"
47+
},
48+
"multiValueQueryStringParameters": {
49+
"param": [
50+
"ab cd"
51+
]
52+
},
53+
"requestContext": {
54+
"accountId": "248139140343",
55+
"apiId": "apiid",
56+
"authorizer": {
57+
"claims": null,
58+
"scopes": null
59+
},
60+
"domainName": "apiid.execute-api.ap-southeast-2.amazonaws.com",
61+
"domainPrefix": "apiid",
62+
"extendedRequestId": null,
63+
"httpMethod": "GET",
64+
"identity": {
65+
"accessKey": null,
66+
"accountId": null,
67+
"caller": null,
68+
"cognitoAuthenticationProvider": null,
69+
"cognitoAuthenticationType": null,
70+
"cognitoIdentityId": null,
71+
"cognitoIdentityPoolId": null,
72+
"principalOrgId": null,
73+
"sourceIp": "1.2.3.4, 4.5.6.7, 9.9.9.9",
74+
"user": null,
75+
"userAgent": "curl/7.54.0",
76+
"userArn": null
77+
},
78+
"path": "/inspect",
79+
"protocol": "HTTP/1.1",
80+
"requestId": null,
81+
"requestTime": "05/Dec/2019:04:04:32 +0000",
82+
"requestTimeEpoch": 1575518672924,
83+
"resourceId": null,
84+
"resourcePath": "/inspect",
85+
"stage": "$default"
86+
},
87+
"pathParameters": null,
88+
"stageVariables": null,
89+
"body": null,
90+
"isBase64Encoded": true
91+
}

test/integration.test.ts

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import app from './app';
33
import lambda from '../src/lambda';
44

55
import event from './fixtures/event.json';
6+
import eventHttpApi from './fixtures/event-http-api.json';
67
import eventAlb from './fixtures/alb-event.json';
78

89
const handler = lambda(app);
@@ -110,11 +111,12 @@ describe('integration', () => {
110111
cookies: {
111112
cookie1: "value1",
112113
cookie2: "value2",
113-
cookie3: "value3", },
114+
cookie3: "value3",
115+
},
114116
fresh: false,
115117
hostname: "apiid.execute-api.ap-southeast-2.amazonaws.com",
116118
ip: "1.152.111.246",
117-
ips: [],
119+
ips: ["1.152.111.246", "54.239.202.85"],
118120
method: "GET",
119121
originalUrl: "/inspect?param=ab%20cd",
120122
params: {},
@@ -138,6 +140,48 @@ describe('integration', () => {
138140
})
139141
})
140142

143+
it('handles HTTP API event', () => {
144+
return handler(eventHttpApi).then(response => {
145+
expect(response.statusCode).toEqual(200);
146+
expect(response.isBase64Encoded).toEqual(false);
147+
expect(response.multiValueHeaders!["content-type"][0]).toEqual('application/json; charset=utf-8');
148+
expect(response.multiValueHeaders!["x-powered-by"][0]).toEqual('Express');
149+
const json = JSON.parse(response.body);
150+
expect(json).toEqual({
151+
baseUrl: "",
152+
body: {},
153+
cookies: {
154+
cookie1: "value1",
155+
cookie2: "value2",
156+
cookie3: "value3",
157+
},
158+
fresh: false,
159+
hostname: "apiid.execute-api.ap-southeast-2.amazonaws.com",
160+
ip: "4.5.6.7",
161+
ips: ["4.5.6.7", "9.9.9.9"],
162+
method: "GET",
163+
originalUrl: "/inspect?param=ab%20cd",
164+
params: {},
165+
path: "/inspect",
166+
protocol: "https",
167+
query: {
168+
param: "ab cd",
169+
},
170+
secure: true,
171+
signedCookies: {},
172+
stale: true,
173+
subdomains: [
174+
"ap-southeast-2",
175+
"execute-api",
176+
"apiid",
177+
],
178+
url: "/inspect?param=ab%20cd",
179+
xForwardedFor: "1.2.3.4, 4.5.6.7, 9.9.9.9",
180+
xhr: false,
181+
})
182+
})
183+
})
184+
141185
it('handles ALB event', () => {
142186
return handler(eventAlb).then(response => {
143187
expect(response.statusCode).toEqual(200);
@@ -151,7 +195,8 @@ describe('integration', () => {
151195
cookies: {
152196
cookie1: "value1",
153197
cookie2: "value2",
154-
cookie3: "value3", },
198+
cookie3: "value3",
199+
},
155200
fresh: false,
156201
hostname: "elbname-234234234234.ap-southeast-2.elb.amazonaws.com",
157202
ip: "1.136.104.131",

0 commit comments

Comments
 (0)