import { readFile, open } from "node:fs/promises"; import { resolve } from "node:path"; import { bao } from "./bao-client"; import { encrypt, decrypt, operatorPassword } from "./operator-crypto"; import { local, root, privateWrite, requireThat, safeFailure } from "./runtime"; export type BaoOperator = { root_token: string; keys_base64: string[] }; export async function unlock(password: string) { return decrypt( await readFile(resolve(local, "operator", "bao.enc"), "utf8"), password, ); } export async function initialize(password: string) { const status = await bao("/sys/init"); requireThat( status.data.initialized === false, "BAO_ALREADY_INITIALIZED_USE_UNSEAL", ); const claim = await open( resolve(local, "operator", "bao-init-claim.json"), "wx", 0o600, ); await claim.writeFile('{"state":"RUNNING"}'); await claim.close(); let response: Awaited>; try { response = await bao("/sys/init", { method: "PUT", body: { secret_shares: 3, secret_threshold: 2 }, }); } catch (error) { // An accepted init can lose its response. Encrypt any partial transport receipt before other diagnostics. const path = resolve(local, "diagnostics", "last-command.json"); if (await Bun.file(path).exists()) { const captured = await readFile(path, "utf8"); await privateWrite( resolve(local, "operator", "bao-init-uncertain.enc"), encrypt({ transport_receipt: captured }, password), ); await privateWrite( path, '{"stage":"BAO_INIT_UNCERTAIN","details":"encrypted operator receipt"}', ); } await privateWrite( resolve(local, "operator", "bao-init-claim.json"), '{"state":"INIT_UNCERTAIN_DO_NOT_RETRY"}', ); throw new Error("BAO_INIT_UNCERTAIN_DO_NOT_RETRY"); } // Persist encrypted init receipt before any readback or unseal; uncertain writes are never automatically retried. await privateWrite( resolve(local, "operator", "bao.enc"), encrypt(response.data, password), ); await privateWrite( resolve(local, "operator", "bao-init-claim.json"), '{"state":"INIT_SAVED_ENCRYPTED"}', ); await unseal(password); await configure(password); console.log("PASS_BAO_NONDEV_INIT_UNSEAL_SCOPED_CERT_AUTH"); } export async function unseal(password: string) { const op = await unlock(password); for (const key of op.keys_base64.slice(0, 2)) await bao("/sys/unseal", { method: "PUT", body: { key } }); const status = await bao("/sys/seal-status"); requireThat(status.data.sealed === false, "BAO_STILL_SEALED"); } export async function configure(password: string) { const op = await unlock(password), token = op.root_token; await bao("/sys/mounts/kv", { method: "POST", token, body: { type: "kv", options: { version: "2" } }, }); await bao("/sys/auth/cert", { method: "POST", token, body: { type: "cert" }, }); const cert = await readFile( resolve(local, "operator", "ca", "ca.crt"), "utf8", ); for (const role of ["api", "worker"]) { const policy = await readFile( resolve(root, "infra/openbao", role + ".hcl"), "utf8", ); await bao("/sys/policies/acl/" + role, { method: "PUT", token, body: { policy }, }); await bao("/auth/cert/certs/" + role, { method: "POST", token, body: { display_name: role, certificate: cert, allowed_common_names: [role], allowed_dns_sans: [role], token_policies: [role], token_no_default_policy: true, token_ttl: 60, token_max_ttl: 300, }, }); } const secrets = decrypt( await readFile(resolve(local, "operator", "services.enc"), "utf8"), password, ); for (const role of ["api", "worker"]) { const suffix = role === "api" ? "Api" : "Worker"; await bao("/kv/data/" + role, { method: "POST", token, body: { data: { database_url: `postgresql://${role}@127.0.0.1:15432/messenger?sslmode=disable`, cache_url: `redis://${role}:${secrets["broker" + suffix]}@127.0.0.1:16379/0`, ...(role === "worker" ? { publisher_url: `redis://publisher:${secrets.brokerPublisher}@127.0.0.1:16379/0`, } : {}), s3_json: JSON.stringify({ Endpoint: "127.0.0.1:19000", Access: secrets["s3" + suffix + "Access"], Secret: secrets["s3" + suffix + "Secret"], Bucket: "messenger-attachments", }), }, }, }); } // OpenBao2.7 manages this device through the server HCL audit stanza. // API-driven device creation is disabled by default; retain that native boundary. } if (import.meta.main) { try { const password = await operatorPassword(); const action = Bun.argv[2]; if (action === "init") await initialize(password); else if (action === "unseal") { await unseal(password); console.log("PASS_BAO_UNSEALED"); } else throw new Error("USE_INIT_OR_UNSEAL"); } catch (e) { safeFailure(e); } }