From 701e472f5a5ea7e6fffc595a481bfca6b52118c7 Mon Sep 17 00:00:00 2001 From: Khaliq Date: Tue, 7 Apr 2026 10:03:48 +0200 Subject: [PATCH] fix updated broker key needed --- .../dashboard-server/src/lib/send-strategy.ts | 15 ++++++++++----- .../dashboard-server/src/lib/spawned-agents.ts | 9 +++++++-- packages/dashboard-server/src/lib/types.ts | 1 + packages/dashboard-server/src/proxy-server.ts | 4 ++++ .../dashboard-server/src/routes/broker-proxy.ts | 8 ++++++++ 5 files changed, 30 insertions(+), 7 deletions(-) diff --git a/packages/dashboard-server/src/lib/send-strategy.ts b/packages/dashboard-server/src/lib/send-strategy.ts index 642d94e..831f3d0 100644 --- a/packages/dashboard-server/src/lib/send-strategy.ts +++ b/packages/dashboard-server/src/lib/send-strategy.ts @@ -61,15 +61,19 @@ export interface SendStrategy { * Used when running in proxy mode with a broker URL configured. */ export class BrokerSendStrategy implements SendStrategy { - constructor(private brokerUrl: string) {} + constructor(private brokerUrl: string, private brokerApiKey?: string) {} async send(request: SendRequest): Promise { try { + const headers: Record = { + 'content-type': 'application/json', + }; + if (this.brokerApiKey) { + headers['x-api-key'] = this.brokerApiKey; + } const upstream = await fetch(`${this.brokerUrl}/api/send`, { method: 'POST', - headers: { - 'content-type': 'application/json', - }, + headers, body: JSON.stringify({ to: request.to, message: request.message, @@ -159,6 +163,7 @@ export class DirectSendStrategy implements SendStrategy { export interface CreateSendStrategyOptions { brokerProxyEnabled: boolean; brokerUrl?: string; + brokerApiKey?: string; relaycastConfig?: RelaycastConfig | null; dataDir: string; } @@ -171,7 +176,7 @@ export interface CreateSendStrategyOptions { */ export function createSendStrategy(opts: CreateSendStrategyOptions): SendStrategy | null { if (opts.brokerProxyEnabled && opts.brokerUrl) { - return new BrokerSendStrategy(opts.brokerUrl); + return new BrokerSendStrategy(opts.brokerUrl, opts.brokerApiKey); } if (opts.relaycastConfig) { diff --git a/packages/dashboard-server/src/lib/spawned-agents.ts b/packages/dashboard-server/src/lib/spawned-agents.ts index c7bd63c..5576439 100644 --- a/packages/dashboard-server/src/lib/spawned-agents.ts +++ b/packages/dashboard-server/src/lib/spawned-agents.ts @@ -275,11 +275,12 @@ export function createSpawnedAgentsCaches(opts: { relayUrl: string | undefined; dataDir: string; verbose: boolean; + brokerApiKey?: string; }): { getSpawnedAgents: () => Promise<{ names: Set | null; agents: SpawnedAgentSummary[] | null }>; getLocalAgentNames: () => Set | null; } { - const { brokerProxyEnabled, relayUrl, dataDir, verbose } = opts; + const { brokerProxyEnabled, relayUrl, dataDir, verbose, brokerApiKey } = opts; let spawnedAgentsCache: { expiresAt: number; names: Set | null; agents: SpawnedAgentSummary[] | null } = { expiresAt: 0, @@ -305,9 +306,13 @@ export function createSpawnedAgentsCaches(opts: { } try { + const headers: Record = { Accept: 'application/json' }; + if (brokerApiKey) { + headers['x-api-key'] = brokerApiKey; + } const response = await fetch(`${relayUrl}/api/spawned`, { method: 'GET', - headers: { Accept: 'application/json' }, + headers, }); if (!response.ok) { diff --git a/packages/dashboard-server/src/lib/types.ts b/packages/dashboard-server/src/lib/types.ts index b13d0ed..8c2bf03 100644 --- a/packages/dashboard-server/src/lib/types.ts +++ b/packages/dashboard-server/src/lib/types.ts @@ -112,6 +112,7 @@ export interface RouteContext { verbose: boolean; relayUrl: string | undefined; brokerProxyEnabled: boolean; + brokerApiKey: string | undefined; resolveRelaycastConfig: () => RelaycastConfig | null; setRelayApiKey: (apiKey: string) => void; setRelayAgentIdentity: (token: string, name: string) => void; diff --git a/packages/dashboard-server/src/proxy-server.ts b/packages/dashboard-server/src/proxy-server.ts index a7feca4..36ad5dc 100644 --- a/packages/dashboard-server/src/proxy-server.ts +++ b/packages/dashboard-server/src/proxy-server.ts @@ -237,11 +237,13 @@ export function createServer(options: DashboardServerOptions = {}): DashboardSer inMemoryAgentName = undefined; clearRegistrationCache(); }; + const brokerApiKey = process.env.RELAY_BROKER_API_KEY?.trim() || undefined; const { getSpawnedAgents, getLocalAgentNames } = createSpawnedAgentsCaches({ brokerProxyEnabled, relayUrl, dataDir, verbose, + brokerApiKey, }); const getRelaycastSnapshot = async (): Promise => { @@ -333,6 +335,7 @@ export function createServer(options: DashboardServerOptions = {}): DashboardSer const strategy: SendStrategy | null = createSendStrategy({ brokerProxyEnabled, brokerUrl: relayUrl, + brokerApiKey, relaycastConfig: config, dataDir, }); @@ -394,6 +397,7 @@ export function createServer(options: DashboardServerOptions = {}): DashboardSer verbose, relayUrl, brokerProxyEnabled, + brokerApiKey, resolveRelaycastConfig, setRelayApiKey, setRelayAgentIdentity, diff --git a/packages/dashboard-server/src/routes/broker-proxy.ts b/packages/dashboard-server/src/routes/broker-proxy.ts index 43a254c..788a5ea 100644 --- a/packages/dashboard-server/src/routes/broker-proxy.ts +++ b/packages/dashboard-server/src/routes/broker-proxy.ts @@ -26,6 +26,9 @@ export function registerBrokerProxyRoutes(app: Express, ctx: RouteContext): void const headers: Record = { 'content-type': 'application/json', }; + if (ctx.brokerApiKey) { + headers['x-api-key'] = ctx.brokerApiKey; + } const workspaceId = req.header('x-workspace-id'); if (workspaceId) { headers['x-workspace-id'] = workspaceId; @@ -74,6 +77,11 @@ export function registerBrokerProxyRoutes(app: Express, ctx: RouteContext): void ws: false, logger: ctx.verbose ? console : undefined, on: { + proxyReq: (proxyReq) => { + if (ctx.brokerApiKey) { + proxyReq.setHeader('x-api-key', ctx.brokerApiKey); + } + }, error: (err, _req, res) => { console.error('[dashboard] Broker proxy error:', (err as Error).message); if (res && 'writeHead' in res && typeof res.writeHead === 'function') {