[go: nahoru, domu]

Skip to content
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

Fix incorrect databaseUrl in Functions emulator #2979

Merged
merged 4 commits into from
Dec 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
- Allow more than 100 concurrent connections to the Realtime Database emulator.
- Fixes incorrect `databaseURL` inside the Cloud Functions emulator for new projects (#2965).
77 changes: 77 additions & 0 deletions src/emulator/adminSdkConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { firebaseApiOrigin } from "../api";
import * as apiv2 from "../apiv2";
import { configstore } from "../configstore";
import { FirebaseError } from "../error";
import * as logger from "../logger";

export type AdminSdkConfig = {
projectId: string;
databaseURL?: string;
storageBucket?: string;
locationId?: string;
};

const _CONFIGSTORE_KEY = "adminsdkconfig";

/**
* When all else fails we can "guess" the AdminSdkConfig, although this is likely to
* be incorrect.
*/
export function constructDefaultAdminSdkConfig(projectId: string): AdminSdkConfig {
// Do our best to provide reasonable FIREBASE_CONFIG, based on firebase-functions implementation
// https://github.com/firebase/firebase-functions/blob/59d6a7e056a7244e700dc7b6a180e25b38b647fd/src/setup.ts#L45
return {
projectId: projectId,
databaseURL: process.env.DATABASE_URL || `https://${projectId}.firebaseio.com`,
storageBucket: process.env.STORAGE_BUCKET_URL || `${projectId}.appspot.com`,
};
}

/**
* Get the Admin SDK configuration associated with a project, falling back to a cache when offline.
*/
export async function getProjectAdminSdkConfigOrCached(
projectId: string
): Promise<AdminSdkConfig | undefined> {
try {
const config = await getProjectAdminSdkConfig(projectId);
setCacheAdminSdkConfig(projectId, config);
return config;
} catch (e) {
logger.debug(`Failed to get Admin SDK config for ${projectId}, falling back to cache`, e);
return getCachedAdminSdkConfig(projectId);
}
}

/**
* Gets the Admin SDK configuration associated with a project.
*/
export async function getProjectAdminSdkConfig(projectId: string): Promise<AdminSdkConfig> {
const apiClient = new apiv2.Client({
auth: true,
apiVersion: "v1beta1",
urlPrefix: firebaseApiOrigin,
});

try {
const res = await apiClient.get<AdminSdkConfig>(`projects/${projectId}/adminSdkConfig`);
return res.body;
} catch (err) {
throw new FirebaseError(
`Failed to get Admin SDK for Firebase project ${projectId}. ` +
"Please make sure the project exists and your account has permission to access it.",
{ exit: 2, original: err }
);
}
}

function setCacheAdminSdkConfig(projectId: string, config: AdminSdkConfig) {
const allConfigs = configstore.get(_CONFIGSTORE_KEY) || {};
allConfigs[projectId] = config;
configstore.set(_CONFIGSTORE_KEY, allConfigs);
}

function getCachedAdminSdkConfig(projectId: string): AdminSdkConfig | undefined {
const allConfigs = configstore.get(_CONFIGSTORE_KEY) || {};
return allConfigs[projectId];
}
28 changes: 27 additions & 1 deletion src/emulator/functionsEmulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ import { FirebaseError } from "../error";
import { WorkQueue } from "./workQueue";
import { createDestroyer } from "../utils";
import { getCredentialPathAsync } from "../defaultCredentials";
import {
getProjectAdminSdkConfigOrCached,
AdminSdkConfig,
constructDefaultAdminSdkConfig,
} from "./adminSdkConfig";

const EVENT_INVOKE = "functions:invoke";

Expand Down Expand Up @@ -128,9 +133,10 @@ export class FunctionsEmulator implements EmulatorInstance {
private workerPool: RuntimeWorkerPool;
private workQueue: WorkQueue;
private logger = EmulatorLogger.forEmulator(Emulators.FUNCTIONS);

private multicastTriggers: { [s: string]: string[] } = {};

private adminSdkConfig: AdminSdkConfig;

constructor(private args: FunctionsEmulatorArgs) {
// TODO: Would prefer not to have static state but here we are!
EmulatorLogger.verbosity = this.args.quiet ? Verbosity.QUIET : Verbosity.DEBUG;
Expand All @@ -141,6 +147,10 @@ export class FunctionsEmulator implements EmulatorInstance {
this.args.disabledRuntimeFeatures.timeout = true;
}

this.adminSdkConfig = {
projectId: this.args.projectId,
};

const mode = this.args.debugPort
? FunctionsExecutionMode.SEQUENTIAL
: FunctionsExecutionMode.AUTO;
Expand Down Expand Up @@ -317,6 +327,18 @@ export class FunctionsEmulator implements EmulatorInstance {
...this.args.env,
};

const adminSdkConfig = await getProjectAdminSdkConfigOrCached(this.args.projectId);
if (adminSdkConfig) {
this.adminSdkConfig = adminSdkConfig;
} else {
this.logger.logLabeled(
"WARN",
"functions",
"Unable to fetch project Admin SDK configuration, Admin SDK behavior in Cloud Functions emulator may be incorrect."
);
this.adminSdkConfig = constructDefaultAdminSdkConfig(this.args.projectId);
}

const { host, port } = this.getInfo();
this.workQueue.start();
const server = this.createHubServer().listen(port, host);
Expand Down Expand Up @@ -685,6 +707,10 @@ export class FunctionsEmulator implements EmulatorInstance {
pubsub: EmulatorRegistry.getInfo(Emulators.PUBSUB),
auth: EmulatorRegistry.getInfo(Emulators.AUTH),
},
adminSdkConfig: {
databaseURL: this.adminSdkConfig.databaseURL,
storageBucket: this.adminSdkConfig.storageBucket,
},
disabled_features: this.args.disabledRuntimeFeatures,
};
}
Expand Down
24 changes: 13 additions & 11 deletions src/emulator/functionsEmulatorRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -654,20 +654,22 @@ async function initializeEnvironmentalVariables(frb: FunctionsRuntimeBundle): Pr
const functionsGt380 = compareVersionStrings(functionsResolution.version, "3.8.0") >= 0;
let emulatedDatabaseURL = undefined;
if (frb.emulators.database && functionsGt380) {
emulatedDatabaseURL = `http://${formatHost(frb.emulators.database)}?ns=${
process.env.GCLOUD_PROJECT
}`;
// Database URL will look like one of:
// - https://${namespace}.firebaseio.com
// - https://${namespace}.${location}.firebasedatabase.app
let ns = frb.projectId;
if (frb.adminSdkConfig.databaseURL) {
const asUrl = new URL(frb.adminSdkConfig.databaseURL);
ns = asUrl.hostname.split(".")[0];
}

emulatedDatabaseURL = `http://${formatHost(frb.emulators.database)}?ns=${ns}`;
samtstern marked this conversation as resolved.
Show resolved Hide resolved
}

// Do our best to provide reasonable FIREBASE_CONFIG, based on firebase-functions implementation
// https://github.com/firebase/firebase-functions/blob/59d6a7e056a7244e700dc7b6a180e25b38b647fd/src/setup.ts#L45
process.env.FIREBASE_CONFIG = JSON.stringify({
databaseURL:
process.env.DATABASE_URL ||
emulatedDatabaseURL ||
`https://${process.env.GCLOUD_PROJECT}.firebaseio.com`,
storageBucket: process.env.STORAGE_BUCKET_URL || `${process.env.GCLOUD_PROJECT}.appspot.com`,
projectId: process.env.GCLOUD_PROJECT,
storageBucket: frb.adminSdkConfig.storageBucket,
databaseURL: emulatedDatabaseURL || frb.adminSdkConfig.databaseURL,
projectId: frb.projectId,
});

if (frb.triggerId) {
Expand Down
4 changes: 4 additions & 0 deletions src/emulator/functionsEmulatorShared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ export interface FunctionsRuntimeBundle {
port: number;
};
};
adminSdkConfig: {
databaseURL?: string;
storageBucket?: string;
};
socketPath?: string;
disabled_features?: FunctionsRuntimeFeatures;
nodeMajorVersion?: number;
Expand Down
36 changes: 26 additions & 10 deletions src/test/emulators/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ export const TIMEOUT_LONG = 10000;
export const TIMEOUT_MED = 5000;

export const MODULE_ROOT = findModuleRoot("firebase-tools", __dirname);
export const FunctionRuntimeBundles = {
export const FunctionRuntimeBundles: { [key: string]: FunctionsRuntimeBundle } = {
onCreate: {
adminSdkConfig: {
databaseURL: "https://fake-project-id-default-rtdb.firebaseio.com",
storageBucket: "fake-project-id.appspot.com",
},
emulators: {
firestore: {
host: "localhost",
Expand Down Expand Up @@ -39,9 +43,12 @@ export const FunctionRuntimeBundles = {
},
triggerId: "function_id",
projectId: "fake-project-id",
} as FunctionsRuntimeBundle,

},
onWrite: {
adminSdkConfig: {
databaseURL: "https://fake-project-id-default-rtdb.firebaseio.com",
storageBucket: "fake-project-id.appspot.com",
},
emulators: {
firestore: {
host: "localhost",
Expand Down Expand Up @@ -75,9 +82,12 @@ export const FunctionRuntimeBundles = {
},
triggerId: "function_id",
projectId: "fake-project-id",
} as FunctionsRuntimeBundle,

},
onDelete: {
adminSdkConfig: {
databaseURL: "https://fake-project-id-default-rtdb.firebaseio.com",
storageBucket: "fake-project-id.appspot.com",
},
emulators: {
firestore: {
host: "localhost",
Expand Down Expand Up @@ -111,9 +121,12 @@ export const FunctionRuntimeBundles = {
},
triggerId: "function_id",
projectId: "fake-project-id",
} as FunctionsRuntimeBundle,

},
onUpdate: {
adminSdkConfig: {
databaseURL: "https://fake-project-id-default-rtdb.firebaseio.com",
storageBucket: "fake-project-id.appspot.com",
},
emulators: {
firestore: {
host: "localhost",
Expand Down Expand Up @@ -159,9 +172,12 @@ export const FunctionRuntimeBundles = {
},
triggerId: "function_id",
projectId: "fake-project-id",
} as FunctionsRuntimeBundle,

},
onRequest: {
adminSdkConfig: {
databaseURL: "https://fake-project-id-default-rtdb.firebaseio.com",
storageBucket: "fake-project-id.appspot.com",
},
emulators: {
firestore: {
host: "localhost",
Expand All @@ -171,5 +187,5 @@ export const FunctionRuntimeBundles = {
cwd: MODULE_ROOT,
triggerId: "function_id",
projectId: "fake-project-id",
} as FunctionsRuntimeBundle,
},
};
24 changes: 12 additions & 12 deletions src/test/emulators/functionsEmulatorRuntime.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ describe("FunctionsEmulator-Runtime", () => {
}).timeout(TIMEOUT_MED);

it("should expose Firestore prod when the emulator is not running", async () => {
const frb = _.cloneDeep(FunctionRuntimeBundles.onRequest) as FunctionsRuntimeBundle;
const frb = _.cloneDeep(FunctionRuntimeBundles.onRequest);
frb.emulators = {};

const worker = invokeRuntimeWithFunctions(frb, () => {
Expand All @@ -260,7 +260,7 @@ describe("FunctionsEmulator-Runtime", () => {
}).timeout(TIMEOUT_MED);

it("should set FIRESTORE_EMULATOR_HOST when the emulator is running", async () => {
const frb = _.cloneDeep(FunctionRuntimeBundles.onRequest) as FunctionsRuntimeBundle;
const frb = _.cloneDeep(FunctionRuntimeBundles.onRequest);
frb.emulators = {
firestore: {
host: "localhost",
Expand All @@ -286,7 +286,7 @@ describe("FunctionsEmulator-Runtime", () => {
}).timeout(TIMEOUT_MED);

it("should expose a stubbed Firestore when the emulator is running", async () => {
const frb = _.cloneDeep(FunctionRuntimeBundles.onRequest) as FunctionsRuntimeBundle;
const frb = _.cloneDeep(FunctionRuntimeBundles.onRequest);
frb.emulators = {
firestore: {
host: "localhost",
Expand Down Expand Up @@ -315,7 +315,7 @@ describe("FunctionsEmulator-Runtime", () => {
}).timeout(TIMEOUT_MED);

it("should expose RTDB prod when the emulator is not running", async () => {
const frb = _.cloneDeep(FunctionRuntimeBundles.onRequest) as FunctionsRuntimeBundle;
const frb = _.cloneDeep(FunctionRuntimeBundles.onRequest);
frb.emulators = {};

const worker = invokeRuntimeWithFunctions(frb, () => {
Expand All @@ -337,11 +337,11 @@ describe("FunctionsEmulator-Runtime", () => {

const data = await callHTTPSFunction(worker, frb);
const info = JSON.parse(data);
expect(info.url).to.eql("https://fake-project-id.firebaseio.com/");
expect(info.url).to.eql("https://fake-project-id-default-rtdb.firebaseio.com/");
}).timeout(TIMEOUT_MED);

it("should set FIREBASE_DATABASE_EMULATOR_HOST when the emulator is running", async () => {
const frb = _.cloneDeep(FunctionRuntimeBundles.onRequest) as FunctionsRuntimeBundle;
const frb = _.cloneDeep(FunctionRuntimeBundles.onRequest);
frb.emulators = {
database: {
host: "localhost",
Expand All @@ -366,7 +366,7 @@ describe("FunctionsEmulator-Runtime", () => {
}).timeout(TIMEOUT_MED);

it("should expose a stubbed RTDB when the emulator is running", async () => {
const frb = _.cloneDeep(FunctionRuntimeBundles.onRequest) as FunctionsRuntimeBundle;
const frb = _.cloneDeep(FunctionRuntimeBundles.onRequest);
frb.emulators = {
database: {
host: "localhost",
Expand Down Expand Up @@ -396,7 +396,7 @@ describe("FunctionsEmulator-Runtime", () => {
}).timeout(TIMEOUT_MED);

it("should return an emulated databaseURL when RTDB emulator is running", async () => {
const frb = _.cloneDeep(FunctionRuntimeBundles.onRequest) as FunctionsRuntimeBundle;
const frb = _.cloneDeep(FunctionRuntimeBundles.onRequest);
frb.emulators = {
database: {
host: "localhost",
Expand All @@ -417,11 +417,11 @@ describe("FunctionsEmulator-Runtime", () => {

const data = await callHTTPSFunction(worker, frb);
const info = JSON.parse(data);
expect(info.databaseURL).to.eql(`http://localhost:9090?ns=${frb.projectId}`);
expect(info.databaseURL).to.eql(`http://localhost:9090?ns=fake-project-id-default-rtdb`);
}).timeout(TIMEOUT_MED);

it("should return a real databaseURL when RTDB emulator is not running", async () => {
const frb = _.cloneDeep(FunctionRuntimeBundles.onRequest) as FunctionsRuntimeBundle;
const frb = _.cloneDeep(FunctionRuntimeBundles.onRequest);
const worker = invokeRuntimeWithFunctions(frb, () => {
const admin = require("firebase-admin");
admin.initializeApp();
Expand All @@ -435,12 +435,12 @@ describe("FunctionsEmulator-Runtime", () => {

const data = await callHTTPSFunction(worker, frb);
const info = JSON.parse(data);
expect(info.databaseURL).to.eql(`https://${frb.projectId}.firebaseio.com`);
expect(info.databaseURL).to.eql(frb.adminSdkConfig.databaseURL!);
}).timeout(TIMEOUT_MED);
});

it("should set FIREBASE_AUTH_EMULATOR_HOST when the emulator is running", async () => {
const frb = _.cloneDeep(FunctionRuntimeBundles.onRequest) as FunctionsRuntimeBundle;
const frb = _.cloneDeep(FunctionRuntimeBundles.onRequest);
frb.emulators = {
auth: {
host: "localhost",
Expand Down
5 changes: 5 additions & 0 deletions src/test/emulators/functionsRuntimeWorker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ class MockRuntimeBundle implements FunctionsRuntimeBundle {
triggerType = EmulatedTriggerType.HTTPS;
cwd = "/home/users/dir";
emulators = {};
adminSdkConfig = {
projectId: "project-1234",
datbaseURL: "https://project-1234-default-rtdb.firebaseio.com",
storageBucket: "project-1234.appspot.com",
};

constructor(public triggerId: string) {}
}
Expand Down