Skip to content

Commit 8e71eca

Browse files
authored
Allow SecretParams to be directly registered in v1/v2 options (#1256)
1 parent 5f924bc commit 8e71eca

16 files changed

+89
-19
lines changed

spec/v1/function-builder.spec.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
// SOFTWARE.
2222

2323
import { expect } from "chai";
24+
import { clearParams, defineSecret } from "../../src/params";
2425

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

98+
it("should allow SecretParams in the secrets array and convert them", () => {
99+
const sp = defineSecret("API_KEY");
100+
const fn = functions
101+
.runWith({
102+
secrets: [sp],
103+
})
104+
.auth.user()
105+
.onCreate((user) => user);
106+
107+
expect(fn.__endpoint.secretEnvironmentVariables).to.deep.equal([
108+
{
109+
key: "API_KEY",
110+
},
111+
]);
112+
113+
clearParams();
114+
});
115+
97116
it("should apply a default failure policy if it's aliased with `true`", () => {
98117
const fn = functions
99118
.runWith({
@@ -493,26 +512,53 @@ describe("FunctionBuilder", () => {
493512
});
494513

495514
it("should throw error given secrets expressed with full resource name", () => {
515+
const sp = defineSecret("projects/my-project/secrets/API_KEY");
516+
496517
expect(() =>
497518
functions.runWith({
498519
secrets: ["projects/my-project/secrets/API_KEY"],
499520
})
500521
).to.throw();
522+
523+
expect(() =>
524+
functions.runWith({
525+
secrets: [sp],
526+
})
527+
).to.throw();
528+
clearParams();
501529
});
502530

503531
it("should throw error given invalid secret config", () => {
532+
const sp = defineSecret("ABC/efg");
533+
504534
expect(() =>
505535
functions.runWith({
506536
secrets: ["ABC/efg"],
507537
})
508538
).to.throw();
539+
540+
expect(() =>
541+
functions.runWith({
542+
secrets: [sp],
543+
})
544+
).to.throw();
545+
clearParams();
509546
});
510547

511548
it("should throw error given invalid secret with versions", () => {
549+
const sp = defineSecret("ABC@3");
550+
512551
expect(() =>
513552
functions.runWith({
514553
secrets: ["ABC@3"],
515554
})
516555
).to.throw();
556+
557+
expect(() =>
558+
functions.runWith({
559+
secrets: [sp],
560+
})
561+
).to.throw();
562+
clearParams();
517563
});
518564
});

src/runtime/manifest.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ export interface ManifestEndpoint {
7878
maxDispatchesPerSecond?: number | Expression<number> | ResetValue;
7979
};
8080
};
81-
8281
scheduleTrigger?: {
8382
schedule: string | Expression<string>;
8483
timeZone?: string | Expression<string> | ResetValue;

src/v1/cloud-functions.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
ManifestRequiredAPI,
3333
} from "../runtime/manifest";
3434
import { ResetValue } from "../common/options";
35+
import { SecretParam } from "../params/types";
3536

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

@@ -485,8 +486,13 @@ export function optionsToEndpoint(options: DeploymentOptions): ManifestEndpoint
485486
);
486487
convertIfPresent(endpoint, options, "region", "regions");
487488
convertIfPresent(endpoint, options, "serviceAccountEmail", "serviceAccount", (sa) => sa);
488-
convertIfPresent(endpoint, options, "secretEnvironmentVariables", "secrets", (secrets) =>
489-
secrets.map((secret) => ({ key: secret }))
489+
convertIfPresent(
490+
endpoint,
491+
options,
492+
"secretEnvironmentVariables",
493+
"secrets",
494+
(secrets: (string | SecretParam)[]) =>
495+
secrets.map((secret) => ({ key: secret instanceof SecretParam ? secret.name : secret }))
490496
);
491497
if (options?.vpcConnector !== undefined) {
492498
if (options.vpcConnector === null || options.vpcConnector instanceof ResetValue) {

src/v1/function-builder.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import * as express from "express";
2424

2525
import { ResetValue } from "../common/options";
26+
import { SecretParam } from "../params/types";
2627
import { EventContext } from "./cloud-functions";
2728
import {
2829
DeploymentOptions,
@@ -192,7 +193,9 @@ function assertRuntimeOptionsValid(runtimeOptions: RuntimeOptions): boolean {
192193
}
193194

194195
if (runtimeOptions.secrets !== undefined) {
195-
const invalidSecrets = runtimeOptions.secrets.filter((s) => !/^[A-Za-z\d\-_]+$/.test(s));
196+
const invalidSecrets = runtimeOptions.secrets.filter(
197+
(s) => !/^[A-Za-z\d\-_]+$/.test(s instanceof SecretParam ? s.name : s)
198+
);
196199
if (invalidSecrets.length > 0) {
197200
throw new Error(
198201
`Invalid secrets: ${invalidSecrets.join(",")}. ` +

src/v1/function-configuration.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Expression } from "../params";
22
import { ResetValue } from "../common/options";
3+
import { SecretParam } from "../params/types";
34

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

@@ -230,7 +231,7 @@ export interface RuntimeOptions {
230231
/*
231232
* Secrets to bind to a function instance.
232233
*/
233-
secrets?: string[];
234+
secrets?: (string | SecretParam)[];
234235

235236
/**
236237
* Determines whether Firebase AppCheck is enforced.

src/v2/options.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { convertIfPresent, copyIfPresent } from "../common/encoding";
2929
import { RESET_VALUE, ResetValue } from "../common/options";
3030
import { ManifestEndpoint } from "../runtime/manifest";
3131
import { declaredParams, Expression } from "../params";
32-
import { ParamSpec } from "../params/types";
32+
import { ParamSpec, SecretParam } from "../params/types";
3333
import { HttpsOptions } from "./providers/https";
3434
import * as logger from "../logger";
3535

@@ -181,7 +181,7 @@ export interface GlobalOptions {
181181
/*
182182
* Secrets to bind to a function.
183183
*/
184-
secrets?: string[];
184+
secrets?: (string | SecretParam)[];
185185

186186
/**
187187
* Determines whether Firebase AppCheck is enforced. Defaults to false.
@@ -296,8 +296,13 @@ export function optionsToEndpoint(
296296
}
297297
return region;
298298
});
299-
convertIfPresent(endpoint, opts, "secretEnvironmentVariables", "secrets", (secrets) =>
300-
secrets.map((secret) => ({ key: secret }))
299+
convertIfPresent(
300+
endpoint,
301+
opts,
302+
"secretEnvironmentVariables",
303+
"secrets",
304+
(secrets: (string | SecretParam)[]) =>
305+
secrets.map((secret) => ({ key: secret instanceof SecretParam ? secret.name : secret }))
301306
);
302307

303308
return endpoint;

src/v2/providers/alerts/alerts.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { CloudEvent, CloudFunction } from "../../core";
2626
import { Expression } from "../../../params";
2727
import { wrapTraceContext } from "../../trace";
2828
import * as options from "../../options";
29+
import { SecretParam } from "../../../params/types";
2930

3031
/**
3132
* The CloudEvent data emitted by Firebase Alerts.
@@ -173,7 +174,7 @@ export interface FirebaseAlertOptions extends options.EventHandlerOptions {
173174
/*
174175
* Secrets to bind to a function.
175176
*/
176-
secrets?: string[];
177+
secrets?: (string | SecretParam)[];
177178

178179
/** Whether failed executions should be delivered again. */
179180
retry?: boolean | Expression<boolean> | ResetValue;

src/v2/providers/alerts/appDistribution.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { CloudEvent, CloudFunction } from "../../core";
3131
import { wrapTraceContext } from "../../trace";
3232
import { convertAlertAndApp, FirebaseAlertData, getEndpointAnnotation } from "./alerts";
3333
import * as options from "../../options";
34+
import { SecretParam } from "../../../params/types";
3435

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

191192
/** Whether failed executions should be delivered again. */
192193
retry?: boolean | Expression<boolean> | ResetValue;

src/v2/providers/alerts/crashlytics.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { CloudEvent, CloudFunction } from "../../core";
3131
import { wrapTraceContext } from "../../trace";
3232
import { convertAlertAndApp, FirebaseAlertData, getEndpointAnnotation } from "./alerts";
3333
import * as options from "../../options";
34+
import { SecretParam } from "../../../params/types";
3435

3536
/** Generic Crashlytics issue interface */
3637
export interface Issue {
@@ -264,7 +265,7 @@ export interface CrashlyticsOptions extends options.EventHandlerOptions {
264265
/*
265266
* Secrets to bind to a function.
266267
*/
267-
secrets?: string[];
268+
secrets?: (string | SecretParam)[];
268269

269270
/** Whether failed executions should be delivered again. */
270271
retry?: boolean | Expression<boolean> | ResetValue;

src/v2/providers/database.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { CloudEvent, CloudFunction } from "../core";
3333
import { Expression } from "../../params";
3434
import { wrapTraceContext } from "../trace";
3535
import * as options from "../options";
36+
import { SecretParam } from "../../params/types";
3637

3738
export { DataSnapshot };
3839

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

190191
/** Whether failed executions should be delivered again. */
191192
retry?: boolean | Expression<boolean> | ResetValue;

src/v2/providers/eventarc.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { CloudEvent, CloudFunction } from "../core";
3232
import { wrapTraceContext } from "../trace";
3333
import { Expression } from "../../params";
3434
import * as options from "../options";
35+
import { SecretParam } from "../../params/types";
3536

3637
/** Options that can be set on an Eventarc trigger. */
3738
export interface EventarcTriggerOptions extends options.EventHandlerOptions {
@@ -149,7 +150,7 @@ export interface EventarcTriggerOptions extends options.EventHandlerOptions {
149150
/*
150151
* Secrets to bind to a function.
151152
*/
152-
secrets?: string[];
153+
secrets?: (string | SecretParam)[];
153154

154155
/** Whether failed executions should be delivered again. */
155156
retry?: boolean | Expression<boolean> | ResetValue;

src/v2/providers/https.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
import { initV2Endpoint, ManifestEndpoint } from "../../runtime/manifest";
4242
import { GlobalOptions, SupportedRegion } from "../options";
4343
import { Expression } from "../../params";
44+
import { SecretParam } from "../../params/types";
4445
import * as options from "../options";
4546

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

147148
/**
148149
* Invoker to set access control on https functions.

src/v2/providers/identity.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import { wrapTraceContext } from "../trace";
3939
import { Expression } from "../../params";
4040
import { initV2Endpoint } from "../../runtime/manifest";
4141
import * as options from "../options";
42+
import { SecretParam } from "../../params/types";
4243

4344
export { AuthUserRecord, AuthBlockingEvent, HttpsError };
4445

@@ -151,7 +152,7 @@ export interface BlockingOptions {
151152
/*
152153
* Secrets to bind to a function.
153154
*/
154-
secrets?: string[];
155+
secrets?: (string | SecretParam)[];
155156
}
156157

157158
/**

src/v2/providers/pubsub.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { CloudEvent, CloudFunction } from "../core";
3232
import { wrapTraceContext } from "../trace";
3333
import { Expression } from "../../params";
3434
import * as options from "../options";
35+
import { SecretParam } from "../../params/types";
3536

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

247248
/** Whether failed executions should be delivered again. */
248249
retry?: boolean | Expression<boolean> | ResetValue;

src/v2/providers/storage.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { CloudEvent, CloudFunction } from "../core";
3333
import { wrapTraceContext } from "../trace";
3434
import { Expression } from "../../params";
3535
import * as options from "../options";
36+
import { SecretParam } from "../../params/types";
3637

3738
/**
3839
* An object within Google Cloud Storage.
@@ -290,7 +291,7 @@ export interface StorageOptions extends options.EventHandlerOptions {
290291
/*
291292
* Secrets to bind to a function.
292293
*/
293-
secrets?: string[];
294+
secrets?: (string | SecretParam)[];
294295

295296
/** Whether failed executions should be delivered again. */
296297
retry?: boolean | Expression<boolean> | ResetValue;

src/v2/providers/tasks.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import * as options from "../options";
3838
import { wrapTraceContext } from "../trace";
3939
import { HttpsFunction } from "./https";
4040
import { Expression } from "../../params";
41+
import { SecretParam } from "../../params/types";
4142
import { initV2Endpoint, initTaskQueueTrigger } from "../../runtime/manifest";
4243

4344
export { AuthData, Request };
@@ -147,7 +148,7 @@ export interface TaskQueueOptions extends options.EventHandlerOptions {
147148
/*
148149
* Secrets to bind to a function.
149150
*/
150-
secrets?: string[];
151+
secrets?: (string | SecretParam)[];
151152

152153
/** Whether failed executions should be delivered again. */
153154
retry?: boolean;

0 commit comments

Comments
 (0)