Skip to content

Instantly share code, notes, and snippets.

@letiesperon
Last active January 18, 2024 21:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save letiesperon/04997e4ac27f55e6a53430d0120a9fd7 to your computer and use it in GitHub Desktop.
Save letiesperon/04997e4ac27f55e6a53430d0120a9fd7 to your computer and use it in GitHub Desktop.
What I need to know about Rails 6 credentials and master key
The credentials are stored in the "credentials.yml.enc" file, encrypted.
To decrypt the credentials file, you need a master key that is set on either:
* config/master.key file (for local development)
* ENV["RAILS_MASTER_KEY"] (for deployed environments)
What should you commit?
* The file config/master.key should be ignored in gitignore (you should not commit the master key)
* The file credentials.yml.enc should be commited along with the codebase (but don't worry, only those who have the master key can decrypt it)
Once you have set the master key you can:
* View the content of the credentials file: rails credentials:show
* Edit the content of the credentials file: EDITOR=vim rails credentials:edit (Make sure you commit the changes afterwards)
(If your master key is not correct, meaning the file could not be decrypted with the given key, a long scary log will appear raised by an "ActiveSupport::MessageEncryptor::InvalidMessage" exception)
To regenerate the master.key (because you lost it or it got compromised) you need to:
* Copy content of original credentials "rails credentials:show" somewhere temporarily.
* Remove the config/master.key and config/credentials.yml.enc files
* Run "EDITOR=vim rails credentials:edit" in the terminal: (This command will create a new master.key and credentials.yml.enc file if they do not exist.)
* Paste the original credentials you copied (step 1) in the new credentials file (and save + quit vim)
* Commit the file config/credentials.yml.enc
* Setup the new master key as an envirnoment variable in your other non-localhost environments and deploy your code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment