Skip to content

Commit eb16cf1

Browse files
committed
fix(kubernetes-nodes): refuse a newline in the Proxmox template tags
COZY_TEMPLATE_TAGS is comma-separated, but the normalisation also split on a newline, so `talos-v1.\n13` became two tags where the operator meant one. A newline there is now refused, like in every other value the program receives. The stub tests clean up their scratch directory after the last assertion, so a failing test leaves its qm, curl and ssh records behind for inspection. Assisted-by: LLM Signed-off-by: Marian Koreniuk <[email protected]>
1 parent cdce06f commit eb16cf1

2 files changed

Lines changed: 11 additions & 6 deletions

File tree

‎hack/proxmox-talos-template.sh‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ matches "$CPU" '[A-Za-z0-9_.+-]+' || die "cpu type $CPU is not a Proxmox CPU mod
9696

9797
# capmox lowercases the selector and compares it with the template's tags as a
9898
# set, so the tags are normalised the same way before they are checked and
99-
# written, and the selector printed at the end is exactly what will match.
99+
# written, and the selector printed at the end is exactly what will match. The
100+
# split is on commas only: a newline would split a tag the operator meant whole.
101+
case $TAGS in *'
102+
'*) die "template tags are comma-separated and may not contain a newline" ;; esac
100103
TAG_SET=$(printf '%s\n' "$TAGS" | tr ',' '\n' | tr '[:upper:]' '[:lower:]' | sed '/^$/d' | LC_ALL=C sort -u)
101104
[ -n "$TAG_SET" ] || die "the template needs at least one tag"
102105
bad=$(printf '%s\n' "$TAG_SET" | grep -Evx '[a-z0-9_][a-z0-9_+.-]*' || true)

‎hack/proxmox-talos-template_test.bats‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ EOF
8383
[ "$rc" -ne 0 ]
8484
rc=0; COZY_TEMPLATE_TAGS="talos,it's" "$SCRIPT" print 9002 main-pool >/dev/null 2>&1 || rc=$?
8585
[ "$rc" -ne 0 ]
86+
rc=0; COZY_TEMPLATE_TAGS="talos-v1.${nl}13,cozystack" "$SCRIPT" print 9002 main-pool >/dev/null 2>&1 || rc=$?
87+
[ "$rc" -ne 0 ]
8688
}
8789

8890
@test "a pre-release Talos version the chart accepts gets a template" {
@@ -99,10 +101,10 @@ EOF
99101
rc=0
100102
out="$("$SCRIPT" print 9002 main-pool | PATH="$stub:$PATH" TMPDIR="$stub" sh 2>&1)" || rc=$?
101103
calls="$(cat "$stub/calls")"
102-
rm -rf "$stub"
103104
[ "$rc" -ne 0 ]
104105
case "$out" in *"already carries"*"9000 (old) on pve1"*) ;; *) echo "$out" >&2; exit 1 ;; esac
105106
[ -z "$calls" ] || { echo "$calls" >&2; exit 1; }
107+
rm -rf "$stub"
106108
}
107109

108110
@test "a template whose tags are a superset does not count, and the template is built" {
@@ -112,11 +114,11 @@ EOF
112114
"$(chart schematicID | cut -c1-8)" > "$stub/resources.json"
113115
"$SCRIPT" print 9002 main-pool | PATH="$stub:$PATH" TMPDIR="$stub" sh >/dev/null
114116
calls="$(cat "$stub/calls")"
115-
rm -rf "$stub"
116117
printf '%s\n' "$calls" | grep -q '^curl .*/nocloud-amd64.raw.xz$'
117118
printf '%s\n' "$calls" | grep -qE '^qm create 9002 .*--scsi0 main-pool:0,import-from=[^ ]*/disk.raw,'
118119
printf '%s\n' "$calls" | grep -qx 'qm disk resize 9002 scsi0 20G'
119120
printf '%s\n' "$calls" | grep -qx 'qm template 9002'
121+
rm -rf "$stub"
120122
}
121123

122124
@test "a VM left behind by a failed conversion is named with the command that removes it" {
@@ -127,10 +129,10 @@ EOF
127129
rc=0
128130
out="$("$SCRIPT" print 9002 main-pool | PATH="$stub:$PATH" TMPDIR="$stub" sh 2>&1)" || rc=$?
129131
calls="$(cat "$stub/calls")"
130-
rm -rf "$stub"
131132
[ "$rc" -ne 0 ]
132133
printf '%s\n' "$calls" | grep -qx 'qm template 9002'
133134
case "$out" in *"remove it with: qm destroy 9002"*) ;; *) echo "$out" >&2; exit 1 ;; esac
135+
rm -rf "$stub"
134136
}
135137

136138
@test "create runs the printed program on the hypervisor over ssh and prints the pool's selector" {
@@ -140,11 +142,11 @@ EOF
140142
out="$(PATH="$stub:$PATH" COZY_PVE_SSH=root@pve "$SCRIPT" create 9002 main-pool)"
141143
args="$(cat "$stub/args")"
142144
sent="$(cat "$stub/stdin")"
143-
rm -rf "$stub"
144145
case "$args" in *"root@pve sh -s") ;; *) echo "$args" >&2; exit 1 ;; esac
145146
[ "$sent" = "$("$SCRIPT" print 9002 main-pool)" ]
146147
printf '%s' "$out" | grep -qxF ' - "talos-'"$(chart version)"'"'
147148
printf '%s' "$out" | grep -qxF ' schematicID: "'"$(chart schematicID)"'"'
149+
rm -rf "$stub"
148150
}
149151

150152
@test "create fails when the hypervisor program fails, and prints no selector" {
@@ -153,7 +155,7 @@ EOF
153155
chmod +x "$stub/ssh"
154156
rc=0
155157
out="$(PATH="$stub:$PATH" COZY_PVE_SSH=root@pve "$SCRIPT" create 9002 main-pool 2>&1)" || rc=$?
156-
rm -rf "$stub"
157158
[ "$rc" -ne 0 ]
158159
case "$out" in *templateTags*) echo "$out" >&2; exit 1 ;; esac
160+
rm -rf "$stub"
159161
}

0 commit comments

Comments
 (0)