Set multiline ENV variable in production

Did you try to set up a private key as ENV variable in production but you get the following error:

OpenSSL::PKey::RSAError: Neither PUB key nor PRIV key: nested asn1 error


So if you are setting your ENV variables through a terminal tool, use the following approach:

MY_KEY=\$(cat my_multiline_key.txt)


Next, we need to remove the escape character added when setting the key:

ENV.fetch("MY_KEY").gsub("\\n", "\n")


That’s it we have fixed this error and our customers are happy again! 🙂