Skip to content

Commit 7bfe8f7

Browse files
jonmcewenkayman-mk
andauthored
feat!: update docker machine and encrypt all EBS (#1204)
## Description - updates the docker-machine to `0.16.2-gitlab.19-cki.5` to set the encryption key for the instances - fleet: creates new instances with encrypted volumes ## Migrations required This could be a breaking change as you might have to change the key policy for the encryption key to allow EBS to access the key. ``` { "Sid": "Allow access through EBS for all principals in the account that are authorized to use EBS", "Effect": "Allow", "Principal": { "AWS": "*" }, "Action": [ "kms:Encrypt", "kms:Decrypt", "kms:ReEncrypt*", "kms:GenerateDataKey*", "kms:CreateGrant", "kms:DescribeKey" ], "Resource": "*", "Condition": { "StringEquals": { "kms:CallerAccount": "990563477234", "kms:ViaService": "ec2.eu-central-1.amazonaws.com" } } } ``` ## Verification Manually tested the new version. --------- Co-authored-by: Matthias Kay <matthias.kay@hlag.com> Co-authored-by: Matthias Kay <github@matthiaskay.de>
1 parent cfc23b7 commit 7bfe8f7

8 files changed

+57
-9
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
# Terraform module for GitLab auto-scaling runners on AWS spot instances <!-- omit in toc -->
1010

1111
💥 See [issue 819](https://github.com/cattle-ops/terraform-aws-gitlab-runner/issues/819) on how to migrate to v7 smoothly.
12+
💥 See [pr 1204](https://github.com/cattle-ops/terraform-aws-gitlab-runner/pull/1204) on how to migrate to v8 smoothly.
1213

1314
This [Terraform](https://www.terraform.io/) modules creates a [GitLab Runner](https://docs.gitlab.com/runner/). A blog post
1415
describes the original version of the runner. See the post at [040code](https://040code.github.io/2017/12/09/runners-on-the-spot/).

docker_autoscaler.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ resource "aws_launch_template" "this" {
8888
volume_type = var.runner_worker_docker_autoscaler_instance.volume_type
8989
iops = contains(["gp3", "io1", "io2"], var.runner_worker_docker_autoscaler_instance.volume_type) ? var.runner_worker_docker_autoscaler_instance.volume_iops : null
9090
throughput = var.runner_worker_docker_autoscaler_instance.volume_type == "gp3" ? var.runner_worker_docker_autoscaler_instance.volume_throughput : null
91+
encrypted = true
92+
kms_key_id = local.kms_key_arn
9193
}
9294
}
9395

locals.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ locals {
9595
runner_worker_graceful_terminate_heartbeat_timeout = (var.runner_terminate_ec2_lifecycle_timeout_duration == null
9696
? min(7200, tonumber(coalesce(var.runner_gitlab_registration_config.maximum_timeout, 0)) + 300)
9797
: var.runner_terminate_ec2_lifecycle_timeout_duration)
98+
99+
kms_key_arn = local.provided_kms_key == "" && var.enable_managed_kms_key ? aws_kms_key.default[0].arn : local.provided_kms_key
98100
}
99101

100102
resource "local_file" "config_toml" {

logging.tf

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ locals {
1111
log_group_name = var.runner_cloudwatch.log_group_name != null ? var.runner_cloudwatch.log_group_name : var.environment
1212
})
1313
provided_kms_key = var.kms_key_id != "" ? var.kms_key_id : ""
14-
kms_key = local.provided_kms_key == "" && var.enable_managed_kms_key ? aws_kms_key.default[0].arn : local.provided_kms_key
1514
}
1615

1716
resource "aws_cloudwatch_log_group" "environment" {
@@ -25,5 +24,5 @@ resource "aws_cloudwatch_log_group" "environment" {
2524
# ignored as decided by the user
2625
# tfsec:ignore:aws-cloudwatch-log-group-customer-key
2726
# checkov:skip=CKV_AWS_158:Encryption can be enabled by user
28-
kms_key_id = local.kms_key
27+
kms_key_id = local.kms_key_arn
2928
}

main.tf

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ resource "aws_ssm_parameter" "runner_registration_token" {
44
type = "SecureString"
55
value = "null"
66

7-
key_id = local.kms_key
7+
key_id = local.kms_key_arn
88

99
tags = local.tags
1010

@@ -18,7 +18,7 @@ resource "aws_ssm_parameter" "runner_sentry_dsn" {
1818
type = "SecureString"
1919
value = "null"
2020

21-
key_id = local.kms_key
21+
key_id = local.kms_key_arn
2222

2323
tags = local.tags
2424

@@ -118,6 +118,7 @@ locals {
118118
launch_template = var.runner_worker_docker_machine_fleet.enable == true ? aws_launch_template.fleet_gitlab_runner[0].name : ""
119119
docker_machine_options = length(local.docker_machine_options_string) == 1 ? "" : local.docker_machine_options_string
120120
runners_max_growth_rate = var.runner_worker_docker_machine_instance.max_growth_rate
121+
runners_volume_kms_key = local.kms_key_arn
121122
})
122123

123124
template_runner_docker_autoscaler = templatefile("${path.module}/template/runner-docker-autoscaler-config.tftpl",
@@ -387,6 +388,8 @@ resource "aws_launch_template" "fleet_gitlab_runner" {
387388
volume_type = var.runner_worker_docker_machine_instance.volume_type
388389
iops = contains(["gp3", "io1", "io2"], var.runner_worker_docker_machine_instance.volume_type) ? var.runner_worker_docker_machine_instance.volume_iops : null
389390
throughput = var.runner_worker_docker_machine_instance.volume_type == "gp3" ? var.runner_worker_docker_machine_instance.volume_throughput : null
391+
encrypted = true
392+
kms_key_id = local.kms_key_arn
390393
}
391394
}
392395

@@ -445,7 +448,7 @@ module "cache" {
445448
cache_logging_bucket = var.runner_worker_cache.access_log_bucket_id
446449
cache_logging_bucket_prefix = var.runner_worker_cache.access_log_bucket_prefix
447450

448-
kms_key_id = local.kms_key
451+
kms_key_id = local.kms_key_arn
449452

450453
name_iam_objects = local.name_iam_objects
451454

@@ -485,7 +488,7 @@ resource "aws_iam_policy" "instance_kms_policy" {
485488
description = "Allow runner instance the ability to use the KMS key."
486489
policy = templatefile("${path.module}/policies/instance-kms-policy.json",
487490
{
488-
kms_key_arn = var.enable_managed_kms_key && var.kms_key_id == "" ? aws_kms_key.default[0].arn : var.kms_key_id
491+
kms_key_arn = local.kms_key_arn
489492
}
490493
)
491494

@@ -786,7 +789,7 @@ module "terminate_agent_hook" {
786789
name_iam_objects = local.name_iam_objects
787790
name_docker_machine_runners = local.runner_tags_merged["Name"]
788791
role_permissions_boundary = var.iam_permissions_boundary == "" ? null : "arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:policy/${var.iam_permissions_boundary}"
789-
kms_key_id = local.kms_key
792+
kms_key_id = local.kms_key_arn
790793
asg_hook_terminating_heartbeat_timeout = local.runner_worker_graceful_terminate_heartbeat_timeout
791794

792795
tags = local.tags

policies/kms-policy.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,45 @@
2828
"Resource": [
2929
"*"
3030
]
31+
},
32+
{
33+
"Sid": "Allow service-linked role use of the customer managed key",
34+
"Effect": "Allow",
35+
"Principal": {
36+
"AWS": [
37+
"arn:aws:iam::${account_id}:role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling"
38+
]
39+
},
40+
"Action": [
41+
"kms:Encrypt",
42+
"kms:Decrypt",
43+
"kms:ReEncrypt*",
44+
"kms:GenerateDataKey*",
45+
"kms:DescribeKey"
46+
],
47+
"Resource": "*"
48+
},
49+
{
50+
"Sid": "Allow access through EBS for all principals in the account that are authorized to use EBS",
51+
"Effect": "Allow",
52+
"Principal": {
53+
"AWS": "*"
54+
},
55+
"Action": [
56+
"kms:Encrypt",
57+
"kms:Decrypt",
58+
"kms:ReEncrypt*",
59+
"kms:GenerateDataKey*",
60+
"kms:CreateGrant",
61+
"kms:DescribeKey"
62+
],
63+
"Resource": "*",
64+
"Condition": {
65+
"StringEquals": {
66+
"kms:CallerAccount": "${account_id}",
67+
"kms:ViaService": "ec2.${aws_region}.amazonaws.com"
68+
}
69+
}
3170
}
3271
]
3372
}

template/runner-docker-machine-config.tftpl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
%{~ if use_fleet == true ~}
3131
,"amazonec2-ssh-keypath=/root/.ssh/id_rsa",
3232
"amazonec2-use-fleet=${use_fleet}",
33-
"amazonec2-launch-template=${launch_template}"
33+
"amazonec2-launch-template=${launch_template}",
34+
"amazonec2-volume-encrypted=true",
35+
"amazonec2-volume-kms-key=${runners_volume_kms_key}"
3436
%{~ endif ~}
3537
${docker_machine_options}
3638
]

variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ variable "runner_install" {
273273
type = object({
274274
amazon_ecr_credential_helper = optional(bool, false)
275275
docker_machine_download_url = optional(string, "")
276-
docker_machine_version = optional(string, "0.16.2-gitlab.19-cki.2")
276+
docker_machine_version = optional(string, "0.16.2-gitlab.19-cki.5")
277277
pre_install_script = optional(string, "")
278278
post_install_script = optional(string, "")
279279
start_script = optional(string, "")

0 commit comments

Comments
 (0)