【问题标题】:How do I set secrets如何设置秘密
【发布时间】:2014-04-30 06:48:49
【问题描述】:

长话短说,除了我的本地机器外,我决定使用 VM 进行开发。 因此,当我在该 VM 中提取源代码并运行 rspec 时,我收到以下输出:

action@rails:~/workspace(master)$ rspec                                                                                                                                                                                                            
/home/action/.rvm/gems/ruby-2.0.0-p451/gems/devise-3.2.3/lib/devise/rails/routes.rb:481:in `raise_no_secret_key': Devise.secret_key was not set. Please add the following to your Devise initializer: (RuntimeError)                                                              

  config.secret_key = '...'  

我已经添加了密钥,但现在我在规范中有以下错误:

2) Password pages user views his passwords                                                                                                                                                                                                                                      
     Failure/Error: sign_in user                                                                                                                                                                                                                                                  
     RuntimeError:                                                                                                                                                                                                                                                                
       Missing `secret_key_base` for 'test' environment, set this value in `config/secrets.yml`                                                                                                                                                                                   
     # ./spec/support/login_macros.rb:3:in `sign_in'                                                                                                                                                                                                                              
     # ./spec/features/account_pages_spec.rb:7:in `block (2 levels) in <top (required)>'       

那个文件里面应该有什么?

【问题讨论】:

    标签: ruby-on-rails rspec nitrousio


    【解决方案1】:

    我刚刚安装了 rails 4.1 并创建了一个新项目。以下是config/secrets.yml默认生成的内容:

    # Be sure to restart your server when you modify this file.
    
    # Your secret key is used for verifying the integrity of signed cookies.
    # If you change this key, all old signed cookies will become invalid!
    
    # Make sure the secret is at least 30 characters and all random,
    # no regular words or you'll be exposed to dictionary attacks.
    # You can use `rake secret` to generate a secure secret key.
    
    # Make sure the secrets in this file are kept private
    # if you're sharing your code publicly.
    
    development:
      secret_key_base: 83aa0c7d6e2ed4574099514eb64bc3896fb8a71a344935fbd54705e0dd65adb897bc062fe477d03395a4d65675c833ba73ed340166be3874bfc01f43d6076385
    
    test:
      secret_key_base: 513fb7657945b56098db290394bf23f5e11463c473fb228719428a30fd34b8b899dff3f6173c32d7e6bc028dc3276f15dcba11b684d27983d8203fb5634ce8ae
    
    # Do not keep production secrets in the repository,
    # instead read values from the environment.
    production:
      secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
    

    【讨论】:

    • 每台机器上的这些键是否应该不同?
    • 对于生产来说,拥有不同的密钥对安全很重要。但是对于开发/分期,它们可能是相同的。生成一个新密钥就像运行rake secret 一样简单,所以最好让它们保持不同(并保密;))。
    【解决方案2】:

    您可以使用rake secret 生成新密钥,然后更新config.secret_key 的值。

    $ rake secret
    

    使用上述命令的输出作为config.secret_key 的值,通常放在config/initializers/devise.rb 中进行设计。如果您也在使用它,请重新启动 rails 服务器。

    【讨论】:

      猜你喜欢
      • 2016-08-21
      • 2016-02-17
      • 1970-01-01
      • 2020-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-23
      • 1970-01-01
      • 2016-03-12
      相关资源
      最近更新 更多