I am working on a Twilio application using Sinatra. Since I dont have too much experience with Ruby (but am excitedly learning), I am having a problem with separating the credentials from my file. I would like to upload the files to a repository, but I want to keep the sensitive credentials in a separate file that would be imported.
我正在使用Sinatra开发一个Twilio应用程序。由于我没有太多使用Ruby的经验(但是我正在兴奋地学习),我在将凭证从文件中分离出来时遇到了问题。我希望将文件上载到存储库,但我希望将敏感凭证保存在一个将被导入的单独文件中。
The file is currently composed of:
该文件目前由:
require 'rubygems'
require 'twilio-ruby'
account_sid = "xxxxxx"
auth_token = "xxxxx"
client = Twilio::REST::Client.new account_sid, auth_token
from = "+12341231234"
friends = {
"+1231231234" => "Lenny"
}
friends.each do |key, value|
client.account.sms.messages.create(
:from => from,
:to => key,
:body => "Hey #{value}, Monkey party at 6PM. Bring Bananas!"
)
puts "Sent message to.#{value}"
end
How would I properly load the account_sid
and auth_token
lines to a separate file? What is the best practice for storing credentials like this?
如何正确地将account_sid和auth_token行加载到一个单独的文件中?存储这样的凭证的最佳实践是什么?
1 个解决方案
#1
6
Two common practices for this are:
对此的两种常见做法是:
1) Store the variables as environment variables on your system and access them with ENV
1)将变量存储为系统上的环境变量,并使用ENV访问它们
account_sid = ENV["TWILIO_ACCOUNT_SID"]
auth_token = ENV["TWILIO_AUTH_TOKEN"]
client = Twilio::REST::Client.new account_sid, auth_token
2) The other is to store them in a YAML file on your server and then when you deploy your application, symlink this file to where it should be in your repository. This file should be in your .gitgnore
2)另一种方法是将它们存储在服务器上的YAML文件中,然后在部署应用程序时,将该文件与存储库中的位置进行符号链接。这个文件应该在你的文件夹里
#config.yml
twilio:
account_sid: "xxxxx"
auth_token: "xxxxx"
Then in your application
然后在你的应用程序
require 'yaml'
config = YAML.load_file("config.yml")
account_sid = config[:twilio][:account_sid]
auth_token = config[:twilio][:auth_token]
There are also several gems for configuration management, the only one I have used personally is figaro but it is rails specific.
还有一些用于配置管理的gem,我只使用过figaro,但是它是rails特有的。
#1
6
Two common practices for this are:
对此的两种常见做法是:
1) Store the variables as environment variables on your system and access them with ENV
1)将变量存储为系统上的环境变量,并使用ENV访问它们
account_sid = ENV["TWILIO_ACCOUNT_SID"]
auth_token = ENV["TWILIO_AUTH_TOKEN"]
client = Twilio::REST::Client.new account_sid, auth_token
2) The other is to store them in a YAML file on your server and then when you deploy your application, symlink this file to where it should be in your repository. This file should be in your .gitgnore
2)另一种方法是将它们存储在服务器上的YAML文件中,然后在部署应用程序时,将该文件与存储库中的位置进行符号链接。这个文件应该在你的文件夹里
#config.yml
twilio:
account_sid: "xxxxx"
auth_token: "xxxxx"
Then in your application
然后在你的应用程序
require 'yaml'
config = YAML.load_file("config.yml")
account_sid = config[:twilio][:account_sid]
auth_token = config[:twilio][:auth_token]
There are also several gems for configuration management, the only one I have used personally is figaro but it is rails specific.
还有一些用于配置管理的gem,我只使用过figaro,但是它是rails特有的。