All files / proxy/handlers handleStatus.ts

100% Statements 27/27
91.66% Branches 22/24
100% Functions 12/12
100% Lines 27/27

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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225    6x 6x                           6x 6x                 6x   36x   36x                       6x                   6x                 6x     12x   12x 8x   4x     12x               3x 18x 3x   3x               18x 18x                 3x                                                                                                                                                                                         6x 6x             6x       3x   3x                            
import { CloudFrontResultResponse } from 'aws-lambda'
import { CustomerVariables } from '../utils/customer-variables/customer-variables'
import { CustomerVariableName, internalVariables } from '../utils/customer-variables/types'
import { maybeObfuscateVariable } from '../utils/customer-variables/maybe-obfuscate-variable'
 
export interface EnvVarInfo {
  envVarName: string
  value: string | number | null | undefined
  isSet: boolean
  isInternal: boolean
  isOptional: 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
  // Determines if the variable is relevant only for V3
  isForV3Only: boolean
}
 
const v3Values = [CustomerVariableName.AgentDownloadPath, CustomerVariableName.GetResultPath]
const optionalValues = [CustomerVariableName.BehaviorPathNestLevel, ...v3Values]
 
export interface StatusInfo {
  version: string
  envInfo: EnvVarInfo[]
  styleNonce: string
}
 
async function getEnvInfo(customerVariables: CustomerVariables) {
  const infoArray: EnvVarInfo[] = await Promise.all(
    Object.values(CustomerVariableName).map(async (variable) => {
      const value = await maybeObfuscateVariable(customerVariables, variable)
 
      return {
        envVarName: variable,
        value: value.value,
        isSet: value.value !== null && value.value !== undefined && value.value !== '',
        isInternal: internalVariables.has(variable),
        isOptional: optionalValues.includes(variable),
        resolvedBy: value.resolvedBy,
        isForV3Only: v3Values.includes(variable),
      }
    })
  )
 
  return infoArray
}
 
interface ItemRow {
  title: string
  description?: string
  children?: string
}
 
function renderItemRow({ title, description, children = '' }: ItemRow) {
  return `
          <div class="item">
              <h3>${title}</h3> 
              ${description ? `<p>${description}</p>` : ''}
              ${children}
          </div>
`.trim()
}
 
const v3Icon = `<span data-tooltip="This variable is relevant only for V3 version of the JS Agent and can be ignored in later versions." class="v3-icon">V3 only</span>`
 
function renderEnvInfoRow(info: EnvVarInfo) {
  let description = ''
 
  if (info.isSet && info.resolvedBy) {
    description = `Value is set`
  } else {
    description = `⚠️ Value is not defined ${info.value ? `and uses default value: ${info.value}` : ''}`
  }
 
  return `
    <li>
        <strong>${info.envVarName}${info.isForV3Only ? ` ${v3Icon}` : ''}</strong> - ${description}
    </li>
  `.trim()
}
 
function renderEnvInfo(envInfo: EnvVarInfo[]) {
  const isAllCustomerDefinedVariablesSet = envInfo
    .filter((info) => !info.isInternal && !info.isOptional)
    .every((info) => info.isSet && info.resolvedBy)
 
  return renderItemRow({
    title: 'Variables',
    description: isAllCustomerDefinedVariablesSet
      ? '✅ All required environment variables are set:'
      : '🚨 Some required environment variables are not set:',
    children: `
      <ul class="env-info">
        ${envInfo
          .filter((info) => !info.isInternal)
          .toSorted((a, b) => (a.isForV3Only === b.isForV3Only ? 0 : a.isForV3Only ? 1 : -1))
          .map(renderEnvInfoRow)
          .join('')}
      </ul>
    `.trim(),
  })
}
 
function renderHtml({ version, envInfo, styleNonce }: StatusInfo) {
  return `
    <html lang="en-US">
      <head>
        <title>CloudFront integration status</title>
        <meta charset="utf-8">
        <style nonce='${styleNonce}'>
         .env-info {
            display: flex;
            flex-direction: column;
          }
          
          body {
            display: flex;
            flex-direction: column;
            align-items: center;
          }
          
          body > * {
            margin-bottom: 1em;
          }
          
          .v3-icon {
            font-size: 10px;
            color: #999;
          }
          
          .items {
            display: flex;
            flex-direction: column;
            align-content: baseline;
            gap: 2rem;
            flex-wrap: wrap;
          }
          
          .item {
            display: flex;
            flex-direction: column;
            align-items: flex-start;
            gap: 1rem;
          }
          
          .item h3, p {
            margin: 0;
          }
          
          ul {
            margin: 0;
          }
          
          [data-tooltip] {
            position: relative;
            border-bottom: 1px dashed #000;
            cursor: help
          }
          
          [data-tooltip]::after {
            position: absolute;
            opacity: 0;
            pointer-events: none;
            content: attr(data-tooltip);
            left: 0;
            top: calc(100% + 10px);
            border-radius: 3px;
            box-shadow: 0 0 5px 2px rgba(100, 100, 100, 0.6);
            background-color: white;
            z-index: 10;
            padding: 8px;
            width: 300px;
            transform: translateY(-20px);
            transition: all 150ms cubic-bezier(.25, .8, .25, 1);
          }
          
          [data-tooltip]:hover::after {
            opacity: 1;
            transform: translateY(0);
            transition-duration: 300ms;
          }
        </style>
      </head>
      <body>
        <h1>CloudFront integration status</h1>
        <div class="items">
            ${renderItemRow({ title: 'Lambda function version', description: version })}
            ${renderEnvInfo(envInfo)}
        </div>
          <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, styleNonce: string): Promise<StatusInfo> {
  return {
    version: '__lambda_func_version__',
    envInfo: await getEnvInfo(customerVariables),
    styleNonce,
  }
}
 
export async function handleStatus(
  customerVariables: CustomerVariables,
  styleNonce: string
): Promise<CloudFrontResultResponse> {
  const body = await getStatusInfo(customerVariables, styleNonce)
 
  return {
    status: '200',
    body: renderHtml(body).trim(),
    headers: {
      'content-type': [{ key: 'Content-Type', value: 'text/html' }],
      'content-security-policy': [
        {
          key: 'Content-Security-Policy',
          value: `default-src 'none'; img-src https://fingerprint.com; style-src 'nonce-${styleNonce}'`,
        },
      ],
    },
  }
}