簡體   English   中英

如何在git存儲庫中隱藏密碼?

[英]How to hide password in git repository?

我有一個非常簡單的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存儲庫中隱藏此密碼,以便我的應用程序仍然可以運行,而不必將我的應用程序放入私有存儲庫中?

你基本上不能。 這類信息最好作為環境變量存儲在服務器上,或者存儲在未添加到git的配置文件中。

我的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。

嘗試使用dotenv gem https://rubygems.org/gems/dotenv 通過在.gitignore中提及.env文件,您可以實現所需的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM