mirror of
https://github.com/profullstack/logicsrc.git
synced 2026-12-04 02:29:06 +00:00
logicsrc passwd: one password for the vault and the SSH keyring (#300)
* feat(cli): logicsrc passwd: one password for the vault and the SSH keyring
The personal vault and the keyring each had their own password, so the
keyring prompt ("Keyring passphrase for id_ed25519") asked for a secret
nobody remembered. `logicsrc passwd` opens both (current password, or the
recovery key; it tries the vault password on the keyring first), asks for
one new password twice, and re-wraps both user keys under it. Not one item
is re-encrypted; recovery keys keep working. Nothing is written until both
stores are open.
The vault sync already follows a password change. The keyring sync compares
the wrapped key, so the account copy is replaced (the `keyring sync
--use-local` path). --reset-keyring starts a fresh keyring when its
passphrase and recovery key are both lost.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* passwd: LOGICSRC_NEW_PASSWORD, so one script asks once
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* passwd: audit the re-wrap as vault.rekey
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
267973f439
commit
cb0a938e89
3 changed files with 255 additions and 0 deletions
92
packages/cli/src/passwd.test.ts
Normal file
92
packages/cli/src/passwd.test.ts
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
import { mkdtempSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import { createVault, createVaultStore, unlockVault, type VaultStore } from "@logicsrc/opencreds";
|
||||
import { KeyringStore, createKeyring, openEntry, sealEntry, unlockKeyring } from "./keyring.js";
|
||||
import { changeAllPasswords } from "./passwd.js";
|
||||
|
||||
const KEY = "-----BEGIN OPENSSH PRIVATE KEY-----\nAAAAfake\n-----END OPENSSH PRIVATE KEY-----\n";
|
||||
const PUB = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIy86FQN77URkkunH3W6C28+mxsyOF5E6NrigzwAZGlz test";
|
||||
|
||||
let dir: string;
|
||||
let vault: VaultStore;
|
||||
let keyring: KeyringStore;
|
||||
|
||||
beforeEach(() => {
|
||||
dir = mkdtempSync(join(tmpdir(), "passwd-test-"));
|
||||
vault = createVaultStore(join(dir, "vault"));
|
||||
keyring = new KeyringStore(join(dir, "keyring"));
|
||||
});
|
||||
afterEach(() => rmSync(dir, { recursive: true, force: true }));
|
||||
|
||||
/** Answers prompts in order. */
|
||||
const answers = (...list: string[]) => async () => {
|
||||
if (!list.length) throw new Error("unexpected prompt");
|
||||
return list.shift()!;
|
||||
};
|
||||
|
||||
async function seed(vaultPassword: string, keyringPassword: string) {
|
||||
const v = await createVault(vaultPassword);
|
||||
vault.writeMeta(v.meta);
|
||||
const k = await createKeyring(keyringPassword);
|
||||
keyring.writeMeta(k.meta);
|
||||
keyring.write(await sealEntry(k.userKey, { name: "id_ed25519", privateKey: KEY, publicKey: PUB }));
|
||||
return { vaultRecovery: v.recoveryKey, keyringRecovery: k.recoveryKey };
|
||||
}
|
||||
|
||||
describe("logicsrc passwd", () => {
|
||||
it("puts the vault and the keyring under one new password; stored keys still open", async () => {
|
||||
await seed("old vault pw", "old keyring pw");
|
||||
const r = await changeAllPasswords(
|
||||
{ noSync: true },
|
||||
{ vault, keyring, ask: answers("old vault pw", "old keyring pw"), askNew: async () => "one password", say: () => {} }
|
||||
);
|
||||
expect(r).toMatchObject({ vault: "changed", keyring: "changed" });
|
||||
await expect(unlockVault(vault.readMeta()!, "one password")).resolves.toBeInstanceOf(Uint8Array);
|
||||
await expect(unlockVault(vault.readMeta()!, "old vault pw")).rejects.toThrow();
|
||||
const kk = await unlockKeyring(keyring.readMeta()!, { passphrase: "one password" });
|
||||
await expect(unlockKeyring(keyring.readMeta()!, { passphrase: "old keyring pw" })).rejects.toThrow();
|
||||
const opened = await openEntry(kk, keyring.find("id_ed25519")!);
|
||||
expect(opened.privateKey).toBe(KEY);
|
||||
}, 60_000);
|
||||
|
||||
it("tries the vault password on the keyring first, so a shared password is asked once", async () => {
|
||||
await seed("same", "same");
|
||||
const r = await changeAllPasswords(
|
||||
{ noSync: true },
|
||||
{ vault, keyring, ask: answers("same"), askNew: async () => "new", say: () => {} }
|
||||
);
|
||||
expect(r.keyring).toBe("changed");
|
||||
}, 60_000);
|
||||
|
||||
it("falls back to the recovery keys, and those keep working afterwards", async () => {
|
||||
const { vaultRecovery, keyringRecovery } = await seed("forgotten 1", "forgotten 2");
|
||||
await changeAllPasswords(
|
||||
{ noSync: true },
|
||||
{ vault, keyring, ask: answers("", vaultRecovery, "", keyringRecovery), askNew: async () => "new", say: () => {} }
|
||||
);
|
||||
await expect(unlockKeyring(keyring.readMeta()!, { recoveryKey: keyringRecovery })).resolves.toBeInstanceOf(Uint8Array);
|
||||
await expect(unlockKeyring(keyring.readMeta()!, { passphrase: "new" })).resolves.toBeInstanceOf(Uint8Array);
|
||||
}, 60_000);
|
||||
|
||||
it("writes nothing when the keyring cannot be opened", async () => {
|
||||
await seed("vault pw", "keyring pw");
|
||||
const before = JSON.stringify(vault.readMeta());
|
||||
await expect(
|
||||
changeAllPasswords({ noSync: true }, { vault, keyring, ask: answers("vault pw", "wrong"), askNew: async () => "new", say: () => {} })
|
||||
).rejects.toThrow();
|
||||
expect(JSON.stringify(vault.readMeta())).toBe(before);
|
||||
}, 60_000);
|
||||
|
||||
it("--reset-keyring starts a fresh keyring under the new password and returns its recovery key", async () => {
|
||||
await seed("vault pw", "lost");
|
||||
const r = await changeAllPasswords(
|
||||
{ noSync: true, resetKeyring: true },
|
||||
{ vault, keyring, ask: answers("vault pw"), askNew: async () => "new", say: () => {} }
|
||||
);
|
||||
expect(r.keyring).toBe("created");
|
||||
expect(r.keyringRecoveryKey).toBeTruthy();
|
||||
await expect(unlockKeyring(keyring.readMeta()!, { passphrase: "new" })).resolves.toBeInstanceOf(Uint8Array);
|
||||
}, 60_000);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue