Secrets Management with OpenBao: Ditch .env Files in Git
Every team remembers the exact moment. Someone runs git push, the pipeline goes green, and three days later an alert fires because an AWS key has shown up in a public repository. Or worse: nobody alerts, and the key sits there in the history for two years.
The .env file is convenient, and that convenience is precisely the problem. In this guide you’ll see how to set up secrets management with OpenBao that works for a team in a hurry, with multiple environments and a CI/CD pipeline. The goal is to make the .env in Git unnecessary — not to ban it with a rule nobody follows.
Why .env files in Git are a debt that always comes due
The .gitignore is a promise, not a guarantee. One stray git add -f, one .env.production that wasn’t on the list, or a file copied as .env.bak is all it takes for the secret to end up in the repository. And once it’s in the history, deleting it in the next commit accomplishes nothing.
The problems teams actually hit are pretty repetitive:
- Secrets shared over chat or email. Every new hire gets a
.envover Slack, which is indexed forever. - No rotation. Nobody changes the database password because nobody knows how many places use it.
- No audit trail. If a secret leaks, there’s no way to know who read it or when.
- Same values across every environment. Dev and production share credentials “to move faster.”
A secrets manager solves this with three ideas: centralized encrypted storage, identity-based access with policies, and a record of every read.
What OpenBao is and why choose it
OpenBao is an open-source secrets manager, maintained under the Linux Foundation, born as a fork of Vault from its last MPL-licensed release. It offers the same conceptual model: secrets engines, authentication methods, HCL policies, tokens with TTLs, and an audit log. Its CLI is called bao.
For a team starting from scratch, the advantage is a genuinely open license and an ecosystem of concepts that’s already well documented. If you already run Vault in production, the transition is manageable: we cover it in our step-by-step migration from Vault to OpenBao in production.
Step 1: spin up OpenBao and create the secrets store
To test locally you can use dev mode — never in production, because it keeps everything in memory and starts already unsealed:
bao server -dev -dev-root-token-id=root
export BAO_ADDR='http://127.0.0.1:8200'
export BAO_TOKEN='root'
Enable the KV version 2 engine, which keeps versions of each secret and lets you recover previous values:
bao secrets enable -path=secret kv-v2
In production, the sensible setup is a cluster with integrated Raft storage, mandatory TLS, and auto-unseal. If you deploy with containers, Docker is enough for a staging environment that mirrors the real design.
A path convention that scales
Decide on the structure before you store anything. A simple, effective convention:
secret/<equipo>/<aplicacion>/<entorno>
For example, secret/pagos/api/prod or secret/pagos/api/staging. That way policies can be expressed precisely and nobody has to guess where each value lives.
Step 2: move the .env contents into the store
Migration is mechanical. Read each variable from your current .env and store it under the matching path:
bao kv put -mount=secret pagos/api/prod \
DATABASE_URL="postgres://app:S3creto@db:5432/pagos" \
STRIPE_KEY="sk_live_xxx" \
JWT_SECRET="un-valor-largo-y-aleatorio"
Check the result with bao kv get -mount=secret pagos/api/prod. If you want a single value, add -field=STRIPE_KEY. Once the store holds everything, the real work begins: getting applications and people to access it with the right identity and least privilege.
Step 3: least-privilege policies
A policy defines which paths an identity can read or write. Watch out for a KV v2 quirk: the API path includes data/.
# api-prod.hcl
path "secret/data/pagos/api/prod" {
capabilities = ["read"]
}
bao policy write api-prod api-prod.hcl
The application only needs read. Developers, meanwhile, get different policies per environment: write access on staging, limited or no read access on prod. That simple split already kills the “everyone has everything” pattern typical of shared .env.
Step 4: authenticate applications with AppRole
Applications shouldn’t use a static token, because that’s just the same problem under a different name. AppRole splits identity into two pieces: a role_id (nearly public, identifies the application) and a secret_id (ephemeral, delivered at deploy time).
bao auth enable approle
bao write auth/approle/role/api-prod \
token_policies="api-prod" \
token_ttl=15m token_max_ttl=1h \
secret_id_ttl=10m secret_id_num_uses=1
With secret_id_num_uses=1 and a short TTL, an intercepted secret_id is nearly useless. The usual flow is for your orchestrator or pipeline to request a secret_id right before deployment and hand it to the application:
bao read auth/approle/role/api-prod/role-id
bao write -f auth/approle/role/api-prod/secret-id
Step 5: get secrets to the app without .env in the repository
This is where many teams stall. There are three approaches, from least to most recommended.
Option A: environment variables at startup
An entrypoint script authenticates, reads the secret, and exports the variables before launching the process. It’s the fastest migration and requires no changes to the application code.
Option B: OpenBao Agent with templates
The agent authenticates for you, renews the token, and renders a file from a template. That file can live on an in-memory volume (tmpfs), so it never touches disk or the repository:
template {
destination = "/run/secrets/api.env"
contents = <<EOT
{{ with secret "secret/data/pagos/api/prod" }}
DATABASE_URL={{ .Data.data.DATABASE_URL }}
STRIPE_KEY={{ .Data.data.STRIPE_KEY }}
{{ end }}
EOT
}
It’s the most practical option for legacy apps that expect a .env on disk: the file exists, but it’s generated at runtime and disappears with the container.
Option C: the application queries OpenBao directly
With a client library, the application reads and renews its own secrets. It’s the cleanest approach and allows rotation without restarts, but it requires code changes. Use it for new services.
Step 6: CI/CD without secrets pasted into config
Storing keys in pipeline variables is better than in Git, but it’s still one more copy to rotate. A better alternative is federated identity: the pipeline presents a signed OIDC token and OpenBao validates it.
With GitHub Actions, for instance, you enable the JWT method with the issuer https://token.actions.githubusercontent.com and create a role that limits access to a specific repository and branch:
bao auth enable jwt
bao write auth/jwt/config \
bound_issuer="https://token.actions.githubusercontent.com" \
oidc_discovery_url="https://token.actions.githubusercontent.com"
bao write auth/jwt/role/deploy-prod \
role_type=jwt user_claim=repository \
bound_claims='{"repository":"mi-org/api","ref":"refs/heads/main"}' \
bound_audiences="https://github.com/mi-org" \
token_policies="api-prod" token_ttl=10m
The result: no long-lived secret stored in the CI system. If someone opens a malicious branch, it doesn’t match the bound_claims and gets nothing.
Step 7: clean up the past and keep it from happening again
Migrating to the store doesn’t erase what’s already in the history. You have to do two things, in this order:
- Rotate every exposed secret. This comes first and is the only truly essential step. Rewriting history doesn’t help if someone already cloned the repository.
- Purge the history with
git filter-repoto remove the files, and force everyone on the team to re-clone.
Then add preventive guardrails: a pre-commit hook with a secrets scanner, the same scan in the pipeline as a merge blocker, and push protection enabled on your hosting platform. Don’t forget a .env.example with variable names and no values, so the project documentation stays useful.
Going further: dynamic secrets and auditing
Once the basics work, the real quality leap is dynamic secrets. With the database engine, OpenBao generates unique credentials per consumer, with automatic expiry:
bao secrets enable database
Each instance of your application gets its own database user that expires in an hour. If it leaks, the damage has an expiration date, and the master password never leaves the manager.
Also enable an audit device to log every read:
bao audit enable file file_path=/var/log/openbao/audit.log
This answers the question no .env ever could: who accessed which secret, and when. Human access deserves the same care; if you want to close that loop, see how Teleport replaces SSH keys with audited access. And for user and service identity, the Keycloak SSO guide for microservices pairs well with OpenBao’s OIDC auth methods.
Common mistakes when adopting a secrets manager
- Leaving the root token in daily use. Generate the root token only for initial configuration and revoke it.
- Losing the unseal keys. Split them with Shamir across several people or use auto-unseal with a KMS.
- TTLs that are too long. A 30-day token is basically a static secret.
- Not backing up the storage. Schedule Raft snapshots and test restoration before you need it.
- Turning OpenBao into a single point of failure. Deploy at least three nodes and monitor their health.
Conclusion: make the secure path the easy one
A team stops committing .env to Git when doing it right is simpler than doing it wrong. With OpenBao, that means clear paths, per-environment policies, AppRole for applications, federated identity in the pipeline, and templates that generate the file at runtime. You don’t have to migrate everything in a week: start with your most critical secret, the production database, measure the outcome, and extend the pattern.
The best time to centralize your secrets was before the first incident. The second best time is today.