0993a4d3bb
Four things that each made keyman quietly less useful than it looked. **Clipboard.** `pbcopy` was spawned unconditionally, with a comment admitting it. Copy is now a list of commands per platform — pbcopy, clip, and wl-copy / xclip / xsel tried in order on everything else, because there is no single answer under Linux and trying them beats detecting the session type. Only an absent tool advances to the next candidate: one that ran and refused has an opinion. And if nothing is installed the key is printed, since "give me this public key" is answerable without a clipboard and used to be a dead end everywhere but macOS. Verified the round trip through real pbcopy/pbpaste. **Home directories.** `/home/<user>` was hardcoded — wrong on the platform this was written on. A named user is now looked for beside the current user's home first, which is right wherever homes live together whatever that directory is called, then in /home and /Users, and the failure names every path tried instead of feeding a nonexistent one to readdir. For the current user, `HOME` still wins, with `os.userInfo()` behind it: `process.env.HOME || ''` made an unset HOME fatal, which it is not in a cron job or a container. **Keys that are not named id_*.** A key called `deploy_ed25519` was absent from every menu with nothing said. It still is — the vault stores `<name minus id_>/id_<name>.age` and decrypt rebuilds the filename from the directory, so relaxing discovery means changing the on-disk layout, which the plan sizes as its largest single item and is not folded in here. What it does do is say so: any file whose first line carries a private key header and whose name lacks the prefix is now reported, per directory, with the reason. A bounded 64-byte read, because classifying a key is no reason to load one. **Plaintext hygiene.** A "Clear decrypted keys" entry, defaulting to no and listing what it would delete first, and a vault `.gitignore` written on first run covering the age identity and the tmp directory — which the README asked the user to do by hand. Never overwritten, and silent about a configured directory that sits outside the vault, since a .gitignore cannot speak for a path above itself and pretending otherwise reads as protection that is absent.
87 lines
2.8 KiB
TypeScript
87 lines
2.8 KiB
TypeScript
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import inquirer from 'inquirer';
|
|
import { scanPrivateKeys } from './keyman.keys.js';
|
|
|
|
/**
|
|
* Writes a `.gitignore` beside the vault, once.
|
|
*
|
|
* The README told the user to do this by hand. A vault holds the age identity and,
|
|
* whenever anything has been decrypted, plaintext private keys — committing it is
|
|
* the exact failure the tool exists to prevent, and it is one file to prevent it.
|
|
*
|
|
* Never overwritten: an existing file may say more than this one does.
|
|
*/
|
|
export function writeVaultGitignore(vaultRoot: string, tmpDir: string, keyPath: string) {
|
|
const gitignore = path.join(vaultRoot, '.gitignore');
|
|
if (fs.existsSync(gitignore)) {
|
|
return;
|
|
}
|
|
|
|
// Both are configurable and may be absolute, so either can sit outside the vault.
|
|
// A .gitignore cannot speak about a path above itself, and claiming to would be
|
|
// worse than saying nothing.
|
|
const inside = (target: string) => {
|
|
const relative = path.relative(vaultRoot, target);
|
|
return relative && !relative.startsWith('..') && !path.isAbsolute(relative) ? relative : null;
|
|
};
|
|
|
|
const tmp = inside(tmpDir);
|
|
const key = inside(keyPath);
|
|
|
|
const lines = [
|
|
'# Written by keyman. The encrypted keys under the keys directory are safe to',
|
|
'# commit; nothing else here is.',
|
|
...(key ? [key, `${key}.pub`] : []),
|
|
...(tmp ? [`${tmp}/`] : []),
|
|
'',
|
|
];
|
|
|
|
fs.writeFileSync(gitignore, lines.join('\n'), { mode: 0o600 });
|
|
}
|
|
|
|
/**
|
|
* Deletes the decrypted keys in the vault's tmp directory.
|
|
*
|
|
* The counterpart to `decrypt`, which had none: a plaintext private key stayed
|
|
* there until someone remembered it, and "someone remembered" is not a security
|
|
* control. Only the key pairs are removed — anything else in the directory is not
|
|
* keyman's to delete.
|
|
*/
|
|
export async function clearDecryptedKeys(tmpDir: string) {
|
|
const { keys } = scanPrivateKeys(tmpDir);
|
|
|
|
if (keys.length === 0) {
|
|
console.log(`✅ Nothing decrypted in ${tmpDir}.`);
|
|
return;
|
|
}
|
|
|
|
console.log(`\n🔓 Decrypted keys in ${tmpDir}:`);
|
|
for (const key of keys) {
|
|
console.log(` ${key}`);
|
|
}
|
|
|
|
const { confirmed } = await inquirer.prompt<{ confirmed: boolean }>([
|
|
{
|
|
type: 'confirm',
|
|
name: 'confirmed',
|
|
message: `Delete ${keys.length === 1 ? 'this key' : `these ${keys.length} keys`}?`,
|
|
// A key that exists only here — generated and not yet deployed — is gone for
|
|
// good, so this is not a question to answer by pressing return.
|
|
default: false,
|
|
},
|
|
]);
|
|
|
|
if (!confirmed) {
|
|
console.log('⏭️ Nothing was deleted.');
|
|
return;
|
|
}
|
|
|
|
for (const key of keys) {
|
|
for (const file of [key, `${key}.pub`]) {
|
|
fs.rmSync(path.join(tmpDir, file), { force: true });
|
|
}
|
|
console.log(`🧹 Removed ${key}`);
|
|
}
|
|
}
|