generated from NHSDigital/nhs-notify-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 3
CCM-13765: Store Files for Virus Scanning #185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ScottFullerton-NHSE
merged 15 commits into
main
from
feature/CCM-13765-store-pdfs-virus-scan
Jan 29, 2026
Merged
Changes from 13 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c65c923
CCM-13765: Added lambda, queue and event listener
ScottFullerton-NHSE 7bbdbb1
CCM-13765: Fix terraform error
ScottFullerton-NHSE 9b476e2
CCM-13765: Merging main in
ScottFullerton-NHSE c61c619
CCM-13765: Added component test
ScottFullerton-NHSE 0965de8
CCM-13765: Fix linting
ScottFullerton-NHSE e0a90d2
CCM-13765: Fix linting
ScottFullerton-NHSE a77625a
CCM-13765: Fix component test
ScottFullerton-NHSE 9f47713
CCM-13765: Updated s3 helpers
ScottFullerton-NHSE 530ec7e
CCM-13765: Updated utils to match other PR and fix trivy
ScottFullerton-NHSE 42e61c9
CCM-13765: Fix typecheck
ScottFullerton-NHSE 9b92d48
CCM-13765: Fix sqs queue name
ScottFullerton-NHSE 5c029e9
CCM-13765: Fix circular dependency check
ScottFullerton-NHSE d177aec
CCM-13765: Merging main in
ScottFullerton-NHSE 996da26
Update infrastructure/terraform/components/dl/locals.tf
ScottFullerton-NHSE 1c17cef
CCM-13765: Fix broken locals.tf file
ScottFullerton-NHSE File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
infrastructure/terraform/components/dl/cloudwatch_event_rule_item_dequeued.tf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| resource "aws_cloudwatch_event_rule" "item_dequeued" { | ||
| name = "${local.csi}-item-dequeued" | ||
| description = "Queue item dequeued event rule" | ||
| event_bus_name = aws_cloudwatch_event_bus.main.name | ||
|
|
||
| event_pattern = jsonencode({ | ||
| "detail" : { | ||
| "type" : [ | ||
| "uk.nhs.notify.digital.letters.queue.item.dequeued.v1" | ||
| ] | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| resource "aws_cloudwatch_event_target" "item_dequeued_scanner" { | ||
| rule = aws_cloudwatch_event_rule.item_dequeued.name | ||
| arn = module.sqs_scanner.sqs_queue_arn | ||
| event_bus_name = aws_cloudwatch_event_bus.main.name | ||
| } |
7 changes: 7 additions & 0 deletions
7
infrastructure/terraform/components/dl/lambda_event_source_mapping_file_scanner.tf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| resource "aws_lambda_event_source_mapping" "file_scanner" { | ||
| event_source_arn = module.sqs_scanner.sqs_queue_arn | ||
| function_name = module.file_scanner.function_name | ||
| batch_size = 10 | ||
| maximum_batching_window_in_seconds = 5 | ||
| function_response_types = ["ReportBatchItemFailures"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
infrastructure/terraform/components/dl/module_lambda_file_scanner.tf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| module "file_scanner" { | ||
| source = "https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.29/terraform-lambda.zip" | ||
|
|
||
| function_name = "file-scanner" | ||
| description = "A function for extracting PDFs from DocumentReference and storing in UnscannedFiles bucket for virus scanning" | ||
|
|
||
| aws_account_id = var.aws_account_id | ||
| component = local.component | ||
| environment = var.environment | ||
| project = var.project | ||
| region = var.region | ||
| group = var.group | ||
|
|
||
| log_retention_in_days = var.log_retention_in_days | ||
| kms_key_arn = module.kms.key_arn | ||
|
|
||
| iam_policy_document = { | ||
| body = data.aws_iam_policy_document.file_scanner_lambda.json | ||
| } | ||
|
|
||
| function_s3_bucket = local.acct.s3_buckets["lambda_function_artefacts"]["id"] | ||
| function_code_base_path = local.aws_lambda_functions_dir_path | ||
| function_code_dir = "file-scanner-lambda/dist" | ||
| function_include_common = true | ||
| handler_function_name = "handler" | ||
| runtime = "nodejs22.x" | ||
| memory = 512 | ||
| timeout = 60 | ||
| log_level = var.log_level | ||
|
|
||
| force_lambda_code_deploy = var.force_lambda_code_deploy | ||
| enable_lambda_insights = false | ||
|
|
||
| log_destination_arn = local.log_destination_arn | ||
| log_subscription_role_arn = local.acct.log_subscription_role_arn | ||
|
|
||
| lambda_env_vars = { | ||
| "DOCUMENT_REFERENCE_BUCKET" = module.s3bucket_pii_data.bucket | ||
| "UNSCANNED_FILES_BUCKET" = local.unscanned_files_bucket | ||
| "UNSCANNED_FILES_PATH_PREFIX" = var.environment | ||
nhsd-angel-pastor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "EVENT_PUBLISHER_EVENT_BUS_ARN" = aws_cloudwatch_event_bus.main.arn | ||
| "EVENT_PUBLISHER_DLQ_URL" = module.sqs_event_publisher_errors.sqs_queue_url | ||
| } | ||
| } | ||
|
|
||
| data "aws_iam_policy_document" "file_scanner_lambda" { | ||
| statement { | ||
| sid = "ReadDocumentReferenceBucket" | ||
| effect = "Allow" | ||
|
|
||
| actions = [ | ||
| "s3:GetObject", | ||
| "s3:ListBucket", | ||
| ] | ||
|
|
||
| resources = [ | ||
| module.s3bucket_pii_data.arn, | ||
| "${module.s3bucket_pii_data.arn}/*", | ||
| ] | ||
| } | ||
|
|
||
| statement { | ||
| sid = "WriteUnscannedFilesBucket" | ||
| effect = "Allow" | ||
|
|
||
| actions = [ | ||
| "s3:PutObject", | ||
| ] | ||
|
|
||
| resources = [ | ||
| "arn:aws:s3:::${local.unscanned_files_bucket}/*", | ||
| ] | ||
| } | ||
|
|
||
| statement { | ||
| sid = "PutEvents" | ||
| effect = "Allow" | ||
|
|
||
| actions = [ | ||
| "events:PutEvents", | ||
| ] | ||
|
|
||
| resources = [ | ||
| aws_cloudwatch_event_bus.main.arn, | ||
| ] | ||
| } | ||
|
|
||
| statement { | ||
| sid = "SQSPermissionsDLQs" | ||
| effect = "Allow" | ||
|
|
||
| actions = [ | ||
| "sqs:SendMessage", | ||
| "sqs:SendMessageBatch", | ||
| ] | ||
|
|
||
| resources = [ | ||
| module.sqs_event_publisher_errors.sqs_queue_arn, | ||
| ] | ||
| } | ||
|
|
||
| statement { | ||
| sid = "SQSPermissionsScannerQueue" | ||
| effect = "Allow" | ||
|
|
||
| actions = [ | ||
| "sqs:ReceiveMessage", | ||
| "sqs:DeleteMessage", | ||
| "sqs:GetQueueAttributes", | ||
| "sqs:GetQueueUrl", | ||
| ] | ||
|
|
||
| resources = [ | ||
| module.sqs_scanner.sqs_queue_arn, | ||
| ] | ||
| } | ||
|
|
||
| statement { | ||
| sid = "KMSPermissions" | ||
| effect = "Allow" | ||
|
|
||
| actions = [ | ||
| "kms:Decrypt", | ||
| "kms:GenerateDataKey", | ||
| ] | ||
|
|
||
| resources = [ | ||
| module.kms.key_arn, | ||
| ] | ||
| } | ||
| } | ||
41 changes: 41 additions & 0 deletions
41
infrastructure/terraform/components/dl/module_sqs_scanner.tf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| module "sqs_scanner" { | ||
| source = "https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.24/terraform-sqs.zip" | ||
|
|
||
| aws_account_id = var.aws_account_id | ||
| component = local.component | ||
| environment = var.environment | ||
| project = var.project | ||
| region = var.region | ||
| name = "file-scanner" | ||
| sqs_kms_key_arn = module.kms.key_arn | ||
| visibility_timeout_seconds = 60 | ||
| delay_seconds = 0 | ||
| create_dlq = true | ||
| sqs_policy_overload = data.aws_iam_policy_document.sqs_scanner.json | ||
| } | ||
|
|
||
| data "aws_iam_policy_document" "sqs_scanner" { | ||
| statement { | ||
| sid = "AllowEventBridgeToSendMessage" | ||
| effect = "Allow" | ||
|
|
||
| principals { | ||
| type = "Service" | ||
| identifiers = ["events.amazonaws.com"] | ||
| } | ||
|
|
||
| actions = [ | ||
| "sqs:SendMessage" | ||
| ] | ||
|
|
||
| resources = [ | ||
| "arn:aws:sqs:${var.region}:${var.aws_account_id}:${local.csi}-file-scanner-queue" | ||
| ] | ||
Ian-Hodges marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| condition { | ||
| test = "ArnEquals" | ||
| variable = "aws:SourceArn" | ||
| values = [aws_cloudwatch_event_rule.item_dequeued.arn] | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import { baseJestConfig } from '../../jest.config.base'; | ||
|
|
||
| const config = baseJestConfig; | ||
|
|
||
| export default config; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| { | ||
| "dependencies": { | ||
| "@aws-sdk/client-s3": "^3.908.0", | ||
| "aws-lambda": "^1.0.7", | ||
| "digital-letters-events": "^0.0.1", | ||
| "utils": "^0.0.1" | ||
| }, | ||
| "devDependencies": { | ||
| "@tsconfig/node22": "^22.0.2", | ||
| "@types/aws-lambda": "^8.10.155", | ||
| "@types/jest": "^29.5.14", | ||
| "jest": "^29.7.0", | ||
| "jest-mock-extended": "^3.0.7", | ||
| "typescript": "^5.9.3" | ||
| }, | ||
| "name": "nhs-notify-digital-letters-file-scanner-lambda", | ||
| "private": true, | ||
| "scripts": { | ||
| "lambda-build": "rm -rf dist && npx esbuild --bundle --minify --sourcemap --target=es2020 --platform=node --loader:.node=file --entry-names=[name] --outdir=dist src/index.ts", | ||
| "lint": "eslint .", | ||
| "lint:fix": "eslint . --fix", | ||
| "test:unit": "jest", | ||
| "typecheck": "tsc --noEmit" | ||
| }, | ||
| "version": "0.0.1" | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.