简体   繁体   中英

error:0909006C:PEM routines:get_name:no start line - for google cloud platform in heroku

When I try to use @google-cloud/storage it returns this error

Error: error:0909006C:PEM routines:get_name:no start line 
2021-12-13T10:36:19.800822+00:00 app[web.1]: at Sign.sign (internal/crypto/sig.js:110:29)
2021-12-13T10:36:19.800823+00:00 app[web.1]: at Object.sign (/app/node_modules/gtoken/node_modules/jwa/index.js:152:45) 
2021-12-13T10:36:19.800823+00:00 app[web.1]: at Object.jwsSign [as sign] (/app/node_modules/gtoken/node_modules/jws/lib/sign-stream.js:32:24) 
2021-12-13T10:36:19.800824+00:00 app[web.1]: at GoogleToken.requestToken (/app/node_modules/gtoken/build/src/index.js:225:31) 
2021-12-13T10:36:19.800824+00:00 app[web.1]: at GoogleToken.getTokenAsyncInner (/app/node_modules/gtoken/build/src/index.js:163:21) 
2021-12-13T10:36:19.800824+00:00 app[web.1]: at GoogleToken.getTokenAsync (/app/node_modules/gtoken/build/src/index.js:142:55) 
2021-12-13T10:36:19.800825+00:00 app[web.1]: at GoogleToken.getToken (/app/node_modules/gtoken/build/src/index.js:94:21) 
2021-12-13T10:36:19.800825+00:00 app[web.1]: at JWT.refreshTokenNoCache (/app/node_modules/google-auth-library/build/src/auth/jwtclient.js:171:36) 
2021-12-13T10:36:19.800826+00:00 app[web.1]: at JWT.refreshToken (/app/node_modules/google-auth-library/build/src/auth/oauth2client.js:152:24) 
2021-12-13T10:36:19.800826+00:00 app[web.1]: at JWT.getRequestMetadataAsync (/app/node_modules/google-auth-library/build/src/auth/oauth2client.js:284:28) 
2021-12-13T10:36:19.800827+00:00 app[web.1]: at JWT.getRequestMetadataAsync (/app/node_modules/google-auth-library/build/src/auth/jwtclient.js:94:26) 
2021-12-13T10:36:19.800827+00:00 app[web.1]: at JWT.getRequestHeaders (/app/node_modules/google-auth-library/build/src/auth/oauth2client.js:247:37) 
2021-12-13T10:36:19.800827+00:00 app[web.1]: at GoogleAuth.authorizeRequest (/app/node_modules/google-auth-library/build/src/auth/googleauth.js:600:38) 
2021-12-13T10:36:19.800828+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:95:5) 
2021-12-13T10:36:19.800828+00:00 app[web.1]: at async Promise.all (index 1) 
{ 2021-12-13T10:36:19.800829+00:00 app[web.1]: library: 'PEM routines', 
2021-12-13T10:36:19.800829+00:00 app[web.1]: function: 'get_name', 
2021-12-13T10:36:19.800829+00:00 app[web.1]: reason: 'no start line', 
2021-12-13T10:36:19.800829+00:00 app[web.1]: code: 'ERR_OSSL_PEM_NO_START_LINE' 
2021-12-13T10:36:19.800830+00:00 app[web.1]: } 
2021-12-13T10:36:19.800879+00:00 app[web.1]: Error error:0909006C:PEM routines:get_name:no start line 

It appears the error is based on the private key from google cloud service account

-----BEGIN PRIVATE KEY-----
ENTIREKEY
-----END PRIVATE KEY----- 

I solved this by removing the \n line in the keys and manually adding a new line eg:

Private key: \n---BEGIN PRIVATE KEY----\nShfkkJHNJK\nJeidj\n---END PRIVATE KEY----\n

To:


---BEGIN PRIVATE KEY----
ShfkkJHNJK
Jeidj
---END PRIVATE KEY----

NB: Don't forget the last \n .

It seems the issue is on "how we import" the key. There are several solution, but the best solution I'd like when in my case I import the key from env variable is adding .replace(/\\n/gm, "\n") when I want to import the key from env.

Before:

process.env.PRIVATE_KEY

After:

process.env.PRIVATE_KEY?.replace(/\\n/gm, "\n")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM