admin管理员组文章数量:1136549
I have a multiline private key in a gatsby .env
file:
GATSBY_GOOGLE_CLIENT_ID="12345"
GATSBY_GOOGLE_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\nflkdflkdf...\n-----END PRIVATE KEY-----"
In my gatsby-config file I have:
module.exports = {
resolve: 'gatsby-source-google-sheets',
options: {
credentials: {
"type": "service_account",
"private_key": process.env.GATSBY_GOOGLE_PRIVATE_KEY,
"client_id": process.env.GATSBY_GOOGLE_CLIENT_ID
}
}
}
The client_id
works fine because it's just a one line string but the private_key
doesn't work, presumably because it's multi line.
Is there a way I can get around this?
Thanks
I have a multiline private key in a gatsby .env
file:
GATSBY_GOOGLE_CLIENT_ID="12345"
GATSBY_GOOGLE_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\nflkdflkdf...\n-----END PRIVATE KEY-----"
In my gatsby-config file I have:
module.exports = {
resolve: 'gatsby-source-google-sheets',
options: {
credentials: {
"type": "service_account",
"private_key": process.env.GATSBY_GOOGLE_PRIVATE_KEY,
"client_id": process.env.GATSBY_GOOGLE_CLIENT_ID
}
}
}
The client_id
works fine because it's just a one line string but the private_key
doesn't work, presumably because it's multi line.
Is there a way I can get around this?
Thanks
Share Improve this question edited Apr 2, 2019 at 8:26 John Sibly 23k7 gold badges64 silver badges83 bronze badges asked Apr 1, 2019 at 16:19 mckeever02mckeever02 7412 gold badges7 silver badges15 bronze badges 1 |9 Answers
Reset to default 66You could use string.replace with a regular expression as below to escape the \n characters again:
"private_key": process.env.GATSBY_GOOGLE_PRIVATE_KEY.replace(/\\n/g, '\n'),
Solution which worked for me -- Encoding the Private Key in base 64
Step1 - Convert Key to Base 64
// Run this code in a JS file on your Dev Machine.
const privateKey= `-----BEGIN PRIVATE KEY-----\nMIIEvSomeMoreCharacterHererplw==\n-----END PRIVATE KEY-----\n`
const buff = Buffer.from(privateKey).toString('base64');
console.log(buff);
Note: You don't need to commit/include the above code in your project. This is just to generate the base64 string of the key.
Step 2 - Copy the console log data to .env
file
PRIVATE_KEY = 'akgjhakdgjhasgf'
Step 3 - Using the Key in the code
const key = Buffer.from(process.env.PRIVATE_KEY , 'base64').toString('ascii');
// Use key anywhere in your code.
- Copy your content from your pem file to the browser's console (add ``):
`-----BEGIN RSA PRIVATE KEY-----
loremipsum...
-----END RSA PRIVATE KEY-----`
- Copy the log from the browser (notice
\n
s were added) - Add it to your env file (notice the
""
):
PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\nloremipsum...\n-----END RSA PRIVATE KEY-----"
I'm adding a manual approach that worked for me. Step 1:
echo "PRIVATE_KEY=\"`sed -E 's/$/\\\n/g' my_rsa_2048_priv.pem`\"" >> .env
Your key in the .env file will look something like this:
PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\n
dasdasdadasdasdasdasdasdasdasdadasdasdadasa\n
huehuauhhuauhahuauhauahuauhehuehuauheuhahue\n
-----END RSA PRIVATE KEY-----\n"
Step 2. Printing the value process.env.PRIVATE_KEY
in your code will only show the first line: -----BEGIN RSA PRIVATE KEY-----\n
. To fix this, edit the variable in .env
to a single line. Like this:
PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\ndasdasdadasdasdasdasdasdasdasdadasdasdadasa\nhuehuauhhuauhahuauhauahuauhehuehuauheuhahue\n-----END RSA PRIVATE KEY-----\n"
Now process.env.PRIVATE_KEY
will be outputted correctly.
I have similar issues where i have to read .pem file content. The following approach worked for me.
- Convert the .pem content into base64 format
- Put converted base64 content (this will be single line string) in .env file
- Now decode env variable into original content
Turns out the path to my .env was incorrect. For some reason the other keys were working but the private key was not.
The correct setup:
require("dotenv").config({
path: `./.env.${process.env.NODE_ENV}`,
});
const private_key = process.env.GATSBY_GOOGLE_PRIVATE_KEY.replace(/\\n/g, '\n');
module.exports = {
resolve: 'gatsby-source-google-sheets',
options: {
credentials: {
"private_key": private_key,
}
}
}
You'd have to load the env variables into gatsby. The simplest way is to use dotenv
:
Setup:
yarn add -D dotenv # or npm install -D dotenv
Then in your gatsby-config.js
:
require('dotenv').config();
module.exports = {
plugins: [ ... ]
}
If your file name is different than .env
or you store it in different location, you can pass in a path option:
require('dotenv').config({
path: 'path/to/env/file'
});
Put in in a pem file and then right it to your .env with replacements
echo "export test_key=\"`sed -E 's/$/\\\n/g' ./gitbu.2018-03-23.private-key.pem`\"" >> .env
I just remove ";" from the end of private key in .env file and it worked for me...I know it would seems dumb for some but many made this mistake as they are too used to with Javascript and solidity...
本文标签: javascriptUsing private key in a env fileStack Overflow
版权声明:本文标题:javascript - Using private key in a .env file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736936767a1956971.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
{
and missing,
in the code above. I've corrected these, but I'm not sure if the same mistake is in your original version? – John Sibly Commented Apr 2, 2019 at 8:27