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: use key file when fetching project id #618

Merged
merged 6 commits into from
Feb 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/auth/googleauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,15 @@ export class GoogleAuth {
return this.cachedCredential.projectId;
}

// Ensure the projectId is loaded from the keyFile if available.
if (this.keyFilename) {
const creds = await this.getClient();

if (creds && creds.projectId) {
return creds.projectId;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@JustinBeckwith made a small amendment to your commit, let me know if you hate it!

}
}

// Try to load a credentials file and read its project ID
const r = await this._tryGetApplicationCredentialsFromEnvironmentVariable();
if (r) {
Expand Down
17 changes: 17 additions & 0 deletions test/test.googleauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,23 @@ describe('googleauth', () => {
assert.strictEqual(projectId, fixedProjectId);
});

it('getProjectId should use `keyFilename` when it is available', async () => {
const envVars = Object.assign({}, process.env, {
GCLOUD_PROJECT: undefined,
gcloud_project: undefined,
google_cloud_project: undefined,
GOOGLE_CLOUD_PROJECT: undefined,
});

sandbox.stub(process, 'env').value(envVars);

const keyFilename =
path.join(__dirname, '../../test/fixtures/private2.json');
const auth = new GoogleAuth({keyFilename});
const projectId = await auth.getProjectId();
assert.strictEqual(projectId, fixedProjectId);
});

it('getProjectId should use GOOGLE_APPLICATION_CREDENTIALS file when it is available',
async () => {
const envVars = Object.assign({}, process.env, {
Expand Down