logicsrc/packages/logicsrc-mcp/src/keyring.test.ts
Anthony Ettinger 1bff3e975c feat(keyring): end-to-end encrypted keyring for SSH keys and key pairs, optional cloud sync (CLI 0.12.0)
`logicsrc keyring` (alias `kr`; `keys` is API keys): add/gen/list/show/restore/
rm/export --pub/sync/status. A keyring is an OpenCreds user-profile vault (same
PBKDF2-600k + HKDF + AES-256-GCM scheme as `logicsrc vault`, recovery key);
each private key is an OpenCreds `key` item envelope, public halves stay
readable. Works fully local under ~/.config/logicsrc/keyring; `keyring sync`
pushes ciphertext to the account. restore writes the default file name,
600/644, refuses a different existing key without --force and backs it up
first; private keys are printed only with --print-private.

- apps/pwa: /api/keyring (person Bearer key; session read-only; machine keys
  refused), additive migration 008_keyring, server refuses any upload that
  carries plaintext private key material (incl. base64'd PEM "ciphertext");
  /keyring page lists public halves and decrypts in the browser (WebCrypto,
  same derivation) for download.
- logicsrc-mcp: list/show/export_authorized_keys/add (sealed only)/remove/
  restore instructions; never returns a private key.
- docs/keyring.md served at /docs/keyring.
- Tests: crypto round trip, ssh-keygen fingerprint parity, AAD binding,
  restore perms + overwrite guard + backup, two-machine sync, server stores
  ciphertext only, browser decrypt opens CLI-sealed envelopes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 18:45:46 +00:00

61 lines
3.7 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js";
import { createLogicSrcMcpServer } from "./server.js";
type Call = { url: string; method: string; body: unknown };
const PUB = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFK+fzMc3AneHlJ9+KGxhlTbhQDv4xlwE0oNmVCPpEL1 throwaway@test";
const entry = { id: "e1", name: "id_ed25519", type: "ssh-ed25519", filename: "id_ed25519", publicKey: PUB, fingerprint: "SHA256:abc", comment: "", createdAt: "2026-10-28T00:00:00Z", updatedAt: "2026-10-28T00:00:00Z", deletedAt: null };
function fakeApi(calls: Call[], payload: (url: string) => unknown): typeof fetch {
return (async (input: string | URL | Request, init?: RequestInit) => {
calls.push({ url: String(input), method: init?.method ?? "GET", body: init?.body ? JSON.parse(String(init.body)) : undefined });
return new Response(JSON.stringify(payload(String(input))), { status: 200, headers: { "content-type": "application/json" } });
}) as typeof fetch;
}
async function connect(doFetch: typeof fetch) {
const [c, s] = InMemoryTransport.createLinkedPair();
const server = createLogicSrcMcpServer({ apiKeys: { env: { LOGICSRC_API_KEY: "lsk_person" }, fetch: doFetch } });
const client = new Client({ name: "t", version: "0.1.0" });
await server.connect(s);
await client.connect(c);
return { client, close: async () => { await client.close(); await server.close(); } };
}
const text = (r: unknown) => ((r as { content: Array<{ text: string }> }).content[0]?.text ?? "");
describe("keyring tools", () => {
it("lists public halves and exports authorized_keys", async () => {
const calls: Call[] = [];
const { client, close } = await connect(fakeApi(calls, () => ({ meta: null, entries: [entry] })));
const names = (await client.listTools()).tools.map((t) => t.name);
expect(names).toEqual(expect.arrayContaining(["list_keyring_keys", "show_keyring_key", "export_authorized_keys", "add_keyring_key_ciphertext", "remove_keyring_key", "keyring_restore_instructions"]));
expect(text(await client.callTool({ name: "list_keyring_keys", arguments: {} }))).toContain("SHA256:abc");
expect(text(await client.callTool({ name: "export_authorized_keys", arguments: {} }))).toBe(PUB);
expect(calls[0]!.url).toBe("https://app.logicsrc.com/api/keyring");
await close();
});
it("refuses a plaintext private key and never relays one back", async () => {
const calls: Call[] = [];
const { client, close } = await connect(fakeApi(calls, () => ({ entry: { ...entry, publicKey: "-----BEGIN OPENSSH PRIVATE KEY-----" } })));
const bad = await client.callTool({
name: "add_keyring_key_ciphertext",
arguments: { entry: { ...entry, deletedAt: undefined, envelope: { id: "e1", type: 5, ciphertext: Buffer.from("-----BEGIN OPENSSH PRIVATE KEY-----").toString("base64"), iv: "AAAAAAAAAAAAAAAA" } } }
});
expect((bad as { isError?: boolean }).isError).toBe(true);
expect(calls).toHaveLength(0);
const shown = await client.callTool({ name: "show_keyring_key", arguments: { name: "id_ed25519" } });
expect((shown as { isError?: boolean }).isError).toBe(true);
expect(text(shown)).not.toContain("BEGIN OPENSSH");
await close();
});
it("restore instructions name the default file and the fingerprint", async () => {
const { client, close } = await connect(fakeApi([], () => ({ entry })));
const out = text(await client.callTool({ name: "keyring_restore_instructions", arguments: { name: "id_ed25519" } }));
expect(out).toContain("logicsrc keyring restore id_ed25519");
expect(out).toContain("~/.ssh/id_ed25519");
await close();
});
});