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

Release CF3's support for environment variables and secrets #4149

Merged
merged 14 commits into from
Feb 10, 2022
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.
Jump to
Jump to file
Failed to load files.
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 @@
- Improves experience for `firebase login --no-localhost`.
- Add support for specifying environment variable of CF3 function using dotenv.
71 changes: 1 addition & 70 deletions scripts/emulator-tests/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,6 @@ export const TIMEOUT_MED = 5000;
export const MODULE_ROOT = findModuleRoot("firebase-tools", __dirname);
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",
port: 8080,
},
},
cwd: MODULE_ROOT,
proto: {
data: {
value: {
Expand All @@ -41,22 +30,8 @@ export const FunctionRuntimeBundles: { [key: string]: FunctionsRuntimeBundle } =
},
},
},
triggerId: "us-central1-function_id",
targetName: "function_id",
projectId: "fake-project-id",
},
onWrite: {
adminSdkConfig: {
databaseURL: "https://fake-project-id-default-rtdb.firebaseio.com",
storageBucket: "fake-project-id.appspot.com",
},
emulators: {
firestore: {
host: "localhost",
port: 8080,
},
},
cwd: MODULE_ROOT,
proto: {
data: {
value: {
Expand All @@ -81,22 +56,8 @@ export const FunctionRuntimeBundles: { [key: string]: FunctionsRuntimeBundle } =
},
},
},
triggerId: "us-central1-function_id",
targetName: "function_id",
projectId: "fake-project-id",
},
onDelete: {
adminSdkConfig: {
databaseURL: "https://fake-project-id-default-rtdb.firebaseio.com",
storageBucket: "fake-project-id.appspot.com",
},
emulators: {
firestore: {
host: "localhost",
port: 8080,
},
},
cwd: MODULE_ROOT,
proto: {
data: {
oldValue: {
Expand All @@ -121,22 +82,8 @@ export const FunctionRuntimeBundles: { [key: string]: FunctionsRuntimeBundle } =
},
},
},
triggerId: "us-central1-function_id",
targetName: "function_id",
projectId: "fake-project-id",
},
onUpdate: {
adminSdkConfig: {
databaseURL: "https://fake-project-id-default-rtdb.firebaseio.com",
storageBucket: "fake-project-id.appspot.com",
},
emulators: {
firestore: {
host: "localhost",
port: 8080,
},
},
cwd: MODULE_ROOT,
proto: {
data: {
oldValue: {
Expand Down Expand Up @@ -173,25 +120,9 @@ export const FunctionRuntimeBundles: { [key: string]: FunctionsRuntimeBundle } =
timestamp: "2019-05-15T16:21:15.148831Z",
},
},
triggerId: "us-central1-function_id",
targetName: "function_id",
projectId: "fake-project-id",
},
onRequest: {
adminSdkConfig: {
databaseURL: "https://fake-project-id-default-rtdb.firebaseio.com",
storageBucket: "fake-project-id.appspot.com",
},
emulators: {
firestore: {
host: "localhost",
port: 8080,
},
},
cwd: MODULE_ROOT,
triggerId: "us-central1-function_id",
targetName: "function_id",
projectId: "fake-project-id",
proto: {},
},
};

Expand Down
95 changes: 87 additions & 8 deletions scripts/emulator-tests/functionsEmulator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import * as fs from "fs";

import { expect } from "chai";
import * as express from "express";
import * as sinon from "sinon";
import * as supertest from "supertest";
import * as winston from "winston";
import * as logform from "logform";

import { SignatureType } from "../../src/emulator/functionsEmulatorShared";
import { EmulatedTriggerDefinition } from "../../src/emulator/functionsEmulatorShared";
import {
EmulatableBackend,
FunctionsEmulator,
Expand All @@ -14,8 +18,7 @@ import { RuntimeWorker } from "../../src/emulator/functionsRuntimeWorker";
import { TIMEOUT_LONG, TIMEOUT_MED, MODULE_ROOT } from "./fixtures";
import { logger } from "../../src/logger";
import * as registry from "../../src/emulator/registry";
import * as winston from "winston";
import * as logform from "logform";
import * as secretManager from "../../src/gcp/secretManager";

if ((process.env.DEBUG || "").toLowerCase().includes("spec")) {
const dropLogLevels = (info: logform.TransformableInfo) => info.message;
Expand All @@ -32,6 +35,7 @@ if ((process.env.DEBUG || "").toLowerCase().includes("spec")) {

const functionsEmulator = new FunctionsEmulator({
projectId: "fake-project-id",
projectDir: MODULE_ROOT,
emulatableBackends: [
{
functionsDir: MODULE_ROOT,
Expand Down Expand Up @@ -96,6 +100,23 @@ functionsEmulator.setTriggersForTesting(
httpsTrigger: {},
labels: {},
},
{
platform: "gcfv1",
name: "secrets_function_id",
id: "us-central1-secrets_function_id",
region: "us-central1",
entryPoint: "secrets_function_id",
secretEnvironmentVariables: [
{
projectId: "fake-project-id",
secret: "MY_SECRET",
key: "MY_SECRET",
version: "1",
},
],
httpsTrigger: {},
labels: {},
},
],
testBackend
);
Expand All @@ -108,13 +129,11 @@ function useFunctions(triggers: () => {}): void {
// eslint-disable-next-line @typescript-eslint/unbound-method
functionsEmulator.startFunctionRuntime = (
backend: EmulatableBackend,
triggerId: string,
targetName: string,
triggerType: SignatureType,
trigger: EmulatedTriggerDefinition,
proto?: any,
runtimeOpts?: InvokeRuntimeOpts
): RuntimeWorker => {
return startFunctionRuntime(testBackend, triggerId, targetName, triggerType, proto, {
): Promise<RuntimeWorker> => {
return startFunctionRuntime(testBackend, trigger, proto, {
nodeBinary: process.execPath,
serializedTriggers,
});
Expand Down Expand Up @@ -700,4 +719,64 @@ describe("FunctionsEmulator-Hub", () => {
});
}).timeout(TIMEOUT_MED);
});

describe("secrets", () => {
let readFileSyncStub: sinon.SinonStub;
let accessSecretVersionStub: sinon.SinonStub;

beforeEach(() => {
readFileSyncStub = sinon.stub(fs, "readFileSync").throws("Unexpected call");
accessSecretVersionStub = sinon
.stub(secretManager, "accessSecretVersion")
.rejects("Unexpected call");
});

afterEach(() => {
readFileSyncStub.restore();
accessSecretVersionStub.restore();
});

it("should load secret values from local secrets file if one exists", async () => {
readFileSyncStub.returns("MY_SECRET=local");

useFunctions(() => {
return {
secrets_function_id: require("firebase-functions").https.onRequest(
(req: express.Request, res: express.Response) => {
res.json({ secret: process.env.MY_SECRET });
}
),
};
});

await supertest(functionsEmulator.createHubServer())
.get("/fake-project-id/us-central1/secrets_function_id")
.expect(200)
.then((res) => {
expect(res.body.secret).to.equal("local");
});
}).timeout(TIMEOUT_LONG);

it("should try to access secret values from Secret Manager", async () => {
readFileSyncStub.throws({ code: "ENOENT" });
accessSecretVersionStub.resolves("secretManager");

useFunctions(() => {
return {
secrets_function_id: require("firebase-functions").https.onRequest(
(req: express.Request, res: express.Response) => {
res.json({ secret: process.env.MY_SECRET });
}
),
};
});

await supertest(functionsEmulator.createHubServer())
.get("/fake-project-id/us-central1/secrets_function_id")
.expect(200)
.then((res) => {
expect(res.body.secret).to.equal("secretManager");
});
}).timeout(TIMEOUT_LONG);
});
});