firebase-tools 15.28.3-feat-firebase-debug-path.0 → 15.30.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/bin/cli.js CHANGED
@@ -82,10 +82,7 @@ function cli(pkg) {
82
82
  help = "Having trouble? Try " + clc.bold("firebase [command] --help");
83
83
  }
84
84
  else {
85
- const logFileDisplay = process.env.FIREBASE_DEBUG_PATH?.trim()
86
- ? logFilename
87
- : "firebase-debug.log";
88
- help = `Having trouble? Try again or contact support with contents of ${logFileDisplay}`;
85
+ help = `Having trouble? Try again or contact support with contents of ${logFilename}`;
89
86
  }
90
87
  if (cmd) {
91
88
  console.log();
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.command = void 0;
4
+ exports.hasNonEmptyProjectEnv = hasNonEmptyProjectEnv;
4
5
  exports.handleSecretEjection = handleSecretEjection;
5
6
  const checkMinRequiredVersion_1 = require("../checkMinRequiredVersion");
6
7
  const command_1 = require("../command");
@@ -115,7 +116,7 @@ async function fnHandler(options) {
115
116
  await handleSecretEjection(options, instance);
116
117
  const convertedEnv = (0, export_1.functionsEnvFromInstance)(instance);
117
118
  const writeLocation = kitExportTarget(instanceId, projectId, options);
118
- if ((0, env_1.hasUserEnvs)(writeLocation)) {
119
+ if (hasNonEmptyProjectEnv(writeLocation)) {
119
120
  logger_1.logger.info(`Exported extensions config appears to already exist in /${instanceId}, aborting write.`);
120
121
  }
121
122
  else {
@@ -126,6 +127,22 @@ async function fnHandler(options) {
126
127
  (0, env_1.writeUserEnvs)(convertedEnv, writeLocation);
127
128
  }
128
129
  }
130
+ function hasNonEmptyProjectEnv(opts) {
131
+ const configDir = opts.configDir || opts.functionsSource;
132
+ const files = [
133
+ `.env.${opts.projectId}`,
134
+ ...(opts.projectAlias ? [`.env.${opts.projectAlias}`] : []),
135
+ ];
136
+ return files.some((f) => {
137
+ const fullPath = (0, path_1.join)(configDir, f);
138
+ try {
139
+ return (0, fs_1.statSync)(fullPath).size > 0;
140
+ }
141
+ catch {
142
+ return false;
143
+ }
144
+ });
145
+ }
129
146
  async function handleSecretEjection(options, instance) {
130
147
  const secrets = await (0, export_1.secretsNeedingEjection)(instance);
131
148
  if (secrets.length === 0) {
@@ -7,19 +7,34 @@ const command_1 = require("../command");
7
7
  const projectUtils_1 = require("../projectUtils");
8
8
  const extensionsHelper_1 = require("../extensions/extensionsHelper");
9
9
  const requirePermissions_1 = require("../requirePermissions");
10
+ const requireConfig_1 = require("../requireConfig");
10
11
  const migrate_1 = require("../extensions/migrate");
12
+ const export_1 = require("../extensions/export");
13
+ const install_1 = require("../functions/kits/install");
11
14
  const kits_1 = require("../functions/kits");
12
- const logger_1 = require("../logger");
13
15
  const utils_1 = require("../utils");
16
+ const error_1 = require("../error");
17
+ const deploy_1 = require("../deploy");
18
+ const checkIam_1 = require("../deploy/functions/checkIam");
19
+ const prompt_1 = require("../prompt");
14
20
  exports.command = new command_1.Command("ext:migrate")
15
21
  .description("migrate an extension instance to a function kit")
16
22
  .option("-p, --package <package>", "optional kit package override to use")
17
- .option("-i, --ext-instance <instanceId>", "extension instance ID to migrate")
23
+ .option("--ext-instance <instanceId>", "extension instance ID to migrate")
18
24
  .option("-e, --extension <extensionRef>", "extension reference or name to migrate")
19
25
  .option("-f, --force", "force update and migration without prompting")
20
- .before(requirePermissions_1.requirePermissions, ["firebaseextensions.instances.list"])
26
+ .before(requireConfig_1.requireConfig)
27
+ .before(requirePermissions_1.requirePermissions, [
28
+ "firebaseextensions.instances.list",
29
+ "firebaseextensions.instances.delete",
30
+ ...deploy_1.TARGET_PERMISSIONS["functions"],
31
+ ])
21
32
  .before(extensionsHelper_1.ensureExtensionsApiEnabled)
22
33
  .before(checkMinRequiredVersion_1.checkMinRequiredVersion, "extMinVersion")
34
+ .before((options) => {
35
+ const projectId = (0, projectUtils_1.needProjectId)(options);
36
+ return (0, checkIam_1.checkServiceAccountIam)(projectId);
37
+ })
23
38
  .action(async (options) => {
24
39
  const projectId = (0, projectUtils_1.needProjectId)(options);
25
40
  if (options.package) {
@@ -34,6 +49,54 @@ exports.command = new command_1.Command("ext:migrate")
34
49
  });
35
50
  (0, utils_1.logLabeledBullet)(extensionsHelper_1.logPrefix, `Selected instance ${clc.bold(plan.instanceId)} (${plan.kitPackage}) for migration.`);
36
51
  plan.instance = await (0, migrate_1.ensureInstanceUpToDate)(projectId, plan.instance, options);
37
- logger_1.logger.info("TODO: Draw the rest of the owl");
52
+ if (plan.instance.state !== "ACTIVE") {
53
+ (0, utils_1.logLabeledWarning)(extensionsHelper_1.logPrefix, `Extension instance ${clc.bold(plan.instanceId)} is in state ${plan.instance.state}. Migration may not function as expected.`);
54
+ }
55
+ plan.instance = await (0, extensionsHelper_1.ensureInstanceSpec)(plan.instance);
56
+ if (!plan.instance.config?.source?.spec) {
57
+ throw new error_1.FirebaseError(`Could not load extension specification for ${clc.bold(plan.instanceId)}. Unable to export configuration.`);
58
+ }
59
+ const exportedEnvs = (0, export_1.functionsEnvFromInstance)(plan.instance);
60
+ await (0, migrate_1.migrateSecrets)(plan.instance, { force: options.force });
61
+ (0, utils_1.logLabeledBullet)(extensionsHelper_1.logPrefix, `Installing kit ${clc.bold(plan.kitPackage)} for instance ${clc.bold(plan.instanceId)}...`);
62
+ const installResult = await (0, install_1.installKitOrInstance)({
63
+ ...options,
64
+ config: options.config,
65
+ package: plan.kitPackage,
66
+ template: "migration",
67
+ defaultInstanceId: plan.instanceId,
68
+ seedEnv: {
69
+ projectId,
70
+ envs: exportedEnvs,
71
+ },
72
+ });
73
+ const kitInstanceId = installResult.instanceId;
74
+ if (!kitInstanceId) {
75
+ throw new error_1.FirebaseError("Kit installation failed: no instance ID returned.", { exit: 1 });
76
+ }
77
+ (0, utils_1.logLabeledBullet)(extensionsHelper_1.logPrefix, `Deploying functions kit instance ${clc.bold(kitInstanceId)}...`);
78
+ const deployOptions = {
79
+ ...options,
80
+ only: `functions:${kitInstanceId}`,
81
+ filteredTargets: ["functions"],
82
+ project: projectId,
83
+ };
84
+ try {
85
+ await (0, deploy_1.deploy)(["functions"], deployOptions);
86
+ }
87
+ catch (err) {
88
+ throw new error_1.FirebaseError(`Failed to deploy. Please fix and retry with firebase deploy --only functions:${kitInstanceId}. After success you may remove the old extension with firebase ext:uninstall ${plan.instanceId} --project ${projectId} --immediate`, { original: err instanceof Error ? err : undefined, exit: 1 });
89
+ }
90
+ const shouldUninstall = await (0, prompt_1.confirm)({
91
+ message: `Functions kit ${kitInstanceId} successfully deployed. After checking function logs to verify that your backend is performing correctly, you should uninstall extension instance ${plan.instanceId}. Uninstall it now?`,
92
+ default: true,
93
+ nonInteractive: options.nonInteractive,
94
+ force: options.force,
95
+ });
96
+ if (!shouldUninstall) {
97
+ (0, utils_1.logLabeledBullet)(extensionsHelper_1.logPrefix, `You may safely uninstall extension instance ${clc.bold(plan.instanceId)} in the future with the command ${clc.bold(`firebase ext:uninstall ${plan.instanceId} --project ${projectId} --immediate`)}`);
98
+ return plan;
99
+ }
100
+ await (0, migrate_1.uninstallExtension)(projectId, plan.instanceId, options, true);
38
101
  return plan;
39
102
  });
@@ -7,10 +7,8 @@ const extensionsHelper_1 = require("../extensions/extensionsHelper");
7
7
  const requirePermissions_1 = require("../requirePermissions");
8
8
  const utils_1 = require("../utils");
9
9
  const manifest = require("../extensions/manifest");
10
- const extensionsApi_1 = require("../extensions/extensionsApi");
11
10
  const projectUtils_1 = require("../projectUtils");
12
- const prompt_1 = require("../prompt");
13
- const error_1 = require("../error");
11
+ const migrate_1 = require("../extensions/migrate");
14
12
  exports.command = new command_1.Command("ext:uninstall <extensionInstanceId>")
15
13
  .description("uninstall an extension that is installed in your Firebase project by instance ID")
16
14
  .option("--local", "deprecated")
@@ -26,31 +24,7 @@ exports.command = new command_1.Command("ext:uninstall <extensionInstanceId>")
26
24
  }
27
25
  if (options.immediate) {
28
26
  const projectId = (0, projectUtils_1.needProjectId)(options);
29
- let config;
30
- try {
31
- config = manifest.loadConfig(options);
32
- }
33
- catch {
34
- (0, utils_1.logLabeledBullet)(extensionsHelper_1.logPrefix, "No firebase.json found. Proceeding to immediate extension instance teardown.");
35
- }
36
- if (config && manifest.instanceExists(instanceId, config)) {
37
- manifest.removeFromManifest(instanceId, config);
38
- }
39
- if (!(await (0, prompt_1.confirm)({
40
- message: `About to delete Extensions instance ${projectId}/${instanceId}, its associated resources, and service account. Continue?`,
41
- nonInteractive: options.nonInteractive,
42
- force: options.force,
43
- default: true,
44
- }))) {
45
- return;
46
- }
47
- try {
48
- await (0, extensionsApi_1.deleteInstance)(projectId, instanceId);
49
- }
50
- catch (err) {
51
- throw new error_1.FirebaseError(`Error when attempting deletion: ${err instanceof Error ? err.message : String(err)}`, { original: err instanceof Error ? err : undefined });
52
- }
53
- (0, utils_1.logLabeledSuccess)(extensionsHelper_1.logPrefix, `Deleted Extensions instance ${projectId}/${instanceId}.`);
27
+ await (0, migrate_1.uninstallExtension)(projectId, instanceId, options, false);
54
28
  return;
55
29
  }
56
30
  const config = manifest.loadConfig(options);
@@ -5,8 +5,12 @@ const command_1 = require("../command");
5
5
  const error_1 = require("../error");
6
6
  const experiments = require("../experiments");
7
7
  const install_1 = require("../functions/kits/install");
8
+ const requireAuth_1 = require("../requireAuth");
9
+ const requireConfig_1 = require("../requireConfig");
8
10
  exports.command = new command_1.Command("functions:kits:install")
9
11
  .description("install a function kit into your project")
12
+ .before(requireConfig_1.requireConfig)
13
+ .before(requireAuth_1.requireAuth)
10
14
  .option("--package <package>", "NPM package name or specifier to install as a function kit")
11
15
  .option("--directory <directory>", "path to a local functions codebase directory to install as a function kit")
12
16
  .option(`--template <template>`, `template to use for the kit index file (${Object.keys(install_1.TEMPLATES).join("|")})`)
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.command = void 0;
4
4
  const requireConfig_1 = require("../requireConfig");
5
+ const requireAuth_1 = require("../requireAuth");
5
6
  const command_1 = require("../command");
6
7
  const config_1 = require("../functions/kits/config");
7
8
  const path_1 = require("path");
@@ -15,6 +16,7 @@ const delete_1 = require("../deploy/functions/delete");
15
16
  exports.command = new command_1.Command("functions:kits:uninstall")
16
17
  .description("uninstall a function kit or kit instance from your project")
17
18
  .before(requireConfig_1.requireConfig)
19
+ .before(requireAuth_1.requireAuth)
18
20
  .option("--kit <kitId>", "")
19
21
  .option("--instance <instanceId>", "")
20
22
  .action(async (options) => {
@@ -70,19 +72,15 @@ async function handleInstance(options, config) {
70
72
  const instanceConfigDirPath = kitForInstance.instances[instanceId];
71
73
  const conservativeDeletion = nonstandardKitLayout(kitForInstance);
72
74
  logger_1.logger.debug(`Kit ${kitForInstance.kit} uninstall mode: conservativeDeletion=${conservativeDeletion}`);
73
- const projectsWithConfigs = [];
74
75
  const configDirContents = config.lsProjectDir(instanceConfigDirPath);
75
- const fileNames = configDirContents.filter((f) => f.isFile()).map((f) => f.name);
76
- for (const fileName of fileNames) {
77
- if (!fileName.startsWith(".env.")) {
78
- continue;
79
- }
80
- projectsWithConfigs.push(fileName.slice(".env.".length));
81
- }
82
- if (projectsWithConfigs.length > 0 && !projectsWithConfigs.includes(projectId)) {
76
+ const envFileNames = configDirContents
77
+ .filter((f) => f.isFile() && f.name.startsWith(".env."))
78
+ .map((f) => f.name.slice(".env.".length));
79
+ const projectNamesInEnvs = envFileNames.map((envName) => options.rc ? options.rc.resolveAlias(envName) : envName);
80
+ if (projectNamesInEnvs.length > 0 && !projectNamesInEnvs.includes(projectId)) {
83
81
  throw new error_1.FirebaseError(`Instance at ${instanceConfigDirPath} contains no .env file for current project`);
84
82
  }
85
- if (projectsWithConfigs.length === 0 || projectsWithConfigs.length === 1) {
83
+ if (envFileNames.length === 0 || envFileNames.length === 1) {
86
84
  if (Object.keys(kitForInstance.instances).length > 1) {
87
85
  await uninstallInstance(options, config, instanceId, instanceConfigDirPath, conservativeDeletion);
88
86
  return;
@@ -99,7 +97,15 @@ async function handleInstance(options, config) {
99
97
  await uninstallKit(options, config, kitForInstance);
100
98
  return;
101
99
  }
102
- await uninstallProjectInstance(options, config, projectId, instanceId, instanceConfigDirPath);
100
+ let filesystemNameForProject = projectId;
101
+ const envFilesWhichAliasToProject = envFileNames.filter((name) => (options.rc?.resolveAlias(name) || name) === projectId);
102
+ if (envFilesWhichAliasToProject.length > 1) {
103
+ throw new error_1.FirebaseError(`Instance ${instanceId} contains multiple .env files which ambiguously resolve to the current project ${envFilesWhichAliasToProject.map((s) => ".env." + s).join(", ")}`);
104
+ }
105
+ if (envFilesWhichAliasToProject.length === 1) {
106
+ filesystemNameForProject = envFilesWhichAliasToProject[0];
107
+ }
108
+ await uninstallProjectInstance(options, config, filesystemNameForProject, instanceId, instanceConfigDirPath);
103
109
  }
104
110
  async function uninstallInstance(options, config, instanceId, instanceConfigDirPath, onlyDeleteEmpty) {
105
111
  const configDirContents = config.lsProjectDir(instanceConfigDirPath);
@@ -108,8 +114,8 @@ async function uninstallInstance(options, config, instanceId, instanceConfigDirP
108
114
  if (!fileName.startsWith(".env.")) {
109
115
  continue;
110
116
  }
111
- const projectId = fileName.replace(new RegExp("^.env."), "");
112
- await uninstallProjectInstance(options, config, projectId, instanceId, instanceConfigDirPath);
117
+ const projectIdOrAlias = fileName.slice(".env.".length);
118
+ await uninstallProjectInstance(options, config, projectIdOrAlias, instanceId, instanceConfigDirPath);
113
119
  }
114
120
  if (!onlyDeleteEmpty || configDirEmpty(config, instanceConfigDirPath)) {
115
121
  config.deleteProjectDir(instanceConfigDirPath);
@@ -132,8 +138,12 @@ async function uninstallInstance(options, config, instanceId, instanceConfigDirP
132
138
  config.set("functions", functionsConfig);
133
139
  config.writeProjectFile("firebase.json", config.src);
134
140
  }
135
- async function uninstallProjectInstance(options, config, projectId, instanceId, kitInstancePath) {
136
- const envFilePath = (0, path_1.join)(kitInstancePath, `.env.${projectId}`);
141
+ async function uninstallProjectInstance(options, config, envName, instanceId, kitInstancePath) {
142
+ const projectId = options.rc?.resolveAlias(envName) || envName;
143
+ const envFilePath = (0, path_1.join)(kitInstancePath, `.env.${envName}`);
144
+ if (!config.projectFileExists(envFilePath)) {
145
+ throw new error_1.FirebaseError(`Expected to clean up project kit instance .env file at ${envFilePath}, but it doesn't exist.`);
146
+ }
137
147
  const epFilters = [{ codebase: instanceId }];
138
148
  const deployContext = { projectId: projectId, filters: epFilters };
139
149
  const deletionCount = await (0, delete_1.deleteFunctionsByEndpointFilters)(deployContext, options);
@@ -8,33 +8,57 @@ const fabricator = require("./release/fabricator");
8
8
  const functionsDeployHelper_1 = require("./functionsDeployHelper");
9
9
  const getProjectNumber_1 = require("../../getProjectNumber");
10
10
  const reporter = require("./release/reporter");
11
- const error_1 = require("../../error");
12
- const functional_1 = require("../../functional");
13
- const prompt_1 = require("../../prompt");
11
+ const prompt = require("../../prompt");
14
12
  const functionsConfig = require("../../functionsConfig");
13
+ const error_1 = require("../../error");
14
+ const projectConfig_1 = require("../../functions/projectConfig");
15
15
  async function deleteFunctionsByEndpointFilters(context, options) {
16
- let haveBackend = await backend.existingBackend(context);
17
- if (options?.region) {
18
- haveBackend = backend.matchingBackend(haveBackend, (endpoint) => endpoint.region === options.region);
16
+ const allExistingBackend = await backend.existingBackend(context);
17
+ const targetedRegionBackend = options?.region
18
+ ? backend.matchingBackend(allExistingBackend, (endpoint) => endpoint.region === options.region)
19
+ : allExistingBackend;
20
+ const codebases = new Set(backend.allEndpoints(allExistingBackend).map((ep) => ep.codebase || projectConfig_1.DEFAULT_CODEBASE));
21
+ const deploymentPlan = {};
22
+ for (const codebase of codebases) {
23
+ const allCodebaseBackend = backend.matchingBackend(allExistingBackend, (ep) => (ep.codebase || projectConfig_1.DEFAULT_CODEBASE) === codebase);
24
+ const targetedCodebaseBackend = backend.matchingBackend(targetedRegionBackend, (ep) => (ep.codebase || projectConfig_1.DEFAULT_CODEBASE) === codebase);
25
+ const totalEndpointsInGcp = backend.allEndpoints(allCodebaseBackend);
26
+ const endpointsToDelete = backend
27
+ .allEndpoints(targetedCodebaseBackend)
28
+ .filter((e) => (0, functionsDeployHelper_1.endpointMatchesAnyFilter)(e, context.filters));
29
+ let existingManagedSA;
30
+ if (endpointsToDelete.length > 0 && endpointsToDelete.length === totalEndpointsInGcp.length) {
31
+ const ep = totalEndpointsInGcp.find((e) => typeof e.serviceAccount === "string" && e.serviceAccount.startsWith("firebase-fn-"));
32
+ existingManagedSA = ep?.serviceAccount;
33
+ }
34
+ deploymentPlan[codebase] = await planner.createDeploymentPlan({
35
+ wantBackend: backend.empty(),
36
+ haveBackend: targetedCodebaseBackend,
37
+ codebase,
38
+ projectId: context.projectId,
39
+ filters: context.filters,
40
+ deleteAll: true,
41
+ existingManagedSA,
42
+ });
19
43
  }
20
- const plan = await planner.createDeploymentPlan({
21
- wantBackend: backend.empty(),
22
- haveBackend: haveBackend,
23
- codebase: "",
24
- projectId: context.projectId,
25
- filters: context.filters,
26
- deleteAll: true,
27
- });
28
- const allEpToDelete = Object.values(plan.regionalChangesets)
29
- .map((changes) => changes.endpointsToDelete)
30
- .reduce(functional_1.reduceFlat, [])
44
+ const allEpToDelete = Object.values(deploymentPlan)
45
+ .flatMap((plan) => Object.values(plan.regionalChangesets).flatMap((changes) => changes.endpointsToDelete))
31
46
  .sort(backend.compareFunctions);
32
47
  if (allEpToDelete.length === 0) {
33
48
  return 0;
34
49
  }
35
50
  const deleteList = allEpToDelete.map((func) => `\t${(0, functionsDeployHelper_1.getFunctionLabel)(func)}`).join("\n");
36
- const confirmDeletion = await (0, prompt_1.confirm)({
37
- message: "You are about to delete the following Cloud Functions:\n" + deleteList + "\n Are you sure?",
51
+ const saToDelete = Object.values(deploymentPlan)
52
+ .map((p) => p.serviceAccountToDelete)
53
+ .filter((sa) => !!sa);
54
+ let message = "You are about to delete the following Cloud Functions:\n" + deleteList;
55
+ if (saToDelete.length > 0) {
56
+ const saList = saToDelete.map((sa) => `\t${sa}`).join("\n");
57
+ message += "\n\nThe following managed service accounts will also be deleted:\n" + saList;
58
+ }
59
+ message += "\n Are you sure?";
60
+ const confirmDeletion = await prompt.confirm({
61
+ message,
38
62
  default: false,
39
63
  force: options?.force,
40
64
  nonInteractive: options?.nonInteractive,
@@ -63,7 +87,7 @@ async function deleteFunctionsByEndpointFilters(context, options) {
63
87
  projectNumber: await (0, getProjectNumber_1.getProjectNumber)({ projectId: context.projectId }),
64
88
  projectId: context.projectId,
65
89
  });
66
- const summary = await fab.applyPlan({ default: plan });
90
+ const summary = await fab.applyPlan(deploymentPlan);
67
91
  await reporter.logAndTrackDeployStats(summary);
68
92
  reporter.printErrors(summary);
69
93
  if (summary.results.some((r) => r.error)) {
@@ -72,8 +96,9 @@ async function deleteFunctionsByEndpointFilters(context, options) {
72
96
  return allEpToDelete.length;
73
97
  }
74
98
  catch (err) {
75
- throw new error_1.FirebaseError(`Failed to delete functions: ${err}`, {
76
- original: err,
99
+ const message = err instanceof Error ? err.message : String(err);
100
+ throw new error_1.FirebaseError(`Failed to delete functions: ${message}`, {
101
+ original: err instanceof Error ? err : undefined,
77
102
  exit: 1,
78
103
  });
79
104
  }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.InlineExecutor = exports.QueueExecutor = exports.isCloudRunResourceExhausted = exports.isServiceAccount404 = exports.isTransientError = exports.isServiceUnavailable = exports.isConflict = exports.isQuotaExhaustion = void 0;
3
+ exports.InlineExecutor = exports.QueueExecutor = exports.isCloudRunResourceExhausted = exports.isServiceAccountPropagationError = exports.isTransientError = exports.isServiceUnavailable = exports.isConflict = exports.isQuotaExhaustion = void 0;
4
4
  exports.parseErrorCode = parseErrorCode;
5
5
  exports.hasErrorCode = hasErrorCode;
6
6
  const queue_1 = require("../../../throttler/queue");
@@ -22,8 +22,9 @@ const isServiceUnavailable = (err) => parseErrorCode(err) === 503;
22
22
  exports.isServiceUnavailable = isServiceUnavailable;
23
23
  const isTransientError = (err) => (0, exports.isQuotaExhaustion)(err) || (0, exports.isConflict)(err) || (0, exports.isServiceUnavailable)(err);
24
24
  exports.isTransientError = isTransientError;
25
- const isServiceAccount404 = (err) => {
26
- if (parseErrorCode(err) !== 404) {
25
+ const isServiceAccountPropagationError = (err) => {
26
+ const code = parseErrorCode(err);
27
+ if (code !== 404 && code !== 400) {
27
28
  return false;
28
29
  }
29
30
  let message = "";
@@ -32,6 +33,7 @@ const isServiceAccount404 = (err) => {
32
33
  err?.message,
33
34
  err?.original?.message,
34
35
  err?.context?.body?.error?.message,
36
+ err?.original?.context?.body?.error?.message,
35
37
  String(err),
36
38
  ]
37
39
  .filter(Boolean)
@@ -41,12 +43,16 @@ const isServiceAccount404 = (err) => {
41
43
  catch {
42
44
  message = String(err).toLowerCase();
43
45
  }
44
- return (message.includes("serviceaccount") ||
45
- message.includes("service account") ||
46
- /\b[a-z0-9._%+-]+@[a-z0-9.-]+\.gserviceaccount\.com\b/i.test(message) ||
47
- /\bgserviceaccount\.com\b/i.test(message));
46
+ const hasSa = message.includes("serviceaccount") || message.includes("service account");
47
+ if (!hasSa) {
48
+ return false;
49
+ }
50
+ if (code === 404) {
51
+ return true;
52
+ }
53
+ return message.includes("invalid service account") && /\bfirebase-fn-[0-9]+@/i.test(message);
48
54
  };
49
- exports.isServiceAccount404 = isServiceAccount404;
55
+ exports.isServiceAccountPropagationError = isServiceAccountPropagationError;
50
56
  const isCloudRunResourceExhausted = (err) => parseErrorCode(err) === 8;
51
57
  exports.isCloudRunResourceExhausted = isCloudRunResourceExhausted;
52
58
  async function handler(op) {
@@ -160,7 +160,7 @@ class Fabricator {
160
160
  secret,
161
161
  serviceAccounts,
162
162
  }), {
163
- retryPredicates: [executor_1.isTransientError, executor_1.isServiceAccount404],
163
+ retryPredicates: [executor_1.isTransientError, executor_1.isServiceAccountPropagationError],
164
164
  })));
165
165
  await Promise.all(secretAccessPromises);
166
166
  const allChangesets = [];
@@ -325,16 +325,19 @@ class Fabricator {
325
325
  apiFunction.httpsTrigger.securityLevel = "SECURE_ALWAYS";
326
326
  }
327
327
  const resultFunction = await this.functionExecutor
328
- .run(async () => {
329
- apiFunction.sourceToken = await scraper.getToken();
328
+ .run(() => scraper.withToken(async (token) => {
329
+ delete apiFunction.sourceToken;
330
+ if (token) {
331
+ apiFunction.sourceToken = token;
332
+ }
330
333
  const op = await gcf.createFunction(apiFunction);
331
- return poller.pollOperation({
334
+ return await poller.pollOperation({
332
335
  ...gcfV1PollerOptions,
333
336
  pollerName: `create-${endpoint.codebase}-${endpoint.region}-${endpoint.id}`,
334
337
  operationResourceName: op.name,
335
338
  onPoll: scraper.poller,
336
339
  });
337
- }, { retryPredicates: [executor_1.isTransientError, executor_1.isServiceAccount404] })
340
+ }), { retryPredicates: [executor_1.isTransientError, executor_1.isServiceAccountPropagationError] })
338
341
  .catch(rethrowAs(endpoint, "create"));
339
342
  endpoint.uri = resultFunction?.httpsTrigger?.url;
340
343
  if (backend.isHttpsTriggered(endpoint)) {
@@ -429,9 +432,12 @@ class Fabricator {
429
432
  let resultFunction = null;
430
433
  while (!resultFunction) {
431
434
  resultFunction = await this.functionExecutor
432
- .run(async () => {
433
- if (experiments.isEnabled("functionsv2deployoptimizations")) {
434
- apiFunction.buildConfig.sourceToken = await scraper.getToken();
435
+ .run(() => scraper.withToken(async (token) => {
436
+ if (apiFunction.buildConfig) {
437
+ delete apiFunction.buildConfig.sourceToken;
438
+ if (experiments.isEnabled("functionsv2deployoptimizations") && token) {
439
+ apiFunction.buildConfig.sourceToken = token;
440
+ }
435
441
  }
436
442
  const op = await gcfV2.createFunction(apiFunction);
437
443
  return await poller.pollOperation({
@@ -440,9 +446,8 @@ class Fabricator {
440
446
  operationResourceName: op.name,
441
447
  onPoll: scraper.poller,
442
448
  });
443
- }, { retryPredicates: [executor_1.isTransientError, executor_1.isServiceAccount404] })
449
+ }), { retryPredicates: [executor_1.isTransientError, executor_1.isServiceAccountPropagationError] })
444
450
  .catch(async (err) => {
445
- scraper.abort();
446
451
  if (err.code === CLOUD_RUN_RESOURCE_EXHAUSTED_CODE) {
447
452
  await this.deleteV2Function(endpoint);
448
453
  return null;
@@ -516,8 +521,11 @@ class Fabricator {
516
521
  }
517
522
  const apiFunction = gcf.functionFromEndpoint(endpoint, sourceUrl);
518
523
  const resultFunction = await this.functionExecutor
519
- .run(async () => {
520
- apiFunction.sourceToken = await scraper.getToken();
524
+ .run(() => scraper.withToken(async (token) => {
525
+ delete apiFunction.sourceToken;
526
+ if (token) {
527
+ apiFunction.sourceToken = token;
528
+ }
521
529
  const op = await gcf.updateFunction(apiFunction);
522
530
  return await poller.pollOperation({
523
531
  ...gcfV1PollerOptions,
@@ -525,7 +533,7 @@ class Fabricator {
525
533
  operationResourceName: op.name,
526
534
  onPoll: scraper.poller,
527
535
  });
528
- })
536
+ }), { retryPredicates: [executor_1.isTransientError, executor_1.isServiceAccountPropagationError] })
529
537
  .catch(rethrowAs(endpoint, "update"));
530
538
  endpoint.uri = resultFunction?.httpsTrigger?.url;
531
539
  let invoker;
@@ -556,9 +564,12 @@ class Fabricator {
556
564
  delete apiFunction.eventTrigger.pubsubTopic;
557
565
  }
558
566
  const resultFunction = await this.functionExecutor
559
- .run(async () => {
560
- if (experiments.isEnabled("functionsv2deployoptimizations")) {
561
- apiFunction.buildConfig.sourceToken = await scraper.getToken();
567
+ .run(() => scraper.withToken(async (token) => {
568
+ if (apiFunction.buildConfig) {
569
+ delete apiFunction.buildConfig.sourceToken;
570
+ if (experiments.isEnabled("functionsv2deployoptimizations") && token) {
571
+ apiFunction.buildConfig.sourceToken = token;
572
+ }
562
573
  }
563
574
  const op = await gcfV2.updateFunction(apiFunction);
564
575
  return await poller.pollOperation({
@@ -567,9 +578,14 @@ class Fabricator {
567
578
  operationResourceName: op.name,
568
579
  onPoll: scraper.poller,
569
580
  });
570
- }, { retryPredicates: [executor_1.isTransientError, executor_1.isCloudRunResourceExhausted, executor_1.isServiceAccount404] })
581
+ }), {
582
+ retryPredicates: [
583
+ executor_1.isTransientError,
584
+ executor_1.isCloudRunResourceExhausted,
585
+ executor_1.isServiceAccountPropagationError,
586
+ ],
587
+ })
571
588
  .catch((err) => {
572
- scraper.abort();
573
589
  logger_1.logger.error(err.message);
574
590
  throw new reporter.DeploymentError(endpoint, "update", err);
575
591
  });
@@ -637,7 +653,13 @@ class Fabricator {
637
653
  operationResourceName: op.name,
638
654
  };
639
655
  await poller.pollOperation(pollerOptions);
640
- }, { retryPredicates: [executor_1.isTransientError, executor_1.isCloudRunResourceExhausted, executor_1.isServiceAccount404] })
656
+ }, {
657
+ retryPredicates: [
658
+ executor_1.isTransientError,
659
+ executor_1.isCloudRunResourceExhausted,
660
+ executor_1.isServiceAccountPropagationError,
661
+ ],
662
+ })
641
663
  .catch(rethrowAs(endpoint, "delete"));
642
664
  }
643
665
  async createRunFunction(endpoint) {
@@ -660,7 +682,7 @@ class Fabricator {
660
682
  const op = await runV2.createService(endpoint.project, endpoint.region, endpoint.id, service);
661
683
  endpoint.uri = op.uri;
662
684
  endpoint.runServiceId = endpoint.id;
663
- }, { retryPredicates: [executor_1.isTransientError, executor_1.isServiceAccount404] })
685
+ }, { retryPredicates: [executor_1.isTransientError, executor_1.isServiceAccountPropagationError] })
664
686
  .catch(rethrowAs(endpoint, "create"));
665
687
  const serviceName = `projects/${endpoint.project}/locations/${endpoint.region}/services/${endpoint.runServiceId}`;
666
688
  if (backend.isHttpsTriggered(endpoint)) {
@@ -10,8 +10,23 @@ class SourceTokenScraper {
10
10
  this.promise = new Promise((resolve) => (this.resolve = resolve));
11
11
  this.fetchState = "NONE";
12
12
  }
13
+ async withToken(fn) {
14
+ const token = await this.getToken();
15
+ try {
16
+ return await fn(token);
17
+ }
18
+ catch (err) {
19
+ this.abort();
20
+ throw err;
21
+ }
22
+ }
13
23
  abort() {
14
- this.resolve({ aborted: true });
24
+ if (this.fetchState === "FETCHING") {
25
+ this.fetchState = "NONE";
26
+ const resolve = this.resolve;
27
+ this.promise = new Promise((r) => (this.resolve = r));
28
+ resolve({ aborted: true });
29
+ }
15
30
  }
16
31
  async getToken() {
17
32
  if (this.fetchState === "NONE") {
@@ -21,7 +36,6 @@ class SourceTokenScraper {
21
36
  else if (this.fetchState === "FETCHING") {
22
37
  const tokenResult = await this.promise;
23
38
  if (tokenResult.aborted) {
24
- this.promise = new Promise((resolve) => (this.resolve = resolve));
25
39
  return undefined;
26
40
  }
27
41
  return tokenResult.token;