我有一個項目配置爲使用Rails加密機密。一切正常,直到我嘗試訪問production.rb
環境文件中的一個祕密。Rails 5不會加載生產中的加密機密.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不知道加載加密的祕密,因爲我們沒有告訴你),但我想一定有更好的方法來處理它。