Fetching Vault secrets from a GitLab pipeline
Things to consider
Before you start, you must consider:
- Who will access the secret? Who else, besides the GitLab pipeline, should be able to read/write it?
This will influence where in Vault the secret should be located. If the secret is located outside the/secret/gitlab
tree, your GitLab pipeline will need a special role with an attached policy giving read access to the secret. - Do you need to update the secret while your application is running? If yes, then maybe the secret should be read from Vault directly by the application, not the pipeline? Or maybe it's ok to redeploy the application when the secret changes?
Reading a secret from your pipeline
In the pipeline job that needs the secret, you need to add two sections.
The first one is id_tokens
, and it should look like this:
id_tokens:
VAULT_ID_TOKEN:
aud: "https://vault.sikt.no:8200"
Then you need to tell the pipeline where in Vault it should look for the secret, by adding a secrets
section:
secrets:
MY_SECRET:
token: $VAULT_ID_TOKEN
vault: "gitlab/mygroup/myproject/SecretKey@secret"
That section will read the key SecretKey
from the secret named myproject
, located at secret/gitlab/mygroup
, and put the value in a file on the GitLab runner. The variable $MY_SECRET
will contain the filename.
From the Vault UI, this secret would be available at https://vault.sikt.no:8200/ui/vault/secrets/secret/kv/list/gitlab/mygroup/myproject.
If you don't want GitLab to put the value in a file, you must add file: false
to the section.
Example: secret in a file
Case: SSH key
Let's say our GitLab project is myApp
, in the GitLab group myGroup
.
(Project URL: https://gitlab.sikt.no/myGroup/myApp)
This project needs to run a playbook on a remote server using Ansible.
Ansible will be using SSH to connect to the server as the user deploy
, so we need to provide an SSH key for authentication.
We need to fetch the key from Vault, which is located at gitlab/myGroup/myApp
.
The SSH key is stored in the deploy_ssh_key key of that secret.
(Vault URL: https://vault.sikt.no:8200/ui/vault/secrets/secret/kv/list/gitlab/myGroup/myApp)
Here is our job:
run_ansible:
id_tokens:
VAULT_ID_TOKEN:
aud: "https://vault.sikt.no:8200"
secrets:
ANSIBLE_PRIVATE_KEY_FILE:
token: $VAULT_ID_TOKEN
vault: "gitlab/myGroup/myApp/deploy_ssh_key@secret"
script:
- ansible-playbook playbook.yml -i server.example.com, -u deploy