Skip to content

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

TypeScript

fix(auth): finish logging and production image contracts

Alfredo Lopez60d69040fb96
diff --git a/control-api/Dockerfile b/control-api/Dockerfile
index e433412d9..e4b41c64c 100644
--- a/control-api/Dockerfile
+++ b/control-api/Dockerfile
@@ -36,6 +36,9 @@ COPY control-api/src ./src
RUN npm run build
RUN npm prune --production
+# Its compiled dist and file-linked llm-providers are runtime inputs; the
+# TypeScript/Vitest toolchain was needed only until both package/service builds.
+RUN npm --prefix /app/packages/workflow-runtime-core prune --omit=dev
FROM node:24-alpine
RUN addgroup -S app && adduser -S app -G app
diff --git a/control-api/Makefile b/control-api/Makefile
index 25c894f91..9f42a1d2f 100644
--- a/control-api/Makefile
+++ b/control-api/Makefile
@@ -1,5 +1,7 @@
.PHONY: install build docker-build docker-push
+PROJECT_ROOT := $(abspath $(dir $(lastword $(MAKEFILE_LIST)))/..)
+
REGISTRY ?= us-central1-docker.pkg.dev/${GCP_PROJECT}/clerum
IMAGE_NAME ?= control-api
TAG ?= $(shell git describe --tags --always 2>/dev/null || echo "latest")
@@ -11,7 +13,7 @@ build: install
npm run build
docker-build:
- docker build -t $(REGISTRY)/$(IMAGE_NAME):$(TAG) .
+ docker build -f "$(PROJECT_ROOT)/control-api/Dockerfile" -t $(REGISTRY)/$(IMAGE_NAME):$(TAG) "$(PROJECT_ROOT)"
docker-push: docker-build
docker push $(REGISTRY)/$(IMAGE_NAME):$(TAG)
diff --git a/control-api/package-lock.json b/control-api/package-lock.json
index d819ebf63..605c2cfc3 100644
--- a/control-api/package-lock.json
+++ b/control-api/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-api",
- "version": "0.1.689",
+ "version": "0.1.690",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.689",
+ "version": "0.1.690",
"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 d3ad9b7fc..e3cad1a60 100644
--- a/control-api/package.json
+++ b/control-api/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-api",
- "version": "0.1.689",
+ "version": "0.1.690",
"description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets",
"main": "dist/main.js",
"engines": {
diff --git a/control-api/test/config.approvalRateLimit.test.ts b/control-api/test/config.approvalRateLimit.test.ts
index b0507dc27..ec1b3f3ef 100644
--- a/control-api/test/config.approvalRateLimit.test.ts
+++ b/control-api/test/config.approvalRateLimit.test.ts
@@ -1,4 +1,45 @@
-import { afterEach, describe, expect, it, vi } from 'vitest'
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+
+const LOG_LEVELS = ['warn', 'info', 'error', 'debug'] as const
+const loggerCalls = {
+ warn: vi.fn<(...args: unknown[]) => void>(),
+ info: vi.fn<(...args: unknown[]) => void>(),
+ error: vi.fn<(...args: unknown[]) => void>(),
+ debug: vi.fn<(...args: unknown[]) => void>(),
+}
+const TOPOLOGY_EVENT = 'external_rate_limit_topology_advisory'
+
+function advisoryCalls(level: (typeof LOG_LEVELS)[number]) {
+ return loggerCalls[level].mock.calls.filter(
+ ([fields]) =>
+ typeof fields === 'object' &&
+ fields !== null &&
+ 'event' in fields &&
+ fields.event === TOPOLOGY_EVENT
+ )
+}
+
+function expectNoAdvisory() {
+ for (const level of LOG_LEVELS) expect(advisoryCalls(level)).toEqual([])
+}
+
+function expectAdvisory(values: readonly number[], sources: readonly string[]) {
+ expect(advisoryCalls('warn')).toEqual([
+ [
+ {
+ event: TOPOLOGY_EVENT,
+ resolved: {
+ operation: { value: values[0], source: sources[0] },
+ session: { value: values[1], source: sources[1] },
+ clientIp: { value: values[2], source: sources[2] },
+ },
+ recommendedTopology: 'operation <= session < clientIp',
+ },
+ 'External rate limits cross the recommended topology; preserving configured values',
+ ],
+ ])
+ for (const level of ['info', 'error', 'debug'] as const) expect(advisoryCalls(level)).toEqual([])
+}
const RATE_LIMIT_KEYS = [
'APPROVAL_RL_REQUEST_PER_MIN',
@@ -18,6 +59,13 @@ async function loadConfigWith(
Object.assign(process.env, overrides)
vi.resetModules()
try {
+ // Attach after the reset to the same real logger instance config imports.
+ const { rootLogger } = await import('../src/observability/logger.js')
+ for (const level of LOG_LEVELS) {
+ vi.spyOn(rootLogger, level).mockImplementation((...args: unknown[]) =>
+ loggerCalls[level](...args)
+ )
+ }
const mod = await import('../src/config.js')
return mod.config
} finally {
@@ -30,6 +78,7 @@ async function loadConfigWith(
}
describe('control-api approval rate limit config', () => {
+ beforeEach(() => vi.clearAllMocks())
afterEach(() => {
vi.resetModules()
vi.restoreAllMocks()
@@ -50,13 +99,12 @@ describe('control-api approval rate limit config', () => {
})
it('uses the recommended external defaults without a boot advisory', async () => {
- const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined)
const config = await loadConfigWith({})
expect(config.approvalRlExternalPerMin).toBe(60)
expect(config.approvalRlExternalEdgePerMin).toBe(120)
expect(config.approvalRlExternalClientIpPerMin).toBe(1200)
- expect(warn).not.toHaveBeenCalled()
+ expectNoAdvisory()
})
it.each([
@@ -64,36 +112,41 @@ describe('control-api approval rate limit config', () => {
label: 'operation above the default session budget',
override: { APPROVAL_RL_EXTERNAL_PER_MIN: '121' },
expected: [121, 120, 1200],
+ sources: ['environment', 'default', 'default'],
},
{
label: 'session equal to the default client-IP budget',
override: { APPROVAL_RL_EXTERNAL_EDGE_PER_MIN: '1200' },
expected: [60, 1200, 1200],
+ sources: ['default', 'environment', 'default'],
},
{
label: 'client-IP equal to the default session budget',
override: { APPROVAL_RL_EXTERNAL_CLIENT_IP_PER_MIN: '120' },
expected: [60, 120, 120],
+ sources: ['default', 'default', 'environment'],
},
{
label: 'session below the default operation budget',
override: { APPROVAL_RL_EXTERNAL_EDGE_PER_MIN: '30' },
expected: [60, 30, 1200],
+ sources: ['default', 'environment', 'default'],
},
- ])('boots with and preserves a partial override: $label', async ({ override, expected }) => {
- const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined)
- const config = await loadConfigWith(override)
-
- expect([
- config.approvalRlExternalPerMin,
- config.approvalRlExternalEdgePerMin,
- config.approvalRlExternalClientIpPerMin,
- ]).toEqual(expected)
- expect(warn).toHaveBeenCalledTimes(1)
- })
+ ])(
+ 'boots with and preserves a partial override: $label',
+ async ({ override, expected, sources }) => {
+ const config = await loadConfigWith(override)
+
+ expect([
+ config.approvalRlExternalPerMin,
+ config.approvalRlExternalEdgePerMin,
+ config.approvalRlExternalClientIpPerMin,
+ ]).toEqual(expected)
+ expectAdvisory(expected, sources)
+ }
+ )
it('accepts equality and reversed scopes while preserving every explicit value', async () => {
- const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined)
const config = await loadConfigWith({
APPROVAL_RL_EXTERNAL_PER_MIN: '500',
APPROVAL_RL_EXTERNAL_EDGE_PER_MIN: '120',
@@ -103,28 +156,16 @@ describe('control-api approval rate limit config', () => {
expect(config.approvalRlExternalPerMin).toBe(500)
expect(config.approvalRlExternalEdgePerMin).toBe(120)
expect(config.approvalRlExternalClientIpPerMin).toBe(120)
- expect(warn).toHaveBeenCalledTimes(1)
+ expectAdvisory([500, 120, 120], ['environment', 'environment', 'environment'])
})
it('warns once with the resolved tuple and source without mutating values', async () => {
- const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined)
const config = await loadConfigWith({ APPROVAL_RL_EXTERNAL_PER_MIN: '121' })
expect(config.approvalRlExternalPerMin).toBe(121)
expect(config.approvalRlExternalEdgePerMin).toBe(120)
expect(config.approvalRlExternalClientIpPerMin).toBe(1200)
- expect(warn).toHaveBeenCalledTimes(1)
- expect(warn).toHaveBeenCalledWith(
- expect.stringContaining('preserving exact configured values'),
- {
- resolved: {
- operation: { value: 121, source: 'environment' },
- session: { value: 120, source: 'default' },
- clientIp: { value: 1200, source: 'default' },
- },
- recommendedTopology: 'operation <= session < clientIp',
- }
- )
+ expectAdvisory([121, 120, 1200], ['environment', 'default', 'default'])
})
it.each([
@@ -142,7 +183,6 @@ describe('control-api approval rate limit config', () => {
it.each(['', ' ', '\t'])(
'treats a blank external rate-limit override (%j) as the default',
async blank => {
- const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined)
const config = await loadConfigWith({
APPROVAL_RL_EXTERNAL_PER_MIN: blank,
APPROVAL_RL_EXTERNAL_EDGE_PER_MIN: blank,
@@ -152,12 +192,11 @@ describe('control-api approval rate limit config', () => {
expect(config.approvalRlExternalPerMin).toBe(60)
expect(config.approvalRlExternalEdgePerMin).toBe(120)
expect(config.approvalRlExternalClientIpPerMin).toBe(1200)
- expect(warn).not.toHaveBeenCalled()
+ expectNoAdvisory()
}
)
it('does not warn for a coherent explicit topology', async () => {
- const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined)
const config = await loadConfigWith({
APPROVAL_RL_EXTERNAL_PER_MIN: '30',
APPROVAL_RL_EXTERNAL_EDGE_PER_MIN: '60',
@@ -167,6 +206,6 @@ describe('control-api approval rate limit config', () => {
expect(config.approvalRlExternalPerMin).toBe(30)
expect(config.approvalRlExternalEdgePerMin).toBe(60)
expect(config.approvalRlExternalClientIpPerMin).toBe(600)
- expect(warn).not.toHaveBeenCalled()
+ expectNoAdvisory()
})
})
diff --git a/control-api/test/config.memberRegistrationMode.test.ts b/control-api/test/config.memberRegistrationMode.test.ts
index f894d2414..d290cf0f4 100644
--- a/control-api/test/config.memberRegistrationMode.test.ts
+++ b/control-api/test/config.memberRegistrationMode.test.ts
@@ -1,6 +1,29 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { generateKeyPairSync, randomBytes } from 'node:crypto'
+const LOG_LEVELS = ['warn', 'info', 'error', 'debug'] as const
+const loggerCalls = {
+ warn: vi.fn<(...args: unknown[]) => void>(),
+ info: vi.fn<(...args: unknown[]) => void>(),
+ error: vi.fn<(...args: unknown[]) => void>(),
+ debug: vi.fn<(...args: unknown[]) => void>(),
+}
+const IGNORED_ENV_EVENT = 'hosted_member_registration_env_secret_ignored'
+
+function ignoredEnvCalls(level: (typeof LOG_LEVELS)[number]) {
+ return loggerCalls[level].mock.calls.filter(
+ ([fields]) =>
+ typeof fields === 'object' &&
+ fields !== null &&
+ 'event' in fields &&
+ fields.event === IGNORED_ENV_EVENT
+ )
+}
+
+function expectNoIgnoredEnvEvent() {
+ for (const level of LOG_LEVELS) expect(ignoredEnvCalls(level)).toEqual([])
+}
+
function generateNonDevPem(): string {
return generateKeyPairSync('rsa', {
modulusLength: 2048,
@@ -49,11 +72,19 @@ const MEMBER_REG_VARS = [
describe('config: member-registration mode', () => {
const origEnv = { ...process.env }
- beforeEach(() => {
+ beforeEach(async () => {
+ vi.clearAllMocks()
vi.resetModules()
process.env = { ...origEnv }
for (const name of MEMBER_REG_VARS) delete process.env[name]
delete process.env.NODE_ENV
+ // Attach after the reset to the same real logger instance config imports.
+ const { rootLogger } = await import('../src/observability/logger.js')
+ for (const level of LOG_LEVELS) {
+ vi.spyOn(rootLogger, level).mockImplementation((...args: unknown[]) =>
+ loggerCalls[level](...args)
+ )
+ }
})
afterEach(() => {
@@ -65,6 +96,7 @@ describe('config: member-registration mode', () => {
it('defaults to remote', async () => {
const { config } = await import('../src/config.js')
expect(config.memberRegistrationMode).toBe('remote')
+ expectNoIgnoredEnvEvent()
})
it('parses hosted and defaults the external hub base URL', async () => {
@@ -74,6 +106,7 @@ describe('config: member-registration mode', () => {
expect(config.memberRegistrationExternalHubBaseUrl).toBe(
'https://registration.evenfire.ai/api/v1'
)
+ expectNoIgnoredEnvEvent()
})
it('honors an explicit external hub override (staging hub)', async () => {
@@ -117,12 +150,16 @@ describe('config: member-registration mode', () => {
})
it('ignores a lone deploy-injected HMAC_SECRET in hosted mode, with a warning', async () => {
- const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
process.env.CONTROL_API_MEMBER_REGISTRATION_MODE = 'hosted'
process.env.CONTROL_API_MEMBER_REGISTRATION_HMAC_SECRET = 'legacy-injected-secret'
await expect(import('../src/config.js')).resolves.toBeDefined()
- expect(warn).toHaveBeenCalledWith(
- expect.stringContaining('CONTROL_API_MEMBER_REGISTRATION_HMAC_SECRET')
+ expect(ignoredEnvCalls('warn')).toEqual([
+ [{ event: IGNORED_ENV_EVENT }, 'Hosted member-registration uses its enrolled credentials'],
+ ])
+ for (const level of ['info', 'error', 'debug'] as const)
+ expect(ignoredEnvCalls(level)).toEqual([])
+ expect(JSON.stringify(ignoredEnvCalls('warn'))).not.toContain(
+ process.env.CONTROL_API_MEMBER_REGISTRATION_HMAC_SECRET
)
})
diff --git a/control-api/tsconfig.jwt-contract-tests.json b/control-api/tsconfig.jwt-contract-tests.json
index 6f314f7be..1944f4977 100644
--- a/control-api/tsconfig.jwt-contract-tests.json
+++ b/control-api/tsconfig.jwt-contract-tests.json
@@ -14,6 +14,8 @@
"test/config.jwtHardening.test.ts",
"test/config.prod.test.ts",
"test/config.registryConnectionMode.test.ts",
+ "test/config.approvalRateLimit.test.ts",
+ "test/config.memberRegistrationMode.test.ts",
"test/bannedDevSigningKeys.test.ts",
"test/devSigningKeys.test.ts",
"test/services.registryConnectionDb.test.ts",
diff --git a/scripts/tests/test-dockerfile-local-packages.sh b/scripts/tests/test-dockerfile-local-packages.sh
index 35eed65c1..85396f44e 100755
--- a/scripts/tests/test-dockerfile-local-packages.sh
+++ b/scripts/tests/test-dockerfile-local-packages.sh
@@ -205,6 +205,30 @@ for service in control-api rpc-proxy external-rest-api; do
assert_jwt_policy_runtime_context "$service"
done
+assert_control_api_runtime_prune_after_builds() {
+ local file="$REPO_ROOT/control-api/Dockerfile" final_build prune runtime
+ final_build="$(line_of_last "$file" 'RUN npm run build')"
+ prune="$(line_of_first "$file" 'RUN npm --prefix /app/packages/workflow-runtime-core prune --omit=dev')"
+ runtime="$(awk '/^FROM / { count++; if (count == 2) { print NR; exit } }' "$file")"
+ if [[ -z "$final_build" || -z "$prune" || -z "$runtime" || \
+ "$prune" -le "$final_build" || "$prune" -ge "$runtime" ]]; then
+ fail 'Control API must prune workflow-runtime-core dev dependencies after all builds, before runtime COPY'
+ fi
+}
+
+assert_control_api_make_root_context() {
+ local from_root from_service expected
+ expected="docker build -f \"$REPO_ROOT/control-api/Dockerfile\" -t example.invalid/evenfire/control-api:fixture \"$REPO_ROOT\""
+ from_root="$(cd "$REPO_ROOT" && make -n -f control-api/Makefile docker-build REGISTRY=example.invalid/evenfire TAG=fixture)"
+ from_service="$(cd "$REPO_ROOT/control-api" && make -n docker-build REGISTRY=example.invalid/evenfire TAG=fixture)"
+ if [[ "$from_root" != "$expected" || "$from_service" != "$expected" ]]; then
+ fail 'Control API Make docker-build must use its explicit Dockerfile and root context from either supported cwd'
+ fi
+}
+
+assert_control_api_runtime_prune_after_builds
+assert_control_api_make_root_context
+
# Direct consumers. The first four are Node services; profile-ui and
# control-ui are Next.js consumers and therefore also require materialization.
assert_copy_before_every_ci control-api/Dockerfile \