diff --git a/INTEGRATIONS.md b/INTEGRATIONS.md index d01ccda..3c7d71e 100644 --- a/INTEGRATIONS.md +++ b/INTEGRATIONS.md @@ -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 diff --git a/server/src/integrations/authentik/adapter.ts b/server/src/integrations/authentik/adapter.ts index 9022992..c7e5f61 100644 --- a/server/src/integrations/authentik/adapter.ts +++ b/server/src/integrations/authentik/adapter.ts @@ -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"; diff --git a/server/src/integrations/httpJson.ts b/server/src/integrations/httpJson.ts index 2f78f2b..11dae6a 100644 --- a/server/src/integrations/httpJson.ts +++ b/server/src/integrations/httpJson.ts @@ -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 }); }); }, diff --git a/server/src/integrations/inspector.ts b/server/src/integrations/inspector.ts index 16ef593..59c432e 100644 --- a/server/src/integrations/inspector.ts +++ b/server/src/integrations/inspector.ts @@ -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(run: () => Promise): 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 (/^ { }); }); +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("Not found"), "an HTML page"); + assert.equal(insp.looksLike(" \n"), "an HTML page"); + assert.equal(insp.looksLike(''), "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/); diff --git a/web/src/api/client.ts b/web/src/api/client.ts index 178fa5a..a189ff8 100644 --- a/web/src/api/client.ts +++ b/web/src/api/client.ts @@ -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" }[]; } diff --git a/web/src/components/IntegrationInspector.tsx b/web/src/components/IntegrationInspector.tsx index f3dd0ac..381cced 100644 --- a/web/src/components/IntegrationInspector.tsx +++ b/web/src/components/IntegrationInspector.tsx @@ -15,6 +15,12 @@ const EMPTY_WORDS: Record {c.error &&
{c.error}
} {c.shape &&
{c.shape}
} - {!c.shape && c.status !== 0 &&
The answer was not JSON{c.bytes === 0 ? " (it was empty)" : ""}.
} + {!c.shape && c.status !== 0 &&
{notJson(c)}
} ))}