0

Rather than passing in an actual JSON, I'm retrieving keyvalues from .env and manually setting them similar to this. When testing locally, everything works fine. In production w/ Elastic Beanstalk, initialization fails:

Dec 29 01:23:41 ip-000-00-00-000 web: /var/app/current/node_modules/firebase-admin/lib/app/credential-internal.js:143

Dec 29 01:23:41 ip-000-00-00-000 web: throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, 'Failed to parse private key: ' + error);

Dec 29 01:23:41 ip-000-00-00-000 web: FirebaseAppError: Failed to parse private key: Error: Only 8, 16, 24, or 32 bits supported: 528

Dec 29 01:23:41 ip-000-00-00-000 web: at FirebaseAppError.FirebaseError [as constructor] (/var/app/current/node_modules/firebase-admin/lib/utils/error.js:44:28)

The only difference is that instead of local .env, I'm pulling environment variables from Elastic Beanstalk: enter image description here

And here are the rest:

config.js:

const dotenv = require('dotenv');
dotenv.config();

const {PORT, TYPE, PROJECT_ID, PRIVATE_KEY_ID, PRIVATE_KEY, ... } = process.env;

module.exports = {
    port: PORT,
    firebaseConfig: {
        type: TYPE,
        projectId: PROJECT_ID,
        privateKeyId: PRIVATE_KEY_ID,
        privateKey: PRIVATE_KEY.replace(/\\n/g, '\n'),
        clientEmail: CLIENT_EMAIL,
        ...
    }
}

db.js:

const config = require('./config'),
    firebase = require('firebase-admin');

const db = firebase.initializeApp({
    credential: firebase.credential.cert(config.firebaseConfig)
});

module.exports = db;

Thanks in advance!

Jin
  • 21
  • 1
  • 5

1 Answers1

1

resolved. It was a simple case of Elastic Beanstalk removing all escape characters from environment variables and making the private key a very long strinig.

Jin
  • 21
  • 1
  • 5