Skip to content

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

TypeScript

fix(hcc): retain identity-stable DNS egress allows; recreate on revoke [R3-B3]

Alfredo Lopezb9a812a1dfd6
diff --git a/host-context-controller/package-lock.json b/host-context-controller/package-lock.json
index 0c6745506..8f2ff7d00 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.210",
+ "version": "0.1.211",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-host-context-controller",
- "version": "0.1.210",
+ "version": "0.1.211",
"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.json
index 4ae07648f..337cf9818 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.210",
+ "version": "0.1.211",
"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/k8sClient.test.ts b/host-context-controller/src/k8sClient.test.ts
index bc038029e..e462bae8e 100644
--- a/host-context-controller/src/k8sClient.test.ts
+++ b/host-context-controller/src/k8sClient.test.ts
@@ -3784,6 +3784,40 @@ describe('McpServerWatcher startup', () => {
await watcher.stop()
})
+ it('wires onExternalEgressRevoked to schedule a MODIFIED external-egress retry (B3)', async () => {
+ const watcher = new McpServerWatcher()
+ ;(watcher as any).contextCacheSynced = true
+ ;(watcher as any).mcpServerCacheSynced = true
+ ;(watcher as any).contextWatchGeneration = 21
+ ;(watcher as any).mcpWatchGeneration = 34
+ const server = {
+ name: 'openai-mcp',
+ namespace: 'mcp-server',
+ spec: {
+ contextRef: 'dev',
+ image: 'openai:latest',
+ transport: { type: 'streamableHttp' as const, port: 3000 },
+ egressBindings: [{ dns: 'api.openai.com', port: 443 }],
+ },
+ }
+ const retrySpy = vi
+ .spyOn((watcher as any).externalEgressCoordinator, 'scheduleRetry')
+ .mockImplementation(() => undefined)
+ let captured: { onExternalEgressRevoked?: (s: unknown) => void } | undefined
+ mocks.netPolFullReconcile.mockImplementationOnce(
+ async (_contexts: unknown[], _servers: unknown[], options: typeof captured) => {
+ captured = options
+ }
+ )
+
+ await (watcher as any).runInitialNetworkPolicyConvergence()
+
+ expect(captured?.onExternalEgressRevoked).toBeTypeOf('function')
+ captured!.onExternalEgressRevoked!(server)
+ expect(retrySpy).toHaveBeenCalledWith('MODIFIED', server)
+ await watcher.stop()
+ })
+
it('does not start runtime fleet convergence without a current NetworkPolicy safety certificate', async () => {
const watcher = new McpServerWatcher()
;(watcher as any).contextCacheSynced = true
diff --git a/host-context-controller/src/k8sClient.ts b/host-context-controller/src/k8sClient.ts
index 38210e404..c980b2ec1 100644
--- a/host-context-controller/src/k8sClient.ts
+++ b/host-context-controller/src/k8sClient.ts
@@ -2766,6 +2766,13 @@ export class McpServerWatcher implements McpServerProvider {
resolveCurrentServer: name => this.servers.get(name),
contextDesiredRevision: () => this.contextDesiredRevision,
serverDesiredRevision: () => this.mcpServerDesiredRevision,
+ // B3 piece-2: queue an additive recreation for every server whose
+ // external-egress allow the authoritative pass revoked — fired at the
+ // deletion, so an abort/throw cannot strand a revoked-but-desired allow.
+ // The coordinator maps against the current cache (absent → DELETED, no
+ // recreate) and rebuilds from the current spec with fresh DNS.
+ onExternalEgressRevoked: server =>
+ this.externalEgressCoordinator.scheduleRetry('MODIFIED', server),
onAuthoritativeRevocationComplete: () => {
if (!this.recordNetworkPolicySafetyCertificate(safetyCertificate)) return
authoritativeRevocationCompleted = true
diff --git a/host-context-controller/src/networkPolicyReconciler.test.ts b/host-context-controller/src/networkPolicyReconciler.test.ts
index 9c9fe099d..0b3011a69 100644
--- a/host-context-controller/src/networkPolicyReconciler.test.ts
+++ b/host-context-controller/src/networkPolicyReconciler.test.ts
@@ -2285,6 +2285,127 @@ describe('NetworkPolicyReconciler', () => {
})
})
+ describe('L3 — DNS-derived retention by binding identity (B3)', () => {
+ // Live DNS policy built through the reconciler's OWN builder so its identity
+ // is exact-by-construction; each test drifts exactly one dimension.
+ function makeDnsFixture() {
+ const server: McpServerCRD = {
+ name: 'openai-mcp',
+ namespace: 'mcp-server',
+ spec: {
+ contextRef: 'dev',
+ image: 'openai:latest',
+ transport: { type: 'streamableHttp', port: 3000 },
+ egressBindings: [{ dns: 'api.openai.com', port: 443 }],
+ },
+ }
+ const binding = server.spec.egressBindings![0]
+ const name = (reconciler as any).externalEgressPolicyName(server.name, binding) as string
+ const live = (reconciler as any).buildExactHostEgressPolicy(server, name, binding, [
+ '1.2.3.4/32',
+ ]) as k8s.V1NetworkPolicy
+ live.metadata!.uid = 'live-dns-uid'
+ live.metadata!.resourceVersion = '11'
+ return { server, binding, name, live }
+ }
+
+ async function runSafety(server: McpServerCRD, live: k8s.V1NetworkPolicy): Promise<boolean> {
+ return (reconciler as any).reconcileExternalEgressSafety(
+ server,
+ [live],
+ () => true
+ ) as Promise<boolean>
+ }
+
+ function expectRevoked(name: string) {
+ expect(mockApi.deleteNamespacedNetworkPolicy).toHaveBeenCalledTimes(1)
+ expect(mockApi.deleteNamespacedNetworkPolicy).toHaveBeenCalledWith(
+ expect.objectContaining({
+ name,
+ namespace: 'mcp-server',
+ body: { preconditions: { uid: 'live-dns-uid', resourceVersion: '11' } },
+ })
+ )
+ }
+
+ it('(a) retains an unchanged DNS-derived allow — no revocation', async () => {
+ const { server, live } = makeDnsFixture()
+ await expect(runSafety(server, live)).resolves.toBe(true)
+ expect(mockApi.deleteNamespacedNetworkPolicy).not.toHaveBeenCalled()
+ })
+
+ it('(b) revokes on spec-level port drift under the same policy name', async () => {
+ const { server, name, live } = makeDnsFixture()
+ live.spec!.egress![0].ports = [{ port: 8443, protocol: 'TCP' }]
+ await expect(runSafety(server, live)).resolves.toBe(true)
+ expectRevoked(name)
+ })
+
+ it('(c) revokes on protocol drift TCP→UDP', async () => {
+ const { server, name, live } = makeDnsFixture()
+ live.spec!.egress![0].ports = [{ port: 443, protocol: 'UDP' }]
+ await expect(runSafety(server, live)).resolves.toBe(true)
+ expectRevoked(name)
+ })
+
+ it('(d) revokes a foreign-owned policy even when the spec is identical', async () => {
+ const { server, name, live } = makeDnsFixture()
+ live.metadata!.labels!['clerum.io/managed-by'] = 'foreign-controller'
+ await expect(runSafety(server, live)).resolves.toBe(true)
+ expectRevoked(name)
+ })
+
+ it('(f) revokes a degenerate zero-cidr "allow" (deny in disguise)', async () => {
+ const { server, name, live } = makeDnsFixture()
+ live.spec!.egress = []
+ await expect(runSafety(server, live)).resolves.toBe(true)
+ expectRevoked(name)
+ })
+
+ it('(e) queues recreation AT the revocation even when the pass then aborts and throws', async () => {
+ const { server, binding, name } = makeDnsFixture()
+ // Identity-drifted (protocol UDP) → this DNS allow is revoked (deleted).
+ const live = (reconciler as any).buildExactHostEgressPolicy(server, name, binding, [
+ '1.2.3.4/32',
+ ]) as k8s.V1NetworkPolicy
+ live.spec!.egress![0].ports = [{ port: 443, protocol: 'UDP' }]
+ live.metadata!.uid = 'live-dns-uid'
+ live.metadata!.resourceVersion = '11'
+
+ mockApi.listNamespacedNetworkPolicy.mockImplementation(
+ async ({ namespace, labelSelector }: { namespace?: string; labelSelector?: string }) => ({
+ items: !labelSelector && namespace === 'mcp-server' ? [live] : [],
+ })
+ )
+
+ let resolved: McpServerCRD | undefined = server
+ const spy = vi.fn()
+ // Flip authority false AT the deletion, so the pass aborts right after the
+ // revocation hook fires — reproducing the abort/throw window B3 must survive.
+ mockApi.deleteNamespacedNetworkPolicy.mockImplementationOnce(async () => {
+ resolved = undefined
+ return {}
+ })
+
+ const rec = makeReconciler(mockApi, new Map([[server.name, server]]), mockCustomApi)
+
+ await expect(
+ rec.fullReconcile([], [server], {
+ ensureDefaults: false,
+ contextInventoryAuthoritative: () => true,
+ serverInventoryAuthoritative: () => true,
+ resolveCurrentServer: () => resolved,
+ onExternalEgressRevoked: spy,
+ })
+ ).rejects.toThrow(/inventory changed during authoritative revocation/)
+
+ expect(spy).toHaveBeenCalledTimes(1)
+ expect(spy).toHaveBeenCalledWith(
+ expect.objectContaining({ name: server.name, namespace: server.namespace })
+ )
+ })
+ })
+
describe('startup fullReconcile', () => {
it('discovers and revokes orphaned policies with one missing ownership marker in every safety lane', async () => {
const partiallyLabelledByNamespace: Record<string, k8s.V1NetworkPolicy[]> = {
@@ -3725,7 +3846,7 @@ describe('NetworkPolicyReconciler', () => {
expect(dns.resolve4).not.toHaveBeenCalled()
})
- it('aborts certification when an identity-bound safety delete reports a recreate conflict', async () => {
+ it('aborts certification when an identity-drifted safety delete reports a recreate conflict', async () => {
const server: McpServerCRD = {
name: 'recreated-dns-server',
namespace: 'mcp-server',
@@ -3741,6 +3862,14 @@ describe('NetworkPolicyReconciler', () => {
const existing = mockApi.createNamespacedNetworkPolicy.mock.calls
.map(call => (call[0] as { body: k8s.V1NetworkPolicy }).body)
.find(policy => policy.metadata?.labels?.['clerum.io/policy-type'] === 'external-egress')!
+ // B3: identity-drift the live policy (protocol TCP→UDP) so the safety pass
+ // still revokes it. A same-intent (identity-stable) DNS allow is now
+ // RETAINED (see the retention pins in the B3 describe block); the revoke /
+ // abort machinery this test guards fires only for a drifted policy.
+ existing.spec!.egress = existing.spec!.egress!.map(rule => ({
+ ...rule,
+ ports: rule.ports?.map(port => ({ ...port, protocol: 'UDP' })),
+ }))
vi.clearAllMocks()
mockApi.listNamespacedNetworkPolicy.mockImplementation(
@@ -3910,7 +4039,7 @@ describe('NetworkPolicyReconciler', () => {
})
})
- it('revokes a same-intent DNS policy before readiness and defers its refresh', async () => {
+ it('revokes an identity-drifted DNS policy before readiness and defers its refresh', async () => {
const server: McpServerCRD = {
name: 'stable-dns-server',
namespace: 'mcp-server',
@@ -3927,6 +4056,13 @@ describe('NetworkPolicyReconciler', () => {
.map(call => (call[0] as { body: k8s.V1NetworkPolicy }).body)
.find(policy => policy.metadata?.labels?.['clerum.io/policy-type'] === 'external-egress')
expect(existing).toBeDefined()
+ // B3: identity-drift (protocol TCP→UDP) so this policy is revoked; a
+ // same-intent DNS allow is now retained. The revoke-before-readiness +
+ // defer-refresh machinery this test guards still fires on the drifted one.
+ existing!.spec!.egress = existing!.spec!.egress!.map(rule => ({
+ ...rule,
+ ports: rule.ports?.map(port => ({ ...port, protocol: 'UDP' })),
+ }))
vi.clearAllMocks()
let currentPolicies = [existing!]
@@ -3956,7 +4092,7 @@ describe('NetworkPolicyReconciler', () => {
expect(dns.resolve4).not.toHaveBeenCalled()
})
- it('does not certify safety when a same-intent DNS policy cannot be revoked', async () => {
+ it('does not certify safety when an identity-drifted DNS policy cannot be revoked', async () => {
const server: McpServerCRD = {
name: 'undeletable-dns-server',
namespace: 'mcp-server',
@@ -3973,6 +4109,13 @@ describe('NetworkPolicyReconciler', () => {
.map(call => (call[0] as { body: k8s.V1NetworkPolicy }).body)
.find(policy => policy.metadata?.labels?.['clerum.io/policy-type'] === 'external-egress')
expect(existing).toBeDefined()
+ // B3: identity-drift (protocol TCP→UDP) so this policy is revoked; a
+ // same-intent DNS allow is now retained. The no-certify-when-revoke-fails
+ // machinery this test guards still fires on the drifted one.
+ existing!.spec!.egress = existing!.spec!.egress!.map(rule => ({
+ ...rule,
+ ports: rule.ports?.map(port => ({ ...port, protocol: 'UDP' })),
+ }))
vi.clearAllMocks()
mockApi.listNamespacedNetworkPolicy.mockImplementation(
diff --git a/host-context-controller/src/networkPolicyReconciler.ts b/host-context-controller/src/networkPolicyReconciler.ts
index c824e90db..a4dc7ec59 100644
--- a/host-context-controller/src/networkPolicyReconciler.ts
+++ b/host-context-controller/src/networkPolicyReconciler.ts
@@ -347,6 +347,15 @@ export interface NetworkPolicyFullReconcileOptions {
* afterwards and must not delay this safety boundary.
*/
onAuthoritativeRevocationComplete?: () => void
+ /**
+ * Fired at the moment each external-egress allow owned by an McpServer is
+ * revoked (or replaced) during the authoritative safety pass — before any
+ * isCurrent abort or inventory-changed throw can unwind the pass. The owner
+ * uses it to queue an additive recreation for the affected server; queuing in
+ * a pass epilogue instead would reproduce exactly the abort gap this hook
+ * closes.
+ */
+ onExternalEgressRevoked?: (server: McpServerCRD) => void
}
export class NetworkPolicyReconciler {
@@ -1455,7 +1464,10 @@ export class NetworkPolicyReconciler {
policy => policy.metadata?.labels?.[MCPSERVER_LABEL] === server.name
),
serverEffectIsCurrent,
- recordSafetyPassRevocation
+ () => {
+ recordSafetyPassRevocation()
+ options.onExternalEgressRevoked?.(current)
+ }
)
if (!completed && serverCleanupAuthoritative()) {
liveDesiredRevisionChanged = true
@@ -1706,7 +1718,27 @@ export class NetworkPolicyReconciler {
}
desired.delete(name!)
if (!selected.policy) {
- unprovableDnsPolicies.push(existing)
+ // DNS binding: the /32s cannot be proven without a fresh lookup, but
+ // the binding IDENTITY (protocol, spec-level ports, selector,
+ // ownership) can. Retain the live allow when identity is unchanged;
+ // revoke only on an identity change (a port move in the CRD changes the
+ // policy NAME and is handled by the stale lane above). The comparison is
+ // "modulo cidr": the desired policy is rebuilt WITH the live cidrs, so
+ // the cidrs compare against themselves and everything else must match
+ // exactly. A policy with no ipBlock peers is a deny disguised as an
+ // allow and is never retained.
+ const existingCidrs = (existing.spec?.egress ?? [])
+ .flatMap(rule => rule.to ?? [])
+ .map(peer => peer.ipBlock?.cidr)
+ .filter((cidr): cidr is string => typeof cidr === 'string')
+ const identityUnchanged =
+ existingCidrs.length > 0 &&
+ hasExpectedPolicyOwnership(existing, EXTERNAL_EGRESS_POLICY_TYPE) &&
+ sameNetworkPolicySpec(
+ existing,
+ this.buildExactHostEgressPolicy(server, name!, selected.binding, existingCidrs)
+ )
+ if (!identityUnchanged) unprovableDnsPolicies.push(existing)
continue
}
const retainable =