我疯了,还是在实际(开发/生产)配置文件中保留我的 ActionMailer 的 SMTP 用户名和密码是个坏主意?似乎我应该将它存储在一个加密的地方,或者至少从我的 Mercurial 推送中排除它。
现在,我只是在执行推送之前从源文件中删除密码,但必须有一种比我使用的更聪明的方法。:)
也许我应该将它作为另一个用户(已经使用加密密码存储)存储在我的数据库中并以编程方式获取它?
我疯了,还是在实际(开发/生产)配置文件中保留我的 ActionMailer 的 SMTP 用户名和密码是个坏主意?似乎我应该将它存储在一个加密的地方,或者至少从我的 Mercurial 推送中排除它。
现在,我只是在执行推送之前从源文件中删除密码,但必须有一种比我使用的更聪明的方法。:)
也许我应该将它作为另一个用户(已经使用加密密码存储)存储在我的数据库中并以编程方式获取它?
使用未存储在存储库中的应用程序配置文件来存储敏感信息。以下是我的做法:
app_config.yml
在您的config
目录中添加一个。它的内容如下所示:
smtp_password: kl240jvfslkr32rKgjlk
some_other_password: 34hg9r0j0g402jg
and_so_on: lkn$@gJkjgsFLK4gaj
preinitializer.rb
在您的目录中添加一个config
包含以下内容的内容:
require 'yaml'
APP_CONFIG = YAML.load(File.read(RAILS_ROOT + "/config/app_config.yml"))
用您的密码替换APP_CONFIG
变量中的值,如下所示:
smtp_password = kl240jvfslkr32rKgjlk # old version
smtp_password = APP_CONFIG['smtp_password'] # new version
确保您不包含app_config.yml
在您的存储库中,尽管您可能想要创建一个已签入的示例文件,只是为了显示其中应该包含的内容的示例。部署应用程序时,请确保将app_config.yml
其存储在服务器上。如果您使用的是标准 Capistrano 部署,请将文件放在共享文件夹中并更新您的部署任务以在当前版本的目录中创建指向它的符号链接。
Jimmy's answer is perfect (+1), I would also note that Github has recommended .gitignore files for every language and the Rails one is here Note that it includes config/*.yml so that no config/yml file is in the respository to begin with. Probably a good move.
Use Capistrano to ask for these things upon deploy:setup the same way you should be doing for your database stuff:
task :my_silly_task do
sendgrid_password = Capistrano::CLI.password_prompt("Sendgrid password: ")
require 'yaml'
spec = {... whatever yaml you need -- probably what Jimmy said...}
run "mkdir -p #{shared_path}/config"
put(spec.to_yaml, "#{shared_path}/config/mailer_config.yml")
end