Skip to content

Commit b59ed24

Browse files
committed
VED-1029: Specify KMS key when copying extended attributes files to DPS.
1 parent d4e7065 commit b59ed24

8 files changed

Lines changed: 34 additions & 23 deletions

File tree

infrastructure/instance/environments/prod/blue/variables.tfvars

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ batch_error_notifications_enabled = true
77
create_mesh_processor = true
88
has_sub_environment_scope = false
99
dspp_submission_s3_bucket_name = "nhsd-dspp-core-prod-s3-submission-upload"
10-
dspp_submission_kms_key_alias = "nhsd-dspp-core-prod-s3-submission-upload-key"
10+
dspp_submission_kms_key_alias = "alias/nhsd-dspp-core-prod-s3-submission-upload-key"

infrastructure/instance/environments/prod/green/variables.tfvars

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ batch_error_notifications_enabled = true
77
create_mesh_processor = true
88
has_sub_environment_scope = false
99
dspp_submission_s3_bucket_name = "nhsd-dspp-core-prod-s3-submission-upload"
10-
dspp_submission_kms_key_alias = "nhsd-dspp-core-prod-s3-submission-upload-key"
10+
dspp_submission_kms_key_alias = "alias/nhsd-dspp-core-prod-s3-submission-upload-key"

infrastructure/instance/file_name_processor.tf

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ resource "aws_iam_policy" "filenameprocessor_dps_extended_attribute_kms_policy"
261261
Resource = "arn:aws:kms:eu-west-2:${var.dspp_core_account_id}:key/*",
262262
"Condition" = {
263263
"ForAnyValue:StringEquals" = {
264-
"kms:ResourceAliases" = "alias/${var.dspp_submission_kms_key_alias}"
264+
"kms:ResourceAliases" = var.dspp_submission_kms_key_alias
265265
}
266266
}
267267
}
@@ -315,17 +315,18 @@ resource "aws_lambda_function" "file_processor_lambda" {
315315

316316
environment {
317317
variables = {
318-
ACCOUNT_ID = var.immunisation_account_id
319-
DPS_ACCOUNT_ID = var.dspp_core_account_id
320-
SOURCE_BUCKET_NAME = aws_s3_bucket.batch_data_source_bucket.bucket
321-
ACK_BUCKET_NAME = aws_s3_bucket.batch_data_destination_bucket.bucket
322-
DPS_BUCKET_NAME = var.dspp_submission_s3_bucket_name
323-
QUEUE_URL = aws_sqs_queue.batch_file_created.url
324-
REDIS_HOST = data.aws_elasticache_cluster.existing_redis.cache_nodes[0].address
325-
REDIS_PORT = data.aws_elasticache_cluster.existing_redis.cache_nodes[0].port
326-
SPLUNK_FIREHOSE_NAME = module.splunk.firehose_stream_name
327-
AUDIT_TABLE_NAME = aws_dynamodb_table.audit-table.name
328-
AUDIT_TABLE_TTL_DAYS = 60
318+
ACCOUNT_ID = var.immunisation_account_id
319+
DPS_ACCOUNT_ID = var.dspp_core_account_id
320+
SOURCE_BUCKET_NAME = aws_s3_bucket.batch_data_source_bucket.bucket
321+
ACK_BUCKET_NAME = aws_s3_bucket.batch_data_destination_bucket.bucket
322+
DPS_BUCKET_NAME = var.dspp_submission_s3_bucket_name
323+
DPS_BUCKET_KMS_KEY_ALIAS = var.dspp_submission_kms_key_alias
324+
QUEUE_URL = aws_sqs_queue.batch_file_created.url
325+
REDIS_HOST = data.aws_elasticache_cluster.existing_redis.cache_nodes[0].address
326+
REDIS_PORT = data.aws_elasticache_cluster.existing_redis.cache_nodes[0].port
327+
SPLUNK_FIREHOSE_NAME = module.splunk.firehose_stream_name
328+
AUDIT_TABLE_NAME = aws_dynamodb_table.audit-table.name
329+
AUDIT_TABLE_TTL_DAYS = 60
329330
}
330331
}
331332
kms_key_arn = data.aws_kms_key.existing_lambda_encryption_key.arn

infrastructure/instance/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ variable "dspp_submission_s3_bucket_name" {
3636
variable "dspp_submission_kms_key_alias" {
3737
description = "Alias of the DSPP (DPS) KMS key required to encrypt extended attributes files"
3838
type = string
39-
default = "nhsd-dspp-core-ref-s3-submission-upload-key"
39+
default = "alias/nhsd-dspp-core-ref-s3-submission-upload-key"
4040
}
4141

4242
variable "create_mesh_processor" {

lambdas/filenameprocessor/src/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515

1616
DPS_DESTINATION_BUCKET_NAME = os.getenv("DPS_BUCKET_NAME")
17+
DPS_DESTINATION_BUCKET_KMS_KEY_ALIAS = os.getenv("DPS_BUCKET_KMS_KEY_ALIAS")
1718
EXPECTED_SOURCE_BUCKET_ACCOUNT = os.getenv("ACCOUNT_ID")
1819
EXPECTED_DPS_DESTINATION_ACCOUNT = os.getenv("DPS_ACCOUNT_ID")
1920
AUDIT_TABLE_NAME = os.getenv("AUDIT_TABLE_NAME")

lambdas/filenameprocessor/src/file_name_processor.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from common.log_decorator import logging_decorator
1818
from common.models.errors import UnhandledAuditTableError
1919
from constants import (
20+
DPS_DESTINATION_BUCKET_KMS_KEY_ALIAS,
2021
DPS_DESTINATION_BUCKET_NAME,
2122
DPS_DESTINATION_PREFIX,
2223
ERROR_TYPE_TO_STATUS_CODE_MAP,
@@ -271,6 +272,7 @@ def handle_extended_attributes_file(
271272
dest_file_key,
272273
EXPECTED_DPS_DESTINATION_ACCOUNT,
273274
EXPECTED_SOURCE_BUCKET_ACCOUNT,
275+
DPS_DESTINATION_BUCKET_KMS_KEY_ALIAS,
274276
)
275277

276278
move_file(bucket_name, file_key, f"{EXTENDED_ATTRIBUTES_ARCHIVE_PREFIX}/{file_key}")

lambdas/filenameprocessor/tests/utils_for_tests/mock_environment_variables.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class BucketNames:
1212
CONFIG = "immunisation-batch-internal-dev-data-configs"
1313
SOURCE = "immunisation-batch-internal-dev-data-sources"
1414
DESTINATION = "immunisation-batch-internal-dev-data-destinations"
15-
DPS_DESTINATION = "nhsd-dspp-core-ref-extended-attributes-gdp"
15+
DPS_DESTINATION = "nhsd-dspp-core-ref-s3-submission-upload"
1616
# Mock firehose bucket used for testing only (due to limitations of the moto testing package)
1717
MOCK_FIREHOSE = "mock-firehose-bucket"
1818

@@ -38,6 +38,7 @@ class Sqs:
3838
"SOURCE_BUCKET_NAME": BucketNames.SOURCE,
3939
"ACK_BUCKET_NAME": BucketNames.DESTINATION,
4040
"DPS_BUCKET_NAME": BucketNames.DPS_DESTINATION,
41+
"DPS_BUCKET_KMS_KEY_ALIAS": "alias/nhsd-dspp-core-ref-s3-submission-upload-key",
4142
"ACCOUNT_ID": MOCK_ACCOUNT_ID,
4243
"DPS_ACCOUNT_ID": MOCK_ACCOUNT_ID,
4344
"QUEUE_URL": "https://sqs.eu-west-2.amazonaws.com/123456789012/imms-batch-file-created-queue.fifo",

lambdas/shared/src/common/aws_s3_utils.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,21 @@ def copy_file_to_external_bucket(
2222
destination_key: str,
2323
expected_bucket_owner: str,
2424
expected_source_bucket_owner: str,
25+
sse_kms_key_id: str | None = None,
2526
) -> None:
27+
copy_params = {
28+
"CopySource": {"Bucket": source_bucket, "Key": source_key},
29+
"Bucket": destination_bucket,
30+
"Key": destination_key,
31+
"ExpectedBucketOwner": expected_bucket_owner,
32+
"ExpectedSourceBucketOwner": expected_source_bucket_owner,
33+
}
34+
35+
if sse_kms_key_id:
36+
copy_params["SSEKMSKeyId"] = sse_kms_key_id
37+
2638
s3_client = get_s3_client()
27-
s3_client.copy_object(
28-
CopySource={"Bucket": source_bucket, "Key": source_key},
29-
Bucket=destination_bucket,
30-
Key=destination_key,
31-
ExpectedBucketOwner=expected_bucket_owner,
32-
ExpectedSourceBucketOwner=expected_source_bucket_owner,
33-
)
39+
s3_client.copy_object(**copy_params)
3440

3541

3642
def delete_file(

0 commit comments

Comments
 (0)