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

Commit

Permalink
fix(webhook): grant KMS permission to decrypt wehn using EventBridge …
Browse files Browse the repository at this point in the history
…(#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>
  • Loading branch information
npalm and philips-labs-pr|bot authored Oct 29, 2024
1 parent 9c9219b commit 380bcaf
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 13 deletions.
1 change: 1 addition & 0 deletions modules/webhook/direct/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ No modules.
|------|------|
| [aws_cloudwatch_log_group.webhook](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) | resource |
| [aws_iam_role.webhook_lambda](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
| [aws_iam_role_policy.webhook_kms](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
| [aws_iam_role_policy.webhook_logging](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
| [aws_iam_role_policy.webhook_sqs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
| [aws_iam_role_policy.webhook_ssm](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
Expand Down
11 changes: 9 additions & 2 deletions modules/webhook/direct/webhook.tf
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,15 @@ resource "aws_iam_role_policy" "webhook_sqs" {

policy = templatefile("${path.module}/../policies/lambda-publish-sqs-policy.json", {
sqs_resource_arns = jsonencode(var.config.sqs_job_queues_arns)
kms_key_arn = var.config.kms_key_arn != null ? var.config.kms_key_arn : ""
})
}

resource "aws_iam_role_policy" "webhook_kms" {
name = "kms-policy"
role = aws_iam_role.webhook_lambda.name

policy = templatefile("${path.module}/../policies/lambda-kms.json", {
kms_key_arn = var.config.kms_key_arn != null ? var.config.kms_key_arn : "arn:${var.config.aws_partition}:kms:::CMK_NOT_IN_USE"
})
}

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

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

Expand Down
2 changes: 2 additions & 0 deletions modules/webhook/eventbridge/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ No modules.
| [aws_cloudwatch_log_group.webhook](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) | resource |
| [aws_iam_role.dispatcher_lambda](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
| [aws_iam_role.webhook_lambda](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
| [aws_iam_role_policy.dispatcher_kms](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
| [aws_iam_role_policy.dispatcher_logging](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
| [aws_iam_role_policy.dispatcher_sqs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
| [aws_iam_role_policy.dispatcher_ssm](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
| [aws_iam_role_policy.dispatcher_xray](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
| [aws_iam_role_policy.webhook_eventbridge](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
| [aws_iam_role_policy.webhook_kms](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
| [aws_iam_role_policy.webhook_logging](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
| [aws_iam_role_policy.webhook_ssm](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
| [aws_iam_role_policy.xray](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
Expand Down
10 changes: 9 additions & 1 deletion modules/webhook/eventbridge/dispatcher.tf
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,15 @@ resource "aws_iam_role_policy" "dispatcher_sqs" {

policy = templatefile("${path.module}/../policies/lambda-publish-sqs-policy.json", {
sqs_resource_arns = jsonencode(var.config.sqs_job_queues_arns)
kms_key_arn = var.config.kms_key_arn != null ? var.config.kms_key_arn : ""
})
}

resource "aws_iam_role_policy" "dispatcher_kms" {
name = "kms-policy"
role = aws_iam_role.webhook_lambda.name

policy = templatefile("${path.module}/../policies/lambda-kms.json", {
kms_key_arn = var.config.kms_key_arn != null ? var.config.kms_key_arn : "arn:${var.config.aws_partition}:kms:::CMK_NOT_IN_USE"
})
}

Expand Down
9 changes: 9 additions & 0 deletions modules/webhook/eventbridge/webhook.tf
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,15 @@ resource "aws_iam_role_policy" "webhook_ssm" {
})
}

resource "aws_iam_role_policy" "webhook_kms" {
name = "kms-policy"
role = aws_iam_role.webhook_lambda.name

policy = templatefile("${path.module}/../policies/lambda-kms.json", {
kms_key_arn = var.config.kms_key_arn != null ? var.config.kms_key_arn : "arn:${var.config.aws_partition}:kms:::CMK_NOT_IN_USE"
})
}

resource "aws_iam_role_policy" "xray" {
count = var.config.tracing_config.mode != null ? 1 : 0
name = "xray-policy"
Expand Down
13 changes: 13 additions & 0 deletions modules/webhook/policies/lambda-kms.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"kms:Decrypt",
"kms:GenerateDataKey"
],
"Resource": "${kms_key_arn}"
}
]
}
10 changes: 0 additions & 10 deletions modules/webhook/policies/lambda-publish-sqs-policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@
"Effect": "Allow",
"Action": ["sqs:SendMessage", "sqs:GetQueueAttributes"],
"Resource": ${sqs_resource_arns}
%{ if kms_key_arn != "" ~}
},
{
"Effect": "Allow",
"Action": [
"kms:Decrypt",
"kms:GenerateDataKey"
],
"Resource": "${kms_key_arn}"
%{ endif ~}
}
]
}

0 comments on commit 380bcaf

Please sign in to comment.