Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
refactor(control-api): extract redeemClaim as the single registry claim path
Jose Ramirezbe0a02e7ae41
diff --git a/control-api/package-lock.json b/control-api/package-lock.jsonindex 4a0d29c1f..74f2c8fd8 100644--- a/control-api/package-lock.json+++ b/control-api/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-control-api",- "version": "0.1.369",+ "version": "0.1.370","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-control-api",- "version": "0.1.369",+ "version": "0.1.370","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 e709a9ce8..eb8e810d5 100644--- a/control-api/package.json+++ b/control-api/package.json@@ -1,6 +1,6 @@{"name": "clerum-control-api",- "version": "0.1.369",+ "version": "0.1.370","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 a363e026f..b18fcd44d 100644--- a/control-api/src/routes/admin/registryConnect.ts+++ b/control-api/src/routes/admin/registryConnect.ts@@ -26,10 +26,100 @@ import { signPop } from '../../services/registryPopSigner.js'* status endpoint so operator approval is visible before the user claims —* matching the register → poll status → claim flow.*/+/** Bound every registry hop (registryClient.ts convention). */+const REGISTRY_FETCH_TIMEOUT_MS = 10_000+export function createRegistryConnectRouter(): Router {const router = Router()const base = (): string => `${config.registryUrl}/api/v1/deployments`+ type ClaimOutcome =+ | { kind: 'connected'; org: string }+ | { kind: 'expired' }+ | { kind: 'rejected' }+ | { kind: 'already_claimed' }+ | { kind: 'client_unavailable' }+ | { kind: 'superseded' }+ | { kind: 'unreachable'; err: unknown }+ | { kind: 'error'; status: number }++ // Single claim implementation shared by the manual paste route and the+ // auto-claim path. EXCEPTION-TOTAL: markConnected encrypts and writes to+ // Postgres AFTER the registry has already burned the one-time secret, so a+ // throw here must be a value the caller can act on, never an unhandled 500.+ async function redeemClaim(input: {+ deploymentId: string+ keyId: string+ privateKeyPem: string+ adminId: string+ claimToken: string+ }): Promise<ClaimOutcome> {+ try {+ const pop = signPop({+ privateKeyPem: input.privateKeyPem,+ sub: input.adminId,+ kid: input.keyId,+ })+ const claimRes = await fetch(`${base()}/claim`, {+ method: 'POST',+ headers: { 'Content-Type': 'application/json', DPoP: pop },+ body: JSON.stringify({ claim_token: input.claimToken }),+ signal: AbortSignal.timeout(REGISTRY_FETCH_TIMEOUT_MS),+ })+ if (claimRes.status === 410) return { kind: 'expired' }+ // The registry returns 401 for both invalid_pop and invalid_claim_token;+ // both mean the same to the operator — the claim was rejected.+ if (claimRes.status === 401) return { kind: 'rejected' }+ if (claimRes.status === 409) {+ const err = await readErrorCode(claimRes)+ return err === 'client_unavailable'+ ? { kind: 'client_unavailable' }+ : { kind: 'already_claimed' }+ }+ if (!claimRes.ok) {+ rootLogger.error(+ { event: 'registry_connect_claim_failed', status: claimRes.status },+ 'claim failed'+ )+ return { kind: 'error', status: claimRes.status }+ }+ const claimed = (await claimRes.json()) as {+ client_id?: unknown+ client_secret?: unknown+ org?: unknown+ }+ // A hostile or mis-proxied registry must produce a clean outcome, not a+ // Buffer.from(null) crash inside the encryption path.+ if (+ typeof claimed.client_id !== 'string' ||+ typeof claimed.client_secret !== 'string' ||+ typeof claimed.org !== 'string'+ ) {+ rootLogger.error(+ { event: 'registry_connect_claim_malformed', status: claimRes.status },+ 'claim response missing required fields'+ )+ return { kind: 'error', status: claimRes.status }+ }+ const wrote = await markConnected({+ deploymentId: input.deploymentId,+ clientId: claimed.client_id,+ clientSecret: claimed.client_secret,+ orgName: claimed.org,+ })+ if (!wrote) {+ rootLogger.error(+ { event: 'registry_connect_claim_superseded', status: claimRes.status },+ 'claim succeeded but the connection row moved; credentials are lost'+ )+ return { kind: 'superseded' }+ }+ return { kind: 'connected', org: claimed.org }+ } catch (err) {+ return { kind: 'unreachable', err }+ }+ }+async function requireSelfHostedAdmin(req: Request,res: Response@@ -218,51 +308,41 @@ export function createRegistryConnectRouter(): Router {res.status(400).json({ error: 'invalid_request' })return}- const pop = signPop({ privateKeyPem: row.privateKeyPem, sub: ctx.adminId, kid: row.keyId })- const claimRes = await fetch(`${base()}/claim`, {- method: 'POST',- headers: { 'Content-Type': 'application/json', DPoP: pop },- body: JSON.stringify({ claim_token: claimToken }),- })- if (claimRes.status === 410) {- res.status(410).json({ error: 'claim_expired' })- return- }- if (claimRes.status === 401) {- // The registry returns 401 for both invalid_pop and invalid_claim_token;- // both mean the same to the operator — the claim was rejected.- res.status(401).json({ error: 'claim_rejected' })- return- }- // C-M6: a claim 409 (already_claimed / client_unavailable) is a distinct,- // actionable state — surface it rather than the opaque 502.- if (claimRes.status === 409) {- const err = await readErrorCode(claimRes)- res- .status(409)- .json({ error: err === 'client_unavailable' ? 'client_unavailable' : 'already_claimed' })- return- }- if (!claimRes.ok) {- rootLogger.error(- { event: 'registry_connect_claim_failed', status: claimRes.status },- 'claim failed'- )- res.status(502).json({ error: 'registry_integration_error' })- return- }- const claimed = (await claimRes.json()) as {- client_id: string- client_secret: string- org: string- }- await markConnected({+ const outcome = await redeemClaim({deploymentId: row.deploymentId,- clientId: claimed.client_id,- clientSecret: claimed.client_secret,- orgName: claimed.org,+ keyId: row.keyId,+ privateKeyPem: row.privateKeyPem,+ adminId: ctx.adminId,+ claimToken,})- res.status(200).json({ state: 'connected', org: claimed.org })+ switch (outcome.kind) {+ case 'connected':+ res.status(200).json({ state: 'connected', org: outcome.org })+ return+ case 'expired':+ res.status(410).json({ error: 'claim_expired' })+ return+ case 'rejected':+ res.status(401).json({ error: 'claim_rejected' })+ return+ case 'already_claimed':+ res.status(409).json({ error: 'already_claimed' })+ return+ case 'client_unavailable':+ res.status(409).json({ error: 'client_unavailable' })+ return+ case 'superseded':+ res.status(409).json({ error: 'connection_superseded' })+ return+ // Preserves today's behaviour exactly: a registry-unreachable failure+ // threw out of this handler and became a 500. Collapsing it to 502 here+ // would be a silent contract change for the manual route.+ case 'unreachable':+ throw outcome.err+ case 'error':+ res.status(502).json({ error: 'registry_integration_error' })+ return+ }} catch (err) {next(err)}diff --git a/control-api/test/routes.registryConnect.test.ts b/control-api/test/routes.registryConnect.test.tsindex d3d25012c..bd38bca45 100644--- a/control-api/test/routes.registryConnect.test.ts+++ b/control-api/test/routes.registryConnect.test.ts@@ -61,6 +61,7 @@ beforeEach(() => {username: 'alice',status: 'active',})+ connDb.markConnected.mockResolvedValue(true)cfg.registryConnectionMode = 'self-hosted'cfg.registryAuthEnabled = true})@@ -233,6 +234,7 @@ describe('registry connect flow', () => {expect(String(url)).toContain('/api/v1/deployments/claim')expect((init as RequestInit).headers).toHaveProperty('DPoP')expect(connDb.markConnected).toHaveBeenCalledWith({+ deploymentId: 'dep-9',clientId: 'cid',clientSecret: 'csecret',orgName: 'acme',@@ -317,6 +319,44 @@ describe('registry connect flow', () => {expect(res.body.error).toBe('not_pending')})+ it('claim → registry 500 → 502 registry_integration_error', async () => {+ connDb.getRegistryConnection.mockResolvedValue({+ status: 'pending',+ deploymentId: 'dep-1',+ keyId: 'kid-1',+ privateKeyPem: pkForTest(),+ requestedOrgName: 'acme',+ })+ vi.spyOn(globalThis, 'fetch').mockResolvedValue(new Response('{}', { status: 500 }))+ const res = await request(app())+ .post('/admin/registry/connect/claim')+ .send({ claim_token: 'tok-abc' })+ .expect(502)+ expect(res.body.error).toBe('registry_integration_error')+ expect(connDb.markConnected).not.toHaveBeenCalled()+ })++ it('claim → markConnected matches no row → 409 connection_superseded', async () => {+ connDb.getRegistryConnection.mockResolvedValue({+ status: 'pending',+ deploymentId: 'dep-1',+ keyId: 'kid-1',+ privateKeyPem: pkForTest(),+ requestedOrgName: 'acme',+ })+ connDb.markConnected.mockResolvedValue(false)+ vi.spyOn(globalThis, 'fetch').mockResolvedValue(+ new Response(JSON.stringify({ client_id: 'cid', client_secret: 'csecret', org: 'acme' }), {+ status: 200,+ })+ )+ const res = await request(app())+ .post('/admin/registry/connect/claim')+ .send({ claim_token: 'tok-abc' })+ .expect(409)+ expect(res.body.error).toBe('connection_superseded')+ })+it('DELETE → 204 drops the row', async () => {connDb.getRegistryConnection.mockResolvedValue({ status: 'pending', deploymentId: 'dep-9' })await request(app()).delete('/admin/registry/connect').expect(204)