我有一个项目配置为使用 Rails 加密的机密。一切正常,直到我尝试访问production.rb
环境文件中的秘密。
我发现如果我尝试访问类似Rails.application.secrets.smtp_user_name
配置块中的内容,它会清除所有加密的秘密(我只剩下secrets.yml
......我没有使用的东西)。例子:
Loading production environment (Rails 5.1.2)
irb(main):001:0> Rails.application.secrets
=> {:secret_key_base=>nil, :secret_token=>nil}
如果我删除了访问机密的尝试,它可以正常工作:
irb(main):001:0> Rails.application.secrets
=> {:secret_key_base=>"...", :smtp_user_name=>"...", :smtp_password=>"...", :secret_token=>nil}
我目前正在通过使用以下两个配置块来解决它production.rb
:
# This is hacky, it needs to come before the second configure block where
# the encrypted secrets are used.
Rails.application.configure do
config.read_encrypted_secrets = true
end
Rails.application.configure do
... stuff that uses Rails.application.secrets, like ActionMailer
end
还有其他人面临这个问题并且可能有更正确的方法来解决它吗?
为什么会发生这种情况是有道理的(Rails 不知道加载加密的秘密,因为我们没有告诉你),但我认为必须有更好的方法来处理它。
更新
这让我在 9 个月后再次陷入困境。需要明确的是,如果您Rails.application.secrets
在调用之前引用,config.read_encrypted_secrets = true
您将缓存空秘密,并且无法访问secrets.yml.enc
!
在我的情况下,我曾尝试在application.rb
我config.read_encrypted_secrets = true
的设置中配置 Paperclip S3 凭据production.rb
。结果是devise.rb
试图读取密钥库的秘密,这一切都是因为application.rb
我有效地缓存了 nil 秘密。