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

Make ServiceAccount parametrizable #1309

Merged
merged 3 commits into from
Mar 24, 2023
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
30 changes: 30 additions & 0 deletions spec/fixtures/sources/commonjs-parametrized-fields/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const functions = require("../../../../src/v1/index");
const functionsv2 = require("../../../../src/v2/index");
const params = require("../../../../src/params");
params.clearParams();

const stringParam = params.defineString("STRING_PARAM");
const intParam = params.defineInt("INT_PARAM");
const boolParam = params.defineBoolean("BOOLEAN_PARAM");

exports.v1http = functions.runWith({
minInstances: intParam,
maxInstances: intParam,
memory: intParam,
timeoutSeconds: intParam,
serviceAccount: stringParam,
omit: boolParam
}).https.onRequest((req, resp) => {
resp.status(200).send("Hello world!");
});

exports.v2http = functionsv2.https.onRequest({
minInstances: intParam,
maxInstances: intParam,
memory: intParam,
timeoutSeconds: intParam,
serviceAccount: stringParam,
omit: boolParam
}, (req, resp) => {
resp.status(200).send("Hello world!");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "commonjs-parametrized-fields"
}
40 changes: 40 additions & 0 deletions spec/runtime/loader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ManifestEndpoint, ManifestRequiredAPI, ManifestStack } from "../../src/
import { clearParams } from "../../src/params";
import { MINIMAL_V1_ENDPOINT, MINIMAL_V2_ENDPOINT } from "../fixtures";
import { MINIMAL_SCHEDULE_TRIGGER, MINIMIAL_TASK_QUEUE_TRIGGER } from "../v1/providers/fixtures";
import { BooleanParam, IntParam, StringParam } from "../../src/params/types";

describe("extractStack", () => {
const httpFn = functions.https.onRequest(() => undefined);
Expand Down Expand Up @@ -369,6 +370,45 @@ describe("loadStack", () => {
],
},
},
{
name: "can use parameterized fields",
modulePath: "./spec/fixtures/sources/commonjs-parametrized-fields",
expected: {
...expected,
params: [
{ name: "STRING_PARAM", type: "string" },
{ name: "INT_PARAM", type: "int" },
{ name: "BOOLEAN_PARAM", type: "boolean" },
],
endpoints: {
v1http: {
...MINIMAL_V1_ENDPOINT,
platform: "gcfv1",
entryPoint: "v1http",
minInstances: new IntParam("INT_PARAM"),
maxInstances: new IntParam("INT_PARAM"),
availableMemoryMb: new IntParam("INT_PARAM"),
timeoutSeconds: new IntParam("INT_PARAM"),
serviceAccountEmail: new StringParam("STRING_PARAM"),
omit: new BooleanParam("BOOLEAN_PARAM"),
httpsTrigger: {},
},
v2http: {
...MINIMAL_V2_ENDPOINT,
platform: "gcfv2",
entryPoint: "v2http",
minInstances: new IntParam("INT_PARAM"),
maxInstances: new IntParam("INT_PARAM"),
availableMemoryMb: new IntParam("INT_PARAM"),
timeoutSeconds: new IntParam("INT_PARAM"),
serviceAccountEmail: new StringParam("STRING_PARAM"),
omit: new BooleanParam("BOOLEAN_PARAM"),
labels: {},
httpsTrigger: {},
},
},
},
},
];

for (const tc of testcases) {
Expand Down
8 changes: 7 additions & 1 deletion src/common/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import { Expression } from "../params";

// Copied from firebase-tools/src/gcp/proto

/**
Expand Down Expand Up @@ -72,9 +74,13 @@ export function convertIfPresent<Src, Dest>(
dest[destField] = converter(src[srcField]);
}

export function serviceAccountFromShorthand(serviceAccount: string): string | null {
export function serviceAccountFromShorthand(
serviceAccount: string | Expression<string>
): string | Expression<string> | null {
if (serviceAccount === "default") {
return null;
} else if (serviceAccount instanceof Expression) {
return serviceAccount;
} else if (serviceAccount.endsWith("@")) {
if (!process.env.GCLOUD_PROJECT) {
throw new Error(
Expand Down
13 changes: 8 additions & 5 deletions src/v1/function-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import * as express from "express";

import { ResetValue } from "../common/options";
import { SecretParam } from "../params/types";
import { Expression, SecretParam } from "../params/types";
import { EventContext } from "./cloud-functions";
import {
DeploymentOptions,
Expand Down Expand Up @@ -88,12 +88,15 @@ function assertRuntimeOptionsValid(runtimeOptions: RuntimeOptions): boolean {
const serviceAccount = runtimeOptions.serviceAccount;
if (
serviceAccount &&
serviceAccount !== "default" &&
!(serviceAccount instanceof ResetValue) &&
!serviceAccount.includes("@")
!(
serviceAccount === "default" ||
serviceAccount instanceof ResetValue ||
serviceAccount instanceof Expression ||
serviceAccount.includes("@")
)
) {
throw new Error(
`serviceAccount must be set to 'default', a service account email, or '{serviceAccountName}@'`
`serviceAccount must be set to 'default', a string expression, a service account email, or '{serviceAccountName}@'`
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/v1/function-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ export interface RuntimeOptions {
/**
* Specific service account for the function to run as.
*/
serviceAccount?: "default" | string | ResetValue;
serviceAccount?: "default" | string | Expression<string> | ResetValue;

/**
* Ingress settings which control where this function can be called from.
Expand Down
4 changes: 2 additions & 2 deletions src/v2/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export interface GlobalOptions {
/**
* Specific service account for the function to run as.
*/
serviceAccount?: string | ResetValue;
serviceAccount?: string | Expression<string> | ResetValue;

/**
* Ingress settings which control where this function can be called from.
Expand Down Expand Up @@ -255,7 +255,7 @@ export interface EventHandlerOptions extends Omit<GlobalOptions, "enforceAppChec
region?: string;

/** The service account that EventArc should use to invoke this function. Requires the P4SA to have ActAs permission on this service account. */
serviceAccount?: string | ResetValue;
serviceAccount?: string | Expression<string> | ResetValue;

/** The name of the channel where the function receives events. */
channel?: string;
Expand Down
2 changes: 1 addition & 1 deletion src/v2/providers/alerts/alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export interface FirebaseAlertOptions extends options.EventHandlerOptions {
* Specific service account for the function to run as.
* A value of null restores the default service account.
*/
serviceAccount?: string | ResetValue;
serviceAccount?: string | Expression<string> | ResetValue;

/**
* Ingress settings which control where this function can be called from.
Expand Down
2 changes: 1 addition & 1 deletion src/v2/providers/alerts/appDistribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export interface AppDistributionOptions extends options.EventHandlerOptions {
/**
* Specific service account for the function to run as.
*/
serviceAccount?: string | ResetValue;
serviceAccount?: string | Expression<string> | ResetValue;

/**
* Ingress settings which control where this function can be called from.
Expand Down
2 changes: 1 addition & 1 deletion src/v2/providers/alerts/crashlytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ export interface CrashlyticsOptions extends options.EventHandlerOptions {
/**
* Specific service account for the function to run as.
*/
serviceAccount?: string | ResetValue;
serviceAccount?: string | Expression<string> | ResetValue;

/**
* Ingress settings which control where this function can be called from.
Expand Down
2 changes: 1 addition & 1 deletion src/v2/providers/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export interface ReferenceOptions<Ref extends string = string> extends options.E
/**
* Specific service account for the function to run as.
*/
serviceAccount?: string | ResetValue;
serviceAccount?: string | Expression<string> | ResetValue;

/**
* Ingress settings which control where this function can be called from.
Expand Down
2 changes: 1 addition & 1 deletion src/v2/providers/eventarc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export interface EventarcTriggerOptions extends options.EventHandlerOptions {
/**
* Specific service account for the function to run as.
*/
serviceAccount?: string | ResetValue;
serviceAccount?: string | Expression<string> | ResetValue;

/**
* Ingress settings which control where this function can be called from.
Expand Down
2 changes: 1 addition & 1 deletion src/v2/providers/https.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export interface HttpsOptions extends Omit<GlobalOptions, "region"> {
/**
* Specific service account for the function to run as.
*/
serviceAccount?: string | ResetValue;
serviceAccount?: string | Expression<string> | ResetValue;

/**
* Ingress settings which control where this function can be called from.
Expand Down
2 changes: 1 addition & 1 deletion src/v2/providers/identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export interface BlockingOptions {
/**
* Specific service account for the function to run as.
*/
serviceAccount?: string | ResetValue;
serviceAccount?: string | Expression<string> | ResetValue;

/**
* Ingress settings which control where this function can be called from.
Expand Down
2 changes: 1 addition & 1 deletion src/v2/providers/pubsub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export interface PubSubOptions extends options.EventHandlerOptions {
/**
* Specific service account for the function to run as.
*/
serviceAccount?: string | ResetValue;
serviceAccount?: string | Expression<string> | ResetValue;

/**
* Ingress settings which control where this function can be called from.
Expand Down
2 changes: 1 addition & 1 deletion src/v2/providers/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ export interface StorageOptions extends options.EventHandlerOptions {
/**
* Specific service account for the function to run as.
*/
serviceAccount?: string | ResetValue;
serviceAccount?: string | Expression<string> | ResetValue;

/**
* Ingress settings which control where this function can be called from.
Expand Down
2 changes: 1 addition & 1 deletion src/v2/providers/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export interface TaskQueueOptions extends options.EventHandlerOptions {
/**
* Specific service account for the function to run as.
*/
serviceAccount?: string | ResetValue;
serviceAccount?: string | Expression<string> | ResetValue;

/**
* Ingress settings which control where this function can be called from.
Expand Down