简体   繁体   English

如何在git存储库中隐藏密码?

[英]How to hide password in git repository?

I have a very simple Rails app that sends out a welcome email when the user signs up. 我有一个非常简单的Rails应用,当用户注册时会发送一封欢迎电子邮件。 I'm using my gmail account to send the message, and I have the password for my gmail account stored in the app, as shown below: 我正在使用我的gmail帐户发送消息,并且我的gmail帐户的密码存储在应用程序中,如下所示:

application.rb 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 application.yml

MAIL_PASSWORD: "my_password"


I want to hide the password stored in the application.yml file in my git repository. 我想在git储存库中隐藏存储在application.yml文件中的密码。 I tried adding the application.yml to my gitignore file, but that just crashes my app. 我尝试将application.yml添加到我的gitignore文件中,但这只会使我的应用程序崩溃。

How do I hide this password in my git repository so that my app still works, and I don't have to put my app into a private repository? 如何在git存储库中隐藏此密码,以便我的应用程序仍然可以运行,而不必将我的应用程序放入私有存储库中?

You basically can't. 你基本上不能。 That sort of info is best stored as an environment variable on the server, or in a config file that you don't add to git. 这类信息最好作为环境变量存储在服务器上,或者存储在未添加到git的配置文件中。

my exemple for sendgrid: 我的sendgrid示例:

development.rb 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

on your server edit bash profile: 在服务器上编辑bash配置文件:

nano .bash_profile

and add your value: 并添加您的价值:

export SENDGRID_USERNAME=yourusername
export SENDGRID_PASSWORD=yourpassword

after this on my local machine I have to close all console windows and open it again to initialize these env variables and you are good to go. 在这之后我的本地计算机上我必须关闭所有控制台窗口,然后再次打开它初始化这些env variables ,你是好去。 If you do this on vps you probably need to restart your vps, not sure. 如果您在vps上执行此操作,则可能不确定是否需要重新启动vps。

Try using dotenv gem https://rubygems.org/gems/dotenv . 尝试使用dotenv gem https://rubygems.org/gems/dotenv By mentioning .env files in .gitignore you can achieve your desired result. 通过在.gitignore中提及.env文件,您可以实现所需的结果。

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

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