Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
feat(host-context-controller): egress reverse-index for guardrail hooks
Andy Pavia3c05e036fdc8
diff --git a/host-context-controller/package-lock.json b/host-context-controller/package-lock.jsonindex 7ed85b211..30215a324 100644--- a/host-context-controller/package-lock.json+++ b/host-context-controller/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-host-context-controller",- "version": "0.1.164",+ "version": "0.1.165","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-host-context-controller",- "version": "0.1.164",+ "version": "0.1.165","license": "SEE LICENSE IN LICENSE","dependencies": {"@clerum/image-policy": "file:../packages/image-policy",diff --git a/host-context-controller/package.json b/host-context-controller/package.jsonindex 9bf751897..2b9bf2c0f 100644--- a/host-context-controller/package.json+++ b/host-context-controller/package.json@@ -1,6 +1,6 @@{"name": "clerum-host-context-controller",- "version": "0.1.164",+ "version": "0.1.165","description": "Host Context Controller - provides McpServer CRDs to mcp-host via REST API","main": "dist/main.js","engines": {diff --git a/host-context-controller/src/hostReconciler.ts b/host-context-controller/src/hostReconciler.tsindex 5730e5553..00630f864 100644--- a/host-context-controller/src/hostReconciler.ts+++ b/host-context-controller/src/hostReconciler.ts@@ -19,6 +19,7 @@ import {WFC_APP_LABEL,sharedFileSystemHash,} from './k8s/sharedFileSystemFactory'+import { referencedHookIds } from './llmHookReconciler'import { hccLogger } from './logger'import { issueMcpHostRuntimeTokens } from './mcpHostRuntimeTokenIssuerClient'import {@@ -3480,6 +3481,89 @@ export class HostReconciler {)}+ /**+ * Egress reverse-index for guardrail hooks (spec §8.2). Lifts the host pod's+ * default-deny egress so it can reach installed LlmHook endpoints in the+ * llm-hooks namespace — the counterpart to the per-podKey INGRESS reverse-index+ * on the hook side (llmHookReconciler.buildNetworkPolicy). Without it a Host+ * that references hooks resolves them but every `/v1` call is dropped by+ * `deny-all-mcp-host`.+ *+ * The AUTHORITATIVE admission control stays the hook-side ingress NP (it admits+ * only the referencing hosts, deny-all otherwise); this rule only opens the+ * host→llm-hooks lane. Ports are per-hook (`img.port`) so the rule is not+ * port-restricted — reachability is scoped by the hook-side ingress. Service+ * targets in OTHER namespaces and remote (external) targets are out of scope+ * (follow-up). Emitted only when the Host references ≥1 hook; otherwise the NP+ * is removed so clearing all hooks re-closes the lane.+ */+ private async ensureMcpHostLlmHooksEgressNetworkPolicy(host: HostCRD): Promise<void> {+ const policyName = `mcp-host-${host.name}-egress-llm-hooks`++ if (referencedHookIds(host).length === 0) {+ // No hooks referenced → ensure the lane is closed (idempotent, ownership-gated).+ await this.deleteIfHccOwned(+ 'NetworkPolicy',+ policyName,+ host.namespace,+ host.name,+ () =>+ this.networkingApi.readNamespacedNetworkPolicy({+ name: policyName,+ namespace: host.namespace,+ }),+ () =>+ this.networkingApi.deleteNamespacedNetworkPolicy({+ name: policyName,+ namespace: host.namespace,+ })+ )+ return+ }++ const policy: k8s.V1NetworkPolicy = {+ apiVersion: 'networking.k8s.io/v1',+ kind: 'NetworkPolicy',+ metadata: {+ name: policyName,+ namespace: host.namespace,+ labels: {+ [MANAGED_BY_LABEL]: MANAGED_BY_VALUE,+ [HOST_LABEL]: host.name,+ 'clerum.io/policy-type': 'llm-hooks-egress',+ },+ },+ spec: {+ podSelector: {+ matchLabels: {+ [HOST_LABEL]: host.name,+ [MANAGED_BY_LABEL]: MANAGED_BY_VALUE,+ },+ },+ policyTypes: ['Egress'],+ egress: [+ {+ to: [+ {+ namespaceSelector: {+ matchLabels: { 'kubernetes.io/metadata.name': config.llmHooksNamespace },+ },+ },+ ],+ },+ ],+ },+ }++ await applyNetworkPolicy(+ this.networkingApi,+ policyName,+ host.namespace,+ policy,+ '[HostReconciler]'+ )+ }+/*** Delete per-Host NetworkPolicies created by this reconciler.* 404-tolerant — safe to call even if NPs were never created or already gone.@@ -3491,6 +3575,7 @@ export class HostReconciler {{ name: `mcp-host-${name}-ingress-workflow-approval-reader`, namespace },{ name: `mcp-host-${name}-ingress-rpc-proxy`, namespace },{ name: `mcp-host-${name}-egress-gfs`, namespace },+ { name: `mcp-host-${name}-egress-llm-hooks`, namespace },{ name: `channel-reader-${name}-egress`, namespace: config.channelsNamespace },{name: `workflow-approval-reader-${name}-egress-mcp-host`,@@ -3688,6 +3773,7 @@ export class HostReconciler {try {await this.ensureMcpHostIngressNetworkPolicy(host)await this.ensureMcpHostGfsEgressNetworkPolicy(host)+ await this.ensureMcpHostLlmHooksEgressNetworkPolicy(host)await this.ensureWorkflowApprovalReaderMcpHostIngressNetworkPolicy(host)} catch (err) {console.error(`[HostReconciler] Failed to ensure mcp-host NP for "${host.name}":`, err)diff --git a/host-context-controller/test/hostReconciler.networkpolicy.test.ts b/host-context-controller/test/hostReconciler.networkpolicy.test.tsindex cf13bdedd..2b5123f8d 100644--- a/host-context-controller/test/hostReconciler.networkpolicy.test.ts+++ b/host-context-controller/test/hostReconciler.networkpolicy.test.ts@@ -22,6 +22,7 @@ vi.mock('../src/config', () => ({hostNamespace: 'mcp-host',rpcProxyNamespace: 'rpc-proxy',channelsNamespace: 'channels',+ llmHooksNamespace: 'llm-hooks',hostFullReconcileConcurrency: 2,channelReaderImage: 'clerum/channel-reader:test',channelReaderImagePullPolicy: 'IfNotPresent',@@ -152,6 +153,52 @@ describe('HostReconciler.ensureChannelReaderEgressNetworkPolicy', () => {})})+describe('HostReconciler.ensureMcpHostLlmHooksEgressNetworkPolicy', () => {+ const HOST_WITH_HOOKS: HostCRD = {+ ...HOST,+ spec: {+ ...HOST.spec,+ guardrails: { hooks: { preCall: [{ id: 'hook-x-v1-0-0-abcd1234' }] } },+ },+ }++ it('opens host→llm-hooks egress when the Host references hooks', async () => {+ const { reconciler, mocks } = createReconciler()++ await (reconciler as any).ensureMcpHostLlmHooksEgressNetworkPolicy(HOST_WITH_HOOKS)++ expect(mocks.networkingApi.createNamespacedNetworkPolicy).toHaveBeenCalledTimes(1)+ const call = (mocks.networkingApi.createNamespacedNetworkPolicy as any).mock.calls[0][0]++ expect(call.namespace).toBe('mcp-host')+ expect(call.body.metadata.name).toBe('mcp-host-chatllm-egress-llm-hooks')+ expect(call.body.metadata.labels['clerum.io/managed-by']).toBe('host-context-controller')+ expect(call.body.metadata.labels['clerum.io/host']).toBe('chatllm')+ expect(call.body.metadata.labels['clerum.io/policy-type']).toBe('llm-hooks-egress')+ // Source pod selector requires managed-by so the policy can't be spoofed.+ expect(call.body.spec.podSelector.matchLabels).toEqual({+ 'clerum.io/host': 'chatllm',+ 'clerum.io/managed-by': 'host-context-controller',+ })+ expect(call.body.spec.policyTypes).toEqual(['Egress'])+ expect(call.body.spec.egress).toHaveLength(1)+ expect(call.body.spec.egress[0].to[0].namespaceSelector.matchLabels).toEqual({+ 'kubernetes.io/metadata.name': 'llm-hooks',+ })+ // Not port-restricted: hook ports vary per hook; the hook-side ingress NP is+ // the authoritative admission control.+ expect(call.body.spec.egress[0].ports).toBeUndefined()+ })++ it('does not open the lane when the Host references no hooks', async () => {+ const { reconciler, mocks } = createReconciler()++ await (reconciler as any).ensureMcpHostLlmHooksEgressNetworkPolicy(HOST)++ expect(mocks.networkingApi.createNamespacedNetworkPolicy).not.toHaveBeenCalled()+ })+})+describe('HostReconciler.ensureMcpHostIngressNetworkPolicy', () => {it('creates the ingress NP in mcp-host namespace with correct selectors', async () => {const { reconciler, mocks } = createReconciler()