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 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:
- GitLab starts a CI job and generates a signed ID token for it.
- The job sends this token to OpenBao using the JWT auth method.
- OpenBao validates the token and checks whether the job is allowed to authenticate.
- If validation succeeds, OpenBao returns a short-lived token with the policies assigned to the role.
- 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.
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.
Tested with OpenBao 2.7.2, GitLab CE 19.5.5 and GitLab Runner 19.5.3. This works on every GitLab tier.
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.
-
Start a dev server. It prints a root token.
$ bao server -dev -
Open another terminal and set the OpenBao environment variables:
$ export BAO_ADDR="http://127.0.0.1:8200"$ export BAO_TOKEN="<root-token>" -
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 -
Follow this guide, but use:
https://gitlab.comas the GitLab URL.BAO_ADDR: http://127.0.0.1:8200in the pipeline.- No
image:and noapk addline.
Make sure
curlandjqare 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_ADDRmust be set to your OpenBao API address.BAO_TOKENmust 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
audvaluehttps://openbao.example.com. - Jobs from your project.
- Jobs on protected branches or tags.
Step 4: Add the job
- OpenBao CLI
- curl
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.
read-secret:
image: alpine
variables:
BAO_ADDR: https://openbao.example.com
id_tokens:
BAO_ID_TOKEN:
aud: https://openbao.example.com
script:
- apk add --no-cache curl jq
- |
set -eu
LOGIN=$(curl -sS \
--request POST \
--header "Content-Type: application/json" \
--data "$(jq -n \
--arg role "gitlab-ci" \
--arg jwt "$BAO_ID_TOKEN" \
'{role: $role, jwt: $jwt}')" \
"$BAO_ADDR/v1/auth/jwt/login")
OPENBAO_CI_TOKEN=$(printf '%s' "$LOGIN" | jq -r '.auth.client_token // empty')
if [ -z "$OPENBAO_CI_TOKEN" ]; then
echo "OpenBao login failed: $LOGIN" >&2
exit 1
fi
RESPONSE=$(curl -sS \
--header "X-Vault-Token: $OPENBAO_CI_TOKEN" \
"$BAO_ADDR/v1/secret/data/ci/test-app")
SECRET=$(printf '%s' "$RESPONSE" | jq -r '.data.data.password // empty')
curl -sS --request POST \
--header "X-Vault-Token: $OPENBAO_CI_TOKEN" \
"$BAO_ADDR/v1/auth/token/revoke-self" >/dev/null
if [ -z "$SECRET" ]; then
echo "OpenBao read failed: $(printf '%s' "$RESPONSE" | jq -c '.errors')" >&2
exit 1
fi
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.
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.
- OpenBao CLI
- curl
$ 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
$ 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
$ apk add --no-cache curl jq
...
OK: 14.5 MiB in 27 packages
$ set -eu # collapsed multi-line command
Secret read from OpenBao
Job succeeded
OpenBao login failed: {"errors":["error validating claims: claim \"ref_protected\" does not match any associated bound claim values"]}
ERROR: Job failed: exit code 1
Troubleshooting
| Error | Cause | Fix |
|---|---|---|
invalid issuer (iss) claim | bound_issuer with trailing slash | Make sure bound_issuer matches the iss claim in the GitLab ID token exactly. |
audience claim found in JWT but no audiences bound to the role | Role without bound_audiences | Add bound_audiences to the role. |
role with oidc role_type is not allowed | role_type set to oidc | Set role_type to jwt. |
permission denied | Policy path without data/ | Check that the policy allows read on the requested path. For KV v2, include data/ in the policy path. |