Multiplayer agent harness for work.
Authenticate qualification directory readiness with portal identity (#1166)
Regan Bell3029c801de31
diff --git a/test/directory-resolve.test.ts b/test/directory-resolve.test.tsindex bf4828a11..0c52b6f3b 100644--- a/test/directory-resolve.test.ts+++ b/test/directory-resolve.test.ts@@ -10,6 +10,7 @@ import { signedRequestHeaders } from "../src/auth/source-auth-sign.ts";import { signRequest } from "../src/auth/source-auth.ts";import { createServer } from "../src/api/server.ts";import { mintCapabilityToken, CAPABILITY_TTL_MS } from "../src/auth/capability-token.ts";+import { CoreClient } from "./live-slack/core.ts";import { testConfig } from "./support/test-config.ts";const SECRET = "directory-resolve-secret".repeat(3);@@ -204,3 +205,43 @@ describe("a deployment without the Slack surface (the directory store is never passert.deepEqual(await matchesOf("[email protected]"), [], "a rejected push must not land");});});++describe("qualification membership readiness with signed portal identity enforcement", () => {+ it("authenticates directory reads with the separate portal key", async () => {+ const portalSecret = "readiness-portal-key-distinct-from-source";+ const originalPortalSecret = process.env.PORTAL_IDENTITY_SECRET;+ const app = buildApp(testConfig({ signingSecret: SECRET }));+ await app.app.upsertDirectory([+ { principalId: process.env.LIVE_E2E_ADMIN_PRINCIPAL || "admin-alice", displayName: "Admin", type: "internal" },+ { principalId: "[email protected]", displayName: "QA", type: "internal", slackId: "UQA" },+ ]);+ await app.app.upsertChannels(+ [{ channelId: "CQA", name: "qa", isPrivate: false }],+ [{ channelId: "CQA", principalId: "[email protected]" }],+ );+ const server = createServer(app.app, {+ signingSecret: SECRET,+ portalIdentitySecret: portalSecret,+ capabilitySecret: "readiness-capability-key-distinct-from-source",+ requireSignedPortalIdentity: true,+ identity: app.identity,+ });+ await new Promise<void>((resolve) => server.listen(0, resolve));+ const base = `http://localhost:${(server.address() as AddressInfo).port}`;+ try {+ const path = "/v1/directory/resolve?q=UQA";+ const unsigned = await fetch(`${base}${path}`, { headers: signedRequestHeaders(SECRET, "GET", path) });+ assert.equal(unsigned.status, 401);+ process.env.PORTAL_IDENTITY_SECRET = portalSecret;+ const core = new CoreClient(base, SECRET);+ await core.waitForChannelMembership("CQA", "UQA", 5000);+ process.env.PORTAL_IDENTITY_SECRET = SECRET;+ await assert.rejects(core.waitForChannelMembership("CQA", "UQA", 5000), /401.*portal identity required/);+ } finally {+ if (originalPortalSecret === undefined) delete process.env.PORTAL_IDENTITY_SECRET;+ else process.env.PORTAL_IDENTITY_SECRET = originalPortalSecret;+ server.closeAllConnections();+ await new Promise<void>((resolve) => server.close(() => resolve()));+ }+ });+});diff --git a/test/live-slack/core.ts b/test/live-slack/core.tsindex 0a68cab6e..55df721ee 100644--- a/test/live-slack/core.ts+++ b/test/live-slack/core.ts@@ -29,17 +29,16 @@ export class CoreClient {return new CoreClient(this.baseUrl, this.signingSecret, this.orgScope, signal);}- private async request(- method: string,- pathWithQuery: string,- body?: unknown,- extra: Record<string, string> = {},- ): Promise<any> {+ private async request(method: string, pathWithQuery: string, body?: unknown): Promise<any> {+ const orgId = this.orgScope.split(":")[1] ?? "acme";+ const portalSecret = process.env.PORTAL_IDENTITY_SECRET || this.signingSecret;+ const identity = await mintPortalIdentity({ p: ADMIN_PRINCIPAL, exp: Date.now() + 60_000 }, portalSecret);const raw = body === undefined ? "" : JSON.stringify(body);const salted = `${pathWithQuery}${pathWithQuery.includes("?") ? "&" : "?"}_nonce=${crypto.randomUUID()}`;const headers = signedRequestHeaders(this.signingSecret, method, salted, raw, {"content-type": "application/json",- ...extra,+ "x-admin-actor": `${ADMIN_PRINCIPAL}@${orgId}`,+ [PORTAL_IDENTITY_HEADER]: identity,});const deadline = AbortSignal.timeout(120_000);const signal = this.requestSignal ? AbortSignal.any([deadline, this.requestSignal]) : deadline;@@ -49,16 +48,6 @@ export class CoreClient {return data;}- private async admin(method: string, pathWithQuery: string, body?: unknown): Promise<any> {- const orgId = this.orgScope.split(":")[1] ?? "acme";- const portalSecret = process.env.PORTAL_IDENTITY_SECRET || this.signingSecret;- const identity = await mintPortalIdentity({ p: ADMIN_PRINCIPAL, exp: Date.now() + 60_000 }, portalSecret);- return this.request(method, pathWithQuery, body, {- "x-admin-actor": `${ADMIN_PRINCIPAL}@${orgId}`,- [PORTAL_IDENTITY_HEADER]: identity,- });- }-async waitForChannelMembership(channelId: string, slackId: string, timeoutMs = 60_000): Promise<void> {const deadline = AbortSignal.timeout(timeoutMs);const signal = this.requestSignal ? AbortSignal.any([deadline, this.requestSignal]) : deadline;@@ -97,23 +86,26 @@ export class CoreClient {providers: Array<{ name: string; actions: string[] }>;sandboxes: Array<{ id: string; name: string; backend: string; state: string }>;}> {- return this.admin("GET", `/v1/admin/sandboxes/${encodeURIComponent(scopeId)}`);+ return this.request("GET", `/v1/admin/sandboxes/${encodeURIComponent(scopeId)}`);}manageSandbox(scopeId: string, body: Record<string, unknown>): Promise<{ id: string; backend: string }> {- return this.admin("POST", `/v1/admin/sandboxes/${encodeURIComponent(scopeId)}`, body);+ return this.request("POST", `/v1/admin/sandboxes/${encodeURIComponent(scopeId)}`, body);}listSessions(): Promise<{ sessions: SessionSummary[] }> {- return this.admin("GET", `/v1/admin/sessions?scope=${encodeURIComponent(this.orgScope)}&limit=200`);+ return this.request("GET", `/v1/admin/sessions?scope=${encodeURIComponent(this.orgScope)}&limit=200`);}getSession(id: string): Promise<{ session: { threadRef?: string; scopeId?: string }; entries: unknown[] }> {- return this.admin("GET", `/v1/admin/sessions/${encodeURIComponent(id)}?scope=${encodeURIComponent(this.orgScope)}`);+ return this.request(+ "GET",+ `/v1/admin/sessions/${encodeURIComponent(id)}?scope=${encodeURIComponent(this.orgScope)}`,+ );}getSessionLlm(id: string): Promise<{ session: unknown; requests: unknown[] }> {- return this.admin(+ return this.request("GET",`/v1/admin/sessions/${encodeURIComponent(id)}/llm?scope=${encodeURIComponent(this.orgScope)}`,);@@ -129,17 +121,17 @@ export class CoreClient {createdBy?: string;}>;}> {- return this.admin("GET", `/v1/admin/crons?scope=${encodeURIComponent(this.orgScope)}`);+ return this.request("GET", `/v1/admin/crons?scope=${encodeURIComponent(this.orgScope)}`);}listErrors(): Promise<{errors: Array<{ ts: number; category: string; code: string; message: string; sessionId?: string }>;}> {- return this.admin("GET", `/v1/admin/errors?scope=${encodeURIComponent(this.orgScope)}`);+ return this.request("GET", `/v1/admin/errors?scope=${encodeURIComponent(this.orgScope)}`);}resolveDirectory(q: string): Promise<{ members: Array<{ principalId: string; displayName: string }> }> {- return this.admin("GET", `/v1/admin/directory?q=${encodeURIComponent(q)}`);+ return this.request("GET", `/v1/admin/directory?q=${encodeURIComponent(q)}`);}async deleteCron(cron: { id: string; ownerScopeId: string; owner?: string; createdBy?: string }): Promise<void> {