Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(oauth): delete orphan oauth-client Secret on uninstall (H-2)
Diego Zacarias55f67c3ab6d5
diff --git a/control-api/package-lock.json b/control-api/package-lock.jsonindex 343c05cc9..f7a852a71 100644--- a/control-api/package-lock.json+++ b/control-api/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-control-api",- "version": "0.1.596",+ "version": "0.1.597","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-control-api",- "version": "0.1.596",+ "version": "0.1.597","dependencies": {"@clerum/display-field": "file:../packages/display-field","@clerum/grok-provider-attempt-contract": "file:../packages/grok-provider-attempt-contract",diff --git a/control-api/package.json b/control-api/package.jsonindex 15acddefd..82574c3bb 100644--- a/control-api/package.json+++ b/control-api/package.json@@ -1,6 +1,6 @@{"name": "clerum-control-api",- "version": "0.1.596",+ "version": "0.1.597","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/__tests__/mcpServerUninstallDeletesOauthClientSecret.test.ts b/control-api/src/routes/admin/__tests__/mcpServerUninstallDeletesOauthClientSecret.test.tsnew file mode 100644index 000000000..8c6a9271c--- /dev/null+++ b/control-api/src/routes/admin/__tests__/mcpServerUninstallDeletesOauthClientSecret.test.ts@@ -0,0 +1,81 @@+import { beforeEach, describe, expect, it, vi } from 'vitest'+import express from 'express'+import request from 'supertest'++/**+ * H-2 route regression (T3/T4): the generic McpServer uninstall+ * (DELETE /admin/mcp-servers/:name) must best-effort delete the pre-registered+ * confidential remote install's `${name}-oauth-client` Secret as an observable+ * uninstall side-effect. That Secret is created with no ownerReferences (install+ * saga step 1, before the CR exists), so K8s GC never reclaims it; before H-2+ * the uninstall deleted `${name}-credentials` but left `${name}-oauth-client`+ * orphaned — breaking install→uninstall→reinstall (createSecret 409) and+ * leaking the client_secret.+ *+ * Harness mirrors mcpServerUninstallPurgesGrants.test.ts (H-3): mount+ * `createAdminResourcesRouter` with a fake K8sGateway + supertest, issue an+ * mcpservers uninstall DELETE, and assert the OBSERVABLE — the fake gateway+ * records `deleteSecret` calls, and we assert one is for `${name}-oauth-client`+ * in the server namespace (T4).+ *+ * The DB `pool` is swapped for a recording fake (like H-3) so the router's+ * DCR/grants teardown runs against benign empty results and the 200 path+ * completes without a real Postgres.+ *+ * T3: this test FAILS against parent 2680eade5 — there the uninstall deletes+ * only `${name}-credentials`, so `deleteSecret` is never called with+ * `${name}-oauth-client` and the assertion fails.+ */++const dbMock = vi.hoisted(() => {+ const query = vi.fn(async () => ({ rows: [], rowCount: 0 }))+ return { query }+})++vi.mock('../../../db.js', async importActual => {+ const actual = await importActual<typeof import('../../../db.js')>()+ return { ...actual, pool: { query: dbMock.query } }+})++// Import AFTER vi.mock so the swapped `pool` wires into the router.+const { createAdminResourcesRouter } = await import('../resources.js')+const { config } = await import('../../../config.js')++describe('DELETE /admin/mcp-servers/:name — deletes the oauth-client Secret on uninstall (H-2)', () => {+ const SERVER_NAME = 'notion'++ function buildApp() {+ const deleteSecretCalls: Array<{ name: string; namespace: string }> = []+ const gateway = {+ deleteResource: vi.fn(async () => ({ metadata: { name: SERVER_NAME } })),+ deleteSecret: vi.fn(async (name: string, namespace: string) => {+ deleteSecretCalls.push({ name, namespace })+ return {}+ }),+ getSecret: vi.fn(async () => ({})),+ listResource: vi.fn(async () => []),+ updateResource: vi.fn(async () => ({})),+ }+ const app = express()+ app.use(express.json({ limit: '1mb' }))+ app.use('/api/v1', createAdminResourcesRouter(gateway as never))+ return { app, gateway, deleteSecretCalls }+ }++ beforeEach(() => {+ dbMock.query.mockClear()+ })++ it('best-effort deletes `${name}-oauth-client` in the server namespace during uninstall', async () => {+ const { app, deleteSecretCalls } = buildApp()++ const res = await request(app).delete(`/api/v1/admin/mcp-servers/${SERVER_NAME}`)+ expect(res.status).toBe(200)++ // Before H-2 the uninstall only deletes `${name}-credentials`; this is the+ // regression anchor.+ const oauthClientDelete = deleteSecretCalls.find(c => c.name === `${SERVER_NAME}-oauth-client`)+ expect(oauthClientDelete).toBeDefined()+ expect(oauthClientDelete!.namespace).toBe(config.mcpServersNamespace)+ })+})diff --git a/control-api/src/routes/admin/resources.ts b/control-api/src/routes/admin/resources.tsindex 2ad41b182..512ed8988 100644--- a/control-api/src/routes/admin/resources.ts+++ b/control-api/src/routes/admin/resources.ts@@ -1073,6 +1073,25 @@ export function createAdminResourcesRouter(gateway: K8sGateway): Router {)}+ // Pre-registered confidential remote installs create a+ // `${name}-oauth-client` Secret with no ownerReferences (created in the+ // install saga before the CR exists, so K8s GC can't own it). Best-effort+ // delete here so uninstall→reinstall doesn't 409 on the residual Secret+ // and the client_secret doesn't linger. A 404 is expected for every+ // non-pre-registered server; never fail the uninstall.+ try {+ await gateway.deleteSecret(`${name}-oauth-client`, ns)+ log.info(+ { event: 'mcpserver_oauth_client_secret_deleted', name, namespace: ns },+ 'deleted mcp-server oauth client secret'+ )+ } catch (err) {+ log.info(+ { event: 'mcpserver_oauth_client_secret_absent', name, namespace: ns, err },+ 'no mcp-server oauth client secret to delete'+ )+ }+// Remote DCR client teardown (K, DEC-18): revoke the encrypted// `dynamic_clients` row (reliable) + best-effort RFC 7592 delete at the AS// (courtesy). Keyed per-server-CR; idempotent (0 rows ⇒ not a DCR server).