All files / mgmt-lambda app.ts

97.91% Statements 47/48
95% Branches 19/20
100% Functions 2/2
97.91% Lines 47/48

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          1x 1x   1x 1x 1x 1x 1x 1x 1x   1x     23x   23x 23x 22x 22x     22x 1x     1x       21x 21x   4x     17x 17x 17x   17x   17x 9x 9x   8x 8x     8x 4x   4x       21x 21x 21x 4x   21x 21x 2x   21x 21x 2x     21x 4x 4x     17x            
import {
  APIGatewayEventRequestContextV2,
  APIGatewayProxyEventV2WithRequestContext,
  APIGatewayProxyResult,
} from 'aws-lambda'
import { SecretsManagerClient } from '@aws-sdk/client-secrets-manager'
import { getAuthSettings, retrieveAuthToken } from './auth'
import type { DeploymentSettings } from './model/DeploymentSettings'
import { handleError, handleNoAuthentication, handleNotFound, handleWrongConfiguration } from './handlers/errorHandlers'
import { defaults } from './DefaultSettings'
import { handleStatus } from './handlers/statusHandler'
import { handleUpdate } from './handlers/updateHandler'
import { LambdaClient } from '@aws-sdk/client-lambda'
import { CloudFrontClient } from '@aws-sdk/client-cloudfront'
import { removeLeadingAndTrailingSlashes } from './routing'
 
export async function handler(
  event: APIGatewayProxyEventV2WithRequestContext<APIGatewayEventRequestContextV2>
): Promise<APIGatewayProxyResult> {
  const secretManagerClient = new SecretsManagerClient({ region: defaults.AWS_REGION })
 
  try {
    const authSettings = await getAuthSettings(secretManagerClient)
    const authToken = retrieveAuthToken(event)
    Iif (!authToken || !authSettings.token) {
      return handleNoAuthentication()
    }
    if (authToken !== authSettings.token) {
      return handleNoAuthentication()
    }
  } catch (error) {
    return handleWrongConfiguration(error)
  }
 
  let deploymentSettings: DeploymentSettings
  try {
    deploymentSettings = loadDeploymentSettings()
  } catch (error) {
    return handleWrongConfiguration(error)
  }
 
  const method = event.requestContext.http.method
  const lambdaClient = new LambdaClient({ region: defaults.AWS_REGION })
  const cloudFrontClient = new CloudFrontClient({ region: defaults.AWS_REGION })
 
  const path = removeLeadingAndTrailingSlashes(event.rawPath)
 
  if (path === 'update' && method === 'POST') {
    try {
      return await handleUpdate(lambdaClient, cloudFrontClient, deploymentSettings)
    } catch (e: any) {
      console.error(e)
      return handleError(e)
    }
  }
  if (path === 'status' && method === 'GET') {
    return handleStatus(lambdaClient, cloudFrontClient, deploymentSettings)
  }
  return handleNotFound()
}
 
function loadDeploymentSettings(): DeploymentSettings {
  const missedVariables = []
  const cfDistributionId = process.env.CFDistributionId || ''
  if (cfDistributionId === '') {
    missedVariables.push('CFDistributionId')
  }
  const lambdaFunctionName = process.env.LambdaFunctionName || ''
  if (lambdaFunctionName === '') {
    missedVariables.push('LambdaFunctionName')
  }
  const lambdaFunctionArn = process.env.LambdaFunctionArn || ''
  if (lambdaFunctionArn === '') {
    missedVariables.push('LambdaFunctionArn')
  }
 
  if (missedVariables.length > 0) {
    const vars = missedVariables.join(', ')
    throw new Error(`environment variables not found: ${vars}`)
  }
 
  return {
    CFDistributionId: cfDistributionId,
    LambdaFunctionArn: lambdaFunctionArn,
    LambdaFunctionName: lambdaFunctionName,
  }
}