Skip to content
This repository was archived by the owner on Nov 20, 2025. It is now read-only.

Commit a4f9f9c

Browse files
authored
fix: Universe Domain Resolution (#1745)
* fix: Universe Domain Resolution * fix: `source_credentials` from JSON * test: Add ADC universe domain test
1 parent 7282af8 commit a4f9f9c

File tree

5 files changed

+45
-23
lines changed

5 files changed

+45
-23
lines changed

src/auth/googleauth.ts

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,6 @@ export class GoogleAuth<T extends AuthClient = JSONClient> {
185185
private scopes?: string | string[];
186186
private clientOptions: AuthClientOptions = {};
187187

188-
/**
189-
* The cached universe domain.
190-
*
191-
* @see {@link GoogleAuth.getUniverseDomain}
192-
*/
193-
#universeDomain?: string = undefined;
194-
195188
/**
196189
* Export DefaultTransporter as a static property of the class.
197190
*/
@@ -220,7 +213,6 @@ export class GoogleAuth<T extends AuthClient = JSONClient> {
220213

221214
if (opts.universeDomain) {
222215
this.clientOptions.universeDomain = opts.universeDomain;
223-
this.#universeDomain = opts.universeDomain;
224216
}
225217
}
226218

@@ -315,9 +307,13 @@ export class GoogleAuth<T extends AuthClient = JSONClient> {
315307
return this._findProjectIdPromise;
316308
}
317309

318-
async #getUniverseFromMetadataServer() {
319-
if (!(await this._checkIsGCE())) return;
320-
310+
/**
311+
* Retrieves a universe domain from the metadata server via
312+
* {@link gcpMetadata.universe}.
313+
*
314+
* @returns a universe domain
315+
*/
316+
async getUniverseDomainFromMetadataServer(): Promise<string> {
321317
let universeDomain: string;
322318

323319
try {
@@ -338,17 +334,18 @@ export class GoogleAuth<T extends AuthClient = JSONClient> {
338334
* Retrieves, caches, and returns the universe domain in the following order
339335
* of precedence:
340336
* - The universe domain in {@link GoogleAuth.clientOptions}
341-
* - {@link gcpMetadata.universe}
337+
* - An existing or ADC {@link AuthClient}'s universe domain
338+
* - {@link gcpMetadata.universe}, if {@link Compute} client
342339
*
343340
* @returns The universe domain
344341
*/
345342
async getUniverseDomain(): Promise<string> {
346-
this.#universeDomain ??= originalOrCamelOptions(this.clientOptions).get(
343+
let universeDomain = originalOrCamelOptions(this.clientOptions).get(
347344
'universe_domain'
348345
);
349-
this.#universeDomain ??= await this.#getUniverseFromMetadataServer();
346+
universeDomain ??= (await this.getClient()).universeDomain;
350347

351-
return this.#universeDomain || DEFAULT_UNIVERSE;
348+
return universeDomain;
352349
}
353350

354351
/**
@@ -438,7 +435,8 @@ export class GoogleAuth<T extends AuthClient = JSONClient> {
438435
if (await this._checkIsGCE()) {
439436
// set universe domain for Compute client
440437
if (!originalOrCamelOptions(options).get('universe_domain')) {
441-
options.universeDomain = await this.getUniverseDomain();
438+
options.universeDomain =
439+
await this.getUniverseDomainFromMetadataServer();
442440
}
443441

444442
(options as ComputeOptions).scopes = this.getAnyScopes();
@@ -622,11 +620,8 @@ export class GoogleAuth<T extends AuthClient = JSONClient> {
622620
}
623621

624622
// Create source client for impersonation
625-
const sourceClient = new UserRefreshClient(
626-
json.source_credentials.client_id,
627-
json.source_credentials.client_secret,
628-
json.source_credentials.refresh_token
629-
);
623+
const sourceClient = new UserRefreshClient();
624+
sourceClient.fromJSON(json.source_credentials);
630625

631626
if (json.service_account_impersonation_url?.length > 256) {
632627
/**
@@ -652,6 +647,7 @@ export class GoogleAuth<T extends AuthClient = JSONClient> {
652647
const targetScopes = this.getAnyScopes() ?? [];
653648

654649
const client = new Impersonated({
650+
...json,
655651
delegates: json.delegates ?? [],
656652
sourceClient: sourceClient,
657653
targetPrincipal: targetPrincipal,
@@ -672,6 +668,10 @@ export class GoogleAuth<T extends AuthClient = JSONClient> {
672668
): JSONClient {
673669
let client: JSONClient;
674670

671+
// user's preferred universe domain
672+
const preferredUniverseDomain =
673+
originalOrCamelOptions(options).get('universe_domain');
674+
675675
if (json.type === USER_REFRESH_ACCOUNT_TYPE) {
676676
client = new UserRefreshClient(options);
677677
client.fromJSON(json);
@@ -694,6 +694,11 @@ export class GoogleAuth<T extends AuthClient = JSONClient> {
694694
this.setGapicJWTValues(client);
695695
client.fromJSON(json);
696696
}
697+
698+
if (preferredUniverseDomain) {
699+
client.universeDomain = preferredUniverseDomain;
700+
}
701+
697702
return client;
698703
}
699704

src/auth/jwtclient.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ export class JWT extends OAuth2Client implements IdTokenProvider {
321321
this.keyId = json.private_key_id;
322322
this.projectId = json.project_id;
323323
this.quotaProjectId = json.quota_project_id;
324+
this.universeDomain = json.universe_domain || this.universeDomain;
324325
}
325326

326327
/**

src/auth/refreshclient.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ export class UserRefreshClient extends OAuth2Client {
114114
this._refreshToken = json.refresh_token;
115115
this.credentials.refresh_token = json.refresh_token;
116116
this.quotaProjectId = json.quota_project_id;
117+
this.universeDomain = json.universe_domain || this.universeDomain;
117118
}
118119

119120
/**

test/fixtures/private2.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
"client_email": "goodbye@youarecool.com",
55
"client_id": "client456",
66
"type": "service_account",
7-
"project_id": "my-awesome-project"
8-
}
7+
"project_id": "my-awesome-project",
8+
"universe_domain": "my-universe"
9+
}

test/test.googleauth.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,6 +1568,20 @@ describe('googleauth', () => {
15681568
assert.equal(await auth.getUniverseDomain(), universeDomain);
15691569
});
15701570

1571+
it('should get the universe from ADC', async () => {
1572+
mockEnvVar(
1573+
'GOOGLE_APPLICATION_CREDENTIALS',
1574+
'./test/fixtures/private2.json'
1575+
);
1576+
const {universe_domain} = JSON.parse(
1577+
fs.readFileSync('./test/fixtures/private2.json', 'utf-8')
1578+
);
1579+
1580+
assert(universe_domain);
1581+
assert.notEqual(universe_domain, DEFAULT_UNIVERSE);
1582+
assert.equal(await auth.getUniverseDomain(), universe_domain);
1583+
});
1584+
15711585
it('should use the metadata service if on GCP', async () => {
15721586
const universeDomain = 'my.universe.com';
15731587
const scope = nockIsGCE({universeDomain});

0 commit comments

Comments
 (0)