Skip to content

Commit 3e14fea

Browse files
authored
fix: Update KMS key policy to prevent public access. (#41)
1 parent ee0a32e commit 3e14fea

2 files changed

Lines changed: 12 additions & 8 deletions

File tree

kms.tf

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ resource "aws_kms_key" "fargate" {
33
deletion_window_in_days = var.key_recovery_period
44
enable_key_rotation = true
55
policy = jsonencode(yamldecode(templatefile("${path.module}/templates/key-policy.yaml.tftpl", {
6-
account_id : data.aws_caller_identity.identity.account_id,
7-
partition : data.aws_partition.current.partition,
8-
region : data.aws_region.current.name,
9-
repository_name : local.prefix,
6+
account_id : data.aws_caller_identity.identity.account_id
7+
ecs_roles : [aws_iam_role.execution.arn, aws_iam_role.task.arn]
8+
partition : data.aws_partition.current.partition
9+
region : data.aws_region.current.name
10+
repository_name : local.prefix
1011
})))
1112

1213
tags = var.tags

templates/key-policy.yaml.tftpl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Version: '2012-10-17'
1+
Version: "2012-10-17"
22
Id: Fargate hosting key policy
33
Statement:
44
- Sid: Enable IAM User Permissions
@@ -20,11 +20,14 @@ Statement:
2020
Condition:
2121
StringEquals:
2222
kms:CallerAccount: "${account_id}"
23-
kms:EncryptionContext:aws:ecr:arn: arn:${partition}:ecr:${region}:${account_id}:repository/${repository_name}
24-
- Sid: Allow Fargate containers to encrypt objects
23+
kms:EncryptionContext:aws:ecr:arn: "arn:${partition}:ecr:${region}:${account_id}:repository/${repository_name}"
24+
- Sid: Allow ECS containers to encrypt objects
2525
Effect: Allow
2626
Resource: "*"
2727
Principal:
28-
AWS: "*"
28+
AWS:
29+
%{ for arn in ecs_roles ~}
30+
- "${arn}"
31+
%{ endfor }
2932
Action:
3033
- kms:GenerateDataKey*

0 commit comments

Comments
 (0)