Skip to content

Commit c749681

Browse files
test(kubernetes-nodes): assert the schematicID guard, not a literal render
The merge took upstream's talos-reconcile-heredoc_test.bats verbatim and lost the adaptation this branch carried, so CI failed on the first case: it sets talos.schematicID to `sch$(id)`q`z` and asserts the value reaches the heredoc and survives literally, while this branch refuses that render outright. The INVARIANT above the data block admits two protections, escaping or render-time validation. talos.version and talos.installerRepository have only the first, so for them the assertion stays what upstream wrote: the heredoc emits and the value survives verbatim. schematicID now has the second as well, so the execution-level assertion inverts -- no heredoc is produced at all -- and asserting a hostile value renders literally would pin behaviour the guard exists to prevent. So the coordinates case keeps schematicID at the same site with a benign multi-segment value (`gpu/nvidia-open`, which the guard admits), and two new cases pin the refusal itself: once through the cluster-wide talos.schematicID and once through the pool-level schematicID this PR adds, since the guard runs on the effective value and a pool override that skipped it would be a hole. The escape chain stays in the template as a second line of defence, unreachable for hostile input while the guard holds. hack/cozytest.sh hack/talos-reconcile-heredoc_test.bats: 4 passed. Assisted-By: Claude <[email protected]> Signed-off-by: Mattia Eleuteri <[email protected]>
1 parent 1cb5705 commit c749681

1 file changed

Lines changed: 64 additions & 2 deletions

File tree

‎hack/talos-reconcile-heredoc_test.bats‎

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@
2323
# heredoc still emits the TalosConfigTemplate (non-empty) and that the hostile
2424
# value rendered as a LITERAL (never command-substituted).
2525
#
26+
# The INVARIANT recorded above the data block admits two protections, escaping or
27+
# render-time validation, and both are exercised here. talos.version and
28+
# talos.installerRepository have only the escaping, so for them the assertion is
29+
# that the heredoc emits and the value survives verbatim. talos.schematicID is
30+
# guarded at render time as well, so for it the assertion is the opposite: the
31+
# render is refused and no heredoc is produced. Its escape chain stays in the
32+
# template as a second line of defence, unreachable for hostile input while the
33+
# guard holds -- a benign schematic carrying a `/` still rides the same site here,
34+
# so the site itself stays covered.
35+
#
2636
# Needs `helm` + `yq`; cozytest.sh runs from the repo root.
2737
# Run with: hack/cozytest.sh hack/talos-reconcile-heredoc_test.bats
2838
# -----------------------------------------------------------------------------
@@ -44,7 +54,7 @@ resources: {cpu: "2", memory: 4Gi}
4454
VALS
4555
helm template kubernetes-nodes-myk8s-md0 packages/apps/kubernetes-nodes -n tenant-test -f "$work/vals.yaml" \
4656
--set 'talos.installerRepository=reg$(id)`x`y/installer' \
47-
--set 'talos.schematicID=sch$(id)`q`z' \
57+
--set 'talos.schematicID=gpu/nvidia-open' \
4858
--set 'talos.version=v1.13.6$(id)`v`w' \
4959
--show-only templates/talos-reconcile-job.yaml \
5060
| yq 'select(.kind == "Job") | .spec.template.spec.containers[0].command[2]' \
@@ -59,7 +69,7 @@ VALS
5969
out=$(sh "$work/heredoc.sh" 2>"$work/err") || { echo "kubernetes-nodes heredoc shell exited non-zero" >&2; cat "$work/err" >&2; rm -rf "$work"; exit 1; }
6070
[ -n "$out" ] || { echo "kubernetes-nodes heredoc emitted no output" >&2; rm -rf "$work"; exit 1; }
6171
printf '%s' "$out" | grep -qF 'reg$(id)`x`y/installer' || { echo "kubernetes-nodes installerRepository was not preserved literally" >&2; rm -rf "$work"; exit 1; }
62-
printf '%s' "$out" | grep -qF 'sch$(id)`q`z' || { echo "kubernetes-nodes schematicID was not preserved literally" >&2; rm -rf "$work"; exit 1; }
72+
printf '%s' "$out" | grep -qF 'gpu/nvidia-open' || { echo "kubernetes-nodes schematicID was not preserved literally" >&2; rm -rf "$work"; exit 1; }
6373
[ "$(printf '%s' "$out" | grep -cF 'v1.13.6$(id)`v`w')" -eq 2 ] || { echo "kubernetes-nodes talos.version was not preserved literally at both sites" >&2; printf '%s\n' "$out" | grep -iE 'talosVersion|image:' >&2; rm -rf "$work"; exit 1; }
6474
rm -rf "$work"
6575
}
@@ -97,3 +107,55 @@ VALS
97107
printf '%s' "$out" | grep -qF 'http://m$(id)`x`y' || { echo "kubernetes-nodes hostile endpoint not preserved literally" >&2; rm -rf "$work"; exit 1; }
98108
rm -rf "$work"
99109
}
110+
111+
# The other half of the INVARIANT above the data block: a field may be protected
112+
# by escaping OR by render-time validation. schematicID takes the second route,
113+
# so the execution-level check for it is that no heredoc is produced at all.
114+
115+
@test "kubernetes-nodes TalosConfigTemplate render rejects a hostile schematicID before any heredoc exists" {
116+
work=$(mktemp -d)
117+
cat > "$work/vals.yaml" <<'VALS'
118+
cluster: myk8s
119+
_cluster:
120+
cluster-domain: cozy.local
121+
version: "v1.35"
122+
minReplicas: 0
123+
maxReplicas: 3
124+
instanceType: ""
125+
diskSize: 20Gi
126+
storageClass: replicated
127+
roles: [ingress-nginx]
128+
resources: {cpu: "2", memory: 4Gi}
129+
VALS
130+
if helm template kubernetes-nodes-myk8s-md0 packages/apps/kubernetes-nodes -n tenant-test -f "$work/vals.yaml" \
131+
--set 'talos.schematicID=sch$(id)`q`z' \
132+
--show-only templates/talos-reconcile-job.yaml >"$work/out" 2>"$work/err"; then
133+
echo "kubernetes-nodes hostile schematicID rendered instead of being rejected" >&2; cat "$work/out" >&2; rm -rf "$work"; exit 1
134+
fi
135+
grep -q 'invalid schematicID' "$work/err" || { echo "kubernetes-nodes render failed for a reason other than the schematicID guard" >&2; cat "$work/err" >&2; rm -rf "$work"; exit 1; }
136+
rm -rf "$work"
137+
}
138+
139+
@test "kubernetes-nodes TalosConfigTemplate render rejects a hostile per-pool schematicID too" {
140+
work=$(mktemp -d)
141+
cat > "$work/vals.yaml" <<'VALS'
142+
cluster: myk8s
143+
_cluster:
144+
cluster-domain: cozy.local
145+
version: "v1.35"
146+
minReplicas: 0
147+
maxReplicas: 3
148+
instanceType: ""
149+
diskSize: 20Gi
150+
storageClass: replicated
151+
roles: [ingress-nginx]
152+
resources: {cpu: "2", memory: 4Gi}
153+
VALS
154+
if helm template kubernetes-nodes-myk8s-md0 packages/apps/kubernetes-nodes -n tenant-test -f "$work/vals.yaml" \
155+
--set 'schematicID=sch$(id)`q`z' \
156+
--show-only templates/talos-reconcile-job.yaml >"$work/out" 2>"$work/err"; then
157+
echo "kubernetes-nodes hostile per-pool schematicID rendered instead of being rejected" >&2; cat "$work/out" >&2; rm -rf "$work"; exit 1
158+
fi
159+
grep -q 'invalid schematicID' "$work/err" || { echo "kubernetes-nodes render failed for a reason other than the schematicID guard" >&2; cat "$work/err" >&2; rm -rf "$work"; exit 1; }
160+
rm -rf "$work"
161+
}

0 commit comments

Comments
 (0)