Authentik: know when the front door is about to shut
New Authentik integration (read-only, every 5 minutes): version, workers, outposts and their health, certificates and API tokens that expire, failed sign-ins and configuration errors. Monitoring -> Authentik page. Alerts: an outpost that serves apps has gone quiet (critical), certificate or token about to run out (critical close to it), no worker, failed sign-ins, configuration errors. Held by maintenance, held when unreadable, in /metrics. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0146eb8bca
commit
aebe98bf15
33 files changed
+8050
-7
No files matched your search
@@ -0,0 +1,15 @@
|
||||
CREATE TABLE `authentik_state` (
|
||||
`integration_id` integer PRIMARY KEY NOT NULL,
|
||||
`version` text,
|
||||
`latest_version` text,
|
||||
`outdated` integer DEFAULT false NOT NULL,
|
||||
`workers` integer,
|
||||
`outposts` text DEFAULT '[]' NOT NULL,
|
||||
`certificates` text DEFAULT '[]' NOT NULL,
|
||||
`tokens` text DEFAULT '[]' NOT NULL,
|
||||
`failed_logins_24h` integer,
|
||||
`config_errors_24h` integer,
|
||||
`last_config_error` text,
|
||||
`updated_at` text NOT NULL,
|
||||
FOREIGN KEY (`integration_id`) REFERENCES `integrations`(`id`) ON UPDATE no action ON DELETE cascade
|
||||
);
|
||||
File diff suppressed because it is too large.
Load diff
@@ -407,6 +407,13 @@
|
||||
"when": 1792870755473,
|
||||
"tag": "0057_lame_cloak",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 58,
|
||||
"version": "6",
|
||||
"when": 1792872166034,
|
||||
"tag": "0058_flippant_sasquatch",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -59,6 +59,7 @@ import { upsRouter } from "./routes/ups.js";
|
||||
import { awayRouter } from "./routes/away.js";
|
||||
import { duplicatiReportRouter, duplicatiRouter } from "./routes/duplicati.js";
|
||||
import { jellyfinRouter } from "./routes/jellyfin.js";
|
||||
import { authentikRouter } from "./routes/authentik.js";
|
||||
import { serviceLevelsRouter } from "./routes/serviceLevels.js";
|
||||
import { publicApiRouter } from "./routes/publicApi.js";
|
||||
import { generatorRouter } from "./routes/generator.js";
|
||||
@@ -195,6 +196,7 @@ export function createApp() {
|
||||
app.use("/api/away", awayRouter);
|
||||
app.use("/api/duplicati", duplicatiRouter);
|
||||
app.use("/api/jellyfin", jellyfinRouter);
|
||||
app.use("/api/authentik", authentikRouter);
|
||||
app.use("/api/service-levels", serviceLevelsRouter);
|
||||
app.use("/api/todos", todosRouter);
|
||||
app.use("/api/notes", notesRouter);
|
||||
|
||||
@@ -554,6 +554,7 @@ export const integrationTypes = [
|
||||
"homeassistant",
|
||||
"ups",
|
||||
"jellyfin",
|
||||
"authentik",
|
||||
] as const;
|
||||
export type IntegrationType = (typeof integrationTypes)[number];
|
||||
|
||||
@@ -1160,6 +1161,27 @@ export const haReadings = sqliteTable(
|
||||
(t) => [index("ha_readings_lookup_idx").on(t.integrationId, t.entityId, t.at)],
|
||||
);
|
||||
|
||||
// ─── Authentik ──────────────────────────────────────────────────────────────
|
||||
|
||||
// How an Authentik (an integration of type "authentik") is right now, one row each, replaced at every read: its version, whether a worker is connected, how each outpost is, the
|
||||
// certificates and tokens that run out, and how many sign-ins and flows failed lately. Read by services/authentikSync.ts.
|
||||
export const authentikState = sqliteTable("authentik_state", {
|
||||
integrationId: integer("integration_id")
|
||||
.primaryKey()
|
||||
.references(() => integrations.id, { onDelete: "cascade" }),
|
||||
version: text("version"),
|
||||
latestVersion: text("latest_version"),
|
||||
outdated: integer("outdated", { mode: "boolean" }).notNull().default(false),
|
||||
workers: integer("workers"), // null: could not be asked
|
||||
outposts: text("outposts").notNull().default("[]"), // JSON: name, type, providers, healthy, instances, outdated, lastSeen
|
||||
certificates: text("certificates").notNull().default("[]"), // JSON: name, expires
|
||||
tokens: text("tokens").notNull().default("[]"), // JSON: identifier, intent, user, expires
|
||||
failedLogins24h: integer("failed_logins_24h"),
|
||||
configErrors24h: integer("config_errors_24h"),
|
||||
lastConfigError: text("last_config_error"),
|
||||
updatedAt: text("updated_at").notNull(), // ISO
|
||||
});
|
||||
|
||||
// ─── Jellyfin ───────────────────────────────────────────────────────────────
|
||||
|
||||
// How a Jellyfin server (an integration of type "jellyfin") is right now, one row each, replaced at every read: its version, whether it wants a restart, who is streaming, how its
|
||||
|
||||
@@ -30,6 +30,7 @@ import { initHomeAssistantSync } from "./services/homeAssistantSync.js";
|
||||
import { initUpsSync } from "./services/upsSync.js";
|
||||
import { initAwayMonitor } from "./services/awayMonitor.js";
|
||||
import { initJellyfinSync } from "./services/jellyfinSync.js";
|
||||
import { initAuthentikSync } from "./services/authentikSync.js";
|
||||
import { backfillMetricRollups } from "./services/serverMetrics.js";
|
||||
import { backfillEndpointHourly } from "./services/endpointChecks.js";
|
||||
import { initSelfBackupScheduler } from "./services/selfBackupScheduler.js";
|
||||
@@ -81,6 +82,7 @@ initHomeAssistantSync();
|
||||
initUpsSync();
|
||||
initAwayMonitor();
|
||||
initJellyfinSync();
|
||||
initAuthentikSync();
|
||||
// The hourly and daily figures are built from what is already kept, once, the first time after they were added (in the background: nothing waits for it).
|
||||
void Promise.all([backfillMetricRollups(), backfillEndpointHourly()]).catch((err) => console.error("[history] couldn't build the hourly and daily figures:", err));
|
||||
|
||||
|
||||
@@ -0,0 +1,218 @@
|
||||
/**
|
||||
* Authentik adapter. Config: url, token (an API token made in Authentik under Directory → Tokens and App passwords, intent "API Token"); optional: insecure.
|
||||
*
|
||||
* Reading only: Authentik's version (and whether a newer one exists), whether a worker is connected, how each outpost is (the proxy outpost that guards your apps, an LDAP one) —
|
||||
* how many instances report in and whether they are the right version — the certificates and their expiry, the API tokens and app passwords that expire, and how many sign-ins and
|
||||
* flows failed lately (from the events log). Nothing is sent to Authentik but GET requests for these.
|
||||
*
|
||||
* Endpoints and field names are from Authentik's published API (`/api/v3/admin/version/`, `/admin/workers/`, `/outposts/instances/` and its `/health/`, `/crypto/certificatekeypairs/`,
|
||||
* `/core/tokens/`, `/events/events/`) and have not been checked against a live server here; every field is read defensively, and a part that cannot be read is left out.
|
||||
*/
|
||||
import { requestJson } from "../httpJson.js";
|
||||
import { withDiagLogging } from "../../services/diagLog.js";
|
||||
|
||||
export interface AuthentikConfig {
|
||||
url: string;
|
||||
token: string;
|
||||
insecure?: boolean;
|
||||
}
|
||||
|
||||
export interface AuthentikOutpost {
|
||||
name: string;
|
||||
/** proxy, ldap, radius, rac. */
|
||||
type: string;
|
||||
/** How many applications (providers) it serves; one with none has nothing to do and is not worth an alert. */
|
||||
providers: number;
|
||||
/** Instances that have reported in lately, and all that ever did. */
|
||||
healthy: number;
|
||||
instances: number;
|
||||
/** Any instance running a different version from the one it should. */
|
||||
outdated: boolean;
|
||||
lastSeen: string | null;
|
||||
}
|
||||
|
||||
export interface AuthentikCertificate {
|
||||
name: string;
|
||||
expires: string;
|
||||
}
|
||||
|
||||
export interface AuthentikToken {
|
||||
identifier: string;
|
||||
/** api, app_password, … */
|
||||
intent: string;
|
||||
user: string | null;
|
||||
expires: string;
|
||||
}
|
||||
|
||||
export interface AuthentikRead {
|
||||
version: string | null;
|
||||
latestVersion: string | null;
|
||||
outdated: boolean;
|
||||
/** Null when the workers could not be asked — which is not the same as none. */
|
||||
workers: number | null;
|
||||
outposts: AuthentikOutpost[];
|
||||
certificates: AuthentikCertificate[];
|
||||
tokens: AuthentikToken[];
|
||||
/** In the last day; null when the events could not be read. A count of 100 means "at least". */
|
||||
failedLogins24h: number | null;
|
||||
configErrors24h: number | null;
|
||||
lastConfigError: string | null;
|
||||
}
|
||||
|
||||
export interface AuthentikAdapter {
|
||||
ping(): Promise<{ ok: boolean; latencyMs?: number; error?: string }>;
|
||||
read(now?: Date): Promise<AuthentikRead>;
|
||||
}
|
||||
|
||||
/** An outpost that reported in within this long counts as connected. */
|
||||
export const OUTPOST_SEEN_WITHIN_MS = 5 * 60_000;
|
||||
const PAGE = 200;
|
||||
|
||||
const obj = (v: unknown): Record<string, unknown> => (v !== null && typeof v === "object" && !Array.isArray(v) ? (v as Record<string, unknown>) : {});
|
||||
const str = (v: unknown): string | null => (typeof v === "string" && v.trim() !== "" ? v.trim() : null);
|
||||
const iso = (v: unknown): string | null => {
|
||||
const s = str(v);
|
||||
return s && Number.isFinite(Date.parse(s)) ? new Date(s).toISOString() : null;
|
||||
};
|
||||
/** Authentik lists come as `{ pagination, results }`; a bare array is read too. */
|
||||
const results = (raw: unknown): unknown[] => (Array.isArray(raw) ? raw : Array.isArray(obj(raw).results) ? (obj(raw).results as unknown[]) : []);
|
||||
|
||||
/** Pure: what /admin/version says. */
|
||||
export function parseVersion(raw: unknown): Pick<AuthentikRead, "version" | "latestVersion" | "outdated"> {
|
||||
const o = obj(raw);
|
||||
return { version: str(o.version_current), latestVersion: str(o.version_latest), outdated: o.outdated === true };
|
||||
}
|
||||
|
||||
/** Pure: how many workers answered; null when the answer is not a list. */
|
||||
export function parseWorkers(raw: unknown): number | null {
|
||||
const list = Array.isArray(raw) ? raw : Array.isArray(obj(raw).results) ? (obj(raw).results as unknown[]) : null;
|
||||
return list ? list.length : null;
|
||||
}
|
||||
|
||||
/** Pure: an outpost with the health its instances reported. */
|
||||
export function parseOutpost(raw: unknown, health: unknown, now: Date): AuthentikOutpost | null {
|
||||
const o = obj(raw);
|
||||
const name = str(o.name);
|
||||
if (!name) return null;
|
||||
const instances = Array.isArray(health) ? health.map(obj) : [];
|
||||
const seen = instances.map((i) => iso(i.last_seen)).filter((t): t is string => t !== null);
|
||||
const healthy = seen.filter((t) => now.getTime() - Date.parse(t) <= OUTPOST_SEEN_WITHIN_MS).length;
|
||||
return {
|
||||
name,
|
||||
type: str(o.type) ?? "outpost",
|
||||
providers: Array.isArray(o.providers) ? o.providers.length : Array.isArray(o.providers_obj) ? o.providers_obj.length : 1,
|
||||
healthy,
|
||||
instances: instances.length,
|
||||
outdated: instances.some((i) => i.version_outdated === true),
|
||||
lastSeen: seen.sort().at(-1) ?? null,
|
||||
};
|
||||
}
|
||||
|
||||
/** Pure: the certificates that Authentik does not renew itself (a managed one is its own business), with when they run out. */
|
||||
export function parseCertificates(raw: unknown): AuthentikCertificate[] {
|
||||
const out: AuthentikCertificate[] = [];
|
||||
for (const item of results(raw)) {
|
||||
const c = obj(item);
|
||||
const name = str(c.name);
|
||||
const expires = iso(c.cert_expiry);
|
||||
if (!name || !expires || str(c.managed)) continue;
|
||||
out.push({ name, expires });
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Pure: the tokens that expire and are meant to last — API tokens and app passwords; a verification token lives for minutes and is no concern. */
|
||||
export function parseTokens(raw: unknown): AuthentikToken[] {
|
||||
const out: AuthentikToken[] = [];
|
||||
for (const item of results(raw)) {
|
||||
const t = obj(item);
|
||||
const identifier = str(t.identifier);
|
||||
const intent = str(t.intent);
|
||||
const expires = iso(t.expires);
|
||||
if (!identifier || !expires || t.expiring === false) continue;
|
||||
if (intent !== "api" && intent !== "app_password") continue;
|
||||
const user = obj(t.user_obj);
|
||||
out.push({ identifier, intent, user: str(user.username) ?? str(user.name), expires });
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Pure: how many events of the page are from the last day (the page is the newest first, so a full page of them means "at least"), and the newest one's words. */
|
||||
export function countRecentEvents(raw: unknown, now: Date): { count: number | null; latest: string | null } {
|
||||
if (!Array.isArray(obj(raw).results) && !Array.isArray(raw)) return { count: null, latest: null };
|
||||
const since = now.getTime() - 86_400_000;
|
||||
let count = 0;
|
||||
let latest: string | null = null;
|
||||
for (const item of results(raw)) {
|
||||
const e = obj(item);
|
||||
const at = Date.parse(String(e.created ?? ""));
|
||||
if (Number.isFinite(at) && at < since) continue;
|
||||
count += 1;
|
||||
if (latest === null) {
|
||||
const ctx = obj(e.context);
|
||||
latest = str(ctx.message) ?? str(ctx.error) ?? str(ctx.reason) ?? null;
|
||||
}
|
||||
}
|
||||
return { count, latest };
|
||||
}
|
||||
|
||||
export function createAuthentikAdapter(config: AuthentikConfig): AuthentikAdapter {
|
||||
const base = String(config.url ?? "").replace(/\/+$/, "");
|
||||
const insecure = config.insecure === true;
|
||||
const headers = { Authorization: `Bearer ${config.token}`, Accept: "application/json" };
|
||||
|
||||
async function get(path: string): Promise<unknown> {
|
||||
const res = await requestJson(`${base}/api/v3${path}`, { headers, insecure });
|
||||
if (res.status === 401 || res.status === 403) throw new Error(`Authentik did not accept the token (HTTP ${res.status}) — it needs to be an API token of a user who may read`);
|
||||
if (res.status === 404) throw new Error("Authentik has no API at this address — check the address");
|
||||
if (res.status < 200 || res.status >= 300) throw new Error(`Authentik error: HTTP ${res.status}`);
|
||||
if (res.json === null) throw new Error("Authentik answered with something that isn't JSON — is the address its own?");
|
||||
return res.json;
|
||||
}
|
||||
|
||||
async function read(now: Date = new Date()): Promise<AuthentikRead> {
|
||||
const info = parseVersion(await get("/admin/version/"));
|
||||
// The rest is worth having but not worth failing for: an Authentik that answers about itself and not about its tokens still says it is up.
|
||||
const soft = async <T>(path: string, parse: (raw: unknown) => T, fallback: T): Promise<T> => {
|
||||
try {
|
||||
return parse(await get(path));
|
||||
} catch {
|
||||
return fallback;
|
||||
}
|
||||
};
|
||||
const outposts = await soft(
|
||||
`/outposts/instances/?page_size=${PAGE}`,
|
||||
(raw) => raw,
|
||||
null as unknown,
|
||||
).then(async (raw) => {
|
||||
const list: AuthentikOutpost[] = [];
|
||||
for (const item of results(raw)) {
|
||||
const pk = str(obj(item).pk);
|
||||
const health = pk ? await soft(`/outposts/instances/${encodeURIComponent(pk)}/health/`, (r) => r, [] as unknown) : [];
|
||||
const o = parseOutpost(item, health, now);
|
||||
if (o) list.push(o);
|
||||
}
|
||||
return list;
|
||||
});
|
||||
const [workers, certificates, tokens, failed, configErrors] = await Promise.all([
|
||||
soft("/admin/workers/", parseWorkers, null as number | null),
|
||||
soft(`/crypto/certificatekeypairs/?page_size=${PAGE}&has_key=false`, parseCertificates, [] as AuthentikCertificate[]),
|
||||
soft(`/core/tokens/?page_size=${PAGE}`, parseTokens, [] as AuthentikToken[]),
|
||||
soft("/events/events/?action=login_failed&ordering=-created&page_size=100", (r) => countRecentEvents(r, now), { count: null, latest: null } as { count: number | null; latest: string | null }),
|
||||
soft("/events/events/?action=configuration_error&ordering=-created&page_size=100", (r) => countRecentEvents(r, now), { count: null, latest: null } as { count: number | null; latest: string | null }),
|
||||
]);
|
||||
return { ...info, workers, outposts, certificates, tokens, failedLogins24h: failed.count, configErrors24h: configErrors.count, lastConfigError: configErrors.latest };
|
||||
}
|
||||
|
||||
async function ping(): Promise<{ ok: boolean; latencyMs?: number; error?: string }> {
|
||||
const start = Date.now();
|
||||
try {
|
||||
await get("/admin/version/");
|
||||
return { ok: true, latencyMs: Date.now() - start };
|
||||
} catch (err) {
|
||||
return { ok: false, error: err instanceof Error ? err.message : String(err) };
|
||||
}
|
||||
}
|
||||
|
||||
return withDiagLogging("authentik", { ping, read });
|
||||
}
|
||||
@@ -99,6 +99,11 @@ export const INTEGRATION_FIELDS: Partial<Record<IntegrationType, IntegrationFiel
|
||||
{ key: "token", label: "Long-lived access token", secret: true, type: "password", placeholder: "made on your profile page in Home Assistant" },
|
||||
{ key: "insecure", label: "Allow self-signed certificate", secret: false, type: "checkbox" },
|
||||
],
|
||||
authentik: [
|
||||
{ key: "url", label: "Authentik URL", secret: false, placeholder: "https://auth.example.lan" },
|
||||
{ key: "token", label: "API token", secret: true, type: "password", placeholder: "Directory → Tokens and App passwords, intent API Token" },
|
||||
{ key: "insecure", label: "Allow self-signed certificate", secret: false, type: "checkbox" },
|
||||
],
|
||||
jellyfin: [
|
||||
{ key: "url", label: "Jellyfin URL", secret: false, placeholder: "http://jellyfin.example.lan:8096" },
|
||||
{ key: "apiKey", label: "API key", secret: true, type: "password", placeholder: "made under Dashboard → API Keys in Jellyfin" },
|
||||
|
||||
@@ -15,6 +15,7 @@ import { createNpmAdapter } from "./npm/adapter.js";
|
||||
import { createHomeAssistantAdapter } from "./homeassistant/adapter.js";
|
||||
import { createUpsAdapter } from "./ups/adapter.js";
|
||||
import { createJellyfinAdapter } from "./jellyfin/adapter.js";
|
||||
import { createAuthentikAdapter } from "./authentik/adapter.js";
|
||||
|
||||
export interface PingableAdapter {
|
||||
ping(): Promise<{ ok: boolean; latencyMs?: number; error?: string }>;
|
||||
@@ -59,6 +60,8 @@ export function createIntegrationAdapter(type: IntegrationType, config: Integrat
|
||||
return createUpsAdapter(config as any);
|
||||
case "jellyfin":
|
||||
return createJellyfinAdapter(config as any);
|
||||
case "authentik":
|
||||
return createAuthentikAdapter(config as any);
|
||||
default:
|
||||
throw new Error(`Integration type "${type}" is not implemented yet`);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
import { Router } from "express";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { db } from "../db/client.js";
|
||||
import { authentikState, integrations } from "../db/schema.js";
|
||||
import { requireAuth } from "../auth/middleware.js";
|
||||
import { authentikProblems, authentikSyncStatuses, certificatesOf, daysUntil, isAuthentikFresh, outpostsOf, tokensOf } from "../services/authentikSync.js";
|
||||
import { asyncHandler } from "../utils/asyncHandler.js";
|
||||
|
||||
export const authentikRouter = Router();
|
||||
authentikRouter.use(requireAuth);
|
||||
|
||||
/** Every Authentik, how it is, from what the last read stored: its outposts, certificates and tokens that run out, and what is wrong. Everyone signed in can see it, like the other pages made of stored readings. */
|
||||
authentikRouter.get("/", asyncHandler(async (_req, res) => {
|
||||
const [rows, states, statuses] = await Promise.all([
|
||||
db.select({ id: integrations.id, name: integrations.name, enabled: integrations.enabled }).from(integrations).where(eq(integrations.type, "authentik")).orderBy(integrations.name),
|
||||
db.select().from(authentikState),
|
||||
authentikSyncStatuses(),
|
||||
]);
|
||||
const byId = new Map(states.map((s) => [s.integrationId, s]));
|
||||
const now = new Date();
|
||||
res.json({
|
||||
authentiks: rows.map((r) => {
|
||||
const state = byId.get(r.id) ?? null;
|
||||
const status = statuses[r.id] ?? null;
|
||||
const fresh = isAuthentikFresh(status ?? undefined, now.getTime());
|
||||
return {
|
||||
id: r.id,
|
||||
name: r.name,
|
||||
enabled: r.enabled,
|
||||
status,
|
||||
fresh,
|
||||
state: state
|
||||
? {
|
||||
version: state.version,
|
||||
latestVersion: state.latestVersion,
|
||||
outdated: state.outdated,
|
||||
workers: state.workers,
|
||||
outposts: outpostsOf(state),
|
||||
certificates: certificatesOf(state).map((c) => ({ ...c, days: daysUntil(c.expires, now) })).sort((a, b) => a.days - b.days),
|
||||
tokens: tokensOf(state).map((t) => ({ ...t, days: daysUntil(t.expires, now) })).sort((a, b) => a.days - b.days),
|
||||
failedLogins24h: state.failedLogins24h,
|
||||
configErrors24h: state.configErrors24h,
|
||||
lastConfigError: state.lastConfigError,
|
||||
updatedAt: state.updatedAt,
|
||||
}
|
||||
: null,
|
||||
problems: state && fresh ? authentikProblems(r.id, r.name, state, now) : [],
|
||||
};
|
||||
}),
|
||||
});
|
||||
}));
|
||||
@@ -30,6 +30,7 @@ import { removeIntegrationData, syncUnifiIntegration } from "../services/unifiSy
|
||||
import { removeHomeAssistantData, syncHomeAssistant } from "../services/homeAssistantSync.js";
|
||||
import { forgetUps, syncUps } from "../services/upsSync.js";
|
||||
import { forgetJellyfin, syncJellyfin } from "../services/jellyfinSync.js";
|
||||
import { forgetAuthentik, syncAuthentik } from "../services/authentikSync.js";
|
||||
import { invalidateAlertsCache } from "../services/alerts.js";
|
||||
import { getSettings, updateSettings } from "../services/settingsStore.js";
|
||||
import { asyncHandler } from "../utils/asyncHandler.js";
|
||||
@@ -133,6 +134,7 @@ integrationsRouter.post("/", requireRole("admin"), asyncHandler(async (req, res)
|
||||
if (type === "homeassistant") void syncHomeAssistant(created.id).then(() => invalidateAlertsCache());
|
||||
if (type === "ups") void syncUps(created.id).then(() => invalidateAlertsCache());
|
||||
if (type === "jellyfin") void syncJellyfin(created.id).then(() => invalidateAlertsCache());
|
||||
if (type === "authentik") void syncAuthentik(created.id).then(() => invalidateAlertsCache());
|
||||
|
||||
res.status(201).json({
|
||||
integration: {
|
||||
@@ -237,6 +239,7 @@ integrationsRouter.patch("/:id", requireRole("admin"), asyncHandler(async (req,
|
||||
if (updated.type === "homeassistant" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncHomeAssistant(updated.id).then(() => invalidateAlertsCache());
|
||||
if (updated.type === "ups" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncUps(updated.id).then(() => invalidateAlertsCache());
|
||||
if (updated.type === "jellyfin" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncJellyfin(updated.id).then(() => invalidateAlertsCache());
|
||||
if (updated.type === "authentik" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncAuthentik(updated.id).then(() => invalidateAlertsCache());
|
||||
|
||||
res.json({
|
||||
integration: {
|
||||
@@ -317,6 +320,7 @@ integrationsRouter.delete("/:id", requireRole("admin"), asyncHandler(async (req,
|
||||
if (existing.type === "homeassistant") await removeHomeAssistantData(id);
|
||||
if (existing.type === "ups") await forgetUps(id);
|
||||
if (existing.type === "jellyfin") await forgetJellyfin(id);
|
||||
if (existing.type === "authentik") await forgetAuthentik(id);
|
||||
await db.delete(integrations).where(eq(integrations.id, id));
|
||||
if (existing.credentialId) {
|
||||
await db.delete(integrationCredentials).where(eq(integrationCredentials.id, existing.credentialId));
|
||||
|
||||
@@ -76,6 +76,7 @@ const SOURCES: Record<string, { label: string; link: string }> = {
|
||||
slo: { label: "Service level", link: "/service-levels" },
|
||||
duplicati: { label: "Duplicati", link: "/duplicati" },
|
||||
jellyfin: { label: "Jellyfin", link: "/jellyfin" },
|
||||
authentik: { label: "Authentik", link: "/authentik" },
|
||||
secrets: { label: "Secrets", link: "/secrets" },
|
||||
domains: { label: "Domains", link: "/dns?view=domains" },
|
||||
endpoint: { label: "Endpoint", link: "/endpoints" },
|
||||
@@ -114,6 +115,8 @@ function originOfConditionKey(key: string): { type: string; id: number | null; c
|
||||
if (m) return { type: "dockhand", id: Number(m[1]), category: "services" };
|
||||
m = /^duplicati-(?:failed|stale):(\d+)/.exec(key);
|
||||
if (m) return { type: "duplicati", id: Number(m[1]), category: "backup" };
|
||||
m = /^authentik-(?:outpost|outpost-version|cert|token|worker|logins|errors):(\d+)/.exec(key);
|
||||
if (m) return { type: "authentik", id: Number(m[1]), category: /^authentik-(?:cert|token):/.test(key) ? "expiry" : /^authentik-(?:outpost|worker)/.test(key) ? "services" : "monitoring" };
|
||||
m = /^jellyfin-(?:restart|task|logins):(\d+)/.exec(key);
|
||||
if (m) return { type: "jellyfin", id: Number(m[1]), category: key.startsWith("jellyfin-task") ? "automation" : "monitoring" };
|
||||
return { type: "server", id: null, category: "disk" };
|
||||
@@ -238,7 +241,7 @@ export async function collectAlerts(): Promise<AlertsReport> {
|
||||
type: origin.type,
|
||||
message: c.message,
|
||||
key: c.key,
|
||||
link: origin.type === "server" && origin.id !== null ? `/servers/${origin.id}${/^(failed-units|smart|zfs):/.test(c.key) ? "?view=health" : c.key.startsWith("new-port:") ? "?view=network" : c.key.startsWith("rule:") || c.key.startsWith("baseline:") ? "?view=history" : ""}` : origin.type === "unifi" ? "/network/devices" : origin.type === "ups" ? "/ups" : origin.type === "slo" ? "/service-levels" : origin.type === "dockhand" ? "/docker" : origin.type === "duplicati" ? "/duplicati" : origin.type === "jellyfin" ? "/jellyfin" : undefined,
|
||||
link: origin.type === "server" && origin.id !== null ? `/servers/${origin.id}${/^(failed-units|smart|zfs):/.test(c.key) ? "?view=health" : c.key.startsWith("new-port:") ? "?view=network" : c.key.startsWith("rule:") || c.key.startsWith("baseline:") ? "?view=history" : ""}` : origin.type === "unifi" ? "/network/devices" : origin.type === "ups" ? "/ups" : origin.type === "slo" ? "/service-levels" : origin.type === "dockhand" ? "/docker" : origin.type === "duplicati" ? "/duplicati" : origin.type === "jellyfin" ? "/jellyfin" : origin.type === "authentik" ? "/authentik" : undefined,
|
||||
silenced: subject !== null && subjects.has(subject), target: targetOfSubject(subject),
|
||||
});
|
||||
}
|
||||
@@ -248,7 +251,7 @@ export async function collectAlerts(): Promise<AlertsReport> {
|
||||
if (held.has("slo")) cannotCheck("Service-level targets", "couldn't be judged — see the server log", "Server and storage health");
|
||||
if (held.has("server:baseline")) cannotCheck("What is usual for the time", "couldn't be judged — see the server log", "Server and storage health");
|
||||
for (const h of held) {
|
||||
const m = /^(proxmox|synology|unifi|ups|dockhand|jellyfin):(\d+)(?::(.+))?$/.exec(h);
|
||||
const m = /^(proxmox|synology|unifi|ups|dockhand|jellyfin|authentik):(\d+)(?::(.+))?$/.exec(h);
|
||||
if (!m || (m[3] && held.has(`${m[1]}:${m[2]}`))) continue;
|
||||
const row = intById.get(Number(m[2]));
|
||||
cannotCheck(`${SOURCES[m[1]].label} “${row?.name ?? `#${m[2]}`}”${m[3] ? ` (node ${m[3]})` : ""}`, "couldn't be read — see the Diagnostic Log", "Server and storage health");
|
||||
|
||||
@@ -0,0 +1,189 @@
|
||||
/**
|
||||
* Keeps what Authentik says up to date: every five minutes each Authentik integration is read (its version, whether a worker is connected, how each outpost is, which certificates
|
||||
* and tokens run out, how many sign-ins and flows failed) and the state is stored. Pages and alerts are built from what is stored, so none of them talks to Authentik.
|
||||
*
|
||||
* Authentik is the front door to everything else, so what is worth knowing early is what would shut it: a proxy outpost that stopped reporting (the apps behind it stop letting
|
||||
* anyone in), a certificate or an API token that is about to run out, no worker (mail and outpost changes stop), and somebody guessing passwords.
|
||||
*/
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { db } from "../db/client.js";
|
||||
import { authentikState, integrations } from "../db/schema.js";
|
||||
import { createAuthentikAdapter, type AuthentikCertificate, type AuthentikOutpost, type AuthentikRead, type AuthentikToken } from "../integrations/authentik/adapter.js";
|
||||
import { loadIntegrationConfig } from "../integrations/loadIntegration.js";
|
||||
import { getInternalFlag, setInternalFlag } from "./settingsStore.js";
|
||||
|
||||
export const SYNC_INTERVAL_MS = 5 * 60_000;
|
||||
/** After this long without a good read, what is stored is no longer believed. */
|
||||
export const STALE_AFTER_MS = 20 * 60_000;
|
||||
/** This many failed sign-ins in a day is a warning. */
|
||||
export const FAILED_LOGINS_WARN = 20;
|
||||
/** This many configuration errors (a flow or stage that is set up wrongly) in a day is a warning. */
|
||||
export const CONFIG_ERRORS_WARN = 3;
|
||||
/** A certificate or a token this close to running out is a warning; closer still, critical. */
|
||||
export const CERT_WARN_DAYS = 14;
|
||||
export const CERT_CRITICAL_DAYS = 3;
|
||||
export const TOKEN_WARN_DAYS = 7;
|
||||
export const TOKEN_CRITICAL_DAYS = 2;
|
||||
const STATUS_FLAG = "authentikSync";
|
||||
const DAY_MS = 86_400_000;
|
||||
|
||||
export interface AuthentikSyncStatus {
|
||||
/** The last read that worked (kept when a later one failed). */
|
||||
at: string;
|
||||
ok: boolean;
|
||||
error: string | null;
|
||||
}
|
||||
|
||||
async function readStatuses(): Promise<Record<string, AuthentikSyncStatus>> {
|
||||
try {
|
||||
const raw = await getInternalFlag(STATUS_FLAG);
|
||||
return raw ? (JSON.parse(raw) as Record<string, AuthentikSyncStatus>) : {};
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
export async function authentikSyncStatuses(): Promise<Record<number, AuthentikSyncStatus>> {
|
||||
return Object.fromEntries(Object.entries(await readStatuses()).map(([k, v]) => [Number(k), v]));
|
||||
}
|
||||
|
||||
export const isAuthentikFresh = (status: AuthentikSyncStatus | undefined, nowMs: number) => !!status && status.ok && nowMs - Date.parse(status.at) <= STALE_AFTER_MS;
|
||||
|
||||
export type StateRow = typeof authentikState.$inferSelect;
|
||||
|
||||
const parse = <T>(text: string): T[] => {
|
||||
try {
|
||||
const v = JSON.parse(text);
|
||||
return Array.isArray(v) ? (v as T[]) : [];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
};
|
||||
export const outpostsOf = (row: Pick<StateRow, "outposts">) => parse<AuthentikOutpost>(row.outposts);
|
||||
export const certificatesOf = (row: Pick<StateRow, "certificates">) => parse<AuthentikCertificate>(row.certificates);
|
||||
export const tokensOf = (row: Pick<StateRow, "tokens">) => parse<AuthentikToken>(row.tokens);
|
||||
|
||||
/** Writes down one read, replacing the last. */
|
||||
export async function storeRead(integrationId: number, r: AuthentikRead, now: Date = new Date()): Promise<void> {
|
||||
const values = {
|
||||
version: r.version,
|
||||
latestVersion: r.latestVersion,
|
||||
outdated: r.outdated,
|
||||
workers: r.workers,
|
||||
outposts: JSON.stringify(r.outposts),
|
||||
certificates: JSON.stringify(r.certificates),
|
||||
tokens: JSON.stringify(r.tokens),
|
||||
failedLogins24h: r.failedLogins24h,
|
||||
configErrors24h: r.configErrors24h,
|
||||
lastConfigError: r.lastConfigError,
|
||||
updatedAt: now.toISOString(),
|
||||
};
|
||||
await db.insert(authentikState).values({ integrationId, ...values }).onConflictDoUpdate({ target: authentikState.integrationId, set: values });
|
||||
}
|
||||
|
||||
/** Reads one Authentik and stores it. Never throws: how it went is the answer, and what was stored is left alone when it failed. */
|
||||
export async function syncAuthentik(integrationId: number, now: Date = new Date(), read?: (config: Record<string, unknown>) => Promise<AuthentikRead>): Promise<AuthentikSyncStatus> {
|
||||
let status: AuthentikSyncStatus;
|
||||
try {
|
||||
const loaded = await loadIntegrationConfig(integrationId);
|
||||
if (!loaded || loaded.integration.type !== "authentik") throw new Error("not an Authentik integration");
|
||||
const result = await (read ? read(loaded.config) : createAuthentikAdapter(loaded.config as never).read(now));
|
||||
await storeRead(integrationId, result, now);
|
||||
status = { at: now.toISOString(), ok: true, error: null };
|
||||
} catch (err) {
|
||||
const previous = (await readStatuses())[String(integrationId)];
|
||||
status = { at: previous?.at ?? now.toISOString(), ok: false, error: err instanceof Error ? err.message : String(err) };
|
||||
console.error(`[authentik] couldn't read integration ${integrationId}:`, status.error);
|
||||
}
|
||||
try {
|
||||
const all = await readStatuses();
|
||||
all[String(integrationId)] = status;
|
||||
await setInternalFlag(STATUS_FLAG, JSON.stringify(all));
|
||||
} catch (err) {
|
||||
console.error("[authentik] couldn't record the result:", err);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
export async function syncAllAuthentik(now: Date = new Date()): Promise<Record<number, AuthentikSyncStatus>> {
|
||||
const rows = await db.select({ id: integrations.id }).from(integrations).where(and(eq(integrations.type, "authentik"), eq(integrations.enabled, true)));
|
||||
const out: Record<number, AuthentikSyncStatus> = {};
|
||||
for (const r of rows) out[r.id] = await syncAuthentik(r.id, now);
|
||||
return out;
|
||||
}
|
||||
|
||||
export async function forgetAuthentik(integrationId: number): Promise<void> {
|
||||
const all = await readStatuses();
|
||||
if (String(integrationId) in all) {
|
||||
delete all[String(integrationId)];
|
||||
await setInternalFlag(STATUS_FLAG, JSON.stringify(all));
|
||||
}
|
||||
}
|
||||
|
||||
// ─── What is wrong ──────────────────────────────────────────────────────────
|
||||
|
||||
export interface AuthentikProblem {
|
||||
key: string;
|
||||
message: string;
|
||||
severity: "warning" | "critical";
|
||||
}
|
||||
|
||||
/** Whole days from now to then, rounded down; negative once it has passed. */
|
||||
export const daysUntil = (iso: string, now: Date) => Math.floor((Date.parse(iso) - now.getTime()) / DAY_MS);
|
||||
|
||||
const when = (days: number) => (days < 0 ? `ran out ${-days} day${days === -1 ? "" : "s"} ago` : days === 0 ? "runs out today" : `runs out in ${days} day${days === 1 ? "" : "s"}`);
|
||||
|
||||
/** "12 min ago", "3 h ago", "2 days ago". */
|
||||
const ago = (iso: string, now: Date) => {
|
||||
const min = Math.max(0, Math.round((now.getTime() - Date.parse(iso)) / 60_000));
|
||||
return min < 90 ? `${min} min ago` : min < 2 * 1440 ? `${Math.round(min / 60)} h ago` : `${Math.round(min / 1440)} days ago`;
|
||||
};
|
||||
|
||||
/** Pure: what is wrong with one Authentik, from what is stored for it. */
|
||||
export function authentikProblems(id: number, name: string, s: Pick<StateRow, "workers" | "outposts" | "certificates" | "tokens" | "failedLogins24h" | "configErrors24h" | "lastConfigError">, now: Date = new Date()): AuthentikProblem[] {
|
||||
const out: AuthentikProblem[] = [];
|
||||
for (const o of outpostsOf(s)) {
|
||||
if (o.providers === 0) continue; // serves nothing, so nothing is lost when it is quiet
|
||||
if (o.healthy === 0) {
|
||||
out.push({
|
||||
key: `authentik-outpost:${id}:${o.name}`,
|
||||
severity: "critical",
|
||||
message: `${name}: the ${o.type} outpost “${o.name}” has not reported in for a while${o.lastSeen ? ` (last heard ${ago(o.lastSeen, now)})` : " (it never has)"} — what it guards is not letting anyone in`,
|
||||
});
|
||||
} else if (o.outdated) {
|
||||
out.push({ key: `authentik-outpost-version:${id}:${o.name}`, severity: "warning", message: `${name}: the ${o.type} outpost “${o.name}” runs a different version from Authentik` });
|
||||
}
|
||||
}
|
||||
for (const c of certificatesOf(s)) {
|
||||
const days = daysUntil(c.expires, now);
|
||||
if (days > CERT_WARN_DAYS) continue;
|
||||
out.push({ key: `authentik-cert:${id}:${c.name}`, severity: days <= CERT_CRITICAL_DAYS ? "critical" : "warning", message: `${name}: the certificate “${c.name}” ${when(days)}` });
|
||||
}
|
||||
for (const t of tokensOf(s)) {
|
||||
const days = daysUntil(t.expires, now);
|
||||
if (days > TOKEN_WARN_DAYS) continue;
|
||||
out.push({
|
||||
key: `authentik-token:${id}:${t.identifier}`,
|
||||
severity: days <= TOKEN_CRITICAL_DAYS ? "critical" : "warning",
|
||||
message: `${name}: the ${t.intent === "app_password" ? "app password" : "API token"} “${t.identifier}”${t.user ? ` (${t.user})` : ""} ${when(days)}`,
|
||||
});
|
||||
}
|
||||
if (s.workers === 0) out.push({ key: `authentik-worker:${id}`, severity: "warning", message: `${name}: no worker is connected — mail, outpost changes and scheduled tasks are not being done` });
|
||||
if (s.failedLogins24h !== null && s.failedLogins24h >= FAILED_LOGINS_WARN) {
|
||||
out.push({ key: `authentik-logins:${id}`, severity: "warning", message: `${name}: ${s.failedLogins24h >= 100 ? "100 or more" : s.failedLogins24h} sign-ins failed in the last day` });
|
||||
}
|
||||
if (s.configErrors24h !== null && s.configErrors24h >= CONFIG_ERRORS_WARN) {
|
||||
out.push({ key: `authentik-errors:${id}`, severity: "warning", message: `${name}: ${s.configErrors24h} configuration errors in the last day${s.lastConfigError ? ` — the latest: ${s.lastConfigError}` : ""}` });
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
let timer: ReturnType<typeof setInterval> | null = null;
|
||||
|
||||
export function initAuthentikSync(): void {
|
||||
if (timer) return;
|
||||
const pass = () => void syncAllAuthentik().catch((err) => console.error("[authentik] pass failed:", err));
|
||||
timer = setInterval(pass, SYNC_INTERVAL_MS);
|
||||
setTimeout(pass, 13_000);
|
||||
console.log(`[authentik] reading Authentik every ${SYNC_INTERVAL_MS / 60000} minutes`);
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { db } from "../db/client.js";
|
||||
import { integrations, jellyfinState, networkDevices, serverPorts, servers, upsState } from "../db/schema.js";
|
||||
import { authentikState, integrations, jellyfinState, networkDevices, serverPorts, servers, upsState } from "../db/schema.js";
|
||||
import { loadIntegrationConfig } from "../integrations/loadIntegration.js";
|
||||
import { createProxmoxAdapter, type ProxmoxNodeStats } from "../integrations/proxmox/adapter.js";
|
||||
import { createSynologyAdapter, type SynologyStorageInfo } from "../integrations/synology/adapter.js";
|
||||
@@ -21,6 +21,7 @@ import { levelFindings, levelKey, type LevelFinding } from "./serviceLevels.js";
|
||||
import { createDockhandAdapter } from "../integrations/dockhand/adapter.js";
|
||||
import { currentProblems as duplicatiProblemsNow, type DuplicatiProblem } from "./duplicati.js";
|
||||
import { isJellyfinFresh, jellyfinProblems, jellyfinSyncStatuses, type JellyfinProblem } from "./jellyfinSync.js";
|
||||
import { authentikProblems, authentikSyncStatuses, isAuthentikFresh, type AuthentikProblem } from "./authentikSync.js";
|
||||
import { evaluateContainers, loadContainerMemory, saveContainerMemory, type ContainerMemory, type DockerSnapshot } from "./containerHealth.js";
|
||||
import { recordStorageSamples } from "./storageHistory.js";
|
||||
import { isUpsFresh, upsProblems, upsSyncStatuses, type UpsStateRow } from "./upsSync.js";
|
||||
@@ -88,6 +89,8 @@ export interface HealthSnapshot {
|
||||
baselines?: BaselineFinding[];
|
||||
/** Service-level targets whose error budget for the month is mostly or wholly used (services/serviceLevels.ts). */
|
||||
levels?: LevelFinding[];
|
||||
/** What is wrong with each Authentik, from what was stored, only while that reading is recent (services/authentikSync.ts). */
|
||||
authentik?: { integrationId: number; problems: AuthentikProblem[] }[];
|
||||
/** What is wrong with each Jellyfin, from what was stored, only while that reading is recent (services/jellyfinSync.ts). */
|
||||
jellyfin?: { integrationId: number; problems: JellyfinProblem[] }[];
|
||||
/** Duplicati backups that failed or have not succeeded in time (services/duplicati.ts). */
|
||||
@@ -201,6 +204,9 @@ export function evaluateHealth(snapshot: HealthSnapshot, thresholds: Thresholds,
|
||||
}
|
||||
}
|
||||
|
||||
// An Authentik outpost that went quiet, a certificate or token about to run out, no worker, failed sign-ins.
|
||||
for (const a of snapshot.authentik ?? []) for (const p of a.problems) out.push({ key: p.key, source: `authentik:${a.integrationId}`, message: p.message, severity: p.severity });
|
||||
|
||||
// A Jellyfin that wants a restart, a scheduled task that failed, a burst of failed sign-ins.
|
||||
for (const j of snapshot.jellyfin ?? []) for (const p of j.problems) out.push({ key: p.key, source: `jellyfin:${j.integrationId}`, message: p.message, severity: p.severity });
|
||||
|
||||
@@ -434,6 +440,24 @@ export async function collectSnapshot(): Promise<{ snapshot: HealthSnapshot; hel
|
||||
}
|
||||
}
|
||||
|
||||
// Authentik: read by its own pass every few minutes; what is judged is what that pass stored, and only while it is recent.
|
||||
try {
|
||||
const rowsA = await db.select({ id: integrations.id, name: integrations.name }).from(integrations).where(and(eq(integrations.type, "authentik"), eq(integrations.enabled, true)));
|
||||
if (rowsA.length > 0) {
|
||||
const statuses = await authentikSyncStatuses();
|
||||
for (const r of rowsA) {
|
||||
const [state] = await db.select().from(authentikState).where(eq(authentikState.integrationId, r.id)).limit(1);
|
||||
if (!state || !isAuthentikFresh(statuses[r.id], Date.now())) {
|
||||
held.add(`authentik:${r.id}`);
|
||||
continue;
|
||||
}
|
||||
(snapshot.authentik ??= []).push({ integrationId: r.id, problems: authentikProblems(r.id, r.name, state) });
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("[health] couldn't judge Authentik:", err instanceof Error ? err.message : err);
|
||||
}
|
||||
|
||||
// Jellyfin: read by its own pass every couple of minutes; what is judged is what that pass stored, and only while it is recent.
|
||||
try {
|
||||
const rowsJ = await db.select({ id: integrations.id, name: integrations.name }).from(integrations).where(and(eq(integrations.type, "jellyfin"), eq(integrations.enabled, true)));
|
||||
|
||||
@@ -33,6 +33,8 @@ export function subjectOfConditionKey(key: string): string | null {
|
||||
if (m) return `integration:${m[1]}`;
|
||||
m = /^jellyfin-(?:restart|task|logins):(\d+)/.exec(key);
|
||||
if (m) return `integration:${m[1]}`;
|
||||
m = /^authentik-(?:outpost|outpost-version|cert|token|worker|logins|errors):(\d+)/.exec(key);
|
||||
if (m) return `integration:${m[1]}`;
|
||||
m = /^duplicati-(?:failed|stale):\d+:server:(\d+)$/.exec(key);
|
||||
if (m) return `server:${m[1]}`;
|
||||
m = /^(?:endpoint(?:-cert)?|heartbeat(?:-fail)?):\d+:server:(\d+)$/.exec(key);
|
||||
|
||||
@@ -5,11 +5,12 @@
|
||||
*/
|
||||
import { eq } from "drizzle-orm";
|
||||
import { db } from "../db/client.js";
|
||||
import { assets, endpointChecks, heartbeats, integrations, maintenanceWindows, secrets, servers, upsState, jellyfinState } from "../db/schema.js";
|
||||
import { assets, endpointChecks, heartbeats, integrations, maintenanceWindows, secrets, servers, upsState, jellyfinState, authentikState } from "../db/schema.js";
|
||||
import { isUpsFresh, upsSyncStatuses } from "./upsSync.js";
|
||||
import { levelStatuses } from "./serviceLevels.js";
|
||||
import { backupState, listAll as listDuplicati } from "./duplicati.js";
|
||||
import { isJellyfinFresh, jellyfinSyncStatuses, streamsOf } from "./jellyfinSync.js";
|
||||
import { authentikSyncStatuses, certificatesOf, daysUntil, isAuthentikFresh, outpostsOf } from "./authentikSync.js";
|
||||
import { latestTests } from "./restoreTests.js";
|
||||
import { heartbeatState, parseStamp } from "./heartbeats.js";
|
||||
import { env } from "../env.js";
|
||||
@@ -61,6 +62,20 @@ class Exposition {
|
||||
}
|
||||
}
|
||||
|
||||
/** Each Authentik, for /metrics: whether each outpost that serves something is reporting, how many workers there are, the failed sign-ins and the days left of each certificate. Only while the reading is recent. */
|
||||
async function addAuthentikMetrics(out: Exposition, now: Date) {
|
||||
const statuses = await authentikSyncStatuses();
|
||||
const names = new Map((await db.select({ id: integrations.id, name: integrations.name, enabled: integrations.enabled }).from(integrations).where(eq(integrations.type, "authentik"))).map((r) => [r.id, r]));
|
||||
for (const s of await db.select().from(authentikState)) {
|
||||
const row = names.get(s.integrationId);
|
||||
if (!row || !row.enabled || !isAuthentikFresh(statuses[s.integrationId], now.getTime())) continue;
|
||||
for (const o of outpostsOf(s)) out.add("homelab_authentik_outpost_up", "gauge", "1 if the Authentik outpost has reported in lately, else 0.", o.healthy > 0 ? 1 : 0, { name: row.name, outpost: o.name });
|
||||
if (s.workers !== null) out.add("homelab_authentik_workers", "gauge", "How many Authentik workers are connected.", s.workers, { name: row.name });
|
||||
if (s.failedLogins24h !== null) out.add("homelab_authentik_failed_logins_24h", "gauge", "Sign-ins that failed in the last day (100 means at least that many).", s.failedLogins24h, { name: row.name });
|
||||
for (const c of certificatesOf(s)) out.add("homelab_authentik_certificate_expiry_days", "gauge", "Whole days until the certificate runs out; negative once it has.", daysUntil(c.expires, now), { name: row.name, certificate: c.name });
|
||||
}
|
||||
}
|
||||
|
||||
/** Each Jellyfin, for /metrics: how many are streaming, how many of those are transcoded, and whether it wants a restart. Only while the reading is recent. */
|
||||
async function addJellyfinMetrics(out: Exposition, now: Date) {
|
||||
const statuses = await jellyfinSyncStatuses();
|
||||
@@ -284,6 +299,7 @@ export async function buildMetrics(now: Date = new Date()): Promise<string> {
|
||||
await addServiceLevelMetrics(out, now);
|
||||
await addDuplicatiMetrics(out, now);
|
||||
await addJellyfinMetrics(out, now);
|
||||
await addAuthentikMetrics(out, now);
|
||||
await addHeartbeatMetrics(out, now);
|
||||
return out.toString();
|
||||
}
|
||||
@@ -0,0 +1,323 @@
|
||||
/**
|
||||
* Authentik: reading what its API says (outposts and their health, certificates, tokens, events), what is stored, what is wrong and how close to running out is a warning
|
||||
* or critical, the health pass, the alerts, the page's route and /metrics.
|
||||
*
|
||||
* Run with: npm test --workspace server
|
||||
*/
|
||||
import { test, describe, before, after } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { createServer as createHttpServer, type Server } from "node:http";
|
||||
import { mkdtempSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import type { AddressInfo } from "node:net";
|
||||
|
||||
const dir = mkdtempSync(join(tmpdir(), "hm-authentik-"));
|
||||
process.env.DATABASE_PATH = join(dir, "test.sqlite");
|
||||
process.env.SESSION_DIR = join(dir, "sessions");
|
||||
process.env.CREDENTIALS_ENCRYPTION_KEY = "ab".repeat(32);
|
||||
process.env.HEALTH_STARTUP_GRACE_MS = "0";
|
||||
|
||||
const express = (await import("express")).default;
|
||||
const { db } = await import("../src/db/client.js");
|
||||
const { users, integrations, authentikState } = await import("../src/db/schema.js");
|
||||
const { runMigrations } = await import("../src/db/migrate.js");
|
||||
const adapter = await import("../src/integrations/authentik/adapter.js");
|
||||
const ak = await import("../src/services/authentikSync.js");
|
||||
const { evaluateHealth, collectSnapshot } = await import("../src/services/healthMonitor.js");
|
||||
const { subjectOfConditionKey } = await import("../src/services/maintenance.js");
|
||||
const { getAlerts, invalidateAlertsCache } = await import("../src/services/alerts.js");
|
||||
const { authentikRouter } = await import("../src/routes/authentik.js");
|
||||
const { buildMetrics } = await import("../src/services/metrics.js");
|
||||
const { setInternalFlag } = await import("../src/services/settingsStore.js");
|
||||
const { eq } = await import("drizzle-orm");
|
||||
|
||||
await runMigrations();
|
||||
|
||||
const MIN = 60_000;
|
||||
const DAY = 86_400_000;
|
||||
const NOW = new Date("2026-10-20T12:00:00Z");
|
||||
const inDays = (d: number, from: Date = NOW) => new Date(from.getTime() + d * DAY).toISOString();
|
||||
|
||||
type Read = import("../src/integrations/authentik/adapter.js").AuthentikRead;
|
||||
const read = (extra: Partial<Read> = {}): Read => ({ version: "2025.8.1", latestVersion: "2025.8.1", outdated: false, workers: 1, outposts: [], certificates: [], tokens: [], failedLogins24h: 0, configErrors24h: 0, lastConfigError: null, ...extra });
|
||||
const outpost = (extra: Partial<import("../src/integrations/authentik/adapter.js").AuthentikOutpost> = {}) => ({ name: "Proxy", type: "proxy", providers: 2, healthy: 1, instances: 1, outdated: false, lastSeen: NOW.toISOString(), ...extra });
|
||||
|
||||
describe("what Authentik's API says", () => {
|
||||
test("the version, and whether a newer one is out", () => {
|
||||
assert.deepEqual(adapter.parseVersion({ version_current: "2025.8.1", version_latest: "2025.10.0", outdated: true }), { version: "2025.8.1", latestVersion: "2025.10.0", outdated: true });
|
||||
assert.deepEqual(adapter.parseVersion(null), { version: null, latestVersion: null, outdated: false });
|
||||
});
|
||||
|
||||
test("workers are counted; an answer that is not a list is not 'none'", () => {
|
||||
assert.equal(adapter.parseWorkers([{ worker_id: "a" }, { worker_id: "b" }]), 2);
|
||||
assert.equal(adapter.parseWorkers([]), 0);
|
||||
assert.equal(adapter.parseWorkers({ detail: "nope" }), null);
|
||||
});
|
||||
|
||||
test("an outpost is healthy when an instance reported within five minutes; its version and what it serves are read", () => {
|
||||
const o = adapter.parseOutpost(
|
||||
{ name: "Proxy", type: "proxy", providers: [1, 2, 3] },
|
||||
[{ last_seen: new Date(NOW.getTime() - 2 * MIN).toISOString(), version_outdated: false }, { last_seen: new Date(NOW.getTime() - 60 * MIN).toISOString(), version_outdated: true }],
|
||||
NOW,
|
||||
)!;
|
||||
assert.deepEqual([o.name, o.providers, o.healthy, o.instances, o.outdated], ["Proxy", 3, 1, 2, true]);
|
||||
const silent = adapter.parseOutpost({ name: "LDAP", type: "ldap", providers: [1] }, [{ last_seen: new Date(NOW.getTime() - 60 * MIN).toISOString() }], NOW)!;
|
||||
assert.deepEqual([silent.healthy, silent.instances], [0, 1]);
|
||||
assert.equal(adapter.parseOutpost({ name: "Idle", providers: [] }, [], NOW)!.providers, 0);
|
||||
assert.equal(adapter.parseOutpost({}, [], NOW), null);
|
||||
});
|
||||
|
||||
test("certificates Authentik renews itself (managed) are left out", () => {
|
||||
const c = adapter.parseCertificates({ results: [{ name: "mine", cert_expiry: "2026-12-01T00:00:00Z", managed: null }, { name: "theirs", cert_expiry: "2026-12-01T00:00:00Z", managed: "goauthentik.io/x" }, { name: "no date" }] });
|
||||
assert.deepEqual(c, [{ name: "mine", expires: "2026-12-01T00:00:00.000Z" }]);
|
||||
});
|
||||
|
||||
test("only API tokens and app passwords that expire are tokens worth watching", () => {
|
||||
const t = adapter.parseTokens({
|
||||
results: [
|
||||
{ identifier: "monitoring", intent: "api", expiring: true, expires: "2026-11-01T00:00:00Z", user_obj: { username: "svc" } },
|
||||
{ identifier: "forever", intent: "api", expiring: false, expires: "2099-01-01T00:00:00Z" },
|
||||
{ identifier: "login", intent: "verification", expiring: true, expires: "2026-10-20T12:30:00Z" },
|
||||
{ identifier: "mail", intent: "app_password", expiring: true, expires: "2026-11-05T00:00:00Z" },
|
||||
],
|
||||
});
|
||||
assert.deepEqual(t.map((x) => [x.identifier, x.intent, x.user]), [["monitoring", "api", "svc"], ["mail", "app_password", null]]);
|
||||
});
|
||||
|
||||
test("events in the last day are counted, with the newest one's words", () => {
|
||||
const raw = { results: [{ created: new Date(NOW.getTime() - MIN).toISOString(), context: { message: "stage failed" } }, { created: new Date(NOW.getTime() - 2 * DAY).toISOString(), context: { message: "old" } }] };
|
||||
assert.deepEqual(adapter.countRecentEvents(raw, NOW), { count: 1, latest: "stage failed" });
|
||||
assert.deepEqual(adapter.countRecentEvents({ detail: "x" }, NOW), { count: null, latest: null });
|
||||
});
|
||||
|
||||
describe("the adapter against an Authentik that answers", () => {
|
||||
let server: Server;
|
||||
let url = "";
|
||||
const seen: string[] = [];
|
||||
before(async () => {
|
||||
server = createHttpServer((req, res) => {
|
||||
const full = req.url ?? "";
|
||||
seen.push(`${req.headers.authorization} ${full}`);
|
||||
const send = (code: number, body: unknown) => {
|
||||
res.writeHead(code, { "content-type": "application/json" });
|
||||
res.end(JSON.stringify(body));
|
||||
};
|
||||
if (req.headers.authorization !== "Bearer tok") return send(403, { detail: "no" });
|
||||
const now = Date.now();
|
||||
const path = full.split("?")[0];
|
||||
if (path === "/api/v3/admin/version/") return send(200, { version_current: "2025.8.1", version_latest: "2025.8.1", outdated: false });
|
||||
if (path === "/api/v3/admin/workers/") return send(200, []);
|
||||
if (path === "/api/v3/outposts/instances/") return send(200, { pagination: {}, results: [{ pk: "o1", name: "Proxy", type: "proxy", providers: [1, 2] }, { pk: "o2", name: "LDAP", type: "ldap", providers: [3] }, { pk: "o3", name: "Idle", type: "radius", providers: [] }] });
|
||||
if (path === "/api/v3/outposts/instances/o1/health/") return send(200, [{ last_seen: new Date(now - MIN).toISOString(), version_outdated: false }]);
|
||||
if (path === "/api/v3/outposts/instances/o2/health/") return send(200, [{ last_seen: new Date(now - 90 * MIN).toISOString() }]);
|
||||
if (path === "/api/v3/outposts/instances/o3/health/") return send(200, []);
|
||||
if (path === "/api/v3/crypto/certificatekeypairs/") return send(200, { results: [{ name: "web", cert_expiry: new Date(now + 5 * DAY).toISOString(), managed: null }, { name: "built-in", cert_expiry: new Date(now + 400 * DAY).toISOString(), managed: "goauthentik.io/x" }] });
|
||||
if (path === "/api/v3/core/tokens/") return send(500, {}); // the rest is worth having, not worth failing for
|
||||
if (path === "/api/v3/events/events/") {
|
||||
const fresh = new Date(now - MIN).toISOString();
|
||||
if (full.includes("action=login_failed")) return send(200, { results: Array.from({ length: 25 }, () => ({ created: fresh })) });
|
||||
return send(200, { results: [{ created: fresh, context: { message: "email stage could not send" } }] });
|
||||
}
|
||||
send(404, {});
|
||||
});
|
||||
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
|
||||
url = `http://127.0.0.1:${(server.address() as AddressInfo).port}`;
|
||||
});
|
||||
after(() => server.close());
|
||||
|
||||
test("it reads everything, sends the token, and a part that cannot be read does not fail the whole", async () => {
|
||||
const r = await adapter.createAuthentikAdapter({ url: `${url}/`, token: "tok" }).read(new Date());
|
||||
assert.equal(r.version, "2025.8.1");
|
||||
assert.equal(r.workers, 0);
|
||||
assert.deepEqual(r.outposts.map((o) => [o.name, o.healthy, o.providers]), [["Proxy", 1, 2], ["LDAP", 0, 1], ["Idle", 0, 0]]);
|
||||
assert.deepEqual(r.certificates.map((c) => c.name), ["web"], "the managed one is left out");
|
||||
assert.deepEqual(r.tokens, [], "the tokens could not be read");
|
||||
assert.deepEqual([r.failedLogins24h, r.configErrors24h, r.lastConfigError], [25, 1, "email stage could not send"]);
|
||||
assert.ok(seen.every((s) => s.startsWith("Bearer tok ")));
|
||||
});
|
||||
|
||||
test("a refused token and a dead address are said plainly", async () => {
|
||||
const bad = await adapter.createAuthentikAdapter({ url, token: "nope" }).ping();
|
||||
assert.equal(bad.ok, false);
|
||||
assert.match(bad.error ?? "", /did not accept the token/);
|
||||
assert.equal((await adapter.createAuthentikAdapter({ url, token: "tok" }).ping()).ok, true);
|
||||
assert.equal((await adapter.createAuthentikAdapter({ url: "http://127.0.0.1:1", token: "tok" }).ping()).ok, false);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("what is stored", () => {
|
||||
let id = 0;
|
||||
before(async () => {
|
||||
id = (await db.insert(integrations).values({ type: "authentik", name: "SSO", baseUrl: "http://10.9.9.7:9000", config: JSON.stringify({ url: "http://10.9.9.7:9000", token: "tok" }) }).returning({ id: integrations.id }))[0].id;
|
||||
});
|
||||
|
||||
test("the state is replaced at each read", async () => {
|
||||
await ak.storeRead(id, read({ workers: 2, outposts: [outpost()] }), NOW);
|
||||
await ak.storeRead(id, read({ workers: 1, outposts: [outpost({ name: "LDAP", type: "ldap" })], failedLogins24h: 3 }), NOW);
|
||||
const [s] = await db.select().from(authentikState).where(eq(authentikState.integrationId, id));
|
||||
assert.deepEqual([s.workers, ak.outpostsOf(s).map((o) => o.name), s.failedLogins24h], [1, ["LDAP"], 3]);
|
||||
});
|
||||
|
||||
test("a read that failed is said, and what was stored is left alone", async () => {
|
||||
const ok = await ak.syncAuthentik(id, new Date(), async () => read({ version: "2025.10.0" }));
|
||||
assert.equal(ok.ok, true);
|
||||
const failed = await ak.syncAuthentik(id, new Date(), async () => {
|
||||
throw new Error("connection refused");
|
||||
});
|
||||
assert.deepEqual([failed.ok, failed.error], [false, "connection refused"]);
|
||||
assert.equal(failed.at, ok.at, "the time of the last good read is kept");
|
||||
const [s] = await db.select().from(authentikState).where(eq(authentikState.integrationId, id));
|
||||
assert.equal(s.version, "2025.10.0");
|
||||
assert.equal(ak.isAuthentikFresh((await ak.authentikSyncStatuses())[id], Date.now()), false, "a failed read is not believed");
|
||||
});
|
||||
|
||||
test("only a good read recent enough is fresh", () => {
|
||||
const now = Date.now();
|
||||
assert.equal(ak.isAuthentikFresh({ at: new Date(now - 6 * MIN).toISOString(), ok: true, error: null }, now), true);
|
||||
assert.equal(ak.isAuthentikFresh({ at: new Date(now - 21 * MIN).toISOString(), ok: true, error: null }, now), false);
|
||||
assert.equal(ak.isAuthentikFresh(undefined, now), false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("what is wrong, and how close is close", () => {
|
||||
const row = (extra: Partial<Parameters<typeof ak.authentikProblems>[2]> = {}) => ({ workers: 1, outposts: "[]", certificates: "[]", tokens: "[]", failedLogins24h: 0, configErrors24h: 0, lastConfigError: null, ...extra });
|
||||
const keys = (r: ReturnType<typeof row>) => ak.authentikProblems(1, "SSO", r, NOW).map((p) => `${p.key}=${p.severity}`);
|
||||
|
||||
test("nothing wrong says nothing", () => {
|
||||
assert.deepEqual(keys(row()), []);
|
||||
assert.deepEqual(keys(row({ workers: null, failedLogins24h: null, configErrors24h: null })), [], "what could not be asked is not a problem");
|
||||
});
|
||||
|
||||
test("an outpost that serves something and has gone quiet is critical; one that serves nothing is left alone; another version is a warning", () => {
|
||||
const outposts = JSON.stringify([outpost({ name: "Proxy", healthy: 0, lastSeen: null }), outpost({ name: "Idle", providers: 0, healthy: 0 }), outpost({ name: "LDAP", type: "ldap", outdated: true })]);
|
||||
assert.deepEqual(keys(row({ outposts })), ["authentik-outpost:1:Proxy=critical", "authentik-outpost-version:1:LDAP=warning"]);
|
||||
assert.match(ak.authentikProblems(1, "SSO", row({ outposts }), NOW)[0].message, /never has/);
|
||||
});
|
||||
|
||||
test("a certificate: a warning within 14 days, critical within 3 and after", () => {
|
||||
const certs = (d: number) => JSON.stringify([{ name: "web", expires: inDays(d) }]);
|
||||
assert.deepEqual(keys(row({ certificates: certs(30) })), []);
|
||||
assert.deepEqual(keys(row({ certificates: certs(14) })), ["authentik-cert:1:web=warning"]);
|
||||
assert.deepEqual(keys(row({ certificates: certs(3) })), ["authentik-cert:1:web=critical"]);
|
||||
assert.deepEqual(keys(row({ certificates: certs(-2) })), ["authentik-cert:1:web=critical"]);
|
||||
assert.match(ak.authentikProblems(1, "SSO", row({ certificates: certs(5) }), NOW)[0].message, /runs out in 5 days/);
|
||||
assert.match(ak.authentikProblems(1, "SSO", row({ certificates: certs(-2) }), NOW)[0].message, /ran out 2 days ago/);
|
||||
});
|
||||
|
||||
test("a token: a warning within 7 days, critical within 2", () => {
|
||||
const tokens = (d: number) => JSON.stringify([{ identifier: "monitoring", intent: "api", user: "svc", expires: inDays(d) }]);
|
||||
assert.deepEqual(keys(row({ tokens: tokens(8) })), []);
|
||||
assert.deepEqual(keys(row({ tokens: tokens(7) })), ["authentik-token:1:monitoring=warning"]);
|
||||
assert.deepEqual(keys(row({ tokens: tokens(1) })), ["authentik-token:1:monitoring=critical"]);
|
||||
assert.match(ak.authentikProblems(1, "SSO", row({ tokens: tokens(4) }), NOW)[0].message, /API token “monitoring” \(svc\) runs out in 4 days/);
|
||||
});
|
||||
|
||||
test("no worker, a burst of failed sign-ins and a run of configuration errors are warnings", () => {
|
||||
assert.deepEqual(keys(row({ workers: 0 })), ["authentik-worker:1=warning"]);
|
||||
assert.deepEqual(keys(row({ failedLogins24h: ak.FAILED_LOGINS_WARN - 1 })), []);
|
||||
assert.deepEqual(keys(row({ failedLogins24h: ak.FAILED_LOGINS_WARN })), ["authentik-logins:1=warning"]);
|
||||
assert.match(ak.authentikProblems(1, "SSO", row({ failedLogins24h: 100 }), NOW)[0].message, /100 or more/);
|
||||
const e = ak.authentikProblems(1, "SSO", row({ configErrors24h: ak.CONFIG_ERRORS_WARN, lastConfigError: "email stage could not send" }), NOW);
|
||||
assert.deepEqual(e.map((p) => p.key), ["authentik-errors:1"]);
|
||||
assert.match(e[0].message, /the latest: email stage could not send/);
|
||||
assert.deepEqual(keys(row({ configErrors24h: ak.CONFIG_ERRORS_WARN - 1 })), []);
|
||||
});
|
||||
});
|
||||
|
||||
describe("the health pass, the page and /metrics", () => {
|
||||
let akId = 0;
|
||||
let app: Server;
|
||||
let base = "";
|
||||
const fresh = () => setInternalFlag("authentikSync", JSON.stringify({ [akId]: { at: new Date().toISOString(), ok: true, error: null } }));
|
||||
const stale = () => setInternalFlag("authentikSync", JSON.stringify({ [akId]: { at: new Date(Date.now() - 30 * MIN).toISOString(), ok: true, error: null } }));
|
||||
|
||||
before(async () => {
|
||||
await db.insert(users).values([{ oidcSub: "viewer", name: "Vic", role: "viewer" }]);
|
||||
akId = (await db.insert(integrations).values({ type: "authentik", name: "Front door", baseUrl: "http://10.9.9.6:9000", config: JSON.stringify({ url: "http://10.9.9.6:9000", token: "tok" }) }).returning({ id: integrations.id }))[0].id;
|
||||
const nowD = new Date();
|
||||
await ak.storeRead(
|
||||
akId,
|
||||
read({ workers: 0, outposts: [outpost({ name: "Proxy", healthy: 0, lastSeen: null }), outpost({ name: "LDAP", type: "ldap", providers: 1, healthy: 1 })], certificates: [{ name: "web", expires: inDays(5, nowD) }, { name: "far", expires: inDays(300, nowD) }], tokens: [{ identifier: "monitoring", intent: "api", user: "svc", expires: inDays(1, nowD) }], failedLogins24h: 30 }),
|
||||
nowD,
|
||||
);
|
||||
await fresh();
|
||||
|
||||
const a = express();
|
||||
a.use((req, _res, next) => {
|
||||
(req as unknown as { session: unknown }).session = { user: { sub: "viewer" } };
|
||||
next();
|
||||
});
|
||||
a.use("/api/authentik", authentikRouter);
|
||||
app = await new Promise<Server>((resolve) => {
|
||||
const s = a.listen(0, "127.0.0.1", () => resolve(s));
|
||||
});
|
||||
base = `http://127.0.0.1:${(app.address() as AddressInfo).port}/api/authentik`;
|
||||
});
|
||||
after(() => {
|
||||
app.close();
|
||||
try {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
} catch {
|
||||
// Windows keeps the database file locked until the process exits.
|
||||
}
|
||||
});
|
||||
|
||||
test("the health pass judges it from what is stored, and holds it when the last read is old", async () => {
|
||||
let { snapshot, held } = await collectSnapshot();
|
||||
assert.deepEqual(snapshot.authentik?.map((a) => a.integrationId), [akId]);
|
||||
assert.equal(held.has(`authentik:${akId}`), false);
|
||||
const list = evaluateHealth(snapshot, { serverOfflineMinutes: 60, diskUsagePercent: 90 }, Date.now(), { skipServers: true });
|
||||
const c = list.find((x) => x.key === `authentik-outpost:${akId}:Proxy`)!;
|
||||
assert.deepEqual([c.severity, c.source], ["critical", `authentik:${akId}`]);
|
||||
assert.equal(subjectOfConditionKey(c.key), `integration:${akId}`, "maintenance on it silences its alerts");
|
||||
for (const k of ["authentik-cert", "authentik-token", "authentik-worker", "authentik-logins", "authentik-errors", "authentik-outpost-version"]) assert.equal(subjectOfConditionKey(`${k}:${akId}`), `integration:${akId}`);
|
||||
await stale();
|
||||
({ snapshot, held } = await collectSnapshot());
|
||||
assert.deepEqual([snapshot.authentik ?? [], held.has(`authentik:${akId}`)], [[], true]);
|
||||
await fresh();
|
||||
});
|
||||
|
||||
test("the Alerts page shows each, in the kind it belongs to, with where to look", async () => {
|
||||
invalidateAlertsCache();
|
||||
const report = await getAlerts(true);
|
||||
const find = (key: string) => report.alerts.find((x) => x.id.endsWith(`${key}`))!;
|
||||
const outposts = find(`authentik-outpost:${akId}:Proxy`);
|
||||
assert.deepEqual([outposts.severity, outposts.category, outposts.source, outposts.link, outposts.target?.type, outposts.target?.id], ["critical", "services", "Authentik", "/authentik", "integration", akId]);
|
||||
assert.equal(find(`authentik-cert:${akId}:web`).category, "expiry");
|
||||
assert.equal(find(`authentik-token:${akId}:monitoring`).severity, "critical");
|
||||
assert.equal(find(`authentik-logins:${akId}`).category, "monitoring");
|
||||
assert.equal(find(`authentik-worker:${akId}`).category, "services");
|
||||
assert.equal(report.alerts.some((x) => x.id.endsWith(`authentik-cert:${akId}:far`)), false, "a certificate with time left is not an alert");
|
||||
});
|
||||
|
||||
test("anyone signed in sees each Authentik, what runs out soonest first, and what is wrong", async () => {
|
||||
const res = await fetch(base);
|
||||
assert.equal(res.status, 200);
|
||||
const body = (await res.json()) as { authentiks: { id: number; fresh: boolean; state: { certificates: { name: string; days: number }[]; tokens: { identifier: string; days: number }[]; outposts: unknown[] } | null; problems: { key: string }[] }[] };
|
||||
const a = body.authentiks.find((x) => x.id === akId)!;
|
||||
assert.equal(a.fresh, true);
|
||||
assert.deepEqual(a.state?.certificates.map((c) => c.name), ["web", "far"], "the soonest first");
|
||||
assert.deepEqual(a.state?.tokens.map((t) => t.identifier), ["monitoring"]);
|
||||
assert.ok((a.state?.tokens[0].days ?? 9) <= 1, "about a day left");
|
||||
assert.equal(a.state?.outposts.length, 2);
|
||||
assert.ok(a.problems.some((p) => p.key === `authentik-outpost:${akId}:Proxy`));
|
||||
await stale();
|
||||
const old = (await (await fetch(base)).json()) as typeof body;
|
||||
assert.deepEqual(old.authentiks.find((x) => x.id === akId)!.problems, [], "an old reading says nothing is wrong, it does not guess");
|
||||
await fresh();
|
||||
});
|
||||
|
||||
test("/metrics has each outpost, the workers, the failed sign-ins and the days left of each certificate — only while the reading is recent", async () => {
|
||||
let text = await buildMetrics(new Date());
|
||||
assert.match(text, /homelab_authentik_outpost_up\{name="Front door",outpost="Proxy"\} 0/);
|
||||
assert.match(text, /homelab_authentik_outpost_up\{name="Front door",outpost="LDAP"\} 1/);
|
||||
assert.match(text, /homelab_authentik_workers\{name="Front door"\} 0/);
|
||||
assert.match(text, /homelab_authentik_failed_logins_24h\{name="Front door"\} 30/);
|
||||
assert.match(text, /homelab_authentik_certificate_expiry_days\{name="Front door",certificate="web"\} [45]/);
|
||||
await stale();
|
||||
text = await buildMetrics(new Date());
|
||||
assert.doesNotMatch(text, /homelab_authentik/);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user