Implement keep: self-hosted E2E encrypted secrets sync
Server: Express + better-sqlite3 (WAL), multi-recipient key-wrapping per IMPLEMENTATION.md's design — vaults/recipients/vault_grants/ access_log. Two auth paths: ADMIN_PASSWORD header for recipient management and revoke (pure metadata operations), signed-request auth (Ed25519 signature over method+path+timestamp+body-hash) for push/pull/grant, mirroring the spirit of this project family's other signed-handshake patterns without naming them. CLI: identity init/show/set-id, push/pull/grant/log, admin recipient add/list/remove and revoke. Grant is a client-side crypto operation (the granter unwraps the vault's current key locally and reseals it for the new recipient) rather than a server-side operation, since the server never holds an unwrapped key to grant with. Verified end-to-end with two independent local identities against a live server and separately against the built Docker image: register, push, pull (granted and ungranted), grant without re-pushing, admin revoke, a subsequent rotation confirming the revoked recipient stays excluded, and rejection of missing/malformed signed-request auth. Two real bugs caught during verification, not just written up: - libsodium-wrappers' published ESM build does a relative import only resolvable under bundler-style resolution — broken under plain Node ESM. Fixed via createRequire to force the CJS build. - Express's req.path inside a sub-router is relative to the mount point, which would have silently mismatched a client signing the full request path. Fixed by verifying against req.originalUrl. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
21
src/cli/commands/identity.ts
Normal file
21
src/cli/commands/identity.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { loadOrCreateIdentity, setRecipientId } from '../identityStore.js';
|
||||
|
||||
export async function identityInit(): Promise<void> {
|
||||
const { identity, created } = await loadOrCreateIdentity();
|
||||
console.log(created ? 'identity created.' : 'identity already exists.');
|
||||
console.log(`public key: ${identity.id}`);
|
||||
console.log(`\nSend this public key to whoever runs 'keep recipient add' for this vault server.`);
|
||||
console.log(`They'll give you back a recipient id — set it locally with:`);
|
||||
console.log(` keep identity set-id <recipient-id>`);
|
||||
}
|
||||
|
||||
export async function identityShow(): Promise<void> {
|
||||
const { identity, recipientId } = await loadOrCreateIdentity();
|
||||
console.log(`public key: ${identity.id}`);
|
||||
console.log(`recipient id: ${recipientId ?? '(not set — see: keep identity set-id)'}`);
|
||||
}
|
||||
|
||||
export function identitySetId(recipientId: string): void {
|
||||
setRecipientId(recipientId);
|
||||
console.log(`recipient id set to: ${recipientId}`);
|
||||
}
|
||||
20
src/cli/commands/recipient.ts
Normal file
20
src/cli/commands/recipient.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { adminFetch, expectOk } from '../signedFetch.js';
|
||||
|
||||
export async function recipientAdd(label: string, publicKey: string): Promise<void> {
|
||||
const data = await expectOk(await adminFetch('POST', '/api/admin/recipients', { label, publicKey }));
|
||||
console.log(`recipient created: ${data.recipientId}`);
|
||||
console.log(`Give this id back to whoever owns this identity — they set it with 'keep identity set-id ${data.recipientId}'.`);
|
||||
}
|
||||
|
||||
export async function recipientList(): Promise<void> {
|
||||
const data = await expectOk(await adminFetch('GET', '/api/admin/recipients'));
|
||||
if (!data.length) { console.log('no recipients yet.'); return; }
|
||||
for (const r of data) {
|
||||
console.log(`${r.recipientId} ${r.label} ${r.publicKey.slice(0, 16)}…`);
|
||||
}
|
||||
}
|
||||
|
||||
export async function recipientRemove(recipientId: string): Promise<void> {
|
||||
await expectOk(await adminFetch('DELETE', `/api/admin/recipients/${encodeURIComponent(recipientId)}`));
|
||||
console.log(`recipient removed: ${recipientId} (and all their vault grants)`);
|
||||
}
|
||||
110
src/cli/commands/vault.ts
Normal file
110
src/cli/commands/vault.ts
Normal file
@@ -0,0 +1,110 @@
|
||||
import fs from 'node:fs';
|
||||
import { requireIdentityAndRecipientId } from '../identityStore.js';
|
||||
import { signedFetch, adminFetch, expectOk } from '../signedFetch.js';
|
||||
import { parseEnv, serializeEnv } from '../envFormat.js';
|
||||
import {
|
||||
ready,
|
||||
Identity,
|
||||
generateSymmetricKey,
|
||||
secretboxEncrypt,
|
||||
secretboxDecrypt,
|
||||
enc,
|
||||
} from '../../shared/crypto.js';
|
||||
|
||||
export async function vaultPush(vaultKey: string, file: string): Promise<void> {
|
||||
await ready();
|
||||
const { identity, recipientId } = await requireIdentityAndRecipientId();
|
||||
|
||||
if (!fs.existsSync(file)) throw new Error(`file not found: ${file}`);
|
||||
const values = parseEnv(fs.readFileSync(file, 'utf8'));
|
||||
const payload = enc.fromUtf8(JSON.stringify(values));
|
||||
|
||||
const existsRes = await expectOk(await signedFetch(identity, recipientId, 'GET', `/api/vaults/${encodeURIComponent(vaultKey)}/exists`));
|
||||
|
||||
const wrapFor = new Map<string, string>(); // recipientId -> publicKey
|
||||
wrapFor.set(recipientId, identity.id);
|
||||
|
||||
if (existsRes.exists) {
|
||||
const recipientsRes = await signedFetch(identity, recipientId, 'GET', `/api/vaults/${encodeURIComponent(vaultKey)}/recipients`);
|
||||
if (!recipientsRes.ok) {
|
||||
const body = await recipientsRes.json().catch(() => ({}));
|
||||
throw new Error(body.error ?? `no access to vault '${vaultKey}' — ask an existing grantee to run 'keep grant ${vaultKey} ${recipientId}'`);
|
||||
}
|
||||
const current = await recipientsRes.json() as { recipientId: string; publicKey: string }[];
|
||||
for (const r of current) if (r.publicKey) wrapFor.set(r.recipientId, r.publicKey);
|
||||
}
|
||||
|
||||
const symmetricKey = generateSymmetricKey();
|
||||
const { nonce, ciphertext } = secretboxEncrypt(symmetricKey, payload);
|
||||
|
||||
const wrappedKeys: Record<string, string> = {};
|
||||
for (const [rid, pubHex] of wrapFor) {
|
||||
wrappedKeys[rid] = enc.toBase64(Identity.sealFor(pubHex, symmetricKey));
|
||||
}
|
||||
|
||||
await expectOk(await signedFetch(identity, recipientId, 'POST', `/api/vaults/${encodeURIComponent(vaultKey)}/push`, {
|
||||
ciphertext: enc.toBase64(ciphertext),
|
||||
nonce: enc.toBase64(nonce),
|
||||
wrappedKeys,
|
||||
}));
|
||||
|
||||
console.log(`pushed '${vaultKey}' — ${Object.keys(values).length} key${Object.keys(values).length === 1 ? '' : 's'}, wrapped for ${wrapFor.size} recipient${wrapFor.size === 1 ? '' : 's'}.`);
|
||||
}
|
||||
|
||||
export async function vaultPull(vaultKey: string, format: 'env' | 'json', outFile?: string): Promise<void> {
|
||||
await ready();
|
||||
const { identity, recipientId } = await requireIdentityAndRecipientId();
|
||||
|
||||
const data = await expectOk(await signedFetch(identity, recipientId, 'GET', `/api/vaults/${encodeURIComponent(vaultKey)}/pull`));
|
||||
|
||||
const symmetricKey = identity.openSealed(enc.fromBase64(data.wrappedKey));
|
||||
if (!symmetricKey) throw new Error('failed to unwrap this vault\'s key — wrong identity, or the grant is stale');
|
||||
|
||||
const plaintext = secretboxDecrypt(symmetricKey, enc.fromBase64(data.nonce), enc.fromBase64(data.ciphertext));
|
||||
const values = JSON.parse(enc.toUtf8(plaintext)) as Record<string, string>;
|
||||
|
||||
const out = format === 'json' ? JSON.stringify(values, null, 2) : serializeEnv(values);
|
||||
if (outFile) {
|
||||
fs.writeFileSync(outFile, out);
|
||||
console.error(`wrote ${Object.keys(values).length} key(s) to ${outFile}`);
|
||||
} else {
|
||||
process.stdout.write(out);
|
||||
}
|
||||
}
|
||||
|
||||
export async function vaultGrant(vaultKey: string, targetRecipientId: string): Promise<void> {
|
||||
await ready();
|
||||
const { identity, recipientId } = await requireIdentityAndRecipientId();
|
||||
|
||||
const pullRes = await expectOk(await signedFetch(identity, recipientId, 'GET', `/api/vaults/${encodeURIComponent(vaultKey)}/pull`));
|
||||
const symmetricKey = identity.openSealed(enc.fromBase64(pullRes.wrappedKey));
|
||||
if (!symmetricKey) throw new Error("failed to unwrap this vault's key — can't grant access to someone else without it");
|
||||
|
||||
const recipientsRes = await expectOk(await signedFetch(identity, recipientId, 'GET', '/api/recipients'));
|
||||
const target = (recipientsRes as { recipientId: string; publicKey: string }[]).find(r => r.recipientId === targetRecipientId);
|
||||
if (!target) throw new Error(`unknown recipient id: ${targetRecipientId} (ask an admin to 'keep recipient add' them first)`);
|
||||
|
||||
const wrappedKey = enc.toBase64(Identity.sealFor(target.publicKey, symmetricKey));
|
||||
await expectOk(await signedFetch(identity, recipientId, 'POST', `/api/vaults/${encodeURIComponent(vaultKey)}/grant`, {
|
||||
recipientId: targetRecipientId,
|
||||
wrappedKey,
|
||||
}));
|
||||
|
||||
console.log(`granted '${vaultKey}' to ${targetRecipientId}.`);
|
||||
}
|
||||
|
||||
export async function vaultRevoke(vaultKey: string, targetRecipientId: string): Promise<void> {
|
||||
await expectOk(await adminFetch('DELETE', `/api/admin/vaults/${encodeURIComponent(vaultKey)}/grants/${encodeURIComponent(targetRecipientId)}`));
|
||||
console.log(`revoked ${targetRecipientId}'s access to '${vaultKey}'.`);
|
||||
console.log(`Note: this stops future pulls only. If this was a compromise response, also run 'keep push ${vaultKey}' with rotated values.`);
|
||||
}
|
||||
|
||||
export async function vaultLog(vaultKey: string): Promise<void> {
|
||||
await ready();
|
||||
const { identity, recipientId } = await requireIdentityAndRecipientId();
|
||||
const entries = await expectOk(await signedFetch(identity, recipientId, 'GET', `/api/vaults/${encodeURIComponent(vaultKey)}/log`));
|
||||
if (!entries.length) { console.log('no access log entries yet.'); return; }
|
||||
for (const e of entries as { recipient_id: string; action: string; accessed_at: number }[]) {
|
||||
console.log(`${new Date(e.accessed_at * 1000).toISOString()} ${e.action.padEnd(6)} ${e.recipient_id}`);
|
||||
}
|
||||
}
|
||||
22
src/cli/envFormat.ts
Normal file
22
src/cli/envFormat.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
export function parseEnv(text: string): Record<string, string> {
|
||||
const out: Record<string, string> = {};
|
||||
for (const rawLine of text.split('\n')) {
|
||||
const line = rawLine.trim();
|
||||
if (!line || line.startsWith('#')) continue;
|
||||
const eq = line.indexOf('=');
|
||||
if (eq === -1) continue;
|
||||
const key = line.slice(0, eq).trim();
|
||||
let value = line.slice(eq + 1).trim();
|
||||
if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
|
||||
value = value.slice(1, -1);
|
||||
}
|
||||
out[key] = value;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export function serializeEnv(values: Record<string, string>): string {
|
||||
return Object.entries(values)
|
||||
.map(([k, v]) => `${k}=${/[\s#"']/.test(v) ? JSON.stringify(v) : v}`)
|
||||
.join('\n') + '\n';
|
||||
}
|
||||
53
src/cli/identityStore.ts
Normal file
53
src/cli/identityStore.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import os from 'node:os';
|
||||
import { Identity } from '../shared/crypto.js';
|
||||
|
||||
const DIR = path.join(os.homedir(), '.keep');
|
||||
const FILE = path.join(DIR, 'identity.json');
|
||||
|
||||
interface StoredIdentity {
|
||||
seedHex: string;
|
||||
// Assigned by whoever runs `keep recipient add` after registering this
|
||||
// identity's public key — not derivable from the keypair itself, so it
|
||||
// has to be stored once known via `keep identity set-id`.
|
||||
recipientId?: string;
|
||||
}
|
||||
|
||||
function readStored(): StoredIdentity | null {
|
||||
if (!fs.existsSync(FILE)) return null;
|
||||
return JSON.parse(fs.readFileSync(FILE, 'utf8')) as StoredIdentity;
|
||||
}
|
||||
|
||||
function writeStored(data: StoredIdentity): void {
|
||||
fs.mkdirSync(DIR, { recursive: true, mode: 0o700 });
|
||||
fs.writeFileSync(FILE, JSON.stringify(data, null, 2), { mode: 0o600 });
|
||||
}
|
||||
|
||||
export async function loadOrCreateIdentity(): Promise<{ identity: Identity; recipientId: string | null; created: boolean }> {
|
||||
const stored = readStored();
|
||||
if (stored) {
|
||||
return { identity: await Identity.fromSeedHex(stored.seedHex), recipientId: stored.recipientId ?? null, created: false };
|
||||
}
|
||||
const identity = await Identity.generate();
|
||||
writeStored({ seedHex: identity.seedHex });
|
||||
return { identity, recipientId: null, created: true };
|
||||
}
|
||||
|
||||
export function setRecipientId(recipientId: string): void {
|
||||
const stored = readStored();
|
||||
if (!stored) throw new Error("no local identity — run 'keep identity init' first");
|
||||
writeStored({ ...stored, recipientId });
|
||||
}
|
||||
|
||||
export async function requireIdentityAndRecipientId(): Promise<{ identity: Identity; recipientId: string }> {
|
||||
const stored = readStored();
|
||||
if (!stored) throw new Error("no local identity — run 'keep identity init' first");
|
||||
if (!stored.recipientId) {
|
||||
throw new Error(
|
||||
"this identity has no recipient id set yet — after an admin registers your public key " +
|
||||
"('keep identity show' to get it), run 'keep identity set-id <recipient-id>' with the id they give you back",
|
||||
);
|
||||
}
|
||||
return { identity: await Identity.fromSeedHex(stored.seedHex), recipientId: stored.recipientId };
|
||||
}
|
||||
61
src/cli/index.ts
Normal file
61
src/cli/index.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
#!/usr/bin/env node
|
||||
import { identityInit, identityShow, identitySetId } from './commands/identity.js';
|
||||
import { recipientAdd, recipientList, recipientRemove } from './commands/recipient.js';
|
||||
import { vaultPush, vaultPull, vaultGrant, vaultRevoke, vaultLog } from './commands/vault.js';
|
||||
|
||||
function flag(args: string[], name: string, fallback?: string): string | undefined {
|
||||
const i = args.indexOf(`--${name}`);
|
||||
return i !== -1 ? args[i + 1] : fallback;
|
||||
}
|
||||
|
||||
async function main(): Promise<void> {
|
||||
const [, , cmd, sub, ...rest] = process.argv;
|
||||
|
||||
try {
|
||||
if (cmd === 'identity') {
|
||||
if (sub === 'init') return await identityInit();
|
||||
if (sub === 'show') return await identityShow();
|
||||
if (sub === 'set-id') return identitySetId(rest[0]);
|
||||
}
|
||||
|
||||
if (cmd === 'recipient') {
|
||||
if (sub === 'add') return await recipientAdd(flag(rest, 'label')!, flag(rest, 'pubkey')!);
|
||||
if (sub === 'list') return await recipientList();
|
||||
if (sub === 'remove') return await recipientRemove(rest[0]);
|
||||
}
|
||||
|
||||
if (cmd === 'push') return await vaultPush(sub, flag(rest, 'file', '.env')!);
|
||||
if (cmd === 'pull') return await vaultPull(sub, (flag(rest, 'format', 'env') as 'env' | 'json'), flag(rest, 'out'));
|
||||
if (cmd === 'grant') return await vaultGrant(sub, rest[0]);
|
||||
if (cmd === 'revoke') return await vaultRevoke(sub, rest[0]);
|
||||
if (cmd === 'log') return await vaultLog(sub);
|
||||
|
||||
printUsage();
|
||||
process.exitCode = cmd ? 1 : 0;
|
||||
} catch (err) {
|
||||
console.error(`error: ${err instanceof Error ? err.message : err}`);
|
||||
process.exitCode = 1;
|
||||
}
|
||||
}
|
||||
|
||||
function printUsage(): void {
|
||||
console.log(`keep — self-hosted, end-to-end encrypted secrets sync
|
||||
|
||||
keep identity init
|
||||
keep identity show
|
||||
keep identity set-id <recipient-id>
|
||||
|
||||
keep push <vault> [--file .env]
|
||||
keep pull <vault> [--format env|json] [--out <path>]
|
||||
keep grant <vault> <recipient-id>
|
||||
keep revoke <vault> <recipient-id> (admin — needs KEEP_ADMIN_PASSWORD)
|
||||
keep log <vault>
|
||||
|
||||
keep recipient add --label <label> --pubkey <hex> (admin)
|
||||
keep recipient list (admin)
|
||||
keep recipient remove <recipient-id> (admin)
|
||||
|
||||
Env: KEEP_SERVER_URL (default http://localhost:3050), KEEP_ADMIN_PASSWORD (for admin commands)`);
|
||||
}
|
||||
|
||||
main();
|
||||
48
src/cli/signedFetch.ts
Normal file
48
src/cli/signedFetch.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { Identity, signingString, enc } from '../shared/crypto.js';
|
||||
|
||||
export function serverUrl(): string {
|
||||
return process.env.KEEP_SERVER_URL ?? 'http://localhost:3050';
|
||||
}
|
||||
|
||||
export async function signedFetch(
|
||||
identity: Identity,
|
||||
recipientId: string,
|
||||
method: string,
|
||||
path: string,
|
||||
body?: unknown,
|
||||
): Promise<Response> {
|
||||
const bodyBytes = body !== undefined ? enc.fromUtf8(JSON.stringify(body)) : new Uint8Array();
|
||||
const timestamp = Math.floor(Date.now() / 1000);
|
||||
const toSign = signingString(method, path, timestamp, bodyBytes);
|
||||
const signature = enc.toHex(identity.sign(enc.fromUtf8(toSign)));
|
||||
|
||||
return fetch(`${serverUrl()}${path}`, {
|
||||
method,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-Recipient-Id': recipientId,
|
||||
'X-Signature': signature,
|
||||
'X-Timestamp': String(timestamp),
|
||||
},
|
||||
body: body !== undefined ? JSON.stringify(body) : undefined,
|
||||
});
|
||||
}
|
||||
|
||||
export async function adminFetch(method: string, path: string, body?: unknown): Promise<Response> {
|
||||
const password = process.env.KEEP_ADMIN_PASSWORD;
|
||||
if (!password) throw new Error('KEEP_ADMIN_PASSWORD is not set');
|
||||
return fetch(`${serverUrl()}${path}`, {
|
||||
method,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-Admin-Password': password,
|
||||
},
|
||||
body: body !== undefined ? JSON.stringify(body) : undefined,
|
||||
});
|
||||
}
|
||||
|
||||
export async function expectOk(res: Response): Promise<any> {
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (!res.ok) throw new Error(data.error ?? `request failed: ${res.status}`);
|
||||
return data;
|
||||
}
|
||||
Reference in New Issue
Block a user