Skip to content

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

TypeScript

fix(control-api): harden registry auth guards against a rejecting accessor

Jose Ramirez5f61e811ff9c
diff --git a/control-api/package-lock.json b/control-api/package-lock.json
index eed550432..eb5c37b60 100644
--- a/control-api/package-lock.json
+++ b/control-api/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-api",
- "version": "0.1.384",
+ "version": "0.1.385",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.384",
+ "version": "0.1.385",
"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.json
index ce70f26db..41d99b11c 100644
--- a/control-api/package.json
+++ b/control-api/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-api",
- "version": "0.1.384",
+ "version": "0.1.385",
"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 e87c523cf..e7e39552b 100644
--- a/control-api/src/routes/admin/registry.ts
+++ b/control-api/src/routes/admin/registry.ts
@@ -2166,7 +2166,14 @@ export function createAdminRegistryRouter(gateway?: K8sGateway): Router {
req: Request,
res: Response
): Promise<{ admin: AdminUserRecord; orgName: string } | null> {
- if (!(await isRegistryAuthActive())) {
+ let authActive: boolean
+ try {
+ authActive = await isRegistryAuthActive()
+ } catch {
+ res.status(502).json({ error: 'registry_integration_error' })
+ return null
+ }
+ if (!authActive) {
res.status(409).json({ error: 'registry_auth_disabled' })
return null
}
@@ -2264,7 +2271,14 @@ export function createAdminRegistryRouter(gateway?: K8sGateway): Router {
req: Request,
res: Response
): Promise<{ orgName: string; actingUserId: string } | null> {
- if (!(await isRegistryAuthActive())) {
+ let authActive: boolean
+ try {
+ authActive = await isRegistryAuthActive()
+ } catch {
+ res.status(502).json({ error: 'registry_integration_error' })
+ return null
+ }
+ if (!authActive) {
res.status(409).json({ error: 'registry_auth_disabled' })
return null
}
diff --git a/control-api/test/routes.adminRegistryGrants.test.ts b/control-api/test/routes.adminRegistryGrants.test.ts
index 159096afb..0879a6e92 100644
--- a/control-api/test/routes.adminRegistryGrants.test.ts
+++ b/control-api/test/routes.adminRegistryGrants.test.ts
@@ -60,6 +60,19 @@ const { cfg } = vi.hoisted(() => ({
cfg: { registryAuthEnabled: true, registryConnectionMode: 'managed' },
}))
vi.mock('../src/config.js', () => ({ config: cfg }))
+// Partial mock (unlike the Keys file's full replacement): the "wire shape"
+// tests below exercise the REAL registryClient.ts wrapper via importActual,
+// and its mintToken() calls resolveMachineCreds() unconditionally whenever env
+// creds are absent — regardless of the auth-enabled flag. A full-replacement
+// mock leaves resolveMachineCreds undefined and 500s those tests. Keep every
+// real export (resolveMachineCreds, getRegistryConnection, ...) and override
+// only the one symbol the route guard now consumes, mirroring the
+// registryClient.js partial mock above.
+const connDb = vi.hoisted(() => ({ isRegistryAuthActive: vi.fn() }))
+vi.mock('../src/services/registryConnectionDb.js', async importOriginal => {
+ const actual = await importOriginal<typeof import('../src/services/registryConnectionDb.js')>()
+ return { ...actual, isRegistryAuthActive: connDb.isRegistryAuthActive }
+})
vi.mock('../src/services/rateLimiterService.js', () => ({
checkAndIncrement: vi.fn(async () => ({
allowed: true,
@@ -84,6 +97,15 @@ function makeApp(adminId: string | null = 'admin-1') {
beforeEach(() => {
vi.clearAllMocks()
cfg.registryAuthEnabled = true
+ cfg.registryConnectionMode = 'managed'
+ // Default mirrors the real accessor's managed-mode branch (registryAuthEnabled
+ // verbatim) so the pre-existing tests below — which only flip
+ // cfg.registryAuthEnabled, not this mock — still reach the same 200/400/409
+ // they did before this module was mocked. Reading cfg at call time (not a
+ // captured literal) also means vi.clearAllMocks() can never silently freeze
+ // this to a stale value. The self-hosted test overrides this per-call via
+ // mockResolvedValue, independent of cfg.registryAuthEnabled.
+ connDb.isRegistryAuthActive.mockImplementation(async () => cfg.registryAuthEnabled)
vi.mocked(checkAndIncrement).mockResolvedValue({
allowed: true,
remaining: 29,
@@ -230,6 +252,18 @@ describe('GET /admin/registry/grants + granted-to-me', () => {
expect(res.body).toEqual({ grants: [] })
expect(listGrantedToMe).toHaveBeenCalledWith('acme')
})
+
+ // Discriminates resolveSelfServiceOrg's guard from a plain config read: reverting
+ // just that guard to `if (!config.registryAuthEnabled)` would make this fail,
+ // since registryAuthEnabled is deliberately false here and only the mocked
+ // accessor says auth is active.
+ it('self-hosted: grants work with NO auth env var once credentials exist', async () => {
+ cfg.registryConnectionMode = 'self-hosted'
+ cfg.registryAuthEnabled = false // deliberately off — must be ignored
+ connDb.isRegistryAuthActive.mockResolvedValue(true) // credentials exist
+ const res = await request(makeApp()).get('/admin/registry/grants')
+ expect(res.status).not.toBe(409)
+ })
})
describe('DELETE /admin/registry/grants/:id', () => {
diff --git a/control-api/test/routes.adminRegistryKeys.test.ts b/control-api/test/routes.adminRegistryKeys.test.ts
index dbea06d72..7d23af578 100644
--- a/control-api/test/routes.adminRegistryKeys.test.ts
+++ b/control-api/test/routes.adminRegistryKeys.test.ts
@@ -124,6 +124,20 @@ describe('GET /admin/registry/keys', () => {
expect(res.status).not.toBe(409)
})
+ // isRegistryAuthActive() can now reject (self-hosted reaches a raw pool.query
+ // in getRegistryConnection with no try/catch of its own). The guard must catch
+ // it and degrade to the same 502 used for a resolvePublishScope failure one
+ // line below, rather than let the rejection escape uncaught — Express 4 does
+ // not forward async rejections to error middleware, so an uncaught one here
+ // would crash the process instead of producing a response.
+ it('502 registry_integration_error when isRegistryAuthActive rejects (does not throw)', async () => {
+ connDb.isRegistryAuthActive.mockRejectedValue(new Error('pg blip'))
+ const res = await request(makeApp()).get('/admin/registry/keys')
+ expect(res.status).toBe(502)
+ expect(res.body).toEqual({ error: 'registry_integration_error' })
+ expect(resolvePublishScope).not.toHaveBeenCalled()
+ })
+
it('401 when admin missing/inactive', async () => {
vi.mocked(findAdminById).mockResolvedValue(null as never)
const res = await request(makeApp()).get('/admin/registry/keys')