@git-stunts/git-cas 5.0.0 → 5.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +49 -0
- package/README.md +120 -7
- package/bin/actions.js +6 -0
- package/bin/git-cas.js +213 -26
- package/index.d.ts +35 -2
- package/index.js +178 -1
- package/package.json +1 -1
- package/src/domain/schemas/ManifestSchema.d.ts +12 -0
- package/src/domain/schemas/ManifestSchema.js +12 -0
- package/src/domain/services/CasService.d.ts +22 -0
- package/src/domain/services/CasService.js +382 -61
- package/src/domain/value-objects/Manifest.d.ts +12 -0
- package/src/domain/value-objects/Manifest.js +3 -1
package/index.js
CHANGED
|
@@ -9,6 +9,7 @@ import { Readable, Transform } from 'node:stream';
|
|
|
9
9
|
import { pipeline } from 'node:stream/promises';
|
|
10
10
|
import CasService from './src/domain/services/CasService.js';
|
|
11
11
|
import VaultService from './src/domain/services/VaultService.js';
|
|
12
|
+
import CasError from './src/domain/errors/CasError.js';
|
|
12
13
|
import GitPersistenceAdapter from './src/infrastructure/adapters/GitPersistenceAdapter.js';
|
|
13
14
|
import GitRefAdapter from './src/infrastructure/adapters/GitRefAdapter.js';
|
|
14
15
|
import NodeCryptoAdapter from './src/infrastructure/adapters/NodeCryptoAdapter.js';
|
|
@@ -265,9 +266,10 @@ export default class ContentAddressableStore {
|
|
|
265
266
|
* @param {string} [options.passphrase] - Derive encryption key from passphrase.
|
|
266
267
|
* @param {Object} [options.kdfOptions] - KDF options when using passphrase.
|
|
267
268
|
* @param {{ algorithm: 'gzip' }} [options.compression] - Enable compression.
|
|
269
|
+
* @param {Array<{label: string, key: Buffer}>} [options.recipients] - Envelope recipients (mutually exclusive with encryptionKey/passphrase).
|
|
268
270
|
* @returns {Promise<import('./src/domain/value-objects/Manifest.js').default>} The resulting manifest.
|
|
269
271
|
*/
|
|
270
|
-
async storeFile({ filePath, slug, filename, encryptionKey, passphrase, kdfOptions, compression }) {
|
|
272
|
+
async storeFile({ filePath, slug, filename, encryptionKey, passphrase, kdfOptions, compression, recipients }) {
|
|
271
273
|
const source = createReadStream(filePath);
|
|
272
274
|
const service = await this.#getService();
|
|
273
275
|
return await service.store({
|
|
@@ -278,6 +280,7 @@ export default class ContentAddressableStore {
|
|
|
278
280
|
passphrase,
|
|
279
281
|
kdfOptions,
|
|
280
282
|
compression,
|
|
283
|
+
recipients,
|
|
281
284
|
});
|
|
282
285
|
}
|
|
283
286
|
|
|
@@ -291,6 +294,7 @@ export default class ContentAddressableStore {
|
|
|
291
294
|
* @param {string} [options.passphrase] - Derive encryption key from passphrase.
|
|
292
295
|
* @param {Object} [options.kdfOptions] - KDF options when using passphrase.
|
|
293
296
|
* @param {{ algorithm: 'gzip' }} [options.compression] - Enable compression.
|
|
297
|
+
* @param {Array<{label: string, key: Buffer}>} [options.recipients] - Envelope recipients (mutually exclusive with encryptionKey/passphrase).
|
|
294
298
|
* @returns {Promise<import('./src/domain/value-objects/Manifest.js').default>} The resulting manifest.
|
|
295
299
|
*/
|
|
296
300
|
async store(options) {
|
|
@@ -421,6 +425,60 @@ export default class ContentAddressableStore {
|
|
|
421
425
|
return await service.deriveKey(options);
|
|
422
426
|
}
|
|
423
427
|
|
|
428
|
+
// ---------------------------------------------------------------------------
|
|
429
|
+
// Recipient management — delegates to CasService
|
|
430
|
+
// ---------------------------------------------------------------------------
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* Adds a recipient to an envelope-encrypted manifest.
|
|
434
|
+
* @param {Object} options
|
|
435
|
+
* @param {import('./src/domain/value-objects/Manifest.js').default} options.manifest
|
|
436
|
+
* @param {Buffer} options.existingKey - KEK of an existing recipient.
|
|
437
|
+
* @param {Buffer} options.newRecipientKey - KEK for the new recipient.
|
|
438
|
+
* @param {string} options.label - Label for the new recipient.
|
|
439
|
+
* @returns {Promise<import('./src/domain/value-objects/Manifest.js').default>}
|
|
440
|
+
*/
|
|
441
|
+
async addRecipient(options) {
|
|
442
|
+
const service = await this.#getService();
|
|
443
|
+
return await service.addRecipient(options);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* Removes a recipient from an envelope-encrypted manifest.
|
|
448
|
+
* @param {Object} options
|
|
449
|
+
* @param {import('./src/domain/value-objects/Manifest.js').default} options.manifest
|
|
450
|
+
* @param {string} options.label - Label to remove.
|
|
451
|
+
* @returns {Promise<import('./src/domain/value-objects/Manifest.js').default>}
|
|
452
|
+
*/
|
|
453
|
+
async removeRecipient(options) {
|
|
454
|
+
const service = await this.#getService();
|
|
455
|
+
return await service.removeRecipient(options);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* Lists recipient labels from an envelope-encrypted manifest.
|
|
460
|
+
* @param {import('./src/domain/value-objects/Manifest.js').default} manifest
|
|
461
|
+
* @returns {Promise<string[]>}
|
|
462
|
+
*/
|
|
463
|
+
async listRecipients(manifest) {
|
|
464
|
+
const service = await this.#getService();
|
|
465
|
+
return service.listRecipients(manifest);
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* Rotates a recipient's key without re-encrypting data blobs.
|
|
470
|
+
* @param {Object} options
|
|
471
|
+
* @param {import('./src/domain/value-objects/Manifest.js').default} options.manifest
|
|
472
|
+
* @param {Buffer} options.oldKey - Current KEK of the recipient to rotate.
|
|
473
|
+
* @param {Buffer} options.newKey - New KEK to wrap the DEK with.
|
|
474
|
+
* @param {string} [options.label] - If provided, only rotate the named recipient.
|
|
475
|
+
* @returns {Promise<import('./src/domain/value-objects/Manifest.js').default>}
|
|
476
|
+
*/
|
|
477
|
+
async rotateKey(options) {
|
|
478
|
+
const service = await this.#getService();
|
|
479
|
+
return await service.rotateKey(options);
|
|
480
|
+
}
|
|
481
|
+
|
|
424
482
|
// ---------------------------------------------------------------------------
|
|
425
483
|
// Vault — delegates to VaultService
|
|
426
484
|
// ---------------------------------------------------------------------------
|
|
@@ -462,4 +520,123 @@ export default class ContentAddressableStore {
|
|
|
462
520
|
const vault = await this.#getVault();
|
|
463
521
|
return vault.getVaultMetadata();
|
|
464
522
|
}
|
|
523
|
+
|
|
524
|
+
// ---------------------------------------------------------------------------
|
|
525
|
+
// Key rotation — orchestrates CasService + VaultService
|
|
526
|
+
// ---------------------------------------------------------------------------
|
|
527
|
+
|
|
528
|
+
/**
|
|
529
|
+
* Rotates the vault-level passphrase. Re-wraps every envelope-encrypted
|
|
530
|
+
* entry's DEK with a new KEK derived from `newPassphrase`. Entries using
|
|
531
|
+
* direct-key encryption are skipped.
|
|
532
|
+
*
|
|
533
|
+
* @param {Object} options
|
|
534
|
+
* @param {string} options.oldPassphrase - Current vault passphrase.
|
|
535
|
+
* @param {string} options.newPassphrase - New vault passphrase.
|
|
536
|
+
* @param {Object} [options.kdfOptions] - KDF options for new passphrase.
|
|
537
|
+
* @returns {Promise<{ commitOid: string, rotatedSlugs: string[], skippedSlugs: string[] }>}
|
|
538
|
+
*/
|
|
539
|
+
async rotateVaultPassphrase({ oldPassphrase, newPassphrase, kdfOptions }) {
|
|
540
|
+
const service = await this.#getService();
|
|
541
|
+
const vault = await this.#getVault();
|
|
542
|
+
|
|
543
|
+
const MAX_RETRIES = 3;
|
|
544
|
+
const RETRY_BASE_MS = 50;
|
|
545
|
+
|
|
546
|
+
for (let attempt = 0; attempt < MAX_RETRIES; attempt++) {
|
|
547
|
+
const state = await vault.readState();
|
|
548
|
+
if (!state.metadata?.encryption) {
|
|
549
|
+
throw new CasError('Vault is not encrypted — nothing to rotate', 'VAULT_METADATA_INVALID');
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
const { kdf } = state.metadata.encryption;
|
|
553
|
+
const oldKek = await ContentAddressableStore.#deriveKekFromKdf(service, oldPassphrase, kdf);
|
|
554
|
+
const { key: newKek, salt: newSalt, params: newParams } = await service.deriveKey({
|
|
555
|
+
passphrase: newPassphrase, ...kdfOptions, algorithm: kdfOptions?.algorithm || kdf.algorithm,
|
|
556
|
+
});
|
|
557
|
+
|
|
558
|
+
const result = await ContentAddressableStore.#rotateEntries({ service, entries: state.entries, oldKek, newKek });
|
|
559
|
+
const newMetadata = ContentAddressableStore.#buildRotatedMetadata(state.metadata, newSalt, newParams);
|
|
560
|
+
|
|
561
|
+
try {
|
|
562
|
+
const { commitOid } = await vault.writeCommit({
|
|
563
|
+
entries: result.updatedEntries,
|
|
564
|
+
metadata: newMetadata,
|
|
565
|
+
parentCommitOid: state.parentCommitOid,
|
|
566
|
+
message: `vault: rotate passphrase (${result.rotatedSlugs.length} rotated, ${result.skippedSlugs.length} skipped)`,
|
|
567
|
+
});
|
|
568
|
+
return { commitOid, rotatedSlugs: result.rotatedSlugs, skippedSlugs: result.skippedSlugs };
|
|
569
|
+
} catch (err) {
|
|
570
|
+
if (err instanceof CasError && err.code === 'VAULT_CONFLICT' && attempt < MAX_RETRIES - 1) {
|
|
571
|
+
await new Promise((r) => setTimeout(r, RETRY_BASE_MS * (2 ** attempt)));
|
|
572
|
+
continue;
|
|
573
|
+
}
|
|
574
|
+
throw err;
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
/* c8 ignore next 2 */
|
|
578
|
+
throw new CasError('Vault CAS retries exhausted', 'VAULT_CONFLICT');
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
/**
|
|
582
|
+
* Derives a KEK from a passphrase using stored KDF params.
|
|
583
|
+
* @private
|
|
584
|
+
*/
|
|
585
|
+
static async #deriveKekFromKdf(service, passphrase, kdf) {
|
|
586
|
+
const { key } = await service.deriveKey({
|
|
587
|
+
passphrase,
|
|
588
|
+
salt: Buffer.from(kdf.salt, 'base64'),
|
|
589
|
+
algorithm: kdf.algorithm,
|
|
590
|
+
iterations: kdf.iterations,
|
|
591
|
+
cost: kdf.cost,
|
|
592
|
+
blockSize: kdf.blockSize,
|
|
593
|
+
parallelization: kdf.parallelization,
|
|
594
|
+
});
|
|
595
|
+
return key;
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
/**
|
|
599
|
+
* Iterates vault entries, rotating envelope-encrypted ones and skipping others.
|
|
600
|
+
* @private
|
|
601
|
+
*/
|
|
602
|
+
static async #rotateEntries({ service, entries, oldKek, newKek }) {
|
|
603
|
+
const rotatedSlugs = [];
|
|
604
|
+
const skippedSlugs = [];
|
|
605
|
+
const updatedEntries = new Map(entries);
|
|
606
|
+
|
|
607
|
+
for (const [slug, treeOid] of entries) {
|
|
608
|
+
const manifest = await service.readManifest({ treeOid });
|
|
609
|
+
if (!manifest.encryption?.recipients?.length) {
|
|
610
|
+
skippedSlugs.push(slug);
|
|
611
|
+
continue;
|
|
612
|
+
}
|
|
613
|
+
const rotated = await service.rotateKey({ manifest, oldKey: oldKek, newKey: newKek });
|
|
614
|
+
updatedEntries.set(slug, await service.createTree({ manifest: rotated }));
|
|
615
|
+
rotatedSlugs.push(slug);
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
return { updatedEntries, rotatedSlugs, skippedSlugs };
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
/**
|
|
622
|
+
* Builds updated vault metadata with new KDF params.
|
|
623
|
+
* @private
|
|
624
|
+
*/
|
|
625
|
+
static #buildRotatedMetadata(metadata, newSalt, newParams) {
|
|
626
|
+
return {
|
|
627
|
+
...metadata,
|
|
628
|
+
encryption: {
|
|
629
|
+
cipher: metadata.encryption.cipher,
|
|
630
|
+
kdf: {
|
|
631
|
+
algorithm: newParams.algorithm,
|
|
632
|
+
salt: newSalt.toString('base64'),
|
|
633
|
+
...('iterations' in newParams && { iterations: newParams.iterations }),
|
|
634
|
+
...('cost' in newParams && { cost: newParams.cost }),
|
|
635
|
+
...('blockSize' in newParams && { blockSize: newParams.blockSize }),
|
|
636
|
+
...('parallelization' in newParams && { parallelization: newParams.parallelization }),
|
|
637
|
+
keyLength: newParams.keyLength,
|
|
638
|
+
},
|
|
639
|
+
},
|
|
640
|
+
};
|
|
641
|
+
}
|
|
465
642
|
}
|
package/package.json
CHANGED
|
@@ -24,6 +24,16 @@ export declare const KdfSchema: z.ZodObject<{
|
|
|
24
24
|
keyLength: z.ZodDefault<z.ZodNumber>;
|
|
25
25
|
}>;
|
|
26
26
|
|
|
27
|
+
/** Validates a single recipient entry in an envelope-encrypted manifest. */
|
|
28
|
+
export declare const RecipientSchema: z.ZodObject<{
|
|
29
|
+
label: z.ZodString;
|
|
30
|
+
wrappedDek: z.ZodString;
|
|
31
|
+
nonce: z.ZodString;
|
|
32
|
+
tag: z.ZodString;
|
|
33
|
+
kekType: z.ZodOptional<z.ZodString>;
|
|
34
|
+
keyVersion: z.ZodOptional<z.ZodNumber>;
|
|
35
|
+
}>;
|
|
36
|
+
|
|
27
37
|
/** Validates the encryption metadata attached to an encrypted manifest. */
|
|
28
38
|
export declare const EncryptionSchema: z.ZodObject<{
|
|
29
39
|
algorithm: z.ZodString;
|
|
@@ -31,6 +41,8 @@ export declare const EncryptionSchema: z.ZodObject<{
|
|
|
31
41
|
tag: z.ZodString;
|
|
32
42
|
encrypted: z.ZodDefault<z.ZodBoolean>;
|
|
33
43
|
kdf: z.ZodOptional<typeof KdfSchema>;
|
|
44
|
+
recipients: z.ZodOptional<z.ZodArray<typeof RecipientSchema>>;
|
|
45
|
+
keyVersion: z.ZodOptional<z.ZodNumber>;
|
|
34
46
|
}>;
|
|
35
47
|
|
|
36
48
|
/** Validates compression metadata. */
|
|
@@ -24,6 +24,16 @@ export const KdfSchema = z.object({
|
|
|
24
24
|
keyLength: z.number().int().positive().default(32),
|
|
25
25
|
});
|
|
26
26
|
|
|
27
|
+
/** Validates a single recipient entry in an envelope-encrypted manifest. */
|
|
28
|
+
export const RecipientSchema = z.object({
|
|
29
|
+
label: z.string().min(1),
|
|
30
|
+
wrappedDek: z.string().min(1),
|
|
31
|
+
nonce: z.string().min(1),
|
|
32
|
+
tag: z.string().min(1),
|
|
33
|
+
kekType: z.string().optional(),
|
|
34
|
+
keyVersion: z.number().int().min(0).optional(),
|
|
35
|
+
});
|
|
36
|
+
|
|
27
37
|
/** Validates the encryption metadata attached to an encrypted manifest. */
|
|
28
38
|
export const EncryptionSchema = z.object({
|
|
29
39
|
algorithm: z.string(),
|
|
@@ -31,6 +41,8 @@ export const EncryptionSchema = z.object({
|
|
|
31
41
|
tag: z.string(),
|
|
32
42
|
encrypted: z.boolean().default(true),
|
|
33
43
|
kdf: KdfSchema.optional(),
|
|
44
|
+
recipients: z.array(RecipientSchema).min(1).optional(),
|
|
45
|
+
keyVersion: z.number().int().min(0).optional(),
|
|
34
46
|
});
|
|
35
47
|
|
|
36
48
|
/** Validates compression metadata. */
|
|
@@ -112,6 +112,7 @@ export default class CasService {
|
|
|
112
112
|
passphrase?: string;
|
|
113
113
|
kdfOptions?: Omit<DeriveKeyOptions, "passphrase">;
|
|
114
114
|
compression?: { algorithm: "gzip" };
|
|
115
|
+
recipients?: Array<{ label: string; key: Buffer }>;
|
|
115
116
|
}): Promise<Manifest>;
|
|
116
117
|
|
|
117
118
|
createTree(options: { manifest: Manifest }): Promise<string>;
|
|
@@ -138,6 +139,27 @@ export default class CasService {
|
|
|
138
139
|
treeOids: string[];
|
|
139
140
|
}): Promise<{ referenced: Set<string>; total: number }>;
|
|
140
141
|
|
|
142
|
+
addRecipient(options: {
|
|
143
|
+
manifest: Manifest;
|
|
144
|
+
existingKey: Buffer;
|
|
145
|
+
newRecipientKey: Buffer;
|
|
146
|
+
label: string;
|
|
147
|
+
}): Promise<Manifest>;
|
|
148
|
+
|
|
149
|
+
removeRecipient(options: {
|
|
150
|
+
manifest: Manifest;
|
|
151
|
+
label: string;
|
|
152
|
+
}): Promise<Manifest>;
|
|
153
|
+
|
|
154
|
+
listRecipients(manifest: Manifest): string[];
|
|
155
|
+
|
|
156
|
+
rotateKey(options: {
|
|
157
|
+
manifest: Manifest;
|
|
158
|
+
oldKey: Buffer;
|
|
159
|
+
newKey: Buffer;
|
|
160
|
+
label?: string;
|
|
161
|
+
}): Promise<Manifest>;
|
|
162
|
+
|
|
141
163
|
verifyIntegrity(manifest: Manifest): Promise<boolean>;
|
|
142
164
|
|
|
143
165
|
deriveKey(options: DeriveKeyOptions): Promise<DeriveKeyResult>;
|