apps/agent (docs/AGENT-1.0-PLAN.md §3-4, §10): listens on nothing; checks every job (pinned Hub key, server, time window, replays for 90 days, type per mode, strict arguments) before running crosscode with an argument array; opens the sealed config into a root-only file only for the job; redacts log lines; keeps unsent reports in an outbox; encrypts exports before upload; check-ports on the central server (read-only); self-update with automatic revert. One bundled dist/cli.js, a systemd unit and the install script that joins a server with a one-time token. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
50 lines
2.0 KiB
TypeScript
50 lines
2.0 KiB
TypeScript
import fs from "node:fs";
|
|
import path from "node:path";
|
|
import { z } from "zod";
|
|
import { agent } from "@crosscode/contracts";
|
|
import { publicKeyOf } from "@crosscode/contracts/signing";
|
|
import { paths } from "./paths";
|
|
|
|
/** /etc/crosscode/agent.json, written by `crosscode-agent join`. No secrets: the keys are files next to it. */
|
|
export const agentConfigSchema = z.object({
|
|
hubUrl: z.url({ protocol: /^https?$/ }),
|
|
serverId: z.string().regex(/^srv_[A-Za-z0-9_-]{8,64}$/),
|
|
mode: agent.agentModeSchema,
|
|
/** The Hub's job-signing key, pinned when joining. */
|
|
hubJobKey: z.string().regex(/^[A-Za-z0-9_-]{43}$/),
|
|
pollSeconds: z.int().min(5).max(600).default(30),
|
|
statsSeconds: z.int().min(15).max(3600).default(60),
|
|
joinedAt: z.string(),
|
|
});
|
|
export type AgentConfig = z.infer<typeof agentConfigSchema>;
|
|
|
|
export interface Keys {
|
|
ed25519Pem: string;
|
|
x25519Pem: string;
|
|
}
|
|
|
|
export const configFile = () => path.join(paths.configDir, "agent.json");
|
|
export const ed25519File = () => path.join(paths.configDir, "agent-ed25519.pem");
|
|
export const x25519File = () => path.join(paths.configDir, "agent-x25519.pem");
|
|
|
|
/** Writes a file only root can read, atomically. */
|
|
export function writePrivate(file: string, text: string): void {
|
|
fs.mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 });
|
|
const tmp = `${file}.tmp-${process.pid}`;
|
|
fs.writeFileSync(tmp, text, { mode: 0o600 });
|
|
fs.renameSync(tmp, file);
|
|
}
|
|
|
|
export function loadConfig(): { config: AgentConfig; keys: Keys } {
|
|
const config = agentConfigSchema.parse(JSON.parse(fs.readFileSync(configFile(), "utf8")));
|
|
const keys = { ed25519Pem: fs.readFileSync(ed25519File(), "utf8"), x25519Pem: fs.readFileSync(x25519File(), "utf8") };
|
|
// A broken pair would sign requests the Hub can't check; better to stop at start.
|
|
publicKeyOf(keys.ed25519Pem);
|
|
publicKeyOf(keys.x25519Pem);
|
|
return { config, keys };
|
|
}
|
|
|
|
export function saveConfig(config: AgentConfig): void {
|
|
writePrivate(configFile(), `${JSON.stringify(config, null, 2)}\n`);
|
|
}
|