Secrets such as passwords, keys, and tokens are never written into code or the repository; they are read at runtime from environment variables or a dedicated secrets service, and rotated regularly.
Why use it
Many people see the code: colleagues, contractors, open source, backups, CI logs. Anything that entered version control remains in history even after deletion.
Environments need different config: development, test, and production should use different passwords; hardcoding in code cannot separate them.
Leaks need fast rotation: keeping secrets out of code lets you rotate without redeploying.
What counts as a secret
Database connection strings, third-party API keys, JWT signing keys (see JWT and sessions), OAuth client_secret, TLS private keys, and cloud account access keys.
How to do it
Zero secrets in code: read from configuration; never hardcode.
Level up with maturity:
Start: environment variables.
Next: a cloud or self-hosted secrets management service (Key Vault, Secrets Manager, Vault) with access control, audit, and automatic rotation.
In containers, inject via the platform Secret mechanism.
Local development files (.env) must be in .gitignore; the repository only keeps example files without real values (such as .env.example).
Least privilege: each service gets only the secrets it needs; different environments use different secrets.
Rotate regularly, and rotate immediately when someone leaves or a leak is suspected.
Never write secrets into logs or error responses; see Logging.
Add scanning to the repository (for example gitleaks or GitHub secret scanning) so accidental commits are caught before merge.
🧭 What to do after a secret is committed by mistake
Discover a secret was committed or leaked
↓
Rotate immediately: revoke the old, issue a new one
↓
Check whether the old secret was abused
↓
Clean leftovers from the repository and logs
↓
Add scanning so it does not happen again
Deleting the line from code is not enough; history still has it, so rotate first.
Component comparison
Environment variables
Built into the frameworkOpsLow
Put passwords and keys in the server's environment variables and read them at program startup, instead of writing them into code.
Best forSmall projects with one or two servers.
Avoid whenMany collaborators, many servers, or a need to audit who has touched the keys.
Pros
Every language and platform supports it, with no extra tool to add
Secrets live in the environment and do not enter the code repository
Set them on the system and read them from code
Cons
There is no access log, so you cannot see who changed or viewed them
They can leak in logs, in error output, or in the process list
Each machine is set by hand, and a change still needs a restart
.env file + deployment platform secrets
Managed serviceOpsLow
Use a .env file for local development (not committed to the repository), and in production enter keys into the "secrets" settings provided by the deployment platform (such as CI/CD or a container platform).
Best forSmall and medium projects, and teams deploying with Docker or cloud platforms.
Avoid whenStrict compliance requirements that need full auditing and automatic rotation.
Pros
Smooth development and production workflow, with simple configuration
Platforms usually hide secret contents, so plaintext is not visible in the UI
Most deployment platforms include it, so no extra service to build
Cons
If .env is committed to the repository by mistake, the keys are leaked and must be rotated immediately
Each platform's secrets only work within that platform, so switching platforms means reconfiguring
Generally no automatic key rotation
HashiCorp Vault
Self-hosted open sourceOpen sourceOpsHigh
A "vault" service dedicated to keeping secrets: programs request keys from it at runtime, and every access is logged.
Best forTeams with dedicated ops, many secrets, and high compliance requirements.
Avoid whenSmall teams with no ops staff.
Pros
Access records are complete, and authorization can be fairly fine-grained
It can rotate secrets automatically and issue short-lived database passwords
You can deploy it yourself, without tying it to one cloud
Cons
You deploy it, back it up, and also handle unsealing
There are many concepts, and connecting it means changing the program or adding a component
If it goes down, every application that depends on it fails to start
A managed key vault provided by the cloud platform: keys are stored in the cloud, and apps read them by authorization with a cloud identity, with no ops on your side.
Best forTeams whose applications are already deployed on a public cloud.
Avoid whenDeploying in your own data center or planning to migrate between multiple clouds.
Pros
No service to build yourself, and availability is guaranteed by the cloud vendor
Has access logs, and can be managed together with the cloud permission system
Some services support automatic key rotation
Cons
Tied to the chosen cloud vendor, so moving to another means changing code
Billed by number of keys or calls, so frequent calls call for caching
Local development needs a separate way to emulate it
Checklist
Code and the repository contain no real secrets
Local files such as .env are in .gitignore
Development, test, and production use different secrets
Secrets live in environment variables or a secrets service, with access control
There is a rotation plan, and leaks can be replaced quickly