Skip to content

Commit 102e694

Browse files
committed
fix(kubernetes): stop registryMirrors comment from breaking the reconcile-Job heredoc
The explanatory lines added next to the registryMirrors block were plain YAML comments (# ...) that rendered verbatim into the worker TalosConfigTemplate's unquoted `cat <<EOF | kubectl apply -f -` heredoc. They contained backticks and ${...}, which the shell tries to expand in an unquoted heredoc; the three backticks are unbalanced, so the body dies with 'no closing backtick', cat emits zero bytes, kubectl apply gets empty stdin, and the TalosConfigTemplate is never applied for ANY non-empty registryMirrors (the whole feature path). The value escaping on the toYaml line was correct; the comment text was not escaped because it is template literal, not toYaml input. Move the note into a Helm comment ({{- /* ... */}}), which is stripped at render and never reaches the heredoc, and keep it free of shell metacharacters. The default ({}) render stays byte-identical (with-block skipped), so the content-hash Job name is unchanged. Add hack/talos-reconcile-heredoc_test.bats: it renders the Job with a hostile mirror endpoint, extracts the cat <<EOF ... EOF block, runs it through a real shell, and asserts non-empty output plus the value surviving literally. A matchRegex over the rendered string could not catch this class; executing the heredoc does. Refs #3513 Signed-off-by: IvanHunters <[email protected]>
1 parent 78912a6 commit 102e694

2 files changed

Lines changed: 82 additions & 6 deletions

File tree

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env bats
2+
# -----------------------------------------------------------------------------
3+
# Execution-level regression test for the worker TalosConfigTemplate heredoc
4+
# (cozystack/cozystack#3513, #3575 review).
5+
#
6+
# The reconcile Job applies the TalosConfigTemplate via an UNQUOTED
7+
# `cat <<EOF | kubectl apply -f -` heredoc, so every line of its body is subject
8+
# to shell parameter expansion and command substitution at Job runtime. The
9+
# `talos.registryMirrors` knob renders free-form tenant-facing input into that
10+
# heredoc. A helm-unittest string `matchRegex` cannot catch a heredoc that the
11+
# shell refuses to emit (e.g. an unbalanced backtick from an un-escaped value OR
12+
# from a stray literal comment): it never runs the shell. This test does.
13+
#
14+
# It renders the Job with a HOSTILE mirror endpoint (`$(...)` + a backtick),
15+
# extracts the `cat <<EOF ... EOF` block, runs it through a real shell, and
16+
# asserts the heredoc still emits the TalosConfigTemplate (non-empty) and that
17+
# the hostile value rendered as a LITERAL (never command-substituted). On the
18+
# pre-fix code (unescaped value, or the explanatory comment carrying raw
19+
# backticks) the heredoc dies with "bad substitution: no closing backtick" and
20+
# `cat` emits zero bytes, which this test catches; a `matchRegex` did not.
21+
#
22+
# Needs `helm` + `yq`; cozytest.sh runs from the repo root.
23+
# Run with: hack/cozytest.sh hack/talos-reconcile-heredoc_test.bats
24+
# -----------------------------------------------------------------------------
25+
26+
CHART=packages/apps/kubernetes
27+
28+
@test "worker TalosConfigTemplate heredoc emits with a hostile registryMirrors value (no shell break, value stays literal)" {
29+
work=$(mktemp -d)
30+
# Hostile endpoint: a command substitution and a backtick. Single-quoted so
31+
# THIS shell does not expand it before helm sees it.
32+
helm template t "$CHART" -n tenant-test -f "$CHART/tests/values/common.yaml" \
33+
--set 'talos.registryMirrors.ghcr\.io.endpoints[0]=http://m$(id)`x`y' \
34+
--show-only templates/talos/talos-reconcile-job.yaml \
35+
| yq 'select(.kind == "Job") | .spec.template.spec.containers[0].command[2]' \
36+
> "$work/cmd.sh"
37+
[ -s "$work/cmd.sh" ] || { echo "render produced no Job command" >&2; rm -rf "$work"; exit 1; }
38+
39+
# Extract just the TalosConfigTemplate heredoc, dropping the kubectl pipe so we
40+
# exercise the shell's handling of the body without applying anything. The
41+
# rendered script dedents the heredoc to column 0, so the terminator is `EOF`.
42+
awk '
43+
/^cat <<EOF \| kubectl apply/ { print "cat <<EOF"; inblock=1; next }
44+
inblock && /^EOF$/ { print "EOF"; inblock=0; next }
45+
inblock { print }
46+
' "$work/cmd.sh" > "$work/heredoc.sh"
47+
grep -q '^cat <<EOF$' "$work/heredoc.sh" || { echo "could not extract the heredoc" >&2; rm -rf "$work"; exit 1; }
48+
49+
# Run the heredoc through a real shell. Unset ${RELEASE}/${CLUSTER_ID}/... just
50+
# expand to empty; the point is that the shell can emit the body at all.
51+
out=$(sh "$work/heredoc.sh" 2>"$work/err") || { echo "heredoc shell exited non-zero" >&2; cat "$work/err" >&2; rm -rf "$work"; exit 1; }
52+
53+
# (a) Non-empty: on the pre-fix break `cat` emitted zero bytes.
54+
[ -n "$out" ] || { echo "heredoc emitted no output (shell refused the body)" >&2; cat "$work/err" >&2; rm -rf "$work"; exit 1; }
55+
# (b) It really is the worker config.
56+
printf '%s' "$out" | grep -q 'kind: TalosConfigTemplate' || { echo "heredoc output is not the TalosConfigTemplate" >&2; rm -rf "$work"; exit 1; }
57+
# (c) The hostile value rendered LITERALLY: the command substitution was NOT
58+
# executed (its literal text survives) and no `id` output leaked in.
59+
printf '%s' "$out" | grep -qF 'http://m$(id)`x`y' || { echo "hostile endpoint was not preserved literally (shell expansion leaked)" >&2; printf '%s\n' "$out" | grep -i ghcr >&2; rm -rf "$work"; exit 1; }
60+
61+
rm -rf "$work"
62+
}
63+
64+
@test "worker TalosConfigTemplate heredoc emits with the default (no registryMirrors)" {
65+
work=$(mktemp -d)
66+
helm template t "$CHART" -n tenant-test -f "$CHART/tests/values/common.yaml" \
67+
--show-only templates/talos/talos-reconcile-job.yaml \
68+
| yq 'select(.kind == "Job") | .spec.template.spec.containers[0].command[2]' \
69+
> "$work/cmd.sh"
70+
awk '
71+
/^cat <<EOF \| kubectl apply/ { print "cat <<EOF"; inblock=1; next }
72+
inblock && /^EOF$/ { print "EOF"; inblock=0; next }
73+
inblock { print }
74+
' "$work/cmd.sh" > "$work/heredoc.sh"
75+
out=$(sh "$work/heredoc.sh" 2>/dev/null) || { echo "default heredoc shell exited non-zero" >&2; rm -rf "$work"; exit 1; }
76+
[ -n "$out" ] || { echo "default heredoc emitted no output" >&2; rm -rf "$work"; exit 1; }
77+
printf '%s' "$out" | grep -q 'kind: TalosConfigTemplate' || { echo "default heredoc output is not the TalosConfigTemplate" >&2; rm -rf "$work"; exit 1; }
78+
# Default renders no machine-level registries mirrors block.
79+
printf '%s' "$out" | grep -q 'mirrors:' && { echo "default unexpectedly rendered a mirrors block" >&2; rm -rf "$work"; exit 1; }
80+
rm -rf "$work"
81+
}

‎packages/apps/kubernetes/templates/talos/talos-reconcile-job.yaml‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -424,14 +424,9 @@ spec:
424424
kubePrism:
425425
enabled: false
426426
{{- with $.Values.talos.registryMirrors }}
427-
# Worker image-pull mirrors (see cozystack/cozystack#3513).
428-
# This block is emitted into the `cat <<EOF` heredoc above,
429-
# which is intentionally unquoted so the ${VARS} the Job sets
430-
# expand. registryMirrors is free-form tenant-facing input, so
431-
# its rendered value is escaped (\ $ `) to render literally and
432-
# never be shell-expanded/command-substituted at Job runtime.
433427
registries:
434428
mirrors:
429+
{{- /* registryMirrors is free-form tenant-facing input rendered into the unquoted reconcile-Job heredoc above, so the value is escaped (backslash, dollar, backtick) to render as a literal and never be shell-expanded or command-substituted at Job runtime. This is a Helm comment: it is stripped at render, so its own text never reaches the heredoc. See cozystack cozystack#3513. */}}
435430
{{- toYaml . | replace "\\" "\\\\" | replace "$" "\\$" | replace "`" "\\`" | nindent 28 }}
436431
{{- end }}
437432
cluster:

0 commit comments

Comments
 (0)