-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathVaultManager.ts
1112 lines (1060 loc) · 33.5 KB
/
VaultManager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import type { DBTransaction, LevelPath } from '@matrixai/db';
import type {
VaultId,
VaultName,
VaultActions,
VaultIdString,
VaultIdEncoded,
} from './types';
import type { Vault } from './Vault';
import type { FileSystem } from '../types';
import type { PolykeyWorkerManagerInterface } from '../workers/types';
import type { NodeId } from '../ids/types';
import type KeyRing from '../keys/KeyRing';
import type NodeManager from '../nodes/NodeManager';
import type GestaltGraph from '../gestalts/GestaltGraph';
import type NotificationsManager from '../notifications/NotificationsManager';
import type ACL from '../acl/ACL';
import type { RemoteInfo } from './VaultInternal';
import type { VaultAction } from './types';
import type { LockRequest } from '@matrixai/async-locks';
import type { Key } from '../keys/types';
import path from 'path';
import { DB } from '@matrixai/db';
import { EncryptedFS, errors as encryptedFsErrors } from 'encryptedfs';
import Logger from '@matrixai/logger';
import {
CreateDestroyStartStop,
ready,
} from '@matrixai/async-init/dist/CreateDestroyStartStop';
import { IdInternal } from '@matrixai/id';
import { withF, withG } from '@matrixai/resources';
import { LockBox, RWLockWriter } from '@matrixai/async-locks';
import VaultInternal from './VaultInternal';
import * as vaultsEvents from './events';
import * as vaultsUtils from './utils';
import * as vaultsErrors from './errors';
import * as utils from '../utils';
import * as gitHttp from '../git/http';
import * as nodesUtils from '../nodes/utils';
import * as keysUtils from '../keys/utils';
import config from '../config';
import { mkdirExists } from '../utils/utils';
/**
* Object map pattern for each vault
*/
type VaultMap = Map<VaultIdString, VaultInternal>;
type VaultList = Map<VaultName, VaultId>;
type VaultMetadata = {
dirty: boolean;
vaultName: VaultName;
remoteInfo?: RemoteInfo;
};
interface VaultManager extends CreateDestroyStartStop {}
@CreateDestroyStartStop(
new vaultsErrors.ErrorVaultManagerRunning(),
new vaultsErrors.ErrorVaultManagerDestroyed(),
{
eventStart: vaultsEvents.EventVaultManagerStart,
eventStarted: vaultsEvents.EventVaultManagerStarted,
eventStop: vaultsEvents.EventVaultManagerStop,
eventStopped: vaultsEvents.EventVaultManagerStopped,
eventDestroy: vaultsEvents.EventVaultManagerDestroy,
eventDestroyed: vaultsEvents.EventVaultManagerDestroyed,
},
)
class VaultManager {
static async createVaultManager({
vaultsPath,
db,
acl,
keyRing,
nodeManager,
gestaltGraph,
notificationsManager,
fs = require('fs'),
logger = new Logger(this.name),
fresh = false,
}: {
vaultsPath: string;
db: DB;
acl: ACL;
keyRing: KeyRing;
nodeManager: NodeManager;
gestaltGraph: GestaltGraph;
notificationsManager: NotificationsManager;
fs?: FileSystem;
logger?: Logger;
fresh?: boolean;
}) {
logger.info(`Creating ${this.name}`);
logger.info(`Setting vaults path to ${vaultsPath}`);
const vaultManager = new this({
vaultsPath,
db,
acl,
keyRing,
nodeManager,
gestaltGraph,
notificationsManager,
fs,
logger,
});
await vaultManager.start({ fresh });
logger.info(`Created ${this.name}`);
return vaultManager;
}
public readonly vaultsPath: string;
public readonly efsPath: string;
protected fs: FileSystem;
protected logger: Logger;
protected db: DB;
protected acl: ACL;
protected keyRing: KeyRing;
protected nodeManager: NodeManager;
protected gestaltGraph: GestaltGraph;
protected notificationsManager: NotificationsManager;
protected vaultsDbPath: LevelPath = [this.constructor.name];
protected vaultsNamesDbPath: LevelPath = [this.constructor.name, 'names'];
// VaultId -> VaultMetadata
protected vaultMap: VaultMap = new Map();
protected vaultLocks: LockBox<RWLockWriter> = new LockBox();
protected vaultKey: Buffer;
protected efsDb: DB;
protected efs: EncryptedFS;
protected vaultIdGenerator = vaultsUtils.createVaultIdGenerator();
constructor({
vaultsPath,
db,
acl,
keyRing,
nodeManager,
gestaltGraph,
notificationsManager,
fs,
logger,
}: {
vaultsPath: string;
db: DB;
acl: ACL;
keyRing: KeyRing;
nodeManager: NodeManager;
gestaltGraph: GestaltGraph;
notificationsManager: NotificationsManager;
fs: FileSystem;
logger: Logger;
}) {
this.logger = logger;
this.vaultsPath = vaultsPath;
this.efsPath = path.join(this.vaultsPath, config.paths.efsBase);
this.db = db;
this.acl = acl;
this.keyRing = keyRing;
this.nodeManager = nodeManager;
this.gestaltGraph = gestaltGraph;
this.notificationsManager = notificationsManager;
this.fs = fs;
}
public async start({
fresh = false,
}: {
fresh?: boolean;
} = {}): Promise<void> {
await this.db.withTransactionF(async (tran) => {
try {
this.logger.info(`Starting ${this.constructor.name}`);
if (fresh) {
await tran.clear(this.vaultsDbPath);
await this.fs.promises.rm(this.vaultsPath, {
force: true,
recursive: true,
});
}
await mkdirExists(this.fs, this.vaultsPath);
const vaultKey = await this.setupKey(tran);
let efsDb: DB;
let efs: EncryptedFS;
try {
efsDb = await DB.createDB({
fresh,
crypto: {
key: vaultKey,
ops: {
encrypt: async (key, plainText) => {
return keysUtils.encryptWithKey(
utils.bufferWrap(key) as Key,
utils.bufferWrap(plainText),
);
},
decrypt: async (key, cipherText) => {
return keysUtils.decryptWithKey(
utils.bufferWrap(key) as Key,
utils.bufferWrap(cipherText),
);
},
},
},
dbPath: this.efsPath,
logger: this.logger.getChild('EFS Database'),
});
efs = await EncryptedFS.createEncryptedFS({
fresh,
db: efsDb,
logger: this.logger.getChild('EncryptedFileSystem'),
});
} catch (e) {
if (e instanceof encryptedFsErrors.ErrorEncryptedFSKey) {
throw new vaultsErrors.ErrorVaultManagerKey(e.message, {
cause: e,
});
}
throw new vaultsErrors.ErrorVaultManagerEFS(e.message, {
data: {
errno: e.errno,
syscall: e.syscall,
code: e.code,
path: e.path,
},
cause: e,
});
}
this.vaultKey = vaultKey;
this.efsDb = efsDb;
this.efs = efs;
this.logger.info(`Started ${this.constructor.name}`);
} catch (e) {
this.logger.warn(`Failed Starting ${this.constructor.name}`);
await this.efs?.stop();
await this.efsDb?.stop();
throw e;
}
});
}
public async stop(): Promise<void> {
this.logger.info(`Stopping ${this.constructor.name}`);
// Iterate over vaults in memory and destroy them, ensuring that
// the working directory commit state is saved
const promises: Array<Promise<void>> = [];
for (const vaultIdString of this.vaultMap.keys()) {
const vaultId = IdInternal.fromString<VaultId>(vaultIdString);
promises.push(
withF(
[this.vaultLocks.lock([vaultId.toString(), RWLockWriter, 'write'])],
async () => {
const vault = this.vaultMap.get(vaultIdString);
if (vault == null) return;
await vault.stop();
this.vaultMap.delete(vaultIdString);
},
),
);
}
await Promise.all(promises);
await this.efs.stop();
await this.efsDb.stop();
this.vaultMap = new Map();
this.logger.info(`Stopped ${this.constructor.name}`);
}
public async destroy(): Promise<void> {
this.logger.info(`Destroying ${this.constructor.name}`);
await this.efsDb.start({
fresh: false,
crypto: {
key: this.vaultKey,
ops: {
encrypt: async (key, plainText) => {
return keysUtils.encryptWithKey(
utils.bufferWrap(key) as Key,
utils.bufferWrap(plainText),
);
},
decrypt: async (key, cipherText) => {
return keysUtils.decryptWithKey(
utils.bufferWrap(key) as Key,
utils.bufferWrap(cipherText),
);
},
},
},
});
await this.efs.destroy();
await this.efsDb.stop();
await this.efsDb.destroy();
// Clearing all vaults db data
await this.db.clear(this.vaultsDbPath);
// Is it necessary to remove the vaults' domain?
await this.fs.promises.rm(this.vaultsPath, {
force: true,
recursive: true,
});
this.logger.info(`Destroyed ${this.constructor.name}`);
}
public setWorkerManager(workerManager: PolykeyWorkerManagerInterface) {
this.efs.setWorkerManager(workerManager);
}
public unsetWorkerManager() {
this.efs.unsetWorkerManager();
}
/**
* Constructs a new vault instance with a given name and
* stores it in memory
*/
@ready(new vaultsErrors.ErrorVaultManagerNotRunning())
public async createVault(
vaultName: VaultName,
tran?: DBTransaction,
): Promise<VaultId> {
if (tran == null) {
return this.db.withTransactionF((tran) =>
this.createVault(vaultName, tran),
);
}
// Adding vault to name map
const vaultId = await this.generateVaultId();
await tran.lock([...this.vaultsNamesDbPath, vaultName].join(''));
const vaultIdBuffer = await tran.get(
[...this.vaultsNamesDbPath, vaultName],
true,
);
// Check if the vault name already exists;
if (vaultIdBuffer != null) {
throw new vaultsErrors.ErrorVaultsVaultDefined();
}
await tran.put(
[...this.vaultsNamesDbPath, vaultName],
vaultId.toBuffer(),
true,
);
const vaultIdString = vaultId.toString() as VaultIdString;
return await this.vaultLocks.withF(
[vaultId.toString(), RWLockWriter, 'write'],
async () => {
// Creating vault
const vault = await VaultInternal.createVaultInternal({
vaultId,
vaultName,
keyRing: this.keyRing,
efs: this.efs,
logger: this.logger.getChild(VaultInternal.name),
db: this.db,
vaultsDbPath: this.vaultsDbPath,
fresh: true,
tran,
});
// Adding vault to object map
this.vaultMap.set(vaultIdString, vault);
return vault.vaultId;
},
);
}
/**
* Retrieves the vault metadata using the VaultId
* and parses it to return the associated vault name
*/
@ready(new vaultsErrors.ErrorVaultManagerNotRunning())
public async getVaultMeta(
vaultId: VaultId,
tran?: DBTransaction,
): Promise<VaultMetadata | undefined> {
if (tran == null) {
return this.db.withTransactionF((tran) =>
this.getVaultMeta(vaultId, tran),
);
}
// First check if the metadata exists
const vaultIdEncoded = vaultsUtils.encodeVaultId(vaultId);
const vaultDbPath: LevelPath = [...this.vaultsDbPath, vaultIdEncoded];
// Return if metadata has no data
if ((await tran.count(vaultDbPath)) === 0) return;
// Obtain the metadata;
const dirty = (await tran.get<boolean>([
...vaultDbPath,
VaultInternal.dirtyKey,
]))!;
const vaultName = (await tran.get<VaultName>([
...vaultDbPath,
VaultInternal.nameKey,
]))!;
const remoteInfo = await tran.get<RemoteInfo>([
...vaultDbPath,
VaultInternal.remoteKey,
]);
return {
dirty,
vaultName,
remoteInfo,
};
}
/**
* Removes the metadata and EFS state of a vault using a
* given VaultId
*/
@ready(new vaultsErrors.ErrorVaultManagerNotRunning())
public async destroyVault(
vaultId: VaultId,
tran?: DBTransaction,
): Promise<void> {
if (tran == null) {
return this.db.withTransactionF((tran) =>
this.destroyVault(vaultId, tran),
);
}
await this.vaultLocks.withF(
[vaultId.toString(), RWLockWriter, 'write'],
async () => {
await tran.lock([...this.vaultsDbPath, vaultId].join(''));
// Ensure protection from write skew
await tran.getForUpdate([
...this.vaultsDbPath,
vaultsUtils.encodeVaultId(vaultId),
VaultInternal.nameKey,
]);
const vaultMeta = await this.getVaultMeta(vaultId, tran);
if (vaultMeta == null) return;
const vaultName = vaultMeta.vaultName;
this.logger.info(
`Destroying Vault ${vaultsUtils.encodeVaultId(vaultId)}`,
);
const vaultIdString = vaultId.toString() as VaultIdString;
const vault = await this.getVault(vaultId, tran);
// Destroying vault state and metadata
await vault.stop();
await vault.destroy(tran);
// Removing from map
this.vaultMap.delete(vaultIdString);
// Removing name->id mapping
await tran.del([...this.vaultsNamesDbPath, vaultName]);
},
);
this.logger.info(`Destroyed Vault ${vaultsUtils.encodeVaultId(vaultId)}`);
}
/**
* Removes vault from the vault map
*/
@ready(new vaultsErrors.ErrorVaultManagerNotRunning())
public async closeVault(
vaultId: VaultId,
tran?: DBTransaction,
): Promise<void> {
if (tran == null) {
return this.db.withTransactionF((tran) => this.closeVault(vaultId, tran));
}
if ((await this.getVaultName(vaultId, tran)) == null) {
throw new vaultsErrors.ErrorVaultsVaultUndefined();
}
const vaultIdString = vaultId.toString() as VaultIdString;
await this.vaultLocks.withF(
[vaultId.toString(), RWLockWriter, 'write'],
async () => {
await tran.lock([...this.vaultsDbPath, vaultId].join(''));
const vault = await this.getVault(vaultId, tran);
await vault.stop();
this.vaultMap.delete(vaultIdString);
},
);
}
/**
* Lists the vault name and associated VaultId of all
* the vaults stored
*/
@ready(new vaultsErrors.ErrorVaultManagerNotRunning())
public async listVaults(tran?: DBTransaction): Promise<VaultList> {
if (tran == null) {
return this.db.withTransactionF((tran) => this.listVaults(tran));
}
const vaults: VaultList = new Map();
// Stream of vaultName VaultId key value pairs
for await (const [vaultNameBuffer, vaultIdBuffer] of tran.iterator(
this.vaultsNamesDbPath,
)) {
const vaultName = vaultNameBuffer.toString() as VaultName;
const vaultId = IdInternal.fromBuffer<VaultId>(vaultIdBuffer);
vaults.set(vaultName, vaultId);
}
return vaults;
}
/**
* Changes the vault name metadata of a VaultId
*/
@ready(new vaultsErrors.ErrorVaultManagerNotRunning())
public async renameVault(
vaultId: VaultId,
newVaultName: VaultName,
tran?: DBTransaction,
): Promise<void> {
if (tran == null) {
return this.db.withTransactionF((tran) =>
this.renameVault(vaultId, newVaultName, tran),
);
}
await this.vaultLocks.withF(
[vaultId.toString(), RWLockWriter, 'write'],
async () => {
await tran.lock(
[...this.vaultsNamesDbPath, newVaultName]
.map((v) => v.toString())
.join(''),
[...this.vaultsDbPath, vaultId].map((v) => v.toString()).join(''),
);
this.logger.info(
`Renaming Vault ${vaultsUtils.encodeVaultId(vaultId)}`,
);
// Checking if new name exists
if (await this.getVaultId(newVaultName, tran)) {
throw new vaultsErrors.ErrorVaultsVaultDefined();
}
// Ensure protection from write skew
await tran.getForUpdate([
...this.vaultsDbPath,
vaultsUtils.encodeVaultId(vaultId),
VaultInternal.nameKey,
]);
// Checking if vault exists
const vaultMetadata = await this.getVaultMeta(vaultId, tran);
if (vaultMetadata == null) {
throw new vaultsErrors.ErrorVaultsVaultUndefined();
}
const oldVaultName = vaultMetadata.vaultName;
// Updating metadata with new name;
const vaultDbPath = [
...this.vaultsDbPath,
vaultsUtils.encodeVaultId(vaultId),
];
await tran.put([...vaultDbPath, VaultInternal.nameKey], newVaultName);
// Updating name->id map
await tran.del([...this.vaultsNamesDbPath, oldVaultName]);
await tran.put(
[...this.vaultsNamesDbPath, newVaultName],
vaultId.toBuffer(),
true,
);
},
);
}
/**
* Retrieves the VaultId associated with a vault name
*/
@ready(new vaultsErrors.ErrorVaultManagerNotRunning())
public async getVaultId(
vaultName: VaultName,
tran?: DBTransaction,
): Promise<VaultId | undefined> {
if (tran == null) {
return this.db.withTransactionF((tran) =>
this.getVaultId(vaultName, tran),
);
}
await tran.lock([...this.vaultsNamesDbPath, vaultName].join(''));
const vaultIdBuffer = await tran.get(
[...this.vaultsNamesDbPath, vaultName],
true,
);
if (vaultIdBuffer == null) return;
return IdInternal.fromBuffer<VaultId>(vaultIdBuffer);
}
/**
* Retrieves the vault name associated with a VaultId
*/
@ready(new vaultsErrors.ErrorVaultManagerNotRunning())
public async getVaultName(
vaultId: VaultId,
tran?: DBTransaction,
): Promise<VaultName | undefined> {
if (tran == null) {
return this.db.withTransactionF((tran) =>
this.getVaultName(vaultId, tran),
);
}
const metadata = await this.getVaultMeta(vaultId, tran);
return metadata?.vaultName;
}
/**
* Returns a dictionary of VaultActions for each node
*/
@ready(new vaultsErrors.ErrorVaultManagerNotRunning())
public async getVaultPermission(
vaultId: VaultId,
tran?: DBTransaction,
): Promise<Record<NodeId, VaultActions>> {
if (tran == null) {
return this.db.withTransactionF((tran) =>
this.getVaultPermission(vaultId, tran),
);
}
const rawPermissions = await this.acl.getVaultPerm(vaultId, tran);
const permissions: Record<NodeId, VaultActions> = {};
// Getting the relevant information
for (const nodeId in rawPermissions) {
permissions[nodeId] = rawPermissions[nodeId].vaults[vaultId];
}
return permissions;
}
/**
* Sets clone, pull and scan permissions of a vault for a
* gestalt and send a notification to this gestalt
*/
@ready(new vaultsErrors.ErrorVaultManagerNotRunning())
public async shareVault(
vaultId: VaultId,
nodeId: NodeId,
tran?: DBTransaction,
): Promise<void> {
if (tran == null) {
return this.db.withTransactionF((tran) =>
this.shareVault(vaultId, nodeId, tran),
);
}
const vaultMeta = await this.getVaultMeta(vaultId, tran);
if (vaultMeta == null) throw new vaultsErrors.ErrorVaultsVaultUndefined();
// NodeId permissions translated to other nodes in
// a gestalt by other domains
await this.gestaltGraph.setGestaltAction(['node', nodeId], 'scan', tran);
await this.acl.setVaultAction(vaultId, nodeId, 'pull', tran);
await this.acl.setVaultAction(vaultId, nodeId, 'clone', tran);
await this.notificationsManager.sendNotification({
nodeId,
data: {
type: 'VaultShare',
vaultId: vaultsUtils.encodeVaultId(vaultId),
vaultName: vaultMeta.vaultName,
actions: {
clone: null,
pull: null,
},
},
});
}
/**
* Unsets clone, pull and scan permissions of a vault for a
* gestalt
*/
@ready(new vaultsErrors.ErrorVaultManagerNotRunning())
public async unshareVault(
vaultId: VaultId,
nodeId: NodeId,
tran?: DBTransaction,
): Promise<void> {
if (tran == null) {
return this.db.withTransactionF((tran) =>
this.unshareVault(vaultId, nodeId, tran),
);
}
const vaultMeta = await this.getVaultMeta(vaultId, tran);
if (!vaultMeta) throw new vaultsErrors.ErrorVaultsVaultUndefined();
await this.gestaltGraph.unsetGestaltAction(['node', nodeId], 'scan', tran);
await this.acl.unsetVaultAction(vaultId, nodeId, 'pull', tran);
await this.acl.unsetVaultAction(vaultId, nodeId, 'clone', tran);
}
/**
* Clones the contents of a remote vault into a new local
* vault instance
*/
@ready(new vaultsErrors.ErrorVaultManagerNotRunning())
public async cloneVault(
nodeId: NodeId,
vaultNameOrId: VaultId | VaultName,
tran?: DBTransaction,
): Promise<VaultId> {
if (tran == null) {
return this.db.withTransactionF((tran) =>
this.cloneVault(nodeId, vaultNameOrId, tran),
);
}
const vaultId = await this.generateVaultId();
const vaultIdString = vaultId.toString() as VaultIdString;
this.logger.info(
`Cloning Vault ${vaultsUtils.encodeVaultId(vaultId)} on Node ${nodeId}`,
);
return await this.vaultLocks.withF(
[vaultId.toString(), RWLockWriter, 'write'],
async () => {
const vault = await VaultInternal.cloneVaultInternal({
targetNodeId: nodeId,
targetVaultNameOrId: vaultNameOrId,
vaultId,
db: this.db,
nodeManager: this.nodeManager,
vaultsDbPath: this.vaultsDbPath,
keyRing: this.keyRing,
efs: this.efs,
logger: this.logger.getChild(VaultInternal.name),
tran,
});
this.vaultMap.set(vaultIdString, vault);
const vaultMetadata = (await this.getVaultMeta(vaultId, tran))!;
const baseVaultName = vaultMetadata.vaultName;
// Need to check if the name is taken, 10 attempts
let newVaultName = baseVaultName;
let attempts = 1;
while (true) {
const existingVaultId = await tran.get(
[...this.vaultsNamesDbPath, newVaultName],
true,
);
if (existingVaultId == null) break;
newVaultName = `${baseVaultName}-${attempts}`;
if (attempts >= 50) {
throw new vaultsErrors.ErrorVaultsNameConflict(
`Too many copies of ${baseVaultName}`,
);
}
attempts++;
}
// Set the vaultName -> vaultId mapping
await tran.put(
[...this.vaultsNamesDbPath, newVaultName],
vaultId.toBuffer(),
true,
);
// Update vault metadata
await tran.put(
[
...this.vaultsDbPath,
vaultsUtils.encodeVaultId(vaultId),
VaultInternal.nameKey,
],
newVaultName,
);
this.logger.info(
`Cloned Vault ${vaultsUtils.encodeVaultId(
vaultId,
)} on Node ${nodeId}`,
);
return vault.vaultId;
},
);
}
/**
* Pulls the contents of a remote vault into an existing vault
* instance
*/
public async pullVault({
vaultId,
pullNodeId,
pullVaultNameOrId,
tran,
}: {
vaultId: VaultId;
pullNodeId?: NodeId;
pullVaultNameOrId?: VaultId | VaultName;
tran?: DBTransaction;
}): Promise<void> {
if (tran == null) {
return this.db.withTransactionF((tran) =>
this.pullVault({ vaultId, pullNodeId, pullVaultNameOrId, tran }),
);
}
if ((await this.getVaultName(vaultId, tran)) == null) return;
await this.vaultLocks.withF(
[vaultId.toString(), RWLockWriter, 'write'],
async () => {
await tran.lock([...this.vaultsDbPath, vaultId].join(''));
const vault = await this.getVault(vaultId, tran);
await vault.pullVault({
nodeManager: this.nodeManager,
pullNodeId,
pullVaultNameOrId,
tran,
});
},
);
}
/**
* Handler for receiving http GET requests when being
* cloned or pulled from
*/
@ready(new vaultsErrors.ErrorVaultManagerNotRunning())
public async *handleInfoRequest(
vaultId: VaultId,
tran?: DBTransaction,
): AsyncGenerator<Buffer, void, void> {
if (tran == null) {
const handleInfoRequest = (tran: DBTransaction) =>
this.handleInfoRequest(vaultId, tran);
return yield* this.db.withTransactionG(async function* (tran) {
return yield* handleInfoRequest(tran);
});
}
const efs = this.efs;
const vault = await this.getVault(vaultId, tran);
return yield* withG(
[
this.vaultLocks.lock([vaultId.toString(), RWLockWriter, 'read']),
vault.getLock().read(),
],
async function* (): AsyncGenerator<Buffer, void, void> {
// Read the commit state of the vault
yield* gitHttp.advertiseRefGenerator({
efs,
dir: path.join(vaultsUtils.encodeVaultId(vaultId), 'contents'),
gitDir: path.join(vaultsUtils.encodeVaultId(vaultId), '.git'),
});
},
);
}
/**
* Handler for receiving http POST requests when being
* cloned or pulled from
*/
@ready(new vaultsErrors.ErrorVaultManagerNotRunning())
public async *handlePackRequest(
vaultId: VaultId,
body: Array<Buffer>,
tran?: DBTransaction,
): AsyncGenerator<Buffer, void, void> {
if (tran == null) {
// Lambda to maintain `this` context
const handlePackRequest = (tran: DBTransaction) =>
this.handlePackRequest(vaultId, body, tran);
return yield* this.db.withTransactionG(async function* (tran) {
return yield* handlePackRequest(tran);
});
}
const vault = await this.getVault(vaultId, tran);
const efs = this.efs;
yield* withG(
[
this.vaultLocks.lock([vaultId.toString(), RWLockWriter, 'read']),
vault.getLock().read(),
],
async function* (): AsyncGenerator<Buffer, void, void> {
yield* gitHttp.generatePackRequest({
efs,
dir: path.join(vaultsUtils.encodeVaultId(vaultId), 'contents'),
gitDir: path.join(vaultsUtils.encodeVaultId(vaultId), '.git'),
body: body,
});
},
);
}
/**
* Retrieves all the vaults for a peers node
*/
public async *scanVaults(targetNodeId: NodeId): AsyncGenerator<{
vaultName: VaultName;
vaultIdEncoded: VaultIdEncoded;
vaultPermissions: VaultAction[];
}> {
// Create a connection to another node
return yield* this.nodeManager.withConnG(
targetNodeId,
async function* (connection): AsyncGenerator<{
vaultName: VaultName;
vaultIdEncoded: VaultIdEncoded;
vaultPermissions: VaultAction[];
}> {
const client = connection.getClient();
const genReadable = await client.methods.vaultsScan({});
for await (const vault of genReadable) {
const vaultName = vault.vaultName;
const vaultIdEncoded = vault.vaultIdEncoded;
const vaultPermissions = vault.vaultPermissions;
yield { vaultName, vaultIdEncoded, vaultPermissions };
}
},
);
}
/**
* Returns all the shared vaults for a NodeId.
*/
public async *handleScanVaults(
nodeId: NodeId,
tran?: DBTransaction,
): AsyncGenerator<{
vaultId: VaultId;
vaultName: VaultName;
vaultPermissions: VaultAction[];
}> {
if (tran == null) {
// Lambda to maintain `this` context
const handleScanVaults = (tran: DBTransaction) =>
this.handleScanVaults(nodeId, tran);
return yield* this.db.withTransactionG(async function* (tran) {
return yield* handleScanVaults(tran);
});
}
// Checking permission
const nodeIdEncoded = nodesUtils.encodeNodeId(nodeId);
const permissions = await this.acl.getNodePerm(nodeId, tran);
if (permissions == null) {
throw new vaultsErrors.ErrorVaultsPermissionDenied(
`No permissions found for ${nodeIdEncoded}`,
);
}
if (permissions.gestalt.scan === undefined) {
throw new vaultsErrors.ErrorVaultsPermissionDenied(
`Scanning is not allowed for ${nodeIdEncoded}`,
);
}
// Getting the list of vaults
const vaults = permissions.vaults;
for (const vaultIdString of Object.keys(vaults)) {
// Getting vault permissions
const vaultId = IdInternal.fromString<VaultId>(vaultIdString);
const vaultPermissions = Object.keys(
vaults[vaultIdString],
) as VaultAction[];
// Getting the vault name
const metadata = await this.getVaultMeta(vaultId, tran);
const vaultName = metadata!.vaultName;
const element = {
vaultId,
vaultName,
vaultPermissions,
};
yield element;
}
}
@ready(new vaultsErrors.ErrorVaultManagerNotRunning())
protected async generateVaultId(): Promise<VaultId> {
let vaultId = this.vaultIdGenerator();
let i = 0;
while (await this.efs.exists(vaultsUtils.encodeVaultId(vaultId))) {
i++;
if (i > 50) {
throw new vaultsErrors.ErrorVaultsCreateVaultId(
'Could not create a unique vaultId after 50 attempts',
);
}
vaultId = this.vaultIdGenerator();
}
return vaultId;
}
@ready(new vaultsErrors.ErrorVaultManagerNotRunning())
protected async getVault(
vaultId: VaultId,
tran: DBTransaction,
): Promise<VaultInternal> {
if (tran == null) {
return this.db.withTransactionF((tran) => this.getVault(vaultId, tran));
}
const vaultIdString = vaultId.toString() as VaultIdString;
// 1. get the vault, if it exists then return that
const vault = this.vaultMap.get(vaultIdString);
if (vault != null) return vault;
// No vault or state exists then we throw error?
if ((await this.getVaultMeta(vaultId, tran)) == null) {
throw new vaultsErrors.ErrorVaultsVaultUndefined(
`Vault ${vaultsUtils.encodeVaultId(vaultId)} doesn't exist`,
);
}
// 2. if the state exists then create, add to map and return that
const newVault = await VaultInternal.createVaultInternal({
vaultId,
keyRing: this.keyRing,
efs: this.efs,
logger: this.logger.getChild(VaultInternal.name),
db: this.db,
vaultsDbPath: this.vaultsDbPath,
tran,
});
this.vaultMap.set(vaultIdString, newVault);
return newVault;
}
/**