Skip to content

Commit c91fd9b

Browse files
authored
Add support for secrets in v2 (#1079)
In addition to adding secrets to v2 `__endpoint` annotation, I'm also making following (small) changes: * Enforce that `platform` be part of trigger annotation - Firebase CLI already assumes that it's included. * Endpoint secret environment variable annotation (for v1 & v2)
1 parent 0406897 commit c91fd9b

File tree

7 files changed

+41
-21
lines changed

7 files changed

+41
-21
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
Adds auth blocking triggers to the auth and identity namespaces (1080).
1+
- Adds auth blocking triggers to the auth and identity namespaces (1080).
2+
- Add support for secrets for v2 triggers (#1079).

spec/v1/cloud-functions.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929
EventContext,
3030
makeCloudFunction,
3131
MakeCloudFunctionArgs,
32-
} from '../../src/cloud-functions';
32+
} from '../../src';
3333

3434
describe('makeCloudFunction', () => {
3535
const cloudFunctionArgs: MakeCloudFunctionArgs<any> = {
@@ -124,7 +124,7 @@ describe('makeCloudFunction', () => {
124124
},
125125
retry: false,
126126
},
127-
secretEnvironmentVariables: [{ secret: 'MY_SECRET', key: 'MY_SECRET' }],
127+
secretEnvironmentVariables: [{ key: 'MY_SECRET' }],
128128
labels: {},
129129
});
130130
});

spec/v2/providers/fixtures.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { ManifestEndpoint } from '../../../src/runtime/manifest';
2+
import { TriggerAnnotation } from '../../../src/v2/core';
13
import * as options from '../../../src/v2/options';
24

35
export const FULL_OPTIONS: options.GlobalOptions = {
@@ -15,9 +17,10 @@ export const FULL_OPTIONS: options.GlobalOptions = {
1517
labels: {
1618
hello: 'world',
1719
},
20+
secrets: ['MY_SECRET'],
1821
};
1922

20-
export const FULL_TRIGGER = {
23+
export const FULL_TRIGGER: TriggerAnnotation = {
2124
platform: 'gcfv2',
2225
regions: ['us-west1'],
2326
availableMemoryMb: 512,
@@ -32,9 +35,10 @@ export const FULL_TRIGGER = {
3235
labels: {
3336
hello: 'world',
3437
},
38+
secrets: ['MY_SECRET'],
3539
};
3640

37-
export const FULL_ENDPOINT = {
41+
export const FULL_ENDPOINT: ManifestEndpoint = {
3842
platform: 'gcfv2',
3943
region: ['us-west1'],
4044
availableMemoryMb: 512,
@@ -52,4 +56,5 @@ export const FULL_ENDPOINT = {
5256
labels: {
5357
hello: 'world',
5458
},
59+
secretEnvironmentVariables: [{ key: 'MY_SECRET' }],
5560
};

src/cloud-functions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ export function optionsToEndpoint(
636636
options,
637637
'secretEnvironmentVariables',
638638
'secrets',
639-
(secrets) => secrets.map((secret) => ({ secret, key: secret }))
639+
(secrets) => secrets.map((secret) => ({ key: secret }))
640640
);
641641
if (options?.vpcConnector) {
642642
endpoint.vpc = { connector: options.vpcConnector };

src/v2/core.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { ManifestEndpoint } from '../runtime/manifest';
2424

2525
/** @internal */
2626
export interface TriggerAnnotation {
27+
platform?: string;
2728
concurrency?: number;
2829
minInstances?: number;
2930
maxInstances?: number;
@@ -44,11 +45,11 @@ export interface TriggerAnnotation {
4445
vpcConnectorEgressSettings?: string;
4546
serviceAccountEmail?: string;
4647
ingressSettings?: string;
48+
secrets?: string[];
4749
blockingTrigger?: {
4850
eventType: string;
4951
options?: Record<string, unknown>;
5052
};
51-
5253
// TODO: schedule
5354
}
5455

src/v2/options.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { ManifestEndpoint } from '../runtime/manifest';
3131
import { TriggerAnnotation } from './core';
3232
import { declaredParams } from './params';
3333
import { ParamSpec } from './params/types';
34+
import { HttpsOptions } from './providers/https';
3435

3536
/**
3637
* List of all regions supported by Cloud Functions v2
@@ -215,6 +216,11 @@ export interface GlobalOptions {
215216
* Invoker to set access control on https functions.
216217
*/
217218
invoker?: 'public' | 'private' | string | string[];
219+
220+
/*
221+
* Secrets to bind to a functions.
222+
*/
223+
secrets?: string[];
218224
}
219225

220226
let globalOptions: GlobalOptions | undefined;
@@ -251,7 +257,7 @@ export interface EventHandlerOptions extends GlobalOptions {
251257
* @internal
252258
*/
253259
export function optionsToTriggerAnnotations(
254-
opts: GlobalOptions | EventHandlerOptions
260+
opts: GlobalOptions | EventHandlerOptions | HttpsOptions
255261
): TriggerAnnotation {
256262
const annotation: TriggerAnnotation = {};
257263
copyIfPresent(
@@ -263,7 +269,8 @@ export function optionsToTriggerAnnotations(
263269
'ingressSettings',
264270
'labels',
265271
'vpcConnector',
266-
'vpcConnectorEgressSettings'
272+
'vpcConnectorEgressSettings',
273+
'secrets'
267274
);
268275
convertIfPresent(
269276
annotation,
@@ -312,7 +319,7 @@ export function optionsToTriggerAnnotations(
312319
* @internal
313320
*/
314321
export function optionsToEndpoint(
315-
opts: GlobalOptions | EventHandlerOptions
322+
opts: GlobalOptions | EventHandlerOptions | HttpsOptions
316323
): ManifestEndpoint {
317324
const endpoint: ManifestEndpoint = {};
318325
copyIfPresent(
@@ -350,6 +357,13 @@ export function optionsToEndpoint(
350357
}
351358
return region;
352359
});
360+
convertIfPresent(
361+
endpoint,
362+
opts,
363+
'secretEnvironmentVariables',
364+
'secrets',
365+
(secrets) => secrets.map((secret) => ({ key: secret }))
366+
);
353367

354368
return endpoint;
355369
}

src/v2/providers/https.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,16 @@ import {
3333
} from '../../common/providers/https';
3434
import { ManifestEndpoint } from '../../runtime/manifest';
3535
import * as options from '../options';
36+
import { GlobalOptions, SupportedRegion } from '../options';
3637

3738
export { Request, CallableRequest, FunctionsErrorCode, HttpsError };
3839

39-
export interface HttpsOptions extends Omit<options.GlobalOptions, 'region'> {
40-
region?:
41-
| options.SupportedRegion
42-
| string
43-
| Array<options.SupportedRegion | string>;
40+
/**
41+
* Options that can be set on an individual HTTPS Cloud Function.
42+
*/
43+
export interface HttpsOptions extends Omit<GlobalOptions, 'region'> {
44+
/* HTTP functions can override and specify more than one regions. */
45+
region?: SupportedRegion | string | Array<SupportedRegion | string>;
4446
cors?: string | boolean | RegExp | Array<string | RegExp>;
4547
}
4648

@@ -54,7 +56,6 @@ export type HttpsFunction = ((
5456
export interface CallableFunction<T, Return> extends HttpsFunction {
5557
run(data: CallableRequest<T>): Return;
5658
}
57-
5859
export function onRequest(
5960
opts: HttpsOptions,
6061
handler: (
@@ -195,9 +196,7 @@ export function onCall<T = any, Return = any | Promise<any>>(
195196
);
196197
// global options calls region a scalar and https allows it to be an array,
197198
// but optionsToTriggerAnnotations handles both cases.
198-
const specificOpts = options.optionsToTriggerAnnotations(
199-
opts as options.GlobalOptions
200-
);
199+
const specificOpts = options.optionsToTriggerAnnotations(opts);
201200
return {
202201
platform: 'gcfv2',
203202
...baseOpts,
@@ -216,8 +215,8 @@ export function onCall<T = any, Return = any | Promise<any>>(
216215

217216
const baseOpts = options.optionsToEndpoint(options.getGlobalOptions());
218217
// global options calls region a scalar and https allows it to be an array,
219-
// but optionsToManifestEndpoint handles both cases.
220-
const specificOpts = options.optionsToEndpoint(opts as options.GlobalOptions);
218+
// but optionsToEndpoint handles both cases.
219+
const specificOpts = options.optionsToEndpoint(opts);
221220
func.__endpoint = {
222221
platform: 'gcfv2',
223222
...baseOpts,

0 commit comments

Comments
 (0)