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:
bobbanandClaude Sonnet 5.5 committed 2026-10-24 22:09:38 +02:00
1 parent 0146eb8bca
commit aebe98bf15
33 files changed
+8050 -7

No files matched your search

+20 -2
View File
@@ -23,8 +23,8 @@ what the app actually creates.
|---|---|
| **File** | `DATABASE_PATH`, default `../data/homelab-manager.sqlite` (relative to `server/`; `/app/data/...` in Docker). The folder is created if missing. |
| **Foreign keys** | Switched on for every connection (`PRAGMA foreign_keys = ON`), so the `ON DELETE` rules below are enforced. |
| **Migrations** | SQL files in [`server/drizzle/`](server/drizzle) (`0000` … `0057`), applied automatically when the server starts. Which ones have run is recorded in the table `__drizzle_migrations`. |
| **Tables** | 70 application tables, plus drizzle's own `__drizzle_migrations`. |
| **Migrations** | SQL files in [`server/drizzle/`](server/drizzle) (`0000` … `0058`), applied automatically when the server starts. Which ones have run is recorded in the table `__drizzle_migrations`. |
| **Tables** | 71 application tables, plus drizzle's own `__drizzle_migrations`. |
**Not in the database, but beside it in the data folder:** the backups of the database itself (`backups/homelab-manager-….sqlite`, taken daily and on request; see [BACKUP.md](BACKUP.md)), and, only while a restore is waiting or has just happened, `restore-pending.sqlite` / `restore-pending.json` and `restore-result.json`.
@@ -69,6 +69,7 @@ erDiagram
integrations ||--o| ups_state : "cascade"
integrations ||--o{ ups_readings : "cascade"
integrations ||--o| jellyfin_state : "cascade"
integrations ||--o| authentik_state : "cascade"
integrations ||--o{ jellyfin_readings : "cascade"
users ||--o{ todos : "assignee (set null)"
servers ||--o{ network_server_links : "cascade"
@@ -847,6 +848,23 @@ A UPS at a moment, for the charts: one every five minutes, and every minute whil
| `runtime_seconds` | integer | |
| `on_battery` | bool NOT NULL, default 0 | |
#### `authentik_state`
How an Authentik (an integration of type `authentik`) is right now, one row each, replaced at every read (every five minutes). `integration_id` is the primary key and a foreign key with **cascade**. There is no history table.
| Column | Type | Notes |
|---|---|---|
| `integration_id` | integer PK | |
| `version`, `latest_version` | text | As Authentik says. |
| `outdated` | bool NOT NULL, default 0 | A newer version is out. |
| `workers` | integer | How many are connected; null when they could not be asked (which is not the same as none). |
| `outposts` | text NOT NULL, default `[]` | JSON: name, type, providers, healthy, instances, outdated, lastSeen. |
| `certificates` | text NOT NULL, default `[]` | JSON: name, expires (those Authentik does not renew itself). |
| `tokens` | text NOT NULL, default `[]` | JSON: identifier, intent, user, expires (API tokens and app passwords that expire). |
| `failed_logins_24h`, `config_errors_24h` | integer | In the last day (a page of 100 means "at least"); null when the events could not be read. |
| `last_config_error` | text | The newest configuration error's message. |
| `updated_at` | text NOT NULL | ISO. |
#### `jellyfin_state`
How a Jellyfin (an integration of type `jellyfin`) is right now, one row each, replaced at every read (every two minutes). `integration_id` is the primary key and a foreign key with **cascade**.
+15
View File
@@ -335,6 +335,21 @@ That works on every Duplicati version, needs no login handshake and leaves nothi
- **Not verified against a live Jellyfin** — the endpoints (`/System/Info`, `/Sessions`, `/ScheduledTasks`, `/Items/Counts`, `/System/ActivityLog/Entries`) and their fields are as Jellyfin publishes them and the reader is tried against a pretend server. Every field is read defensively, and a part that cannot be read (older versions
differ) is left out without failing the rest; the **Diagnostic Log** has what was asked and answered.
### Authentik
- **Config fields:** the **address** of Authentik (`https://auth.example.lan`), an **API token** (Authentik → Directory → Tokens and App passwords → *Create*, intent **API Token**, for a user who may read; stored encrypted) and, for an https address with a certificate you made yourself, *insecure*.
- **Required access:** read only. This app only ever sends GET requests: the version, the workers, the outposts and their health, the certificates, the tokens and the events log. **Nothing is ever changed.** Give the token a user with just enough to read these (an administrator, or a user in a role that may view them);
if one of the lists is refused, that part is left out and the rest still works.
- **What it does:** every **five minutes** Authentik is read and stored — its version (and whether a newer one is out), whether a **worker** is connected, each **outpost** (the proxy outpost that guards your apps, an LDAP one) with how many instances reported in during the last five minutes and whether they run the right version, the
**certificates** (those Authentik does not renew by itself) with their expiry, the **API tokens and app passwords** that expire, and how many **sign-ins failed** and **configuration errors** happened in the last day. **Monitoring → Authentik** shows it, with what runs out soonest first.
- **Alerts** (source *Authentik*; held back by maintenance on the integration): **an outpost that serves applications has not reported in** (critical — the apps behind it stop letting anyone in; an outpost with no applications is left alone), **an outpost runs another version** (warning),
**a certificate runs out** (warning within 14 days, critical within 3 and once it has), **an API token or app password runs out** (warning within 7 days, critical within 2 — the token this app uses to read Authentik is among them), **no worker is connected** (warning; mail, outpost changes and scheduled tasks stop),
**20 or more sign-ins failed in a day** (warning; shown as "100 or more" when the log's page is full) and **3 or more configuration errors in a day** (warning; a flow or stage that is set up wrongly, with the newest message).
- If Authentik cannot be read for 20 minutes its alerts are neither raised nor cleared, the Alerts page says it could not check, and the integration is raised as failing like any other. A part that could not be asked (no workers endpoint on an older version) is not a problem: only *none* is.
- `/metrics` has `homelab_authentik_outpost_up`, `homelab_authentik_workers`, `homelab_authentik_failed_logins_24h` and `homelab_authentik_certificate_expiry_days` for each Authentik that was read lately.
- **Not verified against a live Authentik** — the endpoints (`/api/v3/admin/version/`, `/admin/workers/`, `/outposts/instances/` with `/health/`, `/crypto/certificatekeypairs/`, `/core/tokens/`, `/events/events/`) and their fields are as Authentik publishes them and the reader is tried against a pretend server. Lists are read one page at a time
(200 items; 100 events), and the newest events come first. The **Diagnostic Log** has what was asked and answered. Flows that fail for one user are not counted — only the configuration errors Authentik itself logs; what the app sees of "failing flows" is that.
## DNS providers
### Cloudflare
+3
View File
@@ -526,6 +526,9 @@ meant to be kept and how far back it actually reaches. A history that holds far
address once, and paste three options into Duplicati's default options; no password for Duplicati is kept and the app never calls it. Every job then appears with when it last succeeded, how the last run went, how long it took, how big the source is and what changed, with a history; a failed run is a warning (critical from the
second in a row), an overdue one (2 days by default, per job) a warning (critical at three times that), and maintenance on the machine's server silences them. See INTEGRATIONS.md and docs/MONITORING.md.
**Authentik** (Monitoring → Authentik). Add an Authentik integration (its address and an API token) and the app watches the sign-in in front of everything else: it raises a **critical** alert when an **outpost** that guards applications stops reporting, and warns when a **certificate** or an **API token** is
about to run out (the token it reads Authentik with included), when **no worker** is connected, and when **sign-ins are failing** or flows are misconfigured. The page lists the outposts, what runs out soonest and the failed sign-ins of the last day. See INTEGRATIONS.md and docs/MONITORING.md.
**Jellyfin** (Monitoring → Jellyfin). Add a Jellyfin integration (its address and an API key) and the app shows whether it is up, **who is streaming what** and whether it is being transcoded, the scheduled tasks that failed, and a chart of how many watched at once; it warns when Jellyfin **wants a restart**, a
**task failed** or **sign-ins are failing**. And when the Jellyfin runs on a server the app manages, **an update run asks first if people are watching**, and a patch plan **waits for them (up to two hours)** instead of restarting the machine in the middle of a film. See INTEGRATIONS.md and docs/MONITORING.md.
+2
View File
@@ -47,6 +47,7 @@ page's own top-level link.
| Home Assistant | `/home-assistant` | ✅ | ✅ | ✅ |
| UPS | `/ups` | ✅ | ✅ | ✅ |
| Jellyfin | `/jellyfin` | ✅ | ✅ | ✅ |
| Authentik | `/authentik` | ✅ | ✅ | ✅ |
| **Automation** | | | | |
| Semaphore | `/semaphore` | ✅ | ✅ | ✅ |
| Gitea | `/gitea` | ✅ | ✅ | ✅ |
@@ -133,6 +134,7 @@ even further, to admin only:
- **UPS** (Monitoring → UPS): everyone signed in sees each UPS, its history and what is on it; adding one (its address and SNMP community) is an administrator's job, like any integration, and what it powers is written in the dependency map.
- **Endpoints**: everyone sees the checks and how they are doing; adding, changing and removing one is administrator-only (each makes the server connect somewhere); that includes the value a check judges and the header (a token) it sends, whose value is never shown again to anyone;
*Check now* needs operator.
- **Authentik** (Monitoring → Authentik): everyone signed in sees the outposts, what runs out and the failed-sign-in count (the names of tokens and certificates, not their values); adding one (its address and API token) is an administrator's job, like any integration.
- **Jellyfin** (Monitoring → Jellyfin): everyone signed in sees who is streaming and what is wrong; adding one (its address and API key) is an administrator's job, like any integration. Starting an update run on a server people are streaming from needs `startAnyway` — the update drawer asks (operators).
- **Duplicati** (Backups → Duplicati): everyone signed in sees every backup and how it went; setting how often one is expected, muting it and forgetting it need operator; adding a machine (which makes an address that can write to this app) and removing one are administrators'. Reports arrive on a secret address with no sign-in: the token is the access.
- **Restore tests**: everyone sees them on the Backup coverage page; recording one needs operator; removing one is administrator-only.
+19
View File
@@ -13,6 +13,7 @@ This page explains what Homelab Manager watches, how often, how a problem become
- [Service levels](#service-levels)
- [Your own alert rules](#your-own-alert-rules)
- [Duplicati backups](#duplicati-backups)
- [Authentik](#authentik)
- [Jellyfin](#jellyfin)
- [Docker containers that are broken](#docker-containers-that-are-broken)
- [Endpoint checks and heartbeats](#endpoint-checks-and-heartbeats)
@@ -130,6 +131,7 @@ Every threshold here is a default; where it can be changed, the place is named.
| **Restore test** | A restore test failed, or none has been done within the window | failure always; age off by default | Settings → Notifications |
| **Expiry** | A secret, certificate, domain or Tailscale key is within its warning window or has expired | per item (domains 30 days) | the item; Settings → Notifications |
| **Docker updates** | A container image has a newer version | | Settings → Notifications |
| **Authentik needs a look** | An outpost that serves apps has gone quiet (critical), a certificate or API token is about to run out (critical close to it), no worker is connected, or sign-ins or flows are failing | on, with the integration | Authentik page; [below](#authentik) |
| **Jellyfin needs a look** | Jellyfin wants a restart, a scheduled task (a library scan) failed, or 10 or more sign-ins failed in a day | on, with the integration | Jellyfin page; [below](#jellyfin) |
| **Duplicati backup failed / overdue** | A backup's last run failed (critical from the second in a row), or it has not succeeded for longer than it should | per job (2 days by default) | Duplicati page; [below](#duplicati-backups) |
| **Docker container broken** | A container keeps restarting (critical), is unhealthy, stopped unexpectedly, or was created and never started; or Dockhand cannot reach a Docker host | on | Settings → Notifications; [below](#docker-containers-that-are-broken) |
@@ -213,6 +215,23 @@ and one that no longer exists can be **forgotten** (with its history; if Duplica
What is kept: the last result of each job, and each report for 400 days or 400 reports. `/metrics` has `homelab_duplicati_backup_ok`, `homelab_duplicati_last_success_age_seconds` and `homelab_duplicati_source_size_bytes` for each job. A job is only learned from its first report, so a job that has
*never* reported is not known to be missing; and because the reports are pushed, **the app being down when a backup finishes** loses that report (the backup shows as overdue only once it is).
## Authentik
An integration of type *Authentik* ([INTEGRATIONS.md](../INTEGRATIONS.md#authentik)) is read every five minutes, and **Monitoring → Authentik** shows its version, workers, each outpost (and whether it is reporting), the certificates and API tokens that run out — soonest first — and the failed sign-ins of the last day.
Authentik is the front door to everything behind it, so what is raised early is what would shut it:
| Alert | When | Severity |
|---|---|---|
| **Outpost has not reported in** | An outpost that serves applications had no instance report within five minutes | critical |
| **Outpost runs another version** | An instance runs a version other than the one Authentik expects | warning |
| **Certificate runs out** | Within 14 days (a certificate Authentik renews itself is left out) | warning; critical within 3 days and after |
| **API token or app password runs out** | Within 7 days | warning; critical within 2 days. The token this app reads Authentik with counts too |
| **No worker connected** | The workers endpoint answers with none | warning |
| **Failed sign-ins** | 20 or more in the last day | warning |
| **Configuration errors** | 3 or more in the last day, with the newest message | warning |
Maintenance on the integration holds them. A reading older than 20 minutes is not believed: nothing is raised or cleared from it, and the Alerts page says Authentik could not be checked. A token that does not expire, a verification token (they live for minutes) and an outpost with no applications are never alerts.
## Jellyfin
An integration of type *Jellyfin* ([INTEGRATIONS.md](../INTEGRATIONS.md#jellyfin)) is read every two minutes, and **Monitoring → Jellyfin** shows whether it is up, who is **streaming** right now (and whether that is transcoded, which is what loads the machine), the scheduled tasks that failed, and a chart of the most that were streaming at once.
+3
View File
@@ -296,6 +296,9 @@ All gauges. The same table is checked by a test against what `/metrics` really s
| `homelab_service_level_availability_percent`, `homelab_service_level_error_budget_remaining_minutes` | `name`, `subject`, `type` | How available the thing has been so far this calendar month, and the downtime still allowed before the target is missed (negative once it is). Left out for a target with nothing recorded this month. |
| `homelab_duplicati_backup_ok` | `backup`, `source` | 1 if the Duplicati backup's last run worked and it is not overdue, else 0. |
| `homelab_duplicati_last_success_age_seconds`, `homelab_duplicati_source_size_bytes` | `backup`, `source` | Seconds since it last succeeded (a run with warnings counts), and the size of what its last report examined. |
| `homelab_authentik_outpost_up` | `name`, `outpost` | 1 if the Authentik outpost has had an instance report in the last five minutes, else 0; left out for an Authentik not read in the last twenty minutes. |
| `homelab_authentik_workers`, `homelab_authentik_failed_logins_24h` | `name` | How many Authentik workers are connected, and how many sign-ins failed in the last day (100 means at least that). |
| `homelab_authentik_certificate_expiry_days` | `name`, `certificate` | Whole days until a certificate (one Authentik does not renew itself) runs out; negative once it has. |
| `homelab_jellyfin_streams`, `homelab_jellyfin_transcodes` | `name` | How many are streaming from Jellyfin right now, and how many of those it is transcoding; left out for a Jellyfin not read in the last ten minutes. |
| `homelab_jellyfin_pending_restart` | `name` | 1 if Jellyfin is waiting to be restarted, else 0. |
| `homelab_ups_on_battery` | `name` | 1 if the UPS has lost mains power and runs on its battery, else 0; left out for a UPS that has not been read in the last five minutes. |
@@ -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
+7
View File
@@ -407,6 +407,13 @@
"when": 1792870755473,
"tag": "0057_lame_cloak",
"breakpoints": true
},
{
"idx": 58,
"version": "6",
"when": 1792872166034,
"tag": "0058_flippant_sasquatch",
"breakpoints": true
}
]
}
+2
View File
@@ -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);
+22
View File
@@ -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
+2
View File
@@ -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 });
}
+5
View File
@@ -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" },
+3
View File
@@ -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`);
}
+51
View File
@@ -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) : [],
};
}),
});
}));
+4
View File
@@ -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));
+5 -2
View File
@@ -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");
+189
View File
@@ -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`);
}
+25 -1
View File
@@ -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)));
+2
View File
@@ -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);
+17 -1
View File
@@ -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();
}
+323
View File
@@ -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/);
});
});
+2
View File
@@ -12,6 +12,7 @@ import AlertRules from "./pages/AlertRules";
import HomeAssistant from "./pages/HomeAssistant";
import Ups from "./pages/Ups";
import Jellyfin from "./pages/Jellyfin";
import Authentik from "./pages/Authentik";
import ServiceLevels from "./pages/ServiceLevels";
import Duplicati from "./pages/Duplicati";
import MyWorkPage from "./pages/MyWork";
@@ -160,6 +161,7 @@ function Shelled({ user }: { user: CurrentUser }) {
<Route path="/home-assistant" element={<HomeAssistant user={user} />} />
<Route path="/ups" element={<Ups user={user} />} />
<Route path="/jellyfin" element={<Jellyfin />} />
<Route path="/authentik" element={<Authentik />} />
<Route path="/service-levels" element={<ServiceLevels user={user} />} />
<Route path="/duplicati" element={<Duplicati user={user} />} />
<Route path="/ports" element={<Ports user={user} />} />
+27 -1
View File
@@ -1228,6 +1228,29 @@ export interface JellyfinInfo {
problems: { key: string; message: string; severity: "warning" | "critical" }[];
}
/** One Authentik, as its page shows it. */
export interface AuthentikInfo {
id: number;
name: string;
enabled: boolean;
status: { at: string; ok: boolean; error: string | null } | null;
fresh: boolean;
state: {
version: string | null;
latestVersion: string | null;
outdated: boolean;
workers: number | null;
outposts: { name: string; type: string; providers: number; healthy: number; instances: number; outdated: boolean; lastSeen: string | null }[];
certificates: { name: string; expires: string; days: number }[];
tokens: { identifier: string; intent: string; user: string | null; expires: string; days: number }[];
failedLogins24h: number | null;
configErrors24h: number | null;
lastConfigError: string | null;
updatedAt: string;
} | null;
problems: { key: string; message: string; severity: "warning" | "critical" }[];
}
export interface JellyfinHistory {
jellyfin: { id: number; name: string };
days: number;
@@ -2148,7 +2171,7 @@ export interface ManualTaskInput {
enabled?: boolean;
}
export type IntegrationType = "proxmox" | "synology" | "semaphore" | "tailscale" | "gitea" | "dockhand" | "uptimekuma" | "phpipam" | "pbs" | "osticket" | "unifi" | "npm" | "homeassistant" | "ups" | "jellyfin";
export type IntegrationType = "proxmox" | "synology" | "semaphore" | "tailscale" | "gitea" | "dockhand" | "uptimekuma" | "phpipam" | "pbs" | "osticket" | "unifi" | "npm" | "homeassistant" | "ups" | "jellyfin" | "authentik";
export interface IntegrationField {
key: string;
@@ -2695,6 +2718,9 @@ export const api = {
update: (id: number, data: ServiceLevelInput) => request<{ ok: boolean }>(`/api/service-levels/${id}`, { method: "PUT", body: JSON.stringify(data) }),
remove: (id: number) => request<void>(`/api/service-levels/${id}`, { method: "DELETE" }),
},
authentik: {
list: () => request<{ authentiks: AuthentikInfo[] }>("/api/authentik"),
},
jellyfin: {
list: () => request<{ jellyfins: JellyfinInfo[] }>("/api/jellyfin"),
history: (id: number, days: number) => request<JellyfinHistory>(`/api/jellyfin/${id}/history?days=${days}`),
+1
View File
@@ -48,6 +48,7 @@ const INTEGRATION_TYPE_LABELS: Record<IntegrationType, string> = {
homeassistant: "Home Assistant",
ups: "UPS",
jellyfin: "Jellyfin",
authentik: "Authentik",
};
const DNS_PROVIDER_LABELS: Record<DnsProviderType, string> = {
@@ -19,6 +19,7 @@ const TYPE_LABELS: Record<IntegrationType, string> = {
homeassistant: "Home Assistant",
ups: "UPS",
jellyfin: "Jellyfin",
authentik: "Authentik",
};
export default function IntegrationEditForm({
+1
View File
@@ -18,6 +18,7 @@ const TYPE_LABELS: Record<IntegrationType, string> = {
homeassistant: "Home Assistant",
ups: "UPS",
jellyfin: "Jellyfin",
authentik: "Authentik",
};
export default function IntegrationForm({ onCreated, onCancel }: { onCreated: () => void; onCancel: () => void }) {
+1
View File
@@ -20,6 +20,7 @@ const TYPE_LABELS: Record<string, string> = {
homeassistant: "Home Assistant",
ups: "UPS",
jellyfin: "Jellyfin",
authentik: "Authentik",
};
export type WallLevel = "critical" | "warning" | "ok" | "unsure";
+2
View File
@@ -17,6 +17,7 @@ import {
IconPlugConnected,
IconBrandDocker,
IconAffiliate,
IconLock,
IconMovie,
IconPlayerPlay,
IconBrandGit,
@@ -123,6 +124,7 @@ export const NAV: NavEntry[] = [
{ to: "/home-assistant", label: "Home Assistant", icon: <IconHome2 size={20} /> },
{ to: "/ups", label: "UPS", icon: <IconBatteryCharging size={20} /> },
{ to: "/jellyfin", label: "Jellyfin", icon: <IconMovie size={20} /> },
{ to: "/authentik", label: "Authentik", icon: <IconLock size={20} /> },
{ to: "/service-levels", label: "Service levels", icon: <IconTargetArrow size={20} /> },
],
},
+170
View File
@@ -0,0 +1,170 @@
import { useEffect, useState } from "react";
import { Link } from "react-router-dom";
import { api, type AuthentikInfo } from "../api/client";
import EmptyState from "../components/EmptyState";
import TimeAgo from "../components/TimeAgo";
import { BlockSkeleton } from "../components/Skeleton";
import { ErrorNotice } from "../components/Notice";
import { readableError } from "../utils/errors";
function Badge({ a }: { a: AuthentikInfo }) {
if (!a.enabled) return <span className="badge bg-secondary-lt text-secondary">Off</span>;
if (!a.state) return <span className="badge bg-secondary-lt text-secondary">Not read yet</span>;
if (!a.fresh) return <span className="badge bg-yellow-lt text-yellow">Can&rsquo;t be read</span>;
if (a.problems.some((p) => p.severity === "critical")) return <span className="badge bg-red-lt text-red">Needs a look</span>;
if (a.problems.length > 0) return <span className="badge bg-yellow-lt text-yellow">Needs a look</span>;
return <span className="badge bg-green-lt text-green">In order</span>;
}
/** "in 12 days", "today", "3 days ago": how long until something runs out, in words, coloured by how close it is. */
function Left({ days, warn, critical }: { days: number; warn: number; critical: number }) {
const text = days < 0 ? `ran out ${-days} day${days === -1 ? "" : "s"} ago` : days === 0 ? "runs out today" : `${days} day${days === 1 ? "" : "s"} left`;
return <span className={days <= critical ? "text-red" : days <= warn ? "text-yellow" : "text-secondary"}>{text}</span>;
}
function Card({ a }: { a: AuthentikInfo }) {
const s = a.state;
return (
<div className="card mb-3">
<div className="card-header">
<h3 className="card-title">
{a.name} <Badge a={a} />
</h3>
<div className="card-actions text-secondary small">{a.status ? <>read <TimeAgo value={a.status.at} /></> : "not read yet"}</div>
</div>
<div className="card-body">
{a.status && !a.status.ok && <div className="text-yellow mb-2">The last attempt to read it failed: {a.status.error ?? "no answer"}.</div>}
{a.problems.length > 0 && (
<ul className="mb-3">
{a.problems.map((p) => (
<li key={p.key} className={p.severity === "critical" ? "text-red" : ""}>
{p.message}
</li>
))}
</ul>
)}
{s && (
<>
<div className="text-secondary small mb-3">
{[s.version ? `version ${s.version}` : null, s.outdated && s.latestVersion ? `${s.latestVersion} is out` : null, s.workers === null ? null : `${s.workers} worker${s.workers === 1 ? "" : "s"} connected`, s.failedLogins24h === null ? null : `${s.failedLogins24h >= 100 ? "100+" : s.failedLogins24h} failed sign-in${s.failedLogins24h === 1 ? "" : "s"} in the last day`, s.configErrors24h ? `${s.configErrors24h} configuration error${s.configErrors24h === 1 ? "" : "s"}` : null]
.filter(Boolean)
.join(" · ")}
</div>
<h4>Outposts</h4>
{s.outposts.length === 0 ? (
<div className="text-secondary mb-3">None.</div>
) : (
<div className="table-responsive mb-3">
<table className="table table-vcenter table-sm">
<thead>
<tr>
<th>Outpost</th>
<th>Type</th>
<th className="text-end">Serves</th>
<th>Reporting</th>
<th>Last heard</th>
</tr>
</thead>
<tbody>
{s.outposts.map((o) => (
<tr key={o.name}>
<td>{o.name}</td>
<td className="text-secondary">{o.type}</td>
<td className="text-end">{o.providers} app{o.providers === 1 ? "" : "s"}</td>
<td>
{o.healthy > 0 ? (
<span className="badge bg-green-lt text-green">
{o.healthy} instance{o.healthy === 1 ? "" : "s"}
</span>
) : o.providers === 0 ? (
<span className="badge bg-secondary-lt text-secondary">nothing to serve</span>
) : (
<span className="badge bg-red-lt text-red">silent</span>
)}
{o.outdated && <span className="badge bg-yellow-lt text-yellow ms-1">other version</span>}
</td>
<td className="text-secondary">{o.lastSeen ? <TimeAgo value={o.lastSeen} /> : "never"}</td>
</tr>
))}
</tbody>
</table>
</div>
)}
<h4>Certificates</h4>
{s.certificates.length === 0 ? (
<div className="text-secondary mb-3">None that Authentik doesn&rsquo;t renew itself.</div>
) : (
<ul className="mb-3">
{s.certificates.map((c) => (
<li key={c.name}>
{c.name} — <Left days={c.days} warn={14} critical={3} />
</li>
))}
</ul>
)}
<h4>API tokens and app passwords that expire</h4>
{s.tokens.length === 0 ? (
<div className="text-secondary mb-3">None.</div>
) : (
<ul className="mb-3">
{s.tokens.map((t) => (
<li key={t.identifier}>
{t.identifier}
<span className="text-secondary">
{" "}
({t.intent === "app_password" ? "app password" : "API token"}
{t.user ? `, ${t.user}` : ""})
</span>{" "}
— <Left days={t.days} warn={7} critical={2} />
</li>
))}
</ul>
)}
{s.lastConfigError && <div className="text-secondary small">Latest configuration error: {s.lastConfigError}</div>}
</>
)}
</div>
</div>
);
}
/** Authentik, the front door: whether its outposts are reporting, what is about to run out, whether a worker is connected, and who is failing to sign in. */
export default function Authentik() {
const [data, setData] = useState<AuthentikInfo[] | null>(null);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
const load = () =>
api.authentik
.list()
.then((r) => {
setData(r.authentiks);
setError(null);
})
.catch((err) => setError(readableError(err)));
void load();
const t = setInterval(load, 60_000);
return () => clearInterval(t);
}, []);
return (
<>
<h2 className="page-title">Authentik</h2>
<p className="text-secondary">
The sign-in in front of everything else. It is read every few minutes with an API token (see <Link to="/integrations">Integrations</Link>). An <strong>outpost that goes quiet</strong> (the apps it guards stop letting anyone in), a <strong>certificate</strong> or an{" "}
<strong>API token</strong> that is about to run out, <strong>no worker</strong> and a burst of <strong>failed sign-ins</strong> are alerts — see <Link to="/alerts">Alerts</Link>.
</p>
<ErrorNotice error={error} />
{!data && !error && <BlockSkeleton lines={4} />}
{data && data.length === 0 && (
<div className="card">
<div className="card-body">
<EmptyState title="No Authentik connected" hint="Add one under Integrations with its address and an API token (Authentik → Directory → Tokens and App passwords → Create, intent API Token)." />
</div>
</div>
)}
{data?.map((a) => <Card key={a.id} a={a} />)}
</>
);
}
+2
View File
@@ -30,6 +30,7 @@ const TYPE_LABELS: Record<IntegrationType, string> = {
homeassistant: "Home Assistant",
ups: "UPS",
jellyfin: "Jellyfin",
authentik: "Authentik",
};
/** The page each kind of integration gets once it is connected. */
@@ -49,6 +50,7 @@ const TYPE_PAGES: Record<IntegrationType, string> = {
homeassistant: "/home-assistant",
ups: "/ups",
jellyfin: "/jellyfin",
authentik: "/authentik",
};
/** "pve.example.lan" out of "https://pve.example.lan:8006/", or the text itself when it isn't an address. */
+1
View File
@@ -28,6 +28,7 @@ const INTEGRATION_NAMES: Record<string, string> = {
homeassistant: "Home Assistant",
ups: "UPS",
jellyfin: "Jellyfin",
authentik: "Authentik",
};
function ColorList({