How to see the changes in Rails encrypted credentials
Rails keeps secrets in encrypted files like config/credentials.yml.enc or config/credentials/production.yml.enc.
When one of them changes, git diff and git log -p show one line of random characters replaced by another. In a
pull request you can’t tell if someone added an API key or deleted the database password.
There are two ways to see the real changes.
With Rails and git
Since Rails 6.1, Rails can tell git how to show its encrypted files. Run this once in the project:
bin/rails credentials:diff --enroll
It adds two lines to .gitattributes:
config/credentials/*.yml.enc diff=rails_credentials
config/credentials.yml.enc diff=rails_credentials
They tell git to show these files with a diff driver called rails_credentials. The driver is set up the next time
you run bin/rails credentials:edit, which adds this to .git/config:
[diff "rails_credentials"]
textconv = bin/rails credentials:diff
You can also add it yourself, without opening the credentials:
git config diff.rails_credentials.textconv "bin/rails credentials:diff"
From then on, git diff, git show and git log -p decrypt both sides with your key and show the change in plain
text:
aws:
access_key_id: AKIA...
- secret_access_key: old-secret
+ secret_access_key: new-secret
+stripe:
+ api_key: sk_live_...
Some things to know:
- Commit
.gitattributesso the whole team gets the entries..git/configstays local, so each clone runscredentials:editonce or sets the driver by hand. - The diff needs the key:
config/master.key,config/credentials/<environment>.keyorRAILS_MASTER_KEY. Without it, there’s nothing to decrypt with. - Each diff starts Rails, so
git log -pover many commits gets slow. - The driver only works where git runs
bin/rails. Pull requests on GitHub or GitLab still show the encrypted line. - To undo it, run
bin/rails credentials:diff --disenroll. It removes the lines from.gitattributes, and deletes the file if nothing else is left in it.
In gitto
gitto shows encrypted Rails files decrypted, with no setup. It works for config/credentials.yml.enc,
config/credentials/<environment>.yml.enc and other .enc files that Rails encrypts the same way.
gitto looks for the key like Rails does: next to the file first (config/credentials/production.key for
production.yml.enc), then master.key in the same folder or the one above. The key has to be in your working tree.
gitto doesn’t read RAILS_MASTER_KEY.
You see the decrypted text in your local changes, in the commits of the history and in pull request reviews. A line above the diff says which key file was used. For a repository on another machine, gitto reads the key from the working tree on that machine.
The file is decrypted in memory. The key and the plain text are never written to disk, sent anywhere or passed to git.
Because git only stores the encrypted file, its lines can’t be staged one by one. gitto stages the whole file, and in a pull request review you comment on the file with Comment on file instead of on single lines.
Without the key, gitto shows the encrypted file like git does.
Questions
Why does git diff show credentials.yml.enc as one long line?
The file is encrypted with AES-GCM. Any change, even one character, changes the whole encrypted text, so git can only show that one line was replaced by another.
Do I have to commit anything to use the diff driver?
Commit .gitattributes, so everyone on the team gets the entries. The driver itself is in .git/config, which is never committed, and each clone sets it up again.
Does gitto send my master key anywhere?
No. gitto reads the key from your working tree, decrypts the file in memory and shows it. The key and the plain text are never written to disk, sent anywhere or passed to git.
Why can't I stage single lines of the decrypted file?
git only stores the encrypted file. A patch of a few plain text lines can’t be turned into a patch of the encrypted one, so the whole file is staged.