encrypt and check your secrets into git

When it comes to managing secret tokens whatever you do someone will come out and say you’re doing it wrong and propose an alternate solution that on the surface seems to be better but upon closer scrutiny is susceptible to the same attack vectors. The secure solutions is always some kind of custom solution for storing secrets that provides an audit trail. The audit trail means whenever a secret is accessed you have a log entry of it somewhere. Which is nice because when stuff leaks you can in theory trace it back to some specific person. Unless that person was hacked and they were used as a patsy. So today let’s compare storing encrypted secrets in git or some custom solution that is not git.

We have to make some assumptions here to see if git is any better or worse. We assume everything is encrypted at rest with some secret key that is not available to the attacker. The essential difference between git and the non-git solution is the audit trail and access to historical ciphertexts for a given plaintext. In git the attacker has access to the history of the ciphertexts starting at some point in time. In the non-git case they only ever get to see a snapshot of the ciphertext. Although I’m sure some custom solutions do offer versioning which makes them equivalent to git.

So what happens when the plaintext is leaked in the git case? Well we have to cut off access and rotate the plaintext secret. This invalidates the plaintext that was leaked and we are good to go. It doesn’t matter if the attacker had the entire history of the ciphertexts for the plaintext because only the latest one has any meaning.

What happens when the plaintext leaks in the non-git case? We cut off access and rotate the plaintext secret. The attacker now holds a secret token that is meaningless. Same as in the git case.

Now let’s assume the attacker gets our secret key and runs away with the crown jewels. What happens in the git and non-git case now?

In the git case the attacker has the history of the ciphertexts and the secret key so they decrypt the ciphertexts and keep the latest version of the plaintext. So the only thing we can do is rotate the private key and the plaintext and check things into git again. The fact that the attacker still has the historical data no longer matters because all the old keys are meaningless.

In the non-git case the attacker decrypts the snapshot of the ciphertext they currently have. In this case again we have to rotate everything. This invalidates whatever the attacker has because the old keys are gone.

So what did we do differently in the git vs non-git case? Nothing. If we leak the plaintext then we rotate it. If we leak the secret key then we have to rotate everything. The response is the same whether you store your secret tokens in git or somewhere else.

Now an argument could be made about access control but again when we do the analysis the solutions in git and non-git case are equivalent. gpg allows encrypting the same secret with multiple keys so that multiple recipients can decrypt it. So the question is how do you go about revoking access?

In the git case you rotate and re-encrypt the plaintexts with one less recipient. If you don’t rotate then it is meaningless to re-encrypt things because assuming the attacker had access at some point they still have the same key.

In the non-git case you revoke the attacker’s access. Again if you don’t rotate the keys the attacker had access to then revoking their access is meaningless. They still have the key so you have to rotate the key after revoking access.

So what did we do differently in the git vs non-git case for access management? Nothing. We had to revoke access and rotate the secret token.

So save yourself the headache of the custom solution and just set up gpg and check in your encrypted secret tokens into git. There are several solutions out there for doing that. One I saw recently was blackbox. Another approach is to use some Rake utilities.