With Settings -> Alert tickets switched on (off by default), every 15 minutes the problems on the Alerts page are looked at and one that is new and has lasted gets one ticket in osTicket, through its API (POST api/tickets.json, key in X-API-Key). One problem opens one ticket however many checks see it; the app remembers what it has seen in a new alert_tickets table (migration 0015). - Which: critical and warning by default, every kind; severities and kinds are settings. - A problem must be seen in 2 checks in a row first (1-4, a setting), so a blip that fixes itself, like one failed call to an integration, never opens a ticket. - One ticket per occurrence; a problem that clears and returns within 60 minutes is the same one. - More than 5 new problems in one check become a single summary ticket. - Never for alerts under a maintenance window, nor about osTicket itself or its overdue tickets. - Problems already there when it is switched on are left alone by default (the first check records them as a baseline); can be set to ticket them too. - If osTicket can't be reached nothing is recorded, so the next check retries; if a source couldn't be read, a problem isn't treated as gone. Every ticket is audited. The ticket API address, key (stored encrypted) and self-signed option are optional fields on the osTicket integration, which otherwise still only reads the database. The settings page can send a test ticket and run a check now; the Alerts page shows each alert's ticket number, linked to osTicket. osTicket's API can't close or update a ticket, so a ticket opened here is the end of this app's part in it. Tested against a stand-in osTicket, not a real one. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
115 lines
4.7 KiB
TypeScript
115 lines
4.7 KiB
TypeScript
import express from "express";
|
|
import session from "express-session";
|
|
import FileStoreFactory from "session-file-store";
|
|
import { fileURLToPath } from "node:url";
|
|
import { dirname, join } from "node:path";
|
|
import { mkdirSync, existsSync } from "node:fs";
|
|
import { env } from "./env.js";
|
|
import { resolveDataPath } from "./paths.js";
|
|
import { authRouter } from "./auth/router.js";
|
|
import { meRouter } from "./routes/me.js";
|
|
import { usersRouter } from "./routes/users.js";
|
|
import { auditLogRouter } from "./routes/auditLog.js";
|
|
import { diagLogRouter } from "./routes/diagLog.js";
|
|
import { secretsRouter } from "./routes/secrets.js";
|
|
import { ipamRouter } from "./routes/ipam.js";
|
|
import { dnsRouter } from "./routes/dns.js";
|
|
import { serversRouter } from "./routes/servers.js";
|
|
import { tasksRouter } from "./routes/tasks.js";
|
|
import { agentReportRouter } from "./routes/agentReport.js";
|
|
import { integrationsRouter } from "./routes/integrations.js";
|
|
import { settingsRouter } from "./routes/settings.js";
|
|
import { searchRouter } from "./routes/search.js";
|
|
import { sessionsRouter } from "./routes/sessions.js";
|
|
import { maintenanceRouter } from "./routes/maintenance.js";
|
|
import { domainsRouter } from "./routes/domains.js";
|
|
import { consistencyRouter } from "./routes/consistency.js";
|
|
import { privacyRouter } from "./routes/privacy.js";
|
|
import { tagsRouter } from "./routes/tags.js";
|
|
import { portsRouter } from "./routes/ports.js";
|
|
import { alertsRouter } from "./routes/alerts.js";
|
|
import { generatorRouter } from "./routes/generator.js";
|
|
import { alertTicketsRouter } from "./routes/alertTickets.js";
|
|
import { apiErrorHandler } from "./utils/apiErrorHandler.js";
|
|
import { securityHeaders } from "./utils/securityHeaders.js";
|
|
import { sameOriginGuard } from "./utils/sameOriginGuard.js";
|
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
const webDist = join(__dirname, "..", "..", "web", "dist");
|
|
const agentDir = join(__dirname, "..", "..", "agent");
|
|
|
|
/**
|
|
* The whole web application, ready to listen. It lives apart from index.ts (which also runs migrations and starts the
|
|
* schedulers) so the tests can run the real thing — the order of the middleware below is part of what they check.
|
|
*/
|
|
export function createApp() {
|
|
const FileStore = FileStoreFactory(session);
|
|
const sessionDir = resolveDataPath(env.sessionDir);
|
|
mkdirSync(sessionDir, { recursive: true });
|
|
|
|
const app = express();
|
|
app.disable("x-powered-by");
|
|
app.set("trust proxy", 1);
|
|
app.use(securityHeaders);
|
|
// The agent endpoint comes first: it reads its own (larger) body, only after checking the token, and an agent has no use for a
|
|
// session. It is also the one place a request isn't from a browser, so it isn't subject to the same-origin check below.
|
|
app.use("/api/agent/report", agentReportRouter);
|
|
// Everything after this is reached with a session cookie, so a request that came from another origin is refused here.
|
|
app.use(sameOriginGuard);
|
|
app.use(express.json());
|
|
app.use(
|
|
session({
|
|
store: new FileStore({ path: sessionDir, logFn: () => {} }),
|
|
secret: env.sessionSecret,
|
|
resave: false,
|
|
saveUninitialized: false,
|
|
cookie: {
|
|
httpOnly: true,
|
|
sameSite: "lax",
|
|
secure: env.nodeEnv === "production",
|
|
maxAge: 7 * 24 * 60 * 60 * 1000,
|
|
},
|
|
}),
|
|
);
|
|
|
|
app.get("/health", (_req, res) => res.json({ ok: true }));
|
|
|
|
// Publicly readable so `curl .../agent/linux/install.sh | bash` works from a
|
|
// freshly provisioned server with no prior session. Contains no secrets.
|
|
if (existsSync(agentDir)) {
|
|
app.use("/agent", express.static(agentDir));
|
|
}
|
|
|
|
app.use("/auth", authRouter);
|
|
app.use("/api/me", meRouter);
|
|
app.use("/api/users", usersRouter);
|
|
app.use("/api/audit-log", auditLogRouter);
|
|
app.use("/api/diag-log", diagLogRouter);
|
|
app.use("/api/secrets", secretsRouter);
|
|
app.use("/api/ipam", ipamRouter);
|
|
app.use("/api/dns", dnsRouter);
|
|
app.use("/api/servers", serversRouter);
|
|
app.use("/api/tasks", tasksRouter);
|
|
app.use("/api/integrations", integrationsRouter);
|
|
app.use("/api/settings", settingsRouter);
|
|
app.use("/api/search", searchRouter);
|
|
app.use("/api/sessions", sessionsRouter);
|
|
app.use("/api/maintenance", maintenanceRouter);
|
|
app.use("/api/domains", domainsRouter);
|
|
app.use("/api/consistency", consistencyRouter);
|
|
app.use("/api/privacy", privacyRouter);
|
|
app.use("/api/tags", tagsRouter);
|
|
app.use("/api/ports", portsRouter);
|
|
app.use("/api/alerts", alertsRouter);
|
|
app.use("/api/generator", generatorRouter);
|
|
app.use("/api/alert-tickets", alertTicketsRouter);
|
|
|
|
if (existsSync(webDist)) {
|
|
app.use(express.static(webDist));
|
|
app.get("*", (_req, res) => res.sendFile(join(webDist, "index.html")));
|
|
}
|
|
|
|
app.use(apiErrorHandler);
|
|
return app;
|
|
}
|