--- type: tutorial --- # Authenticating and Reading Secrets With Hashicorp Vault This tutorial demonstrates how to authenticate, configure, and read secrets with HashiCorp's Vault from GitLab CI/CD. ## Requirements This tutorial assumes you are familiar with GitLab CI/CD and Vault. To follow along, you will need: - An account on GitLab. - A running Vault server and the access required to configure authentication and create roles and policies. NOTE: **Note:** You will need to replace the `vault.example.com` URL below with the URL of your Vault server and `gitlab.example.com` with the URL of your GitLab instance. ## How it works Each job has JSON Web Token (JWT) provided as environment variable named `CI_JOB_JWT`. This JWT can be used to authenticate with Vault using the [JWT Auth](https://www.vaultproject.io/docs/auth/jwt/#jwt-authentication) method. The JWT's payload looks like this: ```json { "jti": "c82eeb0c-5c6f-4a33-abf5-4c474b92b558", # Unique identifier for this token "iss": "gitlab.example.com", # Issuer, the domain of your GitLab instance "iat": 1585710286, # Issued at "nbf": 1585798372, # Not valid before "exp": 1585713886, # Expire at "sub": "job_1212", # Subject (job id) "namespace_id": "1", "namespace_path": "mygroup", "project_id": "22", "project_path": "mygroup/myproject", "user_id": "42", "user_login": "myuser", "user_email": "myuser@example.com" "pipeline_id": "1212", "job_id": "1212", "ref": "auto-deploy-2020-04-01", # Git ref for this job "ref_type": "branch", # Git ref type, branch or tag "ref_protected": "true" # true if this git ref is protected, false otherwise } ``` The JWT is encoded by using RS256 and signed with your GitLab instance's OpenID Connect private key. The expire time for the token will be set to job's timeout, if specifed, or 5 minutes if it is not. The key used to sign this token may change without any notice. In such case retrying the job will generate new JWT using the current signing key. You can use this JWT and your instance's JWKS endpoint (`https://gitlab.example.com/-/jwks`) to authenticate with a Vault server that is configured to allow the JWT Authentication method for authentication. When configuring roles in Vault, you can use [bound_claims](https://www.vaultproject.io/docs/auth/jwt/#bound-claims) to match against the JWT's claims and restrict which secrets each CI job has access to. To communicate with Vault, you can use either its CLI client or perform API requests (using `curl` or another client). ## Example CAUTION: **Caution**: JWTs are credentials, which can grant access to resources. Be careful where you paste them! Let's say you have the passwords for your staging and production databases stored in a Vault server that is running on `http://vault.example.com:8200`. Your staging password is `pa$$w0rd` and your production password is `real-pa$$w0rd`. ```shell $ vault kv get -field=password secret/myproject/staging/db pa$$w0rd $ vault kv get -field=password secret/myproject/production/db real-pa$$w0rd ``` To configure your Vault server, start by enabling the [JWT Auth](https://www.vaultproject.io/docs/auth/jwt/) method: ```shell $ vault auth enable jwt Success! Enabled jwt auth method at: jwt/ ``` Then create policies that allow you to read these secrets (one for each secret): ```shell $ vault policy write myproject-staging - <