|
| 1 | +# Example - Spot Runner - Private subnet |
| 2 | + |
| 3 | +In this scenario the runner agent is running on a single EC2 node. |
| 4 | + |
| 5 | +The example is intended to show how the runner can be configured for self-hosted Gitlab environments with certificates signed by a custom CA. |
| 6 | + |
| 7 | +> This currently only works with the `docker` executor. Support for the `docker+machine` executor is not yet implemented. Contributions are welcome. |
| 8 | +
|
| 9 | +## Prerequisites |
| 10 | + |
| 11 | +The terraform version is managed using [tfenv](https://github.com/Zordrak/tfenv). If you are not using `tfenv` please check `.terraform-version` for the tested version. |
| 12 | + |
| 13 | +Before configuring certificates, it is important to review the [Gitlab documentation](https://docs.gitlab.com/runner/configuration/tls-self-signed.html). |
| 14 | + |
| 15 | +In particular, note the following docker images are involved: |
| 16 | + |
| 17 | +> - The **Runner helper image**, which is used to handle Git, artifacts, and cache operations. In this scenario, the user only needs to make a certificate file available at a specific location (for example, /etc/gitlab-runner/certs/ca.crt), and the Docker container will automatically install it for the user. |
| 18 | +
|
| 19 | +> - The **user image**, which is used to run the user script. In this scenario, the user must take ownership regarding how to install a certificate, since this is highly dependent on the image itself, and the Runner has no way of knowing how to install a certificate in each possible scenario. |
| 20 | +
|
| 21 | +### Certificates for the runner-helper image |
| 22 | + |
| 23 | +The Gitlab **runner-helper image** needs to communicate with your Gitlab instance. |
| 24 | + |
| 25 | +Create a PEM-encoded `.crt` file containing the public certificate of your Gitlab server instance. |
| 26 | + |
| 27 | +```hcl |
| 28 | +module { |
| 29 | + ... |
| 30 | + # Public cert of my companys gitlab instance |
| 31 | + runners_gitlab_certificate = file("${path.module}/my_gitlab_instance_cert.crt") |
| 32 | + ... |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +Add your CA and intermediary certs to a second PEM-encoded `.crt` file. |
| 37 | +```hcl |
| 38 | +module { |
| 39 | + ... |
| 40 | + # Other public certs relating to my company. |
| 41 | + runners_ca_certificate = file("${path.module}/my_company_ca_cert_bundle.crt") |
| 42 | + ... |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +### Certificates for user images |
| 47 | + |
| 48 | +For **user images**, you must: |
| 49 | + |
| 50 | +1. Mount the certificates from the EC2 host into all user images. |
| 51 | + |
| 52 | +The runner module can be configured to do this step. Configure the module like so: |
| 53 | + |
| 54 | +```terraform |
| 55 | +module { |
| 56 | + # ... |
| 57 | + |
| 58 | + # Mount EC2 host certs in docker so all user docker images can reference them. |
| 59 | + runners_additional_volumes = ["/etc/gitlab-runner/certs/:/etc/gitlab-runner/certs:ro"] |
| 60 | + |
| 61 | + # ... |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +2. Trust the certificates from within the user image. |
| 66 | + |
| 67 | +Each user image will need to execute commands to copy the certificates into the correct place and trust them. |
| 68 | + |
| 69 | +The below examples some ways to do this, assuming user images with the Ubuntu OS or similar. |
| 70 | +For Alpine OS user images, the specific commands may differ. |
| 71 | + |
| 72 | +**Option 1:** Build a custom user image and update your `Dockerfile`: |
| 73 | +```docker |
| 74 | + FROM python:3 # Some base image |
| 75 | +
|
| 76 | + RUN apt-get -y update |
| 77 | + RUN apt-get -y upgrade |
| 78 | +
|
| 79 | + RUN apt-get install -y ca-certificates |
| 80 | + RUN cp /etc/gitlab-runner/certs/* /usr/local/share/ca-certificates/ |
| 81 | + RUN update-ca-certificates |
| 82 | + ... |
| 83 | +``` |
| 84 | + |
| 85 | +**Option 2:** Add a section to each pipeline using `before_script`: |
| 86 | + |
| 87 | +This change would need to be added to every pipeline file which requires certificates. |
| 88 | +It could be customised depending on the OS of the pipeline user image. |
| 89 | + |
| 90 | +```yaml |
| 91 | +default: |
| 92 | + before_script: |
| 93 | + # Install certificates into user image |
| 94 | + - apt-get install -y ca-certificates |
| 95 | + - cp /etc/gitlab-runner/certs/* /usr/local/share/ca-certificates/ |
| 96 | + - update-ca-certificates |
| 97 | +``` |
| 98 | +
|
| 99 | +**Option 3:** Add the script from Option 2 into `runners_pre_build_script` variable: |
| 100 | + |
| 101 | +This avoids maintaining the script in each pipeline file, but expects that all user images use the same OS. |
| 102 | + |
| 103 | +```terraform |
| 104 | +module { |
| 105 | + # ... |
| 106 | +
|
| 107 | + runners_pre_build_script = <<EOT |
| 108 | + ''' |
| 109 | + apt-get install -y ca-certificates |
| 110 | + cp /etc/gitlab-runner/certs/* /usr/local/share/ca-certificates/ |
| 111 | + update-ca-certificates |
| 112 | + ''' |
| 113 | + EOT |
| 114 | +
|
| 115 | + # ... |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +<!-- BEGIN_TF_DOCS --> |
| 120 | +<!-- END_TF_DOCS --> |
0 commit comments