Skip to content

Commit 6a22443

Browse files
committed
feat(kubernetes-nodes): trust a factory CA in the Proxmox template script
A pool can now point CDI at a self-hosted Image Factory served by an internal CA through talos.imageFactoryCA. hack/proxmox-talos-template.sh downloads the same image with curl on the hypervisor, which trusts only the system store and fails there with the same unknown-authority error. COZY_TALOS_FACTORY_CA names a PEM file with those certificates. The program writes them after the hypervisor's system bundle and hands the result to curl --cacert, so they are trusted on top of the system store, as CDI does, and the hypervisor's own trust store is not changed. The file is pasted into the root program, so it must be complete certificate blocks and nothing else; a private key sharing the file, trailing text or an unclosed block is refused, as is a CA given for a plain-http factory. Without the variable the program is byte-for-byte what it was. Assisted-by: LLM Signed-off-by: Marian Koreniuk <[email protected]>
1 parent 6852f72 commit 6a22443

2 files changed

Lines changed: 111 additions & 0 deletions

File tree

‎hack/proxmox-talos-template.sh‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@
3939
# template has, so this is every worker's system disk.
4040
# COZY_TEMPLATE_CPU default x86-64-v2-AES. Talos needs x86-64-v2, which
4141
# the Proxmox default kvm64 does not provide.
42+
# COZY_TALOS_FACTORY_CA path to a PEM file with the CA certificate(s) of a
43+
# self-hosted factory or mirror served by an internal
44+
# CA, the blocks a kubevirt pool puts in
45+
# talos.imageFactoryCA. curl on the hypervisor trusts
46+
# them on top of the system store, as CDI does, and
47+
# the hypervisor's own trust store is left as it is.
4248
# COZY_PVE_SSH_KEY default ~/.ssh/id_ed25519
4349
set -eu
4450

@@ -60,6 +66,21 @@ chart_talos() {
6066
' "$VALUES"
6167
}
6268

69+
# Prints the certificate blocks of a PEM file and fails unless the file holds
70+
# complete certificate blocks and nothing else: they are pasted into the root
71+
# program, and a private key kept in the same file must not travel with them.
72+
pem_certificates() {
73+
tr -d '\r' < "$1" | awk '
74+
{ sub(/[ \t]+$/, "") }
75+
/^-----BEGIN CERTIFICATE-----$/ && !inside { inside = 1; body = 0; print; next }
76+
/^-----END CERTIFICATE-----$/ && inside && body { inside = 0; blocks++; print; next }
77+
inside && /^[A-Za-z0-9+\/=]+$/ { body = 1; print; next }
78+
!inside && /^$/ { next }
79+
{ bad = 1; exit }
80+
END { exit (bad || inside || !blocks) }
81+
'
82+
}
83+
6384
# grep -x matches line by line, so a value with a newline in it would pass on
6485
# any one of its lines and carry the rest into the program as commands of their
6586
# own. Such a value is refused before grep sees it.
@@ -81,6 +102,7 @@ FACTORY=${COZY_TALOS_FACTORY_URL:-$(chart_talos imageFactoryURL)}
81102
FACTORY=${FACTORY%/}
82103
DISK_SIZE=${COZY_TEMPLATE_DISK_SIZE:-20G}
83104
CPU=${COZY_TEMPLATE_CPU:-x86-64-v2-AES}
105+
FACTORY_CA=${COZY_TALOS_FACTORY_CA:-}
84106
TAGS=${COZY_TEMPLATE_TAGS:-cozystack,talos,talos-$VERSION,schematic-$(printf '%s' "$SCHEMATIC" | cut -c1-8)}
85107

86108
# Every value below is pasted into a program that runs as root on the
@@ -93,6 +115,14 @@ matches "$SCHEMATIC" '[0-9a-f]{64}' || die "schematic '$SCHEMATIC' is not an Ima
93115
matches "$FACTORY" 'https?://[A-Za-z0-9.-]+(:[0-9]+)?(/[A-Za-z0-9._~/-]*)?' || die "factory URL '$FACTORY' is not a plain http(s) URL"
94116
matches "$DISK_SIZE" '[1-9][0-9]*G' || die "disk size $DISK_SIZE is not a whole number of gigabytes such as 20G"
95117
matches "$CPU" '[A-Za-z0-9_.+-]+' || die "cpu type $CPU is not a Proxmox CPU model name"
118+
CA_PEM=
119+
if [ -n "$FACTORY_CA" ]; then
120+
case $FACTORY in https://*) ;; *) die "COZY_TALOS_FACTORY_CA is set, but the factory URL '$FACTORY' is not https" ;; esac
121+
[ -f "$FACTORY_CA" ] || die "COZY_TALOS_FACTORY_CA: $FACTORY_CA is not a file"
122+
[ -r "$FACTORY_CA" ] || die "COZY_TALOS_FACTORY_CA: cannot read $FACTORY_CA"
123+
CA_PEM=$(pem_certificates "$FACTORY_CA") ||
124+
die "COZY_TALOS_FACTORY_CA: $FACTORY_CA is not a file of complete PEM certificate blocks and nothing else"
125+
fi
96126

97127
# capmox lowercases the selector and compares it with the template's tags as a
98128
# set, so the tags are normalised the same way before they are checked and
@@ -147,7 +177,25 @@ fi
147177
148178
work=$(mktemp -d "${TMPDIR:-/var/tmp}/talos-template.XXXXXX")
149179
trap 'rm -rf "$work"' EXIT
180+
EOF
181+
if [ -z "$CA_PEM" ]; then
182+
cat <<'EOF'
150183
curl -fL --retry 3 -o "$work/disk.raw.xz" "$URL"
184+
EOF
185+
else
186+
# The system bundle goes first so that a redirect to a publicly trusted
187+
# host still verifies: CDI appends talos.imageFactoryCA to its system pool
188+
# the same way. The blank line keeps a bundle without a final newline from
189+
# running into the first block. The terminator holds an underscore, which
190+
# no line of a checked certificate block can.
191+
cat <<EOF
192+
{ cat /etc/ssl/certs/ca-certificates.crt; echo; cat; } > "\$work/factory-ca.pem" <<'FACTORY_CA_PEM'
193+
$CA_PEM
194+
FACTORY_CA_PEM
195+
curl -fL --retry 3 --cacert "\$work/factory-ca.pem" -o "\$work/disk.raw.xz" "\$URL"
196+
EOF
197+
fi
198+
cat <<'EOF'
151199
xz -d "$work/disk.raw.xz"
152200
153201
# The layout of the templates the Proxmox substrate was verified on. No NIC:

‎hack/proxmox-talos-template_test.bats‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,69 @@ EOF
9393
printf '%s' "$out" | grep -qxF "NAME='talos-v1.13.6-beta.1-$(chart schematicID | cut -c1-8)'"
9494
}
9595

96+
# Two certificate blocks in the shape the CA checks accept. Only the shape is
97+
# checked here, so the bodies are base64 text and not real certificates.
98+
write_ca() {
99+
printf '%s\n' '-----BEGIN CERTIFICATE-----' 'MIIBszCCAVmgAwIBAgIUZmFjdG9yeS1jYS1vbmU=' '-----END CERTIFICATE-----' \
100+
'' '-----BEGIN CERTIFICATE-----' 'MIIBszCCAVmgAwIBAgIUZmFjdG9yeS1jYS10d28=' 'c2Vjb25kIGxpbmU=' '-----END CERTIFICATE-----' > "$1"
101+
}
102+
103+
@test "without a factory CA the download trusts the system store alone" {
104+
out="$("$SCRIPT" print 9002 main-pool)"
105+
printf '%s' "$out" | grep -qx 'curl -fL --retry 3 -o "$work/disk.raw.xz" "$URL"'
106+
if printf '%s' "$out" | grep -q -- '--cacert'; then echo "FAIL: no CA was given, so curl must not be handed one"; false; fi
107+
}
108+
109+
@test "a factory CA is trusted on top of the system store for the download" {
110+
stub="$(mktemp -d)"
111+
stub_pve "$stub"
112+
cat > "$stub/curl" <<EOF
113+
#!/bin/sh
114+
echo "curl \$*" >> "$stub/calls"
115+
while [ \$# -gt 0 ]; do
116+
case "\$1" in
117+
--cacert) cp "\$2" "$stub/cacert" ;;
118+
-o) : > "\$2" ;;
119+
esac
120+
shift
121+
done
122+
EOF
123+
echo '[]' > "$stub/resources.json"
124+
printf 'system bundle\n' > "$stub/system.pem"
125+
write_ca "$stub/ca.pem"
126+
sed 's/$/\r/' "$stub/ca.pem" > "$stub/ca-crlf.pem"
127+
COZY_TALOS_FACTORY_URL=https://factory.internal COZY_TALOS_FACTORY_CA="$stub/ca-crlf.pem" \
128+
"$SCRIPT" print 9002 main-pool |
129+
sed "s|/etc/ssl/certs/ca-certificates.crt|$stub/system.pem|" |
130+
PATH="$stub:$PATH" TMPDIR="$stub" sh >/dev/null
131+
grep -qE '^curl -fL --retry 3 --cacert [^ ]*/factory-ca.pem -o [^ ]*/disk.raw.xz https://factory.internal/' "$stub/calls"
132+
got="$(cat "$stub/cacert")"
133+
want="$(printf 'system bundle\n\n'; grep -v '^$' "$stub/ca.pem")"
134+
[ "$got" = "$want" ] || { printf 'curl was handed:\n%s\nexpected:\n%s\n' "$got" "$want" >&2; false; }
135+
rm -rf "$stub"
136+
}
137+
138+
@test "a factory CA file that is anything more than certificate blocks is refused" {
139+
stub="$(mktemp -d)"
140+
write_ca "$stub/ca.pem"
141+
{ cat "$stub/ca.pem"; printf '%s\n' '-----BEGIN PRIVATE KEY-----' 'MC4CAQAwBQYDK2VwBCIEIA==' '-----END PRIVATE KEY-----'; } > "$stub/with-key.pem"
142+
{ cat "$stub/ca.pem"; echo 'echo INJECTED'; } > "$stub/trailing.pem"
143+
printf '%s\n' '-----BEGIN CERTIFICATE-----' 'MIIBszCCAVmgAwIBAgIU' > "$stub/open.pem"
144+
printf '%s\n' '-----BEGIN CERTIFICATE-----' '-----END CERTIFICATE-----' > "$stub/no-body.pem"
145+
: > "$stub/empty.pem"
146+
for ca in with-key.pem trailing.pem open.pem no-body.pem empty.pem missing.pem .; do
147+
rc=0
148+
out="$(COZY_TALOS_FACTORY_URL=https://factory.internal COZY_TALOS_FACTORY_CA="$stub/$ca" "$SCRIPT" print 9002 main-pool 2>&1)" || rc=$?
149+
[ "$rc" -ne 0 ] || { echo "accepted: $ca" >&2; false; }
150+
case "$out" in *"set -eu"*) echo "printed a program for: $ca" >&2; false ;; esac
151+
done
152+
rc=0
153+
out="$(COZY_TALOS_FACTORY_URL=http://factory.internal COZY_TALOS_FACTORY_CA="$stub/ca.pem" "$SCRIPT" print 9002 main-pool 2>&1)" || rc=$?
154+
[ "$rc" -ne 0 ]
155+
case "$out" in *"is not https"*) ;; *) echo "$out" >&2; false ;; esac
156+
rm -rf "$stub"
157+
}
158+
96159
@test "a template already carrying the same tag set stops the program before the download" {
97160
stub="$(mktemp -d)"
98161
stub_pve "$stub"

0 commit comments

Comments
 (0)