import { mkdir, readFile, writeFile } from "node:fs/promises"; import { resolve } from "node:path"; import { randomBytes } from "node:crypto"; import { local, privateWrite, requireThat, run, safeFailure } from "./runtime"; import { decrypt, encrypt, operatorPassword } from "./operator-crypto"; import { createAccountProvisioner, createAccountWorkflow, validateStoredAccount, type Account, type Principal, } from "./openobserve-accounts"; import { containerIP } from "./runtime-inspect"; import { validateOpenObservePath } from "./openobserve-request-path"; import { parseOpenObserveResponse } from "./openobserve-response"; const ownerPath = resolve(local, "operator/openobserve-owner.enc"); const accountPath = (name: Principal) => resolve(local, "operator", `openobserve-${name}.enc`); export async function prepareOpenObserve(password: string) { await mkdir(resolve(local, "config"), { recursive: true, mode: 0o700 }); await mkdir(resolve(local, "data/openobserve"), { recursive: true, mode: 0o700, }); await mkdir(resolve(local, "plugins"), { recursive: true, mode: 0o700 }); if (await Bun.file(ownerPath).exists()) { decrypt(await readFile(ownerPath, "utf8"), password); requireThat( await Bun.file(resolve(local, "config/openobserve.env")).exists(), "OPENOBSERVE_PREPARE_INCOMPLETE", ); return; } const owner = { user: "vp7-owner@example.invalid", token: "Aa1!" + randomBytes(32).toString("hex"), }; await writeFile(ownerPath, encrypt(owner, password), { flag: "wx", mode: 0o600, }); await writeFile( resolve(local, "config/openobserve.env"), `ZO_ROOT_USER_EMAIL=${owner.user}\nZO_ROOT_USER_PASSWORD=${owner.token}\n`, { flag: "wx", mode: 0o600 }, ); // Compose validates every service's env_file even before Grafana is started; this placeholder contains no query credential. await writeFile( resolve(local, "config/grafana-query.env"), "# Dedicated query service account is provisioned before Grafana startup.\n", { flag: "wx", mode: 0o600 }, ); } export async function savedAccount(name: Principal, password: string) { requireThat( name === "ingest" || name === "query", "OPENOBSERVE_ACCOUNT_INVALID_PRINCIPAL", ); return validateStoredAccount( name, decrypt(await readFile(accountPath(name), "utf8"), password), ); } export async function openObserveRequest( path: string, options: { password: string; method?: string; body?: unknown; account?: Principal; credentials?: Account; role?: "operator" | "collector" | "grafana"; kind?: | "operator" | "trace_streams" | "worker_trace" | "trace_chain" | "correlated_logs" | "metric_http_requests" | "metric_jobs_processed" | "metric_http_p95" | "metric_queue_depth" | "metric_database_observer"; }, ) { validateOpenObservePath(path); requireThat( (options.role !== "grafana" || options.account === "query") && (options.role !== "collector" || options.account === "ingest"), "OPENOBSERVE_TRANSPORT_ACCOUNT_MISMATCH", ); requireThat( !options.credentials || options.account, "OPENOBSERVE_CREDENTIALS_WITHOUT_PRINCIPAL", ); const account = options.account ? options.credentials ? validateStoredAccount(options.account, options.credentials) : await savedAccount(options.account, options.password) : decrypt(await readFile(ownerPath, "utf8"), options.password); const role = options.role ?? "operator", ip = await containerIP("openobserve-gateway"); const scratch = resolve( local, "operator", "openobserve-request-" + crypto.randomUUID(), ); await mkdir(scratch, { recursive: false, mode: 0o700 }); const kind = options.kind ?? "operator"; requireThat( [ "operator", "trace_streams", "worker_trace", "trace_chain", "correlated_logs", "metric_http_requests", "metric_jobs_processed", "metric_http_p95", "metric_queue_depth", "metric_database_observer", ].includes(kind), "OPENOBSERVE_REQUEST_KIND_INVALID", ); await writeFile( resolve(scratch, "request-meta.json"), JSON.stringify({ kind, path, method: options.method ?? (options.body === undefined ? "GET" : "POST"), }), { flag: "wx", mode: 0o600 }, ); const headerPath = resolve(scratch, "headers"), responsePath = resolve(scratch, "response"); await writeFile( headerPath, `Authorization: Basic ${Buffer.from(account.user + ":" + account.token).toString("base64")}\nContent-Type: application/json\n`, { flag: "wx", mode: 0o600 }, ); await writeFile(responsePath, "", { flag: "wx", mode: 0o600 }); const port = role === "collector" ? 5080 : 5082; const args = [ "curl", "--silent", "--show-error", "--noproxy", "*", "--connect-timeout", "2", "--max-time", "10", "--max-filesize", "8388608", "--cacert", resolve(local, "pki", role, "ca.crt"), "--cert", resolve(local, "pki", role, "cert.pem"), "--key", resolve(local, "pki", role, "key.pem"), "--resolve", `openobserve-gateway:${port}:${ip}`, "--header", "@" + headerPath, "--request", options.method ?? (options.body === undefined ? "GET" : "POST"), "--url", `https://openobserve-gateway:${port}${path}`, "--output", responsePath, "--write-out", "%{http_code}\n%{content_type}", ]; if (options.body !== undefined) args.push("--data-binary", "@-"); const result = await run(args, { input: options.body === undefined ? undefined : JSON.stringify(options.body), allowFailure: true, }); await Promise.allSettled([ writeFile( resolve(scratch, "transport.json"), JSON.stringify({ code: result.code, status: result.out, stderr: result.err, }), { flag: "wx", mode: 0o600 }, ), ]); // The native response, including a one-time token, is retained0600 before parsing/status guards. requireThat(result.code === 0, "OPENOBSERVE_REQUEST_TRANSPORT_FAILED"); const [statusText, contentType] = result.out.split("\n"), status = Number(statusText); requireThat( Number.isInteger(status) && status >= 100 && status <= 599, "OPENOBSERVE_REQUEST_INVALID_STATUS", ); const bytes = await readFile(responsePath); requireThat(bytes.length <= 8388608, "OPENOBSERVE_RESPONSE_TOO_LARGE"); const text = bytes.toString("utf8"); await Promise.allSettled([ writeFile( resolve(scratch, "response-meta.json"), JSON.stringify({ kind, status, content_type: contentType, bytes: bytes.length, body_shape: /^\s*[\[{]/.test(text) ? "json-looking" : "text", }), { flag: "wx", mode: 0o600 }, ), ]); return parseOpenObserveResponse(status, text); } export async function provisionOpenObserve(password: string) { const provision = createAccountProvisioner({ create: (path, body) => openObserveRequest(path, { password, body }), save: async (name, account) => { await writeFile(accountPath(name), encrypt(account, password), { flag: "wx", mode: 0o600, }); }, }); const accounts = {} as Record; for (const name of ["ingest", "query"] as const) accounts[name] = (await Bun.file(accountPath(name)).exists()) ? await savedAccount(name, password) : await provision(name); const verify = createAccountWorkflow({ positive: async (name, credentials) => { const result = await openObserveRequest("/api/default/streams", { password, account: name, credentials, role: name === "query" ? "grafana" : "operator", }); requireThat( result.status !== 200 || Array.isArray((result.data as any)?.list), `OPENOBSERVE_CREDENTIAL_${name.toUpperCase()}_INVALID_NATIVE_RESPONSE`, ); return result.status; }, deny: async (name, credentials) => { const result = await openObserveRequest( `/api/default/service_accounts/vp7-${name}@example.invalid?rotateToken=true`, { password, account: name, credentials, method: "PUT", body: {} }, ); return result.status; }, publish: async (values) => { for (const name of ["ingest", "query"] as const) { const account = values[name]; const authorization = "Basic " + Buffer.from(account.user + ":" + account.token).toString("base64"); requireThat( !/[\r\n]/.test(authorization), "OPENOBSERVE_AUTH_CONTAINS_NEWLINE", ); await privateWrite( resolve(local, "config", `openobserve_${name}_auth`), authorization, ); if (name === "query") { requireThat( /^[A-Za-z0-9@._-]+$/.test(account.user) && /^[A-Za-z0-9!#%+,.\/:;=?@^_~-]+$/.test(account.token), "OPENOBSERVE_QUERY_ENV_UNSAFE", ); await privateWrite( resolve(local, "config/grafana-query.env"), `OO_QUERY_USER=${account.user}\nOO_QUERY_PASSWORD=${account.token}\n`, ); } } }, }); const controls = await verify(accounts); console.log( "PASS_OWN_OPENOBSERVE_ACCOUNTS_CAPTURED_NATIVE_RIGHTS_REQUIRE_GATEWAY", ); return controls; } if (import.meta.main) { try { const action = Bun.argv[2]; if (action === "prepare") await prepareOpenObserve(await operatorPassword()); else if (action === "provision") await provisionOpenObserve(await operatorPassword()); else throw new Error("OPENOBSERVE_ACTION_PREPARE_OR_PROVISION_REQUIRED"); } catch (error) { safeFailure(error); } }