Skip to content
This repository was archived by the owner on Jan 16, 2025. It is now read-only.

Commit 380bcaf

Browse files
npalmphilips-labs-pr|bot
and
philips-labs-pr|bot
authored
fix(webhook): grant KMS permission to decrypt wehn using EventBridge (#4220)
## Description This PR grants the webhook (for EventBridge) access to the provided KMS key. In case no key is provided a dummy policy will be created. This to avoid terraform conditon is throwing errors when a KMS key is created in the same Terraform deploy as runner module ## Tested - [x] default example with KMS no eventbridge - [x] default example with KMS and eventbridge - [x] default example without KMS and eventbridge - [x] default example without KMS no eventbridge fix: #4218 --------- Co-authored-by: philips-labs-pr|bot <philips-labs-pr[bot]@users.noreply.github.com>
1 parent 9c9219b commit 380bcaf

File tree

7 files changed

+43
-13
lines changed

7 files changed

+43
-13
lines changed

modules/webhook/direct/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ No modules.
2424
|------|------|
2525
| [aws_cloudwatch_log_group.webhook](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) | resource |
2626
| [aws_iam_role.webhook_lambda](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
27+
| [aws_iam_role_policy.webhook_kms](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
2728
| [aws_iam_role_policy.webhook_logging](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
2829
| [aws_iam_role_policy.webhook_sqs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
2930
| [aws_iam_role_policy.webhook_ssm](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |

modules/webhook/direct/webhook.tf

+9-2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,15 @@ resource "aws_iam_role_policy" "webhook_sqs" {
117117

118118
policy = templatefile("${path.module}/../policies/lambda-publish-sqs-policy.json", {
119119
sqs_resource_arns = jsonencode(var.config.sqs_job_queues_arns)
120-
kms_key_arn = var.config.kms_key_arn != null ? var.config.kms_key_arn : ""
120+
})
121+
}
122+
123+
resource "aws_iam_role_policy" "webhook_kms" {
124+
name = "kms-policy"
125+
role = aws_iam_role.webhook_lambda.name
126+
127+
policy = templatefile("${path.module}/../policies/lambda-kms.json", {
128+
kms_key_arn = var.config.kms_key_arn != null ? var.config.kms_key_arn : "arn:${var.config.aws_partition}:kms:::CMK_NOT_IN_USE"
121129
})
122130
}
123131

@@ -128,7 +136,6 @@ resource "aws_iam_role_policy" "webhook_workflow_job_sqs" {
128136

129137
policy = templatefile("${path.module}/../policies/lambda-publish-sqs-policy.json", {
130138
sqs_resource_arns = jsonencode([var.config.sqs_workflow_job_queue.arn])
131-
kms_key_arn = var.config.kms_key_arn != null ? var.config.kms_key_arn : ""
132139
})
133140
}
134141

modules/webhook/eventbridge/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ No modules.
3030
| [aws_cloudwatch_log_group.webhook](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) | resource |
3131
| [aws_iam_role.dispatcher_lambda](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
3232
| [aws_iam_role.webhook_lambda](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
33+
| [aws_iam_role_policy.dispatcher_kms](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
3334
| [aws_iam_role_policy.dispatcher_logging](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
3435
| [aws_iam_role_policy.dispatcher_sqs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
3536
| [aws_iam_role_policy.dispatcher_ssm](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
3637
| [aws_iam_role_policy.dispatcher_xray](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
3738
| [aws_iam_role_policy.webhook_eventbridge](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
39+
| [aws_iam_role_policy.webhook_kms](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
3840
| [aws_iam_role_policy.webhook_logging](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
3941
| [aws_iam_role_policy.webhook_ssm](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
4042
| [aws_iam_role_policy.xray](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |

modules/webhook/eventbridge/dispatcher.tf

+9-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,15 @@ resource "aws_iam_role_policy" "dispatcher_sqs" {
116116

117117
policy = templatefile("${path.module}/../policies/lambda-publish-sqs-policy.json", {
118118
sqs_resource_arns = jsonencode(var.config.sqs_job_queues_arns)
119-
kms_key_arn = var.config.kms_key_arn != null ? var.config.kms_key_arn : ""
119+
})
120+
}
121+
122+
resource "aws_iam_role_policy" "dispatcher_kms" {
123+
name = "kms-policy"
124+
role = aws_iam_role.webhook_lambda.name
125+
126+
policy = templatefile("${path.module}/../policies/lambda-kms.json", {
127+
kms_key_arn = var.config.kms_key_arn != null ? var.config.kms_key_arn : "arn:${var.config.aws_partition}:kms:::CMK_NOT_IN_USE"
120128
})
121129
}
122130

modules/webhook/eventbridge/webhook.tf

+9
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,15 @@ resource "aws_iam_role_policy" "webhook_ssm" {
127127
})
128128
}
129129

130+
resource "aws_iam_role_policy" "webhook_kms" {
131+
name = "kms-policy"
132+
role = aws_iam_role.webhook_lambda.name
133+
134+
policy = templatefile("${path.module}/../policies/lambda-kms.json", {
135+
kms_key_arn = var.config.kms_key_arn != null ? var.config.kms_key_arn : "arn:${var.config.aws_partition}:kms:::CMK_NOT_IN_USE"
136+
})
137+
}
138+
130139
resource "aws_iam_role_policy" "xray" {
131140
count = var.config.tracing_config.mode != null ? 1 : 0
132141
name = "xray-policy"
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"Version": "2012-10-17",
3+
"Statement": [
4+
{
5+
"Effect": "Allow",
6+
"Action": [
7+
"kms:Decrypt",
8+
"kms:GenerateDataKey"
9+
],
10+
"Resource": "${kms_key_arn}"
11+
}
12+
]
13+
}

modules/webhook/policies/lambda-publish-sqs-policy.json

-10
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,6 @@
55
"Effect": "Allow",
66
"Action": ["sqs:SendMessage", "sqs:GetQueueAttributes"],
77
"Resource": ${sqs_resource_arns}
8-
%{ if kms_key_arn != "" ~}
9-
},
10-
{
11-
"Effect": "Allow",
12-
"Action": [
13-
"kms:Decrypt",
14-
"kms:GenerateDataKey"
15-
],
16-
"Resource": "${kms_key_arn}"
17-
%{ endif ~}
188
}
199
]
2010
}

0 commit comments

Comments
 (0)