Skip to content

Commit 51d63e6

Browse files
Conorebykayman-mk
andauthored
feat: add option to read Gitlab Runner Registration token from SSM (#822)
## Description Adds the ability to read the Gitlab registration token from SSM. If no registration token is passed in, it will look in SSM to find the token to use. This prevents the token from being leaked as part of the user_data. ```hcl module "gitlab_runner" { # ... gitlab_runner_registration_config = { registration_token = "" # this is the default value too # ... } secure_parameter_store_gitlab_runner_registration_token_name = "name-of-ssm-parameter-holding-the-registration-token" ``` Closes #776 Precondition for #186 to get rid of pre-registered runners. ## Migrations required NO ## Verification I modified the runner-default example to not pass in a registration token and added the token to SSM instead. Then I started up the runner and confirmed that it successfully registered with Gitlab. --------- Co-authored-by: Matthias Kay <[email protected]> Co-authored-by: Matthias Kay <[email protected]>
1 parent 1e05a71 commit 51d63e6

File tree

4 files changed

+54
-31
lines changed

4 files changed

+54
-31
lines changed

README.md

+12-3
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ gitlab_runner_registration_config = {
170170
}
171171
```
172172

173+
The registration token can also be read in via SSM parameter store. If no registration token is passed in, the module
174+
will look up the token in the SSM parameter store at the location specified by `secure_parameter_store_gitlab_runner_registration_token_name`.
175+
173176
For migration to the new setup simply add the runner token to the parameter store. Once the runner is started it will lookup the
174177
required values via the parameter store. If the value is `null` a new runner will be registered and a new token created/stored.
175178

@@ -380,12 +383,18 @@ module "runner" {
380383

381384
### Scenario: Use of Spot Fleet
382385

383-
Since spot instances can be taken over by AWS depending on the instance type and AZ you are using, you may want multiple instances types in multiple AZs. This is where spot fleets come in, when there is no capacity on one instance type and one AZ, AWS will take the next instance type and so on. This update has been possible since the [fork](https://gitlab.com/cki-project/docker-machine/-/tree/v0.16.2-gitlab.19-cki.2) of docker-machine supports spot fleets.
386+
Since spot instances can be taken over by AWS depending on the instance type and AZ you are using, you may want multiple instances
387+
types in multiple AZs. This is where spot fleets come in, when there is no capacity on one instance type and one AZ, AWS will take
388+
the next instance type and so on. This update has been possible since the [fork](https://gitlab.com/cki-project/docker-machine/-/tree/v0.16.2-gitlab.19-cki.2)
389+
of docker-machine supports spot fleets.
384390

385-
We have seen that the [fork](https://gitlab.com/cki-project/docker-machine/-/tree/v0.16.2-gitlab.19-cki.2) of docker-machine this module is using consume more RAM using spot fleets.
386-
For comparison, if you launch 50 machines in the same time, it consumes ~1.2GB of RAM. In our case, we had to change the `instance_type` of the runner from `t3.micro` to `t3.small`.
391+
We have seen that the [fork](https://gitlab.com/cki-project/docker-machine/-/tree/v0.16.2-gitlab.19-cki.2) of docker-machine this
392+
module is using consume more RAM using spot fleets.
393+
For comparison, if you launch 50 machines in the same time, it consumes ~1.2GB of RAM. In our case, we had to change the
394+
`instance_type` of the runner from `t3.micro` to `t3.small`.
387395

388396
#### Configuration example
397+
389398
```hcl
390399
module "runner" {
391400
# https://registry.terraform.io/modules/npalm/gitlab-runner/aws/

main.tf

+28-27
Original file line numberDiff line numberDiff line change
@@ -53,33 +53,34 @@ locals {
5353

5454
template_gitlab_runner = templatefile("${path.module}/template/gitlab-runner.tftpl",
5555
{
56-
gitlab_runner_version = var.gitlab_runner_version
57-
docker_machine_version = var.docker_machine_version
58-
docker_machine_download_url = var.docker_machine_download_url
59-
runners_config = local.template_runner_config
60-
runners_userdata = var.runners_userdata
61-
runners_executor = var.runners_executor
62-
runners_install_amazon_ecr_credential_helper = var.runners_install_amazon_ecr_credential_helper
63-
curl_cacert = length(var.runners_gitlab_certificate) > 0 ? "--cacert /etc/gitlab-runner/certs/gitlab.crt" : ""
64-
pre_install_certificates = local.pre_install_certificates
65-
pre_install = var.userdata_pre_install
66-
post_install = var.userdata_post_install
67-
runners_gitlab_url = var.runners_gitlab_url
68-
runners_token = var.runners_token
69-
secure_parameter_store_runner_token_key = local.secure_parameter_store_runner_token_key
70-
secure_parameter_store_runner_sentry_dsn = local.secure_parameter_store_runner_sentry_dsn
71-
secure_parameter_store_region = var.aws_region
72-
gitlab_runner_registration_token = var.gitlab_runner_registration_config["registration_token"]
73-
gitlab_runner_description = var.gitlab_runner_registration_config["description"]
74-
gitlab_runner_tag_list = var.gitlab_runner_registration_config["tag_list"]
75-
gitlab_runner_locked_to_project = var.gitlab_runner_registration_config["locked_to_project"]
76-
gitlab_runner_run_untagged = var.gitlab_runner_registration_config["run_untagged"]
77-
gitlab_runner_maximum_timeout = var.gitlab_runner_registration_config["maximum_timeout"]
78-
gitlab_runner_access_level = lookup(var.gitlab_runner_registration_config, "access_level", "not_protected")
79-
sentry_dsn = var.sentry_dsn
80-
public_key = var.use_fleet == true ? tls_private_key.fleet[0].public_key_openssh : ""
81-
use_fleet = var.use_fleet
82-
private_key = var.use_fleet == true ? tls_private_key.fleet[0].private_key_pem : ""
56+
gitlab_runner_version = var.gitlab_runner_version
57+
docker_machine_version = var.docker_machine_version
58+
docker_machine_download_url = var.docker_machine_download_url
59+
runners_config = local.template_runner_config
60+
runners_userdata = var.runners_userdata
61+
runners_executor = var.runners_executor
62+
runners_install_amazon_ecr_credential_helper = var.runners_install_amazon_ecr_credential_helper
63+
curl_cacert = length(var.runners_gitlab_certificate) > 0 ? "--cacert /etc/gitlab-runner/certs/gitlab.crt" : ""
64+
pre_install_certificates = local.pre_install_certificates
65+
pre_install = var.userdata_pre_install
66+
post_install = var.userdata_post_install
67+
runners_gitlab_url = var.runners_gitlab_url
68+
runners_token = var.runners_token
69+
secure_parameter_store_runner_token_key = local.secure_parameter_store_runner_token_key
70+
secure_parameter_store_runner_sentry_dsn = local.secure_parameter_store_runner_sentry_dsn
71+
secure_parameter_store_gitlab_runner_registration_token_name = var.secure_parameter_store_gitlab_runner_registration_token_name
72+
secure_parameter_store_region = var.aws_region
73+
gitlab_runner_registration_token = lookup(var.gitlab_runner_registration_config, "registration_token", "__GITLAB_REGISTRATION_TOKEN_FROM_SSM__")
74+
gitlab_runner_description = var.gitlab_runner_registration_config["description"]
75+
gitlab_runner_tag_list = var.gitlab_runner_registration_config["tag_list"]
76+
gitlab_runner_locked_to_project = var.gitlab_runner_registration_config["locked_to_project"]
77+
gitlab_runner_run_untagged = var.gitlab_runner_registration_config["run_untagged"]
78+
gitlab_runner_maximum_timeout = var.gitlab_runner_registration_config["maximum_timeout"]
79+
gitlab_runner_access_level = lookup(var.gitlab_runner_registration_config, "access_level", "not_protected")
80+
sentry_dsn = var.sentry_dsn
81+
public_key = var.use_fleet == true ? tls_private_key.fleet[0].public_key_openssh : ""
82+
use_fleet = var.use_fleet
83+
private_key = var.use_fleet == true ? tls_private_key.fleet[0].private_key_pem : ""
8384
})
8485

8586
template_runner_config = templatefile("${path.module}/template/runner-config.tftpl",

template/gitlab-runner.tftpl

+8-1
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,17 @@ then
3232
[[ "$valid_token_response" != "200" ]] && valid_token=false
3333
fi
3434

35+
gitlab_runner_registration_token=${gitlab_runner_registration_token}
36+
# fetch registration token from SSM
37+
if [[ "$gitlab_runner_registration_token" == "__GITLAB_REGISTRATION_TOKEN_FROM_SSM__" ]]
38+
then
39+
gitlab_runner_registration_token=$(aws ssm get-parameter --name "${secure_parameter_store_gitlab_runner_registration_token_name}" --with-decryption --region "${secure_parameter_store_region}" | jq -r ".Parameter | .Value")
40+
fi
41+
3542
if [[ "${runners_token}" == "__REPLACED_BY_USER_DATA__" && "$token" == "null" ]] || [[ "$valid_token" == "false" ]]
3643
then
3744
token=$(curl ${curl_cacert} --request POST -L "${runners_gitlab_url}/api/v4/runners" \
38-
--form "token=${gitlab_runner_registration_token}" \
45+
--form "token=$gitlab_runner_registration_token" \
3946
--form "tag_list=${gitlab_runner_tag_list}" \
4047
--form "description=${gitlab_runner_description}" \
4148
--form "locked=${gitlab_runner_locked_to_project}" \

variables.tf

+6
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,12 @@ variable "gitlab_runner_registration_config" {
615615
}
616616
}
617617

618+
variable "secure_parameter_store_gitlab_runner_registration_token_name" {
619+
description = "The name of the SSM parameter to read the GitLab Runner registration token from."
620+
type = string
621+
default = "gitlab-runner-registration-token"
622+
}
623+
618624
variable "secure_parameter_store_runner_token_key" {
619625
description = "The key name used store the Gitlab runner token in Secure Parameter Store"
620626
type = string

0 commit comments

Comments
 (0)