Skip to content

Commit 498e95c

Browse files
committed
fix: Set quota project ID for ADC human accounts
1 parent 5affb73 commit 498e95c

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

src/app/credential-internal.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export class ApplicationDefaultCredential implements Credential {
3939
private readonly googleAuth: GoogleAuth;
4040
private authClient: AnyAuthClient;
4141
private projectId?: string;
42+
private quotaProjectId?: string;
4243
private accountId?: string;
4344

4445
constructor(httpAgent?: Agent) {
@@ -58,6 +59,7 @@ export class ApplicationDefaultCredential implements Credential {
5859
}
5960
await this.authClient.getAccessToken();
6061
const credentials = this.authClient.credentials;
62+
this.quotaProjectId = this.authClient.quotaProjectId;
6163
return populateCredential(credentials);
6264
}
6365

@@ -68,6 +70,13 @@ export class ApplicationDefaultCredential implements Credential {
6870
return Promise.resolve(this.projectId);
6971
}
7072

73+
public getQuotaProjectId(): string | undefined {
74+
if (!this.quotaProjectId) {
75+
this.quotaProjectId = this.authClient.quotaProjectId;
76+
}
77+
return this.quotaProjectId;
78+
}
79+
7180
public async isComputeEngineCredential(): Promise<boolean> {
7281
if (!this.authClient) {
7382
this.authClient = await this.googleAuth.getClient();

src/utils/api-request.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import url = require('url');
2626
import { EventEmitter } from 'events';
2727
import { Readable } from 'stream';
2828
import * as zlibmod from 'zlib';
29+
import { ApplicationDefaultCredential } from '../app/credential-internal';
2930

3031
/** Http method type definition. */
3132
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD';
@@ -1076,10 +1077,16 @@ export class AuthorizedHttpClient extends HttpClient {
10761077
const authHeader = 'Authorization';
10771078
requestCopy.headers[authHeader] = `Bearer ${token}`;
10781079

1079-
// Fix issue where firebase-admin does not specify quota project that is
1080-
// necessary for use when utilizing human account with ADC (RSDF)
1081-
if (!requestCopy.headers['x-goog-user-project'] && this.app.options.projectId) {
1082-
requestCopy.headers['x-goog-user-project'] = this.app.options.projectId
1080+
let quotaProjectId: string | undefined;
1081+
if (process.env.GOOGLE_CLOUD_QUOTA_PROJECT) {
1082+
quotaProjectId = process.env.GOOGLE_CLOUD_QUOTA_PROJECT;
1083+
}
1084+
else if (this.app.options.credential instanceof ApplicationDefaultCredential){
1085+
quotaProjectId = this.app.options.credential.getQuotaProjectId();
1086+
}
1087+
1088+
if (!requestCopy.headers['x-goog-user-project'] && validator.isNonEmptyString(quotaProjectId)) {
1089+
requestCopy.headers['x-goog-user-project'] = quotaProjectId;
10831090
}
10841091

10851092
if (!requestCopy.httpAgent && this.app.options.httpAgent) {

test/integration/setup.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import minimist = require('minimist');
1919
import path = require('path');
2020
import { random } from 'lodash';
2121
import {
22-
App, Credential, GoogleOAuthAccessToken, cert, deleteApp, initializeApp,
22+
App, Credential, GoogleOAuthAccessToken, applicationDefault, cert, deleteApp, initializeApp,
2323
} from '../../lib/app/index'
2424

2525
// eslint-disable-next-line @typescript-eslint/no-var-requires
@@ -87,7 +87,8 @@ before(() => {
8787
storageBucket = projectId + '.appspot.com';
8888

8989
defaultApp = initializeApp({
90-
...getCredential(),
90+
//...getCredential(),
91+
credential: applicationDefault(),
9192
projectId,
9293
databaseURL: databaseUrl,
9394
storageBucket,

0 commit comments

Comments
 (0)