Skip to content

Instantly share code, notes, and snippets.

@binarygit
Last active November 8, 2022 07:10
Show Gist options
  • Save binarygit/79be65ce9fdfa63ef1bccd1ad54d914a to your computer and use it in GitHub Desktop.
Save binarygit/79be65ce9fdfa63ef1bccd1ad54d914a to your computer and use it in GitHub Desktop.
All about Rails secrets/credentials

setup your editor first

export EDITOR=<your editor of choice, eg: vim>

To edit your secrets use:

rails credentials:edit

The above command is editing your config/credentials.yml.enc file.

Let's say after the above command, you wrote down your secrets this way:

javascript_books_i_own:
  book_one: Eloquent Javascript
  book_two: How to become cooler by learning react in 7 days
  book_three: How to hide your javascript obsession from your Rails friends?

important_api_key: xyzlmrailsisloveyyyxoxo

As you can see that they are written down in a key: value style with nesting allowed. You can access the values like this:

 Rails.application.credentials.dig(:javascript_books_i_own, :book_one)
=> 'Eloquent Javascript'

 Rails.application.credentials.important_api_key
=> 'xyzlmrailsisloveyyyxoxo'

To let your heroku instance access your secrets, you'll need to set your RAILS_MASTER_KEY variable to the contents of your config/master.key file. The command to do this is:

heroku config:set RAILS_MASTER_KEY=<contents of config/master.key>

Useful links: https://guides.rubyonrails.org/security.html#custom-credentials https://www.codewithjason.com/understanding-rails-secrets-credentials/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment