Skip to content

Commit 0f6ebaa

Browse files
committed
eli-306 splitting out KMS policies for each s3 object + dynamo, for Lambda access
1 parent 8185e13 commit 0f6ebaa

1 file changed

Lines changed: 63 additions & 40 deletions

File tree

infrastructure/stacks/api-layer/iam_policies.tf

Lines changed: 63 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -127,71 +127,94 @@ resource "aws_iam_role_policy" "external_s3_write_policy" {
127127
}
128128

129129
## KMS
130-
data "aws_iam_policy_document" "kms_key_policy" {
130+
data "aws_iam_policy_document" "dynamodb_kms_key_policy" {
131131
statement {
132132
sid = "EnableIamUserPermissions"
133133
effect = "Allow"
134134
principals {
135135
type = "AWS"
136136
identifiers = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"]
137137
}
138-
actions = ["kms:*"]
139-
resources = [
140-
module.eligibility_status_table.dynamodb_kms_key_arn,
141-
module.s3_rules_bucket.storage_bucket_kms_key_arn,
142-
module.s3_audit_bucket.storage_bucket_kms_key_arn,
143-
module.eligibility_signposting_api_gateway.kms_key_arn,
138+
actions = ["kms:*"]
139+
resources = ["*"]
140+
}
144141

145-
]
142+
statement {
143+
sid = "AllowLambdaDecrypt"
144+
effect = "Allow"
145+
principals {
146+
type = "AWS"
147+
identifiers = [aws_iam_role.eligibility_lambda_role.arn]
148+
}
149+
actions = ["kms:Decrypt"]
150+
resources = ["*"]
146151
}
152+
}
153+
154+
resource "aws_kms_key_policy" "dynamodb_kms_key" {
155+
key_id = module.eligibility_status_table.dynamodb_kms_key_id
156+
policy = data.aws_iam_policy_document.dynamodb_kms_key_policy.json
157+
}
158+
159+
data "aws_iam_policy_document" "s3_rules_kms_key_policy" {
147160
statement {
148-
sid = "Allow lambda decrypt role"
161+
sid = "EnableIamUserPermissions"
149162
effect = "Allow"
150163
principals {
151-
type = "AWS"
152-
identifiers = [
153-
aws_iam_role.eligibility_lambda_role.arn
154-
]
164+
type = "AWS"
165+
identifiers = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"]
155166
}
156-
actions = [
157-
"kms:Decrypt"
158-
]
159-
resources = [
160-
module.eligibility_status_table.dynamodb_kms_key_arn,
161-
module.s3_rules_bucket.storage_bucket_kms_key_arn,
162-
]
167+
actions = ["kms:*"]
168+
resources = ["*"]
163169
}
164170

165171
statement {
166-
sid = "Allow lambda full write role"
172+
sid = "AllowLambdaDecrypt"
167173
effect = "Allow"
168174
principals {
169-
type = "AWS"
170-
identifiers = [
171-
aws_iam_role.eligibility_lambda_role.arn
172-
]
175+
type = "AWS"
176+
identifiers = [aws_iam_role.eligibility_lambda_role.arn]
173177
}
174-
actions = [
178+
actions = ["kms:Decrypt"]
179+
resources = ["*"]
180+
}
181+
}
182+
183+
resource "aws_kms_key_policy" "s3_rules_kms_key" {
184+
key_id = module.s3_rules_bucket.storage_bucket_kms_key_arn
185+
policy = data.aws_iam_policy_document.s3_rules_kms_key_policy.json
186+
}
187+
188+
data "aws_iam_policy_document" "s3_audit_kms_key_policy" {
189+
statement {
190+
sid = "EnableIamUserPermissions"
191+
effect = "Allow"
192+
principals {
193+
type = "AWS"
194+
identifiers = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"]
195+
}
196+
actions = ["kms:*"]
197+
resources = ["*"]
198+
}
199+
200+
statement {
201+
sid = "AllowLambdaFullWrite"
202+
effect = "Allow"
203+
principals {
204+
type = "AWS"
205+
identifiers = [aws_iam_role.eligibility_lambda_role.arn]
206+
}
207+
actions = [
175208
"kms:Decrypt",
176209
"kms:Encrypt",
177210
"kms:GenerateDataKey",
178211
"kms:DescribeKey"
179212
]
180-
resources = [
181-
module.s3_audit_bucket.storage_bucket_kms_key_arn,
182-
]
213+
resources = ["*"]
183214
}
184215
}
185216

186-
# attach kms decrypt policy kms key
187-
resource "aws_kms_key_policy" "kms_key" {
188-
key_id = module.eligibility_status_table.dynamodb_kms_key_id
189-
policy = data.aws_iam_policy_document.kms_key_policy.json
190-
}
191-
192-
resource "aws_kms_grant" "lambda_s3_decrypt" {
193-
name = "lambda-s3-decrypt"
194-
key_id = module.s3_rules_bucket.storage_bucket_kms_key_arn
195-
grantee_principal = aws_iam_role.eligibility_lambda_role.arn
196-
operations = ["Decrypt"]
217+
resource "aws_kms_key_policy" "s3_audit_kms_key" {
218+
key_id = module.s3_audit_bucket.storage_bucket_kms_key_arn
219+
policy = data.aws_iam_policy_document.s3_audit_kms_key_policy.json
197220
}

0 commit comments

Comments
 (0)