Skip to content

Commit

Permalink
feat: trigger lambda on local
Browse files Browse the repository at this point in the history
  • Loading branch information
LinHuiqing committed Sep 19, 2023
1 parent 4371c0d commit ae85cb2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ services:
- PAYMENT_MIN_PAYMENT_AMOUNT_CENTS=50
- SSM_ENV_SITE_NAME=development
- PAYMENT_GUIDE_LINK
- PAYMENT_LANDING_GUIDE_LINK
# Cron secrets
- CRON_PAYMENT_API_SECRET=secretKey
# env vars for go integration
Expand All @@ -123,6 +124,8 @@ services:
- API_KEY_VERSION=v1
# env vars for growthbook
- GROWTHBOOK_CLIENT_KEY
# env vars for virus scanner
- VIRUS_SCANNER_LAMBDA_FUNCTION_NAME=function

mockpass:
build: https://github.com/opengovsg/mockpass.git#v4.0.4
Expand Down
5 changes: 4 additions & 1 deletion src/app/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ const s3 = new aws.S3({
})

// using aws-sdk v3
const virusScannerLambda = new Lambda({ region: basicVars.awsConfig.region })
const virusScannerLambda = new Lambda({
region: basicVars.awsConfig.region,
...(isDev ? { endpoint: 'http://host.docker.internal:9999' } : undefined),
})

const awsConfig: AwsConfig = {
...s3BucketUrlVars,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { celebrate, Joi, Segments } from 'celebrate'
import { NextFunction } from 'express'
import { StatusCodes } from 'http-status-codes'
import { chain, omit } from 'lodash'
import { okAsync, ResultAsync } from 'neverthrow'
import { errAsync, okAsync, ResultAsync } from 'neverthrow'

import { featureFlags } from '../../../../../shared/constants'
import {
Expand Down Expand Up @@ -193,7 +193,7 @@ export const scanAttachments = async (
next: NextFunction,
) => {
const logMeta = {
action: 'checkAttachmentQuarantineKeys',
action: 'scanAttachments',
...createReqMeta(req),
}

Expand Down Expand Up @@ -225,29 +225,45 @@ export const scanAttachments = async (
const triggerLambdaResult = await ResultAsync.combine(
req.body.responses.map((response) => {
if (isQuarantinedAttachmentResponse(response)) {
return triggerVirusScanning(response.answer).map((data) => {
const returnPayload = JSON.parse(
return triggerVirusScanning(response.filename).andThen((data) => {
const { statusCode, body } = JSON.parse(
Buffer.from(data?.Payload ?? '').toString(),
) as {
statusCode: number
body: {
message: string
cleanFileKey: string
destinationVersionId: string
}
body: string
}
const parsedBody = JSON.parse(body)
const returnPayload = {
statusCode,
body: parsedBody,
}
logger.info({
message: 'Successfully invoked lambda function',
meta: {
...logMeta,
responseMetadata: data?.$metadata,
statusCode: data?.StatusCode,
returnPayload,
},
})

// If return payload is not OK, either file cannot be found or virus scan failed. This should be
// treated as a fatal error we should investigate.
if (statusCode !== StatusCodes.OK) {
logger.warn({
message: 'Some lambda invocations failed',
meta: {
...logMeta,
responseMetadata: data?.$metadata,
returnPayload,
},
})
return errAsync(returnPayload)
}

return okAsync(returnPayload)
})
}
// If response is not an attachment, return ok.
return okAsync(true)
}),
)
Expand Down
1 change: 0 additions & 1 deletion src/app/modules/submission/receiver/receiver.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ export const addAttachmentToResponses = (
responses.forEach((response) => {
if (isAttachmentResponseFromMap(attachmentMap, response)) {
const file = attachmentMap[response._id]
response.answer = file.filename
response.filename = file.filename
response.content = file.content
}
Expand Down

0 comments on commit ae85cb2

Please sign in to comment.