I\'m attempting to load an RSA private key into my nodejs application using environment variables, but the newlines seem to be being auto-escaped.
For the following, ass
dotenv supports newlines with double quotes:
double quoted values expand new lines
MULTILINE="new\nline"
becomes{MULTILINE: 'new line'}
Just replace \n
before use the value:
var private_value = process.env.PRIVATE_KEY.replace(/\\n/g, '\n');
console.log(private_value);
will result correctly:
-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQCqGKukO1De7zhZj6+H0qtjTkVxwTCpvKe4eCZ0FPqri0cb2JZfXJ/DgYSF6vUp
wmJG8wVQZKjeGcjDOL5UlsuusFncCzWBQ7RKNUSesmQRMSGkVb1/3j+skZ6UtW+5u09lHNsj6tQ5
1s1SPrCBkedbNf0Tp0GbMJDyR4e9T04ZZwIDAQABAoGAFijko56+qGyN8M0RVyaRAXz++xTqHBLh
3tx4VgMtrQ+WEgCjhoTwo23KMBAuJGSYnRmoBZM3lMfTKevIkAidPExvYCdm5dYq3XToLkkLv5L2
pIIVOFMDG+KESnAFV7l2c+cnzRMW0+b6f8mR1CJzZuxVLL6Q02fvLi55/mbSYxECQQDeAw6fiIQX
GukBI4eMZZt4nscy2o12KyYner3VpoeE+Np2q+Z3pvAMd/aNzQ/W9WaI+NRfcxUJrmfPwIGm63il
AkEAxCL5HQb2bQr4ByorcMWm/hEP2MZzROV73yF41hPsRC9m66KrheO9HPTJuo3/9s5p+sqGxOlF
L0NDt4SkosjgGwJAFklyR1uZ/wPJjj611cdBcztlPdqoxssQGnh85BzCj/u3WqBpE2vjvyyvyI5k
X6zk7S0ljKtt2jny2+00VsBerQJBAJGC1Mg5Oydo5NwD6BiROrPxGo2bpTbu/fhrT8ebHkTz2epl
U9VQQSQzY1oZMVX8i1m5WUTLPz2yLJIBQVdXqhMCQBGoiuSoSjafUhV7i1cEGpb88h5NBYZzWXGZ
37sJ5QsW+sJyoNde3xH8vdXhzU7eT82D6X/scw9RZz+/6rCJ4p0=
-----END RSA PRIVATE KEY-----
For the benefit of anyone out there trying to use multi-line variable (i.e RSA private keys) in a dotenv file (should work for any configuration file though).
1) make each line of the private key end with a \n
such that first line**\n**
second line**\n**
and the third line and so on
2) now make the variable in single line in double quotes as follow KEY="such that first line**\nsecond line\nand the third line and so on\n**"
3) Ensure you add a leading and trailing \n character for the key as well KEY="\nsuch that first line**\nsecond line\nand the third line and so on\n**"
4) now in your code (i.e node, of course if you do not have access to code you have rely on the behaviour of the platform/app)
goodlyRepresentedKey = (process.env.KEY).replace(/\n/g, '\n');
(if biggly why not goodly)
I was using:
JSON.parse(`"${process.env.PUBPEM}"`)
which suddenly started to fail. So I changed it to:
JSON.parse(JSON.stringify(process.env.PUBPEM))
Which solved it for me.
Node will take the environment vars and escape them to avoid interpolation:
% export X="hey\nman"
% echo $X
hey
man
% node
> process.env['X']
'hey\\nman'
>
One option is to set the variable outside of node with actual newlines:
% export X="hey
dquote> man"
% node
> process.env['X']
'hey\nman'
> console.log(process.env['X'])
hey
man
undefined
>
This also works within script files, just use newlines within quotes. You can also do a replacement:
% export X="hey\nman\ndude\nwhat"
% node
> console.log(process.env['X'].replace(/\\n/g, '\n'))
hey
man
dude
what
As described in another topic you can encode yout multiline string in base64 and put it inside .env like
HTTPS_CA_CERTIFICATE=LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURDVENDQWZHZ0F3SUJBZ0lVY29lWFp1R
And then decode it inside your app
var cert = new Buffer(process.env.HTTPS_CA_CERTIFICATE, 'base64').toString('ascii');