Foolproof Method to Not Push Unencrypted Secrets into Git
Quick fix to prevent committing secrets into your Git repository
The problem
It’s a frequent problem that every developer faced: committing secrets into your Git repositories.
Once a secret has been pushed to a remote repository, you are doomed to execute a gruesome procedure to clear it. The best way to deal with the problem is to prevent it entirely.
Lots of solutions exist to address the problem: AWS git-secrets, git-secret or even a dedicated SonarQube plugin. Apart from the manifest lack of imagination in finding a meaningful name for these solutions 😘 you sometimes only want a simple and quick fix.
No Brainer 5 Minutes Solution
Most of these full-blown solution work by installing themselves as Git client hooks. But it’s really simple to write a tailored hook for dealing with exactly what you need.
In my case, I only want to prevent unencrypted secrets from being pushed to the repository. To elaborate, I encode some secrets using sops and I want to check before each commit that all my secrets are properly encrypted.
These secrets all share a naming convention and live in specific folders. Writing the pre-commit
hook is straightforward.