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

feat: set x-goog-user-project header, with quota_project from default credentials #829

Merged
merged 3 commits into from
Nov 20, 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"eslint-plugin-node": "^10.0.0",
"eslint-plugin-prettier": "^3.0.0",
"execa": "^3.0.0",
"gts": "^1.0.0",
"gts": "^1.1.2",
"is-docker": "^2.0.0",
"js-green-licenses": "^1.0.0",
"karma": "^4.0.0",
Expand Down
1 change: 1 addition & 0 deletions src/auth/credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface JWTInput {
client_id?: string;
client_secret?: string;
refresh_token?: string;
quota_project?: string;
bcoe marked this conversation as resolved.
Show resolved Hide resolved
}

export interface CredentialBody {
Expand Down
16 changes: 15 additions & 1 deletion src/auth/googleauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,21 @@ export class GoogleAuth {
*/
async getRequestHeaders(url?: string) {
const client = await this.getClient();
return client.getRequestHeaders(url);
let headers = client.getRequestHeaders(url);
// quota_project, stored in application_default_credentials.json, is set in
// the x-goog-user-project header, to indicate an alternate account for
// billing and quota:
if (this.jsonContent?.quota_project) {
// If x-goog-user-project has explicitly been set in the client headers,
// it takes precedence.
headers = Object.assign(
{
'x-goog-user-project': this.jsonContent.quota_project,
},
headers
);
}
return headers;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"client_id": "764086051850-6qr4p6gpi6hn506pt8ejuq83di341hur.apps.googleusercontent.com",
"client_secret": "privatekey",
"refresh_token": "refreshtoken",
"type": "authorized_user",
"project_id": "my-project",
"quota_project": "my-quota-project"
}
38 changes: 38 additions & 0 deletions test/test.googleauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1444,4 +1444,42 @@ describe('googleauth', () => {
/Passing options to getClient is forbidden in v5.0.0/
);
});

it('getRequestHeaders() populates x-goog-user-project with quota_project if present', async () => {
// Fake a home directory in our fixtures path.
mockEnvVar('GCLOUD_PROJECT', 'my-fake-project');
mockEnvVar('HOME', './test/fixtures');
mockEnvVar('APPDATA', './test/fixtures/.config');
// The first time auth.getClient() is called /token endpoint is used to
// fetch a JWT.
const req = nock('https://oauth2.googleapis.com')
.post('/token')
.reply(200, {});

const auth = new GoogleAuth();
const headers = await auth.getRequestHeaders();
assert.strictEqual(headers['x-goog-user-project'], 'my-quota-project');
req.done();
});

it('getRequestHeaders() does not populate x-goog-user-project if quota_project is not present', async () => {
// Fake a home directory in our fixtures path.
mockEnvVar('GCLOUD_PROJECT', 'my-fake-project');
mockEnvVar('HOME', './test/fixtures');
mockEnvVar('APPDATA', './test/fixtures/.config');
// The first time auth.getClient() is called /token endpoint is used to
// fetch a JWT.
const req = nock('https://oauth2.googleapis.com')
.post('/token')
.reply(200, {});

const auth = new GoogleAuth();
// Force jsonContent to load, and then remove the quota_project parameter.
await auth.getClient();
delete auth.jsonContent!.quota_project;

const headers = await auth.getRequestHeaders();
assert.strictEqual(headers['x-goog-user-project'], undefined);
req.done();
});
});