Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to connect to Firestore Emulator with preferRest: true #1811

Open
lahirumaramba opened this issue Jan 17, 2023 · 1 comment
Open

Unable to connect to Firestore Emulator with preferRest: true #1811

lahirumaramba opened this issue Jan 17, 2023 · 1 comment
Assignees
Labels
api: firestore Issues related to the googleapis/nodejs-firestore API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@lahirumaramba
Copy link

lahirumaramba commented Jan 17, 2023

When REST fallback is enabled by setting preferRest: true, the fallback client obtains an authenticated google-auth-library client to communicate with the Firestore BE services.

If the emulator mode is enabled by setting FIRESTORE_EMULATOR_HOST env var, the rest client will look for application default credentials in the environment to initialize credentials to obtain an authenticated google-auth-library client. This step fails if there are no credentials set in the environment. Firestore Client should not use service account credentials to connect to the emulator even if the real service credentials are available, instead we should explicitly set emulator credentials on emulator mode.

We already set emulator credentials on gRPC mode by setting custom headers at:

Authorization: 'Bearer owner',
but for some reason this is not effective on REST fallback.

Steps to reproduce:

  • Setexport FIRESTORE_EMULATOR_HOST="localhost:8080"
  • Remove any application default credentials from your environment:
    • unset GOOGLE_APPLICATION_CREDENTIALS
    • gcloud auth application-default revoke
import { Firestore } from '@google-cloud/firestore'

// Create a new client
const firestore = new Firestore({ preferRest: true })

async function quickstart() {
  // Obtain a document reference.
  const document = firestore.doc('posts/intro-to-firestore')

  // Enter new data into the document.
  await document.set({
    title: 'Welcome to Firestore',
    body: 'Hello World',
  })
  console.log('Entered new data into the document')
}
quickstart()

Expected:

  • A new entry should be added to the Firestore Emulator

Actual:

  • Error log:
(node:62230) UnhandledPromiseRejectionWarning: Error: Could not load the default credentials. Browse to 
https://cloud.google.com/docs/authentication/getting-started for more information.
    at GoogleAuth.getApplicationDefaultAsync (nodejs-firestore/node_modules/google-auth-
library/build/src/auth/googleauth.js:206:19)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async GoogleAuth.getClient (nodejs-firestore/node_modules/google-auth-library/build/src/auth/googleauth.js:638:17)
    at async GrpcClient.createStub (nodejs-firestore/node_modules/google-gax/build/src/fallback.js:211:35)
Caused by: Error
    at WriteBatch.commit (nodejs-firestore/node_modules/@google-cloud/firestore/build/src/write-batch.js:433:23)
    at DocumentReference.set (nodejs-firestore/node_modules/@google-cloud/firestore/build/src/reference.js:392:27)
    at quickstart (file://nodejs-firestore/node-js-firestore-repro.js:11:18)
    at file://nodejs-firestore/node-js-firestore-repro.js:17:1
    at ModuleJob.run (internal/modules/esm/module_job.js:152:23)
    at async Loader.import (internal/modules/esm/loader.js:166:24)
    at async Object.loadESM (internal/process/esm_loader.js:68:5)

Related to: firebase/firebase-admin-node#2016 🦕

@lahirumaramba lahirumaramba added priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels Jan 17, 2023
@product-auto-label product-auto-label bot added the api: firestore Issues related to the googleapis/nodejs-firestore API. label Jan 17, 2023
@MarkDuckworth
Copy link
Contributor

Requested a feature in google-gax to support the solution for this: googleapis/gax-nodejs#1409

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: firestore Issues related to the googleapis/nodejs-firestore API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants