First of the six planned live integrations (Proxmox, Synology, Semaphore, Tailscale, Gitea, Dockhand), reusing Sloth Manager's existing Tailscale adapter logic. Generalizes the "integrations" table already scaffolded in the foundation pass into a working config-in-UI + encrypted-credentials flow, following the same pattern as the DNS providers module — an "Add integration" form only offers types with an implemented adapter (currently just Tailscale), so the framework is ready for the next five integrations without further schema/plumbing changes. - server/src/integrations/tailscale/adapter.ts: ported from Sloth Manager's backend/src/adapters/tailscale.js — listDevices/setAuthorized/deleteDevice against the Tailscale API, now config-based (tailnet + apiKey) instead of reading process.env, and returning a ping() result instead of throwing. - server/src/routes/integrations.ts: generic integration CRUD (admin) + a "test connection" endpoint, plus Tailscale-specific device routes (dashboard-and-basic-actions depth per the plan: authorize/deauthorize/ remove, gated to operator+, audit-logged). - web: an Integrations page (provider-style manage/browse split, matching the DNS page's UX) with a device table, and a live Tailscale widget on the Dashboard. Bug found and fixed while testing: hitting the Tailscale device routes on a non-Tailscale integration row crashed the ENTIRE server process, not just that request — the generic adapter registry throws for unimplemented types, and that throw happened inside an async handler with no surrounding try/catch, which Express 4 doesn't catch, so it became an unhandled rejection that (on modern Node) kills the process. Fixed at the source (check the row's type before ever constructing an adapter) and, since the same "a helper throws before any local try/catch runs" shape existed wherever a route calls into loadDnsProviderConfig/loadIntegrationConfig (both call decryptSecret, which throws if CREDENTIALS_ENCRYPTION_KEY is ever wrong/missing after data was already encrypted with a different key), added a small asyncHandler() wrapper and applied it to every route handler across every router — a single bad request should never be able to take the whole app down for every user. Verified: full build passes. Fresh HTTP-layer tests against a running server (17 checks) cover not-implemented-type rejection, missing-field validation, a real network call to api.tailscale.com with a bogus key (clean ok:false, not a crash), role gating at every tier, credential non-leakage, disabled-integration blocking, and the wrong_type case that originally crashed the server — confirmed it now returns 400 cleanly and the server stays up. Re-ran the existing DNS (13 checks) and Servers/Tasks suites afterward to confirm the asyncHandler sweep didn't regress anything — all passing. Authorizing/removing a real device still needs a real Tailscale API key to verify end-to-end. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
178 lines
6.0 KiB
TypeScript
178 lines
6.0 KiB
TypeScript
import { Router } from "express";
|
|
import { and, eq, like, or } from "drizzle-orm";
|
|
import { z } from "zod";
|
|
import { db } from "../db/client.js";
|
|
import { scheduledTasks, servers } from "../db/schema.js";
|
|
import { requireAuth, requireRole } from "../auth/middleware.js";
|
|
import { recordAudit } from "../services/audit.js";
|
|
import { asyncHandler } from "../utils/asyncHandler.js";
|
|
|
|
export const tasksRouter = Router();
|
|
tasksRouter.use(requireAuth);
|
|
|
|
const TASK_COLUMNS = {
|
|
id: scheduledTasks.id,
|
|
serverId: scheduledTasks.serverId,
|
|
serverName: servers.name,
|
|
scheduleType: scheduledTasks.scheduleType,
|
|
origin: scheduledTasks.origin,
|
|
name: scheduledTasks.name,
|
|
command: scheduledTasks.command,
|
|
scheduleExpression: scheduledTasks.scheduleExpression,
|
|
source: scheduledTasks.source,
|
|
enabled: scheduledTasks.enabled,
|
|
nextRunAt: scheduledTasks.nextRunAt,
|
|
isStale: scheduledTasks.isStale,
|
|
firstSeenAt: scheduledTasks.firstSeenAt,
|
|
lastSeenAt: scheduledTasks.lastSeenAt,
|
|
};
|
|
|
|
tasksRouter.get("/", asyncHandler(async (req, res) => {
|
|
const serverId = req.query.serverId ? Number(req.query.serverId) : undefined;
|
|
const scheduleType = typeof req.query.scheduleType === "string" ? req.query.scheduleType : undefined;
|
|
const search = typeof req.query.search === "string" ? req.query.search.trim() : undefined;
|
|
const includeStale = req.query.includeStale === "true";
|
|
|
|
const conditions = [];
|
|
if (serverId && Number.isInteger(serverId)) {
|
|
conditions.push(eq(scheduledTasks.serverId, serverId));
|
|
}
|
|
if (scheduleType) {
|
|
conditions.push(eq(scheduledTasks.scheduleType, scheduleType));
|
|
}
|
|
if (!includeStale) {
|
|
conditions.push(eq(scheduledTasks.isStale, false));
|
|
}
|
|
if (search) {
|
|
const pattern = `%${search}%`;
|
|
conditions.push(or(like(scheduledTasks.name, pattern), like(scheduledTasks.command, pattern)));
|
|
}
|
|
|
|
const rows = await db
|
|
.select(TASK_COLUMNS)
|
|
.from(scheduledTasks)
|
|
.innerJoin(servers, eq(scheduledTasks.serverId, servers.id))
|
|
.where(conditions.length > 0 ? and(...conditions) : undefined)
|
|
.orderBy(servers.name, scheduledTasks.scheduleType, scheduledTasks.name);
|
|
|
|
res.json({ tasks: rows });
|
|
}));
|
|
|
|
const manualTaskSchema = z.object({
|
|
serverId: z.number().int(),
|
|
scheduleType: z.enum(["cron", "systemd_timer", "docker", "backup", "update", "n8n_workflow", "manual"]),
|
|
name: z.string().min(1).max(200),
|
|
command: z.string().max(1000).optional(),
|
|
scheduleExpression: z.string().max(200).optional(),
|
|
nextRunAt: z.string().optional(),
|
|
enabled: z.boolean().optional(),
|
|
});
|
|
|
|
tasksRouter.post("/", requireRole("operator"), asyncHandler(async (req, res) => {
|
|
const parsed = manualTaskSchema.safeParse(req.body);
|
|
if (!parsed.success) {
|
|
return res.status(400).json({ error: "invalid_body", details: parsed.error.flatten() });
|
|
}
|
|
|
|
const server = await db.query.servers.findFirst({ where: eq(servers.id, parsed.data.serverId) });
|
|
if (!server) {
|
|
return res.status(400).json({ error: "unknown_server" });
|
|
}
|
|
|
|
const now = new Date().toISOString();
|
|
const [created] = await db
|
|
.insert(scheduledTasks)
|
|
.values({
|
|
serverId: parsed.data.serverId,
|
|
scheduleType: parsed.data.scheduleType,
|
|
origin: "manual",
|
|
name: parsed.data.name,
|
|
command: parsed.data.command,
|
|
scheduleExpression: parsed.data.scheduleExpression,
|
|
nextRunAt: parsed.data.nextRunAt,
|
|
enabled: parsed.data.enabled ?? true,
|
|
firstSeenAt: now,
|
|
lastSeenAt: now,
|
|
})
|
|
.returning({ id: scheduledTasks.id });
|
|
|
|
await recordAudit({
|
|
actor: req.currentUser!,
|
|
category: "task",
|
|
action: "create",
|
|
targetType: "scheduled_task",
|
|
targetId: created.id,
|
|
detail: { name: parsed.data.name, serverId: parsed.data.serverId },
|
|
});
|
|
|
|
const [task] = await db
|
|
.select(TASK_COLUMNS)
|
|
.from(scheduledTasks)
|
|
.innerJoin(servers, eq(scheduledTasks.serverId, servers.id))
|
|
.where(eq(scheduledTasks.id, created.id));
|
|
res.status(201).json({ task });
|
|
}));
|
|
|
|
const manualTaskUpdateSchema = manualTaskSchema.omit({ serverId: true }).partial();
|
|
|
|
tasksRouter.patch("/:id", requireRole("operator"), asyncHandler(async (req, res) => {
|
|
const id = Number(req.params.id);
|
|
if (!Number.isInteger(id)) return res.status(400).json({ error: "invalid_id" });
|
|
|
|
const parsed = manualTaskUpdateSchema.safeParse(req.body);
|
|
if (!parsed.success) {
|
|
return res.status(400).json({ error: "invalid_body", details: parsed.error.flatten() });
|
|
}
|
|
|
|
const existing = await db.query.scheduledTasks.findFirst({ where: eq(scheduledTasks.id, id) });
|
|
if (!existing) return res.status(404).json({ error: "not_found" });
|
|
if (existing.origin !== "manual") {
|
|
return res.status(403).json({ error: "not_editable", message: "Only manually entered tasks can be edited." });
|
|
}
|
|
|
|
await db
|
|
.update(scheduledTasks)
|
|
.set({ ...parsed.data, lastSeenAt: new Date().toISOString() })
|
|
.where(eq(scheduledTasks.id, id));
|
|
|
|
await recordAudit({
|
|
actor: req.currentUser!,
|
|
category: "task",
|
|
action: "update",
|
|
targetType: "scheduled_task",
|
|
targetId: id,
|
|
detail: { name: existing.name },
|
|
});
|
|
|
|
const [task] = await db
|
|
.select(TASK_COLUMNS)
|
|
.from(scheduledTasks)
|
|
.innerJoin(servers, eq(scheduledTasks.serverId, servers.id))
|
|
.where(eq(scheduledTasks.id, id));
|
|
res.json({ task });
|
|
}));
|
|
|
|
tasksRouter.delete("/:id", requireRole("operator"), asyncHandler(async (req, res) => {
|
|
const id = Number(req.params.id);
|
|
if (!Number.isInteger(id)) return res.status(400).json({ error: "invalid_id" });
|
|
|
|
const existing = await db.query.scheduledTasks.findFirst({ where: eq(scheduledTasks.id, id) });
|
|
if (!existing) return res.status(404).json({ error: "not_found" });
|
|
if (existing.origin !== "manual") {
|
|
return res.status(403).json({ error: "not_editable", message: "Only manually entered tasks can be deleted." });
|
|
}
|
|
|
|
await db.delete(scheduledTasks).where(eq(scheduledTasks.id, id));
|
|
|
|
await recordAudit({
|
|
actor: req.currentUser!,
|
|
category: "task",
|
|
action: "delete",
|
|
targetType: "scheduled_task",
|
|
targetId: id,
|
|
detail: { name: existing.name },
|
|
});
|
|
|
|
res.status(204).end();
|
|
}));
|