Skip to content

Commit e886df8

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

File tree

9 files changed

+20
-12
lines changed

9 files changed

+20
-12
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: 8 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,13 @@ 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 (this.app.options.credential instanceof ApplicationDefaultCredential){
1082+
quotaProjectId = this.app.options.credential.getQuotaProjectId();
1083+
}
1084+
quotaProjectId = process.env.GOOGLE_CLOUD_QUOTA_PROJECT || undefined;
1085+
if (!requestCopy.headers['x-goog-user-project'] && validator.isNonEmptyString(quotaProjectId)) {
1086+
requestCopy.headers['x-goog-user-project'] = quotaProjectId;
10831087
}
10841088

10851089
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,

test/unit/app-check/app-check-api-client-internal.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ describe('AppCheckApiClient', () => {
4545
const EXPECTED_HEADERS = {
4646
'Authorization': 'Bearer mock-token',
4747
'X-Firebase-Client': `fire-admin-node/${getSdkVersion()}`,
48-
'x-goog-user-project': 'test-project',
4948
};
5049

5150
const noProjectId = 'Failed to determine project ID. Initialize the SDK with service '

test/unit/data-connect/data-connect-api-client-internal.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ describe('DataConnectApiClient', () => {
4343
const EXPECTED_HEADERS = {
4444
'Authorization': 'Bearer mock-token',
4545
'X-Firebase-Client': `fire-admin-node/${getSdkVersion()}`,
46-
'x-goog-user-project': 'test-project',
4746
};
4847

4948
const noProjectId = 'Failed to determine project ID. Initialize the SDK with service '

test/unit/functions/functions-api-client-internal.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ describe('FunctionsApiClient', () => {
4646
const EXPECTED_HEADERS = {
4747
'X-Firebase-Client': `fire-admin-node/${getSdkVersion()}`,
4848
'Authorization': 'Bearer mock-token',
49-
'x-goog-user-project': 'test-project',
5049
};
5150

5251
const noProjectId = 'Failed to determine project ID. Initialize the SDK with service '

test/unit/machine-learning/machine-learning-api-client.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ describe('MachineLearningApiClient', () => {
117117
const EXPECTED_HEADERS = {
118118
'Authorization': 'Bearer mock-token',
119119
'X-Firebase-Client': `fire-admin-node/${getSdkVersion()}`,
120-
'x-goog-user-project': 'test-project',
121120
};
122121
const noProjectId = 'Failed to determine project ID. Initialize the SDK with service '
123122
+ 'account credentials, or set project ID as an app option. Alternatively, set the '

test/unit/remote-config/remote-config-api-client.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ describe('RemoteConfigApiClient', () => {
5757
'Authorization': 'Bearer mock-token',
5858
'X-Firebase-Client': `fire-admin-node/${getSdkVersion()}`,
5959
'Accept-Encoding': 'gzip',
60-
'x-goog-user-project': 'test-project',
6160
};
6261

6362
const VERSION_INFO: Version = {

test/unit/security-rules/security-rules-api-client.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ describe('SecurityRulesApiClient', () => {
4444
const EXPECTED_HEADERS = {
4545
'Authorization': 'Bearer mock-token',
4646
'X-Firebase-Client': `fire-admin-node/${getSdkVersion()}`,
47-
'x-goog-user-project': 'test-project',
4847
};
4948
const noProjectId = 'Failed to determine project ID. Initialize the SDK with service '
5049
+ 'account credentials, or set project ID as an app option. Alternatively, set the '

0 commit comments

Comments
 (0)