简体   繁体   中英

How does a rails app know where to pull an ENV variable?

I am curious exactly how ENV[SOME_VARIABLE] tries to find the data with a standard rails install in a mac environment.

I encountered a problem that I overcame using the figaro gem, but it didn't answer my questions as to why it didn't work. I am no longer interested in troubleshooting the problem, but am interested in learning the specifics of how ENV[SOME_VARIABLE] finds that information.

Whenever you run a command on your computer, some other program is running that command. That parent program can provide settings to that child; these are generally known as environment variables .

If you open a terminal window on your Mac, you're looking at a shell . This is a command-line interface to your computer. It's running inside the terminal program, which itself is running inside something else - either your dock, or from Spotlight, or from the Finder (depending on how you ran it). By extension, if you run your Rails program by running rails server , the rails command is being run by your shell.

There's a number of ways to set environment variables in the shell. If you run export in a terminal, you should see a whole bunch. Your shell will set some automatically for you; common ones include HOME for your home directory, PATH for the locations to look for executable files, and USER for your username. You can print out these values by prepending a $ sign. You can also set your own by using the export command.

bob@host ~$ echo $USER
bob
bob@host ~$ echo $HOME
/Users/bob
bob@host ~$ export MYVAR="whatever"
bob@host ~$ echo $MYVAR
whatever

Anything you set using export will only exist until you close that terminal, and won't carry over into new terminals. You can put your export commands in a file that gets run when you open your terminal; Bash is the default shell on Macs, so you can edit your ~/.bashrc file to make it available in all terminals.


So let's go back to your rails server command. Let's say you wanted to provide a SECRET_KEY environment variable so you don't have to entrust that secret to your source control. You could put it in your .bashrc , as seen above. You could also provide it when you run your server, by setting it before the command:

bob@host rails_app$ SECRET_KEY=abc1234 rails server

You can also use add-ons like Figaro and dotenv to manage your settings in files. Inside Ruby, ENV is just a hash - a special hash provided to you by Ruby itself, but just a hash. Any code can add keys, delete keys, or change values within it. I haven't looked at the code for dotenv or Figaro, but I bet that's what they're doing - they're loading themselves via an initializer , reading the files where the environment variables are configured, and adding these environment variables to ENV .

Environment Variables

To simplify Alex P 's answer, environment variables are defined in the operating system - meaning they are available to any application which wishes to use them:

Environment variables are a set of dynamic named values that can affect the way running processes will behave on a computer.

Rails uses environment variables for a number of reasons, but mainly for security. If your application uses the likes of API keys etc, the environment variables allow you to define the sensitive information in a way which is only accessible at OS-level

This means if you run your Rails application on a host which is hacked, and your code is compromised, the hackers will not have full access to your data, as the ENV vars are stored in the OS.

How this works is described in Alex 's answer

--

Rails "Secrets" YML

One of the advances with Rails 4.1+ is the introduction of the secrets gem. This is designed to give you the same functionality as the ENV vars, except you'll be able to keep all your values local to your Rails app:

Rails 4.1 generates a new secrets.yml file in the config folder. By default, this file contains the application's secret_key_base, but it could also be used to store other secrets such as access keys for external APIs.

The secrets added to this file are accessible via Rails.application.secrets. For example, with the following config/secrets.yml

development:
  secret_key_base: 3b7cd727ee24e8444053437c36cc66c3
  some_api_key: SOMEKEY

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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