How do I open source my Rails' apps without giving away the app's secret keys and credentials

后端 未结 5 1595
[愿得一人]
[愿得一人] 2020-12-13 07:38

I have a number of Rails apps hosted on GitHub. They are all currently private, and I often will deploy them from their GitHub repository. I\'d like to be able to make some

5条回答
  •  一个人的身影
    2020-12-13 08:01

    I actually took a hint from your question, using ENV.

    I had three different secret values that I didn't want made available. They're the app's secret token of course, and Twitter's consumer key and secret. In my secret token initializer:

    KinTwit::Application.config.secret_token = ENV['SECRET_TOKEN']
    
    Twitter.consumer_key                     = ENV['CONSUMER_KEY']
    Twitter.consumer_secret                  = ENV['CONSUMER_SECRET']
    

    I'm hosting my project on Heroku, so I added these as configuration variables to Heroku.

    [03:07:48] [william@enterprise ~/dev/rwc/kintwit]$ heroku config:add CONSUMER_KEY=ub3rs3cr3tk3y
    Adding config vars and restarting app... done, v7
      CONSUMER_KEY => ub3rs3cr3tk3y
    [03:08:40] [william@enterprise ~/dev/rwc/kintwit]$ heroku config:add CONSUMER_SECRET=ub3rs3cr3tk3y
    Adding config vars and restarting app... done, v8
      CONSUMER_SECRET => ub3rs3cr3tk3y
    [03:08:57] [william@enterprise ~/dev/rwc/kintwit]$ heroku config:add SECRET_TOKEN=ub3rs3cr3tk3y
    Adding config vars and restarting app... done, v9
      SECRET_TOKEN => ub3rs3cr3tk3y
    

    Now, the values are ready on my next push. But, what if you aren't using Heroku? I'm obviously not an expert on every single rails deployment (jeesh, not even a Heroku pro), but an example of this would be doing a db:migrate for testing.

    $ RAILS_ENV=test rake db:migrate
    

    The KEY=value pair before the command sets the environment variable, so running this command, echo ENV['RAILS_ENV'] would print test. So however this is set up in your environment is how you would do it. But, the environment variables aren't in your code, so that's the trick.

提交回复
热议问题