Skip to content

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

Open
@lahirumaramba

Description

@lahirumaramba

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 🦕

Metadata

Metadata

Assignees

Labels

api: firestoreIssues related to the googleapis/nodejs-firestore API.priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions