Multi-line config variables in Heroku

ConfigurationHeroku

Configuration Problem Overview


I've have a Rails app that loads a number of RSA certificates before a transaction is made with Paypal. On my development machine, these certificates are read from files in the file system but because Heroku (which I'm using for delpoyment) is largely read-only, I can't upload these files so I'm guessing I'll have to read the certificates from config variables (see Heroku Config Vars).

Because the certificates consist of multiple lines of data, I'm not sure how to set them as variables or even if this is possible. Does anyone know how I could do this or be able to suggest an alternative approach?

Many thanks, Eddie

Configuration Solutions


Solution 1 - Configuration

I found that a easy way to add multi-line configs is to double quote them and then echo them from my local environment

heroku config:add EC2_PRIVATE_KEY="$EC2_PRIVATE_KEY"

Solution 2 - Configuration

If you want to set Heroku config values from your file contents, you can use the following shell trick:

$ heroku config:set SECRET_KEY="$(cat path/to/secret.key)"

Multi-line values can be set directly by putting quotes around the value:

$ heroku config:set SECRET_KEY='first line
> second line'

If you're using Foreman to run locally (now heroku local), it doesn't support multi-line variables. You must use something to inject them into the environment first, such as envdir:

$ envdir my-env-dir heroku local

Solution 3 - Configuration

Or you can just go to the Settings tab of the Heroku dashboard, open Config Vars and paste it in.

Heroku Settings

Easy peasy.

Solution 4 - Configuration

We needed to do the same thing.

You can wrap the variable value in double quotes:

bobvila@bobuntu:~/svnroot/app/myapp$ heroku config:add woodchuck="How much wood
> could a woodchuck chuck
> if a woodchuck could chuck wood"
Adding config vars and restarting myapp... done, v25
woodchuck: How much wood
could a woodchuck chuck
if a woodchuck could chuck wood
bobvila@bobuntu:~/svnroot/app/myapp$ heroku config
=== Config Vars for myapp
woodchuck:                       How much wood
could a woodchuck chuck
if a woodchuck could chuck wood
bobvila@bobuntu:~/svnroot/app/myapp$ 

If you're using Foreman for localhost development, the .env file doesn't support multi-line variables so you'll need to export it to the shell before launching Foreman

Solution 5 - Configuration

My answer comes a bit late but I had the same issue recently with multi-line env. variables on Heroku. My solution was to use strict_encode64:

encoded_secret = Base64.strict_encode64("my_multi_line_secret")

add the key:

$ heroku config:set SECRET_KEY='the encoded_secret string here'

In the code, you then decode it using Base64.strict_decode64(ENV['SECRET_KEY'])

Solution 6 - Configuration

An example of how to deal with this problem using NodeJS. Sanitize the value by replacing \\n characters with \n:

process.env.FIREBASE_PRIVATE_KEY.replace(/\\n/g, '\n')

Taken from: https://stackoverflow.com/questions/39492587/escaping-issue-with-firebase-privatekey-as-a-heroku-config-variable

Solution 7 - Configuration

I know this is a very manual way of doing it, but what worked for me is to paste the private key into a text editor, find and replace \n with an actual line break and paste that as the value for the env var in Heroku.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEddie JohnstonView Question on Stackoverflow
Solution 1 - ConfigurationdanmayerView Answer on Stackoverflow
Solution 2 - ConfigurationJudy2KView Answer on Stackoverflow
Solution 3 - ConfigurationJames GentesView Answer on Stackoverflow
Solution 4 - ConfigurationdaveespoView Answer on Stackoverflow
Solution 5 - ConfigurationpaulldView Answer on Stackoverflow
Solution 6 - ConfigurationEugene KulabuhovView Answer on Stackoverflow
Solution 7 - Configurationdmc85View Answer on Stackoverflow