mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-22 12:43:32 +09:00
fix(dashboard): treat an apiserver answer on a proxy path as no proxy
Until the console nginx routes the proxy roots, those paths reach kube-apiserver, which RBAC-denies the namespace root with 403 for a tenant, so the Logs page told every tenant they lacked permission. The proxy itself answers in plain text, so a Kubernetes Status body is a reliable sign that the path did not reach it. Assisted-by: LLM Signed-off-by: ZverGuy <maximbel2003@gmail.com>
This commit is contained in:
1 parent
c5b57bffd1
commit
97dcf4c4f0
3 files changed
+40
-1
No files matched your search
+10
@@ -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(<LogsView scope={{ namespace: "tenant-a" }} />, { 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),
|
||||
|
||||
+17
@@ -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"))
|
||||
|
||||
+13
-1
@@ -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
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user