|
1 | 1 | // WARNING! All statement blocks MUST have a UNIQUE SID, this is to allow the individual documents to be merged. |
2 | 2 | // Statement blocks with the same SID will replace each other when merged. |
3 | 3 |
|
| 4 | +locals { |
| 5 | + glue_access_presets = { |
| 6 | + read_only = [ |
| 7 | + "glue:Get*", |
| 8 | + "glue:BatchGet*", |
| 9 | + ] |
| 10 | + read_write = [ |
| 11 | + "glue:Get*", |
| 12 | + "glue:BatchGet*", |
| 13 | + "glue:Create*", |
| 14 | + "glue:Update*", |
| 15 | + "glue:Delete*", |
| 16 | + "glue:BatchCreate*", |
| 17 | + "glue:BatchUpdate*", |
| 18 | + "glue:BatchDelete*", |
| 19 | + ] |
| 20 | + } |
| 21 | + |
| 22 | + common_department_databases = [ |
| 23 | + aws_glue_catalog_database.raw_zone_catalog_database.name, |
| 24 | + aws_glue_catalog_database.refined_zone_catalog_database.name, |
| 25 | + aws_glue_catalog_database.trusted_zone_catalog_database.name, |
| 26 | + "unrestricted-*-zone", |
| 27 | + "${var.identifier_prefix}-raw-zone-unrestricted-addresses-api" |
| 28 | + ] |
| 29 | +} |
| 30 | + |
4 | 31 | // S3 read only access policy |
5 | 32 | data "aws_iam_policy_document" "read_only_s3_department_access" { |
6 | 33 | # Include CloudTrail bucket access for data-and-insight department |
@@ -176,21 +203,28 @@ data "aws_iam_policy_document" "read_only_glue_access" { |
176 | 203 | "glue:SearchTables", |
177 | 204 | "glue:Query*", |
178 | 205 | ] |
179 | | - resources = ["*"] |
| 206 | + resources = flatten([ |
| 207 | + ["arn:aws:glue:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:catalog"], |
| 208 | + [for db in local.common_department_databases : "arn:aws:glue:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:database/${db}"], |
| 209 | + [for db in local.common_department_databases : "arn:aws:glue:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:table/${db}/*"] |
| 210 | + ]) |
180 | 211 | } |
181 | 212 |
|
182 | 213 | dynamic "statement" { |
183 | | - for_each = var.additional_glue_database_access |
184 | | - iterator = additional_db_access |
| 214 | + for_each = { |
| 215 | + read_only = var.additional_glue_database_access.read_only |
| 216 | + read_write = var.additional_glue_database_access.read_write |
| 217 | + } |
| 218 | + iterator = access_level |
185 | 219 | content { |
186 | | - sid = "AdditionalGlueDatabaseAccess${replace(additional_db_access.value.database_name, "/[^a-zA-Z0-9]/", "")}" |
| 220 | + sid = "AdditionalGlueDatabaseAccess${title(replace(access_level.key, "_", ""))}" |
187 | 221 | effect = "Allow" |
188 | | - actions = additional_db_access.value.actions |
189 | | - resources = [ |
190 | | - "arn:aws:glue:eu-west-2:${data.aws_caller_identity.current.account_id}:catalog", |
191 | | - "arn:aws:glue:eu-west-2:${data.aws_caller_identity.current.account_id}:database/${additional_db_access.value.database_name}", |
192 | | - "arn:aws:glue:eu-west-2:${data.aws_caller_identity.current.account_id}:table/${additional_db_access.value.database_name}/*" |
193 | | - ] |
| 222 | + actions = local.glue_access_presets[access_level.key] |
| 223 | + resources = length(access_level.value) > 0 ? flatten([ |
| 224 | + ["arn:aws:glue:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:catalog"], |
| 225 | + [for db in access_level.value : "arn:aws:glue:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:database/${db}"], |
| 226 | + [for db in access_level.value : "arn:aws:glue:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:table/${db}/*"] |
| 227 | + ]) : [] |
194 | 228 | } |
195 | 229 | } |
196 | 230 | } |
@@ -521,7 +555,29 @@ data "aws_iam_policy_document" "glue_access" { |
521 | 555 | "glue:GetDatabases", |
522 | 556 | "glue:Query*", |
523 | 557 | ] |
524 | | - resources = ["*"] |
| 558 | + resources = flatten([ |
| 559 | + ["arn:aws:glue:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:catalog"], |
| 560 | + [for db in local.common_department_databases : "arn:aws:glue:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:database/${db}"], |
| 561 | + [for db in local.common_department_databases : "arn:aws:glue:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:table/${db}/*"] |
| 562 | + ]) |
| 563 | + } |
| 564 | + |
| 565 | + dynamic "statement" { |
| 566 | + for_each = { |
| 567 | + read_only = var.additional_glue_database_access.read_only |
| 568 | + read_write = var.additional_glue_database_access.read_write |
| 569 | + } |
| 570 | + iterator = access_level |
| 571 | + content { |
| 572 | + sid = "AdditionalGlueDatabaseFullAccess${title(replace(access_level.key, "_", ""))}" |
| 573 | + effect = "Allow" |
| 574 | + actions = local.glue_access_presets[access_level.key] |
| 575 | + resources = length(access_level.value) > 0 ? flatten([ |
| 576 | + ["arn:aws:glue:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:catalog"], |
| 577 | + [for db in access_level.value : "arn:aws:glue:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:database/${db}"], |
| 578 | + [for db in access_level.value : "arn:aws:glue:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:table/${db}/*"] |
| 579 | + ]) : [] |
| 580 | + } |
525 | 581 | } |
526 | 582 | } |
527 | 583 |
|
|
0 commit comments