Skip to content

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

TypeScript

test(guardrails): assert effective hook-pod egress, not one policy (n5)

Andy Pavia341e8aafdbc7
diff --git a/deploy/scripts/np-enforce-preflight.sh b/deploy/scripts/np-enforce-preflight.sh
index d4d98a6e8..55f273db2 100755
--- a/deploy/scripts/np-enforce-preflight.sh
+++ b/deploy/scripts/np-enforce-preflight.sh
@@ -12,12 +12,17 @@
# 2. allow-k8s-api-egress-* carries the endpoint IP in all 6 namespaces
# 3. DNS egress to kube-dns ClusterIP present in every clerum namespace
# 4. runtime-namespace pods with K8s-API RBAC carry the opt-in label (WARN)
+# 5. llm-hooks: no policy grants a sealed guardrail hook pod egress (N5)
#
# Usage:
# make gcp-prod-np-preflight # sets CONTEXT for you
# make gcp-dev-np-preflight
# CONTEXT=<kube-context> deploy/scripts/np-enforce-preflight.sh
#
+# PROBE_HOOK_DNS=1 additionally execs one `getent hosts` inside a sealed hook pod
+# (check 5) and expects it to FAIL. Off by default: every other check here reads
+# cluster state only, and that one runs a command in a workload.
+#
# CONTEXT MUST be passed explicitly — the script never inherits the current
# kubectl context (per CLAUDE.md: the active context is not load-bearing).
@@ -40,6 +45,9 @@ K8S_API_NAMESPACES="channels control-plane mcp-host mcp-server rpc-proxy sandbox
ALL_NAMESPACES="channels control-plane ingress mcp-host mcp-server profiles rpc-proxy sandbox-recipes sandbox-ui webhook-ingress"
# HCC runtime namespaces — allow-k8s-api-egress-* is opt-in here (PR #314).
RUNTIME_NAMESPACES="mcp-server rpc-proxy sandbox-recipes sandbox-ui"
+# Guardrail hook workloads. Deliberately NOT a runtime namespace: that list also
+# emits namespace-wide DNS, which N5 forbids for a hook declaring no egress.
+HOOKS_NS="${HOOKS_NS:-llm-hooks}"
blockers=0
warns=0
@@ -280,6 +288,80 @@ if [ "$flagged" -eq 0 ]; then
fi
echo
+# ── Check 5: llm-hooks sealed-hook egress (N5) ──────────────────────────────
+# A guardrail hook that declares no egressBindings must reach NOTHING — not even
+# DNS (deploy/base/llm-hooks/networkpolicies.yaml). Two ways that breaks:
+# - a namespace-wide Egress policy. Listing llm-hooks in
+# CONTEXT_MAPPER_RUNTIME_NAMESPACES makes HCC emit allow-dns-egress-llm-hooks
+# with podSelector: {}, which selects every pod in the namespace.
+# - an Egress policy selecting clerum.io/managed-by=host-context-controller,
+# the label every hook pod template carries.
+# `llmhook-<podKey>` policies are the per-hook ones: egress there is the DECLARED
+# kind, scoped by the reconciler to that hook's own bindings, so it is expected.
+echo "[5] llm-hooks: no policy grants a sealed hook pod egress (N5)"
+if ! kq get namespace "$HOOKS_NS" >/dev/null 2>&1; then
+ pass "namespace $HOOKS_NS not present — nothing to check"
+else
+ hook_findings=0
+ # `egress` is read LAST so a rule containing '|' cannot shift the earlier fields.
+ # policyTypes alone is not a grant: the baseline default-deny carries
+ # policyTypes [Ingress, Egress] with NO egress rules, which is what denies all
+ # outbound. Only a non-empty egress list actually opens anything.
+ while IFS='|' read -r np ptypes selector egress; do
+ [ -z "$np" ] && continue
+ case "$ptypes" in *Egress*) ;; *) continue ;; esac
+ if [ -z "$egress" ] || [ "$egress" = "[]" ]; then continue; fi
+ case "$np" in llmhook-*) continue ;; esac
+ if [ -z "$selector" ] || [ "$selector" = "{}" ]; then
+ bad "$HOOKS_NS/$np grants Egress to EVERY pod in the namespace (podSelector: {})"
+ hook_findings=$((hook_findings + 1))
+ elif printf '%s' "$selector" | grep -q 'host-context-controller'; then
+ bad "$HOOKS_NS/$np grants Egress via clerum.io/managed-by — that selects hook pods"
+ hook_findings=$((hook_findings + 1))
+ else
+ warn "$HOOKS_NS/$np grants Egress via $selector — confirm it selects no hook pod"
+ hook_findings=$((hook_findings + 1))
+ fi
+ done <<EOF
+$(kq -n "$HOOKS_NS" get networkpolicies -o jsonpath='{range .items[*]}{.metadata.name}|{.spec.policyTypes}|{.spec.podSelector.matchLabels}|{.spec.egress}{"\n"}{end}' 2>/dev/null)
+EOF
+ if [ "$hook_findings" -eq 0 ]; then
+ pass "no namespace-wide or managed-by Egress policy in $HOOKS_NS"
+ fi
+
+ # Static policy inspection cannot see a policy applied out of band, nor a CNI
+ # that fails to enforce one. The probe can: resolve from inside a hook whose
+ # own policy declares no Egress and expect it to hang/fail.
+ if [ "${PROBE_HOOK_DNS:-0}" = "1" ]; then
+ probed=0
+ while IFS='|' read -r pod podkey; do
+ if [ -z "$pod" ] || [ -z "$podkey" ]; then continue; fi
+ PTYPES="$(kq -n "$HOOKS_NS" get networkpolicy "llmhook-$podkey" \
+ -o jsonpath='{.spec.policyTypes}' 2>/dev/null)"
+ case "$PTYPES" in *Egress*) continue ;; esac
+ # A blocked lookup hangs until `timeout` kills it, which surfaces as a
+ # non-zero exit (143) plus kubectl noise on stderr — both expected here.
+ if kq -n "$HOOKS_NS" exec "$pod" -- sh -c \
+ 'timeout 5 getent hosts kubernetes.default.svc.cluster.local >/dev/null 2>&1' \
+ 2>/dev/null; then
+ bad "$HOOKS_NS/$pod resolved DNS — a hook declaring no egress reached kube-dns"
+ else
+ pass "$HOOKS_NS/$pod cannot resolve DNS (sealed as designed)"
+ fi
+ probed=1
+ break
+ done <<EOF
+$(kq -n "$HOOKS_NS" get pods -o jsonpath='{range .items[*]}{.metadata.name}|{.metadata.labels.clerum\.io/hook-pod-key}{"\n"}{end}' 2>/dev/null)
+EOF
+ if [ "$probed" -eq 0 ]; then
+ printf ' ---- no sealed hook pod to probe (none running, or all declare egress)\n'
+ fi
+ else
+ printf ' ---- DNS probe skipped (PROBE_HOOK_DNS=1 execs a resolve test in a hook pod)\n'
+ fi
+fi
+echo
+
# ── Summary ─────────────────────────────────────────────────────────────────
echo "─────────────────────────────────────────────"
if [ "$blockers" -ne 0 ]; then
diff --git a/host-context-controller/package-lock.json b/host-context-controller/package-lock.json
index 0445983f3..d9d0e6cc4 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.176",
+ "version": "0.1.177",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-host-context-controller",
- "version": "0.1.176",
+ "version": "0.1.177",
"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 50c3259a2..e1d8d1077 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.176",
+ "version": "0.1.177",
"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/__tests__/llmHooksEffectiveEgress.test.ts b/host-context-controller/src/__tests__/llmHooksEffectiveEgress.test.ts
new file mode 100644
index 000000000..d007b146d
--- /dev/null
+++ b/host-context-controller/src/__tests__/llmHooksEffectiveEgress.test.ts
@@ -0,0 +1,200 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest'
+import * as k8s from '@kubernetes/client-node'
+import {
+ type MockAppsApi,
+ type MockCoreApi,
+ type MockCustomApi,
+ type MockNetworkingApi,
+ asAppsApi,
+ asCoreApi,
+ asCustomApi,
+ asNetworkingApi,
+ createMockAppsApi,
+ createMockCoreApi,
+ createMockCustomApi,
+ createMockNetworkingApi,
+} from '../../test/__fixtures__/testMocks'
+import { config } from '../config'
+import { HOOK_PODKEY_LABEL, MANAGED_BY_LABEL, MANAGED_BY_VALUE } from '../constants'
+import { LlmHookReconciler, computePodKey, podKeyResourceName } from '../llmHookReconciler'
+import { NetworkPolicyReconciler } from '../networkPolicyReconciler'
+import type { HostCRD, LlmHookCRD } from '../types'
+
+/**
+ * N5 ("no implicit DNS") asserted at the level the invariant is actually stated:
+ * a pod's EFFECTIVE egress, which is the union of every NetworkPolicy selecting
+ * it — not one policy in isolation. `llmHookReconciler.test.ts` inspects the
+ * per-pod-key policy alone, so it cannot see a namespace-wide policy handing the
+ * same pod DNS. That blind spot is exactly how llm-hooks in `runtimeNamespaces`
+ * granted every hook pod kube-system:53 while that test stayed green.
+ *
+ * Deliberately runs against the REAL config module: the point is the shipped
+ * default, so re-adding llm-hooks to `runtimeNamespaces` (or any future infra
+ * policy selecting hook pods) must fail here.
+ */
+
+const IMG = 'registry.example.com/hook@sha256:' + 'a'.repeat(64)
+
+function makeHook(name: string, spec: Partial<LlmHookCRD['spec']> = {}): LlmHookCRD {
+ return {
+ name,
+ namespace: config.llmHooksNamespace,
+ generation: 1,
+ spec: {
+ target: { image: { ref: IMG, port: 8080 } },
+ path: '/',
+ lifecyclePoints: ['preCall'],
+ ...spec,
+ },
+ }
+}
+
+function makeHostRef(name: string, hookIds: string[]): HostCRD {
+ return {
+ name,
+ namespace: config.hostNamespace,
+ spec: {
+ host: name,
+ contextRef: 'ctx',
+ secretRef: 'secret',
+ guardrails: { hooks: { preCall: hookIds.map(id => ({ id })) } },
+ },
+ }
+}
+
+/** The labels HCC stamps on a hook pod template (llmHookReconciler.podKeyLabels). */
+function hookPodLabels(hook: LlmHookCRD): Record<string, string> {
+ const podKey = computePodKey(hook)!
+ return {
+ app: podKeyResourceName(podKey),
+ [MANAGED_BY_LABEL]: MANAGED_BY_VALUE,
+ [HOOK_PODKEY_LABEL]: podKey,
+ }
+}
+
+/**
+ * Does `selector` select a pod carrying `labels`? An empty selector selects every
+ * pod in the namespace — the case that matters most here. matchExpressions is
+ * unsupported on purpose: silently treating it as "no match" would under-report
+ * reachable egress, so it throws instead.
+ */
+function selects(
+ selector: k8s.V1LabelSelector | undefined,
+ labels: Record<string, string>
+): boolean {
+ if (selector?.matchExpressions?.length) {
+ throw new Error('matchExpressions selector is not modelled — extend selects() before using it')
+ }
+ const matchLabels = selector?.matchLabels
+ if (!matchLabels || Object.keys(matchLabels).length === 0) return true
+ return Object.entries(matchLabels).every(([k, v]) => labels[k] === v)
+}
+
+describe('llm-hooks effective pod egress (N5)', () => {
+ let appsApi: MockAppsApi
+ let coreApi: MockCoreApi
+ let customApi: MockCustomApi
+ let networkingApi: MockNetworkingApi
+ let hooks: Map<string, LlmHookCRD>
+ let hosts: Map<string, HostCRD>
+
+ /** Every policy written into the llm-hooks namespace, by either reconciler. */
+ function policiesInLlmHooksNamespace(): k8s.V1NetworkPolicy[] {
+ const written = [
+ ...networkingApi.createNamespacedNetworkPolicy.mock.calls,
+ ...networkingApi.replaceNamespacedNetworkPolicy.mock.calls,
+ ] as Array<[{ namespace?: string; body?: k8s.V1NetworkPolicy }]>
+ return written
+ .map(([arg]) => arg)
+ .filter(arg => arg?.namespace === config.llmHooksNamespace && arg.body)
+ .map(arg => arg.body!)
+ }
+
+ /** Policies from that set which actually select the given pod. */
+ function policiesSelecting(labels: Record<string, string>): k8s.V1NetworkPolicy[] {
+ return policiesInLlmHooksNamespace().filter(p => selects(p.spec?.podSelector, labels))
+ }
+
+ /** The egress rules a pod can actually use: the union across selecting policies. */
+ function effectiveEgress(labels: Record<string, string>): k8s.V1NetworkPolicyEgressRule[] {
+ return policiesSelecting(labels).flatMap(p =>
+ p.spec?.policyTypes?.includes('Egress') ? (p.spec?.egress ?? []) : []
+ )
+ }
+
+ async function reconcileAll(hook: LlmHookCRD): Promise<void> {
+ const hookReconciler = new LlmHookReconciler({} as k8s.KubeConfig, hooks, hosts, {
+ appsApi: asAppsApi(appsApi),
+ coreApi: asCoreApi(coreApi),
+ customApi: asCustomApi(customApi),
+ networkingApi: asNetworkingApi(networkingApi),
+ })
+ const kc = {
+ makeApiClient: (type: unknown) =>
+ type === k8s.NetworkingV1Api ? asNetworkingApi(networkingApi) : asCustomApi(customApi),
+ } as unknown as k8s.KubeConfig
+ const netPolReconciler = new NetworkPolicyReconciler(kc, new Map())
+
+ await hookReconciler.reconcile(hook)
+ await netPolReconciler.ensureDefaultPolicies()
+ }
+
+ beforeEach(() => {
+ vi.clearAllMocks()
+ appsApi = createMockAppsApi()
+ coreApi = createMockCoreApi()
+ customApi = createMockCustomApi()
+ networkingApi = createMockNetworkingApi()
+ customApi.getNamespacedCustomObjectStatus.mockResolvedValue({
+ metadata: { resourceVersion: '1' },
+ status: { conditions: [] },
+ })
+ hooks = new Map()
+ hosts = new Map()
+ })
+
+ it('leaves a pure responder pod with no egress at all — not even DNS', async () => {
+ const responder = makeHook('resp')
+ hooks.set('resp', responder)
+ hosts.set('host-1', makeHostRef('host-1', ['resp']))
+
+ await reconcileAll(responder)
+
+ expect(effectiveEgress(hookPodLabels(responder))).toEqual([])
+ })
+
+ it('names no namespace-wide egress policy in llm-hooks', async () => {
+ const responder = makeHook('resp')
+ hooks.set('resp', responder)
+
+ await reconcileAll(responder)
+
+ // The failure mode this guards: allow-dns-egress-<ns> / allow-hcc-api-egress-<ns>
+ // appear the moment llm-hooks is listed in runtimeNamespaces.
+ const egressPolicyNames = policiesInLlmHooksNamespace()
+ .filter(p => p.spec?.policyTypes?.includes('Egress'))
+ .map(p => p.metadata?.name)
+ expect(egressPolicyNames).not.toContain(`allow-dns-egress-${config.llmHooksNamespace}`)
+ expect(egressPolicyNames).not.toContain(`allow-hcc-api-egress-${config.llmHooksNamespace}`)
+ })
+
+ // Positive control: without this, the two assertions above would also pass if
+ // effectiveEgress() were simply blind to egress rules.
+ it('does surface the egress a declaring hook is granted (scoped DNS + its target)', async () => {
+ const dialer = makeHook('dialer', {
+ target: {
+ image: { ref: IMG, port: 8080, egressBindings: [{ cidr: '8.8.8.8/32', ports: [443] }] },
+ },
+ })
+ hooks.set('dialer', dialer)
+
+ await reconcileAll(dialer)
+
+ const egress = effectiveEgress(hookPodLabels(dialer))
+ expect(egress).not.toEqual([])
+ // its declared target …
+ expect(egress.some(r => r.to?.some(t => t.ipBlock?.cidr === '8.8.8.8/32'))).toBe(true)
+ // … plus the scoped CoreDNS allow that resolving it requires.
+ expect(egress.some(r => r.ports?.some(p => p.port === 53))).toBe(true)
+ })
+})
diff --git a/host-context-controller/src/llmHookReconciler.test.ts b/host-context-controller/src/llmHookReconciler.test.ts
index b148814bc..09fd93068 100644
--- a/host-context-controller/src/llmHookReconciler.test.ts
+++ b/host-context-controller/src/llmHookReconciler.test.ts
@@ -811,7 +811,12 @@ describe('LlmHookReconciler', () => {
expect(dns?.ports?.map(p => p.port)).toEqual([53, 53])
})
- it('grants NO egress (not even DNS) to a pure responder hook (no egressBindings)', async () => {
+ // Scoped to the per-pod-key policy this file owns. It deliberately does NOT
+ // claim the pod has no egress — that is the union of every policy selecting it,
+ // asserted in __tests__/llmHooksEffectiveEgress.test.ts. Titling this one
+ // "grants NO egress (not even DNS)" is what let namespace-wide DNS through
+ // while the suite stayed green.
+ it('declares no Egress in the per-pod-key policy for a pure responder (no egressBindings)', async () => {
const responder = makeHook({ name: 'resp' }) // default image, no egressBindings
hooks.set('resp', responder)
await reconciler.reconcile(responder)