Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set multiline RSA private key environment variable for AWS Elastic Beans

I am deploying a Ruby on Rails application to AWS using Elastic Beanstalk and have to set a private key as an environment variable

E.g

-----BEGIN RSA PRIVATE KEY----- SpvpksXQIBA65ICOgQxV2TvMIICAiMeV9prhdJSKjjsk2 tYdz8lhn/ibROQW71utuHLAyHGMBxz3kIaaIq1kjdkkk tYdz8lhn/ibROQW71utuHLAyHGMBxz3kIaaIq1kjdkkk tYdz8lhn/ibROQW71utuHLAyHGMBxz3kIaaIq1kjdkkk tYdz8lhn/ibROQW71utuHLAyHGMBxz3kIaaIq1kjdkkk -----END RSA PRIVATE KEY-----

However this doesn't seem to work when deploying the app as it always fails with a

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

I think it's because the RSA Key is malformed.

However unlike in Heroku, AWS EB does not accept multiline input (see below) so I have to use \n to create new lines.

enter image description here

I tried with few different styles but none of them seem to interpolate the \n properly and I always keep getting the same error.

I've tried with \n and the end of each line, then \\n and also tried tried double quotes \" to wrap the key but I still keep getting the same error.

How do I properly set a multiline environment variable in AWS Elastic Beanstalk ?

like image 322
Ranhiru Jude Cooray Avatar asked Mar 29 '17 01:03

Ranhiru Jude Cooray


2 Answers

You could set it in EB using \n and then convert the '\n' to newlines before you pass it to config.key - something like this (note the single and double quotes in the call to gsub):

single_line_key = ENV.fetch('CLOUDFRONT_KEY')
multi_line_key = single_line_key.gsub('\n', "\n")
config.key = multi_line_key
like image 189
Brian Avatar answered Sep 30 '22 13:09

Brian


In I had the same problem with Golang and the elastic beanstalk, I did this went to AWS console and set the value like this:

-----BEGIN RSA PRIVATE KEY-----\nSpvpksXQIBA65ICOgQxV2TvMIICAiMeV9prhdJSKjjsk2\ntYdz8lhn/ibROQW71utuHLAyHGMBxz3kIaaIq1kjdkkk\ntYdz8lhn/ibROQW71utuHLAyHGMBxz3kIaaIq1kjdkkk\ntYdz8lhn/ibROQW71utuHLAyHGMBxz3kIaaIq1kjdkkk\ntYdz8lhn/ibROQW71utuHLAyHGMBxz3kIaaIq1kjdkkk\n-----END RSA PRIVATE KEY-----  

inside my code

key := os.Getenv("PUSH_AUTH_KEY")
key = strings.Replace(key, `\n`, "\n", 5)
like image 29
Shubham Dhanera Avatar answered Sep 30 '22 12:09

Shubham Dhanera