使用生产环境设置的Heroku测试环境中的Rails加密凭据

11
Heroku建议不要使用名为staging的自定义环境,而是建议在production环境下使用不同的ENV变量集。这很有道理(请看这个问题)。
然而,我想知道如何将这种做法与Rails 6加密凭据功能结合起来。加密凭据支持多个环境,因此我们可以将developmentproduction凭据分开;但是,使用Heroku的建议意味着production凭据将在实际的生产服务器和实际的暂存服务器之间共享。这是我不想看到的。
我想要的是staging服务器在production环境中使用development凭据!
保持所有不同的凭据上传并更新(包括生产、暂存和所有开发人员)很麻烦,加密文件似乎是一个非常必要的改进;我只是弄不清如何让暂存使用非生产凭据。
PS:也许可以根据在赫鲁晓克设置的环境变量来覆盖文档中的config.credentials.content_path以指示是否使用生产或开发凭证。好奇其他人正在做什么或可能会做什么。
1个回答

20

覆盖确实是解决方案。这是我的设置。

由于 RAILS_ENV 按照 Heroku 的建议设置为 production,因此我使用另一个环境变量,称为 PIPE_ENV,并将其设置为管道中的位置,例如 stagingedge(用于开发)等。

现在在 application.rb 中,我设置了 content_path。

module MyAppName
  class Application < Rails::Applicationif ENV["PIPE_ENV"].present?
      Rails.application.config.credentials.content_path = Rails.root.join("config/credentials/#{ENV["PIPE_ENV"]}.yml.enc")
    end
  end
end

我不喜欢在这里放东西,但config/environments/production.rb使用凭据来设置邮件发送器,所以必须提前设置。

另外,请勿忘记将RAILS_MASTER_KEY设置为相应的环境,因此对于暂存你需要执行:

heroku config:set RAILS_MASTER_KEY=your-staging-key -a your-staging-app

当然,your-staging-key是位于config/credentials/staging.key中的字符串。

额外提示:对于您的应用程序的其余部分,您可以将以下内容添加到config/initializer/pipe_env.rb中,这样您就可以像调用Rails.env一样调用Rails.pipe_env

module Rails
  class << self
    def pipe_env
      @_pipe_env = ActiveSupport::StringInquirer.new(ENV["PIPE_ENV"].presence || Rails.env)
    end
  end
end

application.rb 中,您还可以设置 key_path,以便在开发环境中测试 PIPE_ENV=staging rails cPIPE_ENV=staging rails s 也能正常工作。而 Rails.application.config.credentials.content_path 实际上可以简化为 config.credentials.content_path - fcatuhe
module MyAppName class Application < Rails::Application … if ENV["PIPE_ENV"].present? config.credentials.key_path = Rails.root.join("config/credentials/#{ENV['PIPE_ENV']}.key") config.credentials.content_path = Rails.root.join("config/credentials/#{ENV['PIPE_ENV']}.yml.enc") end end end ``` - fcatuhe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接