找到你要的答案

Q:Missing secret_key_base in rails production

Q:在钢轨生产secret_key_base失踪

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...

我不明白为什么我找不到secret_key_base。当我开始我的应用程序在生产我得到这个消息的Web浏览器:失踪的secret_key_base '为'生产'环境,设置这个值在“配置/秘密吗”。

我的配置/ secret.yml看起来像这样:

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

当我代替<;% = env [“secret_key_base”] % & gt;与密钥生成的工作,否则,不推荐…

所以我把我的钥匙在/etc/profile &;~ / .bashrc &;~ /。RVM /环境/ ruby-2.1.5如下:

export SECRET_KEY_BASE=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

当我在生产模式下启动Rails控制台时,我可以看到我的键:

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

我不明白为什么不知道这一切。我错过了一些明显的东西吗?


完整错误日志:

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'

编辑1:

我看了~ /一看。RVM /宝石/ ruby-2.1.5/gems/railties-4.1.6/lib/rails/application.rb和我做了一些调试跟踪使用提出。它看起来像是我的环境变量文件中的Ruby零而且在配置/ secret.yml当文件解析。

下面是我在分析过程中放在文件上的日志:

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

这里的结果是当我开始我的服务器使用rvmsudo Rails的-“生产”- 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}

它看起来rvmsudo没有通过环境变量一样,我会看看它…

answer1: 回答1:

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 !

我的假设是正确的# 1编辑。问题是,rvmsudo通过环境变量的一些条件,可以在这里找到下grep命令:

http:/ / GitHub。COM / / / / dff3eeac883ce6314c021f3d00730bcd5a628e3e RVM RVM BLOB / binscripts / rvmsudo # L46

所以我改变我的环境变量名称:http_secret_key_base尝试和它的工作!

ruby-on-rails  secret-key