firebase-admin-node icon indicating copy to clipboard operation
firebase-admin-node copied to clipboard

Firebase does not set projectID property when setting up Storage object using application default credentials

Open guibirow opened this issue 4 years ago • 7 comments

[READ] Step 1: Are you in the right place?

yes

[REQUIRED] Step 2: Describe your environment

  • Operating System version: MacOs BigSur
  • Firebase SDK version: "firebase-admin”: “^9.5.0"
  • Firebase Product: storage
  • Node.js version: 14
  • NPM version: _____

[REQUIRED] Step 3: Describe the problem

When using Firebase admin sdk locally and authenticated with personal account and application default credentials, the storage api does not work properly because the project Id provided in the Firebase application Initialisation is not forwarded to the Auth library used by the storage, causing an error because the project it was not found.

Steps to reproduce:

  • login with personal account using gcloud auth login --update-adc
  • create a storage object from the app
  • try create a file in the bucket
  • an error will be thrown

Relevant Code:

const app = admin.initializeApp({
    projectId: "abc",
    storageBucket: "def",
});
const storage = app.storage();
const bucket = storage.bucket("fghij");
bucket.file("filename").save("something")

Error:
(node:2750) UnhandledPromiseRejectionWarning: Error: Unable to detect a Project Id in the current environment.

When debugging the code above, is visible that the projectId is nor set for storage.storageClient.projectId and storage.storageClient.authClient._cachedProjectId

Workaround:

Set the project Id into internal objects manually

const storage = admin.storage();

//dodgy workaround to make it work
storage.storageClient.projectId = "abc";
storage.storageClient.authClient._cachedProjectId = "abc"; //this is the required one

const bucket = storage.bucket("fghij");
bucket.file("filename").save("something")

Updated workaround

Passing the project id via environment variable GOOGLE_CLOUD_PROJECT did the trick, but ideally I would expect it to use the information I passed in the initializeApp() method

guibirow avatar Oct 15 '21 11:10 guibirow

I suspect this issue might be related to https://github.com/googleapis/google-auth-library-nodejs/issues/755

Note: The work around mentioned in the auth-library issue didn't solve the problem, the value had to be set directly in the object inside the storage object returned from firebase sdk

guibirow avatar Oct 15 '21 11:10 guibirow

Just tested the following code, and it worked fine:

const admin = require('firebase-admin');

const app = admin.initializeApp({
  projectId: "*******",
});

const storage = app.storage();
const bucket = storage.bucket("******.appspot.com");
bucket.file("filename").save("something")
  .then((result) => console.log('Success', result))
  .catch((err) => console.log('Error', err));

On my first attempt I got an ADC error:

% node main.js 
Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
    at GoogleAuth.getApplicationDefaultAsync (/Users/hkj/Projects/firebase-admin-node/v10/node_modules/google-auth-library/build/src/auth/googleauth.js:180:19)

But after running gcloud auth login --update-adc it's working without issues.

May be try setting a project ID for your gcloud tools? Something like `gcloud config set project my_project_id"?

hiranya911 avatar Oct 15 '21 18:10 hiranya911

We tried the approach you mentioned and didn't solve the problem. We end up passing the project id required by the Auth library using environment variables. I've updated my issue with the environment variable approach

guibirow avatar Oct 18 '21 14:10 guibirow

That's really strange. I'd advise you to try and repro the issue with just the GCS client as follows:

const { Storage } = require('@google-cloud/storage');

// This is how the Admin SDK initializes the GCS client when using ADC.
const storage = new Storage();

If this also doesn't work, then we should report a bug at https://github.com/googleapis/nodejs-storage

hiranya911 avatar Oct 18 '21 17:10 hiranya911

What is the status of this issue? I am having the same issue and I would like to set the project id via the projectId parameter in the initializeApp function.

AMFTech512 avatar Feb 04 '22 20:02 AMFTech512

Update

I submitted a PR to fix this issue: https://github.com/firebase/firebase-admin-node/pull/1567

AMFTech512 avatar Feb 04 '22 21:02 AMFTech512

@AMFTech512 if you are using ADC could you try to repro the issue with just the google cloud storage client (as mentioned in the comment above)

The Admin SDK uses the google-cloud storage client. If you can repro the issue, please file a bug at https://github.com/googleapis/nodejs-storage

lahirumaramba avatar Feb 10 '22 20:02 lahirumaramba

Closing due to inactivity

lahirumaramba avatar Jan 17 '23 20:01 lahirumaramba