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

Conversation

lahirumaramba
Copy link

Setting Emulator credentials when REST fallback is enabled by setting preferRest: true.

I am not familiar with the codebase and looking for review from the experts :)
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.

Test coverage:
Tried to add unit tests with sinon.spy on modules.exports.v1 and FirestoreClient, but couldn't get it to work. Appreciate any thoughts on adding test coverage to this.

Fixes #1811 🦕

@lahirumaramba lahirumaramba requested review from a team as code owners January 17, 2023 15:12
@product-auto-label product-auto-label bot added size: s Pull request size is small. api: firestore Issues related to the googleapis/nodejs-firestore API. labels Jan 17, 2023
};
const emulatorClient =
// 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.

Copy link
Contributor

@MarkDuckworth MarkDuckworth left a comment

Choose a reason for hiding this comment

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

LGTM. I am curious about the 'google-auth-library' require.

};
const emulatorClient =
// eslint-disable-next-line node/no-extraneous-require
new (require('google-auth-library').OAuth2Client)();
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
Contributor

@MarkDuckworth MarkDuckworth left a comment

Choose a reason for hiding this comment

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

I updated the system tests to run against the emulator. Unfortunately, they are failing with this change, but passing without this change. Need to rescind the approval until we can understand the issue.

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?

@MarkDuckworth
Copy link
Contributor

I'm closing this PR for now to clean things up. We're waiting on a change in the gax-fallback, so that there will be consistent implementation of credentials used for the emulator regardless of using rest or grpc. Thanks for creating this @lahirumaramba, we can re-open it later if the other approach fails.

@lahirumaramba
Copy link
Author

Makes sense! Thanks @MarkDuckworth !

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. size: s Pull request size is small.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to connect to Firestore Emulator with preferRest: true
2 participants