【问题标题】:How to hide password in git repository?如何在 git 存储库中隐藏密码?
【发布时间】:2013-07-12 14:46:18
【问题描述】:

我有一个非常简单的 Rails 应用程序,它会在用户注册时发送一封欢迎电子邮件。我正在使用我的 gmail 帐户发送消息,并且我的 gmail 帐户的密码存储在应用程序中,如下所示:

application.rb

require File.expand_path('../boot', __FILE__)

require 'rails/all'

if defined?(Bundler)
  # If you precompile assets before deploying to production, use this line
  Bundler.require(*Rails.groups(:assets => %w(development test)))
  # If you want your assets lazily compiled in production, use this line
  # Bundler.require(:default, :assets, Rails.env)
end

ENV.update YAML.load(File.read(File.expand_path('../application.yml', __FILE__)))

module TestMailApp
  class Application < Rails::Application

    config.action_mailer.smtp_settings = {
      :address              => "smtp.gmail.com",
      :port                 => 587,
      :domain               => "my address",
      :user_name            => "my_gmail_name",
      :password             => ENV["MAIL_PASSWORD"],
      :authentication       => :plain,
      :enable_starttls_auto => true
    }

    config.action_mailer.default_url_options = {
      :host => "my address"
    }   


application.yml

MAIL_PASSWORD: "my_password"


我想隐藏存储在我的 git 存储库中的 application.yml 文件中的密码。我尝试将 application.yml 添加到我的 gitignore 文件中,但这只会使我的应用程序崩溃。

如何在我的 git 存储库中隐藏此密码,以便我的应用仍然可以运行,并且我不必将我的应用放入私有存储库?

【问题讨论】:

标签: ruby-on-rails git github


【解决方案1】:

你基本上不能。这类信息最好作为环境变量存储在服务器上,或者存储在您不添加到 git 的配置文件中。

【讨论】:

  • 您不能将其存储在 git 存储库中,也不能对存储库的其他用户隐藏它。那是真实的。但是您可以在 .gitingore 中拥有该文件,并在您的 repo 克隆(在服务器上)中有一个本地版本。它不会受版本控制,但这可能正是您想要的凭据。见stackoverflow.com/a/17484565/2536029
【解决方案2】:

我的 sendgrid 示例:

development.rb

  if ENV['SENDGRID_USERNAME']
    config.action_mailer.smtp_settings = {
        :address => 'smtp.sendgrid.net',
        :domain => 'heroku.com',
        :port => '587',
        :authentication => :plain,
        :user_name => ENV['SENDGRID_USERNAME'],
        :password => ENV['SENDGRID_PASSWORD']
    }
  end

在您的服务器上编辑 bash 配置文件:

nano .bash_profile

并添加您的价值:

export SENDGRID_USERNAME=yourusername
export SENDGRID_PASSWORD=yourpassword

在我的本地机器上之后,我必须关闭所有控制台窗口并再次打开它来初始化这些env variables,然后你就可以开始了。如果您在 vps 上执行此操作,您可能需要重新启动您的 vps,不确定。

【讨论】:

    【解决方案3】:

    尝试使用 dotenv gem https://rubygems.org/gems/dotenv 。通过在 .gitignore 中提及 .env 文件,您可以获得所需的结果。

    【讨论】:

      猜你喜欢
      • 2012-09-05
      • 2014-12-10
      • 2021-04-07
      • 2013-09-27
      • 1970-01-01
      • 1970-01-01
      • 2021-12-10
      • 1970-01-01
      • 2020-05-14
      相关资源
      最近更新 更多