TL;DR
Use a password manager or a dedicated secrets management tool like HashiCorp Vault. Avoid storing secrets directly in code, configuration files, or environment variables without encryption. For simple cases, encrypt the secret with GPG before storing it.
1. Understand the Risks
Storing secrets (passwords, API keys, database credentials) carelessly is a major security risk. If compromised, attackers can gain access to your systems and data. Common mistakes include:
- Hardcoding in code: Easily discovered during code reviews or if the code is exposed.
- Plaintext configuration files: Vulnerable to anyone with file system access.
- Unencrypted environment variables: Can be accessed by malicious processes on the server.
2. Password Managers (Simple Cases)
For personal projects or small teams, a reputable password manager is often sufficient.
- Benefits: Easy to use, strong encryption, cross-platform support.
- Examples: 1Password, LastPass, Bitwarden.
- Usage: Store your secrets within the password manager and access them as needed (copy/paste).
3. Dedicated Secrets Management Tools (Teams & Production)
For larger teams and production environments, a dedicated secrets management tool provides more features and control.
- HashiCorp Vault: A popular open-source option offering encryption, access control, auditing, and dynamic secret generation.
- Installation (example using brew on macOS):
brew install vault - Basic Workflow: Store secrets in Vault, define policies to control access, and retrieve secrets programmatically using an API token or other authentication method.
- AWS Secrets Manager/Azure Key Vault/Google Cloud Secret Manager: Cloud provider-specific solutions integrated with their respective ecosystems.
4. Encryption (Intermediate Option)
If you need to store secrets in files, encrypt them before doing so.
- GPG (GNU Privacy Guard): A widely used encryption tool.
- Generate a key pair:
gpg --gen-key - Encrypt the secret:
gpg -e -r "Your Name" your_secret_file.txt(This will create
your_secret_file.txt.gpg) - Decrypt the secret:
gpg -d your_secret_file.txt.gpg > your_secret_file.txt - Important: Securely store the private key used for encryption! Losing it means losing access to your secrets. Consider using a hardware security module (HSM) for added protection.
5. Avoid Common Pitfalls
- Never commit encrypted files to version control without proper safeguards: Ensure the repository is properly secured and access is restricted.
- Rotate secrets regularly: Change passwords and API keys periodically to limit the impact of a potential compromise.
- Use least privilege principle: Grant only the necessary permissions to access secrets.
- Monitor access logs: Detect any unauthorized attempts to retrieve secrets.

