Multiplayer agent harness for work.
Read deployment broker credentials from BROKER_CREDENTIALS at startup and fail fast on bad entries
Josh France7e439d31732c
diff --git a/src/config.ts b/src/config.tsindex 11d90ba19..0b97d112c 100644--- a/src/config.ts+++ b/src/config.ts@@ -1,4 +1,8 @@import { codexAuthFileForEnv, loadCodexAuthFile } from "./model/chatgpt-auth-file.ts";+import {+ deploymentBrokerCredentialsFromEnv,+ type DeploymentBrokerCredential,+} from "./credentials/deployment-credentials.ts";import { errMessage } from "./util/errors.ts";import type { ExternalSlackPolicies } from "./resolution/external-slack.ts";import { isBearerSecret, isStrongSigningSecret } from "./auth/source-auth.ts";@@ -99,6 +103,7 @@ export interface Config {modelProvider?: ModelProvider;providerBaseUrls: ProviderBaseUrls;modelGateway?: ModelGatewayTransportConfig;+ brokerCredentials?: DeploymentBrokerCredential[];piCaptureRequests: boolean;piSystemCacheSplit: boolean;adminGrants?: string;@@ -1426,6 +1431,7 @@ export function loadConfig(env: NodeJS.ProcessEnv = process.env): Config {if (env.RUN_STORE === "memory" || env.RUN_STORE === "postgres") runStore = env.RUN_STORE;const providerBaseUrls = providerBaseUrlsFromEnv(env);const modelGateway = modelGatewayFromEnv(env);+ const brokerCredentials = deploymentBrokerCredentialsFromEnv(env);const claudeProcessEnv = Object.fromEntries(["PATH",@@ -1523,6 +1529,7 @@ export function loadConfig(env: NodeJS.ProcessEnv = process.env): Config {...(modelProvider ? { modelProvider } : {}),providerBaseUrls,...(modelGateway ? { modelGateway } : {}),+ ...(brokerCredentials.length ? { brokerCredentials } : {}),...(env.TRUSTED_OIDC_ADMIN_ISSUER ? { trustedOidcAdminIssuer: env.TRUSTED_OIDC_ADMIN_ISSUER } : {}),...(env.ADMIN_GRANTS ? { adminGrants: env.ADMIN_GRANTS } : {}),...(env.AUTH_ALLOWED_EMAILSdiff --git a/src/credentials/deployment-credentials.ts b/src/credentials/deployment-credentials.tsnew file mode 100644index 000000000..606335078--- /dev/null+++ b/src/credentials/deployment-credentials.ts@@ -0,0 +1,173 @@+import {+ credentialInjectionError,+ isComposioHost,+ isValidCredentialSlug,+ type CredentialInjection,+ type DecryptedServiceCredential,+ type PublicServiceCredential,+ type ServiceCredentialStore,+} from "./keychain.ts";+import type { ScopeId } from "../types.ts";++export interface DeploymentBrokerCredential {+ slug: string;+ name: string;+ secret: string;+ host: string;+ injection?: CredentialInjection;+ allowedMethods?: string[];+ allowedPathPrefixes?: string[];+ maxRequestsPerDay?: number;+}++const FIELDS = new Set([+ "slug",+ "displayName",+ "secretEnv",+ "host",+ "injection",+ "allowedMethods",+ "allowedPathPrefixes",+ "maxRequestsPerDay",+]);+const METHODS = new Set(["GET", "HEAD", "POST", "PUT", "PATCH", "DELETE"]);++function stringList(value: unknown, label: string, valid: (item: string) => boolean): string[] | undefined {+ if (value === undefined) return undefined;+ if (!Array.isArray(value) || value.some((item) => typeof item !== "string" || !valid(item)))+ throw new Error(`${label} is invalid`);+ return value as string[];+}++function parseEntry(raw: unknown, index: number, env: NodeJS.ProcessEnv): DeploymentBrokerCredential {+ if (!raw || typeof raw !== "object" || Array.isArray(raw))+ throw new Error(`BROKER_CREDENTIALS[${index}] must be an object`);+ const entry = raw as Record<string, unknown>;+ const unknownField = Object.keys(entry).find((key) => !FIELDS.has(key));+ if (unknownField) throw new Error(`BROKER_CREDENTIALS[${index}] has unknown field "${unknownField}"`);+ const slug = entry.slug;+ if (typeof slug !== "string" || !isValidCredentialSlug(slug))+ throw new Error(`BROKER_CREDENTIALS[${index}].slug must be lowercase kebab-case (a-z, 0-9, -)`);+ const at = `BROKER_CREDENTIALS "${slug}"`;+ const host = entry.host;+ if (typeof host !== "string" || !/^[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)+$/.test(host))+ throw new Error(`${at}: host must be a bare lowercase hostname`);+ if (isComposioHost(host)) throw new Error(`${at}: Composio hosts cannot be brokered`);+ const secretEnv = entry.secretEnv;+ if (typeof secretEnv !== "string" || !/^[A-Z][A-Z0-9_]*$/.test(secretEnv) || secretEnv === "BROKER_CREDENTIALS")+ throw new Error(`${at}: secretEnv must name an UPPER_SNAKE_CASE env var`);+ const secret = env[secretEnv]?.trim();+ if (!secret) throw new Error(`${at}: env var ${secretEnv} is unset or empty`);+ const injectionError = credentialInjectionError(entry.injection);+ if (injectionError) throw new Error(`${at}: ${injectionError}`);+ const allowedMethods = stringList(entry.allowedMethods, `${at}: allowedMethods`, (m) => METHODS.has(m));+ const allowedPathPrefixes = stringList(+ entry.allowedPathPrefixes,+ `${at}: allowedPathPrefixes`,+ (p) => p.startsWith("/") && !/[\r\n]/.test(p),+ );+ const cap = entry.maxRequestsPerDay;+ if (cap !== undefined && !(typeof cap === "number" && Number.isSafeInteger(cap) && cap >= 1))+ throw new Error(`${at}: maxRequestsPerDay must be a positive whole number`);+ const name = entry.displayName ?? slug;+ if (typeof name !== "string" || !name.trim() || name.length > 100)+ throw new Error(`${at}: displayName must be 1-100 characters`);+ return {+ slug,+ name: name.trim(),+ secret,+ host,+ ...(entry.injection ? { injection: entry.injection as CredentialInjection } : {}),+ ...(allowedMethods?.length ? { allowedMethods } : {}),+ ...(allowedPathPrefixes?.length ? { allowedPathPrefixes } : {}),+ ...(cap !== undefined ? { maxRequestsPerDay: cap as number } : {}),+ };+}++export function deploymentBrokerCredentialsFromEnv(env: NodeJS.ProcessEnv): DeploymentBrokerCredential[] {+ const raw = env.BROKER_CREDENTIALS?.trim();+ if (!raw) return [];+ let parsed: unknown;+ try {+ parsed = JSON.parse(raw);+ } catch {+ throw new Error("BROKER_CREDENTIALS must be a JSON array");+ }+ if (!Array.isArray(parsed)) throw new Error("BROKER_CREDENTIALS must be a JSON array");+ const credentials = parsed.map((entry, index) => parseEntry(entry, index, env));+ const seen = new Set<string>();+ for (const { slug } of credentials) {+ if (seen.has(slug)) throw new Error(`BROKER_CREDENTIALS lists slug "${slug}" more than once`);+ seen.add(slug);+ }+ return credentials;+}++function decrypted(c: DeploymentBrokerCredential): DecryptedServiceCredential {+ return {+ slug: c.slug,+ name: c.name,+ secret: c.secret,+ delivery: "broker",+ host: c.host,+ ...(c.injection ? { injection: c.injection } : {}),+ ...(c.allowedMethods ? { allowedMethods: c.allowedMethods } : {}),+ ...(c.allowedPathPrefixes ? { allowedPathPrefixes: c.allowedPathPrefixes } : {}),+ ...(c.maxRequestsPerDay !== undefined ? { maxRequestsPerDay: c.maxRequestsPerDay } : {}),+ deployments: false,+ enabled: true,+ };+}++function projected(c: DeploymentBrokerCredential): PublicServiceCredential {+ const { secret: _secret, ...rest } = decrypted(c);+ return { ...rest, hasSecret: true, source: "deployment", updatedAt: 0 };+}++export function reservedSlugError(slug: string): Error {+ return new Error(`credential "${slug}" is supplied by this deployment and cannot be changed here`);+}++export function withDeploymentCredentials(+ store: ServiceCredentialStore,+ orgScopeId: ScopeId,+ credentials: readonly DeploymentBrokerCredential[],+): ServiceCredentialStore {+ if (!credentials.length) return store;+ const bySlug = new Map(credentials.map((c) => [c.slug, c]));+ const reserved = (scope: ScopeId, slug: string) => scope === orgScopeId && bySlug.has(slug);+ const guard = (scope: ScopeId, slug: string) => {+ if (reserved(scope, slug)) throw reservedSlugError(slug);+ };+ return {+ async getServiceCredentialSecret(scope, slug) {+ const own = scope === orgScopeId ? bySlug.get(slug) : undefined;+ return own ? decrypted(own) : store.getServiceCredentialSecret(scope, slug);+ },+ async listServiceCredentials(scope) {+ const stored = await store.listServiceCredentials(scope);+ if (scope !== orgScopeId) return stored;+ return [...credentials.map(projected), ...stored.filter((c) => !bySlug.has(c.slug))];+ },+ async setServiceCredential(scope, input) {+ guard(scope, input.slug);+ return store.setServiceCredential(scope, input);+ },+ async setServiceCredentialIfAbsent(scope, input) {+ guard(scope, input.slug);+ return store.setServiceCredentialIfAbsent(scope, input);+ },+ async setServiceCredentialIfCurrent(scope, input, expectedUpdatedAt) {+ guard(scope, input.slug);+ return store.setServiceCredentialIfCurrent(scope, input, expectedUpdatedAt);+ },+ async deleteServiceCredential(scope, slug) {+ guard(scope, slug);+ return store.deleteServiceCredential(scope, slug);+ },+ async deleteServiceCredentialIfCurrent(scope, slug, expectedUpdatedAt) {+ guard(scope, slug);+ return store.deleteServiceCredentialIfCurrent(scope, slug, expectedUpdatedAt);+ },+ };+}