Skip to content

Commit 7186c7d

Browse files
committed
CCM-8572: add lambdas tf and set up routing
1 parent 07b6765 commit 7186c7d

10 files changed

+198
-135
lines changed

infrastructure/terraform/modules/backend-api/cloudwatch_event_rule_virus_scan_complete.tf

Lines changed: 0 additions & 60 deletions
This file was deleted.
Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
resource "aws_cloudwatch_event_rule" "virus_scan_failed" {
22
name = "${local.csi}-virus-scan-failed"
3-
description = "Forwards enriched events to SQS from quarantine bucket where GuardDuty virus scan has failed"
3+
description = "Forwards enriched events from quarantine bucket where GuardDuty virus scan has failed"
44

55
event_pattern = jsonencode({
66
source = ["templates.${var.environment}.${var.project}"]
@@ -18,40 +18,39 @@ resource "aws_cloudwatch_event_rule" "virus_scan_failed" {
1818
})
1919
}
2020

21-
resource "aws_cloudwatch_event_target" "virus_scan_failed" {
21+
resource "aws_cloudwatch_event_target" "scan_failed_delete_object" {
2222
rule = aws_cloudwatch_event_rule.virus_scan_failed.name
23-
arn = module.sqs_virus_scan_failed.sqs_queue_arn
24-
role_arn = aws_iam_role.virus_scan_failed_to_sqs.arn
23+
arn = module.lambda_delete_failed_scanned_object.function_arn
24+
role_arn = aws_iam_role.handle_scan_failed.arn
2525
}
2626

27-
resource "aws_iam_role" "virus_scan_failed_to_sqs" {
28-
name = "${local.csi}-virus-scan-failed-to-sqs"
27+
resource "aws_cloudwatch_event_target" "scan_failed_set_file_status" {
28+
rule = aws_cloudwatch_event_rule.virus_scan_failed.name
29+
arn = module.lambda_set_letter_file_virus_scan_status.function_arn
30+
role_arn = aws_iam_role.handle_scan_failed.arn
31+
}
32+
33+
resource "aws_iam_role" "handle_scan_failed" {
34+
name = "${local.csi}-virus-scan-failed"
2935
assume_role_policy = data.aws_iam_policy_document.events_assume_role.json
3036
}
3137

32-
resource "aws_iam_role_policy" "virus_scan_failed_to_sqs" {
33-
name = "${local.csi}-virus-scan-failed-to-sqs"
34-
role = aws_iam_role.virus_scan_failed_to_sqs.id
35-
policy = data.aws_iam_policy_document.virus_scan_failed_to_sqs.json
38+
resource "aws_iam_role_policy" "handle_scan_failed" {
39+
name = "${local.csi}-virus-scan-failed"
40+
role = aws_iam_role.handle_scan_failed.id
41+
policy = data.aws_iam_policy_document.handle_scan_failed.json
3642
}
3743

38-
data "aws_iam_policy_document" "virus_scan_failed_to_sqs" {
44+
data "aws_iam_policy_document" "handle_scan_failed" {
3945
version = "2012-10-17"
4046

4147
statement {
42-
sid = "AllowSQSSendMessage"
43-
effect = "Allow"
44-
actions = ["sqs:SendMessage"]
45-
resources = [module.sqs_virus_scan_failed.sqs_queue_arn]
46-
}
47-
48-
statement {
49-
sid = "AllowKMS"
50-
effect = "Allow"
51-
actions = [
52-
"kms:Decrypt",
53-
"kms:GenerateDataKey"
48+
sid = "AllowLambdaInvoke"
49+
effect = "Allow"
50+
actions = ["lambda:InvokeFunction"]
51+
resources = [
52+
module.lambda_delete_failed_scanned_object.function_arn,
53+
module.lambda_set_letter_file_virus_scan_status.function_arn,
5454
]
55-
resources = [var.kms_key_arn]
5655
}
5756
}

infrastructure/terraform/modules/backend-api/locals.tf

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ locals {
1515
})
1616

1717
backend_lambda_entrypoints = {
18-
create_template = "src/templates/create.ts"
19-
create_letter_template = "src/templates/create-letter.ts"
20-
get_template = "src/templates/get.ts"
21-
update_template = "src/templates/update.ts"
22-
list_template = "src/templates/list.ts"
23-
template_client = "src/index.ts"
18+
create_template = "src/templates/create.ts"
19+
create_letter_template = "src/templates/create-letter.ts"
20+
get_template = "src/templates/get.ts"
21+
update_template = "src/templates/update.ts"
22+
list_template = "src/templates/list.ts"
23+
template_client = "src/index.ts"
24+
set_letter_file_virus_scan_status = "src/set-letter-file-virus-scan-status.ts"
2425
}
2526

2627
dynamodb_kms_key_arn = var.dynamodb_kms_key_arn == "" ? aws_kms_key.dynamo[0].arn : var.dynamodb_kms_key_arn

infrastructure/terraform/modules/backend-api/module_build_template_lambda.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module "build_template_lambda" {
88
local.backend_lambda_entrypoints.get_template,
99
local.backend_lambda_entrypoints.update_template,
1010
local.backend_lambda_entrypoints.list_template,
11+
local.backend_lambda_entrypoints.set_letter_file_virus_scan_status,
1112
]
1213
}
1314

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module "build_virus_scan_lambdas" {
2+
source = "../typescript-build-zip"
3+
4+
source_code_dir = abspath("${path.module}/../../../../lambdas/virus-scan")
5+
6+
entrypoints = [
7+
"src/copy-scanned-object-to-internal.ts",
8+
"src/delete-failed-scanned-object.ts",
9+
"src/get-s3-object-tags.ts"
10+
]
11+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
module "lambda_copy_scanned_object_to_internal" {
2+
source = "../lambda-function"
3+
description = "Copies quarantine files that have passed virus scan check to internal bucket"
4+
5+
function_name = "${local.csi}-copy-scanned-object-to-internal"
6+
filename = module.build_virus_scan_lambdas.zips["src/copy-scanned-object-to-internal.ts"].path
7+
source_code_hash = module.build_virus_scan_lambdas.zips["src/copy-scanned-object-to-internal.ts"].base64sha256
8+
handler = "copy-scanned-object-to-internal.handler"
9+
10+
environment_variables = {
11+
TEMPLATES_INTERNAL_S3_BUCKET_NAME = module.s3bucket_internal.id
12+
}
13+
14+
log_retention_in_days = var.log_retention_in_days
15+
16+
execution_role_policy_document = data.aws_iam_policy_document.copy_scanned_object_to_internal.json
17+
}
18+
19+
data "aws_iam_policy_document" "copy_scanned_object_to_internal" {
20+
statement {
21+
sid = "AllowS3QuarantineList"
22+
effect = "Allow"
23+
24+
actions = [
25+
"s3:ListBucket",
26+
"s3:ListBucketVersions",
27+
]
28+
29+
resources = [module.s3bucket_quarantine.arn]
30+
}
31+
32+
statement {
33+
sid = "AllowS3QuarantineGetObject"
34+
effect = "Allow"
35+
36+
actions = [
37+
"s3:GetObject",
38+
"s3:GetObjectVersion",
39+
"s3:GetObjectTagging",
40+
"s3:GetObjectVersionTagging",
41+
]
42+
43+
resources = ["${module.s3bucket_quarantine.arn}/*"]
44+
}
45+
46+
statement {
47+
sid = "AllowS3InternalWrite"
48+
effect = "Allow"
49+
50+
actions = [
51+
"s3:PutObject",
52+
"s3:PutObjectVersion",
53+
"s3:PutObjectTagging",
54+
"s3:PutObjectVersionTagging",
55+
]
56+
57+
resources = ["${module.s3bucket_internal.arn}/*"]
58+
}
59+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module "lambda_delete_failed_scanned_object" {
2+
source = "../lambda-function"
3+
description = "Deletes quarantine files that have failed virus scan check"
4+
5+
function_name = "${local.csi}-delete-failed-scanned-object"
6+
filename = module.build_virus_scan_lambdas.zips["src/delete-failed-scanned-object.ts"].path
7+
source_code_hash = module.build_virus_scan_lambdas.zips["src/delete-failed-scanned-object.ts"].base64sha256
8+
handler = "delete-failed-scanned-object.handler"
9+
10+
log_retention_in_days = var.log_retention_in_days
11+
12+
execution_role_policy_document = data.aws_iam_policy_document.delete_failed_scanned_object.json
13+
}
14+
15+
data "aws_iam_policy_document" "delete_failed_scanned_object" {
16+
statement {
17+
sid = "AllowS3QuarantineDelete"
18+
effect = "Allow"
19+
20+
actions = [
21+
"s3:DeleteObject"
22+
]
23+
24+
resources = ["${module.s3bucket_quarantine.arn}/*"]
25+
}
26+
}

infrastructure/terraform/modules/backend-api/module_lambda_get_s3_object_tags.tf

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ module "lambda_get_s3_object_tags" {
33
description = "Get S3 Object Tags"
44

55
function_name = "${local.csi}-get-s3-object-tags"
6-
filename = module.build_get_s3_object_tags_lambda.zips["src/get-s3-object-tags.ts"].path
7-
source_code_hash = module.build_get_s3_object_tags_lambda.zips["src/get-s3-object-tags.ts"].base64sha256
6+
filename = module.build_virus_scan_lambdas.zips["src/get-s3-object-tags.ts"].path
7+
source_code_hash = module.build_virus_scan_lambdas.zips["src/get-s3-object-tags.ts"].base64sha256
88
handler = "get-s3-object-tags.handler"
99

1010
log_retention_in_days = var.log_retention_in_days
@@ -13,29 +13,30 @@ module "lambda_get_s3_object_tags" {
1313
}
1414

1515
data "aws_iam_policy_document" "get_s3_object_tags" {
16-
statement {
17-
sid = "AllowSQS"
18-
effect = "Allow"
16+
# TODO: should this be here? Not on the pipe?
17+
# statement {
18+
# sid = "AllowSQS"
19+
# effect = "Allow"
1920

20-
actions = [
21-
"sqs:DeleteMessage",
22-
"sqs:GetQueueAttributes",
23-
"sqs:ReceiveMessage",
24-
]
21+
# actions = [
22+
# "sqs:DeleteMessage",
23+
# "sqs:GetQueueAttributes",
24+
# "sqs:ReceiveMessage",
25+
# ]
2526

26-
resources = [module.sqs_quarantine_tags_added.sqs_queue_arn]
27-
}
27+
# resources = [module.sqs_quarantine_tags_added.sqs_queue_arn]
28+
# }
2829

29-
statement {
30-
sid = "AllowKMS"
31-
effect = "Allow"
30+
# statement {
31+
# sid = "AllowKMS"
32+
# effect = "Allow"
3233

33-
actions = [
34-
"kms:Decrypt",
35-
]
34+
# actions = [
35+
# "kms:Decrypt",
36+
# ]
3637

37-
resources = [var.kms_key_arn]
38-
}
38+
# resources = [var.kms_key_arn]
39+
# }
3940

4041
statement {
4142
sid = "AllowS3Read"
@@ -51,14 +52,3 @@ data "aws_iam_policy_document" "get_s3_object_tags" {
5152
resources = ["${module.s3bucket_quarantine.arn}/*"]
5253
}
5354
}
54-
55-
module "build_get_s3_object_tags_lambda" {
56-
source = "../typescript-build-zip"
57-
58-
source_code_dir = abspath("${path.module}/../../../../lambdas/get-s3-object-tags")
59-
60-
entrypoints = [
61-
"src/get-s3-object-tags.ts"
62-
]
63-
}
64-
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
module "lambda_set_letter_file_virus_scan_status" {
2+
source = "../lambda-function"
3+
description = "Sets virus scan status on letter files"
4+
5+
function_name = "${local.csi}-set-letter-file-virus-scan-status"
6+
filename = module.build_template_lambda.zips[local.backend_lambda_entrypoints.set_letter_file_virus_scan_status].path
7+
source_code_hash = module.build_template_lambda.zips[local.backend_lambda_entrypoints.set_letter_file_virus_scan_status].base64sha256
8+
handler = "set-letter-file-virus-scan-status.handler"
9+
10+
environment_variables = {
11+
TEMPLATES_TABLE_NAME = aws_dynamodb_table.templates.name
12+
}
13+
14+
15+
log_retention_in_days = var.log_retention_in_days
16+
17+
execution_role_policy_document = data.aws_iam_policy_document.set_letter_file_virus_scan_status.json
18+
}
19+
20+
data "aws_iam_policy_document" "set_letter_file_virus_scan_status" {
21+
statement {
22+
sid = "AllowDynamoAccess"
23+
effect = "Allow"
24+
25+
actions = [
26+
"dynamodb:UpdateItem",
27+
]
28+
29+
resources = [
30+
aws_dynamodb_table.templates.arn,
31+
]
32+
}
33+
34+
statement {
35+
sid = "AllowKMSAccess"
36+
effect = "Allow"
37+
38+
actions = [
39+
"kms:Decrypt",
40+
"kms:DescribeKey",
41+
"kms:Encrypt",
42+
"kms:GenerateDataKey*",
43+
"kms:ReEncrypt*",
44+
]
45+
46+
resources = [
47+
local.dynamodb_kms_key_arn,
48+
]
49+
}
50+
}

infrastructure/terraform/modules/backend-api/module_sqs_virus_scan_complete.tf

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)