googleapis / googleapis/google-cloud-node
Unable to connect to Firestore Emulator with preferRest: true
- Dominant language
- TypeScript
- Stars
- 3.2k
- Forks
- 712
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 99
Description
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: https://github.com/googleapis/nodejs-firestore/blob/653a8e996b6e6be01825f030e81d0379b4318c8a/dev/src/index.ts#L1453 but for some reason this is not effective on REST fallback.
### Steps to reproduce:
- Set`export FIRESTORE_EMULATOR_HOST="localhost:8080"`
- Remove any application default credentials from your environment:
- `unset GOOGLE_APPLICATION_CREDENTIALS`
- `gcloud auth application-default revoke`
```js
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: https://github.com/firebase/firebase-admin-node/issues/2016 🦕
Contributor guide
Assessment
This issue has not been assessed yet.