diff --git a/charts/zelkor-agent/templates/_helpers.tpl b/charts/zelkor-agent/templates/_helpers.tpl index 7787dd96..d0107aed 100644 --- a/charts/zelkor-agent/templates/_helpers.tpl +++ b/charts/zelkor-agent/templates/_helpers.tpl @@ -423,10 +423,12 @@ else inherit {releaseName}-ai-gateway-consumer (zelkor deploy copies it into oth {{- end }} {{/* -CE-21: "true" when the pod runs without DNS: lockdown is on and something fills its hostAliases -(platform.hostAliases from `zelkor deploy`, or the platform admission policy on K8s >= 1.36). -"inactive" when lockdown is on but neither can: the pod keeps DNS and is marked for -`zelkor verify` (plain Helm / GitOps installs on older clusters must not break). "false" when off. +CE-21 lockdown mode for this pod: +"map" the platform admission policy (K8s >= 1.36) locks it at create: the pod asks with + dns-lockdown=requested and keeps DNS until the policy flips the label to "on". +"true" older clusters: `zelkor deploy` supplied platform.hostAliases; the chart locks it. +"inactive" lockdown on, but nothing can fill its hostAliases: DNS stays, marked for `zelkor verify`. +"false" off. */}} {{- define "zelkor-agent.dnsLockdown" -}} {{- $on := (((.Values.security).dnsLockdown).enabled) | default false -}} @@ -434,7 +436,9 @@ CE-21: "true" when the pod runs without DNS: lockdown is on and something fills {{- $map := and (.Capabilities.APIVersions.Has "admissionregistration.k8s.io/v1/MutatingAdmissionPolicy") (include "zelkor-agent.platformReleaseName" .) -}} {{- if not $on -}} false -{{- else if or $aliases $map -}} +{{- else if $map -}} +map +{{- else if $aliases -}} true {{- else -}} inactive diff --git a/charts/zelkor-agent/templates/deployment.yaml b/charts/zelkor-agent/templates/deployment.yaml index 15a4b920..61bbebda 100644 --- a/charts/zelkor-agent/templates/deployment.yaml +++ b/charts/zelkor-agent/templates/deployment.yaml @@ -57,7 +57,10 @@ spec: {{- with (include "zelkor-agent.platformReleaseName" .) }} zelkor.io/platform-release: {{ . | quote }} {{- end }} - {{- if eq (include "zelkor-agent.dnsLockdown" .) "true" }} + {{- if eq (include "zelkor-agent.dnsLockdown" .) "map" }} + # CE-21: the platform admission policy locks the pod and flips this to "on" atomically. + zelkor.io/dns-lockdown: requested + {{- else if eq (include "zelkor-agent.dnsLockdown" .) "true" }} zelkor.io/dns-lockdown: "on" {{- end }} spec: diff --git a/charts/zelkor-agent/templates/networkpolicies.yaml b/charts/zelkor-agent/templates/networkpolicies.yaml index 4d788a59..ebd92063 100644 --- a/charts/zelkor-agent/templates/networkpolicies.yaml +++ b/charts/zelkor-agent/templates/networkpolicies.yaml @@ -18,7 +18,7 @@ spec: policyTypes: - Egress egress: - {{- if ne (include "zelkor-agent.dnsLockdown" .) "true" }} + {{- if eq (include "zelkor-agent.dnsLockdown" .) "false" }} - to: - namespaceSelector: matchLabels: @@ -99,6 +99,38 @@ spec: - protocol: TCP port: 6379 {{- end }} +{{- if ne (include "zelkor-agent.dnsLockdown" .) "false" }} +--- +# CE-21: DNS only while this pod is not locked (requested, inactive). The admission policy or the +# chart sets dns-lockdown=on together with dnsPolicy None and hostAliases. +apiVersion: networking.k8s.io/v1 +kind: NetworkPolicy +metadata: + name: {{ $fullname }}-egress-dns + labels: + {{- include "zelkor-agent.labels" . | nindent 4 }} + zelkor.io/intent: security.dnsLockdown +spec: + podSelector: + matchLabels: + {{- include "zelkor-agent.selectorLabels" . | nindent 6 }} + matchExpressions: + - key: zelkor.io/dns-lockdown + operator: NotIn + values: ["on"] + policyTypes: + - Egress + egress: + - to: + - namespaceSelector: + matchLabels: + kubernetes.io/metadata.name: kube-system + ports: + - protocol: UDP + port: 53 + - protocol: TCP + port: 53 +{{- end }} --- apiVersion: networking.k8s.io/v1 kind: NetworkPolicy diff --git a/charts/zelkor-platform/templates/_helpers.tpl b/charts/zelkor-platform/templates/_helpers.tpl index b75490b5..f81dade9 100644 --- a/charts/zelkor-platform/templates/_helpers.tpl +++ b/charts/zelkor-platform/templates/_helpers.tpl @@ -1786,3 +1786,65 @@ key carries no security.dnsLockdown, and the lockdown must still default on. {{- define "zelkor-platform.dnsLockdownWriterDeadline" -}} {{- (((.Values.security | default dict).dnsLockdown | default dict).writerDeadlineSeconds | default 900) | int -}} {{- end }} + +{{/* +CE-21 second slice: how the platform Aegra gets its hostAliases. +"map" K8s >= 1.36: the admission policy injects them at pod create. +"lookup" older clusters: the host map already exists (Helm lookup at upgrade time). +"inactive" lockdown on, but no policy and no map yet (first install): Aegra keeps DNS. +"false" lockdown off, or security.dnsLockdown.aegra=false. +*/}} +{{- define "zelkor-platform.aegraDnsLockdown" -}} +{{- $dl := (.Values.security | default dict).dnsLockdown | default dict -}} +{{- $aegraOn := ternary true (eq (toString $dl.aegra) "true") (not (hasKey $dl "aegra")) -}} +{{- if or (ne (include "zelkor-platform.dnsLockdownEnabled" .) "true") (not $aegraOn) -}} +false +{{- else if .Capabilities.APIVersions.Has "admissionregistration.k8s.io/v1/MutatingAdmissionPolicy" -}} +map +{{- else if eq (include "zelkor-platform.aegraHostsComplete" .) "true" -}} +lookup +{{- else -}} +inactive +{{- end -}} +{{- end }} + +{{/* The live host map as {"data": {...}} (empty under `helm template`). */}} +{{- define "zelkor-platform.agentHostsLookup" -}} +{{- $cm := lookup "v1" "ConfigMap" .Release.Namespace (printf "%s-agent-hosts" (include "zelkor-platform.fullname" .)) -}} +{{- dict "data" (($cm | default dict).data | default dict) | toJson -}} +{{- end }} + +{{/* hostAliases from the live host map, one entry per IP (older clusters, platform Aegra). */}} +{{- define "zelkor-platform.agentHostAliases" -}} +{{- $data := (include "zelkor-platform.agentHostsLookup" . | fromJson).data -}} +{{- $byIp := dict -}} +{{- range $host, $ip := $data -}} +{{- $_ := set $byIp $ip (append (get $byIp $ip | default list) $host) -}} +{{- end -}} +{{- range $ip := keys $byIp | sortAlpha }} +- ip: {{ $ip | quote }} + hostnames: + {{- range (get $byIp $ip | sortAlpha) }} + - {{ . | quote }} + {{- end }} +{{- end -}} +{{- end }} + +{{/* +"true" when the live host map already holds every platform name the Aegra pod calls. A map written +by an older revision can lack a Service this revision added (qdrant, in 2.6.0); locking Aegra on it +would cut that name until the next upgrade, so Aegra stays inactive for one more round instead. +*/}} +{{- define "zelkor-platform.aegraHostsComplete" -}} +{{- $data := (include "zelkor-platform.agentHostsLookup" . | fromJson).data -}} +{{- $fn := include "zelkor-platform.fullname" . -}} +{{- $need := list -}} +{{- if and .Values.aiGateway.enabled .Values.aiGateway.inClusterService.enabled -}}{{- $need = append $need (printf "%s-ai-gateway" $fn) -}}{{- end -}} +{{- if eq (include "zelkor-platform.mcpMcprouteEnabled" .) "true" -}}{{- $need = append $need (printf "%s-mcp" $fn) -}}{{- end -}} +{{- if (.Values.postgresql).enabled -}}{{- $need = append $need (printf "%s-postgresql" $fn) -}}{{- end -}} +{{- if (.Values.valkey).enabled -}}{{- $need = append $need (printf "%s-valkey" $fn) -}}{{- end -}} +{{- if (.Values.qdrant).enabled -}}{{- $need = append $need (printf "%s-qdrant" $fn) -}}{{- end -}} +{{- $ok := and $data true -}} +{{- range $need -}}{{- if not (hasKey $data .) -}}{{- $ok = false -}}{{- end -}}{{- end -}} +{{- ternary "true" "false" (and $ok true) -}} +{{- end }} diff --git a/charts/zelkor-platform/templates/aegra/deployment.yaml b/charts/zelkor-platform/templates/aegra/deployment.yaml index f2438b66..8eaa3d8e 100644 --- a/charts/zelkor-platform/templates/aegra/deployment.yaml +++ b/charts/zelkor-platform/templates/aegra/deployment.yaml @@ -35,12 +35,33 @@ spec: app.kubernetes.io/component: aegra template: metadata: + {{- $dnsLock := include "zelkor-platform.aegraDnsLockdown" . }} annotations: checksum/ai-gateway-agents: {{ include "zelkor-platform.aiGatewayKeyChecksum" (dict "root" . "key" "agents") | quote }} + {{- if eq $dnsLock "inactive" }} + # CE-21: no admission policy and no host map yet (first install); DNS stays until the next upgrade. + zelkor.io/dns-lockdown: inactive + {{- end }} labels: {{- include "zelkor-platform.selectorLabels" . | nindent 8 }} app.kubernetes.io/component: aegra + {{- if eq $dnsLock "map" }} + # CE-21: the admission policy locks the pod and flips this to "on" atomically. + zelkor.io/dns-lockdown: requested + zelkor.io/platform-release: {{ .Release.Name | quote }} + {{- else if eq $dnsLock "lookup" }} + zelkor.io/dns-lockdown: "on" + zelkor.io/platform-release: {{ .Release.Name | quote }} + {{- end }} spec: + {{- if eq $dnsLock "lookup" }} + # CE-21: no DNS. Names resolve from /etc/hosts; nothing answers on 127.0.0.1:53. + dnsPolicy: None + dnsConfig: + nameservers: ["127.0.0.1"] + hostAliases: + {{- include "zelkor-platform.agentHostAliases" . | nindent 8 }} + {{- end }} {{- include "zelkor-platform.imagePullSecrets" . | nindent 6 }} terminationGracePeriodSeconds: {{ .Values.aegra.terminationGracePeriodSeconds | default 35 }} initContainers: diff --git a/charts/zelkor-platform/templates/security/agent-hosts.yaml b/charts/zelkor-platform/templates/security/agent-hosts.yaml index 107f2cab..dd0abedb 100644 --- a/charts/zelkor-platform/templates/security/agent-hosts.yaml +++ b/charts/zelkor-platform/templates/security/agent-hosts.yaml @@ -9,7 +9,7 @@ {{- $dataplane := include "zelkor-platform.envoyDataplaneHost" . | trim }} {{- $dpNs := "" }} {{- if $dataplane }}{{- $dpNs = index (splitList "." $dataplane) 1 }}{{- end }} -{{- $services := list (printf "%s-ai-gateway" $fullname) (printf "%s-mcp" $fullname) (printf "%s-otlp" $fullname) (printf "%s-postgresql" $fullname) (printf "%s-langfuse" $fullname) (printf "%s-valkey" $fullname) }} +{{- $services := list (printf "%s-ai-gateway" $fullname) (printf "%s-mcp" $fullname) (printf "%s-otlp" $fullname) (printf "%s-postgresql" $fullname) (printf "%s-langfuse" $fullname) (printf "%s-valkey" $fullname) (printf "%s-qdrant" $fullname) }} {{- range (.Values.__httpApis | default "[]" | fromJsonArray) }} {{- $services = append $services (printf "%s-api-%s" $fullname .name) }} {{- end }} @@ -142,7 +142,8 @@ spec: limits: {cpu: 200m, memory: 128Mi} {{- if .Capabilities.APIVersions.Has "admissionregistration.k8s.io/v1/MutatingAdmissionPolicy" }} --- -# K8s >= 1.36: every new agent pod of this platform gets no DNS and the current host map. +# K8s >= 1.36: every new locked pod of this platform (agents, platform Aegra) gets no DNS and +# the current host map. # One JSONPatch per entry: a CEL-built list of objects panics the 1.36.4 API server. apiVersion: admissionregistration.k8s.io/v1 kind: MutatingAdmissionPolicy @@ -160,13 +161,12 @@ spec: apiVersions: [v1] operations: [CREATE] resources: [pods] + # Opt-in by label: pods ask with dns-lockdown=requested; this policy locks them and flips the + # label to "on" in the same mutation. A pod it misses keeps DNS and stays "requested". objectSelector: matchLabels: zelkor.io/platform-release: {{ .Release.Name | quote }} - matchExpressions: - - key: zelkor.io/workload-type - operator: In - values: [agent, app] + zelkor.io/dns-lockdown: requested failurePolicy: Fail reinvocationPolicy: IfNeeded mutations: @@ -177,7 +177,9 @@ spec: JSONPatch{op: "add", path: "/spec/dnsPolicy", value: "None"}, JSONPatch{op: "add", path: "/spec/dnsConfig", value: Object.spec.dnsConfig{nameservers: ["127.0.0.1"]}}, - JSONPatch{op: "add", path: "/spec/hostAliases", value: []} + JSONPatch{op: "add", path: "/spec/hostAliases", value: []}, + JSONPatch{op: "replace", path: "/metadata/labels/" + jsonpatch.escapeKey("zelkor.io/dns-lockdown"), + value: "on"} ] + params.data.map(h, JSONPatch{op: "add", path: "/spec/hostAliases/-", value: Object.spec.hostAliases{ip: params.data[h], hostnames: [h]}}) --- diff --git a/charts/zelkor-platform/values.schema.json b/charts/zelkor-platform/values.schema.json index 2d7777ab..2540bad7 100644 --- a/charts/zelkor-platform/values.schema.json +++ b/charts/zelkor-platform/values.schema.json @@ -4150,6 +4150,10 @@ "type": "boolean", "description": "Value for security.dnsLockdown.enabled." }, + "aegra": { + "type": "boolean", + "description": "Value for security.dnsLockdown.aegra." + }, "writerDeadlineSeconds": { "type": [ "integer", diff --git a/charts/zelkor-platform/values.yaml b/charts/zelkor-platform/values.yaml index 1f36bd8f..3a96f94d 100644 --- a/charts/zelkor-platform/values.yaml +++ b/charts/zelkor-platform/values.yaml @@ -715,6 +715,9 @@ security: # writes the map; agents get it from the admission policy (K8s >= 1.36) or `zelkor deploy`. dnsLockdown: enabled: true + # The platform Aegra too (locked from the first upgrade on clusters without the admission + # API). Turn off only if platform graphs must resolve names outside the platform. + aegra: true writerDeadlineSeconds: 900 mTLS: enabled: false diff --git a/cli/src/zelkor/hosts_check.py b/cli/src/zelkor/hosts_check.py index d38309c8..9d7514e8 100644 --- a/cli/src/zelkor/hosts_check.py +++ b/cli/src/zelkor/hosts_check.py @@ -2,7 +2,7 @@ Checks, each printed as PASS / FAIL / WARN: 1. every host-map entry still points at the live ClusterIP (ExternalName followed); - 2. every running agent pod of this platform carries exactly the map's entries; + 2. every running locked pod of this platform (agents, platform Aegra) carries exactly the map's entries; 3. every HTTP API an agent's sidecar relays to exists on the platform (no stale route names); 4. agents left with DNS (lockdown inactive) are reported. """ @@ -94,14 +94,23 @@ def check_agent_hosts(release: str, namespace: str, *, kubectl: list[str], runne + ("" if ok else f" (live: {live or 'missing'}; upgrade the platform to rewrite the map)")) apis = {n.strip() for n in contract_data.get("HTTP_APIS", "").split(",") if n.strip()} - pods = _json(runner, kubectl + ["get", "pods", "-A", "-l", "zelkor.io/workload-type=agent", "-o", "json"]) or {} + # Agents (any namespace) and this platform's own Aegra; anything else is not ours to judge. + pods = _json(runner, kubectl + ["get", "pods", "-A", "-o", "json"]) or {} for pod in pods.get("items") or []: meta = pod.get("metadata") or {} where = f"{meta.get('namespace')}/{meta.get('name')}" labels = meta.get("labels") or {} + is_agent = labels.get("zelkor.io/workload-type") == "agent" + is_platform_aegra = (meta.get("namespace") == namespace + and labels.get("app.kubernetes.io/instance") == release + and labels.get("app.kubernetes.io/component") == "aegra") + if not (is_agent or is_platform_aegra): + continue if labels.get("zelkor.io/platform-release", release) != release: continue - if (meta.get("annotations") or {}).get("zelkor.io/dns-lockdown") == "inactive": + if labels.get("zelkor.io/dns-lockdown") == "requested": + print(f"WARN dns {where} asked for lockdown but the admission policy did not lock it; delete the pod") + elif (meta.get("annotations") or {}).get("zelkor.io/dns-lockdown") == "inactive": print(f"WARN dns {where} keeps DNS (lockdown inactive); redeploy with `zelkor deploy`") elif labels.get("zelkor.io/dns-lockdown") == "on": have = {_short(h, namespace): ip for h, ip in _pod_aliases(pod).items()} diff --git a/tests/test_helm_agent_dns_lockdown.py b/tests/test_helm_agent_dns_lockdown.py index b642ff77..bb107b1e 100644 --- a/tests/test_helm_agent_dns_lockdown.py +++ b/tests/test_helm_agent_dns_lockdown.py @@ -42,14 +42,24 @@ def test_host_aliases_from_zelkor_deploy_lock_the_pod(): assert pod["metadata"]["labels"]["zelkor.io/dns-lockdown"] == "on" assert pod["metadata"]["labels"]["zelkor.io/platform-release"] == "zelkor-platform" assert not _egress_allows_dns(docs) + # Locked by the chart: the label is "on", so the label-keyed DNS policy no longer selects it. + assert _dns_policy(docs)["spec"]["podSelector"]["matchExpressions"][0]["values"] == ["on"] -def test_admission_policy_cluster_locks_without_host_aliases(): - docs = _docs("--api-versions", MAP_API) +def _dns_policy(docs: list[dict]) -> dict: + return next(d for d in docs if d["kind"] == "NetworkPolicy" and d["metadata"]["name"].endswith("-egress-dns")) + + +def test_admission_policy_cluster_asks_and_keeps_dns_until_locked(): + """The policy sets dnsPolicy, hostAliases and label "on" together; until then DNS works.""" + docs = _docs("--api-versions", MAP_API, *ALIASES) pod = _pod(docs) - assert pod["spec"]["dnsPolicy"] == "None" and "hostAliases" not in pod["spec"] - assert pod["metadata"]["labels"]["zelkor.io/dns-lockdown"] == "on" + assert "dnsPolicy" not in pod["spec"] and "hostAliases" not in pod["spec"] + assert pod["metadata"]["labels"]["zelkor.io/dns-lockdown"] == "requested" assert not _egress_allows_dns(docs) + dns = _dns_policy(docs) + assert dns["spec"]["podSelector"]["matchExpressions"] == [ + {"key": "zelkor.io/dns-lockdown", "operator": "NotIn", "values": ["on"]}] def test_plain_helm_on_an_older_cluster_keeps_dns_and_is_marked(): @@ -58,7 +68,8 @@ def test_plain_helm_on_an_older_cluster_keeps_dns_and_is_marked(): assert "dnsPolicy" not in pod["spec"] assert "zelkor.io/dns-lockdown" not in pod["metadata"]["labels"] assert pod["metadata"]["annotations"]["zelkor.io/dns-lockdown"] == "inactive" - assert _egress_allows_dns(docs) + assert not _egress_allows_dns(docs) + assert _dns_policy(docs) # inactive pods keep DNS through the label-keyed policy def test_lockdown_off_keeps_dns_unmarked(): diff --git a/tests/test_helm_dns_lockdown.py b/tests/test_helm_dns_lockdown.py index 7d6a9a77..6c72852d 100644 --- a/tests/test_helm_dns_lockdown.py +++ b/tests/test_helm_dns_lockdown.py @@ -99,12 +99,15 @@ def test_admission_policy_only_when_the_api_is_served(): (policy,) = _kind(docs, "MutatingAdmissionPolicy") (binding,) = _kind(docs, "MutatingAdmissionPolicyBinding") selector = policy["spec"]["matchConstraints"]["objectSelector"] - assert selector["matchLabels"] == {"zelkor.io/platform-release": "zelkor-platform"} + assert selector["matchLabels"] == {"zelkor.io/platform-release": "zelkor-platform", + "zelkor.io/dns-lockdown": "requested"} assert binding["spec"]["paramRef"] == {"name": "zelkor-platform-agent-hosts", "namespace": "zelkor", "parameterNotFoundAction": "Deny"} expr = policy["spec"]["mutations"][0]["jsonPatch"]["expression"] # One patch per entry; a CEL-built list of objects panics the 1.36.4 API server. assert '"/spec/hostAliases/-"' in expr and "split(" not in expr + # The label flips to "on" in the same mutation, so a locked pod is never half-locked. + assert 'escapeKey("zelkor.io/dns-lockdown")' in expr and 'value: "on"' in expr def test_lockdown_off_keeps_dns_and_renders_no_writer(): @@ -119,3 +122,32 @@ def test_reuse_values_upgrade_without_the_key_still_locks_down(): docs = _docs("--set", "security.dnsLockdown=null") assert _writer_env(docs)["AGENT_HOSTS_DEADLINE_S"] == "900" assert not _allows_dns(_agent_egress(docs)) + + +def _aegra_pod_template(docs: list[dict]) -> dict: + return next(d for d in _kind(docs, "Deployment") if d["metadata"]["name"] == "zelkor-platform-aegra")["spec"]["template"] + + +def test_platform_aegra_asks_the_admission_policy_on_136(): + docs = _docs("--api-versions", MAP_API) + pod = _aegra_pod_template(docs) + assert pod["metadata"]["labels"]["zelkor.io/dns-lockdown"] == "requested" + assert pod["metadata"]["labels"]["zelkor.io/platform-release"] == "zelkor-platform" + # The policy sets these with the label; the template must not, or a missed pod hangs without DNS. + assert "dnsPolicy" not in pod["spec"] and "dnsConfig" not in pod["spec"] + + +def test_platform_aegra_without_policy_or_map_keeps_dns_and_is_marked(): + pod = _aegra_pod_template(_docs()) + assert "dnsPolicy" not in pod["spec"] and "zelkor.io/dns-lockdown" not in pod["metadata"]["labels"] + assert pod["metadata"]["annotations"]["zelkor.io/dns-lockdown"] == "inactive" + + +def test_platform_aegra_opt_out(): + pod = _aegra_pod_template(_docs("--set", "security.dnsLockdown.aegra=false", "--api-versions", MAP_API)) + assert "dnsPolicy" not in pod["spec"] and "zelkor.io/dns-lockdown" not in pod["metadata"].get("annotations", {}) + + +def test_qdrant_joins_the_host_map(): + assert "zelkor-platform-qdrant" in _writer_env(_docs())["AGENT_HOSTS_SERVICES"].split() + diff --git a/tests/test_s90_dns_egress_live.py b/tests/test_s90_dns_egress_live.py index 0d00f646..523c64fb 100644 --- a/tests/test_s90_dns_egress_live.py +++ b/tests/test_s90_dns_egress_live.py @@ -5,7 +5,8 @@ blocked. Proof 2026-11-18 on demohetzner: a never-seen `.example.com` came back with example.com's SOA from ns.cloudflare.com. -Live (L3): for every locked agent pod (zelkor.io/dns-lockdown=on) the probe runs in its sidecar: +Live (L3): for every locked pod (zelkor.io/dns-lockdown=on: agents, platform Aegra) the probe runs +in its sidecar or main container, and for every sandbox worker: A and TXT lookups of a never-seen outside name and of github.com must fail, kube-dns must be unreachable on UDP and TCP 53, and no answer may carry an outside SOA. Positive control: the gateway is still reachable by name from /etc/hosts. @@ -57,7 +58,7 @@ def resolves(name): "udp_a": dns(tag, 1, kube_dns), "udp_txt": dns("example.com", 16, kube_dns), "tcp_a": dns(tag, 1, kube_dns, tcp=True), - "gateway": resolves(os.environ.get("S90_GATEWAY_HOST", "")), + "gateway": bool(os.environ.get("S90_GATEWAY_HOST")) and resolves(os.environ["S90_GATEWAY_HOST"]), } print("S90 " + json.dumps(out)) """ @@ -117,3 +118,27 @@ def test_s90_locked_agents_cannot_resolve_or_reach_dns(): host = _gateway_host(pod) assert host, f"{where}: no gateway URL in the pod env; the positive control cannot run" assert out["gateway"], f"{where}: positive control failed, {host} does not resolve from /etc/hosts" + + +def test_s90_sandbox_workers_have_no_dns_path(): + """Sandbox workers run the code agents generate; their egress is deny-all, DNS included.""" + if not os.environ.get("KUBECONTEXT", "").strip(): + pytest.skip("KUBECONTEXT not set") + pods = [p for p in _kubectl_json("get", "pods", "-A", "-l", "app.kubernetes.io/component=mcp-sandbox-worker") + .get("items") or [] if (p.get("status") or {}).get("phase") == "Running"] + if not pods: + pytest.skip("no running sandbox worker (sandboxMCP off)") + kube_dns = _kube_dns_ip() + for pod in pods: + meta = pod["metadata"] + where = f"{meta['namespace']}/{meta['name']}" + res = subprocess.run( + kubectl_cmd("exec", "-i", "-n", meta["namespace"], meta["name"], "-c", pod["spec"]["containers"][0]["name"], + "--", "env", f"S90_KUBE_DNS={kube_dns}", "S90_GATEWAY_HOST=", "python3", "-"), + input=PROBE, capture_output=True, text=True, timeout=120) + line = next((ln for ln in res.stdout.splitlines() if ln.startswith("S90 ")), "") + assert line, f"{where}: probe did not run: {res.stderr[-300:]}" + out = json.loads(line[4:]) + assert not out["random_resolves"] and not out["github_resolves"], f"{where}: outside names resolve: {out}" + for key in ("udp_a", "udp_txt", "tcp_a"): + assert not out[key]["answered"], f"{where}: kube-dns answered {key}: {out[key]}" diff --git a/tests/test_zelkor_hosts.py b/tests/test_zelkor_hosts.py index 516f111b..7c22da8e 100644 --- a/tests/test_zelkor_hosts.py +++ b/tests/test_zelkor_hosts.py @@ -63,7 +63,8 @@ def test_deploy_fails_naming_the_writer_job_when_the_map_never_appears(): load_host_aliases(ENV, info, runner=lambda *a, **k: MISSING, wait_sec=0, sleep=lambda _s: None) -def _cluster(*, live_gw=GW, pod_aliases=None, upstreams="", annotations=None, lockdown="on"): +def _cluster(*, live_gw=GW, pod_aliases=None, upstreams="", annotations=None, lockdown="on", extra_pods=()): + extra_pods = list(extra_pods) contract = {"data": {"AGENT_HOSTS_CONFIGMAP": "zelkor-platform-agent-hosts", "HTTP_APIS": "github"}} services = { "zelkor-platform-ai-gateway": {"spec": {"type": "ExternalName", @@ -90,7 +91,7 @@ def runner(argv, **_k): svc = services.get(argv[argv.index("svc") + 1]) return _ok(svc) if svc else MISSING if "pods" in argv: - return _ok({"items": [pod]}) + return _ok({"items": [pod] + extra_pods}) return MISSING return runner @@ -126,3 +127,29 @@ def test_stale_http_api_name_is_reported(capsys): def test_inactive_lockdown_is_a_warning_not_a_failure(capsys): assert _check(_cluster(lockdown="", annotations={"zelkor.io/dns-lockdown": "inactive"})) == 0 assert "WARN dns" in capsys.readouterr().out + + +def _aegra_pod(aliases): + return {"metadata": {"namespace": "zelkor", "name": "zelkor-platform-aegra-x", + "labels": {"app.kubernetes.io/instance": "zelkor-platform", "app.kubernetes.io/component": "aegra", + "zelkor.io/dns-lockdown": "on", "zelkor.io/platform-release": "zelkor-platform"}}, + "spec": {"hostAliases": aliases, "containers": [{"name": "aegra", "env": []}]}} + + +def test_platform_aegra_is_checked_too(capsys): + stale = [{"ip": "10.43.1.1", "hostnames": ["zelkor-platform-ai-gateway"]}] + assert _check(_cluster(extra_pods=[_aegra_pod(stale)])) == 1 + assert "FAIL pod hosts zelkor/zelkor-platform-aegra-x" in capsys.readouterr().out + + +def test_unrelated_pods_are_ignored(capsys): + other = {"metadata": {"namespace": "kube-system", "name": "coredns-x", "labels": {"k8s-app": "kube-dns"}}, + "spec": {"containers": []}} + assert _check(_cluster(extra_pods=[other])) == 0 + assert "coredns" not in capsys.readouterr().out + + +def test_pod_the_policy_missed_is_a_warning(capsys): + assert _check(_cluster(lockdown="requested")) == 0 + assert "did not lock it; delete the pod" in capsys.readouterr().out +