Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create fresh Rails 5 credentials on clone

Problem I am creating a rails 5.2 template. I've created a new project which is a fork of the template. I don't want to use the same config/master.key since this would be shared across X other projects. Is there a way to generate a new key & config/credentials.yml.enc pair? That way I could include a config/credentials.yml.enc.sample and they run rails credentials:new or something then copy the contents over?

Can't find anything in the documentation or google/so searches about this and my alternative is to use the same key across all my public projects :,(

like image 331
Myk Klemme Avatar asked Jan 21 '18 22:01

Myk Klemme


People also ask

How do you save credentials in rails?

Adding credentials# secret_access_key: 345# Used as the base secret for all MessageVerifiers in Rails, including the one protecting cookies. The credentials are stored in the YAML format and can be grouped by nesting keys.. Let's modify this by removing the comments and adding a key and a value.

How do I change my rails credentials?

Your text editor will open an unencrypted version of your credentials. If you don't have EDITOR set, you can run EDITOR=vi bin/rails credentials:edit or use your favorite text editor. After saving the file, the encrypted version will be saved to config/credentials. yml.

How do I create a master key in rails?

We have to do it manually. Copy content of original credentials rails credentials:show somewhere temporarily. Run EDITOR=vim rails credentials:edit in the terminal: This command will create a new master. key and credentials.

How do you change credentials in rails 6?

To decrypt and view or edit your credentials. yml , you can run rails credentials:edit or EDITOR=vim rails credentials:edit .


3 Answers

https://github.com/rails/rails/blob/master/railties/lib/rails/commands/credentials/USAGE

For applications created prior to Rails 5.2, we'll automatically generate a new credentials file in config/credentials.yml.enc the first time you run bin/rails credentials:edit. If you didn't have a master key saved in config/master.key, that'll be created too.

So I can create a plain text version of the encrypted file to show which keys are required:

foo_api_key: 123

They run bin/rails credentials:edit which generates the key and encrypted file then they copy the keys over to add them to the encrypted file.

like image 147
Myk Klemme Avatar answered Oct 18 '22 18:10

Myk Klemme


as described here: https://blog.eq8.eu/til/rails-52-credentials-tricks.html

Regenerate key

Currently there is no “edit password” feature, you need copy original content of the credentials, remove the enc files and regenerate fresh credentials file (source)

  • step 1 copy content of original credentials rails credentials:show
  • step 2 move your config/credentials.yml.enc and config/master.key away (mv config/credentials.yml.enc ./tmp/ && mv config/master.key ./tmp/)
  • step 3 run EDITOR=vim rails credentials:edit
  • step 4 paste copied values from original credentials
  • step 5 save and commit config/credentials.yml.enc

note! EDITOR=vim rails credentials:edit may not work if you require credential value in some file (e.g. in config/database.yml)

like image 43
equivalent8 Avatar answered Oct 18 '22 20:10

equivalent8


Using @Myk Klemme's answer at https://stackoverflow.com/a/48373368/936494 I was able to successfully re-generate credential files config/credentials.yml.enc, config/master.key.

For that I first removed the existing config/credentials.yml.enc file I got from cloned template-repo and then ran following command

rails_new_app$ EDITOR="mate --wait" bin/rails credentials:edit

which generated following output:

Adding config/master.key to store the encryption key: <encryption_key>

Save this in a password manager your team can access.

If you lose the key, no one, including you, can access anything encrypted with it.

      create  config/master.key


File encrypted and saved.
like image 1
Jignesh Gohel Avatar answered Oct 18 '22 20:10

Jignesh Gohel