Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Production - How to set Secret Key Base?

Tags:

So I am trying to get my rails app to deploy in production mode, but I get the error: Missing secret_token and secret_key_base for 'production' environment, set these values in config/secrets.yml

My secrets.yml file is as expected:

development:   secret_key_base: xxxxxxx  test:   secret_key_base: xxxxxxx  production:   secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> 

But even after google and research, I have no idea what to do with the production secret key base. Most of the info out there assumes I have certain background knowledge, but the reality is that I'm a noob.

Can anyone explain to me how to set my secret key and get this to work in production mode?

like image 225
nvrpicurnose Avatar asked Mar 21 '15 19:03

nvrpicurnose


People also ask

What is secret key base Rails?

Have you ever wondered what the secret_key_base value is and how it's used in a Rails application? This configuration value was introduced in Rails 4 and is usually defined on a per-environment basis. It's purpose is simple: to be the secret input for the application's key_generator method.

What is Rails master key?

The key used to encrypt credentials, called the Rails master key, is automatically generated when you create a new Rails app or when you run bin/rails credentials:edit . If you like to create a new key, you can run bin/rails runner 'puts ActiveSupport::EncryptedFile.generate_key'

What is Secrets Yml?

Secrets YAML Reference. This document is the reference for the YAML grammar used for creating Semaphore secrets. A secret is a bucket that stores environment variables and files.

Where is Rails application secrets?

Rails stores secrets in config/credentials. yml. enc, which is encrypted and cannot be edited directly.


2 Answers

You can generate the key by using following commands

$ irb >> require 'securerandom' => true >> SecureRandom.hex(64) => "3fe397575565365108556c3e5549f139e8078a8ec8fd2675a83de96289b30550a266ac04488d7086322efbe573738e7b3ae005b2e3d9afd718aa337fa5e329cf" >> exit 
like image 53
Tarun Rathi Avatar answered Oct 09 '22 23:10

Tarun Rathi


The errors you are getting just indicate that the environment variable for secret_key_base are not properly set on the server.

You can use various scripts like capistrano that automate the process of setting these before the application is run.

As for a quick fix try this:

export SECRET_KEY_BASE=YOUR SECRET BASE 

Validate the environment variables and check if these have been set.

Command:

env | grep -E "SECRET_TOKEN|SECRET_KEY_BASE"

If your values pop up then these are set on the production server.

Also it is best practice to use ENV.fetch(SECRET_KEY) as this will raise an exception before the app even tries to start.

like image 20
Jens Avatar answered Oct 09 '22 23:10

Jens