I am experiencing a hard time with firebase-admin sdk and cloud run environment variables.
Everything works fine on localhost, but the warnings and error show up on cloud run.
The cloud run console shows an endless warning message every few seconds:
@firebase/database: FIREBASE WARNING: {"library":"PEM routines","function":"get_name","reason":"no start line","code":"ERR_OSSL_PEM_NO_START_LINE"}
In addition, an error is shown when I hit a page with server side rendering with firebase admin request to database.
"FirebaseAppError: Failed to parse private key: Error: Invalid PEM formatted message.
at new ServiceAccount (/app/node_modules/firebase-admin/lib/app/credential-internal.js:144:19)
at new ServiceAccountCredential (/app/node_modules/firebase-admin/lib/app/credential-internal.js:70:15)
at Object.cert (/app/node_modules/firebase-admin/lib/app/credential-factory.js:103:54)
at Object.355 (/app/.next/server/chunks/898.js:32:86)
at __webpack_require__ (/app/.next/server/webpack-runtime.js:25:42)
at Object.5175 (/app/.next/server/pages/user/order/[orderId].js:24:76)
at __webpack_require__ (/app/.next/server/webpack-runtime.js:25:42)
at __webpack_exec__ (/app/.next/server/pages/user/order/[orderId].js:350:39)"
I would like to solve both issues:
After following the steps showed in the link below to get the private key structured like in the example:
https://park.is/blog_posts/20210118_add_a_multiline_env_variable_to_vercel/
-----BEGIN PRIVATE KEY-----
abcde
fghij
klmno
pqrs=
-----END PRIVATE KEY-----
I am now getting this new error showed bellow which I Presume it is still related to firebase admin private key.
I also tried to paste the private key just like it came from the downloaded .json file and other ways as well, but nothing seems to work.
Error: 16 UNAUTHENTICATED: Failed to retrieve auth metadata with error: error:0909006C:PEM routines:get_name:no start line at Object.callErrorFromStatus (/app/node_modules/@grpc/grpc-js/build/src/call.js:31:19) at Object.onReceiveStatus (/app/node_modules/@grpc/grpc-js/build/src/client.js:360:49) at Object.onReceiveStatus (/app/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:328:181) at /app/node_modules/@grpc/grpc-js/build/src/call-stream.js:188:78 at processTicksAndRejections (node:internal/process/task_queues:78:11)
How to make it work?
I appreciate any help!
As mentioned by @ImBIOS in the similar thread, you have to escape \n
in the private Key by Importing private key from .env
using process.env.PRIVATE_KEY?.replace(/\\n/gm, "\n")
.