Skip to content

Commit 04632c1

Browse files
authored
DL-5 grant data and insight department permission to use cloudtrail bucket (#2362)
* DL-5 grant data and insight department permission to use cloudtrail bucket * grant the bucket permission to airflow instead of ecs role * make the cloudtrail access become part of the s3 department access * remove the cloudtrail from airflow policy which is included in the s3_access
1 parent f3d9849 commit 04632c1

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
lines changed

terraform/core/05-departments.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ module "department_data_and_insight" {
139139
departmental_airflow_user = true
140140
mwaa_etl_scripts_bucket_arn = aws_s3_bucket.mwaa_etl_scripts_bucket.arn
141141
mwaa_key_arn = aws_kms_key.mwaa_key.arn
142+
cloudtrail_bucket = module.cloudtrail_storage
142143
}
143144

144145
module "department_env_enforcement" {

terraform/modules/department/02-inputs-optional.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,12 @@ variable "additional_s3_access" {
5858
default = []
5959
}
6060

61+
variable "cloudtrail_bucket" {
62+
description = "CloudTrail storage S3 bucket"
63+
type = object({
64+
bucket_id = string
65+
bucket_arn = string
66+
kms_key_arn = string
67+
})
68+
default = null
69+
}

terraform/modules/department/50-aws-iam-policies.tf

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33

44
// S3 read only access policy
55
data "aws_iam_policy_document" "read_only_s3_department_access" {
6+
# Include CloudTrail bucket access for data-and-insight department
7+
source_policy_documents = local.department_identifier == "data-and-insight" && var.cloudtrail_bucket != null ? [
8+
data.aws_iam_policy_document.cloudtrail_access[0].json
9+
] : []
10+
611
statement {
712
sid = "ListAllS3AndKmsKeys"
813
effect = "Allow"
@@ -32,6 +37,8 @@ data "aws_iam_policy_document" "read_only_s3_department_access" {
3237
]
3338
}
3439

40+
41+
3542
dynamic "statement" {
3643
for_each = var.additional_s3_access
3744
iterator = additional_access_item
@@ -90,6 +97,8 @@ data "aws_iam_policy_document" "read_only_s3_department_access" {
9097
]
9198
}
9299

100+
101+
93102
dynamic "statement" {
94103
for_each = var.additional_s3_access
95104
iterator = additional_access_item
@@ -180,6 +189,11 @@ resource "aws_iam_policy" "read_only_glue_access" {
180189

181190
// Full departmental S3 access policy
182191
data "aws_iam_policy_document" "s3_department_access" {
192+
# Include CloudTrail bucket access for data-and-insight department
193+
source_policy_documents = local.department_identifier == "data-and-insight" && var.cloudtrail_bucket != null ? [
194+
data.aws_iam_policy_document.cloudtrail_access[0].json
195+
] : []
196+
183197
statement {
184198
sid = "ListAllS3AndKmsKeys"
185199
effect = "Allow"
@@ -215,6 +229,8 @@ data "aws_iam_policy_document" "s3_department_access" {
215229
]
216230
}
217231

232+
233+
218234
dynamic "statement" {
219235
for_each = var.additional_s3_access
220236
iterator = additional_access_item
@@ -277,10 +293,12 @@ data "aws_iam_policy_document" "s3_department_access" {
277293
var.mwaa_etl_scripts_bucket_arn,
278294
"${var.mwaa_etl_scripts_bucket_arn}/${replace(local.department_identifier, "-", "_")}/*",
279295
"${var.mwaa_etl_scripts_bucket_arn}/unrestricted/*",
280-
"${var.mwaa_etl_scripts_bucket_arn}/shared/*",
296+
"${var.mwaa_etl_scripts_bucket_arn}/shared/*"
281297
]
282298
}
283299

300+
301+
284302
dynamic "statement" {
285303
for_each = var.additional_s3_access
286304
iterator = additional_access_item
@@ -1079,3 +1097,40 @@ resource "aws_iam_policy" "mtfh_access_policy" {
10791097
description = "Allows ${local.department_identifier} department access for ecs tasks to mtfh/ subdirectory in landing zone"
10801098
policy = data.aws_iam_policy_document.mtfh_access[0].json
10811099
}
1100+
1101+
// Read-only CloudTrail access for Data and Insight department only
1102+
data "aws_iam_policy_document" "cloudtrail_access" {
1103+
count = local.department_identifier == "data-and-insight" && var.cloudtrail_bucket != null ? 1 : 0
1104+
1105+
statement {
1106+
sid = "CloudTrailKmsReadAccess"
1107+
effect = "Allow"
1108+
actions = [
1109+
"kms:Decrypt",
1110+
"kms:GenerateDataKey*",
1111+
"kms:DescribeKey"
1112+
]
1113+
resources = [var.cloudtrail_bucket.kms_key_arn]
1114+
}
1115+
1116+
statement {
1117+
sid = "CloudTrailS3ReadAccess"
1118+
effect = "Allow"
1119+
actions = [
1120+
"s3:GetObject",
1121+
"s3:GetObjectVersion",
1122+
"s3:ListBucket"
1123+
]
1124+
resources = [
1125+
var.cloudtrail_bucket.bucket_arn,
1126+
"${var.cloudtrail_bucket.bucket_arn}/*"
1127+
]
1128+
}
1129+
}
1130+
1131+
resource "aws_iam_policy" "cloudtrail_access_policy" {
1132+
count = local.department_identifier == "data-and-insight" && var.cloudtrail_bucket != null ? 1 : 0
1133+
name = lower("${var.identifier_prefix}-${local.department_identifier}-cloudtrail-access-policy")
1134+
description = "Allows ${local.department_identifier} department read-only access to CloudTrail bucket"
1135+
policy = data.aws_iam_policy_document.cloudtrail_access[0].json
1136+
}

terraform/modules/department/50-aws-iam-roles.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ resource "aws_iam_role_policy_attachment" "glue_runner_pass_role_to_glue_for_not
9696
policy_arn = aws_iam_policy.glue_runner_pass_role_to_glue_for_notebook_use.arn
9797
}
9898

99-
# Define a map for the departmentalairflow policies
99+
# Define a map for the departmental airflow policies
100100
locals {
101101
airflow_policy_map = {
102102
s3_access = aws_iam_policy.s3_access.arn,

0 commit comments

Comments
 (0)