Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: How to fix "Missing secret_key_base for 'production' environment"

I simply can't get past the message:

Missing `secret_key_base` for 'production' environment, set this string with `rails credentials:edit` (ArgumentError) 

I have Rails 5.2.0, and ran

EDITOR=vim rails credentials:edit 

and inside:

production:    secret_key_base: xxxxxxxxxxxxxxxxxxxxxxx 

Save and, in the terminal:

RAILS_ENV=production rails c 

Am I missing something? I've restarted the server and got the same issue, but have no issue in development mode.

like image 283
Sylar Avatar asked Jul 22 '18 16:07

Sylar


People also ask

What is Secret_key_base?

secret_key_base is used to encrypt and sign session.

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.

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'


2 Answers

Keep default the secrets.yml file

# config/secrets.yml production:   secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>   aws_secret: abcde   some_password: abcdex  development:   secret_key_base: static_secret_key   aws_secret: abcde  test:   secret_key_base: static_test_secret_key   #not_indented: key for all env in once secret_key_base: global_key_for_all_env 
RAILS_ENV=production SECRET_KEY_BASE=production_test_key rails c 

If using Rails 5.2.0, add to production env below, check this LINK

config.require_master_key = true    #config/environments/production.rb 
like image 179
7urkm3n Avatar answered Sep 22 '22 13:09

7urkm3n


Rails 5.2.0 requires an extra stage for the production environment:

config.require_master_key = true    # in config/environments/production.rb 

Without it, Rails still falls back to the legacy secret.yml mechanism (for now).

Engine Yard's Christopher Rigor has written a concise post on it. The relevant piece:

Reading the Credentials

If you want to use the credentials in the production environment, add the following to config/environments/production.rb

config.require_master_key = true 

A good read to also see up and down sides.

Note: As @TomDogg found out, Rails 5.2.1 seems again different, so this answer may only apply to 5.2.0.

like image 35
Eric Platon Avatar answered Sep 18 '22 13:09

Eric Platon