Skip to content

Add support for running the emulators over SSL #8967

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/gentle-laws-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@firebase/database-compat": patch
"@firebase/database": patch
"@firebase/firestore": patch
---

Add SSL checks to `connectDatabaseEmulator` and `connectFirestoreEmulator`
1 change: 1 addition & 0 deletions common/api-review/firestore-lite.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export class CollectionReference<AppModelType = DocumentData, DbModelType extend
// @public
export function connectFirestoreEmulator(firestore: Firestore, host: string, port: number, options?: {
mockUserToken?: EmulatorMockTokenOptions | string;
ssl?: boolean;
}): void;

// @public
Expand Down
1 change: 1 addition & 0 deletions common/api-review/firestore.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export class CollectionReference<AppModelType = DocumentData, DbModelType extend
// @public
export function connectFirestoreEmulator(firestore: Firestore, host: string, port: number, options?: {
mockUserToken?: EmulatorMockTokenOptions | string;
ssl?: boolean;
}): void;

// @public
Expand Down
16 changes: 8 additions & 8 deletions config/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,18 @@ module.exports = {
'object': 'it',
'property': 'skip'
},
{
'object': 'it',
'property': 'only'
},
// {
// 'object': 'it',
// 'property': 'only'
// },
{
'object': 'describe',
'property': 'skip'
},
{
'object': 'describe',
'property': 'only'
},
// {
// 'object': 'describe',
// 'property': 'only'
// },
{
'object': 'xit'
}
Expand Down
16 changes: 16 additions & 0 deletions packages/database-compat/test/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,22 @@ describe('Database Tests', () => {
expect((db as any)._delegate._repo.repoInfo_.isUsingEmulator).to.be.false;
});

it('uses ssl when useEmulator is called with https is specified', () => {
const db = firebase.database();
db.useEmulator('https://localhost', 80);
expect((db as any)._delegate._repo.repoInfo_.isUsingEmulator).to.be.true;
expect((db as any)._delegate._repo.repoInfo_.host).to.equal('localhost:80');
expect((db as any)._delegate._repo.repoInfo_.secure).to.be.true;
});

it('uses ssl when useEmulator is called with wss is specified', () => {
const db = firebase.database();
db.useEmulator('wss://localhost', 80);
expect((db as any)._delegate._repo.repoInfo_.isUsingEmulator).to.be.true;
expect((db as any)._delegate._repo.repoInfo_.host).to.equal('localhost:80');
expect((db as any)._delegate._repo.repoInfo_.secure).to.be.true;
});

it('cannot call useEmulator after use', () => {
const db = (firebase as any).database();

Expand Down
4 changes: 3 additions & 1 deletion packages/database/src/api/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function repoManagerApplyEmulatorSettings(
): void {
repo.repoInfo_ = new RepoInfo(
hostAndPort,
/* secure= */ false,
/* secure= */ emulatorOptions?.ssl ?? false,
repo.repoInfo_.namespace,
repo.repoInfo_.webSocketOnly,
repo.repoInfo_.nodeAdmin,
Expand Down Expand Up @@ -348,10 +348,12 @@ export function connectDatabaseEmulator(
port: number,
options: {
mockUserToken?: EmulatorMockTokenOptions | string;
ssl?: boolean;
} = {}
): void {
db = getModularInstance(db);
db._checkNotDeleted('useEmulator');

const hostAndPort = `${host}:${port}`;
const repo = db._repoInternal;
if (db._instanceStarted) {
Expand Down
1 change: 1 addition & 0 deletions packages/database/src/core/RepoInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { each } from './util/util';

export interface RepoInfoEmulatorOptions {
mockUserToken?: string | EmulatorMockTokenOptions;
ssl?: boolean;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion packages/firestore/src/lite-api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,11 @@ export function connectFirestoreEmulator(
port: number,
options: {
mockUserToken?: EmulatorMockTokenOptions | string;
ssl?: boolean;
} = {}
): void {
firestore = cast(firestore, Firestore);
const ssl = options.ssl ?? false;
const settings = firestore._getSettings();
const existingConfig = {
...settings,
Expand All @@ -340,7 +342,7 @@ export function connectFirestoreEmulator(
const newConfig = {
...settings,
host: newHostSetting,
ssl: false,
ssl,
emulatorOptions: options
};
// No-op if the new configuration matches the current configuration. This supports SSR
Expand Down
11 changes: 11 additions & 0 deletions packages/firestore/test/unit/api/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,17 @@ describe('Settings', () => {
expect(db._getEmulatorOptions()).to.equal(emulatorOptions);
});

it('sets ssl to true if set in options', () => {
// Use a new instance of Firestore in order to configure settings.
const db = newTestFirestore();
const emulatorOptions = { mockUserToken: 'test', ssl: true };
connectFirestoreEmulator(db, '127.0.0.1', 9000, emulatorOptions);

expect(db._getSettings().host).to.exist.and.to.equal('127.0.0.1:9000');
expect(db._getSettings().ssl).to.exist.and.to.be.true;
expect(db._getEmulatorOptions()).to.equal(emulatorOptions);
});

it('prefers host from useEmulator to host from settings', () => {
// Use a new instance of Firestore in order to configure settings.
const db = newTestFirestore();
Expand Down
8 changes: 8 additions & 0 deletions packages/functions/src/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ describe('Firebase Functions > Service', () => {
'http://localhost:5005/my-project/us-central1/foo'
);
});
it('can use emulator with SSL', () => {
service = createTestService(app);
connectFunctionsEmulator(service, 'localhost', 5005, true);
assert.equal(
service._url('foo'),
'https://localhost:5005/my-project/us-central1/foo'
);
});

it('correctly sets region', () => {
service = createTestService(app, 'my-region');
Expand Down
5 changes: 3 additions & 2 deletions packages/functions/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,10 @@ export class FunctionsService implements _FirebaseService {
export function connectFunctionsEmulator(
functionsInstance: FunctionsService,
host: string,
port: number
port: number,
ssl?: boolean
): void {
functionsInstance.emulatorOrigin = `http://${host}:${port}`;
functionsInstance.emulatorOrigin = `http${ssl ? 's' : ''}://${host}:${port}`;
}

/**
Expand Down
Loading