import { writeFile } from "node:fs/promises"; import { resolve } from "node:path"; import { root, requireThat } from "../scripts/runtime"; import { bao } from "../scripts/bao-client"; import { parseKV } from "../scripts/kv-secrets"; export async function auditBao() { const results = []; for (const role of ["api", "worker"]) { const login = await bao("/auth/cert/login", { role, body: { name: role } }); const auth = login.data.auth; requireThat( typeof auth.client_token === "string" && auth.lease_duration > 0 && auth.lease_duration <= 300 && auth.renewable === true, "BAO_TOKEN_LIFETIME_INVALID", ); requireThat( auth.policies.length === 1 && auth.policies[0] === role, "BAO_POLICY_NOT_SCOPED", ); const token = auth.client_token; const own = await bao("/kv/data/" + role, { role, token }); parseKV(own, role as "api" | "worker"); const other = await bao("/kv/data/" + (role === "api" ? "worker" : "api"), { role, token, expected: [403], }); const admin = await bao("/sys/policies/acl/forbidden", { role, token, body: { policy: 'path "*" {capabilities=["read"]}' }, expected: [403], }); const renew = await bao("/auth/token/renew-self", { role, token, body: { increment: 60 }, }); requireThat( renew.data.auth.lease_duration > 0 && renew.data.auth.lease_duration <= 300, "BAO_RENEWAL_INVALID", ); await bao("/auth/token/revoke-self", { role, token, body: {} }); await bao("/kv/data/" + role, { role, token, expected: [403] }); results.push({ role, policy: role, lease_seconds: auth.lease_duration, own_path: own.status, foreign_path: other.status, policy_write: admin.status, renewed: true, revoked_new_request_denied: true, }); } const forbidden = await bao("/auth/cert/login", { role: "forbidden", body: { name: "api" }, expected: [400, 403], }); requireThat(forbidden.status !== 200, "BAO_FORBIDDEN_IDENTITY_ACCEPTED"); await writeFile( resolve(root, "evidence/security/openbao-policy.json"), JSON.stringify( { non_dev: true, persistent_storage: "pebbledb", service_auth: "native cert auto-auth", results, same_ca_forbidden_identity: forbidden.status, }, null, 2, ) + "\n", ); console.log("PASS_BAO_OWN_PATH_ALLOW_FOREIGN_ADMIN_DENY_RENEW_REVOKE"); }