Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(control-api): fence the uninstall oauth-client Secret delete on a snapshot precondition

Diego Zacarias3b49460bedc3
diff --git a/control-api/package-lock.json b/control-api/package-lock.json
index 5ec8f7bf8..c590eda6b 100644
--- a/control-api/package-lock.json
+++ b/control-api/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-api",
- "version": "0.1.643",
+ "version": "0.1.644",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.643",
+ "version": "0.1.644",
"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.json
index 3aafb96fd..f6e9dac46 100644
--- a/control-api/package.json
+++ b/control-api/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-api",
- "version": "0.1.643",
+ "version": "0.1.644",
"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/registry.ts b/control-api/src/routes/admin/registry.ts
index be080b3bf..52ec611d3 100644
--- a/control-api/src/routes/admin/registry.ts
+++ b/control-api/src/routes/admin/registry.ts
@@ -4014,6 +4014,8 @@ export function createAdminRegistryRouter(gateway?: K8sGateway): Router {
// Uninstall MCP Server
let credentialSecretSnapshot: SecretSnapshot | null = null
const credentialSecretName = `${resourceName}-credentials`
+ let oauthClientSecretSnapshot: SecretSnapshot | null = null
+ const oauthClientSecretName = `${resourceName}-oauth-client`
// Capture the credential identity before deleting the parent CR. A
// same-name Secret replacement can be created after the CR delete;
@@ -4044,6 +4046,36 @@ export function createAdminRegistryRouter(gateway?: K8sGateway): Router {
}
}
+ // Same fence for the managed OAuth client Secret. It is created in the
+ // install saga WITHOUT ownerReferences (it predates the CR), so K8s GC
+ // never collects it and control-api must delete it here — but by-name
+ // after the CR delete is exposed to the same delete/recreate race a
+ // concurrent reinstall opens. Bind the later delete to this snapshot.
+ // Absent (404) is the norm for non-OAuth / reference-mode servers.
+ try {
+ oauthClientSecretSnapshot = normalizeSecretSnapshot(
+ await gateway.getSecret(oauthClientSecretName, namespace),
+ oauthClientSecretName,
+ namespace
+ )
+ } catch (err) {
+ if (extractK8sError(err)?.status !== 404) {
+ res.status(503).json({
+ error: 'registry_uninstall_outcome_ambiguous',
+ outcome: 'repair_required',
+ resourceName,
+ resourceType,
+ namespace,
+ deleted,
+ warnings: [
+ ...warnings,
+ `Secret/${oauthClientSecretName}: ${err instanceof Error ? err.message : 'unable to verify identity'}`,
+ ],
+ })
+ return
+ }
+ }
+
try {
const current = await readResourceForRollback(gateway, 'mcpservers', {
metadata: { name: resourceName, namespace },
@@ -4121,16 +4153,43 @@ export function createAdminRegistryRouter(gateway?: K8sGateway): Router {
// DERIVED (`${serverName}-oauth-client`), so it can only exist if this
// server was installed in OAuth managed mode; reference mode points the
// clientIdRef/clientSecretRef at an operator-named Secret and never
- // creates this name. Best-effort by-name, same as `-credentials` above:
- // a no-op when the server had no OAuth block or used reference mode.
+ // creates this name. Fenced to the pre-CR-delete snapshot, same as
+ // `-credentials` above: a no-op when the server had no OAuth block or
+ // used reference mode, and a delete bound to the exact UID/RV so a
+ // concurrent reinstall's same-name Secret is never razed.
try {
- await gateway.deleteSecret(`${resourceName}-oauth-client`, namespace)
- await waitForDeletion(
- () => gateway.getSecret(`${resourceName}-oauth-client`, namespace),
- `Secret/${resourceName}-oauth-client`
- )
- deleted.push(`Secret/${resourceName}-oauth-client`)
- } catch {
+ if (oauthClientSecretSnapshot) {
+ await gateway.deleteSecret(
+ oauthClientSecretSnapshot.name,
+ oauthClientSecretSnapshot.namespace,
+ secretPreconditions(oauthClientSecretSnapshot)
+ )
+ await waitForDeletion(
+ () =>
+ gateway.getSecret(
+ oauthClientSecretSnapshot!.name,
+ oauthClientSecretSnapshot!.namespace
+ ),
+ `Secret/${oauthClientSecretSnapshot.name}`
+ )
+ deleted.push(`Secret/${oauthClientSecretSnapshot.name}`)
+ }
+ } catch (err) {
+ if (extractK8sError(err)?.status !== 404) {
+ res.status(503).json({
+ error: 'registry_uninstall_outcome_ambiguous',
+ outcome: 'repair_required',
+ resourceName,
+ resourceType,
+ namespace,
+ deleted,
+ warnings: [
+ ...warnings,
+ `Secret/${oauthClientSecretName}: ${err instanceof Error ? err.message : 'unable to verify deletion'}`,
+ ],
+ })
+ return
+ }
// Secret may not exist (no OAuth block, or reference mode)
}
diff --git a/control-api/test/routes.registryInstall.oauth.test.ts b/control-api/test/routes.registryInstall.oauth.test.ts
index 86a0bc0f6..e7818091d 100644
--- a/control-api/test/routes.registryInstall.oauth.test.ts
+++ b/control-api/test/routes.registryInstall.oauth.test.ts
@@ -535,6 +535,59 @@ describe('POST /admin/registry/install — OAuth (S1-U2/U3)', () => {
await expect(gw.getSecret('my-gmail-oauth-client', 'mcp-server')).rejects.toThrow()
})
+ // P2 (T3): the managed oauth-client Secret must be deleted under the same
+ // snapshot/precondition fence as `-credentials`, never by-name. If a concurrent
+ // reinstall recreates a DIFFERENT owner's same-name Secret between the pre-CR
+ // snapshot capture and the delete, the fenced delete must NOT raze it. A by-name
+ // delete (the pre-fix behaviour) would.
+ it('does not raze a recreated (different-uid) oauth-client Secret on uninstall', async () => {
+ const gw = new MockGateway('mcp-server')
+ gw.createResource('mcpservers', {
+ metadata: { name: 'my-gmail' },
+ spec: {
+ image: 'clerum/gmail-mcp:1.0.0',
+ auth: { type: 'oauth' },
+ oauth: {
+ id: 'my-gmail',
+ provider: 'google',
+ clientIdRef: { name: 'my-gmail-oauth-client', key: 'client_id' },
+ clientSecretRef: { name: 'my-gmail-oauth-client', key: 'client_secret' },
+ },
+ },
+ })
+ // The Secret whose identity the uninstall captures before deleting the CR.
+ gw.seedSecret('my-gmail-oauth-client', 'mcp-server', {
+ stringData: { client_id: 'id', client_secret: 'sec' },
+ })
+ // Identity derived from the real producer (MockGateway.getSecret), not hand-built.
+ const original = await gw.getSecret('my-gmail-oauth-client', 'mcp-server')
+ const originalUid = (original as { metadata: { uid: string } }).metadata.uid
+
+ // Model the delete/recreate race dev closed for `-credentials`: once the CR is
+ // gone, a concurrent reinstall's Secret takes the same name with a new uid.
+ const origDeleteResource = gw.deleteResource.bind(gw)
+ gw.deleteResource = (async (plural, name, ns, precond) => {
+ const result = await origDeleteResource(plural, name, ns, precond)
+ if (plural === 'mcpservers' && name === 'my-gmail') {
+ gw.seedSecret('my-gmail-oauth-client', 'mcp-server', {
+ uid: 'uid-reinstall-different-owner',
+ stringData: { client_id: 'new', client_secret: 'new' },
+ })
+ }
+ return result
+ }) as typeof gw.deleteResource
+
+ await request(makeApp(gw)).delete('/admin/registry/uninstall/my-gmail')
+
+ // The reinstall's Secret survives — a by-name delete would have deleted it.
+ const survivor = await gw.getSecret('my-gmail-oauth-client', 'mcp-server').catch(() => null)
+ expect(survivor).not.toBeNull()
+ expect((survivor as { metadata: { uid: string } }).metadata.uid).toBe(
+ 'uid-reinstall-different-owner'
+ )
+ expect(originalUid).not.toBe('uid-reinstall-different-owner')
+ })
+
// FIX 2: a present-but-invalid body.oauth must report the real validation
// failure, not the "credentials required" message — and must not leak values.
it('400s with a descriptive message when body.oauth.grantScope is invalid', async () => {