Video in progress
What is wrong with hard-coding a database password or an API key in the code?
A secret that gets into git stays in the history forever, and everyone with access to the repo can see it. On top of that, changing the password requires a rebuild and a redeploy.
📖 Answer
Deleting the file after the commit does not help, because the password is still in the old commits. If the repo is ever exposed for a day, or a laptop is lost, you treat the key as leaked and rotate it right away.
The right way is to supply secrets from outside:
spring:
datasource:
password: ${DB_PASSWORD} # the value comes from an env var
And the value itself is stored in the CI secret store, in a Kubernetes Secret, or in a dedicated system such as Vault.
Extra habits:
- add
.envand local config files to.gitignore; - turn on gitleaks or a similar scanner in CI, which catches a key in a commit;
- do not print secrets in logs:
log.info("config: {}", props)sometimes writes out the password too.
Sign up to mark studied questions and save your progress.