// FED R6 (#1800, sage-3142): the bus host's boot config carries the runtime's `access` // ({ file, context }) when /access.json exists, with the cli's accessContextFor as the // context. The broker then binds a granted user; with no file, or a file R1's loader refuses, no // user binds and the owner's bus still boots. Scratch roots only. The boot config goes through a // JSON round trip first, as it does over the host's IPC. import test from "node:test"; import assert from "node:assert/strict"; import { chmodSync, writeFileSync } from "node:fs"; import { dirname, join } from "node:path"; import { startBroker } from "../../bus/src/runtime.mjs"; import { bootConfig, loadSystem } from "../src/config.mjs"; import { fixture, tmp } from "./helpers.mjs"; const SSO = { sso: { providers: [{ id: "authentik", subjects: [{ sub: "jason-sub" }] }] } }; const DOC = { accessVersion: 1, revision: 1, users: [{ id: "alice", home: "factory", subjects: [{ provider: "authentik", sub: "alice-sub" }] }], grants: [{ id: "g-1", user: "alice", business: "acme", projects: ["*"], instances: ["*"], level: "collaborator" }], }; // A scratch instance whose system config names the sign-in provider and one peer, and the boot // config bootConfig makes for it. `file` is the access file's text, or null for none. async function boot(t, file) { const f = fixture(tmp(t, "mosaic-access-boot-"), "acme"); const system = { ...loadSystem({ env: f.env }), console: SSO, federation: { self: "hub", peers: [{ id: "factory", origin: "https://factory.example.test", label: "Software factory" }] }, }; const path = join(dirname(f.config), "access.json"); if (file !== null) { writeFileSync(path, file); chmodSync(path, 0o600); } const warnings = []; const { trackers, ...config } = JSON.parse(JSON.stringify(bootConfig({ system, businessId: "acme", env: f.env, warn: (w) => warnings.push(w) }))); const runtime = await startBroker(config); t.after(() => runtime.close()); const bind = (user) => { try { return runtime.broker.bindUser({ business: "acme", user }) ? "bound" : "none"; } catch (e) { return e.code; } }; return { config, path, bind, warnings }; } test("R6 B1 a boot with access.json binds a granted user, whose home is a configured peer", async (t) => { const s = await boot(t, JSON.stringify(DOC)); assert.equal(s.config.access.file, s.path); assert.deepEqual(s.config.access.context.peers, ["factory"]); assert.deepEqual(s.config.access.context.ssoSubjects, [{ provider: "authentik", sub: "jason-sub" }]); assert.equal(s.bind("alice"), "bound"); assert.equal(s.bind("bob"), "access-revoked"); }); test("R6 B2 a boot without access.json has no access and binds no user", async (t) => { const s = await boot(t, null); assert.equal(Object.hasOwn(s.config, "access"), false); assert.equal(s.bind("alice"), "access-revoked"); }); test("R6 B3 a boot with a file R1's loader refuses still boots and binds no user", async (t) => { for (const text of ["{", JSON.stringify({ ...DOC, users: [{ ...DOC.users[0], home: "elsewhere" }] })]) { const s = await boot(t, text); assert.equal(s.config.access.file, s.path); assert.equal(s.bind("alice"), "access-revoked", text); } });