Skip to content

Commit 701cbe4

Browse files
committed
PoC for 902: to be re-engineered IV (with test bucket in TF)
1 parent e820103 commit 701cbe4

File tree

4 files changed

+100
-11
lines changed

4 files changed

+100
-11
lines changed

infrastructure/instance/ecs_batch_processor_config.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ resource "aws_ecs_task_definition" "ecs_task" {
207207
name = "ACK_BUCKET_NAME"
208208
value = aws_s3_bucket.batch_data_destination_bucket.bucket
209209
},
210+
{
211+
name = "EA_BUCKET_NAME"
212+
value = aws_s3_bucket.batch_data_ea_bucket.bucket
213+
},
210214
{
211215
name = "KINESIS_STREAM_ARN"
212216
value = local.kinesis_arn

infrastructure/instance/file_name_processor.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ resource "aws_lambda_function" "file_processor_lambda" {
277277

278278
environment {
279279
variables = {
280+
ACCOUNT_ID = var.immunisation_account_id
280281
SOURCE_BUCKET_NAME = aws_s3_bucket.batch_data_source_bucket.bucket
281282
ACK_BUCKET_NAME = aws_s3_bucket.batch_data_destination_bucket.bucket
282283
QUEUE_URL = aws_sqs_queue.batch_file_created.url

infrastructure/instance/s3_config.tf

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,78 @@ resource "aws_s3_bucket_policy" "batch_config_bucket_policy" {
231231
]
232232
})
233233
}
234+
235+
# ---
236+
# temp: test output bucket for EA files. pending DPS bucket.
237+
238+
resource "aws_s3_bucket" "batch_data_ea_bucket" {
239+
bucket = "${local.batch_prefix}-data-ea"
240+
force_destroy = local.is_temp
241+
}
242+
243+
resource "aws_s3_bucket_public_access_block" "batch_data_ea_bucket_public_access_block" {
244+
bucket = aws_s3_bucket.batch_data_ea_bucket.id
245+
246+
block_public_acls = true
247+
block_public_policy = true
248+
ignore_public_acls = true
249+
restrict_public_buckets = true
250+
}
251+
252+
resource "aws_s3_bucket_policy" "batch_data_ea_bucket_policy" {
253+
bucket = aws_s3_bucket.batch_data_ea_bucket.id
254+
policy = jsonencode({
255+
Version : "2012-10-17",
256+
Statement : [
257+
{
258+
Effect : "Allow",
259+
Principal : {
260+
AWS : "arn:aws:iam::${var.dspp_core_account_id}:root"
261+
},
262+
Action : var.environment == "prod" ? [
263+
"s3:ListBucket",
264+
"s3:GetObject",
265+
] : [
266+
"s3:ListBucket",
267+
"s3:GetObject",
268+
"s3:DeleteObject"
269+
],
270+
Resource : [
271+
aws_s3_bucket.batch_data_ea_bucket.arn,
272+
"${aws_s3_bucket.batch_data_ea_bucket.arn}/*"
273+
]
274+
},
275+
{
276+
Sid = "HTTPSOnly"
277+
Effect = "Deny"
278+
Principal = {
279+
"AWS" : "*"
280+
}
281+
Action = "s3:*"
282+
Resource = [
283+
aws_s3_bucket.batch_data_ea_bucket.arn,
284+
"${aws_s3_bucket.batch_data_ea_bucket.arn}/*",
285+
]
286+
Condition = {
287+
Bool = {
288+
"aws:SecureTransport" = "false"
289+
}
290+
}
291+
},
292+
]
293+
})
294+
}
295+
296+
resource "aws_s3_bucket_server_side_encryption_configuration" "s3_batch_ea_encryption" {
297+
bucket = aws_s3_bucket.batch_data_ea_bucket.id
298+
299+
rule {
300+
apply_server_side_encryption_by_default {
301+
kms_master_key_id = data.aws_kms_key.existing_s3_encryption_key.arn
302+
sse_algorithm = "aws:kms"
303+
}
304+
}
305+
}
306+
307+
# ---
308+

lambdas/filenameprocessor/src/file_name_processor.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from common.log_decorator import logging_decorator
1919
from common.models.errors import UnhandledAuditTableError
2020
from constants import (
21+
EA_BUCKET_NAME,
2122
ERROR_TYPE_TO_STATUS_CODE_MAP,
2223
SOURCE_BUCKET_NAME,
2324
FileNotProcessedReason,
@@ -42,7 +43,6 @@
4243
# We could implement a new lambda triggered on it BUT if it's never triggered, we never get the upsert.
4344

4445
EXPECTED_BUCKET_OWNER_ACCOUNT = os.getenv("ACCOUNT_ID")
45-
TEST_EA_BUCKET = "902-test-ea-bucket"
4646
TEST_EA_FILENAME = "Vaccination_Extended_Attributes"
4747

4848

@@ -103,7 +103,16 @@ def handle_record(record) -> dict:
103103
# here: if it's an EA file, move it, and upsert it to PROCESSING; use the bucket name as the queue name
104104
if TEST_EA_FILENAME in file_key:
105105
queue_name = "TEST_COVID"
106-
dest_bucket_name = TEST_EA_BUCKET
106+
dest_bucket_name = EA_BUCKET_NAME
107+
108+
upsert_audit_table(
109+
message_id,
110+
file_key,
111+
created_at_formatted_string,
112+
expiry_timestamp,
113+
queue_name,
114+
FileStatus.PROCESSING,
115+
)
107116

108117
s3_client = get_s3_client()
109118
s3_client.copy_object(
@@ -114,14 +123,6 @@ def handle_record(record) -> dict:
114123
ExpectedSourceBucketOwner=EXPECTED_BUCKET_OWNER_ACCOUNT,
115124
)
116125

117-
upsert_audit_table(
118-
message_id,
119-
file_key,
120-
created_at_formatted_string,
121-
expiry_timestamp,
122-
dest_bucket_name,
123-
FileStatus.PROCESSING,
124-
)
125126
logger.info("Lambda invocation successful for file '%s'", file_key)
126127

127128
# TODO: check the file is in the dest bucket, upsert again accordingly.
@@ -144,7 +145,7 @@ def handle_record(record) -> dict:
144145
file_key,
145146
created_at_formatted_string,
146147
expiry_timestamp,
147-
dest_bucket_name,
148+
queue_name,
148149
file_status,
149150
)
150151
s3_client.delete_object(
@@ -156,6 +157,14 @@ def handle_record(record) -> dict:
156157
status_code = 400
157158
message = (f"Failed to send to {dest_bucket_name} for further processing",)
158159
file_status = FileStatus.FAILED
160+
upsert_audit_table(
161+
message_id,
162+
file_key,
163+
created_at_formatted_string,
164+
expiry_timestamp,
165+
queue_name,
166+
file_status,
167+
)
159168
move_file(bucket_name, file_key, f"archive/{file_key}")
160169

161170
# Return details for logs

0 commit comments

Comments
 (0)