Bulk snooze, alert rules judged on report, photos on notes

Bulk snooze: Snooze... and Wake on the ticked alerts, with undo that puts back
each alert's earlier snooze exactly.

Alert rules are judged for a server right after its agent reports, instead of at
the next 15-minute pass; only that server's rule findings change, and the check
is serialized with the pass so neither overwrites the other's state.

Photos on server and device notes: up to 12 each, shrunk in the browser,
accepted by their own bytes (JPEG/PNG/GIF/WebP, 4 MB), kept in the database and
removed with the server or device.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-10-24 22:53:09 +02:00
1 parent 251a4a9d73
commit a16996d975
27 files changed
+8442 -54

No files matched your search

+3
View File
@@ -3,6 +3,7 @@ import { z } from "zod";
import { db } from "../db/client.js";
import { hashToken } from "../services/tokens.js";
import { syncServerTasks } from "../services/taskSync.js";
import { checkRulesFor } from "../services/healthMonitor.js";
import { asyncHandler } from "../utils/asyncHandler.js";
import { getSettings } from "../services/settingsStore.js";
import { latestAgentSha256, latestAgentVersions } from "../services/agentVersions.js";
@@ -206,4 +207,6 @@ agentReportRouter.post("/", asyncHandler(authenticateAgent), express.json({ limi
});
res.status(202).json({ ok: true, taskCount: parsed.data.tasks.length });
// The figures are as new as they will be: judge the alert rules for this server now rather than at the next pass. After the answer, so the agent is not kept waiting.
void checkRulesFor(server.id).catch((err) => console.error("[health] couldn't judge the alert rules after a report:", err instanceof Error ? err.message : err));
}));
+26 -2
View File
@@ -2,8 +2,9 @@ import { Router } from "express";
import { z } from "zod";
import { requireAuth, requireRole } from "../auth/middleware.js";
import { AckError, MAX_HOURS, MAX_NOTE } from "../services/alertAcks.js";
import { SnoozeError } from "../services/alertSnoozes.js";
import { recordAudit } from "../services/audit.js";
import { acknowledgeAlerts, BulkInputError, changeAssets, changeServerTag, MAX_BULK, restoreAcks, restoreAssets, restoreServerParents, setServerParents, setServerTags, silenceTargets, targetsOfAlerts, undoSilence, unacknowledgeAlerts } from "../services/bulkActions.js";
import { acknowledgeAlerts, BulkInputError, changeAssets, changeServerTag, MAX_BULK, restoreAcks, restoreAssets, restoreServerParents, restoreSnoozes, setServerParents, setServerTags, silenceTargets, snoozeAlerts, targetsOfAlerts, undoSilence, unacknowledgeAlerts, unsnoozeAlerts } from "../services/bulkActions.js";
import { InvalidTagError } from "../services/serverTags.js";
import { asyncHandler } from "../utils/asyncHandler.js";
@@ -102,7 +103,13 @@ const previousAck = z
})
.nullable();
const snoozeRecord = z.object({ conditionKey: z.string().max(700), message: z.string().max(500), until: z.string().max(40), snoozedBy: z.string().max(200), snoozedAt: z.string().max(40) });
const alertsBody = z.discriminatedUnion("action", [
/** When they should come back, as a date and time with its zone. */
z.object({ action: z.literal("snooze"), alertIds: stringIds, until: z.string().min(1).max(40) }),
z.object({ action: z.literal("unsnooze"), alertIds: stringIds }),
z.object({ action: z.literal("restore_snoozes"), items: z.array(z.object({ alertId: z.string().min(1).max(600), previous: snoozeRecord.nullable() })).min(1).max(MAX_BULK) }),
z.object({ action: z.literal("acknowledge"), alertIds: stringIds, note: z.string().max(MAX_NOTE).default(""), assigneeId: z.number().int().positive().nullable().default(null), hours: z.number().int().min(1).max(MAX_HOURS).nullable().default(null) }),
z.object({ action: z.literal("unacknowledge"), alertIds: stringIds }),
z.object({ action: z.literal("restore_acks"), items: z.array(z.object({ alertId: z.string().min(1).max(600), previous: previousAck })).min(1).max(MAX_BULK) }),
@@ -132,6 +139,23 @@ alertsBulkRouter.post("/", asyncHandler(async (req, res) => {
await recordAudit({ actor, category: "alert", action: "bulk_undo", detail: { changed } });
return res.json({ changed, skipped: [], undo: null });
}
case "snooze": {
const until = new Date(body.until);
if (Number.isNaN(until.getTime())) return bad(res, "Say when they should come back.");
const r = await snoozeAlerts(body.alertIds, until, nameOf(req));
await recordAudit({ actor, category: "alert", action: "bulk_snooze", detail: { changed: r.changed, until: until.toISOString(), alerts: detailIds(body.alertIds) } });
return res.json(r);
}
case "unsnooze": {
const r = await unsnoozeAlerts(body.alertIds);
await recordAudit({ actor, category: "alert", action: "bulk_unsnooze", detail: { changed: r.changed, alerts: detailIds(body.alertIds) } });
return res.json(r);
}
case "restore_snoozes": {
const changed = await restoreSnoozes(body.items);
await recordAudit({ actor, category: "alert", action: "bulk_undo", detail: { changed, what: "snooze" } });
return res.json({ changed, skipped: [], undo: null });
}
case "silence": {
const { targets, skipped } = await targetsOfAlerts(body.alertIds);
const r = await silenceTargets(targets, body.minutes, body.reason || "Maintenance, from several alerts", nameOf(req), false);
@@ -145,7 +169,7 @@ alertsBulkRouter.post("/", asyncHandler(async (req, res) => {
}
}
} catch (err) {
if (err instanceof AckError) return res.status(err.status).json({ error: err.code, message: err.message });
if (err instanceof AckError || err instanceof SnoozeError) return res.status(err.status).json({ error: err.code, message: err.message });
throw err;
}
}));
+83
View File
@@ -0,0 +1,83 @@
import express, { Router, type NextFunction, type Request, type Response } from "express";
import { z } from "zod";
import { requireAuth, requireRole } from "../auth/middleware.js";
import { recordAudit } from "../services/audit.js";
import { addPhoto, getPhoto, listPhotos, MAX_PHOTO_BYTES, PhotoError, removePhoto, setCaption } from "../services/notePhotos.js";
import { TARGET_TYPES, targetName } from "../services/todos.js";
import { asyncHandler } from "../utils/asyncHandler.js";
/** Photos on the notes of servers and devices: everyone signed in sees them; operators add, caption and remove. */
export const notePhotosRouter = Router();
notePhotosRouter.use(requireAuth);
const idOf = (v: string) => (/^\d+$/.test(v) ? Number(v) : NaN);
const label = (u: { name: string | null; email: string | null; oidcSub: string }) => u.name ?? u.email ?? u.oidcSub;
notePhotosRouter.get("/", asyncHandler(async (req, res) => {
const type = z.enum(TARGET_TYPES).safeParse(req.query.targetType);
const id = idOf(String(req.query.targetId ?? ""));
if (!type.success || !Number.isInteger(id)) return res.status(400).json({ error: "invalid_target" });
res.json({ photos: await listPhotos(type.data, id) });
}));
// The picture itself. What it is is what its bytes say (it was checked when it was added), it cannot run anything — the sandbox policy and nosniff see to that even if it were opened on its own —
// and it never changes, so the browser may keep it.
notePhotosRouter.get("/:id/image", asyncHandler(async (req, res) => {
const id = idOf(req.params.id);
if (!Number.isInteger(id)) return res.status(404).json({ error: "not_found" });
const photo = await getPhoto(id);
if (!photo) return res.status(404).json({ error: "not_found" });
res.setHeader("Content-Type", photo.info.contentType);
res.setHeader("Content-Length", String(photo.data.length));
res.setHeader("X-Content-Type-Options", "nosniff");
res.setHeader("Content-Security-Policy", "default-src 'none'; sandbox");
res.setHeader("Cache-Control", "private, max-age=86400, immutable");
res.send(photo.data);
}));
// The bytes of the picture are the body, whatever it says its type is (the first bytes decide); the target and the caption are in the address.
notePhotosRouter.post(
"/",
requireRole("operator"),
express.raw({ type: () => true, limit: MAX_PHOTO_BYTES + 1024 }),
asyncHandler(async (req, res) => {
const type = z.enum(TARGET_TYPES).safeParse(req.query.targetType);
const id = idOf(String(req.query.targetId ?? ""));
if (!type.success || !Number.isInteger(id)) return res.status(400).json({ error: "invalid_target" });
const caption = typeof req.query.caption === "string" ? req.query.caption : null;
const body = Buffer.isBuffer(req.body) ? req.body : Buffer.alloc(0);
try {
const actor = req.currentUser!;
const photo = await addPhoto(type.data, id, body, caption, label(actor));
await recordAudit({ actor, category: "note", action: "photo_add", targetType: type.data, targetId: id, detail: { name: await targetName(type.data, id), photoId: photo.id, size: photo.size, caption: photo.caption } });
res.status(201).json({ photo });
} catch (err) {
if (err instanceof PhotoError) return res.status(err.status).json({ error: err.code, message: err.message });
throw err;
}
}),
);
notePhotosRouter.put("/:id", requireRole("operator"), asyncHandler(async (req, res) => {
const id = idOf(req.params.id);
const parsed = z.object({ caption: z.string().max(500).nullable() }).safeParse(req.body);
if (!Number.isInteger(id) || !parsed.success) return res.status(400).json({ error: "invalid_body" });
const photo = await setCaption(id, parsed.data.caption);
if (!photo) return res.status(404).json({ error: "not_found" });
res.json({ photo });
}));
notePhotosRouter.delete("/:id", requireRole("operator"), asyncHandler(async (req, res) => {
const id = idOf(req.params.id);
if (!Number.isInteger(id)) return res.status(400).json({ error: "invalid_id" });
const removed = await removePhoto(id);
if (!removed) return res.status(404).json({ error: "not_found" });
await recordAudit({ actor: req.currentUser!, category: "note", action: "photo_delete", targetType: removed.targetType, targetId: removed.targetId, detail: { name: await targetName(removed.targetType, removed.targetId), photoId: id } });
res.status(204).end();
}));
// An upload over the limit is told plainly, not with the framework's page.
notePhotosRouter.use((err: Error & { type?: string }, _req: Request, res: Response, next: NextFunction) => {
if (err.type === "entity.too.large") return res.status(413).json({ error: "too_big", message: `A photo can be at most ${MAX_PHOTO_BYTES / 1024 / 1024} MB.` });
next(err);
});