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

fix: Set emulator credentials on rest fallback #1812

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions dev/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,25 @@ export class Firestore implements firestore.Firestore {
// also set the `protocol` option for GAX fallback to force http
if (useFallback) {
settings.protocol = 'http';

// If the emulator mode is enabled, we set emulator credentials
// to prevent the gRPC-fallback client from trying to obtain
// service account credentials from the environment.
if (process.env.FIRESTORE_EMULATOR_HOST) {
const emulatorCredentials = {
access_token: 'owner',
};
const emulatorClient =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Root cause for failing tests. The emulatorClient (cached credentials) needs to specify the project ID. If not specified here and not specified in the firestore settings, then the project ID will not be available, causing request the Firestore instance to behave incorrectly.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see! How does the system tests obtain the project id? We can use the firestore settings and GCLOUD_PROJECT env var here, if that makes sense.

 emulatorClient.projectId = settings.projectId ?? process.env.GCLOUD_PROJECT;

WDYT?

// eslint-disable-next-line node/no-extraneous-require
new (require('google-auth-library').OAuth2Client)();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since google-auth-library is not a direct dependency (it is a transient dependency of google-gax) We get extraneous-require lint error here. I think disabling it for this line is okay... WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we added 'google-auth-library' to nodejs-firestore dependencies, would that also resolve the issue? Or am I missing some other context?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, adding google-auth-library to package.json would resolve this, but I am not sure if that is necessary here as google-gax should pull in google-auth-library as a dependency anyway.

Reference: https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/no-extraneous-require.md

If a require()'s target is extraneous (it's not written in package.json), the program works in local, but will not work after dependencies are re-installed. It will cause troubles to your team/contributors. This rule disallows require() of extraneous modules.

emulatorClient.setCredentials(emulatorCredentials);
emulatorClient.projectId =
settings.projectId ?? process.env.GCLOUD_PROJECT;
const emulatorAuth = new (require('google-gax').GoogleAuth)({
authClient: emulatorClient,
});
settings.auth = emulatorAuth;
}
}

client = new module.exports.v1(settings, gax);
Expand Down