Files
mtusi2026lab7/scripts/grafana.ts

258 lines
8.0 KiB
TypeScript

import { readFile, writeFile, mkdir } from "node:fs/promises";
import { resolve } from "node:path";
import { randomBytes } from "node:crypto";
import { local, project, run, requireThat } from "./runtime";
import { decrypt, encrypt } from "./operator-crypto";
import { createEvidencePreserver } from "./checkpoint-guards";
import { containerID } from "./runtime-inspect";
export function isOwnHistoryWrite(
method: string,
path: string,
body: any,
name: string,
) {
const collection =
"/apis/userstorage.grafana.app/v0alpha1/namespaces/default/user-storage";
if (!/^grafana-page-history:[a-z0-9]{8,40}$/.test(name)) return false;
return (
(method === "POST" &&
[collection, collection + "/"].includes(path) &&
body?.metadata?.name === name) ||
(method === "PATCH" &&
path === collection + "/" + name &&
typeof body?.metadata?.resourceVersion === "string" &&
body.metadata.resourceVersion.length > 0)
);
}
export function publicGrafanaEndpoint(info: any) {
requireThat(
info?.Config?.Labels?.["com.docker.compose.project"] === project &&
info?.Config?.Labels?.["com.docker.compose.service"] ===
"grafana-gateway" &&
info?.State?.Running === true,
"GRAFANA_ENDPOINT_RESOURCE_MISMATCH",
);
const bindings = info?.NetworkSettings?.Ports?.["3443/tcp"];
requireThat(
Array.isArray(bindings) && bindings.length === 1,
"GRAFANA_ENDPOINT_BINDING_MISSING",
);
const binding = bindings[0];
requireThat(
binding.HostIp === "127.0.0.1" &&
typeof binding.HostPort === "string" &&
/^[0-9]{1,5}$/.test(binding.HostPort),
"GRAFANA_ENDPOINT_BINDING_UNSAFE",
);
const port = Number(binding.HostPort);
requireThat(port >= 1 && port <= 65535, "GRAFANA_ENDPOINT_PORT_INVALID");
return { host: "127.0.0.1", port };
}
export function metricFrameValues(body: any, ref = "A") {
const result = body?.results?.[ref];
requireThat(
result &&
!result.error &&
(!result.status || result.status === 200) &&
Array.isArray(result.frames) &&
result.frames.length > 0,
"GRAFANA_METRIC_RESULT_INVALID",
);
const numbers: number[] = [];
for (const frame of result.frames) {
const fields = frame?.schema?.fields,
values = frame?.data?.values;
requireThat(
Array.isArray(fields) &&
Array.isArray(values) &&
fields.length === values.length &&
fields.length > 0,
"GRAFANA_METRIC_FRAME_INVALID",
);
const rows = values[0]?.length;
requireThat(
Number.isInteger(rows) &&
rows > 0 &&
values.every(
(column: any) => Array.isArray(column) && column.length === rows,
),
"GRAFANA_METRIC_FRAME_EMPTY_OR_RAGGED",
);
for (let i = 0; i < fields.length; i++) {
if (fields[i].type !== "number") continue;
for (const value of values[i]) {
if (value === null) continue;
requireThat(
typeof value === "number" && Number.isFinite(value),
"GRAFANA_METRIC_SAMPLE_INVALID",
);
numbers.push(value);
}
}
}
requireThat(numbers.length > 0, "GRAFANA_METRIC_NO_FINITE_SAMPLES");
return numbers;
}
export async function prepareGrafana(password: string) {
const services = decrypt<{ grafana: string }>(
await readFile(resolve(local, "operator/services.enc"), "utf8"),
password,
),
consumer = await readFile(
resolve(local, "operator/grafana-password"),
"utf8",
);
requireThat(
/^[a-f0-9]{64}$/.test(services.grafana) && consumer === services.grafana,
"GRAFANA_ADMIN_ENVELOPE_CONSUMER_MISMATCH",
);
const path = resolve(local, "operator/grafana-security.enc");
let secret: { secretKey: string };
if (await Bun.file(path).exists())
secret = decrypt(await readFile(path, "utf8"), password);
else {
secret = { secretKey: randomBytes(32).toString("hex") };
await writeFile(path, encrypt(secret, password), {
flag: "wx",
mode: 0o600,
});
}
requireThat(
/^[a-f0-9]{64}$/.test(secret.secretKey),
"GRAFANA_SECURITY_KEY_SHAPE_INVALID",
);
const preserve = createEvidencePreserver({
read: readFile,
writeExclusive: (p, b) => writeFile(p, b, { flag: "wx", mode: 0o600 }),
});
await preserve(
resolve(local, "operator/grafana-secret-key"),
Buffer.from(secret.secretKey),
);
return {
admin_envelope_matches_consumer: true,
security_key_encrypted_before_publication: true,
admin_password_shape_and_envelope_verified: true,
};
}
export async function grafanaAddress() {
const id = await containerID("grafana-gateway");
return publicGrafanaEndpoint(
JSON.parse((await run(["docker", "inspect", id])).out)[0],
);
}
export async function grafanaRequest(
path: string,
options: { body?: unknown; method?: string; anonymous?: boolean } = {},
) {
requireThat(
/^\/api\/[A-Za-z0-9_./?=&%-]+$/.test(path) && !path.includes(".."),
"GRAFANA_REQUEST_PATH_INVALID",
);
const endpoint = await grafanaAddress(),
ca = await readFile(resolve(local, "pki/grafana/ca.crt"));
const headers: Record<string, string> = {
"Content-Type": "application/json",
};
if (!options.anonymous) {
const password = await readFile(
resolve(local, "operator/grafana-password"),
"utf8",
);
requireThat(
/^[a-f0-9]{64}$/.test(password),
"GRAFANA_ADMIN_CONSUMER_INVALID",
);
headers.Authorization =
"Basic " + Buffer.from("operator:" + password).toString("base64");
}
const scratch = resolve(
local,
"diagnostics/grafana-request-" + crypto.randomUUID(),
);
await mkdir(scratch, { recursive: true, mode: 0o700 });
const response = await fetch(
`https://${endpoint.host}:${endpoint.port}${path}`,
{
method: options.method ?? (options.body === undefined ? "GET" : "POST"),
headers,
body:
options.body === undefined ? undefined : JSON.stringify(options.body),
tls: { ca: ca.toString() },
signal: AbortSignal.timeout(15000),
redirect: "manual",
},
);
const meta = {
status: response.status,
content_type: response.headers.get("content-type") ?? "",
};
await Promise.allSettled([
writeFile(resolve(scratch, "http.json"), JSON.stringify(meta), {
flag: "wx",
mode: 0o600,
}),
]);
const reader = response.body?.getReader(),
chunks: Uint8Array[] = [];
let size = 0;
try {
if (reader)
for (;;) {
const { done, value } = await reader.read();
if (done) break;
size += value.length;
if (size > 12 * 1024 * 1024) await reader.cancel();
requireThat(
size <= 12 * 1024 * 1024,
"GRAFANA_TEST_CLIENT_LIMIT_12MIB",
);
chunks.push(value);
}
} finally {
reader?.releaseLock();
}
const bytes = Buffer.concat(chunks);
await Promise.allSettled([
writeFile(resolve(scratch, "response"), bytes, { flag: "wx", mode: 0o600 }),
]);
let body: any = null;
if (meta.content_type.includes("application/json")) {
try {
body = JSON.parse(bytes.toString());
} catch {
requireThat(response.status >= 400, "GRAFANA_SUCCESS_INVALID_JSON");
}
}
return { ...meta, body, bytes: bytes.length };
}
export function assertOriginalGrafanaRules(actual: any[], expected: any[]) {
requireThat(
Array.isArray(actual) &&
actual.length === expected.length &&
expected.length > 0,
"GRAFANA_INCIDENT_RULES_MISSING",
);
for (const e of expected) {
const a = actual.find((r) => r.uid === e.uid),
ae = a?.data?.[2]?.model?.conditions?.[0]?.evaluator,
ee = e.data[2].model.conditions[0].evaluator;
requireThat(
a &&
a.title === e.title &&
a.data?.[0]?.model?.expr === e.data[0].model.expr &&
ae?.type === ee.type &&
Array.isArray(ae.params) &&
ae.params.length === ee.params.length &&
ae.params.every(
(value: any, i: number) =>
typeof value === "number" &&
Number.isFinite(value) &&
value === ee.params[i],
),
"GRAFANA_INCIDENT_RULE_CHANGED",
);
}
}