Skip to content

Commit

Permalink
fix: add option to disable emulator auth handling (temp fix)
Browse files Browse the repository at this point in the history
  • Loading branch information
feywind committed Dec 14, 2023
1 parent 970c4c6 commit d955020
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@google-cloud/pubsub",
"description": "Cloud Pub/Sub Client Library for Node.js",
"version": "4.1.0",
"version": "4.1.0-disable-emulator-test",
"license": "Apache-2.0",
"author": "Google Inc.",
"engines": {
Expand Down
9 changes: 8 additions & 1 deletion src/pubsub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;

export interface ClientConfig extends gax.GrpcClientOptions {
apiEndpoint?: string;
useFakeCredentials?: boolean;
servicePath?: string;
port?: string | number;
sslCreds?: gax.grpc.ChannelCredentials;
Expand Down Expand Up @@ -798,9 +799,15 @@ 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 useFakeCredentials flag lets them override this behaviour.
const officialUrlMatch =
this.options.servicePath!.endsWith('.googleapis.com');
if (!officialUrlMatch) {
if (
(!officialUrlMatch && this.options.useFakeCredentials !== false) ||
this.options.useFakeCredentials === 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!.useFakeCredentials = 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!.useFakeCredentials = 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 d955020

Please sign in to comment.