Skip to content

Allow SecretParams to be directly registered in v1/v2 options #1256

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 2 commits into from
Oct 6, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions spec/v1/function-builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// SOFTWARE.

import { expect } from "chai";
import { clearParams, defineSecret } from "../../src/params";

import * as functions from "../../src/v1";
import { ResetValue } from "../../src/common/options";
Expand Down Expand Up @@ -94,6 +95,24 @@ describe("FunctionBuilder", () => {
expect(fn.__endpoint.eventTrigger.retry).to.deep.equal(true);
});

it("should allow SecretParams in the secrets array and convert them", () => {
const sp = defineSecret("API_KEY");
const fn = functions
.runWith({
secrets: [sp],
})
.auth.user()
.onCreate((user) => user);

expect(fn.__endpoint.secretEnvironmentVariables).to.deep.equal([
{
key: "API_KEY",
},
]);

clearParams();
});

it("should apply a default failure policy if it's aliased with `true`", () => {
const fn = functions
.runWith({
Expand Down Expand Up @@ -493,26 +512,53 @@ describe("FunctionBuilder", () => {
});

it("should throw error given secrets expressed with full resource name", () => {
const sp = defineSecret("projects/my-project/secrets/API_KEY");

expect(() =>
functions.runWith({
secrets: ["projects/my-project/secrets/API_KEY"],
})
).to.throw();

expect(() =>
functions.runWith({
secrets: [sp],
})
).to.throw();
clearParams();
});

it("should throw error given invalid secret config", () => {
const sp = defineSecret("ABC/efg");

expect(() =>
functions.runWith({
secrets: ["ABC/efg"],
})
).to.throw();

expect(() =>
functions.runWith({
secrets: [sp],
})
).to.throw();
clearParams();
});

it("should throw error given invalid secret with versions", () => {
const sp = defineSecret("ABC@3");

expect(() =>
functions.runWith({
secrets: ["ABC@3"],
})
).to.throw();

expect(() =>
functions.runWith({
secrets: [sp],
})
).to.throw();
clearParams();
});
});
1 change: 0 additions & 1 deletion src/runtime/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ export interface ManifestEndpoint {
maxDispatchesPerSecond?: number | Expression<number> | ResetValue;
};
};

scheduleTrigger?: {
schedule: string | Expression<string>;
timeZone?: string | Expression<string> | ResetValue;
Expand Down
10 changes: 8 additions & 2 deletions src/v1/cloud-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
ManifestRequiredAPI,
} from "../runtime/manifest";
import { ResetValue } from "../common/options";
import { SecretParam } from "../params/types";

export { Change } from "../common/change";

Expand Down Expand Up @@ -485,8 +486,13 @@ export function optionsToEndpoint(options: DeploymentOptions): ManifestEndpoint
);
convertIfPresent(endpoint, options, "region", "regions");
convertIfPresent(endpoint, options, "serviceAccountEmail", "serviceAccount", (sa) => sa);
convertIfPresent(endpoint, options, "secretEnvironmentVariables", "secrets", (secrets) =>
secrets.map((secret) => ({ key: secret }))
convertIfPresent(
endpoint,
options,
"secretEnvironmentVariables",
"secrets",
(secrets: (string | SecretParam)[]) =>
secrets.map((secret) => ({ key: secret instanceof SecretParam ? secret.name : secret }))
);
if (options?.vpcConnector !== undefined) {
if (options.vpcConnector === null || options.vpcConnector instanceof ResetValue) {
Expand Down
5 changes: 4 additions & 1 deletion src/v1/function-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import * as express from "express";

import { ResetValue } from "../common/options";
import { SecretParam } from "../params/types";
import { EventContext } from "./cloud-functions";
import {
DeploymentOptions,
Expand Down Expand Up @@ -192,7 +193,9 @@ function assertRuntimeOptionsValid(runtimeOptions: RuntimeOptions): boolean {
}

if (runtimeOptions.secrets !== undefined) {
const invalidSecrets = runtimeOptions.secrets.filter((s) => !/^[A-Za-z\d\-_]+$/.test(s));
const invalidSecrets = runtimeOptions.secrets.filter(
(s) => !/^[A-Za-z\d\-_]+$/.test(s instanceof SecretParam ? s.name : s)
);
if (invalidSecrets.length > 0) {
throw new Error(
`Invalid secrets: ${invalidSecrets.join(",")}. ` +
Expand Down
3 changes: 2 additions & 1 deletion src/v1/function-configuration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Expression } from "../params";
import { ResetValue } from "../common/options";
import { SecretParam } from "../params/types";

export { RESET_VALUE } from "../common/options";

Expand Down Expand Up @@ -230,7 +231,7 @@ export interface RuntimeOptions {
/*
* Secrets to bind to a function instance.
*/
secrets?: string[];
secrets?: (string | SecretParam)[];

/**
* Determines whether Firebase AppCheck is enforced.
Expand Down
13 changes: 9 additions & 4 deletions src/v2/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { convertIfPresent, copyIfPresent } from "../common/encoding";
import { RESET_VALUE, ResetValue } from "../common/options";
import { ManifestEndpoint } from "../runtime/manifest";
import { declaredParams, Expression } from "../params";
import { ParamSpec } from "../params/types";
import { ParamSpec, SecretParam } from "../params/types";
import { HttpsOptions } from "./providers/https";
import * as logger from "../logger";

Expand Down Expand Up @@ -181,7 +181,7 @@ export interface GlobalOptions {
/*
* Secrets to bind to a function.
*/
secrets?: string[];
secrets?: (string | SecretParam)[];

/**
* Determines whether Firebase AppCheck is enforced. Defaults to false.
Expand Down Expand Up @@ -296,8 +296,13 @@ export function optionsToEndpoint(
}
return region;
});
convertIfPresent(endpoint, opts, "secretEnvironmentVariables", "secrets", (secrets) =>
secrets.map((secret) => ({ key: secret }))
convertIfPresent(
endpoint,
opts,
"secretEnvironmentVariables",
"secrets",
(secrets: (string | SecretParam)[]) =>
secrets.map((secret) => ({ key: secret instanceof SecretParam ? secret.name : secret }))
);

return endpoint;
Expand Down
3 changes: 2 additions & 1 deletion src/v2/providers/alerts/alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { CloudEvent, CloudFunction } from "../../core";
import { Expression } from "../../../params";
import { wrapTraceContext } from "../../trace";
import * as options from "../../options";
import { SecretParam } from "../../../params/types";

/**
* The CloudEvent data emitted by Firebase Alerts.
Expand Down Expand Up @@ -173,7 +174,7 @@ export interface FirebaseAlertOptions extends options.EventHandlerOptions {
/*
* Secrets to bind to a function.
*/
secrets?: string[];
secrets?: (string | SecretParam)[];

/** Whether failed executions should be delivered again. */
retry?: boolean | Expression<boolean> | ResetValue;
Expand Down
3 changes: 2 additions & 1 deletion src/v2/providers/alerts/appDistribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { CloudEvent, CloudFunction } from "../../core";
import { wrapTraceContext } from "../../trace";
import { convertAlertAndApp, FirebaseAlertData, getEndpointAnnotation } from "./alerts";
import * as options from "../../options";
import { SecretParam } from "../../../params/types";

/**
* The internal payload object for adding a new tester device to app distribution.
Expand Down Expand Up @@ -186,7 +187,7 @@ export interface AppDistributionOptions extends options.EventHandlerOptions {
/*
* Secrets to bind to a function.
*/
secrets?: string[];
secrets?: (string | SecretParam)[];

/** Whether failed executions should be delivered again. */
retry?: boolean | Expression<boolean> | ResetValue;
Expand Down
3 changes: 2 additions & 1 deletion src/v2/providers/alerts/crashlytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { CloudEvent, CloudFunction } from "../../core";
import { wrapTraceContext } from "../../trace";
import { convertAlertAndApp, FirebaseAlertData, getEndpointAnnotation } from "./alerts";
import * as options from "../../options";
import { SecretParam } from "../../../params/types";

/** Generic Crashlytics issue interface */
export interface Issue {
Expand Down Expand Up @@ -264,7 +265,7 @@ export interface CrashlyticsOptions extends options.EventHandlerOptions {
/*
* Secrets to bind to a function.
*/
secrets?: string[];
secrets?: (string | SecretParam)[];

/** Whether failed executions should be delivered again. */
retry?: boolean | Expression<boolean> | ResetValue;
Expand Down
3 changes: 2 additions & 1 deletion src/v2/providers/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { CloudEvent, CloudFunction } from "../core";
import { Expression } from "../../params";
import { wrapTraceContext } from "../trace";
import * as options from "../options";
import { SecretParam } from "../../params/types";

export { DataSnapshot };

Expand Down Expand Up @@ -185,7 +186,7 @@ export interface ReferenceOptions<Ref extends string = string> extends options.E
/*
* Secrets to bind to a function.
*/
secrets?: string[];
secrets?: (string | SecretParam)[];

/** Whether failed executions should be delivered again. */
retry?: boolean | Expression<boolean> | ResetValue;
Expand Down
3 changes: 2 additions & 1 deletion src/v2/providers/eventarc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { CloudEvent, CloudFunction } from "../core";
import { wrapTraceContext } from "../trace";
import { Expression } from "../../params";
import * as options from "../options";
import { SecretParam } from "../../params/types";

/** Options that can be set on an Eventarc trigger. */
export interface EventarcTriggerOptions extends options.EventHandlerOptions {
Expand Down Expand Up @@ -149,7 +150,7 @@ export interface EventarcTriggerOptions extends options.EventHandlerOptions {
/*
* Secrets to bind to a function.
*/
secrets?: string[];
secrets?: (string | SecretParam)[];

/** Whether failed executions should be delivered again. */
retry?: boolean | Expression<boolean> | ResetValue;
Expand Down
3 changes: 2 additions & 1 deletion src/v2/providers/https.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
import { initV2Endpoint, ManifestEndpoint } from "../../runtime/manifest";
import { GlobalOptions, SupportedRegion } from "../options";
import { Expression } from "../../params";
import { SecretParam } from "../../params/types";
import * as options from "../options";

export { Request, CallableRequest, FunctionsErrorCode, HttpsError };
Expand Down Expand Up @@ -142,7 +143,7 @@ export interface HttpsOptions extends Omit<GlobalOptions, "region"> {
/*
* Secrets to bind to a function.
*/
secrets?: string[];
secrets?: (string | SecretParam)[];

/**
* Invoker to set access control on https functions.
Expand Down
3 changes: 2 additions & 1 deletion src/v2/providers/identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { wrapTraceContext } from "../trace";
import { Expression } from "../../params";
import { initV2Endpoint } from "../../runtime/manifest";
import * as options from "../options";
import { SecretParam } from "../../params/types";

export { AuthUserRecord, AuthBlockingEvent, HttpsError };

Expand Down Expand Up @@ -151,7 +152,7 @@ export interface BlockingOptions {
/*
* Secrets to bind to a function.
*/
secrets?: string[];
secrets?: (string | SecretParam)[];
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/v2/providers/pubsub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { CloudEvent, CloudFunction } from "../core";
import { wrapTraceContext } from "../trace";
import { Expression } from "../../params";
import * as options from "../options";
import { SecretParam } from "../../params/types";

/**
* Google Cloud Pub/Sub is a globally distributed message bus that automatically scales as you need it.
Expand Down Expand Up @@ -242,7 +243,7 @@ export interface PubSubOptions extends options.EventHandlerOptions {
/*
* Secrets to bind to a function.
*/
secrets?: string[];
secrets?: (string | SecretParam)[];

/** Whether failed executions should be delivered again. */
retry?: boolean | Expression<boolean> | ResetValue;
Expand Down
3 changes: 2 additions & 1 deletion src/v2/providers/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { CloudEvent, CloudFunction } from "../core";
import { wrapTraceContext } from "../trace";
import { Expression } from "../../params";
import * as options from "../options";
import { SecretParam } from "../../params/types";

/**
* An object within Google Cloud Storage.
Expand Down Expand Up @@ -290,7 +291,7 @@ export interface StorageOptions extends options.EventHandlerOptions {
/*
* Secrets to bind to a function.
*/
secrets?: string[];
secrets?: (string | SecretParam)[];

/** Whether failed executions should be delivered again. */
retry?: boolean | Expression<boolean> | ResetValue;
Expand Down
3 changes: 2 additions & 1 deletion src/v2/providers/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import * as options from "../options";
import { wrapTraceContext } from "../trace";
import { HttpsFunction } from "./https";
import { Expression } from "../../params";
import { SecretParam } from "../../params/types";
import { initV2Endpoint, initTaskQueueTrigger } from "../../runtime/manifest";

export { AuthData, Request };
Expand Down Expand Up @@ -147,7 +148,7 @@ export interface TaskQueueOptions extends options.EventHandlerOptions {
/*
* Secrets to bind to a function.
*/
secrets?: string[];
secrets?: (string | SecretParam)[];

/** Whether failed executions should be delivered again. */
retry?: boolean;
Expand Down