Skip to content

Commit

Permalink
feat: Improve Universe Domain Ergonomics (#1732)
Browse files Browse the repository at this point in the history
  • Loading branch information
d-goog authored Jan 25, 2024
1 parent 058a503 commit eec82f5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
17 changes: 15 additions & 2 deletions src/auth/googleauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ export interface GoogleAuthOptions<T extends AuthClient = JSONClient> {
* Your project ID.
*/
projectId?: string;

/**
* The default service domain for a given Cloud universe.
*
* This is an ergonomic equivalent to {@link clientOptions}'s `universeDomain`
* property and will be set for all generated {@link AuthClient}s.
*/
universeDomain?: string;
}

export const CLOUD_SDK_CLIENT_ID =
Expand Down Expand Up @@ -175,7 +183,7 @@ export class GoogleAuth<T extends AuthClient = JSONClient> {
defaultScopes?: string | string[];
private keyFilename?: string;
private scopes?: string | string[];
private clientOptions?: AuthClientOptions;
private clientOptions: AuthClientOptions = {};

/**
* The cached universe domain.
Expand Down Expand Up @@ -208,7 +216,12 @@ export class GoogleAuth<T extends AuthClient = JSONClient> {
this.keyFilename = opts.keyFilename || opts.keyFile;
this.scopes = opts.scopes;
this.jsonContent = opts.credentials || null;
this.clientOptions = opts.clientOptions;
this.clientOptions = opts.clientOptions || {};

if (opts.universeDomain) {
this.clientOptions.universeDomain = opts.universeDomain;
this.#universeDomain = opts.universeDomain;
}
}

// GAPIC client libraries should always use self-signed JWTs. The following
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {GoogleAuth} from './auth/googleauth';

export * as gcpMetadata from 'gcp-metadata';

export {AuthClient} from './auth/authclient';
export {AuthClient, DEFAULT_UNIVERSE} from './auth/authclient';
export {Compute, ComputeOptions} from './auth/computeclient';
export {
CredentialBody,
Expand Down
7 changes: 7 additions & 0 deletions test/test.googleauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1551,6 +1551,13 @@ describe('googleauth', () => {
});

describe('getUniverseDomain', () => {
it('should prefer `universeDomain` > metadata service when available', async () => {
const universeDomain = 'my.universe.com';
const auth = new GoogleAuth({universeDomain});

assert.equal(await auth.getUniverseDomain(), universeDomain);
});

it('should prefer `clientOptions` > metadata service when available', async () => {
const universeDomain = 'my.universe.com';
const auth = new GoogleAuth({clientOptions: {universeDomain}});
Expand Down

0 comments on commit eec82f5

Please sign in to comment.