Multiplayer agent harness for work.
fix: unify email-auth and directory identity (#689)
Joshua Francef59bccf32f40
diff --git a/cli/package-lock.json b/cli/package-lock.jsonindex 8b469f6a2..471eabca4 100644--- a/cli/package-lock.json+++ b/cli/package-lock.json@@ -1,12 +1,12 @@{"name": "@yc-software/qm",- "version": "0.1.6",+ "version": "0.1.7","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "@yc-software/qm",- "version": "0.1.6",+ "version": "0.1.7","license": "MIT","bin": {"qm": "dist/bin/qm.js"diff --git a/cli/package.json b/cli/package.jsonindex 4fbc357d4..588271b14 100644--- a/cli/package.json+++ b/cli/package.json@@ -1,6 +1,6 @@{"name": "@yc-software/qm",- "version": "0.1.6",+ "version": "0.1.7","license": "MIT","description": "Control-plane CLI for portable QM deployments on Docker, Fly, and AWS.","type": "module",diff --git a/cli/src/backends/fly.ts b/cli/src/backends/fly.tsindex 8507d95e0..b1fefe389 100644--- a/cli/src/backends/fly.ts+++ b/cli/src/backends/fly.ts@@ -220,6 +220,29 @@ function stageSecret(app: string, name: string, value: string): void {if (result.status !== 0) throw new CliError(`failed to stage ${name} on ${app}`);}+export function stageFlyEmailAllowlist(+ config: QmConfig,+ configDir: string,+ selectedWorkloads: ReadonlySet<string>,+): void {+ const values = readEnvFile(join(configDir, ".env"));+ const value = deploymentSecretValue("AUTH_ALLOWED_EMAILS", values.get("AUTH_ALLOWED_EMAILS"));+ if (value === undefined) return;+ if (isInvalidSecret("AUTH_ALLOWED_EMAILS", value)) {+ throw new CliError("required secret AUTH_ALLOWED_EMAILS is missing or invalid");+ }+ const secret = computedSecrets(config).find((candidate) => candidate.name === "AUTH_ALLOWED_EMAILS");+ if (!secret) return;+ const staged: string[] = [];+ for (const [workload, names] of secretDestinations(secret)) {+ if (!selectedWorkloads.has(workload)) continue;+ const app = `${appPrefixOf(config)}-${workload}`;+ for (const name of names) stageSecret(app, name, value);+ staged.push(app);+ }+ if (staged.length) step(`AUTH_ALLOWED_EMAILS: staged from .env on ${staged.join(", ")}`);+}+function flySensitive(args: string[], failure: string): string {const result = spawnSync(flyBin(), args, { encoding: "utf8" });if (result.status !== 0) throw new CliError(failure);@@ -1108,6 +1131,10 @@ export async function flyUp(config: QmConfig, configDir: string, opts: FlyUpOptsreturn;}+ if (!opts.dryRun) {+ stageFlyEmailAllowlist(config, configDir, new Set([...services, ...plugins.map((plugin) => plugin.name)]));+ }+const gateSecrets = (app: string, header: string, path: string, required: string[], timingKey: string): boolean => {const existing = timing.time(timingKey, "secret checks", () => secretNames(app)) ?? new Set<string>();const missing = required.filter((sec) => !existing.has(sec));diff --git a/cli/src/secrets.ts b/cli/src/secrets.tsindex cfa187cc5..f03e41aec 100644--- a/cli/src/secrets.ts+++ b/cli/src/secrets.ts@@ -334,6 +334,20 @@ export const FIRST_PARTY_SECRET_SPECS: readonly SecretSpec[] = [required: { when: { kind: "env-absent", service: "auth", name: "AUTH_ALLOWED_EMAIL_DOMAIN" } },description: "Comma-separated email addresses allowed to sign in through the built-in broker.",},+ {+ name: "AUTH_ALLOWED_EMAILS",+ service: "core",+ required: {+ when: {+ kind: "all",+ conditions: [+ { kind: "service-enabled", service: "auth" },+ { kind: "env-absent", service: "auth", name: "AUTH_ALLOWED_EMAIL_DOMAIN" },+ ],+ },+ },+ description: "Email-auth principals protected from unrelated directory-source deactivation.",+ },{name: "AUTH_ALLOWED_EMAILS",service: "portal",diff --git a/cli/test/auth-broker.test.ts b/cli/test/auth-broker.test.tsindex 0f0c8d2ff..a0fc69e55 100644--- a/cli/test/auth-broker.test.ts+++ b/cli/test/auth-broker.test.ts@@ -1,6 +1,6 @@import { test } from "node:test";import assert from "node:assert/strict";-import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";+import { chmodSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import { dirname, join } from "node:path";import { fileURLToPath } from "node:url";@@ -9,6 +9,7 @@ import { derivedTomlFor } from "../src/backends/fly.ts";import { serviceEnvironment } from "../src/backends/aws.ts";import { dockerServiceEnv } from "../src/backends/docker.ts";import { computedSecrets, runtimeSecretNames, secretsForService } from "../src/secrets.ts";+import { stageFlyEmailAllowlist } from "../src/backends/fly.ts";import { isReservedContainerName, SERVICE_NAMES, serviceDef } from "../src/services.ts";const repoRoot = join(dirname(fileURLToPath(import.meta.url)), "..", "..");@@ -162,6 +163,7 @@ test("without a configured domain the allowlist becomes a required secret on botassert.ok(allowed.required);assert.deepEqual(runtimeSecretNames("auth", allowed), ["AUTH_ALLOWED_EMAILS"]);assert.deepEqual(runtimeSecretNames("portal", allowed), ["OIDC_ALLOWED_EMAILS"]);+ assert.deepEqual(runtimeSecretNames("core", allowed), ["AUTH_ALLOWED_EMAILS"]);const names = new Set(computedSecrets(config).map((secret) => secret.name));for (const name of ["SMTP_HOST", "SMTP_USERNAME", "SMTP_PASSWORD"]) assert.ok(names.has(name), name);assert.ok(!names.has("RESEND_API_KEY"));@@ -216,3 +218,30 @@ test("a broker deployment with no allowlist at all is refused once secret values);assert.doesNotThrow(() => validatePortalTrust(brokerConfig(), "config", new Map()));});++test("fly up staging copies a changed .env email allowlist to auth, portal, and core", () => {+ const dir = mkdtempSync(join(tmpdir(), "qm-allowlist-up-"));+ const fly = join(dir, "fly");+ const log = join(dir, "fly.log");+ writeFileSync(+ fly,+ `#!/usr/bin/env node+const fs=require("node:fs"); fs.appendFileSync(${JSON.stringify(log)}, process.argv.slice(2).join(" ")+" value="+fs.readFileSync(0,"utf8")+"\\n");`,+ );+ chmodSync(fly, 0o755);+ writeFileSync(join(dir, ".env"), "[email protected],[email protected]\n");+ const config = configWith(configText({ env: `{ "auth": { "AUTH_EMAIL_TRANSPORT": "smtp" } }` }));+ const prior = process.env.FLY_BIN;+ process.env.FLY_BIN = fly;+ try {+ stageFlyEmailAllowlist(config, dir, new Set(["core", "auth", "portal"]));+ } finally {+ if (prior === undefined) delete process.env.FLY_BIN;+ else process.env.FLY_BIN = prior;+ }+ const calls = readFileSync(log, "utf8");+ const prefix = "acme";+ assert.match(calls, new RegExp(`-a ${prefix}-core AUTH_ALLOWED_EMAILS=- [email protected],[email protected]`));+ assert.match(calls, new RegExp(`-a ${prefix}-auth AUTH_ALLOWED_EMAILS=- [email protected],[email protected]`));+ assert.match(calls, new RegExp(`-a ${prefix}-portal OIDC_ALLOWED_EMAILS=- [email protected],[email protected]`));+});diff --git a/src/api/app-messaging.ts b/src/api/app-messaging.tsindex 86677da3d..d2f2ccea6 100644--- a/src/api/app-messaging.ts+++ b/src/api/app-messaging.ts@@ -89,6 +89,12 @@ export function createMessagingMethods(const contextRequests = deps.contextRequests ?? createMemoryMap<SurfaceContextRequest>();const contextRequestListeners = new Set<(request: SurfaceContextRequest) => void>();const contextRequestTokens = new Map<string, string>();+ const emailAuthMembers = deps.emailAuthMembers ?? [];+ const mergedDirectoryMembers = async () => {+ const stored = await deps.directory.list();+ const seen = new Set(stored.map((member) => personKey(member.principalId)));+ return [...stored, ...emailAuthMembers.filter((member) => !seen.has(personKey(member.principalId)))];+ };return {async createCron(input) {@@ -394,20 +400,30 @@ export function createMessagingMethods(...(isPrivate !== undefined ? { isPrivate } : {}),});},- resolveRecipient(query) {- return deps.directory.resolve(query);+ async resolveRecipient(query) {+ const stored = await deps.directory.resolve(query);+ if (stored.kind !== "none") return stored;+ const key = personKey(query);+ const member = emailAuthMembers.find(+ (candidate) => personKey(candidate.principalId) === key || personKey(candidate.displayName) === key,+ );+ return member ? { kind: "one", member } : { kind: "none" };},resolveChannel(query) {return deps.directory.resolveChannel(query);},directoryMembers() {- return deps.directory.list();+ return mergedDirectoryMembers();},directoryChannels() {return deps.directory.listChannels();},- directoryMember(principalId) {- return deps.directory.get(principalId);+ async directoryMember(principalId) {+ return (+ (await deps.directory.get(principalId)) ??+ emailAuthMembers.find((member) => personKey(member.principalId) === personKey(principalId)) ??+ null+ );},samePerson(a, b) {return samePersonInDirectory(deps.directory, a, b);diff --git a/src/api/app-types.ts b/src/api/app-types.tsindex 7d4bcc319..e33509529 100644--- a/src/api/app-types.ts+++ b/src/api/app-types.ts@@ -537,6 +537,7 @@ export interface AppDeps {webhooks: WebhookStore;deliveries: DeliveryStore;directory: DirectoryStore;+ emailAuthMembers?: DirectoryMember[];projects?: ProjectStore;deploy: DeployService;deploymentLayer?: DeploymentLayerRuntime;diff --git a/src/api/routes/admin/users.ts b/src/api/routes/admin/users.tsindex c629b2379..2cfd57974 100644--- a/src/api/routes/admin/users.ts+++ b/src/api/routes/admin/users.ts@@ -29,12 +29,12 @@ export async function listUsers(ctx: ApiCtx): Promise<void> {}export async function searchDirectory(ctx: ApiCtx): Promise<void> {- const { res, deps, url } = ctx;+ const { res, deps, app, url } = ctx;const actor = await authorizeAdmin(ctx, orgScope(deps));if (!actor) return;const q = (url.searchParams.get("q") ?? "").trim();if (!q || !deps.directory) return sendJson(res, 200, { members: [] });- const r = await deps.directory.resolve(q);+ const r = await app.resolveRecipient(q);let members: DirectoryMember[] = [];if (r.kind === "one") members = [r.member];else if (r.kind === "ambiguous") members = r.candidates;diff --git a/src/config.ts b/src/config.tsindex 4d1fb16e5..50efe23e8 100644--- a/src/config.ts+++ b/src/config.ts@@ -60,6 +60,7 @@ export interface Config {piSystemCacheSplit: boolean;sessionTapeMode: "shadow" | "serve";adminGrants?: string;+ emailAuthPrincipals?: string[];rateLimitPerWindow: number;rateLimitWindowMs: number;budgetUsdPerWindow?: number;@@ -795,6 +796,17 @@ export function loadConfig(env: NodeJS.ProcessEnv = process.env): Config {...(modelProvider ? { modelProvider } : {}),providerBaseUrls,...(env.ADMIN_GRANTS ? { adminGrants: env.ADMIN_GRANTS } : {}),+ ...(env.AUTH_ALLOWED_EMAILS+ ? {+ emailAuthPrincipals: [+ ...new Set(+ env.AUTH_ALLOWED_EMAILS.split(",")+ .map((email) => email.trim().toLowerCase())+ .filter(Boolean),+ ),+ ],+ }+ : {}),piCaptureRequests: boolEnvStrict("PI_CAPTURE_REQUESTS", env.PI_CAPTURE_REQUESTS) ?? true,piSystemCacheSplit: boolEnvStrict("PI_SYSTEM_CACHE_SPLIT", env.PI_SYSTEM_CACHE_SPLIT) ?? false,sessionTapeMode: env.SESSION_TAPE_MODE === "shadow" ? "shadow" : "serve",diff --git a/src/deployment/secret-schema.ts b/src/deployment/secret-schema.tsindex 265cc4258..4fce5abd6 100644--- a/src/deployment/secret-schema.ts+++ b/src/deployment/secret-schema.ts@@ -12,6 +12,7 @@ type SecretGate =| "google-oauth"| "dropbox-oauth"| "linear-oauth"+ | "email-auth"| "model-anthropic"| "model-openai"| "model-openrouter";@@ -27,6 +28,7 @@ export const CORE_SECRET_SPECS: readonly RuntimeSecretSpec[] = [{ name: "CORE_SIGNING_SECRET", requiredWhen: "production" },{ name: "PORTAL_IDENTITY_SECRET", requiredWhen: "production" },{ name: "SKILL_SIGNING_SECRET", requiredWhen: "production" },+ { name: "AUTH_ALLOWED_EMAILS", requiredWhen: "email-auth" },{ name: "OPENAI_API_KEY", requiredWhen: ["codex", "model-openai"] },{ name: "ANTHROPIC_API_KEY", requiredWhen: "model-anthropic" },{ name: "OPENROUTER_API_KEY", requiredWhen: "model-openrouter" },@@ -53,6 +55,7 @@ const GATE_PREDICATES: Readonly<Record<SecretGate, (env: NodeJS.ProcessEnv) => b"google-oauth": (env) => Boolean(env.GOOGLE_OAUTH_CLIENT_ID),"dropbox-oauth": (env) => Boolean(env.DROPBOX_OAUTH_CLIENT_ID),"linear-oauth": (env) => Boolean(env.LINEAR_OAUTH_CLIENT_ID),+ "email-auth": (env) => env.AUTH_ALLOWED_EMAILS !== undefined,"model-anthropic": (env) => env.MODEL_PROVIDER?.trim() === "anthropic","model-openai": (env) => env.MODEL_PROVIDER?.trim() === "openai","model-openrouter": (env) => env.MODEL_PROVIDER?.trim() === "openrouter",diff --git a/src/identity/identity-service.ts b/src/identity/identity-service.tsindex bcf3f479d..9790f08eb 100644--- a/src/identity/identity-service.ts+++ b/src/identity/identity-service.ts@@ -30,16 +30,24 @@ export interface IdentityService extends IdentityProvider {refresh(): Promise<void>;}-export function createIdentityService(backing?: DurableMap<DeactivationRecord>): IdentityService {+export function createIdentityService(+ backing?: DurableMap<DeactivationRecord>,+ opts: { directorySyncProtected?: readonly string[] } = {},+): IdentityService {const store = backing ?? createMemoryMap<DeactivationRecord>();const deactivated = new Map<string, DeactivationRecord>();+ const directorySyncProtected = new Set((opts.directorySyncProtected ?? []).map(personKey).filter(Boolean));const REFRESH_TTL_MS = 10_000;let refreshedAt = 0;let refreshP: Promise<void> | null = null;let hydrateP: Promise<void> | null = null;function classify(externalId: string, isExternalGuest?: boolean): Principal {- const type: Principal["type"] = deactivated.has(personKey(externalId)) || isExternalGuest ? "guest" : "internal";+ const record = deactivated.get(personKey(externalId));+ const inactive =+ record?.source === "manual" ||+ (record?.source === "directory-sync" && !directorySyncProtected.has(personKey(externalId)));+ const type: Principal["type"] = inactive || isExternalGuest ? "guest" : "internal";return { id: externalId, type };}@@ -65,7 +73,7 @@ export function createIdentityService(backing?: DurableMap<DeactivationRecord>):async recordDirectorySync(removedIds: string[], presentIds: string[]): Promise<DirectorySyncOutcome> {const outcome: DirectorySyncOutcome = { deactivated: [], reactivated: [] };for (const id of removedIds) {- if (deactivated.has(personKey(id))) continue;+ if (directorySyncProtected.has(personKey(id)) || deactivated.has(personKey(id))) continue;await deactivate(id, "directory-sync");outcome.deactivated.push(id);}diff --git a/src/wiring.ts b/src/wiring.tsindex 3499722b3..fb4c2764d 100644--- a/src/wiring.ts+++ b/src/wiring.ts@@ -429,7 +429,9 @@ export function buildApp(...(config.openrouterApiKey ? { openrouter: config.openrouterApiKey } : {}),},});- const identity = createIdentityService(artifactMap<DeactivationRecord>("deactivated_principals"));+ const identity = createIdentityService(artifactMap<DeactivationRecord>("deactivated_principals"), {+ directorySyncProtected: config.emailAuthPrincipals,+ });void identity.hydrate();const leaderLease: LeaderLease = pgArtifactMap? createPostgresLeaderLease(pgArtifactMap.pool)@@ -1178,6 +1180,15 @@ export function buildApp(webhooks,deliveries,directory,+ ...(config.emailAuthPrincipals?.length+ ? {+ emailAuthMembers: config.emailAuthPrincipals.map((principalId) => ({+ principalId,+ displayName: principalId,+ type: "internal" as const,+ })),+ }+ : {}),projects,environments,deploy: deployService,diff --git a/test/admin-users.test.ts b/test/admin-users.test.tsindex cc6df67b9..f725b0eb6 100644--- a/test/admin-users.test.ts+++ b/test/admin-users.test.ts@@ -238,7 +238,12 @@ test("/v1/admin/users/:principalId: a grant-holder with no sessions still resolv});test("/v1/admin/directory: org_admin resolves a name or id to candidates; empty query → []; non-admin denied", async () => {- const built = buildApp(testConfig({ dataDir: mkdtempSync(join(tmpdir(), "admin-dir-")) }));+ const built = buildApp(+ testConfig({+ dataDir: mkdtempSync(join(tmpdir(), "admin-dir-")),+ emailAuthPrincipals: ["[email protected]"],+ }),+ );const server = createInsecureTestServer(built.app, {admin: built.admin,sessions: built.sessions,@@ -265,6 +270,14 @@ test("/v1/admin/directory: org_admin resolves a name or id to candidates; empty);assert.ok(!d.members.some((m: any) => m.principalId === "[email protected]"), "non-matching member excluded");+ const onboarded = await fetch(`${base}/v1/admin/directory?q=${encodeURIComponent("[email protected]")}`, {+ headers: { "x-admin-actor": "admin-alice@default-org" },+ });+ assert.equal(onboarded.status, 200);+ assert.deepEqual(((await onboarded.json()) as any).members, [+ { principalId: "[email protected]", displayName: "[email protected]" },+ ]);+const empty = await fetch(`${base}/v1/admin/directory`, {headers: { "x-admin-actor": "admin-alice@default-org" },});diff --git a/test/config.test.ts b/test/config.test.tsindex 6a85c9789..4911d65ad 100644--- a/test/config.test.ts+++ b/test/config.test.ts@@ -27,6 +27,14 @@ test("ORG_BRAND_* parses into a validated branding default", () => {assert.deepEqual(loadConfig({ ORG_BRAND_SELF_LABEL: "{{straylight}}" }).brandingDefault, { selfLabel: "straylight" });});+test("AUTH_ALLOWED_EMAILS becomes a normalized email-auth principal set", () => {+ assert.equal(loadConfig({}).emailAuthPrincipals, undefined);+ assert.deepEqual(+ loadConfig({ AUTH_ALLOWED_EMAILS: " [email protected],[email protected],[email protected] " }).emailAuthPrincipals,+ ["[email protected]", "[email protected]"],+ );+});+test("store kinds default to memory and accept postgres", () => {const def = loadConfig({});assert.equal(def.sessionStore, "memory");diff --git a/test/identity-offboarding.test.ts b/test/identity-offboarding.test.tsindex 5d1dd6ceb..ba4201f79 100644--- a/test/identity-offboarding.test.ts+++ b/test/identity-offboarding.test.ts@@ -38,6 +38,7 @@ describe("offboarding: directory sync and the /v1/principals routes drive deactitestConfig({dataDir: mkdtempSync(join(tmpdir(), "offboarding-")),signingSecret: SECRET,+ emailAuthPrincipals: ["[email protected]"],}),);await built.identity.hydrate();@@ -68,6 +69,24 @@ describe("offboarding: directory sync and the /v1/principals routes drive deacti);});+ it("a Slack roster omission does not deactivate or hide a configured email-auth principal", async () => {+ await built.app.upsertDirectory([member("U-stay"), member("[email protected]")]);+ await built.app.upsertDirectory([member("U-stay")]);++ assert.equal(built.identity.classify("[email protected]").type, "internal");+ const resolved = await built.app.resolveRecipient("[email protected]");+ assert.equal(resolved.kind, "one");+ if (resolved.kind === "one") {+ assert.equal(resolved.member.principalId, "[email protected]");+ assert.equal(resolved.member.displayName, "[email protected]");+ }+ assert.ok(+ !(await built.auditLog.events()).some(+ (e) => e.action === "principal.deactivate" && e.principalId === "[email protected]",+ ),+ );+ });+it("the deactivate/reactivate routes flip classification and are audited", async () => {const off = await signedPost("/v1/principals/U-manual/deactivate");assert.equal(off.status, 200);