Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
refactor(control-api): derive registryClient's auth decision from credential presence
Jose Ramirezc61af822883c
diff --git a/control-api/package-lock.json b/control-api/package-lock.jsonindex 1edb84401..324f6cab5 100644--- a/control-api/package-lock.json+++ b/control-api/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-control-api",- "version": "0.1.386",+ "version": "0.1.387","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-control-api",- "version": "0.1.386",+ "version": "0.1.387","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 8f4a4c6cd..9c429ab9a 100644--- a/control-api/package.json+++ b/control-api/package.json@@ -1,6 +1,6 @@{"name": "clerum-control-api",- "version": "0.1.386",+ "version": "0.1.387","description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets","main": "dist/main.js","engines": {diff --git a/control-api/src/services/registryClient.ts b/control-api/src/services/registryClient.tsindex dee0020a0..dfea3ab24 100644--- a/control-api/src/services/registryClient.ts+++ b/control-api/src/services/registryClient.ts@@ -7,7 +7,7 @@*/import { config } from '../config.js'import { rootLogger } from '../observability/logger.js'-import { resolveMachineCreds } from './registryConnectionDb.js'+import { isRegistryAuthActive, resolveMachineCreds } from './registryConnectionDb.js'const API_BASE = `${config.registryUrl}/api/v1`@@ -100,13 +100,12 @@ export async function mintToken(envOverride?: NodeJS.ProcessEnv): Promise<string}}- const authEnabledRaw = env.CLERUM_REGISTRY_AUTH_ENABLED- const authEnabled =- authEnabledRaw === undefined- ? envOverride- ? false- : config.registryAuthEnabled- : authEnabledRaw === 'true'+ // Auth is ACTIVE when credentials actually exist (self-hosted) or the env+ // says so (managed). The old CLERUM_REGISTRY_AUTH_ENABLED override is gone:+ // in self-hosted the var is inert, and in managed isRegistryAuthActive()+ // returns it verbatim. envOverride still short-circuits for the env-driven+ // test/minikube paths, which must never touch the DB.+ const authEnabled = envOverride ? false : await isRegistryAuthActive()if (!id || !secret) {if (!authEnabled) return '' // minikube / auth-offdiff --git a/control-api/test/registryClient.test.ts b/control-api/test/registryClient.test.tsindex d433e4fda..86cdcccf4 100644--- a/control-api/test/registryClient.test.ts+++ b/control-api/test/registryClient.test.ts@@ -11,6 +11,17 @@ import {searchEntries,} from '../src/services/registryClient.js'+// Controls the derived-auth path (Task 7): mintToken() with NO envOverride+// consults isRegistryAuthActive() for the authEnabled decision and+// resolveMachineCreds() for self-hosted credentials. Every OTHER test in this+// file drives mintToken via ENV_OVERRIDE or process.env id/secret directly,+// so it never reaches either mock — only the two derived-auth tests below do.+const connDb = vi.hoisted(() => ({+ isRegistryAuthActive: vi.fn(),+ resolveMachineCreds: vi.fn(),+}))+vi.mock('../src/services/registryConnectionDb.js', () => connDb)+function fakeTokenResponse(token = 'tok-abc', expiresIn = 600) {return new Response(JSON.stringify({ access_token: token, token_type: 'Bearer', expires_in: expiresIn }),@@ -28,6 +39,13 @@ const ENV_OVERRIDE: NodeJS.ProcessEnv = {beforeEach(() => {__resetTokenCacheForTests()__resetScopeCacheForTests()+ // Deterministic defaults for the connDb mock (no creds, auth off) rather+ // than relying on an unconfigured vi.fn() resolving `undefined` (falsy, but+ // for the wrong reason). Only the derived-auth tests below reach these; every+ // other test in the file resolves id/secret from ENV_OVERRIDE/process.env+ // before this mock would ever be consulted.+ connDb.resolveMachineCreds.mockReset().mockResolvedValue(null)+ connDb.isRegistryAuthActive.mockReset().mockResolvedValue(false)})afterEach(() => {@@ -127,6 +145,37 @@ describe('registryClient — mintToken cache behavior', () => {})})+describe('registryClient — mintToken derived auth (no envOverride)', () => {+ // Critical: no ENV_OVERRIDE here. An envOverride short-circuits authEnabled+ // to false by design (see mintToken), so these two tests must call+ // mintToken() bare to actually reach the isRegistryAuthActive()-derived path.+ // They also explicitly blank CLIENT_ID/SECRET: earlier tests in this file+ // mutate the REAL process.env via Object.assign(process.env, ENV_OVERRIDE)+ // and never reset it (only afterEach's unstubAllGlobals/useRealTimers run),+ // so without this, a leftover id/secret from a prior test would populate+ // `id`/`secret` and the credential-missing branch under test would never+ // trigger.+ beforeEach(() => {+ process.env = {+ ...process.env,+ CLERUM_REGISTRY_CLIENT_ID: '',+ CLERUM_REGISTRY_CLIENT_SECRET: '',+ }+ })++ it('returns an empty token when auth is inactive and no creds exist', async () => {+ connDb.resolveMachineCreds.mockResolvedValue(null)+ connDb.isRegistryAuthActive.mockResolvedValue(false)+ await expect(mintToken()).resolves.toBe('') // NO ENV_OVERRIDE — see above+ })++ it('throws when auth is active but credentials are unavailable', async () => {+ connDb.resolveMachineCreds.mockResolvedValue(null)+ connDb.isRegistryAuthActive.mockResolvedValue(true)+ await expect(mintToken()).rejects.toThrow(/machine credentials unavailable/)+ })+})+describe('registryClient — authedFetch', () => {it('attaches Authorization: Bearer on GET reads', async () => {const fetchMock = vi.fn().mockImplementation((url: string) => {diff --git a/control-api/test/services.registryClientMachineCreds.test.ts b/control-api/test/services.registryClientMachineCreds.test.tsindex e2212375c..e7f463d53 100644--- a/control-api/test/services.registryClientMachineCreds.test.ts+++ b/control-api/test/services.registryClientMachineCreds.test.ts@@ -13,7 +13,15 @@ const { cfg } = vi.hoisted(() => ({}))vi.mock('../src/config.js', () => ({ config: cfg }))-const resolver = vi.hoisted(() => ({ resolveMachineCreds: vi.fn() }))+// Task 7: mintToken() now also calls isRegistryAuthActive() to derive+// authEnabled (replacing the CLERUM_REGISTRY_AUTH_ENABLED env read). This is+// a whole-module mock, so isRegistryAuthActive must be included here too —+// otherwise it is undefined on the mocked module and mintToken's `await+// isRegistryAuthActive()` throws a TypeError in both tests below.+const resolver = vi.hoisted(() => ({+ resolveMachineCreds: vi.fn(),+ isRegistryAuthActive: vi.fn(),+}))vi.mock('../src/services/registryConnectionDb.js', () => resolver)afterEach(() => {@@ -29,6 +37,8 @@ describe('mintToken — self-hosted sources creds from the DB row', () => {clientId: 'db-id',clientSecret: 'db-secret',})+ // Mirrors the real self-hosted isRegistryAuthActive(): creds resolved → active.+ resolver.isRegistryAuthActive.mockResolvedValue(true)const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue(@@ -44,6 +54,8 @@ describe('mintToken — self-hosted sources creds from the DB row', () => {it('returns "" (auth-off) when no creds are resolvable and auth is disabled', async () => {cfg.registryAuthEnabled = falseresolver.resolveMachineCreds.mockResolvedValue(null)+ // Mirrors the real self-hosted isRegistryAuthActive(): no creds → inactive.+ resolver.isRegistryAuthActive.mockResolvedValue(false)expect(await mintToken()).toBe('')})})