@astrofoundry/pi-astro 0.37.1 → 0.37.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/extensions/astro-discord/arcane-watch-monitor.ts +133 -0
- package/extensions/astro-discord/arcane-watch.test.ts +242 -0
- package/extensions/astro-discord/arcane-watch.ts +215 -0
- package/extensions/astro-discord/daily-report.ts +3 -1
- package/extensions/astro-discord/index.ts +27 -11
- package/extensions/astro-discord/security-monitor.test.ts +5 -1
- package/extensions/astro-discord/security-monitor.ts +21 -1
- package/extensions/astro-discord/security.test.ts +81 -2
- package/extensions/astro-discord/security.ts +126 -3
- package/package.json +1 -1
- package/skills/arcane/SKILL.md +2 -0
- package/skills/security/SKILL.md +1 -1
- package/specialists/arcane/run.ts +28 -0
- package/specialists/entry/remote/spc-security-obs-entry.sh +1 -1
- package/specialists/lib/container-watch.test.ts +54 -0
- package/specialists/lib/container-watch.ts +94 -0
- package/specialists/security/run.ts +1 -1
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { durationSeconds, exitCodeOf, healthOf, isRecent, listContainer, uptimeSeconds, withDetail } from "./container-watch.ts";
|
|
3
|
+
|
|
4
|
+
const NOW = Date.parse("2026-10-22T08:00:00Z");
|
|
5
|
+
|
|
6
|
+
describe("container-watch parsing", () => {
|
|
7
|
+
it("reads Docker's human durations as a lower bound", () => {
|
|
8
|
+
expect(durationSeconds("Less than a second")).toBe(0);
|
|
9
|
+
expect(durationSeconds("1 second")).toBe(1);
|
|
10
|
+
expect(durationSeconds("About a minute")).toBe(60);
|
|
11
|
+
expect(durationSeconds("12 minutes")).toBe(720);
|
|
12
|
+
expect(durationSeconds("About an hour")).toBe(3600);
|
|
13
|
+
expect(durationSeconds("3 weeks")).toBe(1_814_400);
|
|
14
|
+
expect(durationSeconds("soon")).toBeNull();
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("reads uptime, health, and exit code from the list status", () => {
|
|
18
|
+
expect(uptimeSeconds("Up 3 minutes (healthy)")).toBe(180);
|
|
19
|
+
expect(uptimeSeconds("Up About a minute")).toBe(60);
|
|
20
|
+
expect(uptimeSeconds("Up 2 hours (Paused)")).toBe(7200);
|
|
21
|
+
expect(uptimeSeconds("Exited (0) 2 days ago")).toBeNull();
|
|
22
|
+
expect(healthOf("Up 3 weeks (healthy)")).toBe("healthy");
|
|
23
|
+
expect(healthOf("Up 14 hours (unhealthy)")).toBe("unhealthy");
|
|
24
|
+
expect(healthOf("Up 5 seconds (health: starting)")).toBe("starting");
|
|
25
|
+
expect(healthOf("Up 6 days")).toBe("none");
|
|
26
|
+
expect(exitCodeOf("Exited (137) 1 minute ago")).toBe(137);
|
|
27
|
+
expect(exitCodeOf("Up 6 days")).toBeNull();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("turns a list row into a container and skips rows without a name or id", () => {
|
|
31
|
+
const row = { id: "abc", names: ["/syncthing"], imageId: "sha256:1", state: "running", status: "Up 14 hours (unhealthy)", created: 1792305689 };
|
|
32
|
+
expect(listContainer(row)).toEqual({ name: "syncthing", id: "abc", imageId: "sha256:1", state: "running", health: "unhealthy", exitCode: null, uptimeSeconds: 50_400, created: new Date(1792305689 * 1000).toISOString() });
|
|
33
|
+
expect(listContainer({ id: "abc", names: [] })).toBeNull();
|
|
34
|
+
expect(listContainer({ names: ["x"] })).toBeNull();
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("asks for details only about recent starts and creations", () => {
|
|
38
|
+
const base = { name: "x", id: "1", imageId: null, health: "none" as const, exitCode: null, created: "2026-10-01T00:00:00Z" };
|
|
39
|
+
expect(isRecent({ ...base, state: "running", uptimeSeconds: 120 }, NOW)).toBe(true);
|
|
40
|
+
expect(isRecent({ ...base, state: "running", uptimeSeconds: 7200 }, NOW)).toBe(false);
|
|
41
|
+
expect(isRecent({ ...base, state: "restarting", uptimeSeconds: null }, NOW)).toBe(true);
|
|
42
|
+
expect(isRecent({ ...base, state: "exited", uptimeSeconds: null, created: "2026-10-22T07:50:00Z" }, NOW)).toBe(true);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("adds the start time, the project, and our operation that covers the start", () => {
|
|
46
|
+
const c = { name: "mealie", id: "1", imageId: "sha256:2", state: "running", health: "healthy" as const, exitCode: null, uptimeSeconds: 120, created: "2026-10-22T07:57:00Z" };
|
|
47
|
+
const detail = { data: { imageId: "sha256:2", state: { status: "running", running: true, startedAt: "2026-10-22T07:58:00Z", health: { status: "healthy" } }, composeInfo: { projectName: "mealie" } } };
|
|
48
|
+
const audit = `${JSON.stringify({ ts: "2026-10-22T07:59:00Z", service: "arcane", caller: "discord-1", argv: ["stack-update", "mealie"], code: 0, durationMs: 180_000 })}\n`;
|
|
49
|
+
expect(withDetail(c, detail, audit)).toEqual({ ...c, startedAt: "2026-10-22T07:58:00Z", project: "mealie", operation: { command: "stack-update mealie", caller: "discord-1", from: "2026-10-22T07:56:00.000Z", to: "2026-10-22T07:59:00Z" } });
|
|
50
|
+
expect(withDetail(c, detail, "")).toEqual({ ...c, startedAt: "2026-10-22T07:58:00Z", project: "mealie" });
|
|
51
|
+
const never = { data: { state: { status: "created", startedAt: "0001-01-01T00:00:00Z" } } };
|
|
52
|
+
expect(withDetail({ ...c, state: "created" }, never, audit)).toEqual({ ...c, state: "created" });
|
|
53
|
+
});
|
|
54
|
+
});
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { coveringOperation, type Operation } from "./change-origin.ts";
|
|
2
|
+
import { containerFacts } from "./deployment.ts";
|
|
3
|
+
|
|
4
|
+
export type Health = "healthy" | "unhealthy" | "starting" | "none";
|
|
5
|
+
|
|
6
|
+
/** One container as `container-watch` reports it; start time, project, and operation only for recent ones. */
|
|
7
|
+
export interface WatchedContainer {
|
|
8
|
+
name: string;
|
|
9
|
+
id: string;
|
|
10
|
+
imageId: string | null;
|
|
11
|
+
state: string;
|
|
12
|
+
health: Health;
|
|
13
|
+
exitCode: number | null;
|
|
14
|
+
uptimeSeconds: number | null;
|
|
15
|
+
created: string | null;
|
|
16
|
+
startedAt?: string;
|
|
17
|
+
project?: string;
|
|
18
|
+
operation?: Operation;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Started or created within this window: the bridge polls every five minutes, so half an hour leaves room for a missed poll. */
|
|
22
|
+
export const RECENT_S = 1800;
|
|
23
|
+
|
|
24
|
+
const UNITS: Record<string, number> = { second: 1, minute: 60, hour: 3600, day: 86_400, week: 604_800, month: 2_592_000, year: 31_536_000 };
|
|
25
|
+
|
|
26
|
+
/** Docker's human duration (`Less than a second`, `About a minute`, `3 minutes`, `2 weeks`) as a lower bound in seconds. */
|
|
27
|
+
export function durationSeconds(text: string): number | null {
|
|
28
|
+
if (text === "Less than a second") return 0;
|
|
29
|
+
const about = /^About an? (minute|hour)$/.exec(text);
|
|
30
|
+
if (about) return UNITS[about[1]];
|
|
31
|
+
const m = /^(\d+) (second|minute|hour|day|week|month|year)s?$/.exec(text);
|
|
32
|
+
return m ? Number(m[1]) * UNITS[m[2]] : null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Seconds since the start from a list status such as `Up 3 minutes (healthy)`; null when the container is not up. */
|
|
36
|
+
export function uptimeSeconds(status: string): number | null {
|
|
37
|
+
const m = /^Up (.+?)(?: \(.*\))?$/.exec(status);
|
|
38
|
+
return m ? durationSeconds(m[1]) : null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function healthOf(status: string): Health {
|
|
42
|
+
const m = /\((healthy|unhealthy|health: starting)\)/.exec(status);
|
|
43
|
+
if (!m) return "none";
|
|
44
|
+
return m[1] === "health: starting" ? "starting" : (m[1] as Health);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function exitCodeOf(status: string): number | null {
|
|
48
|
+
const m = /^Exited \((-?\d+)\)/.exec(status);
|
|
49
|
+
return m ? Number(m[1]) : null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function str(v: unknown): string | null {
|
|
53
|
+
return typeof v === "string" && v.length > 0 ? v : null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** One row of `containers list --all`. */
|
|
57
|
+
export function listContainer(row: Record<string, unknown>): WatchedContainer | null {
|
|
58
|
+
const names = Array.isArray(row.names) ? row.names : [];
|
|
59
|
+
const name = typeof names[0] === "string" ? names[0].replace(/^\//, "") : null;
|
|
60
|
+
const id = str(row.id);
|
|
61
|
+
if (!name || !id) return null;
|
|
62
|
+
const status = typeof row.status === "string" ? row.status : "";
|
|
63
|
+
return {
|
|
64
|
+
name,
|
|
65
|
+
id,
|
|
66
|
+
imageId: str(row.imageId),
|
|
67
|
+
state: str(row.state) ?? "unknown",
|
|
68
|
+
health: healthOf(status),
|
|
69
|
+
exitCode: exitCodeOf(status),
|
|
70
|
+
uptimeSeconds: uptimeSeconds(status),
|
|
71
|
+
created: typeof row.created === "number" ? new Date(row.created * 1000).toISOString() : null,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Whether the bridge needs the exact start time and project: started or created recently, or restarting now. */
|
|
76
|
+
export function isRecent(c: WatchedContainer, now: number): boolean {
|
|
77
|
+
if (c.state === "restarting") return true;
|
|
78
|
+
if (c.uptimeSeconds !== null && c.uptimeSeconds < RECENT_S) return true;
|
|
79
|
+
return c.created !== null && now - Date.parse(c.created) < RECENT_S * 1000;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Adds the start time and project from `containers get`, and our operation on that stack whose run covers the start. */
|
|
83
|
+
export function withDetail(c: WatchedContainer, detail: unknown, auditText: string): WatchedContainer {
|
|
84
|
+
const facts = containerFacts(detail, c.name);
|
|
85
|
+
const top = ((detail as { data?: unknown } | null)?.data ?? detail) as { state?: { startedAt?: unknown } } | null;
|
|
86
|
+
const startedAt = str(top?.state?.startedAt);
|
|
87
|
+
const out: WatchedContainer = { ...c, ...(startedAt && !startedAt.startsWith("0001-") ? { startedAt } : {}), ...(facts.project ? { project: facts.project } : {}) };
|
|
88
|
+
const at = Date.parse(out.startedAt ?? c.created ?? "");
|
|
89
|
+
if (out.project && !Number.isNaN(at)) {
|
|
90
|
+
const operation = coveringOperation(auditText, out.project, at);
|
|
91
|
+
if (operation) out.operation = operation;
|
|
92
|
+
}
|
|
93
|
+
return out;
|
|
94
|
+
}
|
|
@@ -32,7 +32,7 @@ export const OBS_COMMANDS: Readonly<Record<string, { args: [number, number]; hel
|
|
|
32
32
|
journal: { args: [2, 2], help: "journal <unit> <since> e.g. journal crowdsec '1 hour ago'" },
|
|
33
33
|
};
|
|
34
34
|
|
|
35
|
-
export const OBS_UNITS = ["crowdsec", "rsyslog", "nftables", "docker", "
|
|
35
|
+
export const OBS_UNITS = ["crowdsec", "rsyslog", "nftables", "docker", "update-home-whitelist", "ssh"];
|
|
36
36
|
const SINCE = /^[A-Za-z0-9 :\-+]{1,40}$/;
|
|
37
37
|
const HOSTNAME = /^[A-Za-z0-9][A-Za-z0-9.-]{0,62}$/;
|
|
38
38
|
const PROGRAM = /^[A-Za-z0-9][A-Za-z0-9_.-]{0,62}$/;
|