Files
mtusi2026lab7/student-tests/openobserve-transport.ts

251 lines
7.1 KiB
TypeScript

import { readFile, writeFile, mkdir } from "node:fs/promises";
import { resolve } from "node:path";
import { connect } from "node:net";
import {
root,
local,
compose,
run,
requireThat,
safeFailure,
} from "../scripts/runtime";
import { verifyBackendHandover } from "../scripts/preflight-build";
import { containerIP } from "../scripts/runtime-inspect";
import { openObserveRequest } from "../scripts/openobserve";
import { tlsProbe } from "./transport";
import { auditMatrix } from "./matrix";
async function refused(ip: string, port: number) {
return new Promise<void>((done, fail) => {
const socket = connect({ host: ip, port });
const timer = setTimeout(
() => finish(new Error("OPENOBSERVE_RAW_PORT_TIMEOUT")),
2000,
);
let settled = false;
function finish(error?: Error) {
if (settled) return;
settled = true;
clearTimeout(timer);
socket.destroy();
error ? fail(error) : done();
}
socket.on("connect", () =>
finish(new Error("OPENOBSERVE_RAW_PORT_EXPOSED")),
);
socket.on("error", (error: NodeJS.ErrnoException) =>
finish(
error.code === "ECONNREFUSED"
? undefined
: new Error("OPENOBSERVE_RAW_PORT_WRONG_FAILURE"),
),
);
});
}
async function main() {
requireThat(
root === "/mnt/data/home/bench-saves/mtuci-vp7-security-20261115",
"OPENOBSERVE_TRANSPORT_REMOTE_BOUNDARY_REQUIRED",
);
const receipt = resolve(
root,
"evidence/openobserve",
"transport-" + crypto.randomUUID() + ".json",
);
await mkdir(resolve(receipt, ".."), { recursive: true });
await writeFile(
receipt,
JSON.stringify({ state: "RUNNING", stage: "SOURCE_LEASE" }),
{ flag: "wx", mode: 0o600 },
);
let stage = "SOURCE_LEASE",
started = false,
primary: unknown;
try {
const lease = await verifyBackendHandover();
requireThat(
lease.sha256 ===
"09fe19253582282e489406600f3ee20c1e52d786778d6ce90f9d9da4146e5609",
"OPENOBSERVE_TRANSPORT_SOURCE_MISMATCH",
);
stage = "OWN_STAND_IDLE";
requireThat(
(
await run([
"docker",
"ps",
"--filter",
"label=com.docker.compose.project=mtuci-vp7-security",
"--format",
"{{.ID}}",
])
).out.trim() === "",
"OPENOBSERVE_EXISTING_OWN_STAND_RUNNING",
);
stage = "NATIVE_STORAGE_START";
started = true;
await compose([
"up",
"-d",
"--no-build",
"openobserve-gateway",
"openobserve",
]);
const password = await readFile(
resolve(root, ".local-preflight-key"),
"utf8",
);
requireThat(
/^[a-f0-9]{64}$/.test(password),
"INVALID_PREFLIGHT_SESSION_KEY",
);
stage = "QUERY_READY";
let ready = false;
const deadline = Date.now() + 60_000;
while (Date.now() < deadline) {
try {
if (
(
await openObserveRequest("/api/default/streams", {
password,
account: "query",
role: "grafana",
})
).status === 200
) {
ready = true;
break;
}
} catch {}
await Bun.sleep(500);
}
requireThat(ready, "OPENOBSERVE_TRANSPORT_START_TIMEOUT");
stage = "BOTH_LISTENER_MTLS_NEGATIVES";
await auditMatrix("openobserve");
stage = "QUERY_METHOD_PATH_DENIALS";
const ip = await containerIP("openobserve-gateway");
const authorization = await readFile(
resolve(local, "config/openobserve_query_auth"),
"utf8",
);
requireThat(
/^Basic [A-Za-z0-9+/]+={0,2}$/.test(authorization),
"INVALID_OPENOBSERVE_AUTH_HEADER_FILE",
);
for (const path of [
"/api/default/v1/logs",
"/api/default/users",
"/api/foreign/_search",
]) {
const result = await tlsProbe(
ip,
5082,
"openobserve-gateway",
"grafana",
`POST ${path} HTTP/1.1\r\nHost: localhost\r\nAuthorization: ${authorization}\r\nContent-Length: 0\r\nConnection: close\r\n\r\n`,
);
requireThat(
result.authorized && /HTTP\/1\.[01] 403/.test(result.data),
"OPENOBSERVE_GRAFANA_PATH_NOT_DENIED",
);
}
stage = "NATIVE_QUERY_ACCOUNT_NONADMIN";
const rotate = await openObserveRequest(
"/api/default/service_accounts/vp7-query@example.invalid?rotateToken=true",
{ password, account: "query", method: "PUT", body: {} },
);
requireThat(
rotate.status === 403,
"OPENOBSERVE_QUERY_ACCOUNT_ADMIN_ROTATE_NOT_DENIED",
);
stage = "RAW_STORAGE_LISTENERS_CLOSED";
await refused(ip, 15080);
await refused(ip, 5081);
stage = "POSITIVE_AFTER_NEGATIVES";
requireThat(
(
await openObserveRequest("/api/default/streams", {
password,
account: "query",
role: "grafana",
})
).status === 200,
"OPENOBSERVE_QUERY_AFTER_NEGATIVE_FAILED",
);
stage = "SOURCE_POSTGUARD";
await lease.verify();
await writeFile(
receipt,
JSON.stringify(
{
state: "PASS_SCOPED_OPENOBSERVE_TRANSPORT",
stage,
source_handover_sha256: lease.sha256,
listeners: [5080, 5082],
tls_matrix:
"separate tls-matrix UUID receipt; positive before/after each negative set",
grafana_ingest_admin_foreign_org_denied: true,
native_query_account_admin_rotate_http: rotate.status,
native_role_read_only: false,
raw_ports_refused: [15080, 5081],
timeouts_counted_as_denial: false,
native_bao: false,
native_grafana: false,
},
null,
2,
) + "\n",
);
console.log("PASS_OPENOBSERVE_NATIVE_MTLS_SCOPE_AND_CLOSED_RAW_LISTENERS");
} catch (error) {
primary = error;
await Promise.allSettled([
writeFile(
receipt,
JSON.stringify({
state: "FAILED",
stage,
code:
error instanceof Error &&
/^[A-Z][A-Za-z0-9_:-]{0,240}$/.test(error.message)
? error.message
: "PRIVATE_DETAILS_ONLY",
}) + "\n",
),
]);
throw error;
} finally {
if (started) {
const stop = await Promise.allSettled([
compose(["stop", "openobserve", "openobserve-gateway"], {
allowFailure: true,
}),
]);
const stopped =
stop[0].status === "fulfilled" && stop[0].value.code === 0;
await Promise.allSettled([
Promise.resolve().then(async () => {
const saved = JSON.parse(await readFile(receipt, "utf8"));
saved.stop_exit =
stop[0].status === "fulfilled" ? stop[0].value.code : null;
if (!stopped && !primary) saved.state = "FAILED_TEARDOWN";
await writeFile(receipt, JSON.stringify(saved, null, 2) + "\n");
}),
]);
if (!stopped && !primary)
throw new Error("OPENOBSERVE_TRANSPORT_STOP_FAILED");
console.log(
stopped
? "OWN_TRANSPORT_STORAGE_STOPPED"
: "OWN_TRANSPORT_STOP_REQUIRES_INSPECTION",
);
}
}
}
if (import.meta.main) {
try {
await main();
} catch (error) {
safeFailure(error);
}
}