GitLab-Vault Ingetration

GitLab integrates with HashiCorp IBM Vault to securely fetch secrets during CI/CD pipelines using JWT authentication, which is secure and designed for that environment. This approach provides:

Features:

How GitLab Authenticates Against Vault

GitLab uses the JWT (JSON Web Token) authentication method to authenticate with Vault. This method is secure and specifically designed for CI/CD environments. Here's how it works:


1. JWT Authentication Flow


2. Vault Configuration for GitLab JWT Authentication

To enable JWT authentication in Vault: - Step 1: Enable the JWT Auth Method: sh vault auth enable jwt - Step 2: Configure GitLab as a JWT Provider: sh vault write auth/jwt/config \ jwks_url="https://<gitlab-domain>/-/jwks" \ bound_issuer="<gitlab-domain>" - Replace <gitlab-domain> with your GitLab instance's domain (e.g., gitlab.com).


Example GitLab CI Job Using Vault

Here’s how you can fetch secrets from Vault in a GitLab CI job:

stages:
  - fetch_secrets

fetch_secrets:
  stage: fetch_secrets
  script:
    - apk add vault jq  # Install Vault CLI and jq
    - export VAULT_TOKEN=$(vault write -field=token auth/jwt/login role=gitlab-ci jwt=$CI_JOB_JWT)
    - export DATABASE_PASSWORD=$(vault kv get -field=password secret/data/my-app/database)
    - echo "Database password: $DATABASE_PASSWORD"

When using JWT authentication for GitLab CI/CD pipelines with HashiCorp Vault, no service accounts or OIDC providers are required. The authentication process relies entirely on GitLab's built-in JWT capabilities and Vault's JWT auth method. Here's why this approach is unique and why it doesn't require additional components like service accounts or OIDC providers:

Why No Service Accounts or OIDC Providers Are Needed


How It Works Without Service Accounts or OIDC


Comparison with OIDC and Service Accounts

Feature GitLab JWT Authentication OIDC Authentication Service Accounts
Identity Provider GitLab (built-in) External OIDC provider (e.g., Google) N/A
Credentials Short-lived JWT OIDC token Long-term API keys/tokens
Integration Complexity Simple (direct GitLab-Vault integration) Requires OIDC provider configuration Requires managing service accounts
Security High (short-lived tokens, no long-term credentials) High (short-lived tokens) Medium (requires careful management of long-term credentials)

Example: GitLab CI Job with Vault (No OIDC or Service Accounts)

Here’s how you can fetch secrets from Vault in a GitLab CI job using JWT authentication:

stages:
  - fetch_secrets

fetch_secrets:
  stage: fetch_secrets
  script:
    - apk add vault jq  # Install Vault CLI and jq
    - export VAULT_TOKEN=$(vault write -field=token auth/jwt/login role=gitlab-ci jwt=$CI_JOB_JWT)
    - export DATABASE_PASSWORD=$(vault kv get -field=password secret/data/my-app/database)
    - echo "Database password: $DATABASE_PASSWORD"