Authentik: reads checked live; inspector says what a non-JSON answer is

Inspect on a real Authentik showed the version, outposts, certificates, tokens and events answer as the reader expects; /admin/workers/ answered with something that is not JSON, so the worker count stays unknown (no alert on a guess). The inspector now records the content type and what a non-JSON answer looks like (a web page, markup, metrics text, plain text; never its words) and shows it with the HTTP status on the page and in the copied report. Tests and docs updated.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-11-17 19:32:32 +01:00
1 parent 730d7fb36e
commit 76d6bfa85e
7 files changed
+44 -6

No files matched your search

+1 -1
View File
@@ -612,7 +612,7 @@ osTicket's API cannot close a ticket, so this is the one place in the app that *
**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
- **Checked against a live Authentik** (Inspect): the version, the outposts with their health, the certificates, the tokens and the events log all answered as the reader expects. **`/admin/workers/` answered with something that is not JSON** on that Authentik (the reason is not known yet: the inspector now shows the status and kind of such an answer), so the number of workers is *unknown* there: the page leaves it out and the *no worker is connected* alert is never raised on a guess. A token is listed only if it **expires** and is an API token or app password, so an Authentik whose tokens are all set not to expire shows none, which is right. Everything else is as Authentik publishes it and 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
+1 -1
View File
@@ -6,7 +6,7 @@
* flows failed lately (from the events log). Nothing is sent to Authentik but GET requests for these; making a user is ./users.ts, with a token of its own.
*
* 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.
* `/core/tokens/`, `/events/events/`) and were checked against a live server (Inspect), where `/admin/workers/` answered with something that is not JSON — the workers are then unknown, not none; 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";
+5 -2
View File
@@ -4,7 +4,7 @@
*/
import * as http from "node:http";
import * as https from "node:https";
import { isInspecting, pathForReport, recordCall, shapeOf } from "./inspector.js";
import { isInspecting, looksLike, pathForReport, recordCall, shapeOf } from "./inspector.js";
export interface JsonResponse {
status: number;
@@ -67,7 +67,10 @@ export function requestJson(
// not JSON: the text is still there
}
// While the inspector runs a read, what was asked and the shape of the answer (never its values) are written down.
if (isInspecting()) recordCall({ method, path: pathForReport(parsed), status: res.statusCode ?? 0, ms: Date.now() - started, bytes: Buffer.byteLength(text), shape: json === null ? null : shapeOf(json) });
if (isInspecting()) {
const contentType = String(res.headers["content-type"] ?? "").split(";")[0].trim().toLowerCase() || undefined;
recordCall({ method, path: pathForReport(parsed), status: res.statusCode ?? 0, ms: Date.now() - started, bytes: Buffer.byteLength(text), shape: json === null ? null : shapeOf(json), ...(contentType ? { contentType } : {}), ...(json === null ? { looks: looksLike(text) } : {}) });
}
resolve({ status: res.statusCode ?? 0, json, text, headers: res.headers });
});
},
+14
View File
@@ -15,6 +15,10 @@ export interface InspectedCall {
bytes: number;
/** What the answer looked like; null when it was not JSON or there was none. */
shape: string | null;
/** The answer's content type (without parameters), when it sent one. */
contentType?: string;
/** For an answer that was not JSON: what kind of text it is (never its words). */
looks?: string;
error?: string;
}
@@ -38,6 +42,16 @@ export async function inspectCalls<T>(run: () => Promise<T>): Promise<{ calls: I
});
}
/** Pure: what an answer that is not JSON looks like — a web page, markup, plain text — from its first characters; none of its words are kept. */
export function looksLike(text: string): string | undefined {
const head = text.trimStart().slice(0, 200);
if (head === "") return undefined;
if (/^<!doctype html|^<html|^<head|^<body/i.test(head)) return "an HTML page";
if (/^<\?xml|^<[a-z]/i.test(head)) return "XML or markup";
if (/^#\s*(help|type)\s/i.test(head)) return "metrics text (Prometheus format)";
return "plain text";
}
/** Pure: a path for a report — the query's parameter names, not what they were set to. */
export function pathForReport(url: URL): string {
const names = [...new Set([...url.searchParams.keys()])];
+14
View File
@@ -72,6 +72,17 @@ describe("where it is empty", () => {
});
});
describe("what an answer that is not JSON looks like", () => {
test("a web page, markup, metrics text, plain text or nothing — from its first characters only", () => {
assert.equal(insp.looksLike("<!DOCTYPE html><html><body>Not found</body></html>"), "an HTML page");
assert.equal(insp.looksLike(" \n<html lang=en>"), "an HTML page");
assert.equal(insp.looksLike('<?xml version="1.0"?><a/>'), "XML or markup");
assert.equal(insp.looksLike("# HELP up whether it is up\nup 1"), "metrics text (Prometheus format)");
assert.equal(insp.looksLike("Not Found"), "plain text");
assert.equal(insp.looksLike(" "), undefined);
});
});
describe("the requests written down while a read runs", () => {
let http: Server;
let url = "";
@@ -100,6 +111,9 @@ describe("the requests written down while a read runs", () => {
assert.deepEqual(calls.map((c) => [c.method, c.path, c.status]), [["GET", "/json?token&page", 200], ["GET", "/html", 200], ["GET", "/missing", 404], ["GET", "/refused", 0]]);
assert.equal(calls[0].shape, "{ items: array(1) of { id: number, secret: string }, total: number }");
assert.equal(calls[1].shape, null, "not JSON");
assert.deepEqual([calls[0].contentType, calls[0].looks], ["application/json", undefined]);
assert.deepEqual([calls[1].contentType, calls[1].looks], ["application/json", "an HTML page"], "what the text looks like, not what it says");
assert.doesNotMatch(JSON.stringify(calls), /nope/);
assert.ok(calls[0].bytes > 10 && calls[0].ms >= 0);
assert.ok(calls[3].error);
assert.doesNotMatch(JSON.stringify(calls), /hidden-value|hidden/);
+1 -1
View File
@@ -1855,7 +1855,7 @@ export interface IntegrationInspection {
ok: boolean;
error: string | null;
ms: number;
calls: { method: string; path: string; status: number; ms: number; bytes: number; shape: string | null; error?: string }[];
calls: { method: string; path: string; status: number; ms: number; bytes: number; shape: string | null; contentType?: string; looks?: string; error?: string }[];
resultShape: string | null;
empty: { path: string; what: "null" | "text" | "list" | "every-item" }[];
}
+8 -1
View File
@@ -15,6 +15,12 @@ const EMPTY_WORDS: Record<IntegrationInspection["empty"][number]["what"], string
"every-item": "is empty in every item of the list",
};
/** What to say of an answer that was not JSON: its status, its content type and what the text looks like — never its words. */
function notJson(c: IntegrationInspection["calls"][number]): string {
const bits = [`HTTP ${c.status}`, c.contentType, c.bytes === 0 ? "empty" : c.looks ? `looks like ${c.looks}` : null].filter(Boolean);
return `The answer was not JSON (${bits.join(", ")}).`;
}
/** The report as plain text, to be pasted into a message: only shapes, no values. */
function reportText(i: IntegrationSummary, r: IntegrationInspection): string {
const lines = [`Inspection of ${TYPE_LABELS[i.type]} "${i.name}" — ${r.ok ? "the read worked" : `the read failed: ${r.error}`} (${r.ms} ms)`, ""];
@@ -22,6 +28,7 @@ function reportText(i: IntegrationSummary, r: IntegrationInspection): string {
for (const c of r.calls) {
lines.push(` ${c.method} ${c.path} → ${c.status === 0 ? `no answer (${c.error ?? "?"})` : c.status} · ${c.ms} ms · ${c.bytes} bytes`);
if (c.shape) lines.push(` ${c.shape}`);
else if (c.status !== 0) lines.push(` ${notJson(c)}`);
}
lines.push("", "What the reader made of it:", ` ${r.resultShape ?? "(nothing)"}`, "", "Empty in it:");
if (r.empty.length === 0) lines.push(" nothing");
@@ -98,7 +105,7 @@ export default function IntegrationInspector({ integration, onClose }: { integra
</div>
{c.error && <div className="text-red small">{c.error}</div>}
{c.shape && <pre className="small text-secondary mb-0 mt-1" style={{ whiteSpace: "pre-wrap", wordBreak: "break-word" }}>{c.shape}</pre>}
{!c.shape && c.status !== 0 && <div className="text-secondary small">The answer was not JSON{c.bytes === 0 ? " (it was empty)" : ""}.</div>}
{!c.shape && c.status !== 0 && <div className="text-secondary small">{notJson(c)}</div>}
</div>
))}