diff --git a/packages/system/dashboard/images/console/apps/console/src/components/observability/LogsView.test.tsx b/packages/system/dashboard/images/console/apps/console/src/components/observability/LogsView.test.tsx index 693adf70a..fae3f6a4b 100644 --- a/packages/system/dashboard/images/console/apps/console/src/components/observability/LogsView.test.tsx +++ b/packages/system/dashboard/images/console/apps/console/src/components/observability/LogsView.test.tsx @@ -90,6 +90,16 @@ describe("LogsView", () => { expect(screen.queryByLabelText("LogsQL filter")).toBeNull() }) + it("renders the tenant logs page as not available when kube-apiserver answers instead of the proxy", async () => { + const { client } = clientWith(() => + Response.json({ kind: "Status", apiVersion: "v1", status: "Failure", reason: "Forbidden", code: 403 }, { status: 403 }), + ) + + renderWithK8sProvider(, { client }) + + expect(await screen.findByText("Logs are not available on this cluster.")).toBeInTheDocument() + }) + it("keeps the controls and explains a rejected query", async () => { const { client } = clientWith((endpoint, params) => endpoint.endsWith("query") ? new Response("cannot parse query", { status: 400 }) : defaultHandler(endpoint, params), diff --git a/packages/system/dashboard/images/console/apps/console/src/lib/observability/proxy.test.ts b/packages/system/dashboard/images/console/apps/console/src/lib/observability/proxy.test.ts index f8cf44656..69a8c7c8c 100644 --- a/packages/system/dashboard/images/console/apps/console/src/lib/observability/proxy.test.ts +++ b/packages/system/dashboard/images/console/apps/console/src/lib/observability/proxy.test.ts @@ -79,6 +79,23 @@ describe("queryRange", () => { expect(err).toMatchObject({ kind: "limit", status: 422, message: "range too long" }) }) + it("treats a Kubernetes Status 403 as a cluster without the proxy route", async () => { + const status = { kind: "Status", apiVersion: "v1", status: "Failure", reason: "Forbidden", code: 403, message: "logs.cozystack.io \"select\" is forbidden" } + const { client } = clientReturning(Response.json(status, { status: 403 })) + + const err = await queryRange(client, metricsRoot(ns), { query: "up", start: 0, end: 1, step: 1 }).catch((e) => e) + + expect(err).toMatchObject({ kind: "not-available", status: 403 }) + }) + + it("keeps the proxy's own plain-text 403 as forbidden", async () => { + const { client } = clientReturning(new Response("forbidden\n", { status: 403 })) + + const err = await queryRange(client, metricsRoot(ns), { query: "up", start: 0, end: 1, step: 1 }).catch((e) => e) + + expect(err).toMatchObject({ kind: "forbidden" }) + }) + it("reports a fetch failure as a network error", async () => { const client = new K8sClient() vi.spyOn(client, "raw").mockRejectedValue(new TypeError("offline")) diff --git a/packages/system/dashboard/images/console/apps/console/src/lib/observability/proxy.ts b/packages/system/dashboard/images/console/apps/console/src/lib/observability/proxy.ts index 345d49815..c9fce3d4f 100644 --- a/packages/system/dashboard/images/console/apps/console/src/lib/observability/proxy.ts +++ b/packages/system/dashboard/images/console/apps/console/src/lib/observability/proxy.ts @@ -69,6 +69,17 @@ export function isScopeWide(error: unknown): boolean { ) } +// The proxy answers in plain text; a Kubernetes Status body means nginx sent +// the path to kube-apiserver, which RBAC-denies or 404s a route it does not serve. +function isKubernetesStatus(text: string): boolean { + try { + const parsed: unknown = JSON.parse(text) + return !!parsed && typeof parsed === "object" && "kind" in parsed && parsed.kind === "Status" + } catch { + return false + } +} + // Prometheus answers {"error": "..."}, VictoriaLogs and nginx answer plain text. function errorMessage(text: string, status: number): string { try { @@ -100,7 +111,8 @@ async function proxyGet( } if (!res.ok) { const text = await res.text().catch(() => "") - throw new ProxyError(errorKindForStatus(res.status), res.status, errorMessage(text, res.status)) + const kind = isKubernetesStatus(text) ? "not-available" : errorKindForStatus(res.status) + throw new ProxyError(kind, res.status, errorMessage(text, res.status)) } return res }