The Figaro Gem: an easier way to securely configure Rails applications

Eric Kollegger
2 min readMay 3, 2018

--

Keeping API keys and other sensitive information off your git repositories is fairly easy to accomplish. While important to get right, this is a commonly occurring and relatively menial task. As developers we want to gain back every minute out of our busy schedules, so naturally we automate such a process whenever possible. Enter Figaro, a gem that strives to be more seamlessly secure by encouraging a convention that keeps configuration out of Git. Figaro is inspired by the Twelve-Factor App methodology, which states:

The twelve-factor app stores config in environment variables (often shortened to env vars or env). Env vars are easy to change between deploys without changing any code; unlike config files, there is little chance of them being checked into the code repo accidentally; and unlike custom config files, or other config mechanisms such as Java System Properties, they are a language- and OS-agnostic standard.

It’s only built to support a simple ENV key/value store, so those requiring more deeply nested configuration structures need not read on. This has met my own development needs the majority of the time, and it’s a snap to get up and running. Simply add gem 'Figaro' to your Gemfile and run bundle exec figaro install which will both generate an application.yml file and add it to your .gitignore file in one shot. Then add your own configuration to that file as needed and your set to go! At this point your default application.yml file (along with your custom config) might look something like this:

# Add configuration values here, as shown below.API_KEY: 'yourprimaryapikeygoeshere'MAPS_KEY: 'thiswouldbemygooglemapsaipkey'GEOCODE_KEY: 'finallymygeocodekey'

Of course it’s possible for local configuration values to change depending on the Rails environment. Luckily with Figaro we can add environment-specific values to our configuration file:

# config/application.yml

pusher_app_id: "2954"
pusher_key: "7381a978f7dd7f9a1117"
pusher_secret: "abdc3b896a0ffb85d373"

test:
pusher_app_id: "5112"
pusher_key: "ad69caf9a44dcac1fb28"
pusher_secret: "83ca7aa160fedaf3b350"

There’s a ton of other great quality-of-life features to explore with Figaro, but this article is meant as a primer to put it on people’s radar so they can go explore it themselves.

--

--