Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
feat(control-api): auto-claim on registry 201 and map the new register rejections
Jose Ramirezaf0f9e88496c
diff --git a/control-api/package-lock.json b/control-api/package-lock.jsonindex 74f2c8fd8..260500750 100644--- a/control-api/package-lock.json+++ b/control-api/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-control-api",- "version": "0.1.370",+ "version": "0.1.371","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-control-api",- "version": "0.1.370",+ "version": "0.1.371","dependencies": {"@clerum/image-policy": "file:../packages/image-policy","@clerum/llm-providers": "file:../packages/llm-providers",diff --git a/control-api/package.json b/control-api/package.jsonindex eb8e810d5..e7215af75 100644--- a/control-api/package.json+++ b/control-api/package.json@@ -1,6 +1,6 @@{"name": "clerum-control-api",- "version": "0.1.370",+ "version": "0.1.371","description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets","main": "dist/main.js","engines": {diff --git a/control-api/src/routes/admin/registryConnect.ts b/control-api/src/routes/admin/registryConnect.tsindex b18fcd44d..cc9222f0e 100644--- a/control-api/src/routes/admin/registryConnect.ts+++ b/control-api/src/routes/admin/registryConnect.ts@@ -228,6 +228,14 @@ export function createRegistryConnectRouter(): Router {res.status(409).json({ error: 'already_connected' })return}+ // An 'approved' row is a registry-side auto-approval whose claim has not+ // landed. Re-registering would DELETE it (dropping the keypair), leaving+ // the approved deployment unrecoverable — rotate is PoP-gated — while it+ // permanently holds its org name. Recovery is the only way forward.+ if (existing && existing.status === 'approved') {+ res.status(409).json({ error: 'recovery_in_progress' })+ return+ }const body = (req.body ?? {}) as {requested_org_name?: unknowncontact_email?: unknown@@ -253,17 +261,48 @@ export function createRegistryConnectRouter(): Router {requested_org_name: requestedOrgName,public_key_pem: publicKey,contact_email: contactEmail,- deployment_info: {},+ // Capability declaration: the registry auto-approves ONLY for clients+ // that can consume a 201 + claim_token. Without it, flipping the+ // registry's open-registration flag would strand every control-api+ // running code older than this change.+ deployment_info: { auto_claim: true, client: 'control-api' },pop,}),+ signal: AbortSignal.timeout(REGISTRY_FETCH_TIMEOUT_MS),})- // C-M6: surface the registry's 400 rejection code (org_blocklisted /- // invalid_request) instead of collapsing it into an opaque 502.+ // Every mapping is an ALLOWLIST with a stated default. readErrorCode+ // returns null on a non-JSON body (routine for an ingress-generated 429),+ // and an unbounded pass-through would reflect a registry-controlled+ // string into our response and the panel's code comparisons.if (regRes.status === 400) {const err = await readErrorCode(regRes)+ const known = [+ 'org_blocklisted',+ 'invalid_contact_email',+ 'invalid_deployment_info',+ 'deployment_info_too_large',+ ]+ res.status(400).json({ error: known.includes(err ?? '') ? err : 'invalid_request' })+ return+ }+ if (regRes.status === 409) {+ const err = await readErrorCode(regRes)+ if (err === 'org_name_taken' || err === 'jti_replayed') {+ res.status(409).json({ error: err })+ return+ }+ res.status(502).json({ error: 'registry_integration_error' })+ return+ }+ if (regRes.status === 429) {+ const err = await readErrorCode(regRes)+ const retryAfter = regRes.headers.get('retry-after')+ if (retryAfter) res.set('Retry-After', retryAfter)res- .status(400)- .json({ error: err === 'org_blocklisted' ? 'org_blocklisted' : 'invalid_request' })+ .status(429)+ .json({+ error: err === 'registration_capacity' ? 'registration_capacity' : 'rate_limited',+ })return}if (!regRes.ok) {@@ -274,7 +313,23 @@ export function createRegistryConnectRouter(): Router {res.status(502).json({ error: 'registry_integration_error' })return}- const reg = (await regRes.json()) as { deployment_id: string; key_id: string }+ const reg = (await regRes.json()) as {+ deployment_id: string+ key_id: string+ claim_token?: unknown+ }+ const autoApproved = regRes.status === 201 && typeof reg.claim_token === 'string'+ if (regRes.status !== 201 && regRes.status !== 202) {+ rootLogger.error(+ { event: 'registry_connect_register_unexpected', status: regRes.status },+ 'unexpected 2xx from register'+ )+ res.status(502).json({ error: 'registry_integration_error' })+ return+ }+ // Persist BEFORE claiming. The keypair is the only artifact that can+ // recover this deployment (rotate is PoP-gated); claiming first and dying+ // before the write would burn the one-time token AND lose the key.await upsertPendingConnection({deploymentId: reg.deployment_id,keyId: reg.key_id,@@ -283,10 +338,37 @@ export function createRegistryConnectRouter(): Router {requestedOrgName,contactEmail,registryUrl: config.registryUrl,+ status: autoApproved ? 'approved' : 'pending',})+ if (!autoApproved) {+ res+ .status(202)+ .json({ state: 'pending', deploymentId: reg.deployment_id, requestedOrgName })+ return+ }+ const outcome = await redeemClaim({+ deploymentId: reg.deployment_id,+ keyId: reg.key_id,+ privateKeyPem: privateKey,+ adminId: ctx.adminId,+ claimToken: reg.claim_token as string,+ })+ if (outcome.kind === 'connected') {+ res.status(200).json({+ state: 'connected',+ org: outcome.org,+ deploymentId: reg.deployment_id,+ authEnabled: config.registryAuthEnabled,+ })+ return+ }+ rootLogger.error(+ { event: 'registry_connect_auto_claim_failed', status: 0, outcome: outcome.kind },+ 'auto-claim failed; recovery is available'+ )res.status(202)- .json({ state: 'pending', deploymentId: reg.deployment_id, requestedOrgName })+ .json({ state: 'connecting', deploymentId: reg.deployment_id, requestedOrgName })} catch (err) {next(err)}diff --git a/control-api/test/routes.registryConnect.test.ts b/control-api/test/routes.registryConnect.test.tsindex bd38bca45..07e1bb4ec 100644--- a/control-api/test/routes.registryConnect.test.ts+++ b/control-api/test/routes.registryConnect.test.ts@@ -54,6 +54,24 @@ function pkForTest(): string {}).privateKey}+/**+ * Route fetches by URL, constructing a FRESH Response per call. The existing+ * mockResolvedValue idiom cannot be reused here: it returns one Response+ * instance, and reading its body twice throws 'Body is unusable'.+ */+function routeFetch(handlers: Record<string, () => Response>): ReturnType<typeof vi.spyOn> {+ return vi.spyOn(globalThis, 'fetch').mockImplementation(async input => {+ const url = String(input)+ for (const [fragment, make] of Object.entries(handlers)) {+ if (url.includes(fragment)) return make()+ }+ throw new Error(`unexpected fetch: ${url}`)+ }) as ReturnType<typeof vi.spyOn>+}++const json = (body: unknown, status: number, headers?: Record<string, string>) =>+ new Response(JSON.stringify(body), { status, headers })+beforeEach(() => {vi.clearAllMocks()adminSvc.findAdminById.mockResolvedValue({@@ -362,4 +380,218 @@ describe('registry connect flow', () => {await request(app()).delete('/admin/registry/connect').expect(204)expect(connDb.deleteConnection).toHaveBeenCalled()})++ // The redeemClaim extraction must NOT convert a registry-unreachable failure+ // into a 502. It escapes the handler and the Express error handler makes it a+ // 500, exactly as before the refactor. Collapsing it to 502 would be a silent+ // contract change for the manual paste route.+ it('manual claim → registry unreachable → 500, not 502', async () => {+ connDb.getRegistryConnection.mockResolvedValue({+ status: 'pending',+ deploymentId: 'dep-1',+ keyId: 'kid-1',+ privateKeyPem: pkForTest(),+ requestedOrgName: 'acme',+ })+ vi.spyOn(globalThis, 'fetch').mockRejectedValue(new Error('ECONNREFUSED'))+ await request(app())+ .post('/admin/registry/connect/claim')+ .send({ claim_token: 'tok-abc' })+ .expect(500)+ })+})++describe('register — auto-claim (201)', () => {+ it('claims immediately and reports connected', async () => {+ connDb.getRegistryConnection.mockResolvedValue(null)+ connDb.markConnected.mockResolvedValue(true)+ routeFetch({+ '/register': () =>+ json(+ {+ deployment_id: 'dep-1',+ key_id: 'kid-1',+ status: 'approved',+ claim_token: 'tok-abc',+ claim_expires_at: '2026-07-30T00:00:00Z',+ },+ 201+ ),+ '/claim': () => json({ client_id: 'cid', client_secret: 'csecret', org: 'acme' }, 200),+ })+ const res = await request(app())+ .post('/admin/registry/connect/request')+ .send({ requested_org_name: 'acme', contact_email: '[email protected]' })+ .expect(200)+ expect(res.body).toMatchObject({ state: 'connected', org: 'acme' })+ expect(res.body.authEnabled).toBe(true)+ })++ // The mocks return undefined, so `await x` and `void x` produce IDENTICAL+ // invocationCallOrder — an ordering assertion built on call order is green+ // against the exact mutation it exists to catch. Assert on an event log+ // driven by a real async implementation instead.+ it('persists the row BEFORE burning the one-time token', { retry: 0 }, async () => {+ const events: string[] = []+ connDb.getRegistryConnection.mockResolvedValue(null)+ connDb.markConnected.mockResolvedValue(true)+ connDb.upsertPendingConnection.mockImplementation(async () => {+ await new Promise(r => setImmediate(r))+ events.push('persisted')+ })+ vi.spyOn(globalThis, 'fetch').mockImplementation(async input => {+ const url = String(input)+ if (url.includes('/register')) {+ events.push('register')+ return json(+ { deployment_id: 'dep-1', key_id: 'kid-1', status: 'approved', claim_token: 'tok-abc' },+ 201+ )+ }+ events.push('claim')+ return json({ client_id: 'cid', client_secret: 'csecret', org: 'acme' }, 200)+ })+ await request(app())+ .post('/admin/registry/connect/request')+ .send({ requested_org_name: 'acme', contact_email: '[email protected]' })+ .expect(200)+ expect(events).toEqual(['register', 'persisted', 'claim'])+ connDb.upsertPendingConnection.mockReset()+ })++ it("persists status 'approved' and reports connecting when the claim fails", async () => {+ connDb.getRegistryConnection.mockResolvedValue(null)+ routeFetch({+ '/register': () =>+ json(+ { deployment_id: 'dep-1', key_id: 'kid-1', status: 'approved', claim_token: 'tok-abc' },+ 201+ ),+ '/claim': () => json({ error: 'boom' }, 500),+ })+ const res = await request(app())+ .post('/admin/registry/connect/request')+ .send({ requested_org_name: 'acme', contact_email: '[email protected]' })+ .expect(202)+ expect(res.body).toMatchObject({ state: 'connecting', deploymentId: 'dep-1' })+ expect(connDb.upsertPendingConnection).toHaveBeenCalledWith(+ expect.objectContaining({ status: 'approved' })+ )+ })++ it('reports connecting when the claim fetch throws', async () => {+ connDb.getRegistryConnection.mockResolvedValue(null)+ vi.spyOn(globalThis, 'fetch').mockImplementation(async input => {+ if (String(input).includes('/register')) {+ return json(+ { deployment_id: 'dep-1', key_id: 'kid-1', status: 'approved', claim_token: 'tok-abc' },+ 201+ )+ }+ throw new Error('ECONNREFUSED')+ })+ const res = await request(app())+ .post('/admin/registry/connect/request')+ .send({ requested_org_name: 'acme', contact_email: '[email protected]' })+ .expect(202)+ expect(res.body.state).toBe('connecting')+ })++ it('declares the auto_claim capability in deployment_info', async () => {+ connDb.getRegistryConnection.mockResolvedValue(null)+ const spy = routeFetch({+ '/register': () => json({ deployment_id: 'dep-1', key_id: 'kid-1', status: 'pending' }, 202),+ })+ await request(app())+ .post('/admin/registry/connect/request')+ .send({ requested_org_name: 'acme', contact_email: '[email protected]' })+ .expect(202)+ const body = JSON.parse(String((spy.mock.calls[0]![1] as RequestInit).body))+ expect(body.deployment_info).toMatchObject({ auto_claim: true })+ })+})++describe('register — 202 pending path is unchanged', () => {+ // Asserting only the response body lets a mutation that pastes+ // status:'approved' into the 202 branch survive: the body is identical, and+ // the never-rotate test below uses a synthetic row so it never observes the+ // real write. Assert the persisted status too.+ it("replies pending AND persists status 'pending'", { retry: 0 }, async () => {+ connDb.getRegistryConnection.mockResolvedValue(null)+ routeFetch({+ '/register': () => json({ deployment_id: 'dep-1', key_id: 'kid-1', status: 'pending' }, 202),+ })+ const res = await request(app())+ .post('/admin/registry/connect/request')+ .send({ requested_org_name: 'acme', contact_email: '[email protected]' })+ .expect(202)+ expect(res.body).toMatchObject({ state: 'pending', deploymentId: 'dep-1' })+ expect(connDb.upsertPendingConnection).toHaveBeenCalledWith(+ expect.objectContaining({ status: 'pending' })+ )+ })+})++describe('register — new registry rejections map to distinct codes', () => {+ const cases: Array<[string, number, unknown, number, string]> = [+ ['org_name_taken', 409, { error: 'org_name_taken' }, 409, 'org_name_taken'],+ ['jti_replayed', 409, { error: 'jti_replayed' }, 409, 'jti_replayed'],+ ['capacity', 429, { error: 'registration_capacity' }, 429, 'registration_capacity'],+ ['per-IP', 429, { error: 'RATE_LIMITED' }, 429, 'rate_limited'],+ ['bad email', 400, { error: 'invalid_contact_email' }, 400, 'invalid_contact_email'],+ ['blocklist', 400, { error: 'org_blocklisted' }, 400, 'org_blocklisted'],+ ['bad pop', 401, { error: 'invalid_pop' }, 502, 'registry_integration_error'],+ ['unknown 409', 409, { error: 'who_knows' }, 502, 'registry_integration_error'],+ ['unparseable 429', 429, 'not json', 429, 'rate_limited'],+ ]+ for (const [name, regStatus, regBody, want, code] of cases) {+ it(`${name} → ${want} ${code}`, async () => {+ connDb.getRegistryConnection.mockResolvedValue(null)+ routeFetch({+ '/register': () =>+ typeof regBody === 'string'+ ? new Response(regBody, { status: regStatus })+ : json(regBody, regStatus),+ })+ const res = await request(app())+ .post('/admin/registry/connect/request')+ .send({ requested_org_name: 'acme', contact_email: '[email protected]' })+ .expect(want)+ expect(res.body.error).toBe(code)+ expect(connDb.upsertPendingConnection).not.toHaveBeenCalled()+ })+ }++ it('forwards Retry-After on a per-IP rate limit', async () => {+ connDb.getRegistryConnection.mockResolvedValue(null)+ routeFetch({+ '/register': () => json({ error: 'RATE_LIMITED' }, 429, { 'Retry-After': '86400' }),+ })+ const res = await request(app())+ .post('/admin/registry/connect/request')+ .send({ requested_org_name: 'acme', contact_email: '[email protected]' })+ .expect(429)+ expect(res.headers['retry-after']).toBe('86400')+ })+})++describe('register — guarded while a recovery is outstanding', () => {+ it('rejects a re-request when the local row is approved', async () => {+ connDb.getRegistryConnection.mockResolvedValue({+ status: 'approved',+ deploymentId: 'dep-1',+ keyId: 'kid-1',+ privateKeyPem: pkForTest(),+ requestedOrgName: 'acme',+ })+ const res = await request(app())+ .post('/admin/registry/connect/request')+ .send({ requested_org_name: 'acme', contact_email: '[email protected]' })+ .expect(409)+ expect(res.body.error).toBe('recovery_in_progress')+ // upsertPendingConnection DELETEs the row and regenerates the keypair.+ // Doing that here would destroy the only artifact that can recover an+ // already-approved deployment and permanently squat its org name.+ expect(connDb.upsertPendingConnection).not.toHaveBeenCalled()+ })})