Skip to content

Commit

Permalink
Merge pull request #6 from fingerprintjs/feature/update-rollback
Browse files Browse the repository at this point in the history
Health status page
  • Loading branch information
TheUnderScorer authored Mar 23, 2023
2 parents 29569bc + b98f6bb commit a7500a4
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 2 deletions.
129 changes: 129 additions & 0 deletions proxy/handlers/status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { HttpRequest } from '@azure/functions'
import { CustomerVariables } from '../customer-variables/CustomerVariables'
import { maybeObfuscateVariable } from '../customer-variables/maybeObfuscateVariable'
import { CustomerVariableType } from '../customer-variables/types'
import { HttpResponseSimple } from '@azure/functions/types/http'
import { EnvVarInfo, StatusFormat } from '../../shared/status'

export interface HandleStatusParams {
httpRequest: HttpRequest
customerVariables: CustomerVariables
}

export interface StatusInfo {
version: string
envInfo: EnvVarInfo[]
}

async function getEnvInfo(customerVariables: CustomerVariables) {
const infoArray: EnvVarInfo[] = await Promise.all(
Object.values(CustomerVariableType).map(async (variableType) => {
const value = await customerVariables.getVariable(variableType)

return {
envVarName: variableType,
value: maybeObfuscateVariable(variableType, value.value),
isSet: Boolean(value.value),
resolvedBy: value.resolvedBy,
}
}),
)

return infoArray
}

function renderEnvInfo(envInfo: EnvVarInfo[]) {
const isAlSet = envInfo.every((info) => info.isSet && info.resolvedBy)

if (isAlSet) {
return `
<div>
✅ All environment variables are set
</div>
`
}

const children = envInfo
.filter((info) => !info.isSet || !info.resolvedBy)
.map(
(info) => `
<div class="env-info-item">
⚠️ <strong>${info.envVarName} </strong> is not defined${info.isSet ? ' and uses default value' : ''}
</div>`,
)

return `
<div class="env-info">
${children.join('')}
</div>
`
}

function renderHtml({ version, envInfo }: StatusInfo) {
return `
<html lang="en-US">
<head>
<title>Azure integration status</title>
<meta charset="utf-8">
<style>
body, .env-info {
display: flex;
}
body {
flex-direction: column;
align-items: center;
}
body > * {
margin-bottom: 1em;
}
</style>
</head>
<body>
<h1>Azure integration status</h1>
<div>
Lambda function version: ${version}
</div>
${renderEnvInfo(envInfo)}
<span>
Please reach out our support via <a href="mailto:[email protected]">[email protected]</a> if you have any issues
</span>
</body>
</html>
`
}

export async function getStatusInfo(customerVariables: CustomerVariables): Promise<StatusInfo> {
return {
version: '__lambda_func_version__',
envInfo: await getEnvInfo(customerVariables),
}
}

export async function handleStatus({
customerVariables,
httpRequest,
}: HandleStatusParams): Promise<HttpResponseSimple> {
const { format } = httpRequest.query

const info = await getStatusInfo(customerVariables)

if (format === StatusFormat.JSON) {
return {
status: '200',
body: info,
headers: {
'Content-Type': 'application/json',
},
}
}

return {
status: '200',
body: renderHtml(info).trim(),
headers: {
'Content-Type': 'text/html',
},
}
}
15 changes: 13 additions & 2 deletions proxy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { handleIngress } from './handlers/ingress'
import { CustomerVariables } from './customer-variables/CustomerVariables'
import { EnvCustomerVariables } from './customer-variables/EnvCustomerVariables'
import { CustomerVariableType } from './customer-variables/types'
import { STATUS_PATH } from '../shared/status'
import { handleStatus } from './handlers/status'

const proxy: AzureFunction = async (context: Context, req: HttpRequest): Promise<void> => {
context.log.verbose('Handling request', {
Expand All @@ -13,13 +15,22 @@ const proxy: AzureFunction = async (context: Context, req: HttpRequest): Promise

const customerVariables = new CustomerVariables([new EnvCustomerVariables()], context.log)

const path = req.params?.restOfPath

if (path === STATUS_PATH) {
context.res = await handleStatus({
httpRequest: req,
customerVariables,
})

return
}

const [clientPath, resultPath] = await Promise.all([
customerVariables.getVariable(CustomerVariableType.AgentDownloadPath),
customerVariables.getVariable(CustomerVariableType.GetResultPath),
])

const path = req.params?.restOfPath

const get404 = () => ({
status: 404,
body: JSON.stringify({
Expand Down
16 changes: 16 additions & 0 deletions shared/status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { CustomerVariableValue } from '../proxy/customer-variables/types'

export enum StatusFormat {
HTML = 'html',
JSON = 'json',
}

export interface EnvVarInfo {
envVarName: string
value: CustomerVariableValue
isSet: boolean
// If null, the variable was resolved with the default value, otherwise it was resolved by the provider with the given name
resolvedBy: string | null
}

export const STATUS_PATH = 'status'

0 comments on commit a7500a4

Please sign in to comment.