@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,1489 @@
|
|
|
1
|
+
import ContentAddressableStore from '../../../index.js';
|
|
2
|
+
import Manifest from '../../../src/domain/value-objects/Manifest.js';
|
|
3
|
+
import Slug from '../../../src/domain/value-objects/Slug.js';
|
|
4
|
+
import { createGitPlumbing } from '../../../src/infrastructure/createGitPlumbing.js';
|
|
5
|
+
import { resolveRestoreOutputTarget } from '../../restore-output-target.js';
|
|
6
|
+
import { buildVaultStats } from '../../ui/vault-report.js';
|
|
7
|
+
import { filterEntries } from '../../ui/vault-list.js';
|
|
8
|
+
import doctorCommand from './doctor.js';
|
|
9
|
+
import {
|
|
10
|
+
resolveAgentPassphraseSource,
|
|
11
|
+
hasAgentPassphraseSource,
|
|
12
|
+
validateAgentPassphraseSource,
|
|
13
|
+
} from '../passphrase-source.js';
|
|
14
|
+
import {
|
|
15
|
+
resolveAgentRestoreEncryptionKey,
|
|
16
|
+
resolveAgentStoreEncryptionKey,
|
|
17
|
+
} from '../../credentials.js';
|
|
18
|
+
import { AGENT_EXIT_CODES } from '../protocol.js';
|
|
19
|
+
import {
|
|
20
|
+
assignPositionals,
|
|
21
|
+
createCas,
|
|
22
|
+
invalidInput,
|
|
23
|
+
needsInput,
|
|
24
|
+
normalizeInputAliases,
|
|
25
|
+
parseAgentInput,
|
|
26
|
+
parsePositiveInteger,
|
|
27
|
+
readAgentPassphraseFile,
|
|
28
|
+
readKeyFile,
|
|
29
|
+
readTextInputFile,
|
|
30
|
+
resolveSlugTarget,
|
|
31
|
+
resolveTarget,
|
|
32
|
+
resolveTree,
|
|
33
|
+
selectStartInput,
|
|
34
|
+
validateCredentialSources,
|
|
35
|
+
writeAgentStart,
|
|
36
|
+
} from '../input.js';
|
|
37
|
+
|
|
38
|
+
export const AVAILABLE_COMMANDS = Object.freeze([
|
|
39
|
+
'store',
|
|
40
|
+
'tree',
|
|
41
|
+
'restore',
|
|
42
|
+
'rotate',
|
|
43
|
+
'inspect',
|
|
44
|
+
'verify',
|
|
45
|
+
'doctor',
|
|
46
|
+
'recipient add',
|
|
47
|
+
'recipient remove',
|
|
48
|
+
'recipient list',
|
|
49
|
+
'vault init',
|
|
50
|
+
'vault list',
|
|
51
|
+
'vault info',
|
|
52
|
+
'vault history',
|
|
53
|
+
'vault remove',
|
|
54
|
+
'vault rotate',
|
|
55
|
+
'vault stats',
|
|
56
|
+
]);
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* @param {Record<string, any>} input
|
|
60
|
+
* @param {string | undefined} requestSource
|
|
61
|
+
* @returns {Promise<string | undefined>}
|
|
62
|
+
*/
|
|
63
|
+
async function resolveVaultPassphrase(input, requestSource, options = {}) {
|
|
64
|
+
return await resolveAgentPassphraseSource({
|
|
65
|
+
label: 'Passphrase',
|
|
66
|
+
inlineValue: input.vaultPassphrase,
|
|
67
|
+
fileValue: input.vaultPassphraseFile,
|
|
68
|
+
osKeychainTarget: input.osKeychainTarget,
|
|
69
|
+
osKeychainAccount: input.osKeychainAccount,
|
|
70
|
+
requestSource,
|
|
71
|
+
readPassphraseFile: (filePath) => readAgentPassphraseFile(filePath, options),
|
|
72
|
+
resolveInlinePassphrase,
|
|
73
|
+
errorFactory: invalidInput,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* @param {string[]} args
|
|
79
|
+
* @param {NodeJS.ReadStream} stdin
|
|
80
|
+
* @returns {Promise<Record<string, any>>}
|
|
81
|
+
*/
|
|
82
|
+
async function parseStoreInput(args, stdin) {
|
|
83
|
+
const { values, positionals, requestSource } = await parseAgentInput(
|
|
84
|
+
args,
|
|
85
|
+
{
|
|
86
|
+
slug: { type: 'string' },
|
|
87
|
+
tree: { type: 'boolean' },
|
|
88
|
+
force: { type: 'boolean' },
|
|
89
|
+
gzip: { type: 'boolean' },
|
|
90
|
+
cwd: { type: 'string' },
|
|
91
|
+
'key-file': { type: 'string' },
|
|
92
|
+
'vault-passphrase': { type: 'string' },
|
|
93
|
+
'vault-passphrase-file': { type: 'string' },
|
|
94
|
+
'os-keychain-target': { type: 'string' },
|
|
95
|
+
'os-keychain-account': { type: 'string' },
|
|
96
|
+
},
|
|
97
|
+
stdin
|
|
98
|
+
);
|
|
99
|
+
return normalizeInputAliases({
|
|
100
|
+
...values,
|
|
101
|
+
...assignPositionals(positionals, ['file']),
|
|
102
|
+
requestSource,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* @param {Record<string, any>} input
|
|
108
|
+
*/
|
|
109
|
+
function validateStoreInput(input) {
|
|
110
|
+
if (input.file !== undefined && typeof input.file !== 'string') {
|
|
111
|
+
throw invalidInput('Request field "file" must be a string');
|
|
112
|
+
}
|
|
113
|
+
if (!input.file) {
|
|
114
|
+
throw invalidInput('Provide a file path');
|
|
115
|
+
}
|
|
116
|
+
if (!input.slug) {
|
|
117
|
+
throw invalidInput('Provide --slug <slug>');
|
|
118
|
+
}
|
|
119
|
+
input.slug = Slug.from(input.slug).toString();
|
|
120
|
+
if (input.force && !input.tree) {
|
|
121
|
+
throw invalidInput('--force requires --tree');
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* @param {{
|
|
127
|
+
* input: Record<string, any>,
|
|
128
|
+
* manifest: import('../../../src/domain/value-objects/Manifest.js').default,
|
|
129
|
+
* treeOid?: string,
|
|
130
|
+
* commitOid?: string,
|
|
131
|
+
* }} options
|
|
132
|
+
* @returns {{ data: Record<string, any> }}
|
|
133
|
+
*/
|
|
134
|
+
function buildStoreOutcome({ input, manifest, treeOid, commitOid }) {
|
|
135
|
+
return {
|
|
136
|
+
data: {
|
|
137
|
+
slug: input.slug,
|
|
138
|
+
manifest: manifest.toJSON(),
|
|
139
|
+
...(treeOid ? { treeOid } : {}),
|
|
140
|
+
...(commitOid ? { commitOid } : {}),
|
|
141
|
+
addedToVault: Boolean(commitOid),
|
|
142
|
+
chunkCount: manifest.chunks.length,
|
|
143
|
+
encrypted: Boolean(manifest.encryption?.encrypted),
|
|
144
|
+
compressed: Boolean(manifest.compression),
|
|
145
|
+
},
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* @param {string[]} args
|
|
151
|
+
* @param {NodeJS.ReadStream} stdin
|
|
152
|
+
* @returns {Promise<Record<string, any>>}
|
|
153
|
+
*/
|
|
154
|
+
async function parseVaultInitInput(args, stdin) {
|
|
155
|
+
const { values, positionals, requestSource } = await parseAgentInput(
|
|
156
|
+
args,
|
|
157
|
+
{
|
|
158
|
+
cwd: { type: 'string' },
|
|
159
|
+
algorithm: { type: 'string' },
|
|
160
|
+
passphrase: { type: 'string' },
|
|
161
|
+
'passphrase-file': { type: 'string' },
|
|
162
|
+
'os-keychain-target': { type: 'string' },
|
|
163
|
+
'os-keychain-account': { type: 'string' },
|
|
164
|
+
},
|
|
165
|
+
stdin
|
|
166
|
+
);
|
|
167
|
+
assignPositionals(positionals, []);
|
|
168
|
+
return normalizeInputAliases({
|
|
169
|
+
...values,
|
|
170
|
+
requestSource,
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* @param {Record<string, any>} input
|
|
176
|
+
*/
|
|
177
|
+
function validateVaultInitInput(input) {
|
|
178
|
+
validateAgentPassphraseSource({
|
|
179
|
+
inlineValue: input.passphrase,
|
|
180
|
+
fileValue: input.passphraseFile,
|
|
181
|
+
osKeychainTarget: input.osKeychainTarget,
|
|
182
|
+
osKeychainAccount: input.osKeychainAccount,
|
|
183
|
+
inlineFlag: '--passphrase',
|
|
184
|
+
fileFlag: '--passphrase-file',
|
|
185
|
+
keychainTargetFlag: '--os-keychain-target',
|
|
186
|
+
keychainAccountFlag: '--os-keychain-account',
|
|
187
|
+
label: 'passphrase source',
|
|
188
|
+
errorFactory: invalidInput,
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
const algorithm = parseKdfAlgorithm(input.algorithm);
|
|
192
|
+
if (
|
|
193
|
+
algorithm &&
|
|
194
|
+
!hasAgentPassphraseSource({
|
|
195
|
+
inlineValue: input.passphrase,
|
|
196
|
+
fileValue: input.passphraseFile,
|
|
197
|
+
osKeychainTarget: input.osKeychainTarget,
|
|
198
|
+
})
|
|
199
|
+
) {
|
|
200
|
+
throw invalidInput(
|
|
201
|
+
'Provide --passphrase <pass>, --passphrase-file <path>, or --os-keychain-target <target> when using --algorithm'
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* @param {Record<string, any>} input
|
|
208
|
+
* @param {string | undefined} requestSource
|
|
209
|
+
* @returns {Promise<string | undefined>}
|
|
210
|
+
*/
|
|
211
|
+
async function resolveVaultInitPassphrase(input, requestSource, options = {}) {
|
|
212
|
+
return await resolveAgentPassphraseSource({
|
|
213
|
+
label: 'Passphrase',
|
|
214
|
+
inlineValue: input.passphrase,
|
|
215
|
+
fileValue: input.passphraseFile,
|
|
216
|
+
osKeychainTarget: input.osKeychainTarget,
|
|
217
|
+
osKeychainAccount: input.osKeychainAccount,
|
|
218
|
+
requestSource,
|
|
219
|
+
readPassphraseFile: (filePath) => readAgentPassphraseFile(filePath, options),
|
|
220
|
+
resolveInlinePassphrase,
|
|
221
|
+
errorFactory: invalidInput,
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* @param {string[]} args
|
|
227
|
+
* @param {NodeJS.ReadStream} stdin
|
|
228
|
+
* @returns {Promise<Record<string, any>>}
|
|
229
|
+
*/
|
|
230
|
+
async function parseRotateInput(args, stdin) {
|
|
231
|
+
const { values, positionals } = await parseAgentInput(
|
|
232
|
+
args,
|
|
233
|
+
{
|
|
234
|
+
slug: { type: 'string' },
|
|
235
|
+
oid: { type: 'string' },
|
|
236
|
+
label: { type: 'string' },
|
|
237
|
+
cwd: { type: 'string' },
|
|
238
|
+
'old-key-file': { type: 'string' },
|
|
239
|
+
'new-key-file': { type: 'string' },
|
|
240
|
+
},
|
|
241
|
+
stdin
|
|
242
|
+
);
|
|
243
|
+
assignPositionals(positionals, []);
|
|
244
|
+
return normalizeInputAliases(values);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* @param {Record<string, any>} input
|
|
249
|
+
*/
|
|
250
|
+
function validateRotateInput(input) {
|
|
251
|
+
if (!input.oldKeyFile) {
|
|
252
|
+
throw invalidInput('Provide --old-key-file <path>');
|
|
253
|
+
}
|
|
254
|
+
if (!input.newKeyFile) {
|
|
255
|
+
throw invalidInput('Provide --new-key-file <path>');
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* @param {unknown} value
|
|
261
|
+
* @returns {'pbkdf2' | 'scrypt' | undefined}
|
|
262
|
+
*/
|
|
263
|
+
function parseKdfAlgorithm(value) {
|
|
264
|
+
if (value === undefined) {
|
|
265
|
+
return undefined;
|
|
266
|
+
}
|
|
267
|
+
if (value === 'pbkdf2' || value === 'scrypt') {
|
|
268
|
+
return value;
|
|
269
|
+
}
|
|
270
|
+
throw invalidInput('Provide --algorithm <pbkdf2|scrypt>');
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* @param {string} label
|
|
275
|
+
* @param {unknown} value
|
|
276
|
+
* @returns {string | undefined}
|
|
277
|
+
*/
|
|
278
|
+
function resolveInlinePassphrase(label, value) {
|
|
279
|
+
if (value === undefined) {
|
|
280
|
+
return undefined;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const passphrase = String(value);
|
|
284
|
+
if (!passphrase.trim()) {
|
|
285
|
+
throw invalidInput(`${label} must not be empty`);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
return passphrase;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* @param {string[]} args
|
|
293
|
+
* @param {NodeJS.ReadStream} stdin
|
|
294
|
+
* @returns {Promise<Record<string, any>>}
|
|
295
|
+
*/
|
|
296
|
+
async function parseVaultRotateInput(args, stdin) {
|
|
297
|
+
const { values, positionals, requestSource } = await parseAgentInput(
|
|
298
|
+
args,
|
|
299
|
+
{
|
|
300
|
+
cwd: { type: 'string' },
|
|
301
|
+
algorithm: { type: 'string' },
|
|
302
|
+
'old-passphrase': { type: 'string' },
|
|
303
|
+
'new-passphrase': { type: 'string' },
|
|
304
|
+
'old-passphrase-file': { type: 'string' },
|
|
305
|
+
'new-passphrase-file': { type: 'string' },
|
|
306
|
+
'old-os-keychain-target': { type: 'string' },
|
|
307
|
+
'old-os-keychain-account': { type: 'string' },
|
|
308
|
+
'new-os-keychain-target': { type: 'string' },
|
|
309
|
+
'new-os-keychain-account': { type: 'string' },
|
|
310
|
+
},
|
|
311
|
+
stdin
|
|
312
|
+
);
|
|
313
|
+
assignPositionals(positionals, []);
|
|
314
|
+
return normalizeInputAliases({
|
|
315
|
+
...values,
|
|
316
|
+
requestSource,
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* @param {Record<string, any>} input
|
|
322
|
+
*/
|
|
323
|
+
function validateVaultRotateInput(input) {
|
|
324
|
+
validateAgentPassphraseSource({
|
|
325
|
+
inlineValue: input.oldPassphrase,
|
|
326
|
+
fileValue: input.oldPassphraseFile,
|
|
327
|
+
osKeychainTarget: input.oldOsKeychainTarget,
|
|
328
|
+
osKeychainAccount: input.oldOsKeychainAccount,
|
|
329
|
+
inlineFlag: '--old-passphrase',
|
|
330
|
+
fileFlag: '--old-passphrase-file',
|
|
331
|
+
keychainTargetFlag: '--old-os-keychain-target',
|
|
332
|
+
keychainAccountFlag: '--old-os-keychain-account',
|
|
333
|
+
label: 'old passphrase source',
|
|
334
|
+
errorFactory: invalidInput,
|
|
335
|
+
});
|
|
336
|
+
validateAgentPassphraseSource({
|
|
337
|
+
inlineValue: input.newPassphrase,
|
|
338
|
+
fileValue: input.newPassphraseFile,
|
|
339
|
+
osKeychainTarget: input.newOsKeychainTarget,
|
|
340
|
+
osKeychainAccount: input.newOsKeychainAccount,
|
|
341
|
+
inlineFlag: '--new-passphrase',
|
|
342
|
+
fileFlag: '--new-passphrase-file',
|
|
343
|
+
keychainTargetFlag: '--new-os-keychain-target',
|
|
344
|
+
keychainAccountFlag: '--new-os-keychain-account',
|
|
345
|
+
label: 'new passphrase source',
|
|
346
|
+
errorFactory: invalidInput,
|
|
347
|
+
});
|
|
348
|
+
if (!hasAgentPassphraseSource({
|
|
349
|
+
inlineValue: input.oldPassphrase,
|
|
350
|
+
fileValue: input.oldPassphraseFile,
|
|
351
|
+
osKeychainTarget: input.oldOsKeychainTarget,
|
|
352
|
+
})) {
|
|
353
|
+
throw invalidInput(
|
|
354
|
+
'Provide --old-passphrase <pass>, --old-passphrase-file <path>, or --old-os-keychain-target <target>'
|
|
355
|
+
);
|
|
356
|
+
}
|
|
357
|
+
if (!hasAgentPassphraseSource({
|
|
358
|
+
inlineValue: input.newPassphrase,
|
|
359
|
+
fileValue: input.newPassphraseFile,
|
|
360
|
+
osKeychainTarget: input.newOsKeychainTarget,
|
|
361
|
+
})) {
|
|
362
|
+
throw invalidInput(
|
|
363
|
+
'Provide --new-passphrase <pass>, --new-passphrase-file <path>, or --new-os-keychain-target <target>'
|
|
364
|
+
);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
parseKdfAlgorithm(input.algorithm);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* @param {Record<string, any>} input
|
|
372
|
+
* @param {string | undefined} requestSource
|
|
373
|
+
* @returns {Promise<{ oldPassphrase: string, newPassphrase: string }>}
|
|
374
|
+
*/
|
|
375
|
+
async function resolveVaultRotatePassphrases(input, requestSource, options = {}) {
|
|
376
|
+
validateVaultRotateStdinSources(input, requestSource);
|
|
377
|
+
|
|
378
|
+
return {
|
|
379
|
+
oldPassphrase: await readVaultRotatePassphrase({
|
|
380
|
+
label: 'Old passphrase',
|
|
381
|
+
inlineValue: input.oldPassphrase,
|
|
382
|
+
fileValue: input.oldPassphraseFile,
|
|
383
|
+
osKeychainTarget: input.oldOsKeychainTarget,
|
|
384
|
+
osKeychainAccount: input.oldOsKeychainAccount,
|
|
385
|
+
requestSource,
|
|
386
|
+
...options,
|
|
387
|
+
}),
|
|
388
|
+
newPassphrase: await readVaultRotatePassphrase({
|
|
389
|
+
label: 'New passphrase',
|
|
390
|
+
inlineValue: input.newPassphrase,
|
|
391
|
+
fileValue: input.newPassphraseFile,
|
|
392
|
+
osKeychainTarget: input.newOsKeychainTarget,
|
|
393
|
+
osKeychainAccount: input.newOsKeychainAccount,
|
|
394
|
+
requestSource,
|
|
395
|
+
...options,
|
|
396
|
+
}),
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* @param {Record<string, any>} input
|
|
402
|
+
* @param {string | undefined} requestSource
|
|
403
|
+
*/
|
|
404
|
+
function validateVaultRotateStdinSources(input, requestSource) {
|
|
405
|
+
if (input.oldPassphraseFile === '-' && input.newPassphraseFile === '-') {
|
|
406
|
+
throw invalidInput('Cannot read both old and new passphrase from stdin');
|
|
407
|
+
}
|
|
408
|
+
if (
|
|
409
|
+
requestSource === '-' &&
|
|
410
|
+
(input.oldPassphraseFile === '-' || input.newPassphraseFile === '-')
|
|
411
|
+
) {
|
|
412
|
+
throw invalidInput('Cannot read both request payload and vault rotation passphrase from stdin');
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* @param {{
|
|
418
|
+
* label: string,
|
|
419
|
+
* inlineValue: unknown,
|
|
420
|
+
* fileValue?: string,
|
|
421
|
+
* osKeychainTarget?: string,
|
|
422
|
+
* osKeychainAccount?: string,
|
|
423
|
+
* requestSource?: string,
|
|
424
|
+
* stdin?: NodeJS.ReadStream,
|
|
425
|
+
* onWarning?: (warning: Record<string, any>) => void,
|
|
426
|
+
* }} options
|
|
427
|
+
* @returns {Promise<string>}
|
|
428
|
+
*/
|
|
429
|
+
async function readVaultRotatePassphrase({
|
|
430
|
+
label,
|
|
431
|
+
inlineValue,
|
|
432
|
+
fileValue,
|
|
433
|
+
osKeychainTarget,
|
|
434
|
+
osKeychainAccount,
|
|
435
|
+
requestSource,
|
|
436
|
+
...options
|
|
437
|
+
}) {
|
|
438
|
+
const passphrase = await resolveAgentPassphraseSource({
|
|
439
|
+
label,
|
|
440
|
+
inlineValue,
|
|
441
|
+
fileValue,
|
|
442
|
+
osKeychainTarget,
|
|
443
|
+
osKeychainAccount,
|
|
444
|
+
requestSource,
|
|
445
|
+
readPassphraseFile: (filePath) => readAgentPassphraseFile(filePath, options),
|
|
446
|
+
resolveInlinePassphrase,
|
|
447
|
+
errorFactory: invalidInput,
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
if (!passphrase?.trim()) {
|
|
451
|
+
throw invalidInput(`${label} must not be empty`);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
return passphrase;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* @param {string[]} args
|
|
459
|
+
* @param {NodeJS.ReadStream} stdin
|
|
460
|
+
* @returns {Promise<Record<string, any>>}
|
|
461
|
+
*/
|
|
462
|
+
async function parseRecipientAddInput(args, stdin) {
|
|
463
|
+
const { values, positionals } = await parseAgentInput(
|
|
464
|
+
args,
|
|
465
|
+
{
|
|
466
|
+
slug: { type: 'string' },
|
|
467
|
+
label: { type: 'string' },
|
|
468
|
+
cwd: { type: 'string' },
|
|
469
|
+
'key-file': { type: 'string' },
|
|
470
|
+
'existing-key-file': { type: 'string' },
|
|
471
|
+
},
|
|
472
|
+
stdin
|
|
473
|
+
);
|
|
474
|
+
assignPositionals(positionals, []);
|
|
475
|
+
return normalizeInputAliases(values);
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* @param {Record<string, any>} input
|
|
480
|
+
*/
|
|
481
|
+
function validateRecipientAddInput(input) {
|
|
482
|
+
if (!input.slug) {
|
|
483
|
+
throw invalidInput('Provide --slug <slug>');
|
|
484
|
+
}
|
|
485
|
+
if (!input.label) {
|
|
486
|
+
throw invalidInput('Provide --label <label>');
|
|
487
|
+
}
|
|
488
|
+
if (!input.keyFile) {
|
|
489
|
+
throw invalidInput('Provide --key-file <path>');
|
|
490
|
+
}
|
|
491
|
+
if (!input.existingKeyFile) {
|
|
492
|
+
throw invalidInput('Provide --existing-key-file <path>');
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* @param {string[]} args
|
|
498
|
+
* @param {NodeJS.ReadStream} stdin
|
|
499
|
+
* @returns {Promise<Record<string, any>>}
|
|
500
|
+
*/
|
|
501
|
+
async function parseRecipientRemoveInput(args, stdin) {
|
|
502
|
+
const { values, positionals } = await parseAgentInput(
|
|
503
|
+
args,
|
|
504
|
+
{
|
|
505
|
+
slug: { type: 'string' },
|
|
506
|
+
label: { type: 'string' },
|
|
507
|
+
cwd: { type: 'string' },
|
|
508
|
+
},
|
|
509
|
+
stdin
|
|
510
|
+
);
|
|
511
|
+
assignPositionals(positionals, []);
|
|
512
|
+
return values;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/**
|
|
516
|
+
* @param {Record<string, any>} input
|
|
517
|
+
*/
|
|
518
|
+
function validateRecipientRemoveInput(input) {
|
|
519
|
+
if (!input.slug) {
|
|
520
|
+
throw invalidInput('Provide --slug <slug>');
|
|
521
|
+
}
|
|
522
|
+
if (!input.label) {
|
|
523
|
+
throw invalidInput('Provide --label <label>');
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
/**
|
|
528
|
+
* @param {{ cwd: string, slug: string }} input
|
|
529
|
+
* @returns {Promise<{
|
|
530
|
+
* cas: ContentAddressableStore,
|
|
531
|
+
* treeOid: string,
|
|
532
|
+
* manifest: import('../../../src/domain/value-objects/Manifest.js').default,
|
|
533
|
+
* }>}
|
|
534
|
+
*/
|
|
535
|
+
async function resolveVaultManifestBySlug(input) {
|
|
536
|
+
const cas = await createCas(input.cwd);
|
|
537
|
+
const treeOid = await cas.resolveVaultEntry({ slug: input.slug });
|
|
538
|
+
const manifest = await cas.readManifest({ treeOid });
|
|
539
|
+
return { cas, treeOid, manifest };
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
/**
|
|
543
|
+
* @param {{
|
|
544
|
+
* action: 'add' | 'remove',
|
|
545
|
+
* slug: string,
|
|
546
|
+
* label: string,
|
|
547
|
+
* previousTreeOid: string,
|
|
548
|
+
* treeOid: string,
|
|
549
|
+
* commitOid: string,
|
|
550
|
+
* manifest: import('../../../src/domain/value-objects/Manifest.js').default,
|
|
551
|
+
* }} options
|
|
552
|
+
* @returns {{ data: Record<string, any> }}
|
|
553
|
+
*/
|
|
554
|
+
function buildRecipientMutationOutcome({
|
|
555
|
+
action,
|
|
556
|
+
slug,
|
|
557
|
+
label,
|
|
558
|
+
previousTreeOid,
|
|
559
|
+
treeOid,
|
|
560
|
+
commitOid,
|
|
561
|
+
manifest,
|
|
562
|
+
}) {
|
|
563
|
+
const recipients = buildRecipientRows(manifest);
|
|
564
|
+
|
|
565
|
+
return {
|
|
566
|
+
data: {
|
|
567
|
+
action,
|
|
568
|
+
slug,
|
|
569
|
+
label,
|
|
570
|
+
previousTreeOid,
|
|
571
|
+
treeOid,
|
|
572
|
+
commitOid,
|
|
573
|
+
recipientCount: recipients.length,
|
|
574
|
+
recipients,
|
|
575
|
+
},
|
|
576
|
+
};
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
/**
|
|
580
|
+
* @param {{
|
|
581
|
+
* previousTreeOid: string,
|
|
582
|
+
* treeOid: string,
|
|
583
|
+
* commitOid?: string,
|
|
584
|
+
* manifest: import('../../../src/domain/value-objects/Manifest.js').default,
|
|
585
|
+
* label?: string,
|
|
586
|
+
* }} options
|
|
587
|
+
* @returns {{ data: Record<string, any> }}
|
|
588
|
+
*/
|
|
589
|
+
function buildRotateOutcome({ previousTreeOid, treeOid, commitOid, manifest, label }) {
|
|
590
|
+
const recipients = buildRecipientRows(manifest);
|
|
591
|
+
|
|
592
|
+
return {
|
|
593
|
+
data: {
|
|
594
|
+
action: 'rotate',
|
|
595
|
+
slug: manifest.slug,
|
|
596
|
+
...(label ? { label } : {}),
|
|
597
|
+
previousTreeOid,
|
|
598
|
+
treeOid,
|
|
599
|
+
...(commitOid ? { commitOid } : {}),
|
|
600
|
+
updatedVault: Boolean(commitOid),
|
|
601
|
+
keyVersion: manifest.encryption?.keyVersion,
|
|
602
|
+
recipientCount: recipients.length,
|
|
603
|
+
recipients,
|
|
604
|
+
},
|
|
605
|
+
};
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
/**
|
|
609
|
+
* @param {{
|
|
610
|
+
* commitOid: string,
|
|
611
|
+
* rotatedSlugs: string[],
|
|
612
|
+
* skippedSlugs: string[],
|
|
613
|
+
* kdfAlgorithm?: string,
|
|
614
|
+
* }} options
|
|
615
|
+
* @returns {{ data: Record<string, any> }}
|
|
616
|
+
*/
|
|
617
|
+
function buildVaultRotateOutcome({ commitOid, rotatedSlugs, skippedSlugs, kdfAlgorithm }) {
|
|
618
|
+
return {
|
|
619
|
+
data: {
|
|
620
|
+
commitOid,
|
|
621
|
+
updatedVault: true,
|
|
622
|
+
rotatedSlugs,
|
|
623
|
+
skippedSlugs,
|
|
624
|
+
rotatedCount: rotatedSlugs.length,
|
|
625
|
+
skippedCount: skippedSlugs.length,
|
|
626
|
+
entryCount: rotatedSlugs.length + skippedSlugs.length,
|
|
627
|
+
...(kdfAlgorithm ? { kdfAlgorithm } : {}),
|
|
628
|
+
},
|
|
629
|
+
};
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
/**
|
|
633
|
+
* @param {{
|
|
634
|
+
* commitOid: string,
|
|
635
|
+
* encrypted: boolean,
|
|
636
|
+
* kdfAlgorithm?: string,
|
|
637
|
+
* }} options
|
|
638
|
+
* @returns {{ data: Record<string, any> }}
|
|
639
|
+
*/
|
|
640
|
+
function buildVaultInitOutcome({ commitOid, encrypted, kdfAlgorithm }) {
|
|
641
|
+
return {
|
|
642
|
+
data: {
|
|
643
|
+
commitOid,
|
|
644
|
+
initializedVault: true,
|
|
645
|
+
encrypted,
|
|
646
|
+
...(kdfAlgorithm ? { kdfAlgorithm } : {}),
|
|
647
|
+
},
|
|
648
|
+
};
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
/**
|
|
652
|
+
* @param {{
|
|
653
|
+
* slug: string,
|
|
654
|
+
* commitOid: string,
|
|
655
|
+
* removedTreeOid: string,
|
|
656
|
+
* }} options
|
|
657
|
+
* @returns {{ data: Record<string, any> }}
|
|
658
|
+
*/
|
|
659
|
+
function buildVaultRemoveOutcome({ slug, commitOid, removedTreeOid }) {
|
|
660
|
+
return {
|
|
661
|
+
data: {
|
|
662
|
+
slug,
|
|
663
|
+
commitOid,
|
|
664
|
+
removedTreeOid,
|
|
665
|
+
updatedVault: true,
|
|
666
|
+
},
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
/**
|
|
671
|
+
* @param {string[]} args
|
|
672
|
+
* @param {NodeJS.ReadStream} stdin
|
|
673
|
+
* @returns {Promise<Record<string, any>>}
|
|
674
|
+
*/
|
|
675
|
+
async function parseTreeInput(args, stdin) {
|
|
676
|
+
const { values, positionals } = await parseAgentInput(
|
|
677
|
+
args,
|
|
678
|
+
{
|
|
679
|
+
manifest: { type: 'string' },
|
|
680
|
+
cwd: { type: 'string' },
|
|
681
|
+
},
|
|
682
|
+
stdin
|
|
683
|
+
);
|
|
684
|
+
assignPositionals(positionals, []);
|
|
685
|
+
return values;
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
/**
|
|
689
|
+
* @param {Record<string, any>} input
|
|
690
|
+
* @returns {Manifest}
|
|
691
|
+
*/
|
|
692
|
+
function resolveManifestInput(input) {
|
|
693
|
+
if (typeof input.manifest === 'string') {
|
|
694
|
+
const manifestPath = input.manifest;
|
|
695
|
+
const raw = readTextInputFile(manifestPath, 'manifest file');
|
|
696
|
+
try {
|
|
697
|
+
return new Manifest(JSON.parse(raw));
|
|
698
|
+
} catch (err) {
|
|
699
|
+
throw invalidInput(
|
|
700
|
+
`Invalid manifest file: ${manifestPath}: ${
|
|
701
|
+
err instanceof Error ? err.message : String(err)
|
|
702
|
+
}`,
|
|
703
|
+
{ filePath: manifestPath }
|
|
704
|
+
);
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
if (input.manifest && typeof input.manifest === 'object' && !Array.isArray(input.manifest)) {
|
|
709
|
+
try {
|
|
710
|
+
return new Manifest(input.manifest);
|
|
711
|
+
} catch (err) {
|
|
712
|
+
throw invalidInput(
|
|
713
|
+
`Invalid manifest object: ${err instanceof Error ? err.message : String(err)}`
|
|
714
|
+
);
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
throw invalidInput('Provide --manifest <path> or request.manifest');
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
/**
|
|
722
|
+
* @param {Manifest} manifest
|
|
723
|
+
* @param {string} treeOid
|
|
724
|
+
* @returns {{ data: Record<string, any> }}
|
|
725
|
+
*/
|
|
726
|
+
function buildTreeOutcome(manifest, treeOid) {
|
|
727
|
+
return {
|
|
728
|
+
data: {
|
|
729
|
+
treeOid,
|
|
730
|
+
slug: manifest.slug,
|
|
731
|
+
chunkCount: manifest.chunks.length,
|
|
732
|
+
encrypted: Boolean(manifest.encryption?.encrypted),
|
|
733
|
+
compressed: Boolean(manifest.compression),
|
|
734
|
+
},
|
|
735
|
+
};
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
const COMMAND_HANDLERS = Object.freeze({
|
|
739
|
+
store: storeCommand,
|
|
740
|
+
tree: treeCommand,
|
|
741
|
+
restore: restoreCommand,
|
|
742
|
+
rotate: rotateCommand,
|
|
743
|
+
inspect: inspectCommand,
|
|
744
|
+
verify: verifyCommand,
|
|
745
|
+
doctor: doctorCommand,
|
|
746
|
+
'recipient.add': recipientAddCommand,
|
|
747
|
+
'recipient.remove': recipientRemoveCommand,
|
|
748
|
+
'recipient.list': recipientListCommand,
|
|
749
|
+
'vault.init': vaultInitCommand,
|
|
750
|
+
'vault.list': vaultListCommand,
|
|
751
|
+
'vault.info': vaultInfoCommand,
|
|
752
|
+
'vault.history': vaultHistoryCommand,
|
|
753
|
+
'vault.remove': vaultRemoveCommand,
|
|
754
|
+
'vault.rotate': vaultRotateCommand,
|
|
755
|
+
'vault.stats': vaultStatsCommand,
|
|
756
|
+
});
|
|
757
|
+
|
|
758
|
+
/**
|
|
759
|
+
* @param {string} command
|
|
760
|
+
* @param {string[]} args
|
|
761
|
+
* @param {{ stdin: NodeJS.ReadStream, session: ReturnType<typeof import('../protocol.js').createAgentSession> }} context
|
|
762
|
+
* @returns {Promise<{ exitCode?: number, data: Record<string, any> }>}
|
|
763
|
+
*/
|
|
764
|
+
export async function executeAgentCommand(command, args, context) {
|
|
765
|
+
const handler = COMMAND_HANDLERS[command];
|
|
766
|
+
|
|
767
|
+
if (!handler) {
|
|
768
|
+
throw invalidInput('Unknown agent command', {
|
|
769
|
+
command,
|
|
770
|
+
availableCommands: AVAILABLE_COMMANDS,
|
|
771
|
+
});
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
return handler(args, context.stdin, context.session);
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
/**
|
|
778
|
+
* @param {string[]} args
|
|
779
|
+
* @param {NodeJS.ReadStream} stdin
|
|
780
|
+
* @returns {Promise<{ input: Record<string, any>, requestSource?: string }>}
|
|
781
|
+
*/
|
|
782
|
+
async function parseRestoreInput(args, stdin) {
|
|
783
|
+
const { values, positionals, requestSource } = await parseAgentInput(
|
|
784
|
+
args,
|
|
785
|
+
{
|
|
786
|
+
slug: { type: 'string' },
|
|
787
|
+
oid: { type: 'string' },
|
|
788
|
+
out: { type: 'string' },
|
|
789
|
+
cwd: { type: 'string' },
|
|
790
|
+
'key-file': { type: 'string' },
|
|
791
|
+
'vault-passphrase': { type: 'string' },
|
|
792
|
+
'vault-passphrase-file': { type: 'string' },
|
|
793
|
+
'os-keychain-target': { type: 'string' },
|
|
794
|
+
'os-keychain-account': { type: 'string' },
|
|
795
|
+
},
|
|
796
|
+
stdin
|
|
797
|
+
);
|
|
798
|
+
assignPositionals(positionals, []);
|
|
799
|
+
|
|
800
|
+
return {
|
|
801
|
+
input: normalizeInputAliases(values),
|
|
802
|
+
requestSource,
|
|
803
|
+
};
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
/**
|
|
807
|
+
* @param {{
|
|
808
|
+
* manifest: import('../../../src/domain/value-objects/Manifest.js').default,
|
|
809
|
+
* treeOid: string,
|
|
810
|
+
* outputPath: string,
|
|
811
|
+
* bytesWritten: number,
|
|
812
|
+
* }} options
|
|
813
|
+
* @returns {{ data: Record<string, any> }}
|
|
814
|
+
*/
|
|
815
|
+
function buildRestoreOutcome({ manifest, treeOid, outputPath, bytesWritten }) {
|
|
816
|
+
return {
|
|
817
|
+
data: {
|
|
818
|
+
slug: manifest.slug,
|
|
819
|
+
treeOid,
|
|
820
|
+
outputPath,
|
|
821
|
+
bytesWritten,
|
|
822
|
+
encrypted: Boolean(manifest.encryption?.encrypted),
|
|
823
|
+
},
|
|
824
|
+
};
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
/**
|
|
828
|
+
* @param {{
|
|
829
|
+
* cas: ContentAddressableStore,
|
|
830
|
+
* input: Record<string, any>,
|
|
831
|
+
* stdin: NodeJS.ReadStream,
|
|
832
|
+
* session: ReturnType<typeof import('../protocol.js').createAgentSession>,
|
|
833
|
+
* }} params
|
|
834
|
+
* @returns {Promise<Uint8Array | undefined>}
|
|
835
|
+
*/
|
|
836
|
+
async function readStoreEncryptionKey({ cas, input, stdin, session }) {
|
|
837
|
+
return await resolveAgentStoreEncryptionKey(cas, input, {
|
|
838
|
+
stdin,
|
|
839
|
+
onWarning: (warning) => session.writeWarning(warning),
|
|
840
|
+
readKeyFile,
|
|
841
|
+
resolveVaultPassphrase,
|
|
842
|
+
errorFactory: invalidInput,
|
|
843
|
+
});
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
/**
|
|
847
|
+
* @param {{
|
|
848
|
+
* cas: ContentAddressableStore,
|
|
849
|
+
* manifest: import('../../../src/domain/value-objects/Manifest.js').default,
|
|
850
|
+
* input: Record<string, any>,
|
|
851
|
+
* stdin: NodeJS.ReadStream,
|
|
852
|
+
* session: ReturnType<typeof import('../protocol.js').createAgentSession>,
|
|
853
|
+
* requestSource: string | undefined,
|
|
854
|
+
* treeOid: string,
|
|
855
|
+
* }} params
|
|
856
|
+
* @returns {Promise<Uint8Array | undefined>}
|
|
857
|
+
*/
|
|
858
|
+
async function readRestoreEncryptionKey({
|
|
859
|
+
cas,
|
|
860
|
+
manifest,
|
|
861
|
+
input,
|
|
862
|
+
stdin,
|
|
863
|
+
session,
|
|
864
|
+
requestSource,
|
|
865
|
+
treeOid,
|
|
866
|
+
}) {
|
|
867
|
+
return await resolveAgentRestoreEncryptionKey(
|
|
868
|
+
{
|
|
869
|
+
cas,
|
|
870
|
+
manifest,
|
|
871
|
+
input: {
|
|
872
|
+
...input,
|
|
873
|
+
stdin,
|
|
874
|
+
onWarning: (warning) => session.writeWarning(warning),
|
|
875
|
+
},
|
|
876
|
+
requestSource,
|
|
877
|
+
treeOid,
|
|
878
|
+
},
|
|
879
|
+
{
|
|
880
|
+
readKeyFile,
|
|
881
|
+
resolveVaultPassphrase,
|
|
882
|
+
errorFactory: invalidInput,
|
|
883
|
+
needsInputFactory: needsInput,
|
|
884
|
+
}
|
|
885
|
+
);
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
/**
|
|
889
|
+
* @param {string[]} args
|
|
890
|
+
* @param {NodeJS.ReadStream} stdin
|
|
891
|
+
* @returns {Promise<{ data: Record<string, any> }>}
|
|
892
|
+
*/
|
|
893
|
+
async function storeCommand(args, stdin, session) {
|
|
894
|
+
const input = await parseStoreInput(args, stdin);
|
|
895
|
+
validateStoreInput(input);
|
|
896
|
+
validateCredentialSources(input);
|
|
897
|
+
writeAgentStart(
|
|
898
|
+
session,
|
|
899
|
+
selectStartInput(input, [
|
|
900
|
+
'cwd',
|
|
901
|
+
'file',
|
|
902
|
+
'slug',
|
|
903
|
+
'tree',
|
|
904
|
+
'force',
|
|
905
|
+
'gzip',
|
|
906
|
+
'keyFile',
|
|
907
|
+
'vaultPassphrase',
|
|
908
|
+
'vaultPassphraseFile',
|
|
909
|
+
'osKeychainTarget',
|
|
910
|
+
'osKeychainAccount',
|
|
911
|
+
'requestSource',
|
|
912
|
+
])
|
|
913
|
+
);
|
|
914
|
+
|
|
915
|
+
const cas = await createCas(input.cwd || '.');
|
|
916
|
+
const encryptionKey = await readStoreEncryptionKey({ cas, input, stdin, session });
|
|
917
|
+
const vaultEncryptionKey = encryptionKey && !input.keyFile ? encryptionKey : undefined;
|
|
918
|
+
const manifest = await cas.storeFile({
|
|
919
|
+
filePath: input.file,
|
|
920
|
+
slug: input.slug,
|
|
921
|
+
...(encryptionKey ? { encryptionKey } : {}),
|
|
922
|
+
...(input.gzip ? { compression: { algorithm: 'gzip' } } : {}),
|
|
923
|
+
});
|
|
924
|
+
|
|
925
|
+
let treeOid;
|
|
926
|
+
let commitOid;
|
|
927
|
+
if (input.tree) {
|
|
928
|
+
treeOid = await cas.createTree({ manifest });
|
|
929
|
+
({ commitOid } = await cas.addToVault({
|
|
930
|
+
slug: input.slug,
|
|
931
|
+
treeOid,
|
|
932
|
+
force: Boolean(input.force),
|
|
933
|
+
...(vaultEncryptionKey ? { encryptionKey: vaultEncryptionKey } : {}),
|
|
934
|
+
}));
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
return buildStoreOutcome({ input, manifest, treeOid, commitOid });
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
/**
|
|
941
|
+
* @param {string[]} args
|
|
942
|
+
* @param {NodeJS.ReadStream} stdin
|
|
943
|
+
* @returns {Promise<{ data: Record<string, any> }>}
|
|
944
|
+
*/
|
|
945
|
+
async function treeCommand(args, stdin, session) {
|
|
946
|
+
const input = await parseTreeInput(args, stdin);
|
|
947
|
+
const manifest = resolveManifestInput(input);
|
|
948
|
+
writeAgentStart(session, selectStartInput(input, ['cwd', 'manifest', 'requestSource']));
|
|
949
|
+
const cas = await createCas(input.cwd || '.');
|
|
950
|
+
const treeOid = await cas.createTree({ manifest });
|
|
951
|
+
|
|
952
|
+
return buildTreeOutcome(manifest, treeOid);
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
/**
|
|
956
|
+
* @param {string[]} args
|
|
957
|
+
* @param {NodeJS.ReadStream} stdin
|
|
958
|
+
* @returns {Promise<{ data: Record<string, any> }>}
|
|
959
|
+
*/
|
|
960
|
+
async function restoreCommand(args, stdin, session) {
|
|
961
|
+
const { input, requestSource } = await parseRestoreInput(args, stdin);
|
|
962
|
+
if (!input.out) {
|
|
963
|
+
throw invalidInput('Provide --out <path>');
|
|
964
|
+
}
|
|
965
|
+
validateCredentialSources(input);
|
|
966
|
+
const target = resolveTarget(input);
|
|
967
|
+
writeAgentStart(
|
|
968
|
+
session,
|
|
969
|
+
selectStartInput(input, [
|
|
970
|
+
'cwd',
|
|
971
|
+
'slug',
|
|
972
|
+
'oid',
|
|
973
|
+
'out',
|
|
974
|
+
'keyFile',
|
|
975
|
+
'vaultPassphrase',
|
|
976
|
+
'vaultPassphraseFile',
|
|
977
|
+
'osKeychainTarget',
|
|
978
|
+
'osKeychainAccount',
|
|
979
|
+
'requestSource',
|
|
980
|
+
])
|
|
981
|
+
);
|
|
982
|
+
const { cas, treeOid } = await resolveTree(target);
|
|
983
|
+
const manifest = await cas.readManifest({ treeOid });
|
|
984
|
+
const encryptionKey = await readRestoreEncryptionKey({
|
|
985
|
+
cas,
|
|
986
|
+
manifest,
|
|
987
|
+
input,
|
|
988
|
+
stdin,
|
|
989
|
+
session,
|
|
990
|
+
requestSource,
|
|
991
|
+
treeOid,
|
|
992
|
+
});
|
|
993
|
+
const restoreTarget = resolveRestoreOutputTarget(input.out);
|
|
994
|
+
const { bytesWritten } = await cas.restoreFile({
|
|
995
|
+
manifest,
|
|
996
|
+
...(encryptionKey ? { encryptionKey } : {}),
|
|
997
|
+
outputPath: restoreTarget.outputPath,
|
|
998
|
+
baseDirectory: restoreTarget.baseDirectory,
|
|
999
|
+
});
|
|
1000
|
+
|
|
1001
|
+
return buildRestoreOutcome({
|
|
1002
|
+
manifest,
|
|
1003
|
+
treeOid,
|
|
1004
|
+
outputPath: input.out,
|
|
1005
|
+
bytesWritten,
|
|
1006
|
+
});
|
|
1007
|
+
}
|
|
1008
|
+
|
|
1009
|
+
/**
|
|
1010
|
+
* @param {string[]} args
|
|
1011
|
+
* @param {NodeJS.ReadStream} stdin
|
|
1012
|
+
* @returns {Promise<{ data: Record<string, any> }>}
|
|
1013
|
+
*/
|
|
1014
|
+
async function rotateCommand(args, stdin, session) {
|
|
1015
|
+
const input = await parseRotateInput(args, stdin);
|
|
1016
|
+
const target = resolveTarget(input);
|
|
1017
|
+
validateRotateInput(input);
|
|
1018
|
+
writeAgentStart(
|
|
1019
|
+
session,
|
|
1020
|
+
selectStartInput(input, ['cwd', 'slug', 'oid', 'label', 'oldKeyFile', 'newKeyFile'])
|
|
1021
|
+
);
|
|
1022
|
+
|
|
1023
|
+
const { cas, treeOid: previousTreeOid } = await resolveTree(target);
|
|
1024
|
+
const manifest = await cas.readManifest({ treeOid: previousTreeOid });
|
|
1025
|
+
const updated = await cas.rotateKey({
|
|
1026
|
+
manifest,
|
|
1027
|
+
oldKey: readKeyFile(input.oldKeyFile),
|
|
1028
|
+
newKey: readKeyFile(input.newKeyFile),
|
|
1029
|
+
...(input.label ? { label: input.label } : {}),
|
|
1030
|
+
});
|
|
1031
|
+
|
|
1032
|
+
const treeOid = await cas.createTree({ manifest: updated });
|
|
1033
|
+
let commitOid;
|
|
1034
|
+
if (input.slug) {
|
|
1035
|
+
({ commitOid } = await cas.addToVault({
|
|
1036
|
+
slug: input.slug,
|
|
1037
|
+
treeOid,
|
|
1038
|
+
force: true,
|
|
1039
|
+
}));
|
|
1040
|
+
}
|
|
1041
|
+
|
|
1042
|
+
return buildRotateOutcome({
|
|
1043
|
+
previousTreeOid,
|
|
1044
|
+
treeOid,
|
|
1045
|
+
commitOid,
|
|
1046
|
+
manifest: updated,
|
|
1047
|
+
label: input.label,
|
|
1048
|
+
});
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
/**
|
|
1052
|
+
* @param {string[]} args
|
|
1053
|
+
* @param {NodeJS.ReadStream} stdin
|
|
1054
|
+
* @returns {Promise<{ data: Record<string, any> }>}
|
|
1055
|
+
*/
|
|
1056
|
+
async function inspectCommand(args, stdin, session) {
|
|
1057
|
+
const { values, positionals } = await parseAgentInput(
|
|
1058
|
+
args,
|
|
1059
|
+
{
|
|
1060
|
+
slug: { type: 'string' },
|
|
1061
|
+
oid: { type: 'string' },
|
|
1062
|
+
cwd: { type: 'string' },
|
|
1063
|
+
},
|
|
1064
|
+
stdin
|
|
1065
|
+
);
|
|
1066
|
+
const positionalInput = assignPositionals(positionals, []);
|
|
1067
|
+
const input = resolveTarget({ ...values, ...positionalInput });
|
|
1068
|
+
writeAgentStart(session, selectStartInput(input, ['cwd', 'slug', 'oid']));
|
|
1069
|
+
const { cas, treeOid } = await resolveTree(input);
|
|
1070
|
+
const manifest = await cas.readManifest({ treeOid });
|
|
1071
|
+
return {
|
|
1072
|
+
data: {
|
|
1073
|
+
treeOid,
|
|
1074
|
+
manifest: manifest.toJSON(),
|
|
1075
|
+
},
|
|
1076
|
+
};
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
/**
|
|
1080
|
+
* @param {string[]} args
|
|
1081
|
+
* @param {NodeJS.ReadStream} stdin
|
|
1082
|
+
* @returns {Promise<{ exitCode: number, data: Record<string, any> }>}
|
|
1083
|
+
*/
|
|
1084
|
+
async function verifyCommand(args, stdin, session) {
|
|
1085
|
+
const { values, positionals } = await parseAgentInput(
|
|
1086
|
+
args,
|
|
1087
|
+
{
|
|
1088
|
+
slug: { type: 'string' },
|
|
1089
|
+
oid: { type: 'string' },
|
|
1090
|
+
cwd: { type: 'string' },
|
|
1091
|
+
},
|
|
1092
|
+
stdin
|
|
1093
|
+
);
|
|
1094
|
+
const positionalInput = assignPositionals(positionals, []);
|
|
1095
|
+
const input = resolveTarget({ ...values, ...positionalInput });
|
|
1096
|
+
writeAgentStart(session, selectStartInput(input, ['cwd', 'slug', 'oid']));
|
|
1097
|
+
const { cas, treeOid } = await resolveTree(input);
|
|
1098
|
+
const manifest = await cas.readManifest({ treeOid });
|
|
1099
|
+
const ok = await cas.verifyIntegrity(manifest);
|
|
1100
|
+
|
|
1101
|
+
return {
|
|
1102
|
+
exitCode: ok ? AGENT_EXIT_CODES.SUCCESS : AGENT_EXIT_CODES.VERIFICATION_FAILED,
|
|
1103
|
+
data: {
|
|
1104
|
+
ok,
|
|
1105
|
+
slug: manifest.slug,
|
|
1106
|
+
treeOid,
|
|
1107
|
+
chunks: manifest.chunks.length,
|
|
1108
|
+
},
|
|
1109
|
+
};
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1112
|
+
/**
|
|
1113
|
+
* @param {string[]} args
|
|
1114
|
+
* @param {NodeJS.ReadStream} stdin
|
|
1115
|
+
* @returns {Promise<{ data: Record<string, any> }>}
|
|
1116
|
+
*/
|
|
1117
|
+
async function recipientAddCommand(args, stdin, session) {
|
|
1118
|
+
const input = await parseRecipientAddInput(args, stdin);
|
|
1119
|
+
validateRecipientAddInput(input);
|
|
1120
|
+
writeAgentStart(
|
|
1121
|
+
session,
|
|
1122
|
+
selectStartInput(input, ['cwd', 'slug', 'label', 'keyFile', 'existingKeyFile'])
|
|
1123
|
+
);
|
|
1124
|
+
|
|
1125
|
+
const target = resolveSlugTarget(input);
|
|
1126
|
+
const { cas, treeOid: previousTreeOid, manifest } = await resolveVaultManifestBySlug(target);
|
|
1127
|
+
const updated = await cas.addRecipient({
|
|
1128
|
+
manifest,
|
|
1129
|
+
existingKey: readKeyFile(input.existingKeyFile),
|
|
1130
|
+
newRecipientKey: readKeyFile(input.keyFile),
|
|
1131
|
+
label: input.label,
|
|
1132
|
+
});
|
|
1133
|
+
const treeOid = await cas.createTree({ manifest: updated });
|
|
1134
|
+
const { commitOid } = await cas.addToVault({
|
|
1135
|
+
slug: input.slug,
|
|
1136
|
+
treeOid,
|
|
1137
|
+
force: true,
|
|
1138
|
+
});
|
|
1139
|
+
|
|
1140
|
+
return buildRecipientMutationOutcome({
|
|
1141
|
+
action: 'add',
|
|
1142
|
+
slug: input.slug,
|
|
1143
|
+
label: input.label,
|
|
1144
|
+
previousTreeOid,
|
|
1145
|
+
treeOid,
|
|
1146
|
+
commitOid,
|
|
1147
|
+
manifest: updated,
|
|
1148
|
+
});
|
|
1149
|
+
}
|
|
1150
|
+
|
|
1151
|
+
/**
|
|
1152
|
+
* @param {string[]} args
|
|
1153
|
+
* @param {NodeJS.ReadStream} stdin
|
|
1154
|
+
* @returns {Promise<{ data: Record<string, any> }>}
|
|
1155
|
+
*/
|
|
1156
|
+
async function recipientRemoveCommand(args, stdin, session) {
|
|
1157
|
+
const input = await parseRecipientRemoveInput(args, stdin);
|
|
1158
|
+
validateRecipientRemoveInput(input);
|
|
1159
|
+
writeAgentStart(session, selectStartInput(input, ['cwd', 'slug', 'label']));
|
|
1160
|
+
|
|
1161
|
+
const target = resolveSlugTarget(input);
|
|
1162
|
+
const { cas, treeOid: previousTreeOid, manifest } = await resolveVaultManifestBySlug(target);
|
|
1163
|
+
const updated = await cas.removeRecipient({
|
|
1164
|
+
manifest,
|
|
1165
|
+
label: input.label,
|
|
1166
|
+
});
|
|
1167
|
+
const treeOid = await cas.createTree({ manifest: updated });
|
|
1168
|
+
const { commitOid } = await cas.addToVault({
|
|
1169
|
+
slug: input.slug,
|
|
1170
|
+
treeOid,
|
|
1171
|
+
force: true,
|
|
1172
|
+
});
|
|
1173
|
+
|
|
1174
|
+
return buildRecipientMutationOutcome({
|
|
1175
|
+
action: 'remove',
|
|
1176
|
+
slug: input.slug,
|
|
1177
|
+
label: input.label,
|
|
1178
|
+
previousTreeOid,
|
|
1179
|
+
treeOid,
|
|
1180
|
+
commitOid,
|
|
1181
|
+
manifest: updated,
|
|
1182
|
+
});
|
|
1183
|
+
}
|
|
1184
|
+
|
|
1185
|
+
/**
|
|
1186
|
+
* @param {import('../../../src/domain/value-objects/Manifest.js').default} manifest
|
|
1187
|
+
* @returns {Array<{ label: string, keyVersion?: number }>}
|
|
1188
|
+
*/
|
|
1189
|
+
function buildRecipientRows(manifest) {
|
|
1190
|
+
return (manifest.encryption?.recipients || []).map((recipient) => ({
|
|
1191
|
+
label: recipient.label,
|
|
1192
|
+
...(recipient.keyVersion !== undefined ? { keyVersion: recipient.keyVersion } : {}),
|
|
1193
|
+
}));
|
|
1194
|
+
}
|
|
1195
|
+
|
|
1196
|
+
/**
|
|
1197
|
+
* @param {string[]} args
|
|
1198
|
+
* @param {NodeJS.ReadStream} stdin
|
|
1199
|
+
* @returns {Promise<{ data: Record<string, any> }>}
|
|
1200
|
+
*/
|
|
1201
|
+
async function recipientListCommand(args, stdin, session) {
|
|
1202
|
+
const { values, positionals } = await parseAgentInput(
|
|
1203
|
+
args,
|
|
1204
|
+
{
|
|
1205
|
+
slug: { type: 'string' },
|
|
1206
|
+
oid: { type: 'string' },
|
|
1207
|
+
cwd: { type: 'string' },
|
|
1208
|
+
},
|
|
1209
|
+
stdin
|
|
1210
|
+
);
|
|
1211
|
+
assignPositionals(positionals, []);
|
|
1212
|
+
|
|
1213
|
+
const input = resolveTarget(values);
|
|
1214
|
+
writeAgentStart(session, selectStartInput(input, ['cwd', 'slug', 'oid']));
|
|
1215
|
+
const { cas, treeOid } = await resolveTree(input);
|
|
1216
|
+
const manifest = await cas.readManifest({ treeOid });
|
|
1217
|
+
const recipients = buildRecipientRows(manifest);
|
|
1218
|
+
|
|
1219
|
+
return {
|
|
1220
|
+
data: {
|
|
1221
|
+
slug: manifest.slug,
|
|
1222
|
+
treeOid,
|
|
1223
|
+
envelope: recipients.length > 0,
|
|
1224
|
+
recipientCount: recipients.length,
|
|
1225
|
+
recipients,
|
|
1226
|
+
},
|
|
1227
|
+
};
|
|
1228
|
+
}
|
|
1229
|
+
|
|
1230
|
+
/**
|
|
1231
|
+
* @param {string[]} args
|
|
1232
|
+
* @param {NodeJS.ReadStream} stdin
|
|
1233
|
+
* @returns {Promise<{ data: Record<string, any> }>}
|
|
1234
|
+
*/
|
|
1235
|
+
async function vaultInitCommand(args, stdin, session) {
|
|
1236
|
+
const input = await parseVaultInitInput(args, stdin);
|
|
1237
|
+
validateVaultInitInput(input);
|
|
1238
|
+
writeAgentStart(
|
|
1239
|
+
session,
|
|
1240
|
+
selectStartInput(input, [
|
|
1241
|
+
'cwd',
|
|
1242
|
+
'algorithm',
|
|
1243
|
+
'passphrase',
|
|
1244
|
+
'passphraseFile',
|
|
1245
|
+
'osKeychainTarget',
|
|
1246
|
+
'osKeychainAccount',
|
|
1247
|
+
'requestSource',
|
|
1248
|
+
])
|
|
1249
|
+
);
|
|
1250
|
+
|
|
1251
|
+
const passphrase = await resolveVaultInitPassphrase(input, input.requestSource, {
|
|
1252
|
+
stdin,
|
|
1253
|
+
onWarning: (warning) => session.writeWarning(warning),
|
|
1254
|
+
});
|
|
1255
|
+
const algorithm = parseKdfAlgorithm(input.algorithm);
|
|
1256
|
+
const cas = await createCas(input.cwd || '.');
|
|
1257
|
+
const { commitOid } = await cas.initVault({
|
|
1258
|
+
...(passphrase ? { passphrase } : {}),
|
|
1259
|
+
...(passphrase && algorithm ? { kdfOptions: { algorithm } } : {}),
|
|
1260
|
+
});
|
|
1261
|
+
const metadata = await cas.getVaultMetadata();
|
|
1262
|
+
|
|
1263
|
+
return buildVaultInitOutcome({
|
|
1264
|
+
commitOid,
|
|
1265
|
+
encrypted: Boolean(metadata?.encryption),
|
|
1266
|
+
kdfAlgorithm: metadata?.encryption?.kdf?.algorithm,
|
|
1267
|
+
});
|
|
1268
|
+
}
|
|
1269
|
+
|
|
1270
|
+
/**
|
|
1271
|
+
* @param {string[]} args
|
|
1272
|
+
* @param {NodeJS.ReadStream} stdin
|
|
1273
|
+
* @returns {Promise<{ data: Record<string, any> }>}
|
|
1274
|
+
*/
|
|
1275
|
+
async function vaultRemoveCommand(args, stdin, session) {
|
|
1276
|
+
const { values, positionals } = await parseAgentInput(
|
|
1277
|
+
args,
|
|
1278
|
+
{
|
|
1279
|
+
slug: { type: 'string' },
|
|
1280
|
+
cwd: { type: 'string' },
|
|
1281
|
+
},
|
|
1282
|
+
stdin
|
|
1283
|
+
);
|
|
1284
|
+
assignPositionals(positionals, []);
|
|
1285
|
+
|
|
1286
|
+
if (!values.slug) {
|
|
1287
|
+
throw invalidInput('Provide --slug <slug>');
|
|
1288
|
+
}
|
|
1289
|
+
values.slug = Slug.from(values.slug).toString();
|
|
1290
|
+
writeAgentStart(session, selectStartInput(values, ['cwd', 'slug']));
|
|
1291
|
+
|
|
1292
|
+
const cas = await createCas(values.cwd || '.');
|
|
1293
|
+
const { commitOid, removedTreeOid } = await cas.removeFromVault({ slug: values.slug });
|
|
1294
|
+
|
|
1295
|
+
return buildVaultRemoveOutcome({
|
|
1296
|
+
slug: values.slug,
|
|
1297
|
+
commitOid,
|
|
1298
|
+
removedTreeOid,
|
|
1299
|
+
});
|
|
1300
|
+
}
|
|
1301
|
+
|
|
1302
|
+
/**
|
|
1303
|
+
* @param {string[]} args
|
|
1304
|
+
* @param {NodeJS.ReadStream} stdin
|
|
1305
|
+
* @returns {Promise<{ data: Record<string, any> }>}
|
|
1306
|
+
*/
|
|
1307
|
+
async function vaultRotateCommand(args, stdin, session) {
|
|
1308
|
+
const input = await parseVaultRotateInput(args, stdin);
|
|
1309
|
+
validateVaultRotateInput(input);
|
|
1310
|
+
writeAgentStart(
|
|
1311
|
+
session,
|
|
1312
|
+
selectStartInput(input, [
|
|
1313
|
+
'cwd',
|
|
1314
|
+
'algorithm',
|
|
1315
|
+
'oldPassphrase',
|
|
1316
|
+
'oldPassphraseFile',
|
|
1317
|
+
'oldOsKeychainTarget',
|
|
1318
|
+
'oldOsKeychainAccount',
|
|
1319
|
+
'newPassphrase',
|
|
1320
|
+
'newPassphraseFile',
|
|
1321
|
+
'newOsKeychainTarget',
|
|
1322
|
+
'newOsKeychainAccount',
|
|
1323
|
+
'requestSource',
|
|
1324
|
+
])
|
|
1325
|
+
);
|
|
1326
|
+
|
|
1327
|
+
const { oldPassphrase, newPassphrase } = await resolveVaultRotatePassphrases(
|
|
1328
|
+
input,
|
|
1329
|
+
input.requestSource,
|
|
1330
|
+
{
|
|
1331
|
+
stdin,
|
|
1332
|
+
onWarning: (warning) => session.writeWarning(warning),
|
|
1333
|
+
}
|
|
1334
|
+
);
|
|
1335
|
+
const cas = await createCas(input.cwd || '.');
|
|
1336
|
+
const { commitOid, rotatedSlugs, skippedSlugs } = await cas.rotateVaultPassphrase({
|
|
1337
|
+
oldPassphrase,
|
|
1338
|
+
newPassphrase,
|
|
1339
|
+
...(input.algorithm ? { kdfOptions: { algorithm: parseKdfAlgorithm(input.algorithm) } } : {}),
|
|
1340
|
+
});
|
|
1341
|
+
const metadata = await cas.getVaultMetadata();
|
|
1342
|
+
|
|
1343
|
+
return buildVaultRotateOutcome({
|
|
1344
|
+
commitOid,
|
|
1345
|
+
rotatedSlugs,
|
|
1346
|
+
skippedSlugs,
|
|
1347
|
+
kdfAlgorithm: metadata?.encryption?.kdf?.algorithm,
|
|
1348
|
+
});
|
|
1349
|
+
}
|
|
1350
|
+
|
|
1351
|
+
/**
|
|
1352
|
+
* @param {string[]} args
|
|
1353
|
+
* @param {NodeJS.ReadStream} stdin
|
|
1354
|
+
* @returns {Promise<{ data: Record<string, any> }>}
|
|
1355
|
+
*/
|
|
1356
|
+
async function vaultListCommand(args, stdin, session) {
|
|
1357
|
+
const { values, positionals } = await parseAgentInput(
|
|
1358
|
+
args,
|
|
1359
|
+
{
|
|
1360
|
+
cwd: { type: 'string' },
|
|
1361
|
+
filter: { type: 'string' },
|
|
1362
|
+
},
|
|
1363
|
+
stdin
|
|
1364
|
+
);
|
|
1365
|
+
assignPositionals(positionals, []);
|
|
1366
|
+
writeAgentStart(session, selectStartInput(values, ['cwd', 'filter']));
|
|
1367
|
+
|
|
1368
|
+
const cas = await createCas(values.cwd || '.');
|
|
1369
|
+
const all = await cas.listVault();
|
|
1370
|
+
const entries = filterEntries(all, values.filter);
|
|
1371
|
+
|
|
1372
|
+
return {
|
|
1373
|
+
data: { entries },
|
|
1374
|
+
};
|
|
1375
|
+
}
|
|
1376
|
+
|
|
1377
|
+
/**
|
|
1378
|
+
* @param {string[]} args
|
|
1379
|
+
* @param {NodeJS.ReadStream} stdin
|
|
1380
|
+
* @returns {Promise<{ data: Record<string, any> }>}
|
|
1381
|
+
*/
|
|
1382
|
+
async function vaultInfoCommand(args, stdin, session) {
|
|
1383
|
+
const { values, positionals } = await parseAgentInput(
|
|
1384
|
+
args,
|
|
1385
|
+
{
|
|
1386
|
+
cwd: { type: 'string' },
|
|
1387
|
+
encryption: { type: 'boolean' },
|
|
1388
|
+
},
|
|
1389
|
+
stdin
|
|
1390
|
+
);
|
|
1391
|
+
const input = { ...values, ...assignPositionals(positionals, ['slug']) };
|
|
1392
|
+
|
|
1393
|
+
if (!input.slug) {
|
|
1394
|
+
throw invalidInput('Provide a vault slug');
|
|
1395
|
+
}
|
|
1396
|
+
input.slug = Slug.from(input.slug).toString();
|
|
1397
|
+
writeAgentStart(session, selectStartInput(input, ['cwd', 'slug', 'encryption']));
|
|
1398
|
+
|
|
1399
|
+
const cas = await createCas(input.cwd || '.');
|
|
1400
|
+
const treeOid = await cas.resolveVaultEntry({ slug: input.slug });
|
|
1401
|
+
/** @type {Record<string, any>} */
|
|
1402
|
+
const result = {
|
|
1403
|
+
slug: input.slug,
|
|
1404
|
+
treeOid,
|
|
1405
|
+
};
|
|
1406
|
+
|
|
1407
|
+
if (input.encryption) {
|
|
1408
|
+
const metadata = await cas.getVaultMetadata();
|
|
1409
|
+
if (metadata?.encryption) {
|
|
1410
|
+
result.encryption = metadata.encryption;
|
|
1411
|
+
}
|
|
1412
|
+
}
|
|
1413
|
+
|
|
1414
|
+
return { data: result };
|
|
1415
|
+
}
|
|
1416
|
+
|
|
1417
|
+
/**
|
|
1418
|
+
* @param {string[]} args
|
|
1419
|
+
* @param {NodeJS.ReadStream} stdin
|
|
1420
|
+
* @returns {Promise<{ data: Record<string, any> }>}
|
|
1421
|
+
*/
|
|
1422
|
+
async function vaultHistoryCommand(args, stdin, session) {
|
|
1423
|
+
const { values, positionals } = await parseAgentInput(
|
|
1424
|
+
args,
|
|
1425
|
+
{
|
|
1426
|
+
cwd: { type: 'string' },
|
|
1427
|
+
'max-count': { type: 'string' },
|
|
1428
|
+
},
|
|
1429
|
+
stdin
|
|
1430
|
+
);
|
|
1431
|
+
assignPositionals(positionals, []);
|
|
1432
|
+
const plumbing = await createGitPlumbing({ cwd: values.cwd || '.' });
|
|
1433
|
+
const argsForGit = ['log', '--oneline', ContentAddressableStore.VAULT_REF];
|
|
1434
|
+
const maxCount = parsePositiveInteger(values['max-count']);
|
|
1435
|
+
writeAgentStart(
|
|
1436
|
+
session,
|
|
1437
|
+
selectStartInput({
|
|
1438
|
+
cwd: values.cwd,
|
|
1439
|
+
maxCount,
|
|
1440
|
+
}, ['cwd', 'maxCount'])
|
|
1441
|
+
);
|
|
1442
|
+
if (maxCount !== undefined) {
|
|
1443
|
+
argsForGit.push(`-${maxCount}`);
|
|
1444
|
+
}
|
|
1445
|
+
|
|
1446
|
+
const output = await plumbing.execute({ args: argsForGit });
|
|
1447
|
+
const history = output
|
|
1448
|
+
.split('\n')
|
|
1449
|
+
.filter(Boolean)
|
|
1450
|
+
.map((line) => {
|
|
1451
|
+
const [commitOid, ...messageParts] = line.trim().split(/\s+/);
|
|
1452
|
+
return { commitOid, message: messageParts.join(' ') };
|
|
1453
|
+
});
|
|
1454
|
+
|
|
1455
|
+
return { data: { history } };
|
|
1456
|
+
}
|
|
1457
|
+
|
|
1458
|
+
/**
|
|
1459
|
+
* @param {string[]} args
|
|
1460
|
+
* @param {NodeJS.ReadStream} stdin
|
|
1461
|
+
* @returns {Promise<{ data: Record<string, any> }>}
|
|
1462
|
+
*/
|
|
1463
|
+
async function vaultStatsCommand(args, stdin, session) {
|
|
1464
|
+
const { values, positionals } = await parseAgentInput(
|
|
1465
|
+
args,
|
|
1466
|
+
{
|
|
1467
|
+
cwd: { type: 'string' },
|
|
1468
|
+
filter: { type: 'string' },
|
|
1469
|
+
},
|
|
1470
|
+
stdin
|
|
1471
|
+
);
|
|
1472
|
+
assignPositionals(positionals, []);
|
|
1473
|
+
writeAgentStart(session, selectStartInput(values, ['cwd', 'filter']));
|
|
1474
|
+
|
|
1475
|
+
const cas = await createCas(values.cwd || '.');
|
|
1476
|
+
const all = await cas.listVault();
|
|
1477
|
+
const entries = filterEntries(all, values.filter);
|
|
1478
|
+
const records = [];
|
|
1479
|
+
for (const entry of entries) {
|
|
1480
|
+
const manifest = await cas.readManifest({ treeOid: entry.treeOid });
|
|
1481
|
+
records.push({ ...entry, manifest });
|
|
1482
|
+
}
|
|
1483
|
+
|
|
1484
|
+
return {
|
|
1485
|
+
data: {
|
|
1486
|
+
stats: buildVaultStats(records),
|
|
1487
|
+
},
|
|
1488
|
+
};
|
|
1489
|
+
}
|