Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions charts/zelkor-agent/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -423,18 +423,22 @@ 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 -}}
{{- $aliases := ((.Values.platform).hostAliases) | default list -}}
{{- $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
Expand Down
5 changes: 4 additions & 1 deletion charts/zelkor-agent/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
34 changes: 33 additions & 1 deletion charts/zelkor-agent/templates/networkpolicies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
62 changes: 62 additions & 0 deletions charts/zelkor-platform/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
21 changes: 21 additions & 0 deletions charts/zelkor-platform/templates/aegra/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 9 additions & 7 deletions charts/zelkor-platform/templates/security/agent-hosts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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]}})
---
Expand Down
4 changes: 4 additions & 0 deletions charts/zelkor-platform/values.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -4150,6 +4150,10 @@
"type": "boolean",
"description": "Value for security.dnsLockdown.enabled."
},
"aegra": {
"type": "boolean",
"description": "Value for security.dnsLockdown.aegra."
},
"writerDeadlineSeconds": {
"type": [
"integer",
Expand Down
3 changes: 3 additions & 0 deletions charts/zelkor-platform/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 12 additions & 3 deletions cli/src/zelkor/hosts_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand Down Expand Up @@ -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()}
Expand Down
21 changes: 16 additions & 5 deletions tests/test_helm_agent_dns_lockdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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():
Expand Down
34 changes: 33 additions & 1 deletion tests/test_helm_dns_lockdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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()

Loading
Loading