All files / proxy/handlers handleStatus.ts

100% Statements 21/21
100% Branches 8/8
100% Functions 10/10
100% Lines 21/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120    3x 3x                                 6x   30x   30x                   6x       3x 15x 6x   3x 1x             2x 10x   4x           2x               3x                                                                   3x 6x           3x 3x   3x                
import { CloudFrontResultResponse } from 'aws-lambda'
import { CustomerVariables } from '../utils/customer-variables/customer-variables'
import { CustomerVariableType, CustomerVariableValue, internalVariables } from '../utils/customer-variables/types'
import { maybeObfuscateVariable } from '../utils/customer-variables/maybe-obfuscate-variable'
 
export interface EnvVarInfo {
  envVarName: string
  value: CustomerVariableValue
  isSet: boolean
  isInternal: 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 interface StatusInfo {
  version: string
  envInfo: EnvVarInfo[]
}
 
async function getEnvInfo(customerVariables: CustomerVariables) {
  const infoArray: EnvVarInfo[] = await Promise.all(
    Object.values(CustomerVariableType).map(async (variable) => {
      const value = await maybeObfuscateVariable(customerVariables, variable)
 
      return {
        envVarName: variable,
        value: value.value,
        isSet: Boolean(value.value),
        isInternal: internalVariables.has(variable),
        resolvedBy: value.resolvedBy,
      }
    })
  )
 
  return infoArray
}
 
function renderEnvInfo(envInfo: EnvVarInfo[]) {
  const isAllCustomerDefinedVariablesSet = envInfo
    .filter((info) => !info.isInternal)
    .every((info) => info.isSet && info.resolvedBy)
 
  if (isAllCustomerDefinedVariablesSet) {
    return `
      <div>
        ✅ All environment variables are set
      </div>
    `
  }
 
  const children = envInfo
    .filter((info) => (!info.isSet || !info.resolvedBy) && !info.isInternal)
    .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>CloudFront 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>CloudFront integration status</h1>
        <div>
          Lambda function version: ${version}
        </div>
        ${renderEnvInfo(envInfo)}
          <span>
            Please reach out our support via <a href="mailto:support@fingerprint.com">support@fingerprint.com</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: CustomerVariables): Promise<CloudFrontResultResponse> {
  const body = await getStatusInfo(customerVariables)
 
  return {
    status: '200',
    body: renderHtml(body).trim(),
    headers: {
      'content-type': [{ key: 'Content-Type', value: 'text/html' }],
    },
  }
}