0

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:

  1. The warning message, which I presume it is related to the firebase admin private key;
  2. How to properly insert the private key in the environment variable box.

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!

Rob
  • 14,746
  • 28
  • 47
  • 65
Aliton Oliveira
  • 1,224
  • 1
  • 15
  • 26
  • Can you show how you are specifying `Private Key` in `PEM file`(-----Begin / End lines) by editing your question? – Roopa M Feb 10 '23 at 07:31
  • I used the example in this link https://park.is/blog_posts/20210118_add_a_multiline_env_variable_to_vercel/. I also edited the question to show how the private key was copied into the environment variable. – Aliton Oliveira Feb 10 '23 at 16:30
  • 1
    Can you try importing private key from `.env` using `process.env.PRIVATE_KEY?.replace(/\\n/gm, "\n")` as discussed [here](https://stackoverflow.com/a/74858179/18265570). Also check this [thread](https://stackoverflow.com/a/52046655/18265570) – Roopa M Feb 12 '23 at 06:24
  • It worked and All the errors are gone! – Aliton Oliveira Feb 12 '23 at 14:24

1 Answers1

1

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").

Roopa M
  • 2,171
  • 4
  • 12