Skip to content

feat: add an IAM policy to grant the runner access to the KMS key #778

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 7 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 30 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,32 @@ resource "aws_iam_role" "instance" {
tags = merge(local.tags, var.role_tags)
}

################################################################################
### Policy for the instance to use the KMS key
################################################################################
resource "aws_iam_policy" "instance_kms_policy" {
count = var.enable_kms ? 1 : 0

name = "${local.name_iam_objects}-kms"
path = "/"
description = "Allow runner instance the ability to use the KMS key."
policy = templatefile("${path.module}/policies/instance-kms-policy.json",
{
kms_key_arn = var.enable_kms && var.kms_key_id == "" ? aws_kms_key.default[0].arn : "arn:aws:kms:${var.aws_region}:${data.aws_caller_identity.current.account_id}:key/${var.kms_key_id}"
}
)

tags = local.tags
}

resource "aws_iam_role_policy_attachment" "instance_kms_policy" {
count = var.enable_kms ? 1 : 0

role = var.create_runner_iam_role ? aws_iam_role.instance[0].name : local.aws_iam_role_instance_name
policy_arn = aws_iam_policy.instance_kms_policy[0].arn
}


################################################################################
### Policies for runner agent instance to create docker machines via spot req.
###
Expand Down Expand Up @@ -459,6 +485,8 @@ resource "aws_iam_role" "docker_machine" {
tags = local.tags
}



resource "aws_iam_instance_profile" "docker_machine" {
count = var.runners_executor == "docker+machine" ? 1 : 0
name = "${local.name_iam_objects}-docker-machine"
Expand All @@ -484,6 +512,8 @@ resource "aws_iam_role_policy_attachment" "docker_machine_session_manager_aws_ma
policy_arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/AmazonSSMManagedInstanceCore"
}



################################################################################
### Service linked policy, optional
################################################################################
Expand Down
18 changes: 18 additions & 0 deletions policies/instance-kms-policy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"kms:Encrypt*",
"kms:Decrypt*",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:Describe*"
],
"Effect": "Allow",
"Resource": [
"${kms_key_arn}"
]
}
]
}