Skip to content

Commit 761cdc8

Browse files
authored
fix: add option to disable emulator auth handling (temp fix) (#1861)
* fix: add option to disable emulator auth handling (temp fix) * fix: update names to be more palatable(?) * docs: fix incorrect comment
1 parent dbd1b2c commit 761cdc8

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

src/pubsub.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,21 @@ export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
7070

7171
export interface ClientConfig extends gax.GrpcClientOptions {
7272
apiEndpoint?: string;
73+
74+
/**
75+
* Configures the emulator mode behaviour:
76+
* - If false, disable emulator mode always
77+
* - If true, enable emulator mode always
78+
* - If unset, use heuristics to decide
79+
* Emulator mode notably sets insecure SSL authentication so that you can
80+
* try the library out without needing a cert.
81+
*
82+
* Also notably, if a TPC universeDomain is set, then this will be counted
83+
* as !emulatorMode for the purposes of the heuristics. If you want emulator
84+
* mode but with a TPC universe domain set, set this to true as well.
85+
*/
86+
emulatorMode?: boolean;
87+
7388
servicePath?: string;
7489
port?: string | number;
7590
sslCreds?: gax.grpc.ChannelCredentials;
@@ -798,9 +813,16 @@ export class PubSub {
798813
// If this looks like a GCP URL of some kind, don't go into emulator
799814
// mode. Otherwise, supply a fake SSL provider so a real cert isn't
800815
// required for running the emulator.
816+
//
817+
// Note that users can provide their own URL here, especially with
818+
// TPC, so the emulatorMode flag lets them override this behaviour.
801819
const officialUrlMatch =
802-
this.options.servicePath!.endsWith('.googleapis.com');
803-
if (!officialUrlMatch) {
820+
this.options.servicePath!.endsWith('.googleapis.com') ||
821+
this.options.universeDomain;
822+
if (
823+
(!officialUrlMatch && this.options.emulatorMode !== false) ||
824+
this.options.emulatorMode === true
825+
) {
804826
const grpcInstance = this.options.grpc || gax.grpc;
805827
this.options.sslCreds = grpcInstance.credentials.createInsecure();
806828
this.isEmulator = true;

test/pubsub.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,30 @@ describe('PubSub', () => {
857857
assert.strictEqual(pubsub.isEmulator, true);
858858
});
859859

860+
it('should allow overriding fake cred mode (on)', () => {
861+
pubsub!.options!.apiEndpoint = 'something.googleapis.com';
862+
pubsub!.options!.emulatorMode = true;
863+
pubsub.determineBaseUrl_?.();
864+
865+
assert.strictEqual(pubsub.options!.sslCreds, fakeCreds);
866+
assert.strictEqual(pubsub.isEmulator, true);
867+
});
868+
869+
it('should allow overriding fake cred mode (off)', () => {
870+
const defaultBaseUrl_ = 'defaulturl';
871+
const testingUrl = 'localhost:8085';
872+
873+
setHost(defaultBaseUrl_);
874+
pubsub!.options!.apiEndpoint = testingUrl;
875+
pubsub!.options!.emulatorMode = false;
876+
pubsub.determineBaseUrl_?.();
877+
878+
assert.strictEqual(pubsub.options?.servicePath, 'localhost');
879+
assert.strictEqual(pubsub.options.port, 8085);
880+
assert.ok(pubsub.options.sslCreds === undefined);
881+
assert.strictEqual(pubsub.isEmulator, false);
882+
});
883+
860884
it('should remove slashes from the baseUrl', () => {
861885
setHost('localhost:8080/');
862886
pubsub.determineBaseUrl_?.();

0 commit comments

Comments
 (0)