If your database password is sitting in your source code, you already have a problem. It's one of the most common security failures, and it's usually not carelessness. It happens because a password feels like one more setting, and settings go in config files.
The fix starts with a change in how you classify things. Passwords, API keys, connection strings, and certificates are not application configuration. They are secrets, and they need to be managed as such.
the rule of thumb
If a secret can accidentally end up in any of these places, it is not securely managed:
- -source control, including old commits and deleted files, which remain in history
- -application logs, error messages, and stack traces
- -Docker images, which anyone with the image can inspect layer by layer
- -deployment artifacts, build outputs, and CI logs
- -environment-specific config files checked into the repository
If your secret can show up in any of those, move it. Store a reference to the secret in configuration, and store the value in a secrets manager.
configuration holds the name, the vault holds the value
The application doesn't need to know the password. It needs to know where to get it.
flowchart LR
A[Application code] --> B[Config: secret name or URI]
B --> C[Secrets manager]
C --> D[Secret value returned at runtime]
D --> AThe config file might say db-password or point to a vault URI. The value itself lives in Azure Key Vault, AWS Secrets Manager, Google Secret Manager, or HashiCorp Vault. The application asks for it at runtime, and the value never touches the repository or the image.
one build, many environments
A common pattern is to deploy the same artifact to development, staging, and production. The only difference between them should be configuration, and that configuration should contain references, not values.
flowchart TB
Build[Single build artifact] --> Dev[Development]
Build --> Stg[Staging]
Build --> Prod[Production]
Dev --> VDev[(Dev vault)]
Stg --> VStg[(Staging vault)]
Prod --> VProd[(Production vault)]Each environment points to its own vault or its own secret names. The code is identical. The production database password is never visible to a developer's laptop, and nothing in the build needs to change when credentials do.
rotation without redeploying
When a password changes, you shouldn't have to rebuild or redeploy your application. You rotate the secret in the vault, and the application picks up the new value.
This only works if the application reads secrets from the vault rather than baking them in at startup forever. Plan for it:
- -Fetch secrets through a client library that supports refresh, or re-read them on a short interval.
- -During a rotation, allow both the old and new credentials to work briefly, so in-flight connections don't fail the moment the secret changes.
- -Test rotation in staging before you need it in production during an incident.
Rotation is much easier to do routinely than to do under pressure. A secret that has never been rotated is a secret you don't know how to rotate.
identity before secrets
There's an obvious question here: if the application needs a password to get secrets, where does that password go? The answer is to avoid needing one at all.
Cloud platforms offer managed identity (Azure) and IAM roles or workload identity (AWS and Google Cloud). The running service is given an identity, the cloud platform vouches for it, and that identity is granted permission to read specific secrets. No password is stored in the application to get the passwords.
The chain of trust becomes: the platform proves who the workload is, the policy decides what it may read, and the vault returns only that value.
control and audit
Once secrets are centralized, you gain two things that scattered config files never gave you:
- -Access control. You can decide which services and people can read which secrets, and revoke that access in one place.
- -Audit. Vaults record who or what accessed a secret and when. When something looks wrong, you can answer "which service read the production database password last Tuesday?" in minutes.
Give each service access only to the secrets it needs. A reporting job has no reason to read the payment provider's API key.
caching carefully
Calling the vault on every request is slow and expensive. Caching is reasonable, but it should be deliberate:
- -Keep secrets in memory, not on disk or in logs.
- -Use short cache lifetimes so rotations take effect promptly.
- -Never print secret values in debug output, even temporarily. A logged secret is a leaked secret.
common mistakes
- -Secrets in git history. Deleting a file doesn't remove the password from older commits. If it was committed, treat it as compromised and rotate it.
- -
.envfiles committed "just for local development." Local development is a common source of leaks, and those files often get copied into images. - -Secrets passed as build arguments. Build arguments can persist in image metadata. Inject secrets at runtime.
- -One shared secret for every environment. A leaked staging password should never open production.
- -Keys with no owner or rotation plan. If nobody knows what a secret is for or how to replace it, it will stay unchanged for years.
the takeaway
Secret management is an architectural decision, not a configuration detail. Treat passwords, API keys, connection strings, and certificates as a separate class of data. Keep them in a dedicated secrets manager, reference them by name, grant access through identity rather than more passwords, rotate them without redeploying, and audit every read.
If you're designing a system today, start by listing every secret it uses and asking where each one lives. If the answer includes a git repository, a container image, or a deployment artifact, that's where to begin.