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>
80 lines
3.7 KiB
TypeScript
80 lines
3.7 KiB
TypeScript
import { checkPorts, parseFrpsIni, parseSs, portAllowed } from "../src/central";
|
|
import { LogTail, redact } from "../src/redact";
|
|
import { churchProcesses, cpuPercent, parseMeminfo } from "../src/stats";
|
|
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
|
|
describe("log lines are redacted before they leave the server", () => {
|
|
it.each([
|
|
["Error: connect postgresql://hope_events:Zx81kLmQ@localhost:5432/hope_events", "postgresql://hope_events:***@localhost"],
|
|
["JWT_SECRET=abcdefghijklmnop", "JWT_SECRET=***"],
|
|
['DB_PASSWORD: "hunter2 x"', "DB_PASSWORD: ***"],
|
|
["Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.payload.sig", "Authorization: Bearer ***"],
|
|
["licence CEH-7K2M-9QXA-P4RT-WD8C refused", "CEH-****"],
|
|
["token a3f9c1d27b8e4f60a3f9c1d27b8e4f60a3f9c1d2", "token ***"],
|
|
])("%s", (line, expected) => {
|
|
const out = redact(line);
|
|
expect(out).toContain(expected);
|
|
});
|
|
|
|
it("keeps ordinary lines", () => {
|
|
expect(redact("==> events: migrations")).toBe("==> events: migrations");
|
|
});
|
|
|
|
it("keeps only the last lines", () => {
|
|
const t = new LogTail(3);
|
|
t.push("1\n2\n3\n4");
|
|
t.push("\n5\n");
|
|
expect(t.end()).toEqual(["3", "4", "5"]);
|
|
});
|
|
});
|
|
|
|
describe("central server checks", () => {
|
|
const ini = "[common]\nbind_port = 7000\nproxy_bind_addr = 127.0.0.1\nallow_ports = 20000-29999,3000\n\n[web]\nproxy_bind_addr = 0.0.0.0\n";
|
|
|
|
it("reads [common] only", () => {
|
|
expect(parseFrpsIni(ini)).toEqual({ proxyBindAddr: "127.0.0.1", allowPorts: "20000-29999,3000" });
|
|
});
|
|
|
|
it("allow_ports ranges", () => {
|
|
expect(portAllowed(20010, "20000-29999,3000")).toBe(true);
|
|
expect(portAllowed(3000, "20000-29999,3000")).toBe(true);
|
|
expect(portAllowed(5000, "20000-29999,3000")).toBe(false);
|
|
expect(portAllowed(5000, null)).toBe(true);
|
|
});
|
|
|
|
it("ports must be allowed, open and bound to 127.0.0.1 only", () => {
|
|
const ss = "LISTEN 0 4096 127.0.0.1:20010 0.0.0.0:*\nLISTEN 0 4096 0.0.0.0:20011 0.0.0.0:*\nLISTEN 0 4096 [::1]:20012 [::]:*\n";
|
|
const res = checkPorts([20010, 20011, 20012, 20013], parseFrpsIni(ini), parseSs(ss));
|
|
expect(res.ports.map((p) => [p.port, p.ok])).toEqual([[20010, true], [20011, false], [20012, true], [20013, false]]);
|
|
expect(res.warnings.join(" ")).toMatch(/20011 is open on more than 127.0.0.1/);
|
|
expect(res.warnings.join(" ")).toMatch(/20013 isn't open/);
|
|
});
|
|
|
|
it("warns when proxy_bind_addr is missing", () => {
|
|
const res = checkPorts([20010], parseFrpsIni("[common]\nbind_port = 7000\n"), parseSs("LISTEN 0 1 127.0.0.1:20010 0.0.0.0:*"));
|
|
expect(res.warnings.join(" ")).toMatch(/proxy_bind_addr is not set/);
|
|
});
|
|
});
|
|
|
|
describe("stats", () => {
|
|
it("RAM from /proc/meminfo", () => {
|
|
const r = parseMeminfo("MemTotal: 4000000 kB\nMemFree: 100 kB\nMemAvailable: 1000000 kB\nSwapTotal: 2000 kB\nSwapFree: 500 kB\n");
|
|
expect(r).toEqual({ totalBytes: 4096000000, usedBytes: 3072000000, availableBytes: 1024000000, swapTotalBytes: 2048000, swapUsedBytes: 1536000 });
|
|
});
|
|
|
|
it("CPU % between two readings", () => {
|
|
expect(cpuPercent("cpu 100 0 100 700 100 0 0 0", "cpu 150 0 150 750 150 0 0 0")).toBe(50);
|
|
});
|
|
|
|
it("PM2 processes are matched to churches through church.env", () => {
|
|
const sites = fs.mkdtempSync(path.join(os.tmpdir(), "sites-"));
|
|
fs.mkdirSync(path.join(sites, "hope"));
|
|
fs.writeFileSync(path.join(sites, "hope", "church.env"), 'CHURCH=hope\nEVENTS_USER=cc-hope\nEVENTS_PM2="hope-events-backend hope-events-frontend"\n');
|
|
fs.mkdirSync(path.join(sites, "Bad Name"));
|
|
const map = churchProcesses(sites);
|
|
expect([...map.entries()]).toEqual([["cc-hope", [{ church: "hope", product: "events", names: ["hope-events-backend", "hope-events-frontend"] }]]]);
|
|
});
|
|
});
|