Skip to content

Commit

Permalink
feat: add ability to override API hosts in AWS Secret
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Shelomentsev authored and sshelomentsev committed Apr 17, 2024
1 parent d8e8b5e commit 11af4c4
Show file tree
Hide file tree
Showing 11 changed files with 279 additions and 216 deletions.
13 changes: 13 additions & 0 deletions proxy/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import { CustomerVariables } from './utils/customer-variables/customer-variables'
import { HeaderCustomerVariables } from './utils/customer-variables/header-customer-variables'
import { SecretsManagerVariables } from './utils/customer-variables/secrets-manager/secrets-manager-variables'
import { getFpCdnUrl, getFpIngressBaseHost } from './utils/customer-variables/selectors'

export const handler = async (event: CloudFrontRequestEvent): Promise<CloudFrontResultResponse> => {
const request = event.Records[0].cf.request
Expand All @@ -27,6 +28,16 @@ export const handler = async (event: CloudFrontRequestEvent): Promise<CloudFront
new HeaderCustomerVariables(request),
])

const fpCdnUrl = await getFpCdnUrl(customerVariables)
const fpIngressBaseHost = await getFpIngressBaseHost(customerVariables)
if (!fpCdnUrl || !fpIngressBaseHost) {
return new Promise((resolve) =>
resolve({
status: '500',
})
)
}

console.debug('Handling request', request)

const resultUri = await getResultUri(customerVariables)
Expand All @@ -38,6 +49,7 @@ export const handler = async (event: CloudFrontRequestEvent): Promise<CloudFront

if (pathname === agentUri) {
return downloadAgent({
fpCdnUrl,
apiKey: getApiKey(request),
version: getVersion(request),
loaderVersion: getLoaderVersion(request),
Expand All @@ -53,6 +65,7 @@ export const handler = async (event: CloudFrontRequestEvent): Promise<CloudFront
suffix = '/' + suffix
}
return handleResult({
fpIngressBaseHost,
region: getRegion(request),
querystring: request.querystring,
method: request.method,
Expand Down
2 changes: 1 addition & 1 deletion proxy/handlers/handleAgentDowloading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function downloadAgent(options: AgentOptions): Promise<CloudFrontResultRe
return new Promise((resolve) => {
const data: any[] = []

const url = new URL('https://__FPCDN__')
const url = new URL(`https://${options.fpCdnUrl}`)
url.pathname = getEndpoint(options.apiKey, options.version, options.loaderVersion)
addTrafficMonitoringSearchParamsForProCDN(url)

Expand Down
6 changes: 3 additions & 3 deletions proxy/handlers/handleResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function handleResult(options: ResultOptions): Promise<CloudFrontResultRe

const data: any[] = []

const url = new URL(getIngressAPIHost(options.region) + options.suffix)
const url = new URL(getIngressAPIHost(options.region, options.fpIngressBaseHost) + options.suffix)
decodeURIComponent(options.querystring)
.split('&')
.filter((it) => it.includes('='))
Expand Down Expand Up @@ -103,7 +103,7 @@ function generateRequestUniqueId(): string {
return generateRandomString(2)
}

function getIngressAPIHost(region: Region): string {
function getIngressAPIHost(region: Region, baseHost: string): string {
const prefix = region === Region.us ? '' : `${region}.`
return `https://${prefix}__INGRESS_API__`
return `https://${prefix}${baseHost}`
}
1 change: 1 addition & 0 deletions proxy/model/AgentOptions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { OutgoingHttpHeaders } from 'http'

export interface AgentOptions {
fpCdnUrl: string
apiKey: string | undefined
version: string
loaderVersion: string | undefined
Expand Down
1 change: 1 addition & 0 deletions proxy/model/ResultOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { OutgoingHttpHeaders } from 'http'
import { Region } from './'

export interface ResultOptions {
fpIngressBaseHost: string
region: Region
querystring: string
method: string
Expand Down
Loading

0 comments on commit 11af4c4

Please sign in to comment.