firebase-tools 10.4.1 → 10.6.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.
Files changed (57) hide show
  1. package/lib/bin/firebase.js +1 -1
  2. package/lib/commands/emulators-start.js +6 -1
  3. package/lib/commands/ext-configure.js +4 -4
  4. package/lib/commands/ext-dev-emulators-start.js +5 -1
  5. package/lib/commands/ext-install.js +5 -4
  6. package/lib/commands/ext-update.js +2 -1
  7. package/lib/commands/functions-secrets-destroy.js +23 -3
  8. package/lib/commands/functions-secrets-prune.js +15 -12
  9. package/lib/commands/functions-secrets-set.js +51 -4
  10. package/lib/deploy/functions/backend.js +1 -5
  11. package/lib/deploy/functions/checkIam.js +44 -1
  12. package/lib/deploy/functions/prepare.js +13 -3
  13. package/lib/deploy/functions/release/fabricator.js +1 -3
  14. package/lib/deploy/functions/release/index.js +21 -0
  15. package/lib/deploy/functions/release/planner.js +1 -2
  16. package/lib/deploy/functions/runtimes/discovery/v1alpha1.js +11 -10
  17. package/lib/deploy/functions/runtimes/node/index.js +1 -1
  18. package/lib/deploy/functions/runtimes/node/parseTriggers.js +5 -19
  19. package/lib/deploy/functions/services/firebaseAlerts.js +1 -17
  20. package/lib/deploy/functions/services/index.js +2 -1
  21. package/lib/deploy/functions/services/storage.js +1 -6
  22. package/lib/emulator/auth/operations.js +21 -20
  23. package/lib/emulator/auth/state.js +79 -43
  24. package/lib/emulator/commandUtils.js +72 -2
  25. package/lib/emulator/controller.js +9 -0
  26. package/lib/emulator/downloadableEmulators.js +13 -6
  27. package/lib/emulator/extensions/postinstall.js +41 -0
  28. package/lib/emulator/functionsEmulator.js +8 -18
  29. package/lib/emulator/functionsEmulatorShared.js +41 -19
  30. package/lib/emulator/shared/request.js +19 -0
  31. package/lib/emulator/storage/apis/firebase.js +25 -33
  32. package/lib/emulator/storage/apis/gcloud.js +78 -63
  33. package/lib/emulator/storage/files.js +48 -52
  34. package/lib/emulator/storage/index.js +23 -3
  35. package/lib/emulator/storage/metadata.js +18 -8
  36. package/lib/emulator/storage/rules/manager.js +7 -17
  37. package/lib/emulator/storage/rules/utils.js +11 -3
  38. package/lib/emulator/storage/server.js +38 -12
  39. package/lib/extensions/askUserForParam.js +25 -20
  40. package/lib/extensions/emulator/optionsHelper.js +5 -7
  41. package/lib/extensions/emulator/triggerHelper.js +11 -14
  42. package/lib/extensions/extensionsApi.js +2 -1
  43. package/lib/extensions/extensionsHelper.js +11 -2
  44. package/lib/extensions/manifest.js +1 -1
  45. package/lib/extensions/paramHelper.js +23 -13
  46. package/lib/functions/env.js +10 -2
  47. package/lib/functions/runtimeConfigExport.js +10 -6
  48. package/lib/functions/secrets.js +99 -6
  49. package/lib/gcp/cloudfunctions.js +6 -13
  50. package/lib/gcp/cloudfunctionsv2.js +14 -23
  51. package/lib/gcp/cloudtasks.js +5 -3
  52. package/lib/gcp/secretManager.js +1 -1
  53. package/lib/requirePermissions.js +4 -1
  54. package/lib/utils.js +30 -1
  55. package/npm-shrinkwrap.json +2 -2
  56. package/package.json +1 -1
  57. package/lib/emulator/storage/list.js +0 -18
@@ -9,12 +9,10 @@ var __asyncValues = (this && this.__asyncValues) || function (o) {
9
9
  Object.defineProperty(exports, "__esModule", { value: true });
10
10
  exports.StorageLayer = exports.StoredFile = void 0;
11
11
  const fs_1 = require("fs");
12
- const list_1 = require("./list");
13
12
  const metadata_1 = require("./metadata");
14
13
  const errors_1 = require("./errors");
15
14
  const path = require("path");
16
15
  const fse = require("fs-extra");
17
- const cloudFunctions_1 = require("./cloudFunctions");
18
16
  const logger_1 = require("../../logger");
19
17
  const adminSdkConfig_1 = require("../adminSdkConfig");
20
18
  const types_1 = require("./rules/types");
@@ -39,17 +37,14 @@ class StoredFile {
39
37
  }
40
38
  exports.StoredFile = StoredFile;
41
39
  class StorageLayer {
42
- constructor(_projectId, _rulesValidator, _adminCredsValidator, _persistence) {
40
+ constructor(_projectId, _files, _buckets, _rulesValidator, _adminCredsValidator, _persistence, _cloudFunctions) {
43
41
  this._projectId = _projectId;
42
+ this._files = _files;
43
+ this._buckets = _buckets;
44
44
  this._rulesValidator = _rulesValidator;
45
45
  this._adminCredsValidator = _adminCredsValidator;
46
46
  this._persistence = _persistence;
47
- this.reset();
48
- this._cloudFunctions = new cloudFunctions_1.StorageCloudFunctions(this._projectId);
49
- }
50
- reset() {
51
- this._files = new Map();
52
- this._buckets = new Map();
47
+ this._cloudFunctions = _cloudFunctions;
53
48
  }
54
49
  createBucket(id) {
55
50
  if (!this._buckets.has(id)) {
@@ -66,11 +61,11 @@ class StorageLayer {
66
61
  }
67
62
  return [...this._buckets.values()];
68
63
  }
69
- async handleGetObject(request, skipAuth = false) {
64
+ async getObject(request) {
70
65
  var _a;
71
66
  const metadata = this.getMetadata(request.bucketId, request.decodedObjectId);
72
67
  const hasValidDownloadToken = ((metadata === null || metadata === void 0 ? void 0 : metadata.downloadTokens) || []).includes((_a = request.downloadToken) !== null && _a !== void 0 ? _a : "");
73
- let authorized = skipAuth || hasValidDownloadToken;
68
+ let authorized = hasValidDownloadToken;
74
69
  if (!authorized) {
75
70
  authorized = await this._rulesValidator.validate(["b", request.bucketId, "o", request.decodedObjectId].join("/"), request.bucketId, types_1.RulesetOperationMethod.GET, { before: metadata === null || metadata === void 0 ? void 0 : metadata.asRulesResource() }, request.authorization);
76
71
  }
@@ -99,10 +94,9 @@ class StorageLayer {
99
94
  }
100
95
  return undefined;
101
96
  }
102
- async handleDeleteObject(request, skipAuth = false) {
97
+ async deleteObject(request) {
103
98
  const storedMetadata = this.getMetadata(request.bucketId, request.decodedObjectId);
104
- const authorized = skipAuth ||
105
- (await this._rulesValidator.validate(["b", request.bucketId, "o", request.decodedObjectId].join("/"), request.bucketId, types_1.RulesetOperationMethod.DELETE, { before: storedMetadata === null || storedMetadata === void 0 ? void 0 : storedMetadata.asRulesResource() }, request.authorization));
99
+ const authorized = await this._rulesValidator.validate(["b", request.bucketId, "o", request.decodedObjectId].join("/"), request.bucketId, types_1.RulesetOperationMethod.DELETE, { before: storedMetadata === null || storedMetadata === void 0 ? void 0 : storedMetadata.asRulesResource() }, request.authorization);
106
100
  if (!authorized) {
107
101
  throw new errors_1.ForbiddenError();
108
102
  }
@@ -131,13 +125,12 @@ class StorageLayer {
131
125
  return true;
132
126
  }
133
127
  }
134
- async handleUpdateObjectMetadata(request, skipAuth = false) {
128
+ async updateObjectMetadata(request) {
135
129
  const storedMetadata = this.getMetadata(request.bucketId, request.decodedObjectId);
136
- const authorized = skipAuth ||
137
- (await this._rulesValidator.validate(["b", request.bucketId, "o", request.decodedObjectId].join("/"), request.bucketId, types_1.RulesetOperationMethod.UPDATE, {
138
- before: storedMetadata === null || storedMetadata === void 0 ? void 0 : storedMetadata.asRulesResource(),
139
- after: storedMetadata === null || storedMetadata === void 0 ? void 0 : storedMetadata.asRulesResource(request.metadata),
140
- }, request.authorization));
130
+ const authorized = await this._rulesValidator.validate(["b", request.bucketId, "o", request.decodedObjectId].join("/"), request.bucketId, types_1.RulesetOperationMethod.UPDATE, {
131
+ before: storedMetadata === null || storedMetadata === void 0 ? void 0 : storedMetadata.asRulesResource(),
132
+ after: storedMetadata === null || storedMetadata === void 0 ? void 0 : storedMetadata.asRulesResource(request.metadata),
133
+ }, request.authorization);
141
134
  if (!authorized) {
142
135
  throw new errors_1.ForbiddenError();
143
136
  }
@@ -147,7 +140,7 @@ class StorageLayer {
147
140
  storedMetadata.update(request.metadata);
148
141
  return storedMetadata;
149
142
  }
150
- async handleUploadObject(upload, skipAuth = false) {
143
+ async uploadObject(upload) {
151
144
  if (upload.status !== upload_1.UploadStatus.FINISHED) {
152
145
  throw new Error(`Unexpected upload status encountered: ${upload.status}.`);
153
146
  }
@@ -162,8 +155,8 @@ class StorageLayer {
162
155
  cacheControl: upload.metadata.cacheControl,
163
156
  customMetadata: upload.metadata.metadata,
164
157
  }, this._cloudFunctions, this._persistence.readBytes(upload.path, upload.size));
165
- const authorized = skipAuth ||
166
- (await this._rulesValidator.validate(["b", upload.bucketId, "o", upload.objectId].join("/"), upload.bucketId, types_1.RulesetOperationMethod.CREATE, { after: metadata === null || metadata === void 0 ? void 0 : metadata.asRulesResource() }, upload.authorization));
158
+ metadata.update(upload.metadata, false);
159
+ const authorized = await this._rulesValidator.validate(["b", upload.bucketId, "o", upload.objectId].join("/"), upload.bucketId, types_1.RulesetOperationMethod.CREATE, { after: metadata === null || metadata === void 0 ? void 0 : metadata.asRulesResource() }, upload.authorization);
167
160
  if (!authorized) {
168
161
  this._persistence.deleteFile(upload.path);
169
162
  throw new errors_1.ForbiddenError();
@@ -174,17 +167,24 @@ class StorageLayer {
174
167
  this._cloudFunctions.dispatch("finalize", new metadata_1.CloudStorageObjectMetadata(metadata));
175
168
  return metadata;
176
169
  }
177
- copyFile(sourceFile, destinationBucket, destinationObject, incomingMetadata) {
178
- const filePath = this.path(destinationBucket, destinationObject);
179
- this._persistence.deleteFile(filePath, true);
180
- const bytes = this.getBytes(sourceFile.bucket, sourceFile.name);
181
- this._persistence.appendBytes(filePath, bytes);
182
- const newMetadata = Object.assign(Object.assign(Object.assign({}, sourceFile), { metadata: sourceFile.customMetadata }), incomingMetadata);
183
- if (sourceFile.downloadTokens.length &&
170
+ copyObject({ sourceBucket, sourceObject, destinationBucket, destinationObject, incomingMetadata, authorization, }) {
171
+ if (!this._adminCredsValidator.validate(authorization)) {
172
+ throw new errors_1.ForbiddenError();
173
+ }
174
+ const sourceMetadata = this.getMetadata(sourceBucket, sourceObject);
175
+ if (!sourceMetadata) {
176
+ throw new errors_1.NotFoundError();
177
+ }
178
+ const sourceBytes = this.getBytes(sourceBucket, sourceObject);
179
+ const destinationFilePath = this.path(destinationBucket, destinationObject);
180
+ this._persistence.deleteFile(destinationFilePath, true);
181
+ this._persistence.appendBytes(destinationFilePath, sourceBytes);
182
+ const newMetadata = Object.assign(Object.assign(Object.assign({}, sourceMetadata), { metadata: sourceMetadata.customMetadata }), incomingMetadata);
183
+ if (sourceMetadata.downloadTokens.length &&
184
184
  !((incomingMetadata === null || incomingMetadata === void 0 ? void 0 : incomingMetadata.metadata) && Object.keys(incomingMetadata === null || incomingMetadata === void 0 ? void 0 : incomingMetadata.metadata).length)) {
185
185
  if (!newMetadata.metadata)
186
186
  newMetadata.metadata = {};
187
- newMetadata.metadata.firebaseStorageDownloadTokens = sourceFile.downloadTokens.join(",");
187
+ newMetadata.metadata.firebaseStorageDownloadTokens = sourceMetadata.downloadTokens.join(",");
188
188
  }
189
189
  if (newMetadata.metadata) {
190
190
  for (const [k, v] of Object.entries(newMetadata.metadata)) {
@@ -201,27 +201,23 @@ class StorageLayer {
201
201
  contentLanguage: newMetadata.contentLanguage,
202
202
  cacheControl: newMetadata.cacheControl,
203
203
  customMetadata: newMetadata.metadata,
204
- }, this._cloudFunctions, bytes, incomingMetadata);
205
- const file = new StoredFile(copiedFileMetadata, this._persistence.getDiskPath(filePath));
206
- this._files.set(filePath, file);
204
+ }, this._cloudFunctions, sourceBytes, incomingMetadata);
205
+ const file = new StoredFile(copiedFileMetadata, this._persistence.getDiskPath(destinationFilePath));
206
+ this._files.set(destinationFilePath, file);
207
207
  this._cloudFunctions.dispatch("finalize", new metadata_1.CloudStorageObjectMetadata(file.metadata));
208
208
  return file.metadata;
209
209
  }
210
- async handleListObjects(request, skipAuth = false) {
211
- var _a, _b, _c;
212
- const authorized = skipAuth ||
213
- (await this._rulesValidator.validate(["b", request.bucketId, "o", request.prefix].join("/"), request.bucketId, types_1.RulesetOperationMethod.LIST, {}, request.authorization));
210
+ async listObjects(request) {
211
+ var _a;
212
+ const { bucketId, prefix, delimiter, pageToken, authorization } = request;
213
+ const authorized = await this._rulesValidator.validate(["b", bucketId, "o", prefix].join("/"), bucketId, types_1.RulesetOperationMethod.LIST, {}, authorization);
214
214
  if (!authorized) {
215
215
  throw new errors_1.ForbiddenError();
216
216
  }
217
- const itemsResults = this.listItems(request.bucketId, request.prefix, request.delimiter, request.pageToken, request.maxResults);
218
- return new list_1.ListResponse((_a = itemsResults.prefixes) !== null && _a !== void 0 ? _a : [], (_c = (_b = itemsResults.items) === null || _b === void 0 ? void 0 : _b.map((i) => new list_1.ListItem(i.name, i.bucket))) !== null && _c !== void 0 ? _c : [], itemsResults.nextPageToken);
219
- }
220
- listItems(bucket, prefix, delimiter, pageToken, maxResults) {
221
217
  let items = [];
222
218
  const prefixes = new Set();
223
219
  for (const [, file] of this._files) {
224
- if (file.metadata.bucket !== bucket) {
220
+ if (file.metadata.bucket !== bucketId) {
225
221
  continue;
226
222
  }
227
223
  const name = file.metadata.name;
@@ -258,9 +254,7 @@ class StorageLayer {
258
254
  items = items.slice(idx);
259
255
  }
260
256
  }
261
- if (!maxResults) {
262
- maxResults = 1000;
263
- }
257
+ const maxResults = (_a = request.maxResults) !== null && _a !== void 0 ? _a : 1000;
264
258
  let nextPageToken = undefined;
265
259
  if (items.length > maxResults) {
266
260
  nextPageToken = items[maxResults].name;
@@ -269,10 +263,10 @@ class StorageLayer {
269
263
  return {
270
264
  nextPageToken,
271
265
  prefixes: prefixes.size > 0 ? [...prefixes].sort() : undefined,
272
- items: items.length > 0 ? items.map((item) => new metadata_1.CloudStorageObjectMetadata(item)) : undefined,
266
+ items: items.length > 0 ? items : undefined,
273
267
  };
274
268
  }
275
- handleCreateDownloadToken(request) {
269
+ createDownloadToken(request) {
276
270
  if (!this._adminCredsValidator.validate(request.authorization)) {
277
271
  throw new errors_1.ForbiddenError();
278
272
  }
@@ -283,7 +277,7 @@ class StorageLayer {
283
277
  metadata.addDownloadToken();
284
278
  return metadata;
285
279
  }
286
- handleDeleteDownloadToken(request) {
280
+ deleteDownloadToken(request) {
287
281
  if (!this._adminCredsValidator.validate(request.authorization)) {
288
282
  throw new errors_1.ForbiddenError();
289
283
  }
@@ -354,10 +348,12 @@ class StorageLayer {
354
348
  logger_1.logger.warn(`Could not find file "${blobPath}" in storage export.`);
355
349
  continue;
356
350
  }
357
- const file = new StoredFile(metadata, blobPath);
358
- this._files.set(blobPath, file);
351
+ const decodedBlobPath = decodeURIComponent(blobPath);
352
+ const blobDiskPath = this._persistence.getDiskPath(decodedBlobPath);
353
+ const file = new StoredFile(metadata, blobDiskPath);
354
+ this._files.set(decodedBlobPath, file);
355
+ fse.copyFileSync(blobAbsPath, blobDiskPath);
359
356
  }
360
- fse.copySync(blobsDir, this.dirPath);
361
357
  }
362
358
  *walkDirSync(dir) {
363
359
  const files = (0, fs_1.readdirSync)(dir);
@@ -13,19 +13,30 @@ const runtime_1 = require("./rules/runtime");
13
13
  const utils_1 = require("./rules/utils");
14
14
  const persistence_1 = require("./persistence");
15
15
  const upload_1 = require("./upload");
16
+ const cloudFunctions_1 = require("./cloudFunctions");
16
17
  class StorageEmulator {
17
18
  constructor(args) {
18
19
  this.args = args;
19
20
  this._logger = emulatorLogger_1.EmulatorLogger.forEmulator(types_1.Emulators.STORAGE);
21
+ this._files = new Map();
22
+ this._buckets = new Map();
20
23
  this._rulesRuntime = new runtime_1.StorageRulesRuntime();
21
- this._rulesManager = (0, manager_1.createStorageRulesManager)(this.args.rules, this._rulesRuntime);
24
+ this._rulesManager = this.createRulesManager(this.args.rules);
25
+ this._cloudFunctions = new cloudFunctions_1.StorageCloudFunctions(args.projectId);
22
26
  this._persistence = new persistence_1.Persistence(this.getPersistenceTmpDir());
23
- this._storageLayer = new files_1.StorageLayer(args.projectId, (0, utils_1.getRulesValidator)((resource) => this._rulesManager.getRuleset(resource)), (0, utils_1.getAdminCredentialValidator)(), this._persistence);
24
27
  this._uploadService = new upload_1.UploadService(this._persistence);
28
+ const createStorageLayer = (rulesValidator) => {
29
+ return new files_1.StorageLayer(args.projectId, this._files, this._buckets, rulesValidator, (0, utils_1.getAdminCredentialValidator)(), this._persistence, this._cloudFunctions);
30
+ };
31
+ this._storageLayer = createStorageLayer((0, utils_1.getFirebaseRulesValidator)((resource) => this._rulesManager.getRuleset(resource)));
32
+ this._adminStorageLayer = createStorageLayer((0, utils_1.getAdminOnlyFirebaseRulesValidator)());
25
33
  }
26
34
  get storageLayer() {
27
35
  return this._storageLayer;
28
36
  }
37
+ get adminStorageLayer() {
38
+ return this._adminStorageLayer;
39
+ }
29
40
  get uploadService() {
30
41
  return this._uploadService;
31
42
  }
@@ -36,7 +47,8 @@ class StorageEmulator {
36
47
  return this._logger;
37
48
  }
38
49
  reset() {
39
- this._storageLayer.reset();
50
+ this._files.clear();
51
+ this._buckets.clear();
40
52
  this._persistence.reset(this.getPersistenceTmpDir());
41
53
  this._uploadService.reset();
42
54
  }
@@ -70,6 +82,14 @@ class StorageEmulator {
70
82
  getApp() {
71
83
  return this._app;
72
84
  }
85
+ async replaceRules(rules) {
86
+ await this._rulesManager.stop();
87
+ this._rulesManager = this.createRulesManager(rules);
88
+ return this._rulesManager.start();
89
+ }
90
+ createRulesManager(rules) {
91
+ return (0, manager_1.createStorageRulesManager)(rules, this._rulesRuntime);
92
+ }
73
93
  getPersistenceTmpDir() {
74
94
  return `${(0, os_1.tmpdir)()}/firebase/storage/blobs`;
75
95
  }
@@ -44,7 +44,7 @@ class StoredFileMetadata {
44
44
  throw new Error("Must pass bytes array or opts object with size, md5hash, and crc32c");
45
45
  }
46
46
  if (incomingMetadata) {
47
- this.update(incomingMetadata);
47
+ this.update(incomingMetadata, false);
48
48
  }
49
49
  this.deleteFieldsSetAsNull();
50
50
  this.setDownloadTokensFromCustomMetadata();
@@ -86,8 +86,10 @@ class StoredFileMetadata {
86
86
  }
87
87
  if (this.customMetadata.firebaseStorageDownloadTokens) {
88
88
  this.downloadTokens = [
89
- ...this.downloadTokens,
90
- ...this.customMetadata.firebaseStorageDownloadTokens.split(","),
89
+ ...new Set([
90
+ ...this.downloadTokens,
91
+ ...this.customMetadata.firebaseStorageDownloadTokens.split(","),
92
+ ]),
91
93
  ];
92
94
  delete this.customMetadata.firebaseStorageDownloadTokens;
93
95
  }
@@ -115,7 +117,7 @@ class StoredFileMetadata {
115
117
  });
116
118
  }
117
119
  }
118
- update(incoming) {
120
+ update(incoming, shouldTrigger = true) {
119
121
  if (incoming.contentDisposition) {
120
122
  this.contentDisposition = incoming.contentDisposition;
121
123
  }
@@ -143,15 +145,17 @@ class StoredFileMetadata {
143
145
  }
144
146
  this.setDownloadTokensFromCustomMetadata();
145
147
  this.deleteFieldsSetAsNull();
146
- this._cloudFunctions.dispatch("metadataUpdate", new CloudStorageObjectMetadata(this));
148
+ if (shouldTrigger) {
149
+ this._cloudFunctions.dispatch("metadataUpdate", new CloudStorageObjectMetadata(this));
150
+ }
147
151
  }
148
- addDownloadToken() {
152
+ addDownloadToken(shouldTrigger = true) {
149
153
  if (!this.downloadTokens.length) {
150
154
  this.downloadTokens.push(uuid.v4());
151
155
  return;
152
156
  }
153
157
  this.downloadTokens = [...this.downloadTokens, uuid.v4()];
154
- this.update({});
158
+ this.update({}, shouldTrigger);
155
159
  }
156
160
  deleteDownloadToken(token) {
157
161
  if (!this.downloadTokens.length) {
@@ -160,7 +164,7 @@ class StoredFileMetadata {
160
164
  const remainingTokens = this.downloadTokens.filter((t) => t !== token);
161
165
  this.downloadTokens = remainingTokens;
162
166
  if (remainingTokens.length === 0) {
163
- this.addDownloadToken();
167
+ this.addDownloadToken(false);
164
168
  }
165
169
  this.update({});
166
170
  }
@@ -265,6 +269,12 @@ class CloudStorageObjectMetadata {
265
269
  if (metadata.cacheControl) {
266
270
  this.cacheControl = metadata.cacheControl;
267
271
  }
272
+ if (metadata.contentDisposition) {
273
+ this.contentDisposition = metadata.contentDisposition;
274
+ }
275
+ if (metadata.contentEncoding) {
276
+ this.contentEncoding = metadata.contentEncoding;
277
+ }
268
278
  if (metadata.customTime) {
269
279
  this.customTime = toSerializedDate(metadata.customTime);
270
280
  }
@@ -5,6 +5,7 @@ const chokidar = require("chokidar");
5
5
  const emulatorLogger_1 = require("../../emulatorLogger");
6
6
  const types_1 = require("../../types");
7
7
  const runtime_1 = require("./runtime");
8
+ const fsutils_1 = require("../../../fsutils");
8
9
  function createStorageRulesManager(rules, runtime) {
9
10
  return Array.isArray(rules)
10
11
  ? new ResourceBasedStorageRulesManager(rules, runtime)
@@ -18,31 +19,24 @@ class DefaultStorageRulesManager {
18
19
  this._logger = emulatorLogger_1.EmulatorLogger.forEmulator(types_1.Emulators.STORAGE);
19
20
  this._rules = _rules;
20
21
  }
21
- start() {
22
- return this.updateSourceFile(this._rules);
22
+ async start() {
23
+ const issues = await this.loadRuleset();
24
+ this.updateWatcher(this._rules.name);
25
+ return issues;
23
26
  }
24
27
  getRuleset() {
25
28
  return this._ruleset;
26
29
  }
27
- async updateSourceFile(rules) {
28
- const prevRulesFile = this._rules.name;
29
- this._rules = rules;
30
- const issues = await this.loadRuleset();
31
- this.updateWatcher(rules.name, prevRulesFile);
32
- return issues;
33
- }
34
30
  async stop() {
35
31
  await this._watcher.close();
36
32
  }
37
- updateWatcher(rulesFile, prevRulesFile) {
38
- if (prevRulesFile) {
39
- this._watcher.unwatch(prevRulesFile);
40
- }
33
+ updateWatcher(rulesFile) {
41
34
  this._watcher = chokidar
42
35
  .watch(rulesFile, { persistent: true, ignoreInitial: true })
43
36
  .on("change", async () => {
44
37
  await new Promise((res) => setTimeout(res, 5));
45
38
  this._logger.logLabeled("BULLET", "storage", "Change detected, updating rules for Cloud Storage...");
39
+ this._rules.content = (0, fsutils_1.readFile)(rulesFile);
46
40
  await this.loadRuleset();
47
41
  });
48
42
  }
@@ -83,10 +77,6 @@ class ResourceBasedStorageRulesManager {
83
77
  var _a;
84
78
  return (_a = this._rulesManagers.get(resource)) === null || _a === void 0 ? void 0 : _a.getRuleset();
85
79
  }
86
- updateSourceFile(rules, resource) {
87
- const rulesManager = this._rulesManagers.get(resource) || this.createRulesManager(resource, rules);
88
- return rulesManager.updateSourceFile(rules);
89
- }
90
80
  async stop() {
91
81
  await Promise.all(Array.from(this._rulesManagers.values(), async (rulesManager) => await rulesManager.stop()));
92
82
  }
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isPermitted = exports.getAdminCredentialValidator = exports.getRulesValidator = void 0;
3
+ exports.isPermitted = exports.getAdminCredentialValidator = exports.getAdminOnlyFirebaseRulesValidator = exports.getFirebaseRulesValidator = void 0;
4
4
  const emulatorLogger_1 = require("../../emulatorLogger");
5
5
  const types_1 = require("../../types");
6
- function getRulesValidator(rulesetProvider) {
6
+ function getFirebaseRulesValidator(rulesetProvider) {
7
7
  return {
8
8
  validate: async (path, bucketId, method, variableOverrides, authorization) => {
9
9
  return await isPermitted({
@@ -16,7 +16,15 @@ function getRulesValidator(rulesetProvider) {
16
16
  },
17
17
  };
18
18
  }
19
- exports.getRulesValidator = getRulesValidator;
19
+ exports.getFirebaseRulesValidator = getFirebaseRulesValidator;
20
+ function getAdminOnlyFirebaseRulesValidator() {
21
+ return {
22
+ validate: (_path, _bucketId, _method, _variableOverrides, _authorization) => {
23
+ return Promise.resolve(true);
24
+ },
25
+ };
26
+ }
27
+ exports.getAdminOnlyFirebaseRulesValidator = getAdminOnlyFirebaseRulesValidator;
20
28
  function getAdminCredentialValidator() {
21
29
  return { validate: isValidAdminCredentials };
22
30
  }
@@ -8,6 +8,7 @@ const types_1 = require("../types");
8
8
  const bodyParser = require("body-parser");
9
9
  const gcloud_1 = require("./apis/gcloud");
10
10
  const firebase_1 = require("./apis/firebase");
11
+ const errors_1 = require("../auth/errors");
11
12
  function createApp(defaultProjectId, emulator) {
12
13
  const { storageLayer } = emulator;
13
14
  const app = express();
@@ -44,21 +45,43 @@ function createApp(defaultProjectId, emulator) {
44
45
  res.sendStatus(200);
45
46
  });
46
47
  app.put("/internal/setRules", async (req, res) => {
47
- const rules = req.body.rules;
48
- if (!(rules && Array.isArray(rules.files) && rules.files.length > 0)) {
49
- res.status(400).send("Request body must include 'rules.files' array .");
48
+ const rulesRaw = req.body.rules;
49
+ if (!(rulesRaw && Array.isArray(rulesRaw.files) && rulesRaw.files.length > 0)) {
50
+ res.status(400).json({
51
+ message: "Request body must include 'rules.files' array",
52
+ });
50
53
  return;
51
54
  }
52
- const file = rules.files[0];
53
- if (!(file.name && file.content)) {
54
- res
55
- .status(400)
56
- .send("Request body must include 'rules.files' array where each member contains 'name' and 'content'.");
57
- return;
55
+ const { files } = rulesRaw;
56
+ function parseRulesFromFiles(files) {
57
+ if (files.length === 1) {
58
+ const file = files[0];
59
+ if (!isRulesFile(file)) {
60
+ throw new errors_1.InvalidArgumentError("Each member of 'rules.files' array must contain 'name' and 'content'");
61
+ }
62
+ return { name: file.name, content: file.content };
63
+ }
64
+ const rules = [];
65
+ for (const file of files) {
66
+ if (!isRulesFile(file) || !file.resource) {
67
+ throw new errors_1.InvalidArgumentError("Each member of 'rules.files' array must contain 'name', 'content', and 'resource'");
68
+ }
69
+ rules.push({ resource: file.resource, rules: { name: file.name, content: file.content } });
70
+ }
71
+ return rules;
58
72
  }
59
- const name = file.name;
60
- const content = file.content;
61
- const issues = await emulator.rulesManager.updateSourceFile({ name, content }, req.params.bucketId);
73
+ let rules;
74
+ try {
75
+ rules = parseRulesFromFiles(files);
76
+ }
77
+ catch (err) {
78
+ if (err instanceof errors_1.InvalidArgumentError) {
79
+ res.status(400).json({ message: err.message });
80
+ return;
81
+ }
82
+ throw err;
83
+ }
84
+ const issues = await emulator.replaceRules(rules);
62
85
  if (issues.errors.length > 0) {
63
86
  res.status(400).json({
64
87
  message: "There was an error updating rules, see logs for more details",
@@ -78,3 +101,6 @@ function createApp(defaultProjectId, emulator) {
78
101
  return Promise.resolve(app);
79
102
  }
80
103
  exports.createApp = createApp;
104
+ function isRulesFile(file) {
105
+ return (typeof file.name === "string" && typeof file.content === "string");
106
+ }
@@ -12,6 +12,7 @@ const utils_1 = require("./utils");
12
12
  const logger_1 = require("../logger");
13
13
  const prompt_1 = require("../prompt");
14
14
  const utils = require("../utils");
15
+ const projectUtils_1 = require("../projectUtils");
15
16
  var SecretLocation;
16
17
  (function (SecretLocation) {
17
18
  SecretLocation[SecretLocation["CLOUD"] = 1] = "CLOUD";
@@ -60,21 +61,21 @@ function checkResponse(response, spec) {
60
61
  return valid;
61
62
  }
62
63
  exports.checkResponse = checkResponse;
63
- async function ask(projectId, instanceId, paramSpecs, firebaseProjectParams, reconfiguring) {
64
- if (_.isEmpty(paramSpecs)) {
64
+ async function ask(args) {
65
+ if (_.isEmpty(args.paramSpecs)) {
65
66
  logger_1.logger.debug("No params were specified for this extension.");
66
67
  return {};
67
68
  }
68
69
  utils.logLabeledBullet(extensionsHelper_1.logPrefix, "answer the questions below to configure your extension:");
69
- const substituted = (0, extensionsHelper_1.substituteParams)(paramSpecs, firebaseProjectParams);
70
+ const substituted = (0, extensionsHelper_1.substituteParams)(args.paramSpecs, args.firebaseProjectParams);
70
71
  const result = {};
71
72
  const promises = _.map(substituted, (paramSpec) => {
72
73
  return async () => {
73
74
  result[paramSpec.param] = await askForParam({
74
- projectId,
75
- instanceId,
76
- paramSpec,
77
- reconfiguring,
75
+ projectId: args.projectId,
76
+ instanceId: args.instanceId,
77
+ paramSpec: paramSpec,
78
+ reconfiguring: args.reconfiguring,
78
79
  });
79
80
  };
80
81
  });
@@ -133,9 +134,10 @@ async function askForParam(args) {
133
134
  secretLocations = await promptSecretLocations(paramSpec);
134
135
  } while (!isValidSecretLocations(secretLocations, paramSpec));
135
136
  if (secretLocations.includes(SecretLocation.CLOUD.toString())) {
137
+ const projectId = (0, projectUtils_1.needProjectId)({ projectId: args.projectId });
136
138
  response = args.reconfiguring
137
- ? await promptReconfigureSecret(args.projectId, args.instanceId, paramSpec)
138
- : await promptCreateSecret(args.projectId, args.instanceId, paramSpec);
139
+ ? await promptReconfigureSecret(projectId, args.instanceId, paramSpec)
140
+ : await promptCreateSecret(projectId, args.instanceId, paramSpec);
139
141
  }
140
142
  if (secretLocations.includes(SecretLocation.LOCAL.toString())) {
141
143
  responseForLocal = await promptLocalSecret(args.instanceId, paramSpec);
@@ -170,12 +172,12 @@ async function promptSecretLocations(paramSpec) {
170
172
  choices: [
171
173
  {
172
174
  checked: true,
173
- name: "Google Cloud Secret Manager",
175
+ name: "Google Cloud Secret Manager (Used by deployed extensions and emulator)",
174
176
  value: SecretLocation.CLOUD.toString(),
175
177
  },
176
178
  {
177
179
  checked: false,
178
- name: "Local file (Only used by Firebase Emulator)",
180
+ name: "Local file (Used by emulator only)",
179
181
  value: SecretLocation.LOCAL.toString(),
180
182
  },
181
183
  ],
@@ -189,25 +191,28 @@ async function promptSecretLocations(paramSpec) {
189
191
  choices: [
190
192
  {
191
193
  checked: false,
192
- name: "Google Cloud Secret Manager",
194
+ name: "Google Cloud Secret Manager (Used by deployed extensions and emulator)",
193
195
  value: SecretLocation.CLOUD.toString(),
194
196
  },
195
197
  {
196
198
  checked: false,
197
- name: "Local file (Only used by Firebase Emulator)",
199
+ name: "Local file (Used by emulator only)",
198
200
  value: SecretLocation.LOCAL.toString(),
199
201
  },
200
202
  ],
201
203
  });
202
204
  }
203
205
  async function promptLocalSecret(instanceId, paramSpec) {
204
- utils.logLabeledBullet(extensionsHelper_1.logPrefix, "Configure a local secret value for Extensions Emulator");
205
- const value = await (0, prompt_1.promptOnce)({
206
- name: paramSpec.param,
207
- type: "input",
208
- message: `This secret will be stored in ./extensions/${instanceId}.secret.local.\n` +
209
- `Enter value for "${paramSpec.label.trim()}" to be used by Extensions Emulator:`,
210
- });
206
+ let value;
207
+ do {
208
+ utils.logLabeledBullet(extensionsHelper_1.logPrefix, "Configure a local secret value for Extensions Emulator");
209
+ value = await (0, prompt_1.promptOnce)({
210
+ name: paramSpec.param,
211
+ type: "input",
212
+ message: `This secret will be stored in ./extensions/${instanceId}.secret.local.\n` +
213
+ `Enter value for "${paramSpec.label.trim()}" to be used by Extensions Emulator:`,
214
+ });
215
+ } while (!value);
211
216
  return value;
212
217
  }
213
218
  async function promptReconfigureSecret(projectId, instanceId, paramSpec) {
@@ -2,7 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getParams = exports.getSecretEnvVars = exports.getNonSecretEnv = exports.getExtensionFunctionInfo = exports.buildOptions = void 0;
4
4
  const fs = require("fs-extra");
5
- const _ = require("lodash");
6
5
  const path = require("path");
7
6
  const paramHelper = require("../paramHelper");
8
7
  const specHelper = require("./specHelper");
@@ -159,12 +158,10 @@ function buildConfig(functionResources, testConfig) {
159
158
  return config;
160
159
  }
161
160
  function getFunctionSourceDirectory(functionResources) {
161
+ var _a;
162
162
  let sourceDirectory;
163
163
  for (const r of functionResources) {
164
- let dir = _.get(r, "properties.sourceDirectory");
165
- if (!dir) {
166
- dir = "functions";
167
- }
164
+ const dir = ((_a = r.properties) === null || _a === void 0 ? void 0 : _a.sourceDirectory) || "functions";
168
165
  if (!sourceDirectory) {
169
166
  sourceDirectory = dir;
170
167
  }
@@ -172,14 +169,15 @@ function getFunctionSourceDirectory(functionResources) {
172
169
  throw new error_1.FirebaseError(`Found function resources with different sourceDirectories: '${sourceDirectory}' and '${dir}'. The extensions emulator only supports a single sourceDirectory.`);
173
170
  }
174
171
  }
175
- return sourceDirectory;
172
+ return sourceDirectory || "functions";
176
173
  }
177
174
  function shouldEmulateFunctions(resources) {
178
175
  return resources.length > 0;
179
176
  }
180
177
  function shouldEmulate(emulatorName, resources) {
178
+ var _a, _b;
181
179
  for (const r of resources) {
182
- const eventType = _.get(r, "properties.eventTrigger.eventType", "");
180
+ const eventType = ((_b = (_a = r.properties) === null || _a === void 0 ? void 0 : _a.eventTrigger) === null || _b === void 0 ? void 0 : _b.eventType) || "";
183
181
  if (eventType.includes(emulatorName)) {
184
182
  return true;
185
183
  }