Prompt
How do I keep SSH keys and certificates out of config files?
Latest observation
A good way to keep SSH keys and certificates out of config files is to treat them as secrets and load them from a separate secret store or runtime input, rather than hardcoding them in your app config.
Common approaches
1. Use environment variables
Store only references in config, not the secret itself.
export SSH_PRIVATE_KEY="$(cat /secure/path/id_rsa)"
export SSH_CERT="$(cat /secure/path/id_rsa-cert.pub)"
Then your app reads them from the environment.
Pros: simple, portable
Cons: can still leak via process listings, crash dumps, or misconfigured logging
2. Mount secrets as files
Keep the key/cert on disk with restrictive permissions and point your app to the file path.
Example config:
ssh:
private_key_file: /run/secrets/ssh/id_rsa
cert_file: /run/secrets/ssh/id_rsa-cert.pub
In containers, this is often done with:
- Docker secrets
- Kubernetes Secrets mounted as volumes
- systemd credentials
- HashiCorp Vault Agent sidecar writing files
Pros: better than embedding contents in config
Cons: still needs file access controls
3. Use a secret manager
Store keys in a tool like:
- HashiCorp Vault
- AWS Secrets Manager
- Azure Key Vault
- Google Secret Manager
- 1Password Secrets Automation / Infisical / Doppler
Your app fetches the secret at startup or via a sidecar/agent.
Pros: strongest operational pattern
Cons: more setup
4. Use SSH agent or hardware-backed keys
Instead of storing the private key in a config file, load it into:
ssh-agent- a smartcard / YubiKey / TPM
- cloud KMS/HSM-backed signing flow
Then config only includes the socket/path to the agent if needed.
Pros: private key never sits in app config
Cons: more complex; not always supported by libraries
Good config pattern
Keep config like this:
ssh:
private_key_source: file
private_key_path: /run/secrets/ssh/id_ed25519
certificate_path: /run/secrets/ssh/id_ed25519-cert.pub
or:
ssh:
private_key_source: env
private_key_env: SSH_PRIVATE_KEY
This way the config stores only where to get the secret, not the secret itself.
Security tips
- Never commit keys to git
- Use least-privilege file permissions (
chmod 600) - Avoid logging secret values
- Rotate keys/certs regularly
- Prefer short-lived SSH certificates over long-lived keys when possible
- Separate config from secrets in CI/CD and deployment manifests
If you want, I can show examples for a specific stack like OpenSSH, Ansible, Docker/Kubernetes, Go/Python/Node, or Vault.