简体   繁体   English

如何使用Ruby on Rails 4.1.0beta1管理秘密密钥和heroku?

[英]How do you manage secret keys and heroku with Ruby on Rails 4.1.0beta1?

With the release of the secrets.yml file, I removed my reliance on Figaro and moved all of my keys to secrets.yml and added that file to .gitignore. 随着secrets.yml文件的发布,我取消了对Figaro的依赖,并将所有密钥移到secrets.yml并将该文件添加到.gitignore。

But when I tried to push to Heroku, Heroku said they needed that file in my repo in order to deploy the website. 但是当我尝试推送到Heroku时,Heroku表示他们需要在我的存储库中使用该文件才能部署网站。 which makes sense, but I don't want my keys in git if I can avoid it. 这是有道理的,但是如果可以避免的话,我不想在git中输入密钥。

With Figaro, I would run a rake task to deploy the keys to heroku as env variables and keep application.yml in the .gitignore. 使用Figaro,我将运行一个rake任务以将密钥作为环境变量部署到heroku,并将application.yml保留在.gitignore中。 Obviously, I can't do that any more. 显然,我不能再这样做了。 So how do I handle this? 那么我该如何处理呢?

Secrets isn't a full solution to the environment variables problem and it's not a direct replacement for something like Figaro . 秘密不是解决环境变量问题的完整方法,也不是像Figaro这样的东西的直接替代品。 Think of Secrets as an extra interface you're now supposed to use between your app and the broader world of environment variables. 将“秘密”视为您现在应在应用程序和更广泛的环境变量之间使用的额外接口。 That's why you're now supposed to call variables by using Rails.application.secrets.your_variable instead of ENV["your_variable"] . 这就是为什么现在应该使用Rails.application.secrets.your_variable而不是ENV["your_variable"]来调用变量的原因。

The secrets.yml file itself is that interface and it's not meant to contain actual secrets (it's not well named). secrets.yml文件本身就是该接口,它并不意味着包含实际的机密(名称不正确)。 You can see this because, even in the examples from the documentation, Secrets imports environment variables for any sensitive values (eg the SECRET_KEY_BASE value) and it's automatically checked into source control. 您可以看到这一点,因为即使在文档中的示例中,Secrets也会为任何敏感值(例如SECRET_KEY_BASE值)导入环境变量,并且该变量会自动检查到源代码管理中。

So rather than trying to hack Secrets into some sort of full-flow environment variable management solution, go with the flow: 因此,与其尝试将Secrets入侵某种全流环境变量管理解决方案,不如顺其自然:

  1. Pull anything sensitive out of secrets.yml . secrets.yml取出所有敏感内容。
  2. Check secrets.yml into source control like they default you to. 像将它们作为默认值一样,将secrets.yml检入源代码管理。
  3. For all sensitive values, import them from normal environment variables into secrets ERB (eg some_var: <%= ENV["some_var"] %> ) 对于所有敏感值,请将其从正常环境变量导入秘密ERB(例如some_var: <%= ENV["some_var"] %>
  4. Manage those ENV vars as you normally would, for instance using the Figaro gem. 像平常一样管理那些ENV变量,例如使用Figaro gem。
  5. Send the ENV vars up to Heroku as you normally would, for instance using the Figaro gem's rake task. 像平常一样,将ENV变量发送到Heroku,例如使用Figaro gem的rake任务。

The point is, it doesn't matter how you manage your ENV vars -- whether it's manually, using Figaro, a .env file, whatever... secrets.yml is just an interface that translates these ENV vars into your Rails app. 问题的关键是,这不要紧,你如何管理你的ENV瓦尔-无论是手动,使用费加罗,一个.env文件,无论... secrets.yml仅仅是翻译这些ENV瓦尔到您的Rails应用程序的接口。

Though it adds an extra step of abstraction and some additional work, there are advantages to using this interface approach. 尽管它增加了抽象的额外步骤和一些额外的工作,但使用此接口方法还是有优势的。

Whether you believe it's conceptually a good idea or not to use Secrets, it'll save you a LOT of headache to just go with the flow on this one. 无论您认为使用Secrets在概念上是一个好主意,还是随心所欲地使用它都会为您省去很多麻烦。

PS. PS。 If you do choose to hack it, be careful with the heroku_secrets gem. 如果您确实选择破解它,请小心使用heroku_secrets gem。 As of this writing, it runs as a before_initialize in the startup sequence so your ENV vars will NOT be available to any config files in your config/environments/ directory (which is where you commonly would put them for things like Amazon S3 keys). 在撰写本文时,它在启动序列中作为before_initialize运行,因此您的ENV vars将不可用于config/environments/目录中的任何配置文件(在该目录中,您通常将它们用于放置Amazon S3密钥之类的东西)。

An equivalent for secrets.yml of that Figaro task is provided by the heroku_secrets gem, from https://github.com/alexpeattie/heroku_secrets : 那个Figaro任务的secrets.yml的等效项是来自heroku_secrets gem,来自https://github.com/alexpeattie/heroku_secrets

gem 'heroku_secrets', github: 'alexpeattie/heroku_secrets'

This lets you run 这使您可以运行

rake heroku:secrets RAILS_ENV=production

to make the contents of secrets.yml available to heroku as environment variables. 将secrets.yml的内容作为环境变量提供给heroku。

see this link for heroku settings 查看此链接了解heroku设置

if u want to run on local use like this 如果你想像这样在本地使用

KEY=xyz OTHER_KEY=123 rails s KEY = xyz OTHER_KEY = 123 rails

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM