@git-stunts/git-cas 5.3.2 → 6.0.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/ADVANCED_GUIDE.md +1027 -0
- package/ARCHITECTURE.md +514 -0
- package/CHANGELOG.md +449 -5
- package/CODE_OF_CONDUCT.md +33 -0
- package/GUIDE.md +926 -0
- package/README.md +106 -366
- package/SECURITY.md +993 -0
- package/SUPPORT.md +54 -0
- package/UPGRADING.md +299 -0
- package/bin/actions.js +70 -21
- package/bin/agent/cli.js +51 -0
- package/bin/agent/commands/doctor.js +99 -0
- package/bin/agent/commands/index.js +1489 -0
- package/bin/agent/input.js +619 -0
- package/bin/agent/passphrase-source.js +123 -0
- package/bin/agent/protocol.js +193 -0
- package/bin/build-version.js +64 -0
- package/bin/credentials.js +425 -0
- package/bin/git-cas.js +595 -449
- package/bin/passphrase-source.js +266 -0
- package/bin/restore-output-target.js +28 -0
- package/bin/ui/blocks/asset-card.js +109 -0
- package/bin/ui/blocks/health-dashboard.js +149 -0
- package/bin/ui/blocks/heatmap-block.js +67 -0
- package/bin/ui/blocks/merkle-explorer.js +163 -0
- package/bin/ui/blocks/operation-feed.js +176 -0
- package/bin/ui/blocks/wizard-block.js +168 -0
- package/bin/ui/components/short-sha.js +36 -0
- package/bin/ui/context.js +56 -1
- package/bin/ui/dashboard-cmds.js +1578 -13
- package/bin/ui/dashboard-view.js +612 -145
- package/bin/ui/dashboard.js +1255 -212
- package/bin/ui/encryption-card.js +69 -26
- package/bin/ui/heatmap.js +26 -54
- package/bin/ui/history-timeline.js +51 -19
- package/bin/ui/manifest-view.js +115 -60
- package/bin/ui/merkle-dag.js +83 -0
- package/bin/ui/progress.js +15 -9
- package/bin/ui/repo-treemap.js +328 -0
- package/bin/ui/shaders/bak-sneppen.js +180 -0
- package/bin/ui/shaders/fractal-zoom.js +99 -0
- package/bin/ui/shaders/merkle-tree.js +142 -0
- package/bin/ui/shaders/organic-flow.js +96 -0
- package/bin/ui/shaders/phase-portrait.js +93 -0
- package/bin/ui/store-wizard.js +367 -0
- package/bin/ui/theme.js +155 -0
- package/bin/ui/vault-report.js +495 -0
- package/build-info.json +4 -0
- package/docs/API.md +2254 -0
- package/docs/ENCRYPTION_MODES.md +33 -0
- package/docs/EXTENDING.md +174 -0
- package/docs/STORE_RESTORE_PIPELINE.md +199 -0
- package/docs/THREAT_MODEL.md +269 -0
- package/docs/VAULT_INTERNALS.md +251 -0
- package/docs/WALKTHROUGH.md +1868 -0
- package/docs/demo.gif +0 -0
- package/docs/releases/v6.0.0.md +104 -0
- package/examples/README.md +260 -0
- package/index.d.ts +273 -80
- package/index.js +220 -59
- package/package.json +35 -6
- package/scripts/migrate-encryption.js +680 -0
- package/src/domain/bytes/ByteLayout.js +96 -0
- package/src/domain/encoding/base64.js +91 -0
- package/src/domain/encoding/hex.js +14 -0
- package/src/domain/encoding/utf8.js +123 -0
- package/src/domain/encryption/schemes.js +132 -0
- package/src/domain/errors/CannotRemoveLastRecipientError.js +10 -0
- package/src/domain/errors/CasError.d.ts +28 -0
- package/src/domain/errors/CasError.js +43 -6
- package/src/domain/errors/Codes.js +54 -0
- package/src/domain/errors/DekUnwrapFailedError.js +10 -0
- package/src/domain/errors/GitError.js +10 -0
- package/src/domain/errors/GitPlumbingInitializationError.js +10 -0
- package/src/domain/errors/IntegrityError.js +10 -0
- package/src/domain/errors/InvalidChunkingStrategyError.js +10 -0
- package/src/domain/errors/InvalidOidError.js +10 -0
- package/src/domain/errors/InvalidOptionsError.js +10 -0
- package/src/domain/errors/ManifestIntegrityError.js +10 -0
- package/src/domain/errors/ManifestNotFoundError.js +10 -0
- package/src/domain/errors/NoMatchingRecipientError.js +10 -0
- package/src/domain/errors/PersistenceCapabilityRequiredError.js +10 -0
- package/src/domain/errors/PortNotImplementedError.js +10 -0
- package/src/domain/errors/RecipientAlreadyExistsError.js +10 -0
- package/src/domain/errors/RecipientNotFoundError.js +10 -0
- package/src/domain/errors/RestoreTooLargeError.js +10 -0
- package/src/domain/errors/RotationNotSupportedError.js +10 -0
- package/src/domain/errors/createCasError.js +68 -0
- package/src/domain/errors/index.js +20 -0
- package/src/domain/helpers/buildKdfMetadata.js +4 -2
- package/src/domain/helpers/codecBytes.js +34 -0
- package/src/domain/helpers/gitRefErrors.js +84 -0
- package/src/domain/helpers/scryptMaxmem.js +22 -0
- package/src/domain/outcomes/RestoreFailure.js +15 -0
- package/src/domain/outcomes/RestoreOutcome.js +11 -0
- package/src/domain/outcomes/RestoreSuccess.js +34 -0
- package/src/domain/outcomes/StoreFailure.js +15 -0
- package/src/domain/outcomes/StoreOutcome.js +11 -0
- package/src/domain/outcomes/StoreSuccess.js +17 -0
- package/src/domain/schemas/ManifestSchema.d.ts +40 -9
- package/src/domain/schemas/ManifestSchema.js +67 -13
- package/src/domain/services/CasService.d.ts +153 -53
- package/src/domain/services/CasService.js +322 -837
- package/src/domain/services/ChunkRepository.js +268 -0
- package/src/domain/services/CompressionStreams.js +72 -0
- package/src/domain/services/ConvergentEncryption.js +128 -0
- package/src/domain/services/GitTreeBuilder.js +86 -0
- package/src/domain/services/IntegrityVerifier.js +199 -0
- package/src/domain/services/KeyResolver.js +49 -42
- package/src/domain/services/ManifestDiff.js +56 -0
- package/src/domain/services/ManifestRepository.js +286 -0
- package/src/domain/services/PrefetchWindow.js +48 -0
- package/src/domain/services/RecipientService.js +157 -0
- package/src/domain/services/RedactingObservability.js +169 -0
- package/src/domain/services/RestorePipeline.js +68 -0
- package/src/domain/services/StorePipeline.js +201 -0
- package/src/domain/services/VaultKeyVerifier.js +101 -0
- package/src/domain/services/VaultMetadataCodec.js +206 -0
- package/src/domain/services/VaultMutationRetryPolicy.js +105 -0
- package/src/domain/services/VaultPersistence.js +410 -0
- package/src/domain/services/VaultPrivacyIndex.js +191 -0
- package/src/domain/services/VaultService.js +749 -251
- package/src/domain/services/VaultStateCache.js +225 -0
- package/src/domain/services/VaultTreeCodec.js +121 -0
- package/src/domain/services/rotateVaultPassphrase.js +37 -20
- package/src/domain/strategies/Aad.js +39 -0
- package/src/domain/strategies/FramedRecordCodec.js +204 -0
- package/src/domain/strategies/RestoreCompressed.js +40 -0
- package/src/domain/strategies/RestoreConvergent.js +44 -0
- package/src/domain/strategies/RestoreFramed.js +55 -0
- package/src/domain/strategies/RestorePlain.js +37 -0
- package/src/domain/strategies/RestoreStrategy.js +35 -0
- package/src/domain/strategies/RestoreWhole.js +135 -0
- package/src/domain/strategies/StoreConvergent.js +28 -0
- package/src/domain/strategies/StoreFramed.js +39 -0
- package/src/domain/strategies/StorePlain.js +20 -0
- package/src/domain/strategies/StoreStrategy.js +61 -0
- package/src/domain/strategies/StoreWhole.js +34 -0
- package/src/domain/value-objects/Chunk.js +1 -2
- package/src/domain/value-objects/EncryptionMetadata.js +87 -0
- package/src/domain/value-objects/Manifest.d.ts +40 -4
- package/src/domain/value-objects/Manifest.js +61 -20
- package/src/domain/value-objects/Oid.js +45 -0
- package/src/domain/value-objects/Slug.js +124 -0
- package/src/domain/value-objects/StoreEncryptionConfig.js +114 -0
- package/src/helpers/aesGcmMeta.js +55 -0
- package/src/helpers/canonicalBase64.js +1 -0
- package/src/helpers/kdfPolicy.js +194 -0
- package/src/infrastructure/adapters/BunCryptoAdapter.js +124 -8
- package/src/infrastructure/adapters/FileIOHelper.js +180 -13
- package/src/infrastructure/adapters/GitPersistenceAdapter.js +235 -42
- package/src/infrastructure/adapters/GitPlumbingFactoryAdapter.js +62 -0
- package/src/infrastructure/adapters/GitRefAdapter.js +19 -6
- package/src/infrastructure/adapters/NodeCompressionAdapter.js +64 -0
- package/src/infrastructure/adapters/NodeCryptoAdapter.js +130 -10
- package/src/infrastructure/adapters/WebCryptoAdapter.js +187 -77
- package/src/infrastructure/chunkers/CdcChunker.js +53 -19
- package/src/infrastructure/chunkers/FixedChunker.js +15 -7
- package/src/infrastructure/chunkers/resolveChunker.js +10 -6
- package/src/infrastructure/codecs/CborCodec.js +3 -3
- package/src/infrastructure/codecs/JsonCodec.js +5 -5
- package/src/infrastructure/createGitPlumbing.js +22 -0
- package/src/package-version.js +1 -0
- package/src/ports/ChunkingPort.js +3 -3
- package/src/ports/CodecPort.js +4 -4
- package/src/ports/CompressionPort.js +57 -0
- package/src/ports/CryptoPort.js +138 -50
- package/src/ports/GitPersistencePort.js +32 -2
- package/src/ports/GitPlumbingFactoryPort.js +16 -0
- package/src/ports/GitRefPort.js +1 -1
- package/src/types/ambient.d.ts +10 -5
- package/test/CONVENTIONS.md +94 -0
- package/test/cycles/README.md +13 -0
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import {
|
|
3
|
+
hasAgentPassphraseSource,
|
|
4
|
+
validateAgentPassphraseSource,
|
|
5
|
+
} from './agent/passphrase-source.js';
|
|
6
|
+
import {
|
|
7
|
+
hasExplicitPassphraseSource,
|
|
8
|
+
hasPassphraseSource,
|
|
9
|
+
resolvePassphrase,
|
|
10
|
+
validatePassphraseSources,
|
|
11
|
+
} from './passphrase-source.js';
|
|
12
|
+
|
|
13
|
+
const UNENCRYPTED_VAULT_PASSPHRASE_IGNORED_MESSAGE =
|
|
14
|
+
'passphrase ignored (vault is not encrypted)';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @param {string} message
|
|
18
|
+
* @returns {Error}
|
|
19
|
+
*/
|
|
20
|
+
function defaultErrorFactory(message) {
|
|
21
|
+
return new Error(message);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @param {string} keyFilePath
|
|
26
|
+
* @param {{
|
|
27
|
+
* readFile?: (filePath: string) => Uint8Array,
|
|
28
|
+
* errorFactory?: (message: string) => Error,
|
|
29
|
+
* }} [options]
|
|
30
|
+
* @returns {Uint8Array}
|
|
31
|
+
*/
|
|
32
|
+
export function readKeyFile(keyFilePath, {
|
|
33
|
+
readFile = (filePath) => readFileSync(filePath),
|
|
34
|
+
errorFactory = defaultErrorFactory,
|
|
35
|
+
} = {}) {
|
|
36
|
+
const key = readFile(keyFilePath);
|
|
37
|
+
if (key.length !== 32) {
|
|
38
|
+
throw errorFactory(`Invalid key length: expected 32 bytes, got ${key.length} (${keyFilePath})`);
|
|
39
|
+
}
|
|
40
|
+
return key;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Derives and verifies the vault encryption key from stored KDF metadata.
|
|
45
|
+
*
|
|
46
|
+
* @param {{ deriveKey: Function, verifyVaultKey: Function }} cas
|
|
47
|
+
* @param {{ encryption?: { kdf?: Record<string, any> } }} metadata
|
|
48
|
+
* @param {string} passphrase
|
|
49
|
+
* @returns {Promise<Uint8Array>}
|
|
50
|
+
*/
|
|
51
|
+
export async function deriveVaultKey(cas, metadata, passphrase) {
|
|
52
|
+
if (!metadata.encryption?.kdf) {
|
|
53
|
+
throw new Error('Missing or malformed encryption metadata');
|
|
54
|
+
}
|
|
55
|
+
const { kdf } = metadata.encryption;
|
|
56
|
+
const { key } = await cas.deriveKey({
|
|
57
|
+
passphrase,
|
|
58
|
+
salt: Buffer.from(kdf.salt, 'base64'),
|
|
59
|
+
algorithm: /** @type {"pbkdf2" | "scrypt"} */ (kdf.algorithm),
|
|
60
|
+
iterations: kdf.iterations,
|
|
61
|
+
cost: kdf.cost,
|
|
62
|
+
blockSize: kdf.blockSize,
|
|
63
|
+
parallelization: kdf.parallelization,
|
|
64
|
+
keyLength: kdf.keyLength,
|
|
65
|
+
});
|
|
66
|
+
await cas.verifyVaultKey({ encryptionKey: key });
|
|
67
|
+
return key;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* @param {Record<string, any>} opts
|
|
72
|
+
* @param {{
|
|
73
|
+
* validatePassphraseSources?: (opts: Record<string, any>) => void,
|
|
74
|
+
* hasExplicitPassphraseSource?: (opts: Record<string, any>) => boolean,
|
|
75
|
+
* errorFactory?: (message: string) => Error,
|
|
76
|
+
* }} [deps]
|
|
77
|
+
*/
|
|
78
|
+
export function validateCliCredentialSources(opts, {
|
|
79
|
+
validatePassphraseSources: validatePassphraseSourcesFn = validatePassphraseSources,
|
|
80
|
+
hasExplicitPassphraseSource: hasExplicitPassphraseSourceFn = hasExplicitPassphraseSource,
|
|
81
|
+
errorFactory = defaultErrorFactory,
|
|
82
|
+
} = {}) {
|
|
83
|
+
validatePassphraseSourcesFn(opts);
|
|
84
|
+
if (opts.keyFile !== undefined && hasExplicitPassphraseSourceFn(opts)) {
|
|
85
|
+
throw errorFactory('Provide --key-file or a vault passphrase source, not both');
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Resolve a human CLI encryption key from a raw key file or vault passphrase source.
|
|
91
|
+
*
|
|
92
|
+
* @param {{ getVaultMetadata: Function }} cas
|
|
93
|
+
* @param {Record<string, any>} opts
|
|
94
|
+
* @param {{
|
|
95
|
+
* readKeyFile?: (keyFilePath: string) => Uint8Array,
|
|
96
|
+
* hasPassphraseSource?: (opts: Record<string, any>) => boolean,
|
|
97
|
+
* resolvePassphrase?: (opts: Record<string, any>) => Promise<string | undefined>,
|
|
98
|
+
* writeWarning?: (message: string) => void,
|
|
99
|
+
* }} [deps]
|
|
100
|
+
* @returns {Promise<Uint8Array | undefined>}
|
|
101
|
+
*/
|
|
102
|
+
export async function resolveCliEncryptionKey(cas, opts, {
|
|
103
|
+
readKeyFile: readKeyFileFn = readKeyFile,
|
|
104
|
+
hasPassphraseSource: hasPassphraseSourceFn = hasPassphraseSource,
|
|
105
|
+
resolvePassphrase: resolvePassphraseFn = resolvePassphrase,
|
|
106
|
+
writeWarning = (message) => process.stderr.write(message),
|
|
107
|
+
} = {}) {
|
|
108
|
+
if (opts.keyFile) {
|
|
109
|
+
return readKeyFileFn(opts.keyFile);
|
|
110
|
+
}
|
|
111
|
+
const metadata = await cas.getVaultMetadata();
|
|
112
|
+
if (!metadata?.encryption) {
|
|
113
|
+
return resolveUnencryptedVaultKey({ opts, hasPassphraseSourceFn, writeWarning });
|
|
114
|
+
}
|
|
115
|
+
return await resolveCliPassphraseKey({ cas, metadata, opts, resolvePassphraseFn });
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* @param {{
|
|
120
|
+
* opts: Record<string, any>,
|
|
121
|
+
* hasPassphraseSourceFn: (opts: Record<string, any>) => boolean,
|
|
122
|
+
* writeWarning: (message: string) => void,
|
|
123
|
+
* }} params
|
|
124
|
+
* @returns {undefined}
|
|
125
|
+
*/
|
|
126
|
+
function resolveUnencryptedVaultKey({ opts, hasPassphraseSourceFn, writeWarning }) {
|
|
127
|
+
if (hasPassphraseSourceFn(opts)) {
|
|
128
|
+
writeWarning('warning: passphrase ignored (vault is not encrypted)\n');
|
|
129
|
+
}
|
|
130
|
+
return undefined;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* @param {{
|
|
135
|
+
* cas: { deriveKey: Function, verifyVaultKey: Function },
|
|
136
|
+
* metadata: { encryption?: { kdf?: Record<string, any> } },
|
|
137
|
+
* opts: Record<string, any>,
|
|
138
|
+
* resolvePassphraseFn: (opts: Record<string, any>) => Promise<string | undefined>,
|
|
139
|
+
* }} params
|
|
140
|
+
* @returns {Promise<Uint8Array | undefined>}
|
|
141
|
+
*/
|
|
142
|
+
async function resolveCliPassphraseKey({ cas, metadata, opts, resolvePassphraseFn }) {
|
|
143
|
+
const passphrase = await resolvePassphraseFn(opts);
|
|
144
|
+
if (!passphrase) {
|
|
145
|
+
return undefined;
|
|
146
|
+
}
|
|
147
|
+
return await deriveVaultKey(cas, metadata, passphrase);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* @param {Record<string, any>} input
|
|
152
|
+
* @returns {boolean}
|
|
153
|
+
*/
|
|
154
|
+
export function hasAgentVaultPassphraseSource(input) {
|
|
155
|
+
return hasAgentPassphraseSource({
|
|
156
|
+
inlineValue: input.vaultPassphrase,
|
|
157
|
+
fileValue: input.vaultPassphraseFile,
|
|
158
|
+
osKeychainTarget: input.osKeychainTarget,
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* @param {Record<string, any>} input
|
|
164
|
+
* @param {{ errorFactory?: (message: string) => Error }} [options]
|
|
165
|
+
*/
|
|
166
|
+
export function validateAgentCredentialSources(input, {
|
|
167
|
+
errorFactory = defaultErrorFactory,
|
|
168
|
+
} = {}) {
|
|
169
|
+
validateAgentPassphraseSource({
|
|
170
|
+
inlineValue: input.vaultPassphrase,
|
|
171
|
+
fileValue: input.vaultPassphraseFile,
|
|
172
|
+
osKeychainTarget: input.osKeychainTarget,
|
|
173
|
+
osKeychainAccount: input.osKeychainAccount,
|
|
174
|
+
inlineFlag: '--vault-passphrase',
|
|
175
|
+
fileFlag: '--vault-passphrase-file',
|
|
176
|
+
keychainTargetFlag: '--os-keychain-target',
|
|
177
|
+
keychainAccountFlag: '--os-keychain-account',
|
|
178
|
+
label: 'vault passphrase source',
|
|
179
|
+
errorFactory,
|
|
180
|
+
});
|
|
181
|
+
if (input.keyFile !== undefined && hasAgentVaultPassphraseSource(input)) {
|
|
182
|
+
throw errorFactory('Provide --key-file or a vault passphrase source, not both');
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* @param {import('../src/domain/value-objects/Manifest.js').default} manifest
|
|
188
|
+
* @returns {boolean}
|
|
189
|
+
*/
|
|
190
|
+
function hasEnvelopeRecipients(manifest) {
|
|
191
|
+
return (
|
|
192
|
+
Array.isArray(manifest.encryption?.recipients) && manifest.encryption.recipients.length > 0
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* @param {import('../src/domain/value-objects/Manifest.js').default} manifest
|
|
198
|
+
* @param {Awaited<ReturnType<import('../index.js').default['getVaultMetadata']>>} metadata
|
|
199
|
+
* @returns {string[]}
|
|
200
|
+
*/
|
|
201
|
+
function getRestoreRequiredInputs(manifest, metadata) {
|
|
202
|
+
if (hasEnvelopeRecipients(manifest)) {
|
|
203
|
+
return ['keyFile'];
|
|
204
|
+
}
|
|
205
|
+
if (metadata?.encryption?.kdf) {
|
|
206
|
+
return ['keyFile', 'vaultPassphrase', 'vaultPassphraseFile', 'osKeychainTarget'];
|
|
207
|
+
}
|
|
208
|
+
return ['keyFile'];
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Resolve an agent store encryption key from a raw key file or vault passphrase source.
|
|
213
|
+
*
|
|
214
|
+
* @param {{ getVaultMetadata: Function }} cas
|
|
215
|
+
* @param {Record<string, any>} input
|
|
216
|
+
* @param {{
|
|
217
|
+
* readKeyFile?: (keyFilePath: string) => Uint8Array,
|
|
218
|
+
* resolveVaultPassphrase: (input: Record<string, any>, requestSource: string | undefined, options?: Record<string, any>) => Promise<string | undefined>,
|
|
219
|
+
* errorFactory?: (message: string) => Error,
|
|
220
|
+
* stdin?: NodeJS.ReadStream,
|
|
221
|
+
* onWarning?: (warning: Record<string, any>) => void,
|
|
222
|
+
* }} options
|
|
223
|
+
* @returns {Promise<Uint8Array | undefined>}
|
|
224
|
+
*/
|
|
225
|
+
export async function resolveAgentStoreEncryptionKey(cas, input, {
|
|
226
|
+
readKeyFile: readKeyFileFn = readKeyFile,
|
|
227
|
+
resolveVaultPassphrase,
|
|
228
|
+
errorFactory = defaultErrorFactory,
|
|
229
|
+
...passphraseOptions
|
|
230
|
+
}) {
|
|
231
|
+
validateAgentCredentialSources(input, { errorFactory });
|
|
232
|
+
if (input.keyFile) {
|
|
233
|
+
return readKeyFileFn(input.keyFile);
|
|
234
|
+
}
|
|
235
|
+
const passphrase = await resolveVaultPassphrase(input, input.requestSource, passphraseOptions);
|
|
236
|
+
if (!passphrase) {
|
|
237
|
+
return undefined;
|
|
238
|
+
}
|
|
239
|
+
const metadata = await cas.getVaultMetadata();
|
|
240
|
+
if (!metadata?.encryption?.kdf) {
|
|
241
|
+
throw errorFactory('Vault passphrase source is only valid for encrypted vaults');
|
|
242
|
+
}
|
|
243
|
+
return await deriveVaultKey(cas, metadata, passphrase);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Resolve an agent diagnostic encryption key from a raw key file or vault passphrase source.
|
|
248
|
+
* Diagnostics can inspect plaintext vaults even when callers supplied a passphrase by mistake.
|
|
249
|
+
*
|
|
250
|
+
* @param {{ getVaultMetadata: Function }} cas
|
|
251
|
+
* @param {Record<string, any>} input
|
|
252
|
+
* @param {{
|
|
253
|
+
* readKeyFile?: (keyFilePath: string) => Uint8Array,
|
|
254
|
+
* resolveVaultPassphrase?: (input: Record<string, any>, requestSource: string | undefined, options?: Record<string, any>) => Promise<string | undefined>,
|
|
255
|
+
* errorFactory?: (message: string) => Error,
|
|
256
|
+
* onWarning?: (warning: Record<string, any>) => void,
|
|
257
|
+
* }} options
|
|
258
|
+
* @returns {Promise<Uint8Array | undefined>}
|
|
259
|
+
*/
|
|
260
|
+
export async function resolveAgentDiagnosticEncryptionKey(cas, input, {
|
|
261
|
+
readKeyFile: readKeyFileFn = readKeyFile,
|
|
262
|
+
resolveVaultPassphrase,
|
|
263
|
+
errorFactory = defaultErrorFactory,
|
|
264
|
+
onWarning,
|
|
265
|
+
...passphraseOptions
|
|
266
|
+
} = {}) {
|
|
267
|
+
validateAgentCredentialSources(input, { errorFactory });
|
|
268
|
+
if (input.keyFile) {
|
|
269
|
+
return readKeyFileFn(input.keyFile);
|
|
270
|
+
}
|
|
271
|
+
const metadata = await cas.getVaultMetadata();
|
|
272
|
+
if (!metadata?.encryption?.kdf) {
|
|
273
|
+
return resolveAgentPlaintextDiagnosticKey(input, onWarning);
|
|
274
|
+
}
|
|
275
|
+
return await resolveAgentEncryptedDiagnosticKey({
|
|
276
|
+
cas,
|
|
277
|
+
input,
|
|
278
|
+
metadata,
|
|
279
|
+
resolveVaultPassphrase,
|
|
280
|
+
errorFactory,
|
|
281
|
+
passphraseOptions,
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* @param {Record<string, any>} input
|
|
287
|
+
* @param {((warning: Record<string, any>) => void) | undefined} onWarning
|
|
288
|
+
* @returns {undefined}
|
|
289
|
+
*/
|
|
290
|
+
function resolveAgentPlaintextDiagnosticKey(input, onWarning) {
|
|
291
|
+
if (hasAgentVaultPassphraseSource(input)) {
|
|
292
|
+
onWarning?.({ message: UNENCRYPTED_VAULT_PASSPHRASE_IGNORED_MESSAGE });
|
|
293
|
+
}
|
|
294
|
+
return undefined;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* @param {{
|
|
299
|
+
* cas: { deriveKey: Function, verifyVaultKey: Function },
|
|
300
|
+
* input: Record<string, any>,
|
|
301
|
+
* metadata: { encryption?: { kdf?: Record<string, any> } },
|
|
302
|
+
* resolveVaultPassphrase?: (input: Record<string, any>, requestSource: string | undefined, options?: Record<string, any>) => Promise<string | undefined>,
|
|
303
|
+
* errorFactory: (message: string) => Error,
|
|
304
|
+
* passphraseOptions: Record<string, any>,
|
|
305
|
+
* }} params
|
|
306
|
+
* @returns {Promise<Uint8Array | undefined>}
|
|
307
|
+
*/
|
|
308
|
+
async function resolveAgentEncryptedDiagnosticKey({
|
|
309
|
+
cas,
|
|
310
|
+
input,
|
|
311
|
+
metadata,
|
|
312
|
+
resolveVaultPassphrase,
|
|
313
|
+
errorFactory,
|
|
314
|
+
passphraseOptions,
|
|
315
|
+
}) {
|
|
316
|
+
if (!hasAgentVaultPassphraseSource(input)) {
|
|
317
|
+
return undefined;
|
|
318
|
+
}
|
|
319
|
+
if (typeof resolveVaultPassphrase !== 'function') {
|
|
320
|
+
throw errorFactory('resolveVaultPassphrase is required when input contains a vault passphrase source');
|
|
321
|
+
}
|
|
322
|
+
const passphrase = await resolveVaultPassphrase(input, input.requestSource, passphraseOptions);
|
|
323
|
+
return passphrase ? await deriveVaultKey(cas, metadata, passphrase) : undefined;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Resolve an agent restore encryption key or raise NEEDS_INPUT metadata.
|
|
328
|
+
*
|
|
329
|
+
* @param {{
|
|
330
|
+
* cas: { getVaultMetadata: Function },
|
|
331
|
+
* manifest: import('../src/domain/value-objects/Manifest.js').default,
|
|
332
|
+
* input: Record<string, any>,
|
|
333
|
+
* requestSource?: string,
|
|
334
|
+
* treeOid: string,
|
|
335
|
+
* }} params
|
|
336
|
+
* @param {{
|
|
337
|
+
* readKeyFile?: (keyFilePath: string) => Uint8Array,
|
|
338
|
+
* resolveVaultPassphrase: (input: Record<string, any>, requestSource: string | undefined, options?: Record<string, any>) => Promise<string | undefined>,
|
|
339
|
+
* errorFactory?: (message: string) => Error,
|
|
340
|
+
* needsInputFactory?: (message: string, meta?: Record<string, any>) => Error,
|
|
341
|
+
* }} options
|
|
342
|
+
* @returns {Promise<Uint8Array | undefined>}
|
|
343
|
+
*/
|
|
344
|
+
export async function resolveAgentRestoreEncryptionKey(
|
|
345
|
+
{ cas, manifest, input, requestSource, treeOid },
|
|
346
|
+
{
|
|
347
|
+
readKeyFile: readKeyFileFn = readKeyFile,
|
|
348
|
+
resolveVaultPassphrase,
|
|
349
|
+
errorFactory = defaultErrorFactory,
|
|
350
|
+
needsInputFactory = (message) => new Error(message),
|
|
351
|
+
}
|
|
352
|
+
) {
|
|
353
|
+
validateAgentCredentialSources(input, { errorFactory });
|
|
354
|
+
if (input.keyFile) {
|
|
355
|
+
return readKeyFileFn(input.keyFile);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
const metadata = await cas.getVaultMetadata();
|
|
359
|
+
const passphrase = await resolveVaultPassphrase(input, requestSource, {
|
|
360
|
+
stdin: input.stdin,
|
|
361
|
+
onWarning: input.onWarning,
|
|
362
|
+
});
|
|
363
|
+
const passphraseKey = await resolveAgentRestorePassphraseKey({
|
|
364
|
+
cas,
|
|
365
|
+
manifest,
|
|
366
|
+
metadata,
|
|
367
|
+
passphrase,
|
|
368
|
+
errorFactory,
|
|
369
|
+
});
|
|
370
|
+
if (passphraseKey) {
|
|
371
|
+
return passphraseKey;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
if (!manifest.encryption?.encrypted) {
|
|
375
|
+
return undefined;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
throw needsInputFactory('Encrypted restore requires --key-file or a vault passphrase source', {
|
|
379
|
+
requiredInputs: getRestoreRequiredInputs(manifest, metadata),
|
|
380
|
+
slug: input.slug || manifest.slug,
|
|
381
|
+
treeOid,
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* @param {{
|
|
387
|
+
* cas: { deriveKey: Function, verifyVaultKey: Function },
|
|
388
|
+
* manifest: import('../src/domain/value-objects/Manifest.js').default,
|
|
389
|
+
* metadata: Awaited<ReturnType<import('../index.js').default['getVaultMetadata']>>,
|
|
390
|
+
* passphrase: string | undefined,
|
|
391
|
+
* errorFactory: (message: string) => Error,
|
|
392
|
+
* }} params
|
|
393
|
+
* @returns {Promise<Uint8Array | undefined>}
|
|
394
|
+
*/
|
|
395
|
+
async function resolveAgentRestorePassphraseKey({
|
|
396
|
+
cas,
|
|
397
|
+
manifest,
|
|
398
|
+
metadata,
|
|
399
|
+
passphrase,
|
|
400
|
+
errorFactory,
|
|
401
|
+
}) {
|
|
402
|
+
if (!passphrase) {
|
|
403
|
+
return undefined;
|
|
404
|
+
}
|
|
405
|
+
assertAgentPassphraseCanDecryptManifest({ manifest, metadata, errorFactory });
|
|
406
|
+
return await deriveVaultKey(cas, metadata, passphrase);
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* @param {{
|
|
411
|
+
* manifest: import('../src/domain/value-objects/Manifest.js').default,
|
|
412
|
+
* metadata: Awaited<ReturnType<import('../index.js').default['getVaultMetadata']>>,
|
|
413
|
+
* errorFactory: (message: string) => Error,
|
|
414
|
+
* }} params
|
|
415
|
+
*/
|
|
416
|
+
function assertAgentPassphraseCanDecryptManifest({ manifest, metadata, errorFactory }) {
|
|
417
|
+
if (hasEnvelopeRecipients(manifest)) {
|
|
418
|
+
throw errorFactory(
|
|
419
|
+
'Vault passphrase source cannot decrypt recipient-encrypted assets; provide --key-file'
|
|
420
|
+
);
|
|
421
|
+
}
|
|
422
|
+
if (!metadata?.encryption?.kdf) {
|
|
423
|
+
throw errorFactory('Vault passphrase source is only valid for encrypted vaults');
|
|
424
|
+
}
|
|
425
|
+
}
|