Skip to content

Commit

Permalink
fix: add option to disable emulator auth handling (temp fix) (googlea…
Browse files Browse the repository at this point in the history
…pis#1861)

* fix: add option to disable emulator auth handling (temp fix)

* fix: update names to be more palatable(?)

* docs: fix incorrect comment
  • Loading branch information
feywind committed Feb 8, 2024
1 parent dbd1b2c commit 761cdc8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/pubsub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@ export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;

export interface ClientConfig extends gax.GrpcClientOptions {
apiEndpoint?: string;

/**
* Configures the emulator mode behaviour:
* - If false, disable emulator mode always
* - If true, enable emulator mode always
* - If unset, use heuristics to decide
* Emulator mode notably sets insecure SSL authentication so that you can
* try the library out without needing a cert.
*
* Also notably, if a TPC universeDomain is set, then this will be counted
* as !emulatorMode for the purposes of the heuristics. If you want emulator
* mode but with a TPC universe domain set, set this to true as well.
*/
emulatorMode?: boolean;

servicePath?: string;
port?: string | number;
sslCreds?: gax.grpc.ChannelCredentials;
Expand Down Expand Up @@ -798,9 +813,16 @@ export class PubSub {
// If this looks like a GCP URL of some kind, don't go into emulator
// mode. Otherwise, supply a fake SSL provider so a real cert isn't
// required for running the emulator.
//
// Note that users can provide their own URL here, especially with
// TPC, so the emulatorMode flag lets them override this behaviour.
const officialUrlMatch =
this.options.servicePath!.endsWith('.googleapis.com');
if (!officialUrlMatch) {
this.options.servicePath!.endsWith('.googleapis.com') ||
this.options.universeDomain;
if (
(!officialUrlMatch && this.options.emulatorMode !== false) ||
this.options.emulatorMode === true
) {
const grpcInstance = this.options.grpc || gax.grpc;
this.options.sslCreds = grpcInstance.credentials.createInsecure();
this.isEmulator = true;
Expand Down
24 changes: 24 additions & 0 deletions test/pubsub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,30 @@ describe('PubSub', () => {
assert.strictEqual(pubsub.isEmulator, true);
});

it('should allow overriding fake cred mode (on)', () => {
pubsub!.options!.apiEndpoint = 'something.googleapis.com';
pubsub!.options!.emulatorMode = true;
pubsub.determineBaseUrl_?.();

assert.strictEqual(pubsub.options!.sslCreds, fakeCreds);
assert.strictEqual(pubsub.isEmulator, true);
});

it('should allow overriding fake cred mode (off)', () => {
const defaultBaseUrl_ = 'defaulturl';
const testingUrl = 'localhost:8085';

setHost(defaultBaseUrl_);
pubsub!.options!.apiEndpoint = testingUrl;
pubsub!.options!.emulatorMode = false;
pubsub.determineBaseUrl_?.();

assert.strictEqual(pubsub.options?.servicePath, 'localhost');
assert.strictEqual(pubsub.options.port, 8085);
assert.ok(pubsub.options.sslCreds === undefined);
assert.strictEqual(pubsub.isEmulator, false);
});

it('should remove slashes from the baseUrl', () => {
setHost('localhost:8080/');
pubsub.determineBaseUrl_?.();
Expand Down

0 comments on commit 761cdc8

Please sign in to comment.