r/git • u/Kind_Willingness_396 • Oct 31 '26
Devs, what happens in your team when someone pushes an API key to main?how do you keep API keys out of git history?
0
Upvotes
r/git • u/Kind_Willingness_396 • Oct 31 '26
80
u/tom-md Oct 31 '26
27
u/marvinfuture Oct 31 '26
In my experience as a DevOps engineer, this type of stuff happens because organizations don't understand the best practices here.
What I mean is a couple things failed here:
Personally I'd use this type of scenario to do two things
First remediate the issue. Treat the key as compromised and rotate it immediately. Undo the git commit as well if possible in your SCM solution.
Second, improve the process. Implement tooling that makes this harder to do and identify why this was checked in and not caught
26
u/OkLettuce338 Oct 31 '26
Rotate the key
14
u/pippin_go_round Oct 31 '26
No dev has access to any API keys. At least none that can cause any real damage. Those are put into the secret manager in prod only by a very select few people.
9
u/carsncode Oct 31 '26
You have to burn the key, period. You can't un-leak a secret. You treat it as a security incident, run a retro, correct the process that allowed it to happen in the first place, starting with people having access to and using real secrets.
6
u/Philluminati Oct 31 '26
Change the password/token so the key no longer works. Don't break your git history for a half chance of undoing this mistake. Changing the key is the right way forwards.
6
u/deZbrownT Oct 31 '26
How does it happen that devs are handling api keys in a professional dev environment?
4
u/mxz117 Oct 31 '26
Happened once, guy pushed “env 3” in a PR
Just rotated the key
4
u/unicornsandrainbowst Oct 31 '26
1 - Api keys (the ones that matter) are not accessible to devs. They are read from a vault as part of the pipeline. 2 - if they are exposed (by a compromised action for example), we just rotate the key in the vault, takes 1 minute. The next pipeline picks the new key.
As simple as that. Anyone running production environments with .env files on each dev machine trusting .gitignore and the dev itself, needs to take a closer look at their security practices.
4
u/Adorable-Strangerx Oct 31 '26
You can but there is no point. Just rotate key.
2
u/Gurnug Oct 31 '26
You will get so much paperwork to fill after that security incident that you will remember
2
u/the_helpdesk Oct 31 '26
You pay for tooling that prevents committing secrets. GitHub has some, datadog has some, and I'm sure there's 40 other options. Or you can write your own pre-commit hooks.
3
u/atarivcs Oct 31 '26
You keep API keys out of git by not committing files that contain an API key.
1
u/mpersico 23d ago
You don't give them access to the key in the first place. You put the key in a vault , and the program has to be registered with the vault. The program goes and grabs the key when it needs it. Doesn't write it anywhere. Doesn't store it.
1
u/chriswaco Oct 31 '26
We’re a very small team that puts local keys into .key files and have git hooks to prevent checking them into any repo.
-1
u/Ok_Entrepreneur_8509 Oct 31 '26
If you can't retire that key and get a new one, you can force a history rewrite to remove the commit. This is not going to be 100% safe, because you don't know if the key got out, but if you are mainly trying to keep your repo clean, it works.
Assuming you are on main and the bad commit is the most recent one:
git reset --hard HEAD~1
git push origin main --force-with-lease
Force with lease just prevents you from accidentally clobbering anything that got committed after.
If you need to pull the bad commit out from deeper in the history, you have to do a more complicated rebase process.