feat(security): implement AES-256-GCM vault, Panic Lock, Argon2id+TOTP auth, guards, and rotate-key CLI
This commit is contained in:
parent
2c60483a90
commit
3b9e084916
5 changed files with 1249 additions and 0 deletions
110
src/cli/gcoord.ts
Normal file
110
src/cli/gcoord.ts
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import readline from 'node:readline/promises';
|
||||
import { parseArgs } from 'node:util';
|
||||
import { VaultService } from '../server/security/vault.js';
|
||||
|
||||
function printUsage(): void {
|
||||
console.log(`GCoordinator CLI (gcoord)
|
||||
|
||||
Commands:
|
||||
rotate-key Re-encrypt vault.enc atomically with a new 32-byte master key
|
||||
--data-dir <path> Data directory (default: GCOORD_DATA_DIR or ./data)
|
||||
--current-key-file <path> Path to file containing current 32-byte key
|
||||
--new-key-out <path> Optional output file (0600) for generated key
|
||||
|
||||
Phase 5 commands (scheduled):
|
||||
backup | verify-backup | restore
|
||||
`);
|
||||
}
|
||||
|
||||
async function readCurrentKey(keyFile?: string): Promise<string> {
|
||||
if (keyFile) {
|
||||
return fs.readFileSync(keyFile, 'utf8').trim();
|
||||
}
|
||||
if (process.env.GCOORD_MASTER_KEY?.trim()) {
|
||||
return process.env.GCOORD_MASTER_KEY.trim();
|
||||
}
|
||||
if (fs.existsSync('/run/secrets/gcoord_master_key')) {
|
||||
return fs.readFileSync('/run/secrets/gcoord_master_key', 'utf8').trim();
|
||||
}
|
||||
const rl = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stderr,
|
||||
});
|
||||
try {
|
||||
const answer = await rl.question('Enter current 32-byte vault master key (hex or base64): ');
|
||||
return answer.trim();
|
||||
} finally {
|
||||
rl.close();
|
||||
}
|
||||
}
|
||||
|
||||
async function runRotateKey(args: string[]): Promise<number> {
|
||||
const parsed = parseArgs({
|
||||
args,
|
||||
options: {
|
||||
'data-dir': { type: 'string' },
|
||||
'current-key-file': { type: 'string' },
|
||||
'new-key-out': { type: 'string' },
|
||||
},
|
||||
allowPositionals: false,
|
||||
});
|
||||
|
||||
const dataDir = path.resolve(
|
||||
parsed.values['data-dir'] ?? process.env.GCOORD_DATA_DIR ?? './data',
|
||||
);
|
||||
const vaultPath = path.join(dataDir, 'vault.enc');
|
||||
if (!fs.existsSync(vaultPath)) {
|
||||
console.error(`Error: Vault file not found at ${vaultPath}`);
|
||||
return 1;
|
||||
}
|
||||
|
||||
const currentKey = await readCurrentKey(parsed.values['current-key-file']);
|
||||
const vault = new VaultService(vaultPath);
|
||||
vault.unlock(currentKey);
|
||||
const { generatedKeyHex } = vault.rotateMasterKey();
|
||||
|
||||
if (!generatedKeyHex) {
|
||||
console.error('Error: Unexpected empty rotated key');
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (parsed.values['new-key-out']) {
|
||||
const outPath = path.resolve(parsed.values['new-key-out']);
|
||||
fs.mkdirSync(path.dirname(outPath), { recursive: true });
|
||||
fs.writeFileSync(outPath, `${generatedKeyHex}\n`, { mode: 0o600 });
|
||||
fs.chmodSync(outPath, 0o600);
|
||||
console.log(`Vault master key rotated cleanly. New key written to ${outPath} (mode 0600).`);
|
||||
} else {
|
||||
console.log('Vault master key rotated cleanly. Store this new key securely:');
|
||||
console.log(generatedKeyHex);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
async function main(): Promise<void> {
|
||||
const [command, ...rest] = process.argv.slice(2);
|
||||
if (!command || command === '-h' || command === '--help') {
|
||||
printUsage();
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
if (command === 'rotate-key') {
|
||||
process.exit(await runRotateKey(rest));
|
||||
}
|
||||
|
||||
if (command === 'backup' || command === 'verify-backup' || command === 'restore') {
|
||||
console.error(`Command "${command}" is scheduled for Phase 5 (.gcoord.enc portable backup).`);
|
||||
process.exit(2);
|
||||
}
|
||||
|
||||
console.error(`Unknown command: ${command}`);
|
||||
printUsage();
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
main().catch((err) => {
|
||||
console.error(err instanceof Error ? err.message : String(err));
|
||||
process.exit(1);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue