firebase-tools 15.29.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.
@@ -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);
@@ -72,19 +72,15 @@ async function handleInstance(options, config) {
72
72
  const instanceConfigDirPath = kitForInstance.instances[instanceId];
73
73
  const conservativeDeletion = nonstandardKitLayout(kitForInstance);
74
74
  logger_1.logger.debug(`Kit ${kitForInstance.kit} uninstall mode: conservativeDeletion=${conservativeDeletion}`);
75
- const projectsWithConfigs = [];
76
75
  const configDirContents = config.lsProjectDir(instanceConfigDirPath);
77
- const fileNames = configDirContents.filter((f) => f.isFile()).map((f) => f.name);
78
- for (const fileName of fileNames) {
79
- if (!fileName.startsWith(".env.")) {
80
- continue;
81
- }
82
- projectsWithConfigs.push(fileName.slice(".env.".length));
83
- }
84
- 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)) {
85
81
  throw new error_1.FirebaseError(`Instance at ${instanceConfigDirPath} contains no .env file for current project`);
86
82
  }
87
- if (projectsWithConfigs.length === 0 || projectsWithConfigs.length === 1) {
83
+ if (envFileNames.length === 0 || envFileNames.length === 1) {
88
84
  if (Object.keys(kitForInstance.instances).length > 1) {
89
85
  await uninstallInstance(options, config, instanceId, instanceConfigDirPath, conservativeDeletion);
90
86
  return;
@@ -101,7 +97,15 @@ async function handleInstance(options, config) {
101
97
  await uninstallKit(options, config, kitForInstance);
102
98
  return;
103
99
  }
104
- 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);
105
109
  }
106
110
  async function uninstallInstance(options, config, instanceId, instanceConfigDirPath, onlyDeleteEmpty) {
107
111
  const configDirContents = config.lsProjectDir(instanceConfigDirPath);
@@ -110,8 +114,8 @@ async function uninstallInstance(options, config, instanceId, instanceConfigDirP
110
114
  if (!fileName.startsWith(".env.")) {
111
115
  continue;
112
116
  }
113
- const projectId = fileName.replace(new RegExp("^.env."), "");
114
- await uninstallProjectInstance(options, config, projectId, instanceId, instanceConfigDirPath);
117
+ const projectIdOrAlias = fileName.slice(".env.".length);
118
+ await uninstallProjectInstance(options, config, projectIdOrAlias, instanceId, instanceConfigDirPath);
115
119
  }
116
120
  if (!onlyDeleteEmpty || configDirEmpty(config, instanceConfigDirPath)) {
117
121
  config.deleteProjectDir(instanceConfigDirPath);
@@ -134,8 +138,12 @@ async function uninstallInstance(options, config, instanceId, instanceConfigDirP
134
138
  config.set("functions", functionsConfig);
135
139
  config.writeProjectFile("firebase.json", config.src);
136
140
  }
137
- async function uninstallProjectInstance(options, config, projectId, instanceId, kitInstancePath) {
138
- 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
+ }
139
147
  const epFilters = [{ codebase: instanceId }];
140
148
  const deployContext = { projectId: projectId, filters: epFilters };
141
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;
@@ -54,36 +54,36 @@
54
54
  },
55
55
  "dataconnect": {
56
56
  "darwin": {
57
- "version": "3.4.18",
58
- "expectedSize": 33375408,
59
- "expectedChecksum": "e38f131497fc43df55c3a47ee87e7fd4",
60
- "expectedChecksumSHA256": "52518fefdd2e52581b0d8d035c6498f304ac493d3757f4fd35851b1f028a9460",
61
- "remoteUrl": "https://storage.googleapis.com/firemat-preview-drop/emulator/dataconnect-emulator-macos-amd64-v3.4.18",
62
- "downloadPathRelativeToCacheDir": "dataconnect-emulator-3.4.18"
57
+ "version": "3.4.19",
58
+ "expectedSize": 33412928,
59
+ "expectedChecksum": "5a65cd626d5c022eab4c2036b18f0314",
60
+ "expectedChecksumSHA256": "4a63243e720d26de7e2e5bd5060fc92768f09446ec42b3369fb6ddc06a9cdddf",
61
+ "remoteUrl": "https://storage.googleapis.com/firemat-preview-drop/emulator/dataconnect-emulator-macos-amd64-v3.4.19",
62
+ "downloadPathRelativeToCacheDir": "dataconnect-emulator-3.4.19"
63
63
  },
64
64
  "darwin_arm64": {
65
- "version": "3.4.18",
66
- "expectedSize": 31505922,
67
- "expectedChecksum": "6a7cfdd68a7671bb660eb8489372a890",
68
- "expectedChecksumSHA256": "96cdec1fd6561af5c10f915f6f3bfd59a7405bf5e9b8fabfedb40cd57d6d5d76",
69
- "remoteUrl": "https://storage.googleapis.com/firemat-preview-drop/emulator/dataconnect-emulator-macos-arm64-v3.4.18",
70
- "downloadPathRelativeToCacheDir": "dataconnect-emulator-3.4.18"
65
+ "version": "3.4.19",
66
+ "expectedSize": 31539602,
67
+ "expectedChecksum": "cd914718f705a9e614bc3024f443bb52",
68
+ "expectedChecksumSHA256": "dafb2e532c29d876cb30d5c2bf32b2e50a614386dc16ba82b0ed6a9fca2a6db3",
69
+ "remoteUrl": "https://storage.googleapis.com/firemat-preview-drop/emulator/dataconnect-emulator-macos-arm64-v3.4.19",
70
+ "downloadPathRelativeToCacheDir": "dataconnect-emulator-3.4.19"
71
71
  },
72
72
  "win32": {
73
- "version": "3.4.18",
74
- "expectedSize": 33414144,
75
- "expectedChecksum": "d2504c643ac8355b90aa31be7575807c",
76
- "expectedChecksumSHA256": "a92f2d01950af613d28b92c2cd7baa85e7629fa201b8ff8dddce73d5584dccc5",
77
- "remoteUrl": "https://storage.googleapis.com/firemat-preview-drop/emulator/dataconnect-emulator-windows-amd64-v3.4.18",
78
- "downloadPathRelativeToCacheDir": "dataconnect-emulator-3.4.18.exe"
73
+ "version": "3.4.19",
74
+ "expectedSize": 33444864,
75
+ "expectedChecksum": "e2f84df8d6c536bea29d2ccc4cb21e79",
76
+ "expectedChecksumSHA256": "756e370334ef982547e7252b3f91fd75b35f85c5d3666e8f3773a4a19a00c8f8",
77
+ "remoteUrl": "https://storage.googleapis.com/firemat-preview-drop/emulator/dataconnect-emulator-windows-amd64-v3.4.19",
78
+ "downloadPathRelativeToCacheDir": "dataconnect-emulator-3.4.19.exe"
79
79
  },
80
80
  "linux": {
81
- "version": "3.4.18",
82
- "expectedSize": 32510136,
83
- "expectedChecksum": "c8b4640f3ea59c813a708814bd2c0f8c",
84
- "expectedChecksumSHA256": "bb381de594485653812bee518ddae025c13e1050670d6fa186f6003c3c9b71ec",
85
- "remoteUrl": "https://storage.googleapis.com/firemat-preview-drop/emulator/dataconnect-emulator-linux-amd64-v3.4.18",
86
- "downloadPathRelativeToCacheDir": "dataconnect-emulator-3.4.18"
81
+ "version": "3.4.19",
82
+ "expectedSize": 32542904,
83
+ "expectedChecksum": "63f099c9b9dad6e7d2adddae8af884a3",
84
+ "expectedChecksumSHA256": "bc74b68cc4bafcccc57dbe659a74b4403c225f01f851b5a4a61dcea81e8b1310",
85
+ "remoteUrl": "https://storage.googleapis.com/firemat-preview-drop/emulator/dataconnect-emulator-linux-amd64-v3.4.19",
86
+ "downloadPathRelativeToCacheDir": "dataconnect-emulator-3.4.19"
87
87
  }
88
88
  }
89
89
  }
@@ -116,8 +116,8 @@ class TasksEmulator {
116
116
  const queueName = req.params.queue_name;
117
117
  if (!this.validateQueueId(queueName)) {
118
118
  res.status(400).json({
119
- error: "Queue ID must start with a letter followed by up to 62 letters, numbers, " +
120
- "hyphens, or underscores and must end with a letter or a number",
119
+ error: "Queue ID can contain only letters ([A-Za-z]), numbers ([0-9]), or hyphens (-). Queue ID " +
120
+ "must be between 1 and 100 characters.",
121
121
  });
122
122
  return;
123
123
  }