Skip to content

use fake "owner" credential when rtdb emulator env var is set #2029

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

Merged
merged 15 commits into from
Aug 9, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 17 additions & 7 deletions packages/database/src/core/AuthTokenProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,28 @@ import { FirebaseAuthTokenData } from '@firebase/app-types/private';
import { log, warn } from './util/util';

/**
* Abstraction around FirebaseApp's token fetching capabilities.
* An interface for token fetchers.
*/
export class AuthTokenProvider {
/**
* @param {!FirebaseApp} app_
*/
constructor(private app_: FirebaseApp) {}

export interface AuthTokenProvider {
/**
* @param {boolean} forceRefresh
* @return {!Promise<FirebaseAuthTokenData>}
*/
getToken(forceRefresh: boolean): Promise<FirebaseAuthTokenData>;

addTokenChangeListener(listener: (token: string | null) => void);

removeTokenChangeListener(listener: (token: string | null) => void);

notifyForInvalidToken();
}

/**
* Abstraction around FirebaseApp's token fetching capabilities.
*/
export class FirebaseAuthTokenProvider implements AuthTokenProvider {
constructor(private app_: FirebaseApp) {}

getToken(forceRefresh: boolean): Promise<FirebaseAuthTokenData> {
return this.app_['INTERNAL']['getToken'](forceRefresh).then(
null,
Expand Down
44 changes: 44 additions & 0 deletions packages/database/src/core/EmulatorAuthTokenProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* @license
* Copyright 2019 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { FirebaseApp } from '@firebase/app-types';
import { FirebaseAuthTokenData } from '@firebase/app-types/private';

import { AuthTokenProvider } from './AuthTokenProvider';
import { log, warn } from './util/util';

class EmulatorAuthToken implements FirebaseAuthTokenData {
constructor(public accessToken: string) {}
}

export class EmulatorAuthTokenProvider implements AuthTokenProvider {
constructor(private app_: FirebaseApp) {}

getToken(forceRefresh: boolean): Promise<FirebaseAuthTokenData> {
return Promise.resolve(new EmulatorAuthToken('owner'));
}

addTokenChangeListener(listener: (token: string | null) => void) {}

removeTokenChangeListener(listener: (token: string | null) => void) {}

notifyForInvalidToken() {
let errorMessage =
'Database emulator unexpectedly rejected fake "owner" credentials.';
warn(errorMessage);
}
}
5 changes: 4 additions & 1 deletion packages/database/src/core/ReadonlyRestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ import { safeGet } from '@firebase/util';
import { querystring } from '@firebase/util';
import { ServerActions } from './ServerActions';
import { RepoInfo } from './RepoInfo';
import { AuthTokenProvider } from './AuthTokenProvider';
import {
FirebaseAuthTokenProvider,
AuthTokenProvider
} from './AuthTokenProvider';
import { Query } from '../api/Query';

/**
Expand Down
25 changes: 22 additions & 3 deletions packages/database/src/core/Repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,20 @@ import { SparseSnapshotTree } from './SparseSnapshotTree';
import { SyncTree } from './SyncTree';
import { SnapshotHolder } from './SnapshotHolder';
import { stringify } from '@firebase/util';
import { beingCrawled, each, exceptionGuard, warn, log } from './util/util';
import {
beingCrawled,
each,
exceptionGuard,
warn,
log,
FIREBASE_DATABASE_EMULATOR_HOST_VAR
} from './util/util';
import { map, isEmpty } from '@firebase/util';
import { AuthTokenProvider } from './AuthTokenProvider';
import {
FirebaseAuthTokenProvider,
AuthTokenProvider
} from './AuthTokenProvider';
import { EmulatorAuthTokenProvider } from './EmulatorAuthTokenProvider';
import { StatsManager } from './stats/StatsManager';
import { StatsReporter } from './stats/StatsReporter';
import { StatsListener } from './stats/StatsListener';
Expand Down Expand Up @@ -81,7 +92,15 @@ export class Repo {
forceRestClient: boolean,
public app: FirebaseApp
) {
const authTokenProvider = new AuthTokenProvider(app);
let authTokenProvider: AuthTokenProvider;
if (
typeof process !== 'undefined' &&
process.env[FIREBASE_DATABASE_EMULATOR_HOST_VAR]
) {
authTokenProvider = new EmulatorAuthTokenProvider(app);
} else {
authTokenProvider = new FirebaseAuthTokenProvider(app);
}

this.stats_ = StatsManager.getCollection(repoInfo_);

Expand Down
12 changes: 1 addition & 11 deletions packages/database/src/core/RepoManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import { FirebaseApp } from '@firebase/app-types';
import { safeGet } from '@firebase/util';
import { Repo } from './Repo';
import { fatal } from './util/util';
import { fatal, FIREBASE_DATABASE_EMULATOR_HOST_VAR } from './util/util';
import { parseRepoInfo, parseURL } from './util/libs/parser';
import { validateUrl } from './util/validation';
import './Repo_transaction';
Expand All @@ -28,16 +28,6 @@ import { RepoInfo } from './RepoInfo';
/** @const {string} */
const DATABASE_URL_OPTION = 'databaseURL';

/**
* This variable is also defined in the firebase node.js admin SDK. Before
* modifying this definition, consult the definition in:
*
* https://github.com/firebase/firebase-admin-node
*
* and make sure the two are consistent.
*/
const FIREBASE_DATABASE_EMULATOR_HOST_VAR = 'FIREBASE_DATABASE_EMULATOR_HOST';

let _staticInstance: RepoManager;

/**
Expand Down
11 changes: 11 additions & 0 deletions packages/database/src/core/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ import { Logger, LogLevel } from '@firebase/logger';

const logClient = new Logger('@firebase/database');

/**
* Environment variable for enabling interaction with the Firebase Realtime Database
* emulator. If set, the module will present the endpoint with a fake "owner" credential
* (see EmulatorAuthTokenProvider) instead of one belonging to a real account.
*
* The expected format for this variable is '<HOST>:<PORT>'. The transfer protocol must be
* omitted and will default to 'http'.
*/
export const FIREBASE_DATABASE_EMULATOR_HOST_VAR =
'FIREBASE_DATABASE_EMULATOR_HOST';

/**
* Returns a locally-unique ID (generated by just incrementing up from 0 each time its called).
* @type {function(): number} Generated ID.
Expand Down