Files
cozystack/hack/kafka-migration-hook.bats
T
Myasnikov DaniilandTimofei Larkin 4b8eaffa53 security(apps): quote and validate tenant values in chart templates
Application values reach chart templates as strings, maps and lists,
and several templates rendered them as-is. Values are now quoted or
converted to their type where they are rendered, map keys used as
names or config keys are checked against what their target accepts,
hook Jobs read their data from environment variables, and values
handed to upstream charts are passed in the form those charts expect.

cozy-lib gains cozy-lib.yaml.toYaml and cozy-lib.yaml.fromYaml, which
fail the render on a value they cannot encode or decode instead of
returning an empty result. external-dns GoDaddy credentials move from
args to a Secret.

Values that were valid before keep rendering: NATS units, restic short
keep flags, MongoDB database names by MongoDB's own rule, uppercase and
IPv6 VPN hosts, bootbox hostnames that are subdomains, netmask
whitelists, bracketed IPv6 registries for the Talos installer, time
zones and intervals in the MongoDB backup schedule.

Co-authored-by: Timofei Larkin <lllamnyp@gmail.com>
Assisted-by: LLM
Signed-off-by: Myasnikov Daniil <myasnikovdaniil2001@gmail.com>
2026-12-03 10:03:17 +00:00

147 lines
4.8 KiB
Bash

#!/usr/bin/env bats
# Behavioural tests for the node-pool hook of packages/apps/kafka. The shell
# script is extracted from the rendered Job and executed against a stub
# kubectl, so what the hook actually applies, and whether a failed apply fails
# the Helm upgrade, are exercised for real. The helm-unittest cases in
# packages/apps/kafka/tests/migration-hook_test.yaml only match the script's
# source TEXT.
#
# Run from the repo root with make bats-unit-tests. Direct assertions and
# inline cleanup retain compatibility with cozytest. Its translator rewrites
# lone closing braces even inside heredocs, so the stub avoids shell functions
# and helper closing braces stay indented.
load test_helper
CHART=packages/apps/kafka
# render_script <out> [extra helm args…]
# The pool manifests reach the script through the Job's environment, so the
# script is preceded by an export of every literal env value of the container,
# as the Job runs it.
render_script() {
out=$1
shift
helm template test-kafka "$CHART" \
--namespace tenant-test \
--show-only templates/migration-hook.yaml "$@" > "$out.yaml" 2>"$out.err"
yq 'select(.kind == "Job") | .spec.template.spec.containers[0].args[0]' "$out.yaml" > "$out.script"
if [ ! -s "$out.script" ]; then
echo "FAIL: the chart rendered no node-pool Job script" >&2
grep -v 'found symbolic link in path' "$out.err" >&2 || true
return 1
fi
yq 'select(.kind == "Job") | .spec.template.spec.containers[0].env[] | select(has("value")) | "export " + .name + "=" + (.value | @sh)' "$out.yaml" > "$out"
cat "$out.script" >> "$out"
}
make_bin() {
mkdir -p "$1"
cat > "$1/kubectl" <<'STUB'
#!/bin/sh
echo "$*" >> "${KUBECTL_LOG:-/dev/null}"
case "$*" in
*"apply "*"-f -"*)
cat >> "${KUBECTL_LOG:-/dev/null}"
[ -n "${STUB_APPLY_FAIL:-}" ] && { echo "apiserver error" >&2; exit 1; }
exit 0
;;
esac
exit 0
STUB
chmod 0755 "$1/kubectl"
}
setup_case() {
tmp=$(mktemp -d) || return 1
render_script "$tmp/s.sh" "$@" || return 1
make_bin "$tmp/bin" || return 1
}
run_hook() {
rc=0
: > "$tmp/kubectl.log"
KUBECONFIG=/dev/null \
KUBECTL_LOG="$tmp/kubectl.log" \
STUB_APPLY_FAIL="${STUB_APPLY_FAIL:-}" \
PATH="$tmp/bin:$PATH" sh "$tmp/s.sh" > "$tmp/out" 2>&1 || rc=$?
[ -s "$tmp/kubectl.log" ] || return 1
}
@test "the hook applies the broker and the controller pool through the v1 API" {
setup_case
run_hook
[ "$rc" = 0 ]
applies=$(grep -c 'apply --openapi-patch=false -f -' "$tmp/kubectl.log")
[ "$applies" = 2 ]
pools=$(grep -c '^apiVersion: kafka.strimzi.io/v1$' "$tmp/kubectl.log")
[ "$pools" = 2 ]
grep -qE '^ *- broker$' "$tmp/kubectl.log"
grep -qE '^ *- controller$' "$tmp/kubectl.log"
if grep -qE 'v1beta2|strimzi.io/kraft|KafkaRebalance|(^| )(annotate|delete|patch|create) ' "$tmp/kubectl.log"; then
echo "FAIL: the hook did more than apply the two pools"
cat "$tmp/kubectl.log"
return 1
fi
rm -rf "$tmp"
}
@test "a storage class holding shell syntax and a line break reaches both pools as one quoted value" {
# The schema refuses this storageClass. The render after that skips the
# schema to show that the hook carries the value as data on its own: the
# shell does not expand it and it does not split into a field of its own.
tmp=$(mktemp -d)
value="x\$(touch $tmp/marker)\`touch $tmp/marker\`
deleteClaim: true"
if helm template test-kafka "$CHART" --namespace tenant-test \
--set-string "kafka.storageClass=$value" > /dev/null 2> "$tmp/schema"; then
echo "FAIL: the schema accepted a storageClass that is not a DNS name"
return 1
fi
if ! grep -q "/kafka/storageClass" "$tmp/schema"; then
echo "FAIL: the storageClass was refused for some other reason"
cat "$tmp/schema"
return 1
fi
render_script "$tmp/s.sh" --skip-schema-validation --set-string "kafka.storageClass=$value"
make_bin "$tmp/bin"
run_hook
[ "$rc" = 0 ]
if [ -e "$tmp/marker" ]; then
echo "FAIL: the substitution in the storageClass was expanded in the hook"
return 1
fi
classes=$(grep -cF "class: \"x\$(touch $tmp/marker)\`touch $tmp/marker\`\\ndeleteClaim: true\"" "$tmp/kubectl.log")
if [ "$classes" != 2 ]; then
echo "FAIL: the storageClass did not reach both pools as one quoted value ($classes)"
cat "$tmp/kubectl.log"
return 1
fi
if grep -qE '^ *deleteClaim: true' "$tmp/kubectl.log"; then
echo "FAIL: the storageClass added a field of its own to a pool"
cat "$tmp/kubectl.log"
return 1
fi
rm -rf "$tmp"
}
@test "a failed pool apply fails the hook" {
setup_case
STUB_APPLY_FAIL=1 run_hook
[ "$rc" != 0 ]
applies=$(grep -c 'apply --openapi-patch=false -f -' "$tmp/kubectl.log")
if [ "$applies" != 1 ]; then
echo "FAIL: the hook went on after the broker pool apply failed ($applies applies)"
cat "$tmp/kubectl.log"
return 1
fi
rm -rf "$tmp"
}