Skip to main content
Version: Development

Read OpenBao secrets from GitLab CI jobs

This guide shows how a GitLab job can read secrets from OpenBao without storing a long-lived OpenBao token in GitLab.

GitLab's native Vault integration

GitLab Premium and Ultimate also support the secrets:vault keyword. It uses an ID token to authenticate with HashiCorp Vault and fetch secrets directly into a CI job.

This guide does not cover this integration. See the GitLab documentation for more details.

How it works​

GitLab and OpenBao use two different tokens during this process:

  • GitLab ID token: A signed JWT generated by GitLab for a CI job. It provides information about the job, such as the project and branch it belongs to.
  • OpenBao token: A token issued by OpenBao after validating the GitLab ID token. It gives the job access to secrets based on the assigned policies.

The process works as follows:

  1. GitLab starts a CI job and generates a signed ID token for it.
  2. The job sends this token to OpenBao using the JWT auth method.
  3. OpenBao validates the token and checks whether the job is allowed to authenticate.
  4. If validation succeeds, OpenBao returns a short-lived token with the policies assigned to the role.
  5. The job uses this token to read the secrets it has access to.

The GitLab ID token is generated when the job runs. It is not stored as a CI/CD variable, and there is no need to create or manage it manually.

info

For more information about the JWT auth method, see JWT Auth Method.

Before you start​

You need:

  • The OpenBao CLI installed and configured to talk to your OpenBao server. See Install the CLI.

  • An OpenBao token that can enable and configure the JWT auth method, create roles and policies, and write secrets.

  • A GitLab project where you're a Maintainer and can create CI jobs.

  • A runner that can reach the OpenBao API.

  • If secret/ is not already mounted as KV v2, you also need permission to enable a secrets engine.

  • OpenBao must be able to reach GitLab. Test it from the OpenBao host:

    $ curl -fsS https://gitlab.example.com/oauth/discovery/keys

You should see a JSON response with the public keys. If not, check your firewall and DNS settings.

gitlab.example.com is a placeholder for your GitLab domain. If you're using a self-hosted GitLab instance, replace it with your actual domain. If you're using GitLab.com, use gitlab.com instead.

Compatibility

Tested with OpenBao 2.7.2, GitLab CE 19.5.5 and GitLab Runner 19.5.3. This works on every GitLab tier.

Try it on your own laptop

No servers? Use a free GitLab.com project and run everything else locally.

This example is for local use only, not for production. For production, you should use CA-signed certificates and a real domain name.

  1. Start a dev server. It prints a root token.

    $ bao server -dev
  2. Open another terminal and set the OpenBao environment variables:

    $ export BAO_ADDR="http://127.0.0.1:8200"
    $ export BAO_TOKEN="<root-token>"
  3. On GitLab.com, open Settings > CI/CD > Runners and create a new runner. Then run it locally:

    $ gitlab-runner register --non-interactive --url https://gitlab.com \
    --token <runner-token> --executor shell
    $ gitlab-runner run
  4. Follow this guide, but use:

    • https://gitlab.com as the GitLab URL.
    • BAO_ADDR: http://127.0.0.1:8200 in the pipeline.
    • No image: and no apk add line.

    Make sure curl and jq are installed on the machine running the runner.

The dev server loses data (in-memory storage) when restarted, but it's enough to test the integration.

Step 1: Trust GitLab​

From the CLI, run the following commands to enable the JWT auth method and configure it to trust GitLab's ID tokens.

Make sure the following environment variables are set:

  • BAO_ADDR must be set to your OpenBao API address.
  • BAO_TOKEN must be set to a token with the permissions from Before you start.

For self-hosted GitLab, replace gitlab.example.com with your actual domain.

$ bao auth enable jwt
$ bao write auth/jwt/config \
jwks_url="https://gitlab.example.com/oauth/discovery/keys" \
bound_issuer="https://gitlab.example.com"

bound_issuer must match the iss claim in the GitLab ID token exactly.

If GitLab uses a private CA that the OpenBao host does not already trust through its system certificates, also set jwks_ca_pem.

For GitLab.com:

$ bao write auth/jwt/config \
jwks_url="https://gitlab.com/oauth/discovery/keys" \
bound_issuer="https://gitlab.com"

Step 2: Create a secret and write a policy​

This example uses a KV v2 secrets engine mounted at secret/. The dev server enables it by default.

On a non-dev server, make sure secret/ is mounted as KV v2. If it is not mounted, enable it:

$ bao secrets enable -path=secret kv-v2

Create the secret that the GitLab job will read:

$ bao kv put -mount=secret ci/test-app password="example-password"

Save this policy as gitlab-ci.hcl:

path "secret/data/ci/test-app" {
capabilities = ["read"]
}

Then write it:

$ bao policy write gitlab-ci gitlab-ci.hcl

KV v2 paths need data/ in the policy. See KV v2.

Step 3: Create a role​

Find your project ID on the project overview page. Save this as gitlab-ci-role.json:

{
"role_type": "jwt",
"user_claim": "project_id",
"bound_audiences": ["https://openbao.example.com"],
"bound_claims": {
"project_id": "<project-id>",
"ref_protected": "true"
},
"token_policies": ["gitlab-ci"],
"token_ttl": "5m",
"token_max_ttl": "10m"
}

Then write it:

$ bao write auth/jwt/role/gitlab-ci @gitlab-ci-role.json

This role allows only:

  • Tokens with the aud value https://openbao.example.com.
  • Jobs from your project.
  • Jobs on protected branches or tags.

Step 4: Add the job​

read-secret:
image:
name: ghcr.io/openbao/openbao:2.7.2
entrypoint: [""]
variables:
BAO_ADDR: https://openbao.example.com

id_tokens:
BAO_ID_TOKEN:
aud: https://openbao.example.com

script:
- BAO_TOKEN="$(bao write -field=token auth/jwt/login role=gitlab-ci jwt="$BAO_ID_TOKEN")"
- export BAO_TOKEN
- SECRET="$(bao kv get -mount=secret -field=password ci/test-app)"
- bao token revoke -self
- echo "Secret read from OpenBao"
# Use "$SECRET" in your deployment command. Do not print it.

The aud value must match bound_audiences in the role.

info

Both jobs revoke the token right after reading the secret. Otherwise, the token stays valid until its 5-minute TTL ends. It is good practice to revoke short-lived tokens as soon as they are no longer needed.

Step 5: Testing​

Run the job on a protected branch or tag. It should succeed and read the secret.

Example output on protected branch
$ BAO_TOKEN="$(bao write -field=token auth/jwt/login role=gitlab-ci jwt="$BAO_ID_TOKEN")"
$ export BAO_TOKEN
$ SECRET="$(bao kv get -mount=secret -field=password ci/test-app)"
$ bao token revoke -self
Success! Revoked token (if it existed)
$ echo "Secret read from OpenBao"
Secret read from OpenBao
Job succeeded
Example output on unprotected branch (feature-x)
$ BAO_TOKEN="$(bao write -field=token auth/jwt/login role=gitlab-ci jwt="$BAO_ID_TOKEN")"
Error writing data to auth/jwt/login: Error making API request.

URL: PUT https://openbao.example.com/v1/auth/jwt/login
Code: 400. Errors:

* error validating claims: claim "ref_protected" does not match any associated bound claim values
ERROR: Job failed: exit code 2

Troubleshooting​

ErrorCauseFix
invalid issuer (iss) claimbound_issuer with trailing slashMake sure bound_issuer matches the iss claim in the GitLab ID token exactly.
audience claim found in JWT but no audiences bound to the roleRole without bound_audiencesAdd bound_audiences to the role.
role with oidc role_type is not allowedrole_type set to oidcSet role_type to jwt.
permission deniedPolicy path without data/Check that the policy allows read on the requested path. For KV v2, include data/ in the policy path.