I'm generating a private key, this key is for demonstrable purposes only:
$ openssl genrsa
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEAvB8fZFRS83Kztend5KO9cnWXaqLWot0qLDeLcS8ly718FUdm
3VcCY5j737zz4iwmFf3b20Q2XxlbYC/M13wTJzHBf2d1mRDlpZq7CgX/JSEUW/Hr
uXiF6PI+ypkvskyoQcz04rlT8skd7tanXhXINnLwW7gCiNlxQQFkrpfO8Fkh+vYL
...
Ewac3GAh9CiMikQEYNxpsuLLboS4NcaQWiGB+1imtPtbp8Gf89pJSVBDubgza2Bb
rucNxP3HZtPd6G9CvkMJREYL7jHkXYa5DBzs9LB9mLB4b5H/6KN/fsfj
-----END RSA PRIVATE KEY-----
There is a newline
\n
at the end of each of these lines that needs removing, I want everything on a single line so I can set it to an env var. Note: I'm unable to store a multiline env var in .env
as docker-compose
doesn't support it.
I've stripped out all the new lines with this:
$(openssl genrsa | tr -d '\n')
-----BEGIN RSA PRIVATE KEY-----MII...-----END RSA PRIVATE KEY-----
I then manually insert two newlines \n
which I'm looking to automate through a script (hence this post). If I don't do this the signing of the JWT fails.
-----BEGIN RSA PRIVATE KEY-----\nMII...\n-----END RSA PRIVATE KEY-----
I define it within a .env
file
JWT_PRIVATE_KEY=-----BEGIN RSA PRIVATE KEY-----\nMII...\n-----END RSA PRIVATE KEY-----
With node and [dotenv](https://www.npmjs.com/package/dotenv) I access it like so:
privateRsaKey = process.env.JWT_PRIVATE_KEY.replace(/\\n/gm, '\n'),
Now privateRsaKey
looks like this:
-----BEGIN RSA PRIVATE KEY-----
MII...
-----END RSA PRIVATE KEY-----
Now I actually use the private key to sign a [JWT](https://www.npmjs.com/package/jsonwebtoken)
const signed = jwt.sign(payload, privateRsaKey, {
algorithm: 'RS256',
...
});
All of the above is working as expected when I bring up the Docker containers.
**I need help in the scripting so I don't have to manually insert two \n
**
Thank you all for you help and patience it's much appreciated.
Asked by user422813
(1 rep)
Jul 15, 2020, 04:26 PM
Last activity: Jul 17, 2020, 03:09 AM
Last activity: Jul 17, 2020, 03:09 AM