feat(relay): rendezvous-relay service — 7 packages + plans (contracts/transport/agent/control-plane/e2e/auth/web)
Multi-tenant reverse-tunnel service ("ngrok for Claude Code" with E2E): a
host-agent dials OUT to an operator-run relay; external devices reach the host
THROUGH the relay, routed by per-tenant subdomain, forwarding ciphertext only
(the relay never sees plaintext). Lets a customer reach their own self-hosted
web-terminal from anywhere with zero networking setup.
Packages — all tsc-strict + vitest green (656 tests), cross-package integration verified:
- relay-contracts: frozen shared contracts (mux frame codec, data model,
capability token, E2E envelope, pairing) — the src/types.ts analog
- term-relay: native WS mux + stateless data plane (subdomain routing, ciphertext forward)
- agent: host-agent (pairing, per-host Ed25519 + mTLS dial-out, forwards to 127.0.0.1:3000)
- control-plane: accounts/hosts registry, pairing-code flow, routing table, provisioning
- relay-e2e: browser<->agent E2E (X25519 ECDH through relay, AEAD, anti-replay, recoverable replay key)
- relay-auth: Passkey/WebAuthn, capability tokens, per-host certs, deny-by-default tenant isolation
- relay-web: browser login + Web Crypto E2E + client-side preview rendering
Security invariants INV1-15 enforced; cross-tenant isolation CI tripwire live
(.github/workflows/relay-tripwire.yml). Design + implementation-level plans in
docs/PLAN_RELAY_*.md and docs/EXPLORE_RELAY_SERVICE.md.
NOTE: generated autonomously per the reviewed plans. The security-critical
packages (relay-e2e, relay-auth) REQUIRE expert security audit before any real
deployment — passing tests prove self-consistency, not resistance to attackers.
Base app (src/, public/) unchanged; concurrent desktop work left uncommitted.
This commit is contained in:
109
agent/src/keys/keystore.ts
Normal file
109
agent/src/keys/keystore.ts
Normal file
@@ -0,0 +1,109 @@
|
||||
/**
|
||||
* On-disk keystore — PLAN_RELAY_AGENT T3 (INV4/INV5). Persists ONLY to `stateDir`, every secret
|
||||
* file mode `0600`. Stores: the Ed25519 private key (PKCS#8 PEM), the mTLS cert + CA chain, and
|
||||
* the FIX 3 unwrapped `hostContentSecret`. The raw pairing code is NEVER written here (T4).
|
||||
*/
|
||||
import {
|
||||
chmodSync,
|
||||
existsSync,
|
||||
mkdirSync,
|
||||
readFileSync,
|
||||
statSync,
|
||||
writeFileSync,
|
||||
} from 'node:fs'
|
||||
import { join } from 'node:path'
|
||||
import type { AgentIdentity } from './identity.js'
|
||||
import { identityFromPrivatePem } from './identity.js'
|
||||
|
||||
const SECRET_MODE = 0o600
|
||||
const DIR_MODE = 0o700
|
||||
|
||||
export interface Keystore {
|
||||
saveIdentity(id: AgentIdentity): void
|
||||
loadIdentity(): AgentIdentity | null
|
||||
saveCert(certPem: string, caChainPem: string): void
|
||||
loadCert(): { certPem: string; caChainPem: string } | null
|
||||
saveContentSecret(secret: Uint8Array): void
|
||||
loadContentSecret(): Uint8Array | null
|
||||
}
|
||||
|
||||
const KEY_FILE = 'agent.key.pem'
|
||||
const CERT_FILE = 'agent.cert.pem'
|
||||
const CA_FILE = 'agent.ca.pem'
|
||||
const CONTENT_SECRET_FILE = 'content.secret'
|
||||
|
||||
/** Corrupt/unreadable key material surfaces as a typed error (never a silent empty read). */
|
||||
export class KeystoreError extends Error {
|
||||
constructor(message: string) {
|
||||
super(message)
|
||||
this.name = 'KeystoreError'
|
||||
}
|
||||
}
|
||||
|
||||
function ensureDir(stateDir: string): void {
|
||||
if (!existsSync(stateDir)) {
|
||||
mkdirSync(stateDir, { recursive: true, mode: DIR_MODE })
|
||||
return
|
||||
}
|
||||
// Refuse to write secrets into a world-/group-accessible directory (INV5 hard error).
|
||||
const mode = statSync(stateDir).mode & 0o777
|
||||
if ((mode & 0o077) !== 0) {
|
||||
throw new KeystoreError(
|
||||
`stateDir ${stateDir} is group/world accessible (mode ${mode.toString(8)}); refusing to write secrets`,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
function writeSecret(path: string, data: string | Uint8Array): void {
|
||||
writeFileSync(path, data, { mode: SECRET_MODE })
|
||||
// Enforce 0600 even if a prior umask/file left it wider.
|
||||
chmodSync(path, SECRET_MODE)
|
||||
}
|
||||
|
||||
export function openKeystore(stateDir: string): Keystore {
|
||||
const keyPath = join(stateDir, KEY_FILE)
|
||||
const certPath = join(stateDir, CERT_FILE)
|
||||
const caPath = join(stateDir, CA_FILE)
|
||||
const secretPath = join(stateDir, CONTENT_SECRET_FILE)
|
||||
|
||||
return {
|
||||
saveIdentity(id: AgentIdentity): void {
|
||||
ensureDir(stateDir)
|
||||
writeSecret(keyPath, id.exportPrivatePkcs8Pem())
|
||||
},
|
||||
loadIdentity(): AgentIdentity | null {
|
||||
if (!existsSync(keyPath)) return null
|
||||
let pem: string
|
||||
try {
|
||||
pem = readFileSync(keyPath, 'utf8')
|
||||
} catch (err) {
|
||||
throw new KeystoreError(`failed to read identity key: ${(err as Error).message}`)
|
||||
}
|
||||
try {
|
||||
return identityFromPrivatePem(pem)
|
||||
} catch (err) {
|
||||
throw new KeystoreError(`corrupt identity key file: ${(err as Error).message}`)
|
||||
}
|
||||
},
|
||||
saveCert(certPem: string, caChainPem: string): void {
|
||||
ensureDir(stateDir)
|
||||
writeSecret(certPath, certPem)
|
||||
writeSecret(caPath, caChainPem)
|
||||
},
|
||||
loadCert(): { certPem: string; caChainPem: string } | null {
|
||||
if (!existsSync(certPath) || !existsSync(caPath)) return null
|
||||
return {
|
||||
certPem: readFileSync(certPath, 'utf8'),
|
||||
caChainPem: readFileSync(caPath, 'utf8'),
|
||||
}
|
||||
},
|
||||
saveContentSecret(secret: Uint8Array): void {
|
||||
ensureDir(stateDir)
|
||||
writeSecret(secretPath, secret)
|
||||
},
|
||||
loadContentSecret(): Uint8Array | null {
|
||||
if (!existsSync(secretPath)) return null
|
||||
return new Uint8Array(readFileSync(secretPath))
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user