Skip to content

fix: add KMS policy statement only if key given #1258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions modules/terminate-agent-hook/iam.tf
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,26 @@ resource "aws_iam_role" "lambda" {
tags = var.tags
}

resource "aws_iam_role_policy_attachment" "lambda_kms" {
count = var.kms_key_id != "" ? 1 : 0

role = aws_iam_role.lambda.name
policy_arn = aws_iam_policy.lambda_kms[0].arn
}

resource "aws_iam_policy" "lambda_kms" {
count = var.kms_key_id != "" ? 1 : 0

name = "${var.name_iam_objects}-${var.name}-lambda-kms"
path = "/"
policy = data.aws_iam_policy_document.kms_key[0].json

tags = var.tags
}

data "aws_iam_policy_document" "kms_key" {
count = var.kms_key_id != "" ? 1 : 0

# This IAM policy is used by the Lambda function.
data "aws_iam_policy_document" "lambda" {
# checkov:skip=CKV_AWS_111:Write access is limited to the resources needed
statement {
sid = "AllowKmsAccess"
Expand All @@ -44,7 +61,9 @@ data "aws_iam_policy_document" "lambda" {
resources = [var.kms_key_id]
effect = "Allow"
}
}

data "aws_iam_policy_document" "lambda" {
# Permit the function to get a list of instances
statement {
sid = "GitLabRunnerLifecycleGetInstances"
Expand Down
2 changes: 1 addition & 1 deletion modules/terminate-agent-hook/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ variable "name_docker_machine_runners" {
}

variable "kms_key_id" {
description = "KMS key id to encrypt the resources, e.g. logs, lambda environment variables, ..."
description = "(optional) KMS key id to encrypt the resources, e.g. logs, lambda environment variables, ..."
type = string
}

Expand Down