Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails convert secrets to credentials

I updated my Rails app from 5.1.6 to 5.2.1 and we were using secrets before. We'd like to switch to credentials since it was supposed to replace secrets. There are a couple of things I'm wondering:

1) Is there a "Railsy" way to do this? I'm aware I can just edit the secrets and copy the contents over to the credentials but I'm not sure if that's the right way to do it since I couldn't find anything that talks about this. Also, I tried copy the encrypted content from secrets.yml.enc and paste it to credentials.yml.enc but that didn't work, ran into some issues with the encryption.

2) Related to the first point, am I supposed to still be able to use secrets in 5.2.1? I thought credentials was supposed to replace secrets so I was surprised I could still use secrets and all my tests are passing.

Thanks for any info or help on this!

like image 908
Ccyan Avatar asked Nov 07 '22 03:11

Ccyan


1 Answers

Backup your config/secrets.yml. Scaffold a temporary vanilla Rails 5.2.1 project via rails new. Copy config/master.key and config/credentials.yml.enc from it to your existing Rails project. Edit these credentials e.g in Ubuntu via:

EDITOR="gedit --wait" bin/rails credentials:edit

Replace the secret_key_base (new flat format prefered, optional for all environments) from the old secrets.yml and / or paste whatever else you need as a secret into it and save it. Delete config/secrets.yml and the temporary Rails project. Access the secrets in your code e.g. via:

Rails.application.credentials.secret_key_base

Ensure that your upgraded Rails 5.1.6 project use the master key in config/environments/*.rb:

Rails.application.configure do
  ...
  config.require_master_key = true
  ...
end

Restart the Rails server. Do'nt forget to .gitignore and .dockerignore the config/master.key!

like image 136
SkatEddy Avatar answered Nov 15 '22 05:11

SkatEddy