Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing production secret_key_base in rails

I have recently deployed an app and got internal server error because of missing production secret_key_base. After hours of testing, I managed to solve this problem with two methods:

Method 1:

I generated a new secret_key with rake secret and replaced it with <%= ENV["SECRET_KEY_BASE"] %> in secrets.yml. Deployed the app again and this time it worked. But I think that this method is wrong.

Method 2:

I generated a new secret_key with rake secret and added it to environments/production.rb like config.secret_key_base = 'd1f4810e662acf46a33960e3aa5bd0************************, without changing secrets.yml (default is production: <%= ENV["SECRET_KEY_BASE"] %>). Deployed the app again and it works fine.

My questions:

  1. Which method is the best?
  2. If the 2nd method is correct, why rails does not generate a secret_key_base in production.rb by default?
  3. Is there any other method to do that?
like image 739
user3631047 Avatar asked May 18 '14 19:05

user3631047


People also ask

What is Rails Secret_key_base?

The application's key_generator , and thus secret_key_base , are used by three core features within the Rails framework: Deriving keys for encrypted cookies which are accessible via cookies. encrypted . Deriving the key for HMAC signed cookies which are accessible via cookies.

What is Rails master key?

The master key When you create a new rails app a file called credentials. yml. enc is added to the config directory. This file will be decrypted in a production environment using a key stored either on a RAILS_MASTER_KEY environment variable or a master.

How do you change credentials in rails?

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.

What is Secrets yml?

2.2 config/secrets. yml file in the config folder. By default, this file contains the application's secret_key_base , but it could also be used to store other secrets such as access keys for external APIs.


2 Answers

I have finally found the corrent method. None of the methods mentioned in question are the correct one.

Correct method:

We ourselves should generate a secret key (by rake secret) then create an environment variables for SECRET_KEY_BASE by running following command from command prompt:

rhc set-env SECRET_KEY_BASE=3dc8b0885b3043c0e38aa2e1dc64******************** -a myapp 

after running this command, connect to your server via SSH and run env so you should see your SECRET_KEY_BASE in the list.

Now restart you app rhc app-stop myapp and rhc app-start myapp, then you are good to go.

like image 197
user3631047 Avatar answered Sep 19 '22 20:09

user3631047


If you're on a normal Ubuntu machine just put export SECRET_KEY_BASE=" <<< output from rake secret here >>> " in your ~/.bashrc.

Run source ~/.bashrc and restart the app.

like image 41
FaZe Unempl0yedd Avatar answered Sep 20 '22 20:09

FaZe Unempl0yedd