-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
385: Put APIGateway in front of private loadbalancer (#834)
- Loading branch information
1 parent
7bd3b5b
commit ec3e89c
Showing
9 changed files
with
347 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { Express, Request } from 'express'; | ||
|
||
declare module 'node:net' { | ||
interface Socket { | ||
encrypted?: boolean; | ||
} | ||
} | ||
/** | ||
* Unfortunately, "trust proxy" is by default broken when Express is behind an | ||
* AWS HTTP API Gateway: | ||
* - https://github.com/expressjs/express/issues/5459 | ||
* - https://repost.aws/en/questions/QUtBHMaz7IQ6aM4RCBMnJvgw/why-does-apigw-http-api-use-forwarded-header-while-other-services-still-use-x-forwarded-headers | ||
* | ||
* Therefore we use Express API overrides to modify our Request IP and Protocol properties: | ||
* - https://expressjs.com/en/guide/overriding-express-api.html | ||
*/ | ||
export function usingForwardedHeader(app: Express): Express { | ||
Object.defineProperties(app.request, { | ||
ip: { | ||
configurable: true, | ||
enumerable: true, | ||
get() { | ||
const proxies = parseForwardedHeader(this as Request); | ||
return proxies?.for ?? (this as Request).socket.remoteAddress; | ||
}, | ||
}, | ||
protocol: { | ||
configurable: true, | ||
enumerable: true, | ||
get() { | ||
const proxies = parseForwardedHeader(this as Request); | ||
return proxies?.proto ?? (this as Request).socket.encrypted | ||
? 'https' | ||
: 'http'; | ||
}, | ||
}, | ||
}); | ||
return app; | ||
} | ||
|
||
/** | ||
* Forwarded header looks like this: | ||
* | ||
* ``` | ||
* for=12.345.67.89;proto=https;host=somehost.org,for=98.76.54.321;proto=http;host=someproxy.net | ||
* ``` | ||
* | ||
* Note we only need the first entry, as that is the client. | ||
* | ||
* @param request incoming express Request object | ||
*/ | ||
function parseForwardedHeader(request: Request) { | ||
return request | ||
.header('Forwarded') | ||
?.split(',') | ||
.at(0) | ||
?.split(';') | ||
.reduce((result, proxyProps) => { | ||
const [key, value] = proxyProps.split('='); | ||
if (key && value) { | ||
result[key] = value; | ||
} | ||
return result; | ||
}, {} as Record<string, string>); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"availability-zones:account=600982866784:region=eu-west-1": [ | ||
"eu-west-1a", | ||
"eu-west-1b", | ||
"eu-west-1c" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.