SJU
SJU

Reputation: 3272

Missing secret_key_base in rails production

I don't understand why my secret_key_base is not found. When I start my app in production I get this message on web browser : Missing 'secret_key_base' for 'production' environment, set this value in 'config/secrets.yml'

My config/secret.yml looks like this :

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

When I replace <%= ENV["SECRET_KEY_BASE"] %> with the key generated it works, otherwise, it is not recommended...

So I put my key in /etc/profile & ~/.bashrc & ~/.rvm/environment/ruby-2.1.5 as follow :

export SECRET_KEY_BASE=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

When I start a rails console in production mode I can see my key :

rails c production
Loading production environment (Rails 4.1.6)
2.1.5 :001 > ENV["SECRET_KEY_BASE"]
 => "XXXXXXXXXXXXXXXXXXXXX"

I don't get why it is not working knowing all of that. Am I missing something obvious ?


Complete error log :

ERROR RuntimeError: Missing `secret_key_base` for 'production' environment, set this value in `config/secrets.yml`
        /home/xxx/.rvm/gems/ruby-2.1.5/gems/railties-4.1.6/lib/rails/application.rb:462:in `validate_secret_key_config!'
        /home/xxx/.rvm/gems/ruby-2.1.5/gems/railties-4.1.6/lib/rails/application.rb:195:in `env_config'
        /home/xxx/.rvm/gems/ruby-2.1.5/gems/railties-4.1.6/lib/rails/engine.rb:510:in `call'
        /home/xxx/.rvm/gems/ruby-2.1.5/gems/railties-4.1.6/lib/rails/application.rb:144:in `call'
        /home/xxx/.rvm/gems/ruby-2.1.5/gems/rack-1.5.2/lib/rack/lock.rb:17:in `call'
        /home/xxx/.rvm/gems/ruby-2.1.5/gems/rack-1.5.2/lib/rack/content_length.rb:14:in `call'
        /home/xxx/.rvm/gems/ruby-2.1.5/gems/rack-1.5.2/lib/rack/handler/webrick.rb:60:in `service'
        /home/xxx/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
        /home/xxx/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
        /home/xxx/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'

EDIT 1 :

I took a look at ~/.rvm/gems/ruby-2.1.5/gems/railties-4.1.6/lib/rails/application.rb and I did some debug trace using puts. It looks like my ENV variable is nil for Ruby in this file but also in config/secret.yml when the file is parsed.

Here are the log I put on the file during the parsing process :

secrets = ActiveSupport::OrderedOptions.new
yaml = config.paths["config/secrets"].first
if File.exist?(yaml)
  puts "FILE EXIST !"
  puts ENV["SECRET_KEY_BASE"]
  require "erb"
  all_secrets = YAML.load(ERB.new(IO.read(yaml)).result) || {}
  puts all_secrets.inspect
  env_secrets = all_secrets[Rails.env]
  puts env_secrets.inspect
  secrets.merge!(env_secrets.symbolize_keys) if env_secrets
  puts secrets.inspect
end

And here is the result when I start my server using rvmsudo rails s -e "production" -p 80

FILE EXIST !
                <-------------- (nil)
{"development"=>{"secret_key_base"=>"OK"}, "test"=>{"secret_key_base"=>"OK"}, "production"=>{"secret_key_base"=>nil}}
{"secret_key_base"=>nil}
{:secret_key_base=>nil}

It looks like rvmsudo doesn't pass env variable, I will take a look at it...

Upvotes: 3

Views: 2210

Answers (1)

SJU
SJU

Reputation: 3272

My supposition on EDIT #1 was true. The issue was that rvmsudo pass environment variables under somes conditions that can be found on the grep command here :

https://github.com/rvm/rvm/blob/dff3eeac883ce6314c021f3d00730bcd5a628e3e/binscripts/rvmsudo#L46

So I change my environment variable name to : http_SECRET_KEY_BASE to try and it worked !

Upvotes: 2

Related Questions