mirror of
https://github.com/profullstack/logicsrc.git
synced 2026-12-01 01:14:47 +00:00
Some checks are pending
CI / build (push) Waiting to run
Deploy to dev2 / deploy (push) Waiting to run
test / test (push) Waiting to run
`logicsrc vault` (OpenCreds) lived only in ~/.config/logicsrc/opencreds. Lose the machine and the vault went with it, and a second machine had no way to get it. It now syncs to the logged-in account. Server (apps/pwa, /api/opencreds, session or lsk_ bearer): - opencreds_vaults: one per user. meta (key material already wrapped under the master password) + an encrypted folder list, each with a revision. - opencreds_items: one row per item, as the spec asks; envelope NULL is a tombstone so purges propagate; seq for incremental pulls. - Every write names its base revision; a stale one gets 409 with the current row. Writes are conditional and read back (each envelope's random IV identifies our write), so this needs nothing dialect-specific from SQLite or Postgres. - Stores ciphertext only. The server learns item count and type codes, as OpenCreds security.md already accepts. Client (@logicsrc/opencreds sync.ts, key-free except where noted): - Pull before every vault command, push after. Offline, the command still works and the change goes up next time. - Conflicts never lose data: the account's edit keeps the id and this machine's becomes "<name> (conflict copy)" (needs the unlocked key, so it waits otherwise). An edit beats a purge. Byte-identical envelopes are adopted, not split, so a lost sync.json is harmless. - Two different vaults (meta.createdAt differs) are never merged: refused, with `vault sync --use-remote` (backs this machine's up to .bak-NNN) or `--use-local`. - Remote item ids must be plain ids; anything else is ignored and never becomes a path. - Folder names are AES-GCM encrypted under the user key before upload. CLI (0.6.0): `logicsrc vault sync [--status|--use-remote|--use-local]`. Only the default vault syncs, and only when logged in; a --home or OPENCREDS_HOME vault stays local unless LOGICSRC_VAULT_SYNC=on; LOGICSRC_VAULT_SYNC=off disables it. The standalone `opencreds` binary gets no remote and never syncs. `init` on a machine that just downloaded the account's vault says to unlock it instead of suggesting --force. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1195 lines
49 KiB
TypeScript
1195 lines
49 KiB
TypeScript
/**
|
|
* The OpenCreds CLI, registered onto a commander parent.
|
|
*
|
|
* These commands ship twice — as `logicsrc creds …` and as the standalone
|
|
* `opencreds` binary — from this one implementation, because the specification
|
|
* treats CLI behaviour (flags, output shapes, exit codes) as a conformance
|
|
* surface and a subcommand that quietly diverged would make the two different
|
|
* contracts.
|
|
*/
|
|
|
|
import { mkdirSync, readFileSync, writeFileSync, chmodSync, existsSync, renameSync } from "node:fs";
|
|
import { dirname, join } from "node:path";
|
|
import type { Command } from "commander";
|
|
|
|
import { auditEvent } from "./audit.js";
|
|
import { emitFixtures, formatReport, runConformance } from "./conformance.js";
|
|
import {
|
|
DATABASE_EXTENSION,
|
|
buildManifest,
|
|
exportDatabase,
|
|
exportPlaintextDatabase,
|
|
mergePayload,
|
|
openDatabase,
|
|
parseDatabase,
|
|
readHeader,
|
|
} from "./database.js";
|
|
import { categorizeItem, parseCategories, toSimpleCsv } from "./categories.js";
|
|
import { CSV_LOSSY_FIELDS, toBitwardenCsv } from "./importers.js";
|
|
import {
|
|
isKnownSource,
|
|
routeImport,
|
|
sourceHelp,
|
|
SOURCE_NAMES,
|
|
} from "./import-router.js";
|
|
import {
|
|
createItem,
|
|
decryptItems,
|
|
encryptItem,
|
|
isItemType,
|
|
maskItem,
|
|
readField,
|
|
recordPasswordChange,
|
|
updateItem,
|
|
} from "./items.js";
|
|
import { confirm, promptNewSecret, promptSecret, resolveSecretFlag } from "./prompt.js";
|
|
import { createVaultStore, opencredsHome } from "./store.js";
|
|
import { SESSION_ENV, clearSession, encodeSession, persistSession, readSession } from "./session.js";
|
|
import {
|
|
ITEM_TYPE,
|
|
ITEM_TYPE_NAMES,
|
|
OPENCREDS_VERSION,
|
|
type DatabasePayload,
|
|
type Item,
|
|
type ItemTypeName,
|
|
type MergeStrategy,
|
|
} from "./types.js";
|
|
import { createVault, resetRecoveryKey, rewrapUserKey, unlockVault, unlockWithRecoveryKey } from "./vault-key.js";
|
|
import { formatDiagnostics, hasErrors, validateDocument } from "./validate.js";
|
|
import { DivergedVaultError, describeSync, pullVault, pushVault, readSyncState, syncVault, type SyncRemote } from "./sync.js";
|
|
import type { VaultStore } from "./store.js";
|
|
|
|
/** Exit codes are part of the contract; see docs/opencreds/cli.md. */
|
|
export const EXIT = {
|
|
OK: 0,
|
|
USAGE: 1,
|
|
VALIDATION: 2,
|
|
CRYPTO: 3,
|
|
REFUSED: 4,
|
|
} as const;
|
|
|
|
class CliError extends Error {
|
|
constructor(
|
|
message: string,
|
|
readonly code: number,
|
|
) {
|
|
super(message);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* The command as the person typed it — `opencreds` standalone, `logicsrc vault`
|
|
* when mounted — so every hint and example is one they can paste as is.
|
|
*/
|
|
let cli = "opencreds";
|
|
|
|
function fail(message: string, code: number): never {
|
|
throw new CliError(message, code);
|
|
}
|
|
|
|
/** Run a command body, mapping a thrown CliError onto its exit code. */
|
|
async function run(body: () => Promise<void>): Promise<void> {
|
|
try {
|
|
await body();
|
|
} catch (err) {
|
|
const code = err instanceof CliError ? err.code : EXIT.USAGE;
|
|
process.stderr.write(`${(err as Error).message}\n`);
|
|
process.exitCode = code;
|
|
}
|
|
}
|
|
|
|
interface GlobalOptions {
|
|
home?: string;
|
|
}
|
|
|
|
function storeFor(command: Command) {
|
|
const opts = command.optsWithGlobals<GlobalOptions>();
|
|
return createVaultStore(opts.home ?? opencredsHome());
|
|
}
|
|
|
|
function requireMeta(store: ReturnType<typeof createVaultStore>) {
|
|
const meta = store.readMeta();
|
|
if (!meta) fail(`No vault at ${store.baseDir} — create one with \`${cli} init\``, EXIT.USAGE);
|
|
return meta;
|
|
}
|
|
|
|
/**
|
|
* The user key for this invocation.
|
|
*
|
|
* A live session is used when there is one; otherwise the master password is
|
|
* asked for. Nothing else unlocks a vault.
|
|
*/
|
|
/**
|
|
* The user key this process unlocked, if any. Sync reads it after a command
|
|
* runs, to encrypt the folder list and to split a conflicting edit; it is
|
|
* never written anywhere by this module.
|
|
*/
|
|
let unlockedKey: Uint8Array | undefined;
|
|
|
|
async function unlock(store: ReturnType<typeof createVaultStore>): Promise<Uint8Array> {
|
|
const meta = requireMeta(store);
|
|
const session = readSession(store.baseDir);
|
|
if (session) return (unlockedKey = session);
|
|
const password = await promptSecret("Master password: ");
|
|
try {
|
|
return (unlockedKey = await unlockVault(meta, password));
|
|
} catch (err) {
|
|
store.appendAudit(
|
|
auditEvent({ action: "vault.unlock_failed", namespace: meta.namespace, profile: meta.profile, outcome: "failed" }),
|
|
);
|
|
fail((err as Error).message, EXIT.CRYPTO);
|
|
}
|
|
}
|
|
|
|
async function loadPayload(store: ReturnType<typeof createVaultStore>, userKey: Uint8Array): Promise<DatabasePayload> {
|
|
const meta = requireMeta(store);
|
|
const { items, failed } = await decryptItems(userKey, store.listEnvelopes(), meta.namespace);
|
|
if (failed.length > 0) {
|
|
// Report and continue: a single corrupt row must not hide the rest of a vault.
|
|
for (const failure of failed) {
|
|
process.stderr.write(`warning: could not decrypt ${failure.id} — ${failure.error}\n`);
|
|
}
|
|
}
|
|
return { folders: store.readFolders(), items };
|
|
}
|
|
|
|
async function saveItem(
|
|
store: ReturnType<typeof createVaultStore>,
|
|
userKey: Uint8Array,
|
|
item: Item,
|
|
): Promise<void> {
|
|
const meta = requireMeta(store);
|
|
store.writeEnvelope(await encryptItem(userKey, item, meta.namespace));
|
|
}
|
|
|
|
/** Find an item by exact id, then by exact name, then by unique prefix. */
|
|
function resolveItem(items: Item[], needle: string): Item {
|
|
const byId = items.find((item) => item.id === needle);
|
|
if (byId) return byId;
|
|
const byName = items.filter((item) => item.name === needle);
|
|
if (byName.length === 1) return byName[0]!;
|
|
if (byName.length > 1) fail(`"${needle}" matches ${byName.length} items; use an id`, EXIT.USAGE);
|
|
const byPrefix = items.filter((item) => item.id.startsWith(needle));
|
|
if (byPrefix.length === 1) return byPrefix[0]!;
|
|
if (byPrefix.length > 1) fail(`"${needle}" matches ${byPrefix.length} items; use a longer id`, EXIT.USAGE);
|
|
return fail(`No item matches "${needle}"`, EXIT.USAGE);
|
|
}
|
|
|
|
/** Type flags, kebab-cased from the field-group names. */
|
|
const TYPE_FLAGS: Record<ItemTypeName, Array<[flag: string, field: string, secret?: boolean]>> = {
|
|
login: [
|
|
["--username <value>", "username"],
|
|
["--password <value>", "password", true],
|
|
["--totp <value>", "totp", true],
|
|
],
|
|
card: [
|
|
["--cardholder-name <value>", "cardholderName"],
|
|
["--brand <value>", "brand"],
|
|
["--number <value>", "number", true],
|
|
["--exp-month <value>", "expMonth"],
|
|
["--exp-year <value>", "expYear"],
|
|
["--code <value>", "code", true],
|
|
],
|
|
identity: [
|
|
["--title <value>", "title"],
|
|
["--first-name <value>", "firstName"],
|
|
["--middle-name <value>", "middleName"],
|
|
["--last-name <value>", "lastName"],
|
|
["--company <value>", "company"],
|
|
["--email <value>", "email"],
|
|
["--phone <value>", "phone"],
|
|
["--address1 <value>", "address1"],
|
|
["--address2 <value>", "address2"],
|
|
["--city <value>", "city"],
|
|
["--state <value>", "state"],
|
|
["--postal-code <value>", "postalCode"],
|
|
["--country <value>", "country"],
|
|
["--ssn <value>", "ssn", true],
|
|
["--passport-number <value>", "passportNumber", true],
|
|
["--license-number <value>", "licenseNumber", true],
|
|
],
|
|
note: [],
|
|
key: [
|
|
["--key-type <value>", "keyType"],
|
|
["--algorithm <value>", "algorithm"],
|
|
["--public-key <value>", "publicKey"],
|
|
["--private-key <value>", "privateKey", true],
|
|
["--passphrase <value>", "passphrase", true],
|
|
["--fingerprint <value>", "fingerprint"],
|
|
["--value <value>", "value", true],
|
|
["--path <value>", "path"],
|
|
["--mode <value>", "mode"],
|
|
],
|
|
account: [
|
|
["--provider <value>", "provider"],
|
|
["--account-id <value>", "accountId"],
|
|
["--handle <value>", "handle"],
|
|
["--email <value>", "email"],
|
|
["--access-token <value>", "accessToken", true],
|
|
["--refresh-token <value>", "refreshToken", true],
|
|
["--token-type <value>", "tokenType"],
|
|
["--environment <value>", "environment"],
|
|
],
|
|
};
|
|
|
|
function optionKey(flag: string): string {
|
|
const long = flag.split(" ")[0]!.replace(/^--/, "");
|
|
return long.replace(/-([a-z])/g, (_, c: string) => c.toUpperCase());
|
|
}
|
|
|
|
/** Build the field group from the parsed flags, resolving any `-` from stdin. */
|
|
async function groupFromOptions(type: ItemTypeName, opts: Record<string, unknown>): Promise<Record<string, unknown>> {
|
|
const group: Record<string, unknown> = {};
|
|
for (const [flag, field] of TYPE_FLAGS[type]) {
|
|
const raw = opts[optionKey(flag)];
|
|
if (raw === undefined) continue;
|
|
const value = await resolveSecretFlag(String(raw));
|
|
if (value !== undefined) group[field] = value;
|
|
}
|
|
if (type === "login" && typeof opts.url === "string") {
|
|
group.uris = [{ uri: opts.url, match: (opts.match as string) ?? "domain" }];
|
|
}
|
|
if (type === "account" && Array.isArray(opts.scope)) {
|
|
group.scopes = opts.scope as string[];
|
|
}
|
|
if (type === "key" && typeof opts.file === "string") {
|
|
// Reading a key from a file is the common path; it avoids a multi-line
|
|
// secret in an argument vector entirely.
|
|
const body = readFileSync(opts.file, "utf8");
|
|
group.privateKey ??= body;
|
|
group.path ??= opts.file;
|
|
}
|
|
return group;
|
|
}
|
|
|
|
function applyTypeFlags(command: Command, type: ItemTypeName): Command {
|
|
for (const [flag] of TYPE_FLAGS[type]) {
|
|
command.option(flag, undefined);
|
|
}
|
|
if (type === "login") {
|
|
command.option("--url <value>", "matching URI");
|
|
command.option("--match <rule>", "URI match rule", "domain");
|
|
}
|
|
if (type === "account") {
|
|
command.option("--scope <value...>", "OAuth scope (repeatable)");
|
|
}
|
|
if (type === "key") {
|
|
command.option("--file <path>", "read the private key from a file");
|
|
}
|
|
return command;
|
|
}
|
|
|
|
function categoriesOrFail(input: string | undefined): Set<string> | undefined {
|
|
try {
|
|
return parseCategories(input);
|
|
} catch (error) {
|
|
return fail((error as Error).message, EXIT.USAGE);
|
|
}
|
|
}
|
|
|
|
function printItemLine(item: Item): string {
|
|
const type = item.type.padEnd(8);
|
|
const id = item.id.slice(0, 8);
|
|
const category = categorizeItem(item).padEnd(9);
|
|
return `${id} ${type} ${category} ${item.name}`;
|
|
}
|
|
|
|
/** Register every OpenCreds command onto `parent`. */
|
|
export interface RegisterOptions {
|
|
/**
|
|
* Where this vault syncs to, if anywhere. Called per command with the store
|
|
* the command will use; return undefined to stay local. The standalone
|
|
* `opencreds` binary passes nothing, so it never syncs.
|
|
*/
|
|
remote?: (store: VaultStore) => SyncRemote | undefined;
|
|
}
|
|
|
|
// Commands that never touch a vault on disk, plus `sync`, which syncs itself.
|
|
const NO_AUTOSYNC = new Set(["validate", "conformance", "manifest", "sync"]);
|
|
|
|
export function registerCredsCommands(parent: Command, options: RegisterOptions = {}): void {
|
|
const names: string[] = [];
|
|
for (let c: Command | null = parent; c; c = c.parent) names.unshift(c.name());
|
|
cli = names.join(" ");
|
|
const examples = (lines: string) => `\nExamples:\n${lines.replace(/^\n/, "").replace(/\$CLI/g, cli)}\n`;
|
|
parent.option("--home <dir>", "vault directory (default $OPENCREDS_HOME)");
|
|
|
|
if (options.remote) registerSync(parent, options.remote, examples);
|
|
|
|
// ---------------------------------------------------------------- vault ---
|
|
|
|
parent
|
|
.command("init")
|
|
.description("create a vault")
|
|
.addHelpText("after", examples(`
|
|
$CLI init create your vault (asks for a master password)
|
|
$CLI status is there a vault, is it unlocked, what is in it`))
|
|
.option("--namespace <name>", "domain-separation namespace", "opencreds")
|
|
.option("--iterations <n>", "PBKDF2 iterations", (v: string) => Number.parseInt(v, 10))
|
|
.option("--password-stdin", "read the master password from stdin instead of prompting twice")
|
|
.option("--force", "replace an existing vault")
|
|
.action(async function (
|
|
this: Command,
|
|
opts: { namespace: string; iterations?: number; force?: boolean; passwordStdin?: boolean },
|
|
) {
|
|
await run(async () => {
|
|
const store = storeFor(this);
|
|
if (store.exists() && !opts.force) {
|
|
if (readSyncState(store).vaultCreatedAt === store.readMeta()?.createdAt) {
|
|
fail(
|
|
`This machine already has your vault, synced from your account. Unlock it with your master password: \`${cli} unlock\`. ` +
|
|
"(--force would start a separate, empty vault.)",
|
|
EXIT.REFUSED,
|
|
);
|
|
}
|
|
fail(`A vault already exists at ${store.baseDir}; pass --force to replace it`, EXIT.REFUSED);
|
|
}
|
|
// Scripted provisioning reads one line and skips the confirmation; a
|
|
// person gets asked twice, because a typo'd master password is an
|
|
// empty vault they cannot open.
|
|
const password = opts.passwordStdin
|
|
? ((await resolveSecretFlag("-")) as string)
|
|
: await promptNewSecret("Master password: ", "Repeat master password: ");
|
|
if (password.length === 0) fail("A master password is required", EXIT.USAGE);
|
|
const { meta, recoveryKey } = await createVault(password, {
|
|
namespace: opts.namespace,
|
|
...(opts.iterations ? { params: { kdf: "pbkdf2-sha256" as const, iterations: opts.iterations } } : {}),
|
|
});
|
|
store.writeMeta(meta);
|
|
store.appendAudit(auditEvent({ action: "vault.create", namespace: meta.namespace, profile: meta.profile }));
|
|
|
|
process.stdout.write(`Vault created at ${store.baseDir}\n\n`);
|
|
process.stdout.write(` Recovery key ${recoveryKey}\n\n`);
|
|
process.stdout.write(
|
|
"Write this down now. It is the only way back into the vault without the\n" +
|
|
"master password, it is not stored anywhere, and it will not be shown again.\n",
|
|
);
|
|
});
|
|
});
|
|
|
|
parent
|
|
.command("unlock")
|
|
.description("start a session")
|
|
.addHelpText("after", examples(`
|
|
eval "$($CLI unlock)" unlock for this shell only (nothing written to disk)
|
|
$CLI unlock --persist --timeout 30 unlock for scripts for 30 minutes; \`lock\` ends it`))
|
|
.option("--persist", "write the session to a 0600 file instead of printing a token")
|
|
.option("--password-stdin", "read the master password from stdin")
|
|
.option("--timeout <minutes>", "session lifetime when persisted", (v: string) => Number.parseInt(v, 10), 15)
|
|
.action(async function (this: Command, opts: { persist?: boolean; timeout: number; passwordStdin?: boolean }) {
|
|
await run(async () => {
|
|
const store = storeFor(this);
|
|
const meta = requireMeta(store);
|
|
const password = opts.passwordStdin
|
|
? ((await resolveSecretFlag("-")) as string)
|
|
: await promptSecret("Master password: ");
|
|
let userKey: Uint8Array;
|
|
try {
|
|
userKey = await unlockVault(meta, password);
|
|
} catch (err) {
|
|
store.appendAudit(auditEvent({ action: "vault.unlock_failed", outcome: "failed" }));
|
|
fail((err as Error).message, EXIT.CRYPTO);
|
|
}
|
|
store.appendAudit(auditEvent({ action: "vault.unlock", namespace: meta.namespace, profile: meta.profile }));
|
|
|
|
if (opts.persist) {
|
|
const path = persistSession(userKey, opts.timeout, store.baseDir);
|
|
process.stdout.write(`Session written to ${path}, expiring in ${opts.timeout} minutes.\n`);
|
|
process.stdout.write(
|
|
"That file holds the key to this vault. Anything that can read it can read\n" +
|
|
"every item. Run `" + cli + " lock` when you are done.\n",
|
|
);
|
|
return;
|
|
}
|
|
|
|
process.stdout.write(`export ${SESSION_ENV}="${encodeSession(userKey)}"\n`);
|
|
});
|
|
});
|
|
|
|
parent
|
|
.command("lock")
|
|
.description("end a persisted session")
|
|
.action(async function (this: Command) {
|
|
await run(async () => {
|
|
const store = storeFor(this);
|
|
const removed = clearSession(store.baseDir);
|
|
process.stdout.write(
|
|
removed
|
|
? "Session cleared.\n"
|
|
: `No persisted session. If you exported ${SESSION_ENV}, unset it.\n`,
|
|
);
|
|
});
|
|
});
|
|
|
|
parent
|
|
.command("status")
|
|
.description("vault presence, lock state and item counts (works locked)")
|
|
.option("--json", "machine-readable output")
|
|
.action(async function (this: Command, opts: { json?: boolean }) {
|
|
await run(async () => {
|
|
const store = storeFor(this);
|
|
const meta = store.readMeta();
|
|
const envelopes = store.listEnvelopes();
|
|
const counts: Partial<Record<ItemTypeName, number>> = {};
|
|
for (const envelope of envelopes) {
|
|
const name = ITEM_TYPE_NAMES.find((n) => ITEM_TYPE[n] === envelope.type);
|
|
if (name) counts[name] = (counts[name] ?? 0) + 1;
|
|
}
|
|
const unlocked = Boolean(readSession(store.baseDir));
|
|
|
|
const report = {
|
|
vault: store.baseDir,
|
|
present: Boolean(meta),
|
|
unlocked,
|
|
opencreds: OPENCREDS_VERSION,
|
|
namespace: meta?.namespace,
|
|
profile: meta?.profile,
|
|
kdf: meta ? `${meta.kdf}/${meta.kdfIterations}` : undefined,
|
|
itemCount: envelopes.length,
|
|
types: counts,
|
|
};
|
|
|
|
if (opts.json) {
|
|
process.stdout.write(`${JSON.stringify(report, null, 2)}\n`);
|
|
return;
|
|
}
|
|
if (!meta) {
|
|
process.stdout.write(`No vault at ${store.baseDir}\n`);
|
|
return;
|
|
}
|
|
process.stdout.write(` Vault ${store.baseDir}\n`);
|
|
process.stdout.write(` State ${unlocked ? "unlocked" : "locked"}\n`);
|
|
process.stdout.write(` Namespace ${meta.namespace} (${meta.profile} profile)\n`);
|
|
process.stdout.write(` KDF ${meta.kdf}, ${meta.kdfIterations} iterations\n`);
|
|
process.stdout.write(` Items ${envelopes.length}\n`);
|
|
for (const name of ITEM_TYPE_NAMES) {
|
|
if (counts[name]) process.stdout.write(` ${name.padEnd(10)}${counts[name]}\n`);
|
|
}
|
|
});
|
|
});
|
|
|
|
parent
|
|
.command("recover")
|
|
.description("unlock with the recovery key and set a new master password")
|
|
.action(async function (this: Command) {
|
|
await run(async () => {
|
|
const store = storeFor(this);
|
|
const meta = requireMeta(store);
|
|
const recoveryKey = await promptSecret("Recovery key: ");
|
|
let userKey: Uint8Array;
|
|
try {
|
|
userKey = await unlockWithRecoveryKey(meta, recoveryKey);
|
|
} catch (err) {
|
|
fail((err as Error).message, EXIT.CRYPTO);
|
|
}
|
|
const password = await promptNewSecret("New master password: ", "Repeat: ");
|
|
const rewrapped = await rewrapUserKey(meta, userKey, password);
|
|
const reset = await resetRecoveryKey(rewrapped, userKey);
|
|
store.writeMeta(reset.meta);
|
|
store.appendAudit(auditEvent({ action: "vault.recovery_reset", namespace: meta.namespace }));
|
|
process.stdout.write(`Master password changed. Not one item was re-encrypted.\n\n`);
|
|
process.stdout.write(` New recovery key ${reset.recoveryKey}\n\n`);
|
|
process.stdout.write("The old recovery key no longer works.\n");
|
|
});
|
|
});
|
|
|
|
// ---------------------------------------------------------------- items ---
|
|
|
|
const add = parent
|
|
.command("add")
|
|
.description("add an item")
|
|
.addHelpText("after", examples(`
|
|
$CLI add login --name GitHub --username me --password - password read from stdin
|
|
$CLI add key --name DATABASE_URL --key-type env --value - one .env secret
|
|
$CLI add login --help every flag for one type`));
|
|
for (const type of ITEM_TYPE_NAMES) {
|
|
const sub = add
|
|
.command(type)
|
|
.description(`add a ${type}`)
|
|
.requiredOption("--name <value>", "display name")
|
|
.option("--notes <value>", "notes")
|
|
.option("--folder <name>", "folder name")
|
|
.option("--favorite", "mark as a favorite")
|
|
.option("--json", "print the created item as masked JSON");
|
|
applyTypeFlags(sub, type);
|
|
sub.action(async function (this: Command, opts: Record<string, unknown>) {
|
|
await run(async () => {
|
|
const store = storeFor(this);
|
|
const userKey = await unlock(store);
|
|
const group = await groupFromOptions(type, opts);
|
|
|
|
let folderId: string | null = null;
|
|
if (typeof opts.folder === "string" && opts.folder !== "") {
|
|
const folders = store.readFolders();
|
|
let folder = folders.find((f) => f.name === opts.folder);
|
|
if (!folder) {
|
|
folder = { id: globalThis.crypto.randomUUID(), name: opts.folder };
|
|
store.writeFolders([...folders, folder]);
|
|
}
|
|
folderId = folder.id;
|
|
}
|
|
|
|
const item = createItem(type, {
|
|
name: String(opts.name),
|
|
notes: typeof opts.notes === "string" ? opts.notes : "",
|
|
favorite: Boolean(opts.favorite),
|
|
folderId,
|
|
[type]: group,
|
|
} as Partial<Item>);
|
|
|
|
await saveItem(store, userKey, item);
|
|
store.appendAudit(auditEvent({ action: "item.create", itemId: item.id, itemType: type }));
|
|
|
|
if (opts.json) {
|
|
process.stdout.write(`${JSON.stringify(maskItem(item), null, 2)}\n`);
|
|
return;
|
|
}
|
|
process.stdout.write(`Added ${type} ${item.id}\n`);
|
|
});
|
|
});
|
|
}
|
|
|
|
parent
|
|
.command("list")
|
|
.description("list items; never prints secret values")
|
|
.addHelpText("after", examples(`
|
|
$CLI list everything (never shows values)
|
|
$CLI list --category db database credentials only
|
|
$CLI list -c social,api two categories at once
|
|
$CLI list --type login --search github`))
|
|
.option("--type <type>", "filter by item type")
|
|
.option("-c, --category <names>", "filter by category: db, social, server, api, … (comma-separated)")
|
|
.option("--folder <name>", "filter by folder")
|
|
.option("--search <text>", "match against the item name")
|
|
.option("--json", "machine-readable output, masked identically")
|
|
.action(async function (
|
|
this: Command,
|
|
opts: { type?: string; category?: string; folder?: string; search?: string; json?: boolean },
|
|
) {
|
|
await run(async () => {
|
|
const store = storeFor(this);
|
|
const userKey = await unlock(store);
|
|
const { items, folders } = await loadPayload(store, userKey);
|
|
|
|
if (opts.type && !isItemType(opts.type)) {
|
|
fail(`Unknown type "${opts.type}"; expected one of ${ITEM_TYPE_NAMES.join(", ")}`, EXIT.USAGE);
|
|
}
|
|
const folderId = opts.folder ? folders.find((f) => f.name === opts.folder)?.id : undefined;
|
|
if (opts.folder && !folderId) fail(`No folder named "${opts.folder}"`, EXIT.USAGE);
|
|
|
|
const categories = categoriesOrFail(opts.category);
|
|
const needle = opts.search?.toLowerCase();
|
|
const filtered = items
|
|
.filter((item) => (opts.type ? item.type === opts.type : true))
|
|
.filter((item) => (categories ? categories.has(categorizeItem(item)) : true))
|
|
.filter((item) => (folderId ? item.folderId === folderId : true))
|
|
.filter((item) => (needle ? item.name.toLowerCase().includes(needle) : true))
|
|
.sort((a, b) => a.name.localeCompare(b.name) || a.id.localeCompare(b.id));
|
|
|
|
if (opts.json) {
|
|
process.stdout.write(`${JSON.stringify(filtered.map((item) => maskItem(item)), null, 2)}\n`);
|
|
return;
|
|
}
|
|
if (filtered.length === 0) {
|
|
process.stdout.write("No matching items.\n");
|
|
return;
|
|
}
|
|
for (const item of filtered) process.stdout.write(`${printItemLine(item)}\n`);
|
|
});
|
|
});
|
|
|
|
parent
|
|
.command("get")
|
|
.argument("<needle>", "item id or name")
|
|
.description("show one item, with every secret masked")
|
|
.addHelpText("after", examples(`
|
|
$CLI get GitHub the item, secrets masked
|
|
$CLI get GitHub --field login.password --reveal one value, in the clear`))
|
|
.option("--field <path>", "a single dotted field path, e.g. login.password")
|
|
.option("--reveal", "print the value of --field in the clear")
|
|
.option("--json", "machine-readable output, masked identically")
|
|
.action(async function (this: Command, needle: string, opts: { field?: string; reveal?: boolean; json?: boolean }) {
|
|
await run(async () => {
|
|
const store = storeFor(this);
|
|
const userKey = await unlock(store);
|
|
const { items } = await loadPayload(store, userKey);
|
|
const item = resolveItem(items, needle);
|
|
|
|
if (opts.reveal) {
|
|
// Revealing is always a deliberate act naming a single value. There
|
|
// is no flag that prints a whole item in the clear, because there is
|
|
// no workflow that needs one.
|
|
if (!opts.field) fail("--reveal needs --field naming a single value", EXIT.USAGE);
|
|
const value = readField(item, opts.field);
|
|
if (value === undefined) fail(`No field "${opts.field}" on this item`, EXIT.USAGE);
|
|
process.stdout.write(`${value}\n`);
|
|
return;
|
|
}
|
|
|
|
const masked = maskItem(item);
|
|
if (opts.field) {
|
|
const value = readField(masked, opts.field);
|
|
if (value === undefined) fail(`No field "${opts.field}" on this item`, EXIT.USAGE);
|
|
process.stdout.write(`${value}\n`);
|
|
return;
|
|
}
|
|
process.stdout.write(`${JSON.stringify(masked, null, 2)}\n`);
|
|
});
|
|
});
|
|
|
|
const edit = parent.command("edit").description("edit an item");
|
|
for (const type of ITEM_TYPE_NAMES) {
|
|
const sub = edit
|
|
.command(type)
|
|
.argument("<needle>", "item id or name")
|
|
.description(`edit a ${type}`)
|
|
.option("--name <value>", "display name")
|
|
.option("--notes <value>", "notes")
|
|
.option("--favorite <bool>", "true or false");
|
|
applyTypeFlags(sub, type);
|
|
sub.action(async function (this: Command, needle: string, opts: Record<string, unknown>) {
|
|
await run(async () => {
|
|
const store = storeFor(this);
|
|
const userKey = await unlock(store);
|
|
const { items } = await loadPayload(store, userKey);
|
|
const item = resolveItem(items, needle);
|
|
if (item.type !== type) fail(`${item.id} is a ${item.type}, not a ${type}`, EXIT.USAGE);
|
|
|
|
const group = await groupFromOptions(type, opts);
|
|
const patch: Partial<Item> = {};
|
|
if (typeof opts.name === "string") patch.name = opts.name;
|
|
if (typeof opts.notes === "string") patch.notes = opts.notes;
|
|
if (opts.favorite !== undefined) patch.favorite = String(opts.favorite) === "true";
|
|
|
|
// A password change is recorded in the item's own history before the
|
|
// new value overwrites the old one — otherwise the value being replaced
|
|
// is the one that gets lost.
|
|
let next = item;
|
|
if (type === "login" && typeof group.password === "string" && group.password !== item.login?.password) {
|
|
next = recordPasswordChange(next, group.password);
|
|
delete group.password;
|
|
}
|
|
next = updateItem(next, { ...patch, [type]: group } as Partial<Item>);
|
|
|
|
await saveItem(store, userKey, next);
|
|
store.appendAudit(auditEvent({ action: "item.update", itemId: next.id, itemType: type }));
|
|
process.stdout.write(`Updated ${next.id}\n`);
|
|
});
|
|
});
|
|
}
|
|
|
|
parent
|
|
.command("rm")
|
|
.argument("<needle>", "item id or name")
|
|
.description("delete an item")
|
|
.option("--purge", "delete irrecoverably rather than moving to the trash")
|
|
.action(async function (this: Command, needle: string, opts: { purge?: boolean }) {
|
|
await run(async () => {
|
|
const store = storeFor(this);
|
|
const userKey = await unlock(store);
|
|
const { items } = await loadPayload(store, userKey);
|
|
const item = resolveItem(items, needle);
|
|
|
|
if (opts.purge) {
|
|
store.deleteEnvelope(item.id);
|
|
store.appendAudit(auditEvent({ action: "item.purge", itemId: item.id, itemType: item.type }));
|
|
process.stdout.write(`Purged ${item.id}\n`);
|
|
return;
|
|
}
|
|
|
|
const envelope = store.readEnvelope(item.id);
|
|
if (!envelope) fail(`No stored item ${item.id}`, EXIT.USAGE);
|
|
const now = new Date();
|
|
store.writeEnvelope({
|
|
...envelope,
|
|
deletedAt: now.toISOString(),
|
|
purgeAfter: new Date(now.getTime() + 30 * 86_400_000).toISOString(),
|
|
});
|
|
store.appendAudit(auditEvent({ action: "item.delete", itemId: item.id, itemType: item.type }));
|
|
process.stdout.write(`Moved ${item.id} to the trash; recoverable for 30 days\n`);
|
|
});
|
|
});
|
|
|
|
parent
|
|
.command("restore")
|
|
.argument("<id>", "item id")
|
|
.description("restore an item from the trash")
|
|
.action(async function (this: Command, id: string) {
|
|
await run(async () => {
|
|
const store = storeFor(this);
|
|
await unlock(store);
|
|
const envelope = store.readEnvelope(id);
|
|
if (!envelope) fail(`No item ${id}`, EXIT.USAGE);
|
|
store.writeEnvelope({ ...envelope, deletedAt: null, purgeAfter: null });
|
|
store.appendAudit(auditEvent({ action: "item.restore", itemId: id }));
|
|
process.stdout.write(`Restored ${id}\n`);
|
|
});
|
|
});
|
|
|
|
// ------------------------------------------------------------- database ---
|
|
|
|
parent
|
|
.command("export")
|
|
.description("export the vault as an OpenCreds database")
|
|
.addHelpText("after", examples(`
|
|
$CLI export --format csv --out vault.csv --yes everything, one flat row per item
|
|
$CLI export --format csv --category db --out db.csv --yes one category
|
|
$CLI export --out backup.opencreds encrypted backup (asks for a passphrase)`))
|
|
.option("--out <file>", "output file", `vault${DATABASE_EXTENSION}`)
|
|
.option("--passphrase-stdin", "read the export passphrase from stdin")
|
|
.option("--plaintext", "write every secret in the clear (requires --yes)")
|
|
.option("--format <format>", "opencreds, csv (one flat row per item) or bitwarden-csv", "opencreds")
|
|
.option("-c, --category <names>", "only items in these categories: db, social, server, api, …")
|
|
.option("--yes", "confirm a plaintext export")
|
|
.action(async function (
|
|
this: Command,
|
|
opts: { out: string; passphraseStdin?: boolean; plaintext?: boolean; format: string; category?: string; yes?: boolean },
|
|
) {
|
|
await run(async () => {
|
|
if (!["opencreds", "csv", "bitwarden-csv"].includes(opts.format)) {
|
|
fail(`Unknown format "${opts.format}"; expected opencreds, csv or bitwarden-csv`, EXIT.USAGE);
|
|
}
|
|
if (opts.format !== "opencreds" && this.getOptionValueSource("out") === "default") opts.out = "vault.csv";
|
|
const categories = categoriesOrFail(opts.category);
|
|
const store = storeFor(this);
|
|
const meta = requireMeta(store);
|
|
const userKey = await unlock(store);
|
|
const loaded = await loadPayload(store, userKey);
|
|
const payload = categories
|
|
? { ...loaded, items: loaded.items.filter((item) => categories.has(categorizeItem(item))) }
|
|
: loaded;
|
|
|
|
const wantsPlaintext = Boolean(opts.plaintext) || opts.format !== "opencreds";
|
|
|
|
if (wantsPlaintext) {
|
|
process.stdout.write(
|
|
`About to write ${payload.items.length} items to ${opts.out} with every secret in the clear.\n` +
|
|
"This file cannot be un-leaked, and every password in it should be treated\n" +
|
|
"as exposed if it is.\n",
|
|
);
|
|
if (!opts.yes && !(await confirm("Continue?"))) {
|
|
fail("Refused: a plaintext export needs --yes", EXIT.REFUSED);
|
|
}
|
|
}
|
|
|
|
if (opts.format === "csv") {
|
|
writeFileSync(opts.out, toSimpleCsv(payload.items, payload.folders), { encoding: "utf8", mode: 0o600 });
|
|
try {
|
|
chmodSync(opts.out, 0o600);
|
|
} catch {
|
|
/* no modes on this platform */
|
|
}
|
|
store.appendAudit(auditEvent({ action: "database.export_plaintext", itemCount: payload.items.length }));
|
|
process.stdout.write(`Wrote ${opts.out} — ${payload.items.length} items, every secret in the clear.\n`);
|
|
return;
|
|
}
|
|
|
|
if (opts.format === "bitwarden-csv") {
|
|
const { csv, dropped } = toBitwardenCsv(payload.items, payload.folders);
|
|
writeFileSync(opts.out, csv, { encoding: "utf8", mode: 0o600 });
|
|
try {
|
|
chmodSync(opts.out, 0o600);
|
|
} catch {
|
|
/* no modes on this platform */
|
|
}
|
|
store.appendAudit(
|
|
auditEvent({ action: "database.export_plaintext", itemCount: payload.items.length }),
|
|
);
|
|
process.stdout.write(`Wrote ${opts.out}\n`);
|
|
const droppedEntries = Object.entries(dropped);
|
|
if (droppedEntries.length > 0) {
|
|
process.stdout.write("\nNo CSV has a column for these, so they were not written:\n");
|
|
for (const [what, count] of droppedEntries) process.stdout.write(` ${String(count).padStart(4)} ${what}\n`);
|
|
process.stdout.write(`\nThe fields a CSV always loses: ${CSV_LOSSY_FIELDS.join(", ")}.\n`);
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (opts.plaintext) {
|
|
const db = await exportPlaintextDatabase(payload, {
|
|
namespace: meta.namespace,
|
|
acknowledged: true,
|
|
});
|
|
writeFileSync(opts.out, `${JSON.stringify(db, null, 2)}\n`, { encoding: "utf8", mode: 0o600 });
|
|
try {
|
|
chmodSync(opts.out, 0o600);
|
|
} catch {
|
|
/* no modes on this platform */
|
|
}
|
|
store.appendAudit(auditEvent({ action: "database.export_plaintext", itemCount: payload.items.length }));
|
|
process.stdout.write(`Wrote ${opts.out} — unprotected, ${payload.items.length} items.\n`);
|
|
return;
|
|
}
|
|
|
|
const passphrase = opts.passphraseStdin
|
|
? ((await resolveSecretFlag("-")) as string)
|
|
: await promptNewSecret("Export passphrase: ", "Repeat: ");
|
|
const db = await exportDatabase(payload, { namespace: meta.namespace, passphrase });
|
|
writeFileSync(opts.out, `${JSON.stringify(db, null, 2)}\n`, { encoding: "utf8", mode: 0o600 });
|
|
store.appendAudit(auditEvent({ action: "database.export", itemCount: payload.items.length }));
|
|
process.stdout.write(
|
|
`Wrote ${opts.out} — encrypted, ${payload.items.length} items, ${payload.folders.length} folders.\n`,
|
|
);
|
|
});
|
|
});
|
|
|
|
parent
|
|
.command("import")
|
|
.argument(
|
|
"<file>",
|
|
"an OpenCreds database, a Bitwarden JSON export, a 1Password .1pux, or a CSV export",
|
|
)
|
|
.description("import into the vault")
|
|
.addHelpText("after", examples(`
|
|
$CLI import bitwarden.csv --dry-run see what would be imported
|
|
$CLI import bitwarden.csv import it (Bitwarden, 1Password, Chrome, LastPass or KeePass CSV)
|
|
$CLI import backup.opencreds restore an OpenCreds export`))
|
|
.option("--dry-run", "report what would happen and write nothing")
|
|
.option("--merge <strategy>", "skip, replace or duplicate", "skip")
|
|
.option(
|
|
"--source <name>",
|
|
`where the export came from, when it cannot be told from the file (${SOURCE_NAMES.join(", ")})`,
|
|
)
|
|
.option("--passphrase-stdin", "read the database passphrase from stdin")
|
|
.option("--allow-unregistered-namespace", "open a database whose namespace is not registered")
|
|
.action(async function (
|
|
this: Command,
|
|
file: string,
|
|
opts: {
|
|
dryRun?: boolean;
|
|
merge: string;
|
|
source?: string;
|
|
passphraseStdin?: boolean;
|
|
allowUnregisteredNamespace?: boolean;
|
|
},
|
|
) {
|
|
await run(async () => {
|
|
const store = storeFor(this);
|
|
const meta = requireMeta(store);
|
|
const userKey = await unlock(store);
|
|
|
|
const strategy = opts.merge as MergeStrategy;
|
|
if (!["skip", "replace", "duplicate"].includes(strategy)) {
|
|
fail(`Unknown merge strategy "${opts.merge}"`, EXIT.USAGE);
|
|
}
|
|
|
|
if (opts.source && !isKnownSource(opts.source)) {
|
|
fail(
|
|
`Unknown --source "${opts.source}".\n Valid sources: ${SOURCE_NAMES.join(", ")}`,
|
|
EXIT.USAGE,
|
|
);
|
|
}
|
|
|
|
// Read as bytes: a .1pux is a ZIP, so decoding as UTF-8 up front would
|
|
// corrupt it before anything got the chance to look.
|
|
let buf: Buffer;
|
|
try {
|
|
buf = readFileSync(file);
|
|
} catch {
|
|
fail(`Could not read ${file}`, EXIT.USAGE);
|
|
}
|
|
const text = buf.toString("utf8");
|
|
|
|
let incoming: DatabasePayload;
|
|
let sourceLabel: string;
|
|
let skipped: Array<{ row: number; reason: string }> = [];
|
|
|
|
// One router decides which reader owns the file, so --source can name
|
|
// any product rather than only a CSV one.
|
|
const route = routeImport(buf, opts.source);
|
|
|
|
if (!route.isOpenCredsDatabase) {
|
|
const parsed = route.parsed;
|
|
if (!parsed || parsed.items.length === 0) {
|
|
const why = route.reason ?? parsed?.skipped[0]?.reason;
|
|
fail(
|
|
[
|
|
why && why !== "Unrecognised export format"
|
|
? `${why}`
|
|
: `Could not identify the export format of ${file}`,
|
|
"",
|
|
" Say where it came from with --source <name>:",
|
|
` ${sourceHelp()}`,
|
|
].join("\n"),
|
|
EXIT.VALIDATION,
|
|
);
|
|
}
|
|
incoming = { folders: parsed.folders, items: parsed.items };
|
|
skipped = parsed.skipped;
|
|
sourceLabel = parsed.source ?? "unknown";
|
|
process.stdout.write(` Source ${file} (${route.description})\n\n`);
|
|
} else {
|
|
const db = parseDatabase(text);
|
|
const header = readHeader(db);
|
|
process.stdout.write(
|
|
` Source ${file} (opencreds ${header.opencreds}, ` +
|
|
`${header.protected ? "encrypted" : "PLAINTEXT"}, namespace ${header.namespace})\n`,
|
|
);
|
|
if (header.generator) {
|
|
process.stdout.write(` Exported ${header.exportedAt} by ${header.generator.name} ${header.generator.version}\n`);
|
|
}
|
|
|
|
const passphrase = header.protected
|
|
? opts.passphraseStdin
|
|
? ((await resolveSecretFlag("-")) as string)
|
|
: await promptSecret("Database passphrase: ")
|
|
: undefined;
|
|
|
|
try {
|
|
incoming = await openDatabase(db, {
|
|
...(passphrase !== undefined ? { passphrase } : {}),
|
|
allowUnregisteredNamespace: opts.allowUnregisteredNamespace,
|
|
});
|
|
} catch (err) {
|
|
// A manifest mismatch writes nothing, regardless of flags.
|
|
fail((err as Error).message, EXIT.CRYPTO);
|
|
}
|
|
process.stdout.write(` Manifest verified — ${incoming.items.length} items, ${incoming.folders.length} folders\n\n`);
|
|
sourceLabel = "opencreds";
|
|
}
|
|
|
|
const existing = await loadPayload(store, userKey);
|
|
const merged = mergePayload(existing, incoming, strategy);
|
|
|
|
const counts: Partial<Record<ItemTypeName, number>> = {};
|
|
for (const item of incoming.items) counts[item.type] = (counts[item.type] ?? 0) + 1;
|
|
for (const name of ITEM_TYPE_NAMES) {
|
|
if (counts[name]) process.stdout.write(` ${name.padEnd(10)}${String(counts[name]).padStart(4)}\n`);
|
|
}
|
|
|
|
process.stdout.write(
|
|
`\n Folders ${merged.outcome.foldersAdded} new, ${merged.outcome.foldersMerged} merged\n`,
|
|
);
|
|
process.stdout.write(
|
|
` Outcome ${merged.outcome.added} added, ${merged.outcome.replaced} replaced, ` +
|
|
`${merged.outcome.duplicated} duplicated, ${merged.outcome.skipped} skipped (${strategy})\n`,
|
|
);
|
|
if (skipped.length > 0) {
|
|
process.stdout.write(` Skipped ${skipped.length} rows\n`);
|
|
for (const row of skipped.slice(0, 20)) {
|
|
process.stdout.write(` row ${row.row}: ${row.reason}\n`);
|
|
}
|
|
if (skipped.length > 20) process.stdout.write(` … and ${skipped.length - 20} more\n`);
|
|
}
|
|
|
|
if (opts.dryRun) {
|
|
process.stdout.write("\n Nothing written. Re-run without --dry-run to import.\n");
|
|
return;
|
|
}
|
|
|
|
store.writeFolders(merged.folders);
|
|
for (const item of merged.items) {
|
|
store.writeEnvelope(await encryptItem(userKey, item, meta.namespace));
|
|
}
|
|
store.appendAudit(auditEvent({ action: "database.import", itemCount: incoming.items.length }));
|
|
process.stdout.write(`\n Imported into ${store.baseDir}\n`);
|
|
});
|
|
});
|
|
|
|
// ------------------------------------------------------------ validate ----
|
|
|
|
parent
|
|
.command("validate")
|
|
.argument("[file]", "a database or item document; omit with --stdin")
|
|
.description("check that a document conforms")
|
|
.option("--stdin", "read the document from stdin")
|
|
.option("--json", "machine-readable diagnostics")
|
|
.action(async function (this: Command, file: string | undefined, opts: { stdin?: boolean; json?: boolean }) {
|
|
await run(async () => {
|
|
let text: string;
|
|
if (opts.stdin || !file) {
|
|
text = await resolveSecretFlag("-") as string;
|
|
} else {
|
|
try {
|
|
text = readFileSync(file, "utf8");
|
|
} catch {
|
|
fail(`Could not read ${file}`, EXIT.USAGE);
|
|
}
|
|
}
|
|
|
|
let parsed: unknown;
|
|
try {
|
|
parsed = JSON.parse(text);
|
|
} catch (err) {
|
|
fail(`Not valid JSON: ${(err as Error).message}`, EXIT.VALIDATION);
|
|
}
|
|
|
|
const { kind, diagnostics } = validateDocument(parsed);
|
|
const failed = hasErrors(diagnostics);
|
|
|
|
if (opts.json) {
|
|
process.stdout.write(`${JSON.stringify({ kind, conformant: !failed, diagnostics }, null, 2)}\n`);
|
|
} else {
|
|
// A warning is not a failure, so a document that only warns still
|
|
// gets told it conforms — otherwise a plaintext database, whose
|
|
// warning is the whole point of it, looks broken.
|
|
if (!failed) process.stdout.write(`OK — a conforming OpenCreds ${kind}\n`);
|
|
if (diagnostics.length > 0) process.stdout.write(`${formatDiagnostics(diagnostics)}\n`);
|
|
}
|
|
if (failed) process.exitCode = EXIT.VALIDATION;
|
|
});
|
|
});
|
|
|
|
parent
|
|
.command("conformance")
|
|
.description("run the OpenCreds conformance suite against this implementation")
|
|
.option("--json", "emit the conformance report as JSON")
|
|
.option("--emit-fixtures <dir>", "write the generated fixture set to a directory")
|
|
.action(async function (this: Command, opts: { json?: boolean; emitFixtures?: string }) {
|
|
await run(async () => {
|
|
if (opts.emitFixtures) {
|
|
const fixtures = await emitFixtures();
|
|
for (const [name, content] of Object.entries(fixtures)) {
|
|
const target = join(opts.emitFixtures, name);
|
|
mkdirSync(dirname(target), { recursive: true });
|
|
writeFileSync(
|
|
target,
|
|
typeof content === "string" ? content : `${JSON.stringify(content, null, 2)}\n`,
|
|
"utf8",
|
|
);
|
|
}
|
|
process.stdout.write(`Wrote ${Object.keys(fixtures).length} fixtures to ${opts.emitFixtures}\n`);
|
|
return;
|
|
}
|
|
|
|
const report = await runConformance();
|
|
process.stdout.write(
|
|
opts.json ? `${JSON.stringify(report, null, 2)}\n` : `${formatReport(report)}\n`,
|
|
);
|
|
// A failed MUST is a validation failure, not a crash.
|
|
if (!report.conformant) process.exitCode = EXIT.VALIDATION;
|
|
});
|
|
});
|
|
|
|
parent
|
|
.command("manifest")
|
|
.argument("<file>", "a plaintext database")
|
|
.description("recompute the manifest of a plaintext database")
|
|
.action(async function (this: Command, file: string) {
|
|
await run(async () => {
|
|
const db = parseDatabase(readFileSync(file, "utf8"));
|
|
if (db.protected) fail("Only a plaintext database can be re-manifested here", EXIT.USAGE);
|
|
const manifest = await buildManifest({ folders: db.folders ?? [], items: db.items ?? [] });
|
|
process.stdout.write(`${JSON.stringify(manifest, null, 2)}\n`);
|
|
});
|
|
});
|
|
}
|
|
|
|
// ----------------------------------------------------------------- sync ---
|
|
|
|
/**
|
|
* Autosync plus an explicit `sync` command, registered only when the host CLI
|
|
* provides a remote.
|
|
*
|
|
* Every vault command pulls first, so `list` on a second machine shows what
|
|
* the first one added, and pushes after, so a write is on the account before
|
|
* the prompt comes back. Sync never fails a command: offline, the vault works
|
|
* as before and the change goes up next time. Only `sync` itself reports
|
|
* failure as an exit code.
|
|
*/
|
|
function registerSync(
|
|
parent: Command,
|
|
remoteFor: (store: VaultStore) => SyncRemote | undefined,
|
|
examples: (lines: string) => string,
|
|
): void {
|
|
const say = (text: string) => {
|
|
if (text) process.stderr.write(`${text}\n`);
|
|
};
|
|
const warn = (err: unknown) => {
|
|
const message = (err as Error).message;
|
|
say(
|
|
err instanceof DivergedVaultError
|
|
? `vault sync: ${message}`
|
|
: `vault sync: skipped (${message}); changes stay on this machine and go up next time.`,
|
|
);
|
|
};
|
|
const autosync = (command: Command) => !NO_AUTOSYNC.has(command.name()) && process.env.OPENCREDS_SYNC !== "off";
|
|
|
|
parent.hook("preAction", async (_self, command) => {
|
|
if (!autosync(command)) return;
|
|
const store = storeFor(command);
|
|
const remote = remoteFor(store);
|
|
if (!remote) return;
|
|
try {
|
|
say(describeSync(await pullVault(store, remote), remote.label));
|
|
} catch (err) {
|
|
warn(err);
|
|
}
|
|
});
|
|
|
|
parent.hook("postAction", async (_self, command) => {
|
|
if (!autosync(command) || process.exitCode) return;
|
|
const store = storeFor(command);
|
|
const remote = remoteFor(store);
|
|
if (!remote || !store.exists()) return;
|
|
try {
|
|
say(describeSync(await pushVault(store, remote, unlockedKey), remote.label));
|
|
} catch (err) {
|
|
warn(err);
|
|
}
|
|
});
|
|
|
|
parent
|
|
.command("sync")
|
|
.description("sync this vault with your account now (it also happens around every command)")
|
|
.addHelpText("after", examples(`
|
|
$CLI sync pull then push, now
|
|
$CLI sync --status what is synced, what is waiting
|
|
$CLI sync --use-remote two different vaults: keep the account's (this machine's is backed up first)
|
|
$CLI sync --use-local two different vaults: replace the account's with this machine's
|
|
OPENCREDS_SYNC=off $CLI list one command without syncing`))
|
|
.option("--status", "show sync state and change nothing")
|
|
.option("--use-remote", "replace this machine's vault with the account's (backed up first)")
|
|
.option("--use-local", "replace the account's vault with this machine's")
|
|
.action(async function (this: Command, opts: { status?: boolean; useRemote?: boolean; useLocal?: boolean }) {
|
|
await run(async () => {
|
|
let store = storeFor(this);
|
|
const remote = remoteFor(store);
|
|
if (!remote) fail("This vault is not linked to an account. Log in first (logicsrc login).", EXIT.USAGE);
|
|
if (opts.useRemote && opts.useLocal) fail("Pick one of --use-remote and --use-local", EXIT.USAGE);
|
|
|
|
if (opts.status) {
|
|
const state = readSyncState(store);
|
|
const envelopes = store.listEnvelopes();
|
|
const here = new Set(envelopes.map((e) => e.id));
|
|
const waiting =
|
|
envelopes.filter((e) => e.revision !== state.items[e.id]).length +
|
|
Object.keys(state.items).filter((id) => !here.has(id)).length;
|
|
const rv = await remote.getVault();
|
|
process.stdout.write(
|
|
`Account ${remote.label}${rv ? "" : " (no vault there yet)"}\n` +
|
|
`Local ${store.exists() ? `${envelopes.length} item(s) at ${store.baseDir}` : `no vault at ${store.baseDir}`}\n` +
|
|
`Synced ${Object.keys(state.items).length} item(s)${state.lastSyncAt ? `, last ${state.lastSyncAt}` : ", never"}\n` +
|
|
`Waiting ${waiting} change(s) to push\n`,
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (opts.useRemote) {
|
|
if (!(await remote.getVault())) fail(`There is no vault on ${remote.label} to use.`, EXIT.USAGE);
|
|
if (store.exists()) {
|
|
let n = 1;
|
|
const backup = () => `${store.baseDir}.bak-${String(n).padStart(3, "0")}`;
|
|
while (existsSync(backup())) n++;
|
|
clearSession(store.baseDir);
|
|
renameSync(store.baseDir, backup());
|
|
process.stderr.write(`Moved this machine's vault to ${backup()}\n`);
|
|
}
|
|
store = createVaultStore(store.baseDir);
|
|
} else if (opts.useLocal) {
|
|
if (!store.exists()) fail(`No vault at ${store.baseDir} to upload.`, EXIT.USAGE);
|
|
const ok = await confirm(`Replace the vault on ${remote.label} with this machine's? Its items there are deleted.`);
|
|
if (!ok) fail("Nothing changed.", EXIT.REFUSED);
|
|
await remote.reset();
|
|
store.writeSyncState({});
|
|
}
|
|
|
|
const key = store.exists() ? (readSession(store.baseDir) ?? undefined) : undefined;
|
|
const report = await syncVault(store, remote, key);
|
|
process.stdout.write(`${describeSync(report, remote.label) || `vault sync (${remote.label}): up to date`}\n`);
|
|
if (!key && store.exists()) {
|
|
process.stderr.write(`Folders sync on the next unlocked command (or \`${cli} unlock --persist\` first).\n`);
|
|
}
|
|
});
|
|
});
|
|
}
|