firebase-tools 15.22.4 → 15.24.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/apiv2.js +91 -32
- package/lib/apphosting/backend.js +11 -4
- package/lib/bin/cli.js +9 -2
- package/lib/bin/progressiveHelp.js +126 -0
- package/lib/checkValidTargetFilters.js +1 -1
- package/lib/command.js +0 -7
- package/lib/commands/dataconnect-execute.js +1 -1
- package/lib/commands/deploy.js +27 -64
- package/lib/commands/functions-delete.js +5 -3
- package/lib/commands/functions-lifecycle-list.js +94 -0
- package/lib/commands/functions-lifecycle-run.js +29 -0
- package/lib/commands/index.js +3 -0
- package/lib/database/import.js +3 -3
- package/lib/dataconnect/webhook.js +1 -2
- package/lib/deploy/apphosting/index.js +26 -1
- package/lib/deploy/auth/index.js +18 -1
- package/lib/deploy/database/index.js +16 -1
- package/lib/deploy/dataconnect/index.js +19 -1
- package/lib/deploy/extensions/index.js +9 -1
- package/lib/deploy/firestore/index.js +20 -1
- package/lib/deploy/functions/backend.js +9 -0
- package/lib/deploy/functions/build.js +3 -0
- package/lib/deploy/functions/index.js +33 -1
- package/lib/deploy/functions/params.js +23 -15
- package/lib/deploy/functions/prepare.js +96 -1
- package/lib/deploy/functions/prompts.js +81 -0
- package/lib/deploy/functions/release/fabricator.js +79 -4
- package/lib/deploy/functions/release/index.js +45 -23
- package/lib/deploy/functions/release/lifecycle.js +79 -53
- package/lib/deploy/functions/release/planner.js +43 -5
- package/lib/deploy/functions/runtimes/discovery/index.js +4 -3
- package/lib/deploy/functions/runtimes/discovery/v1alpha1.js +18 -1
- package/lib/deploy/functions/runtimes/node/index.js +1 -2
- package/lib/deploy/functions/runtimes/python/index.js +1 -2
- package/lib/deploy/hosting/index.js +30 -1
- package/lib/deploy/hosting/uploader.js +2 -3
- package/lib/deploy/index.js +68 -8
- package/lib/deploy/remoteconfig/index.js +9 -1
- package/lib/deploy/storage/index.js +16 -1
- package/lib/downloadUtils.js +3 -1
- package/lib/emulator/auth/operations.js +18 -8
- package/lib/emulator/downloadableEmulatorInfo.json +24 -24
- package/lib/emulator/storage/apis/gcloud.js +61 -0
- package/lib/emulator/storage/multipart.js +82 -2
- package/lib/emulator/taskQueue.js +3 -11
- package/lib/extensions/extensionsHelper.js +6 -4
- package/lib/frameworks/utils.js +50 -1
- package/lib/gcp/iam.js +68 -2
- package/lib/gcp/knownRoles.json +99 -0
- package/lib/gcp/resourceManager.js +42 -1
- package/lib/gemini/fdcExperience.js +2 -2
- package/lib/hosting/initMiddleware.js +1 -1
- package/lib/hosting/proxy.js +57 -23
- package/lib/index.js +0 -1
- package/lib/management/apps.js +4 -2
- package/lib/profiler.js +1 -2
- package/lib/streamUtils.js +24 -0
- package/lib/track.js +1 -2
- package/lib/tsconfig.compile.tsbuildinfo +1 -1
- package/lib/tsconfig.publish.tsbuildinfo +1 -1
- package/lib/utils.js +4 -21
- package/package.json +2 -4
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.EVENTARC_SOURCE_ENV = void 0;
|
|
4
|
+
exports.discoverSecurityDetails = discoverSecurityDetails;
|
|
4
5
|
exports.prepare = prepare;
|
|
5
6
|
exports.resolveDefaultRegionsForBuild = resolveDefaultRegionsForBuild;
|
|
6
7
|
exports.inferDetailsFromExisting = inferDetailsFromExisting;
|
|
@@ -43,7 +44,100 @@ const backend_1 = require("./backend");
|
|
|
43
44
|
const functional_1 = require("../../functional");
|
|
44
45
|
const prepare_1 = require("../extensions/prepare");
|
|
45
46
|
const prompt = require("../../prompt");
|
|
47
|
+
const iam = require("../../gcp/iam");
|
|
48
|
+
const resourcemanager = require("../../gcp/resourceManager");
|
|
46
49
|
exports.EVENTARC_SOURCE_ENV = "EVENTARC_CLOUD_EVENT_SOURCE";
|
|
50
|
+
async function discoverSecurityDetails(codebase, want, have, projectId, filters) {
|
|
51
|
+
const requiredRoles = want.requiredRoles;
|
|
52
|
+
const firstHave = backend.allEndpoints(have)[0];
|
|
53
|
+
let existingManagedSA;
|
|
54
|
+
let haveRolesEtag;
|
|
55
|
+
if (firstHave) {
|
|
56
|
+
haveRolesEtag = firstHave.labels?.["firebase-declarative-security-etag"];
|
|
57
|
+
existingManagedSA = firstHave.serviceAccount?.startsWith("firebase-fn-")
|
|
58
|
+
? firstHave.serviceAccount
|
|
59
|
+
: undefined;
|
|
60
|
+
}
|
|
61
|
+
const isPartiallyFiltered = !!(filters &&
|
|
62
|
+
filters.some((f) => (!f.codebase || f.codebase === codebase) && f.idChunks && f.idChunks.length > 0));
|
|
63
|
+
const isEnrolling = !!requiredRoles && !existingManagedSA;
|
|
64
|
+
const isUnenrolling = !requiredRoles && !!existingManagedSA && !!haveRolesEtag;
|
|
65
|
+
if (isPartiallyFiltered && (isEnrolling || isUnenrolling)) {
|
|
66
|
+
throw new error_1.FirebaseError("To ensure a whole codebase is migrated cleanly, you may not deploy only part of a " +
|
|
67
|
+
"codebase when opting into or out of declarative security (starting or no longer using `requireRoles`)");
|
|
68
|
+
}
|
|
69
|
+
if (!requiredRoles && (!existingManagedSA || !haveRolesEtag)) {
|
|
70
|
+
return {};
|
|
71
|
+
}
|
|
72
|
+
if (requiredRoles &&
|
|
73
|
+
backend.someEndpoint(want, (e) => typeof e.serviceAccount === "string" && !e.serviceAccount.startsWith("firebase-fn-"))) {
|
|
74
|
+
throw new error_1.FirebaseError(`Cannot use explicit custom service accounts on functions while using declarative security in codebase ${codebase}.`);
|
|
75
|
+
}
|
|
76
|
+
if (!requiredRoles && existingManagedSA && haveRolesEtag) {
|
|
77
|
+
for (const endpoint of backend.allEndpoints(want)) {
|
|
78
|
+
if (!endpoint.serviceAccount || endpoint.serviceAccount === existingManagedSA) {
|
|
79
|
+
endpoint.serviceAccount = null;
|
|
80
|
+
}
|
|
81
|
+
if (endpoint.labels) {
|
|
82
|
+
delete endpoint.labels["firebase-declarative-security-etag"];
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
haveRolesEtag,
|
|
87
|
+
existingManagedSA,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
let managedSA = existingManagedSA;
|
|
91
|
+
if (!managedSA) {
|
|
92
|
+
const saToCreate = await iam.generateManagedServiceAccountName(projectId, "firebase-fn");
|
|
93
|
+
managedSA = `${saToCreate}@${projectId}.iam.gserviceaccount.com`;
|
|
94
|
+
}
|
|
95
|
+
const existingSalt = haveRolesEtag ? haveRolesEtag.split("-")[0] : undefined;
|
|
96
|
+
const newEtag = iam.computeRolesEtag(requiredRoles, existingSalt);
|
|
97
|
+
for (const endpoint of backend.allEndpoints(want)) {
|
|
98
|
+
endpoint.serviceAccount = managedSA;
|
|
99
|
+
endpoint.labels = endpoint.labels || {};
|
|
100
|
+
endpoint.labels["firebase-declarative-security-etag"] = newEtag;
|
|
101
|
+
}
|
|
102
|
+
if (haveRolesEtag && haveRolesEtag === newEtag) {
|
|
103
|
+
return {
|
|
104
|
+
haveRoles: requiredRoles,
|
|
105
|
+
haveRolesEtag,
|
|
106
|
+
existingManagedSA,
|
|
107
|
+
managedSA,
|
|
108
|
+
newEtag,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
const permissionsToTest = ["resourcemanager.projects.setIamPolicy"];
|
|
112
|
+
if (!existingManagedSA) {
|
|
113
|
+
permissionsToTest.push("iam.serviceAccounts.create");
|
|
114
|
+
}
|
|
115
|
+
const iamResult = await iam.testIamPermissions(projectId, permissionsToTest);
|
|
116
|
+
if (!iamResult.passed) {
|
|
117
|
+
if (!existingManagedSA) {
|
|
118
|
+
throw new error_1.FirebaseError(`Cannot enable declarative security because you do not have permissions necessary to create the service account. Please ask an IAM administrator to perform the next deploy.`);
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
throw new error_1.FirebaseError(`You do not have access to make the policy changes required in codebase ${codebase} deploy. Please ask an IAM administrator to perform the next deploy.`);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
let haveRoles = [];
|
|
125
|
+
if (existingManagedSA) {
|
|
126
|
+
try {
|
|
127
|
+
haveRoles = await resourcemanager.getServiceAccountRoles(projectId, managedSA);
|
|
128
|
+
}
|
|
129
|
+
catch (err) {
|
|
130
|
+
throw new error_1.FirebaseError(`The declarative security roles for codebase ${codebase} have changed, but you do not have access to see what has changed. Please ask an IAM administrator to perform the next deploy.`, { original: err });
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return {
|
|
134
|
+
haveRoles,
|
|
135
|
+
haveRolesEtag,
|
|
136
|
+
existingManagedSA,
|
|
137
|
+
managedSA,
|
|
138
|
+
newEtag,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
47
141
|
async function prepare(context, options, payload) {
|
|
48
142
|
const projectId = (0, projectUtils_1.needProjectId)(options);
|
|
49
143
|
const projectNumber = await (0, projectUtils_1.needProjectNumber)(options);
|
|
@@ -196,7 +290,8 @@ async function prepare(context, options, payload) {
|
|
|
196
290
|
const haveBackends = (0, functionsDeployHelper_1.groupEndpointsByCodebase)(wantBackends, backend.allEndpoints(existingBackend));
|
|
197
291
|
for (const [codebase, wantBackend] of Object.entries(wantBackends)) {
|
|
198
292
|
const haveBackend = haveBackends[codebase] || backend.empty();
|
|
199
|
-
|
|
293
|
+
const security = await discoverSecurityDetails(codebase, wantBackend, haveBackend, projectId, context.filters);
|
|
294
|
+
payload.functions[codebase] = { wantBackend, haveBackend, ...security };
|
|
200
295
|
}
|
|
201
296
|
for (const [codebase, { wantBackend, haveBackend }] of Object.entries(payload.functions)) {
|
|
202
297
|
inferDetailsFromExisting(wantBackend, haveBackend, codebaseUsesEnvs.includes(codebase));
|
|
@@ -5,6 +5,8 @@ exports.promptForFunctionDeletion = promptForFunctionDeletion;
|
|
|
5
5
|
exports.promptForUnsafeMigration = promptForUnsafeMigration;
|
|
6
6
|
exports.promptForMinInstances = promptForMinInstances;
|
|
7
7
|
exports.promptForCleanupPolicyDays = promptForCleanupPolicyDays;
|
|
8
|
+
exports.promptForSecurityChanges = promptForSecurityChanges;
|
|
9
|
+
exports.promptForLifecycleEvent = promptForLifecycleEvent;
|
|
8
10
|
const clc = require("colorette");
|
|
9
11
|
const functionsDeployHelper_1 = require("./functionsDeployHelper");
|
|
10
12
|
const error_1 = require("../../error");
|
|
@@ -14,6 +16,7 @@ const backend = require("./backend");
|
|
|
14
16
|
const pricing = require("./pricing");
|
|
15
17
|
const utils = require("../../utils");
|
|
16
18
|
const artifacts = require("../../functions/artifacts");
|
|
19
|
+
const iam = require("../../gcp/iam");
|
|
17
20
|
async function promptForFailurePolicies(options, want, have) {
|
|
18
21
|
const retryEndpoints = backend.allEndpoints(want).filter((e) => {
|
|
19
22
|
return backend.isEventTriggered(e) && e.eventTrigger.retry;
|
|
@@ -191,3 +194,81 @@ async function promptForCleanupPolicyDays(options, locations) {
|
|
|
191
194
|
validate: (days) => !days || isNaN(days) || days < 0 ? "Please enter a non-negative number" : true,
|
|
192
195
|
});
|
|
193
196
|
}
|
|
197
|
+
async function promptForSecurityChanges(plan, options) {
|
|
198
|
+
if (options.force) {
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
for (const [codebase, codebasePlan] of Object.entries(plan)) {
|
|
202
|
+
if (codebasePlan.serviceAccountToDelete) {
|
|
203
|
+
if (options.nonInteractive) {
|
|
204
|
+
throw new error_1.FirebaseError(`Cannot opt out of declarative security and delete managed service account ${codebasePlan.serviceAccountToDelete} in non-interactive mode. Please deploy with --force to confirm.`, { exit: 1 });
|
|
205
|
+
}
|
|
206
|
+
const msg = `Deploying this code will opt out of declarative security for codebase ${codebase}. All functions which do not specify a custom service account will use a default service account on next deploy. As a cleanup, the managed service account ${codebasePlan.serviceAccountToDelete} will be deleted. Continue?`;
|
|
207
|
+
const confirmed = await (0, prompt_1.confirm)({ default: false, message: msg });
|
|
208
|
+
if (!confirmed) {
|
|
209
|
+
throw new error_1.FirebaseError("Deployment canceled by user.");
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
if (codebasePlan.serviceAccountToCreate) {
|
|
213
|
+
if (options.nonInteractive) {
|
|
214
|
+
throw new error_1.FirebaseError(`Cannot enable declarative security and create managed service account ${codebasePlan.serviceAccountToCreate} in non-interactive mode. Please deploy with --force to confirm.`, { exit: 1 });
|
|
215
|
+
}
|
|
216
|
+
const roleNames = await Promise.all((codebasePlan.rolesToAdd || []).map((r) => iam.getRoleName(r)));
|
|
217
|
+
let msg = "This codebase uses declarative security. ";
|
|
218
|
+
if (roleNames.length > 0) {
|
|
219
|
+
msg += `It will use the following role(s):\n${roleNames
|
|
220
|
+
.map((r) => `* ${r}`)
|
|
221
|
+
.join("\n")}\nContinue?`;
|
|
222
|
+
}
|
|
223
|
+
else {
|
|
224
|
+
msg += "It will not use any additional roles. Continue?";
|
|
225
|
+
}
|
|
226
|
+
const confirmed = await (0, prompt_1.confirm)({ default: false, message: msg });
|
|
227
|
+
if (!confirmed) {
|
|
228
|
+
throw new error_1.FirebaseError("Deployment canceled by user.");
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
else if ((codebasePlan.rolesToAdd && codebasePlan.rolesToAdd.length > 0) ||
|
|
232
|
+
(codebasePlan.rolesToRemove && codebasePlan.rolesToRemove.length > 0)) {
|
|
233
|
+
if (options.nonInteractive) {
|
|
234
|
+
throw new error_1.FirebaseError(`Cannot modify declarative security roles for codebase ${codebase} in non-interactive mode. Please deploy with --force to confirm.`, { exit: 1 });
|
|
235
|
+
}
|
|
236
|
+
let msg = `Deploying this code will modify the managed service account for codebase ${codebase}.\n`;
|
|
237
|
+
if (codebasePlan.rolesToAdd && codebasePlan.rolesToAdd.length > 0) {
|
|
238
|
+
const addedNames = await Promise.all(codebasePlan.rolesToAdd.map((r) => iam.getRoleName(r)));
|
|
239
|
+
msg += `All functions in this codebase will be granted the following new role(s):\n${addedNames
|
|
240
|
+
.map((r) => `* ${r}`)
|
|
241
|
+
.join("\n")}\n`;
|
|
242
|
+
}
|
|
243
|
+
if (codebasePlan.rolesToRemove && codebasePlan.rolesToRemove.length > 0) {
|
|
244
|
+
const removedNames = await Promise.all(codebasePlan.rolesToRemove.map((r) => iam.getRoleName(r)));
|
|
245
|
+
msg += `All functions in this codebase will lose access to the following role(s):\n${removedNames
|
|
246
|
+
.map((r) => `* ${r}`)
|
|
247
|
+
.join("\n")}\n`;
|
|
248
|
+
}
|
|
249
|
+
msg += "Continue?";
|
|
250
|
+
const confirmed = await (0, prompt_1.confirm)({ default: false, message: msg });
|
|
251
|
+
if (!confirmed) {
|
|
252
|
+
throw new error_1.FirebaseError("Deployment canceled by user.");
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
async function promptForLifecycleEvent(codebase, wantBackend, options) {
|
|
258
|
+
const hooks = wantBackend.lifecycleHooks || {};
|
|
259
|
+
const choices = [];
|
|
260
|
+
for (const hookName of Object.keys(hooks)) {
|
|
261
|
+
choices.push({ name: hookName, value: hookName });
|
|
262
|
+
}
|
|
263
|
+
if (choices.length === 0) {
|
|
264
|
+
return undefined;
|
|
265
|
+
}
|
|
266
|
+
choices.push({ name: "skip (default)", value: "skip" });
|
|
267
|
+
const selection = await (0, prompt_1.select)({
|
|
268
|
+
message: `We cannot determine whether this deployment is a first deploy or a redeploy for codebase "${codebase}" because it is recovering from a previous partial failure.`,
|
|
269
|
+
choices,
|
|
270
|
+
default: "skip",
|
|
271
|
+
nonInteractive: options?.nonInteractive,
|
|
272
|
+
});
|
|
273
|
+
return selection === "skip" ? undefined : selection;
|
|
274
|
+
}
|
|
@@ -49,6 +49,8 @@ const eventarcPollerOptions = {
|
|
|
49
49
|
maxBackoff: 10000,
|
|
50
50
|
};
|
|
51
51
|
const CLOUD_RUN_RESOURCE_EXHAUSTED_CODE = 8;
|
|
52
|
+
const iam = require("../../../gcp/iam");
|
|
53
|
+
const resourcemanager = require("../../../gcp/resourceManager");
|
|
52
54
|
const rethrowAs = (endpoint, op) => (err) => {
|
|
53
55
|
logger_1.logger.error(err.message);
|
|
54
56
|
throw new reporter.DeploymentError(endpoint, op, err);
|
|
@@ -61,6 +63,70 @@ class Fabricator {
|
|
|
61
63
|
this.sources = args.sources;
|
|
62
64
|
this.appEngineLocation = args.appEngineLocation;
|
|
63
65
|
this.projectNumber = args.projectNumber;
|
|
66
|
+
this.projectId = args.projectId;
|
|
67
|
+
}
|
|
68
|
+
async grantNewRoles(plan, codebase) {
|
|
69
|
+
if (plan.serviceAccountToCreate) {
|
|
70
|
+
utils.logLabeledBullet("functions", `Creating managed service account ${plan.serviceAccountToCreate}...`);
|
|
71
|
+
const saName = plan.serviceAccountToCreate.split("@")[0];
|
|
72
|
+
try {
|
|
73
|
+
await iam.createServiceAccount(this.projectId, saName, `Managed by Firebase CLI for codebase ${codebase}`, `Firebase Functions ${codebase}`);
|
|
74
|
+
}
|
|
75
|
+
catch (e) {
|
|
76
|
+
throw new error_1.FirebaseError("Cannot enable declarative security because you do not have permissions necessary to create the service account. Please ask an IAM administrator to perform the next deploy.", { original: e });
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (plan.rolesToAdd?.length) {
|
|
80
|
+
if (!plan.managedServiceAccount) {
|
|
81
|
+
throw new error_1.FirebaseError("Failed to grant IAM roles: managed service account is missing.", {
|
|
82
|
+
exit: 1,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
utils.logLabeledBullet("functions", `Granting IAM roles to ${plan.managedServiceAccount}...`);
|
|
86
|
+
try {
|
|
87
|
+
await resourcemanager.addServiceAccountRoles(this.projectId, plan.managedServiceAccount, plan.rolesToAdd, true);
|
|
88
|
+
}
|
|
89
|
+
catch (e) {
|
|
90
|
+
throw new error_1.FirebaseError("The declarative security roles for this codebase have changed, but you do not have access to see what has changed. Please ask an IAM administrator to perform the next deploy.", { original: e });
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
else if (plan.rolesToRemove?.length) {
|
|
94
|
+
const iamResult = await iam.testIamPermissions(this.projectId, [
|
|
95
|
+
"resourcemanager.projects.setIamPolicy",
|
|
96
|
+
]);
|
|
97
|
+
if (!iamResult.passed) {
|
|
98
|
+
throw new error_1.FirebaseError("The declarative security roles for this codebase have changed, but you do not have access to see what has changed. Please ask an IAM administrator to perform the next deploy.");
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
async removeOldRoles(plan, codebase) {
|
|
103
|
+
if (plan.serviceAccountToDelete) {
|
|
104
|
+
utils.logLabeledBullet("functions", `Deleting managed service account ${plan.serviceAccountToDelete}...`);
|
|
105
|
+
try {
|
|
106
|
+
await iam.deleteServiceAccount(this.projectId, plan.serviceAccountToDelete);
|
|
107
|
+
}
|
|
108
|
+
catch (e) {
|
|
109
|
+
throw new error_1.FirebaseError("Failed to delete managed service account " +
|
|
110
|
+
plan.serviceAccountToDelete +
|
|
111
|
+
". Please ask an IAM administrator to delete it manually.", { original: e });
|
|
112
|
+
}
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
if (!plan.rolesToRemove?.length) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
if (!plan.managedServiceAccount) {
|
|
119
|
+
throw new error_1.FirebaseError("Failed to revoke IAM roles: managed service account is missing.", {
|
|
120
|
+
exit: 1,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
utils.logLabeledBullet("functions", `Revoking unneeded IAM roles from ${plan.managedServiceAccount} for codebase ${codebase}...`);
|
|
124
|
+
try {
|
|
125
|
+
await resourcemanager.removeServiceAccountRoles(this.projectId, plan.managedServiceAccount, plan.rolesToRemove);
|
|
126
|
+
}
|
|
127
|
+
catch (e) {
|
|
128
|
+
throw new error_1.FirebaseError("The declarative security roles for this codebase have changed, but you do not have access to see what has changed. Please ask an IAM administrator to perform the next deploy.", { original: e });
|
|
129
|
+
}
|
|
64
130
|
}
|
|
65
131
|
async applyPlan(plan) {
|
|
66
132
|
const timer = new timer_1.Timer();
|
|
@@ -68,8 +134,14 @@ class Fabricator {
|
|
|
68
134
|
totalTime: 0,
|
|
69
135
|
results: [],
|
|
70
136
|
};
|
|
71
|
-
const
|
|
72
|
-
|
|
137
|
+
for (const [codebase, codebasePlan] of Object.entries(plan)) {
|
|
138
|
+
await this.grantNewRoles(codebasePlan, codebase);
|
|
139
|
+
}
|
|
140
|
+
const allChangesets = [];
|
|
141
|
+
for (const codebasePlan of Object.values(plan)) {
|
|
142
|
+
allChangesets.push(...Object.values(codebasePlan.regionalChangesets));
|
|
143
|
+
}
|
|
144
|
+
const createAndUpdatePromises = allChangesets.map((changes) => {
|
|
73
145
|
const scraperV1 = new sourceTokenScraper_1.SourceTokenScraper();
|
|
74
146
|
const scraperV2 = new sourceTokenScraper_1.SourceTokenScraper();
|
|
75
147
|
return this.applyUpserts(changes, scraperV1, scraperV2);
|
|
@@ -85,7 +157,7 @@ class Fabricator {
|
|
|
85
157
|
const hasFailures = summary.results.some((r) => r.error);
|
|
86
158
|
if (hasFailures) {
|
|
87
159
|
utils.logLabeledWarning("functions", "Deploys failed. Skipping deletes.");
|
|
88
|
-
summary.results =
|
|
160
|
+
summary.results = allChangesets.reduce((accum, changes) => {
|
|
89
161
|
const currentAborts = changes.endpointsToDelete.map((endpoint) => ({
|
|
90
162
|
endpoint,
|
|
91
163
|
durationMs: 0,
|
|
@@ -96,7 +168,7 @@ class Fabricator {
|
|
|
96
168
|
summary.totalTime = timer.stop();
|
|
97
169
|
return summary;
|
|
98
170
|
}
|
|
99
|
-
const deleteResultsArray = await Promise.allSettled(
|
|
171
|
+
const deleteResultsArray = await Promise.allSettled(allChangesets.map((changes) => this.applyDeletes(changes)));
|
|
100
172
|
const deleteResults = deleteResultsArray.reduce((acc, r) => {
|
|
101
173
|
if (r.status === "fulfilled") {
|
|
102
174
|
return [...acc, ...r.value];
|
|
@@ -105,6 +177,9 @@ class Fabricator {
|
|
|
105
177
|
return acc;
|
|
106
178
|
}, []);
|
|
107
179
|
summary.results.push(...deleteResults);
|
|
180
|
+
for (const [codebase, codebasePlan] of Object.entries(plan)) {
|
|
181
|
+
await this.removeOldRoles(codebasePlan, codebase);
|
|
182
|
+
}
|
|
108
183
|
summary.totalTime = timer.stop();
|
|
109
184
|
return summary;
|
|
110
185
|
}
|
|
@@ -32,38 +32,39 @@ async function release(context, options, payload) {
|
|
|
32
32
|
if (!context.sources) {
|
|
33
33
|
return;
|
|
34
34
|
}
|
|
35
|
-
|
|
36
|
-
for (const [codebase, { wantBackend, haveBackend }] of Object.entries(payload.functions)) {
|
|
37
|
-
plan = {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
35
|
+
const plan = {};
|
|
36
|
+
for (const [codebase, { wantBackend, haveBackend, haveRoles, existingManagedSA, managedSA },] of Object.entries(payload.functions)) {
|
|
37
|
+
plan[codebase] = await planner.createDeploymentPlan({
|
|
38
|
+
codebase,
|
|
39
|
+
wantBackend,
|
|
40
|
+
haveBackend,
|
|
41
|
+
projectId: context.projectId,
|
|
42
|
+
filters: context.filters,
|
|
43
|
+
haveRoles,
|
|
44
|
+
existingManagedSA,
|
|
45
|
+
managedSA,
|
|
46
|
+
});
|
|
46
47
|
}
|
|
47
|
-
|
|
48
|
+
await prompts.promptForSecurityChanges(plan, options);
|
|
49
|
+
const allRegionalChanges = Object.values(plan)
|
|
50
|
+
.map((codebasePlan) => Object.values(codebasePlan.regionalChangesets))
|
|
51
|
+
.reduce(functional_1.reduceFlat, []);
|
|
52
|
+
const fnsToDelete = allRegionalChanges
|
|
48
53
|
.map((regionalChanges) => regionalChanges.endpointsToDelete)
|
|
49
54
|
.reduce(functional_1.reduceFlat, []);
|
|
50
55
|
const shouldDelete = await prompts.promptForFunctionDeletion(fnsToDelete, options);
|
|
51
56
|
if (!shouldDelete) {
|
|
52
|
-
for (const
|
|
53
|
-
|
|
57
|
+
for (const changes of allRegionalChanges) {
|
|
58
|
+
changes.endpointsToDelete = [];
|
|
54
59
|
}
|
|
55
60
|
}
|
|
56
|
-
const fnsToUpdate =
|
|
61
|
+
const fnsToUpdate = allRegionalChanges
|
|
57
62
|
.map((regionalChanges) => regionalChanges.endpointsToUpdate)
|
|
58
63
|
.reduce(functional_1.reduceFlat, []);
|
|
59
64
|
const fnsToUpdateSafe = await prompts.promptForUnsafeMigration(fnsToUpdate, options);
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
for (const eu of fnsToUpdateSafe) {
|
|
64
|
-
const e = eu.endpoint;
|
|
65
|
-
const key = `${e.codebase || ""}-${e.region}-${e.availableMemoryMb || "default"}`;
|
|
66
|
-
plan[key].endpointsToUpdate.push(eu);
|
|
65
|
+
const safeEndpoints = new Set(fnsToUpdateSafe.map((eu) => eu.endpoint));
|
|
66
|
+
for (const changes of allRegionalChanges) {
|
|
67
|
+
changes.endpointsToUpdate = changes.endpointsToUpdate.filter((eu) => safeEndpoints.has(eu.endpoint));
|
|
67
68
|
}
|
|
68
69
|
const throttlerOptions = {
|
|
69
70
|
retries: 30,
|
|
@@ -83,6 +84,7 @@ async function release(context, options, payload) {
|
|
|
83
84
|
sources: context.sources,
|
|
84
85
|
appEngineLocation: (0, functionsConfig_1.getAppEngineLocation)(context.firebaseConfig),
|
|
85
86
|
projectNumber: projectNumber,
|
|
87
|
+
projectId: context.projectId,
|
|
86
88
|
});
|
|
87
89
|
const summary = await fab.applyPlan(plan);
|
|
88
90
|
await reporter.logAndTrackDeployStats(summary, context);
|
|
@@ -90,11 +92,23 @@ async function release(context, options, payload) {
|
|
|
90
92
|
const wantBackend = backend.merge(...Object.values(payload.functions).map((p) => p.wantBackend));
|
|
91
93
|
printTriggerUrls(wantBackend, projectNumber);
|
|
92
94
|
for (const [codebase, { wantBackend: w, haveBackend: h }] of Object.entries(payload.functions)) {
|
|
93
|
-
|
|
95
|
+
const { errors } = getCodebaseDeployStats(codebase, w, h, summary);
|
|
96
|
+
if (errors.length === 0) {
|
|
97
|
+
await (0, lifecycle_1.executeLifecycleHooks)(w, h, plan, codebase, options);
|
|
98
|
+
}
|
|
94
99
|
}
|
|
95
100
|
await setupArtifactCleanupPolicies(options, options.projectId, Object.keys(wantBackend.endpoints));
|
|
96
101
|
const allErrors = summary.results.filter((r) => r.error).map((r) => r.error);
|
|
97
102
|
if (allErrors.length) {
|
|
103
|
+
for (const [codebase, { wantBackend: w, haveBackend: h }] of Object.entries(payload.functions)) {
|
|
104
|
+
const { errors } = getCodebaseDeployStats(codebase, w, h, summary);
|
|
105
|
+
if (errors.length > 0) {
|
|
106
|
+
const event = (0, lifecycle_1.determineLifecycleEvent)(h);
|
|
107
|
+
if (w.lifecycleHooks?.[event]) {
|
|
108
|
+
utils.logLabeledWarning("functions", `Lifecycle hook "${event}" for codebase "${codebase}" was configured but not executed because one or more function deployments failed.`);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
98
112
|
const opts = allErrors.length === 1 ? { original: allErrors[0] } : { children: allErrors };
|
|
99
113
|
logger_1.logger.debug("Functions deploy failed.");
|
|
100
114
|
for (const error of allErrors) {
|
|
@@ -123,6 +137,14 @@ function printTriggerUrls(results, projectNumber) {
|
|
|
123
137
|
logger_1.logger.info(clc.bold("Function URL"), `(${(0, functionsDeployHelper_1.getFunctionLabel)(httpsFunc)}):`, httpsFunc.uri);
|
|
124
138
|
}
|
|
125
139
|
}
|
|
140
|
+
function getCodebaseDeployStats(codebase, wantBackend, haveBackend, summary) {
|
|
141
|
+
const cb = codebase || "default";
|
|
142
|
+
const results = summary.results.filter((r) => (r.endpoint.codebase ?? "default") === cb ||
|
|
143
|
+
!!wantBackend.endpoints[r.endpoint.region]?.[r.endpoint.id] ||
|
|
144
|
+
!!haveBackend.endpoints[r.endpoint.region]?.[r.endpoint.id]);
|
|
145
|
+
const errors = results.filter((r) => r.error);
|
|
146
|
+
return { results, errors };
|
|
147
|
+
}
|
|
126
148
|
async function setupArtifactCleanupPolicies(options, projectId, locations) {
|
|
127
149
|
if (locations.length === 0) {
|
|
128
150
|
return;
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.determineLifecycleEvent = determineLifecycleEvent;
|
|
4
|
+
exports.hasLifecycleHooks = hasLifecycleHooks;
|
|
5
|
+
exports.isRecoveryDeployment = isRecoveryDeployment;
|
|
4
6
|
exports.executeLifecycleHooks = executeLifecycleHooks;
|
|
7
|
+
exports.executeHook = executeHook;
|
|
5
8
|
const backend = require("../backend");
|
|
6
9
|
const error_1 = require("../../../error");
|
|
7
10
|
const logger_1 = require("../../../logger");
|
|
@@ -10,58 +13,75 @@ const cloudtasks = require("../../../gcp/cloudtasks");
|
|
|
10
13
|
const computeEngine = require("../../../gcp/computeEngine");
|
|
11
14
|
const projects_1 = require("../../../management/projects");
|
|
12
15
|
const functional_1 = require("../../../functional");
|
|
13
|
-
|
|
14
|
-
|
|
16
|
+
const prompts = require("../prompts");
|
|
17
|
+
function determineLifecycleEvent(haveBackend) {
|
|
18
|
+
const hasExistingEndpoints = backend.someEndpoint(haveBackend, (ep) => ep.state !== "FAILED");
|
|
15
19
|
if (!hasExistingEndpoints) {
|
|
16
|
-
return "
|
|
20
|
+
return "afterFirstDeploy";
|
|
17
21
|
}
|
|
18
|
-
return "
|
|
22
|
+
return "afterRedeploy";
|
|
19
23
|
}
|
|
20
|
-
|
|
21
|
-
|
|
24
|
+
function hasLifecycleHooks(backendSpec) {
|
|
25
|
+
return !!(backendSpec.lifecycleHooks && Object.keys(backendSpec.lifecycleHooks).length > 0);
|
|
26
|
+
}
|
|
27
|
+
function isRecoveryDeployment(wantBackend, haveBackend) {
|
|
28
|
+
const wantEndpoints = backend.allEndpoints(wantBackend);
|
|
29
|
+
const haveEndpoints = backend.allEndpoints(haveBackend);
|
|
30
|
+
const wantHashes = new Set(wantEndpoints.map((ep) => ep.hash).filter((h) => !!h));
|
|
31
|
+
if (!wantHashes.size) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
const hasSameHash = haveEndpoints.some((ep) => ep.hash && wantHashes.has(ep.hash));
|
|
35
|
+
if (!hasSameHash) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
const hasNetNewFunctions = wantEndpoints.some((wantEp) => !backend.findEndpoint(haveBackend, (haveEp) => haveEp.id === wantEp.id && haveEp.region === wantEp.region && haveEp.state !== "FAILED"));
|
|
39
|
+
return hasNetNewFunctions;
|
|
40
|
+
}
|
|
41
|
+
async function executeLifecycleHooks(wantBackend, haveBackend, plan, codebase, options) {
|
|
42
|
+
if (!hasLifecycleHooks(wantBackend)) {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
let event;
|
|
46
|
+
if (isRecoveryDeployment(wantBackend, haveBackend)) {
|
|
47
|
+
event = await prompts.promptForLifecycleEvent(codebase ?? "default", wantBackend, options);
|
|
48
|
+
if (!event) {
|
|
49
|
+
(0, utils_1.logLabeledBullet)("functions", `Skipping lifecycle hooks for codebase "${codebase ?? "default"}".`);
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
event = determineLifecycleEvent(haveBackend);
|
|
55
|
+
}
|
|
22
56
|
const hooks = wantBackend.lifecycleHooks || {};
|
|
23
|
-
const hook = hooks[
|
|
57
|
+
const hook = hooks[event];
|
|
24
58
|
if (!hook) {
|
|
25
|
-
logger_1.logger.debug(`No lifecycle hook configured for event: ${
|
|
59
|
+
logger_1.logger.debug(`No lifecycle hook configured for event: ${event}`);
|
|
26
60
|
return false;
|
|
27
61
|
}
|
|
28
|
-
if (
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
if (!codebase)
|
|
32
|
-
return true;
|
|
33
|
-
if (key.startsWith(`${codebase}-`))
|
|
34
|
-
return true;
|
|
35
|
-
if (codebase === "default" && key.startsWith("-"))
|
|
36
|
-
return true;
|
|
37
|
-
return false;
|
|
38
|
-
})
|
|
39
|
-
.map(([, c]) => c);
|
|
40
|
-
const hasResourceModifications = relevantChangesets.some((changeset) => changeset.endpointsToCreate.length > 0 ||
|
|
62
|
+
if (event === "afterRedeploy" && plan) {
|
|
63
|
+
const codebasePlans = codebase ? [plan[codebase]].filter(Boolean) : Object.values(plan);
|
|
64
|
+
const hasResourceModifications = codebasePlans.some((codebasePlan) => Object.values(codebasePlan.regionalChangesets).some((changeset) => changeset.endpointsToCreate.length > 0 ||
|
|
41
65
|
changeset.endpointsToUpdate.length > 0 ||
|
|
42
|
-
changeset.endpointsToDelete.length > 0);
|
|
66
|
+
changeset.endpointsToDelete.length > 0));
|
|
43
67
|
if (!hasResourceModifications) {
|
|
44
|
-
(0, utils_1.logLabeledBullet)("functions",
|
|
68
|
+
(0, utils_1.logLabeledBullet)("functions", `No resources modified for codebase: ${codebase ?? "default"}. Skipping afterRedeploy lifecycle hook.`);
|
|
45
69
|
return false;
|
|
46
70
|
}
|
|
47
71
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
await executeTaskQueueHook(hook.task, wantBackend);
|
|
72
|
+
try {
|
|
73
|
+
await executeHook(event, hook, wantBackend);
|
|
51
74
|
return true;
|
|
52
75
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
59
|
-
else {
|
|
60
|
-
(0, functional_1.assertExhaustive)(hook);
|
|
76
|
+
catch (err) {
|
|
77
|
+
const errorMsg = err instanceof Error ? err.message : String(err);
|
|
78
|
+
(0, utils_1.logLabeledWarning)("functions", `Failed to execute ${event} lifecycle hook: ${errorMsg}`);
|
|
79
|
+
(0, utils_1.logLabeledBullet)("functions", `You can retry the lifecycle hook in isolation by running: firebase functions:lifecycle:run ${event} ${codebase ?? "default"}`);
|
|
80
|
+
return false;
|
|
61
81
|
}
|
|
62
82
|
}
|
|
63
83
|
async function executeTaskQueueHook(taskHook, wantBackend) {
|
|
64
|
-
const targetEndpoint =
|
|
84
|
+
const targetEndpoint = backend.findEndpoint(wantBackend, (ep) => ep.id === taskHook.function);
|
|
65
85
|
if (!targetEndpoint) {
|
|
66
86
|
throw new error_1.FirebaseError(`Target endpoint "${taskHook.function}" not found in backend for lifecycle hook.`);
|
|
67
87
|
}
|
|
@@ -94,23 +114,9 @@ async function executeTaskQueueHook(taskHook, wantBackend) {
|
|
|
94
114
|
if (body) {
|
|
95
115
|
task.httpRequest.body = body;
|
|
96
116
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
(0, utils_1.logLabeledBullet)("functions", `View logs for ${taskHook.function} at: ${getCloudConsoleLogUrl(targetEndpoint)}`);
|
|
101
|
-
}
|
|
102
|
-
catch (err) {
|
|
103
|
-
const errorMsg = err instanceof Error ? err.message : String(err);
|
|
104
|
-
(0, utils_1.logLabeledWarning)("functions", `Failed to enqueue task for lifecycle hook ${taskHook.function}: ${errorMsg}`);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
function findTargetEndpoint(backendSpec, targetId) {
|
|
108
|
-
for (const endpoint of backend.allEndpoints(backendSpec)) {
|
|
109
|
-
if (endpoint.id === targetId) {
|
|
110
|
-
return endpoint;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
return undefined;
|
|
117
|
+
await cloudtasks.enqueueTask(queueName, task);
|
|
118
|
+
(0, utils_1.logLabeledSuccess)("functions", `Successfully queued task for lifecycle hook ${taskHook.function} in queue ${queueName}.`);
|
|
119
|
+
return targetEndpoint;
|
|
114
120
|
}
|
|
115
121
|
function getCloudConsoleLogUrl(endpoint) {
|
|
116
122
|
const { project, region, id } = endpoint;
|
|
@@ -118,3 +124,23 @@ function getCloudConsoleLogUrl(endpoint) {
|
|
|
118
124
|
const query = `resource.type="cloud_run_revision"\nresource.labels.service_name="${serviceName}"\nresource.labels.location="${region}"`;
|
|
119
125
|
return `https://console.cloud.google.com/logs/query;query=${encodeURIComponent(query)};project=${project}`;
|
|
120
126
|
}
|
|
127
|
+
async function executeHook(event, hook, backendSpec) {
|
|
128
|
+
let executedEndpoint;
|
|
129
|
+
if ("task" in hook) {
|
|
130
|
+
(0, utils_1.logLabeledBullet)("functions", `Executing ${event} lifecycle hook targeting: ${hook.task.function}...`);
|
|
131
|
+
executedEndpoint = await executeTaskQueueHook(hook.task, backendSpec);
|
|
132
|
+
}
|
|
133
|
+
else if ("call" in hook) {
|
|
134
|
+
throw new error_1.FirebaseError(`Lifecycle hook action type "call" is not supported.`);
|
|
135
|
+
}
|
|
136
|
+
else if ("http" in hook) {
|
|
137
|
+
throw new error_1.FirebaseError(`Lifecycle hook action type "http" is not supported.`);
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
(0, functional_1.assertExhaustive)(hook);
|
|
141
|
+
}
|
|
142
|
+
if (executedEndpoint) {
|
|
143
|
+
(0, utils_1.logLabeledBullet)("functions", `View logs for ${event} at: ${getCloudConsoleLogUrl(executedEndpoint)}`);
|
|
144
|
+
}
|
|
145
|
+
return executedEndpoint;
|
|
146
|
+
}
|