Escaping issue with firebase privateKey as a Heroku config variable
Asked Answered
E

3

37

I'm trying to create an Heroku node task that reads data from Firebase and console.log it.

My node script (located inside the /bin directory) is:

require('dotenv').config({ silent: true });

var firebase = require('firebase');
firebase.initializeApp({
  serviceAccount: {
    projectId: process.env.fb_projectId,
    clientEmail: process.env.fb_clientEmail,
    privateKey: process.env.fb_privateKey
  },
  databaseURL: process.env.fb_databaseURL
})

console.log(process.env.fb_privateKey);

firebase.database().ref('tasks').once('value', function(snapshot) {
  console.log(snapshot.val());
  process.exit();
})

All env variables are correct, except fb_privateKey, since it contains \n characters.

Both my local .env file and Heroku environment variables contains a key named fb_privateKey and a value as a string with \n characters - surrounded with ".

fb_privateKey="-----BEGIN PRIVATE KEY-----\nMY-PRIVATE-KEY\n-----END PRIVATE KEY-----\n"

When I run the script locally, it logs me the private key with new lines + the tasks that were returned from Firebase. However, when I run it on Heroku, (heroku run myScript) is logs me private key with \n characters (as a single line) and fails to read the data from Firebase (probably due to a bad private key). Any ideas?

Exactly answered 14/9, 2016 at 14:9 Comment(1)
You should mark Erem's answer as correct.Gawain
K
107

I had the same problem today. You need to sanitize the read private key by replacing \\n characters with \n.

admin.initializeApp({
  credential: admin.credential.cert({
    "projectId": process.env.FIREBASE_PROJECT_ID,
    "private_key": process.env.FIREBASE_PRIVATE_KEY.replace(/\\n/g, '\n'),
    "clientEmail": process.env.FIREBASE_CLIENT_EMAIL,
  }),
  databaseURL: process.env.FIREBASE_DATABASE_URL,
});
Kemberlykemble answered 8/12, 2016 at 16:47 Comment(4)
This solution worked for me to make the Firebase private key work in Vercel.Cowes
Worked for me w/ private key in Netlify env vars :)Oviparous
This solution worked for me when deploying NextJs app to AWS Amplify.Rocher
Works on AutocodeVesicate
N
4

I solved this by using the .replace(/\n/g, '\n') on the private key and also removing the quotes from the private key value on heroku config vars like it is bellow.

-----BEGIN PRIVATE KEY-----\nMY-PRIVATE-KEY\n-----END PRIVATE KEY-----\n

Noakes answered 30/4, 2020 at 10:20 Comment(0)
L
2

In my case I solved it by converting FIREBASE_PRIVATE_KEY to a base64 string, then I saved this new string as an environment variable in Heroku. In my code, I converted the base64 FIREBASE_PRIVATE_KEY to the original private key again.

const firebase_private_key_b64 = Buffer.from(process.env.FIREBASE_PRIVATE_KEY_BASE64, 'base64');
const firebase_private_key = firebase_private_key_b64.toString('utf8');

admin.initializeApp({
    credential: admin.credential.cert({
        "project_id": process.env.FIREBASE_PROJECT_ID,
        "private_key": firebase_private_key,
        "client_email": process.env.FIREBASE_CLIENT_EMAIL,
    }),
    databaseURL: process.env.FIREBASE_DATABASE_URL
});
Lute answered 30/6, 2020 at 2:10 Comment(1)
Best solution, well explained. Works with Netlify.Moonfaced

© 2022 - 2024 — McMap. All rights reserved.