Files
cozystack/hack/tenant-values-line-break_test.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

93 lines
3.6 KiB
Bash

#!/usr/bin/env bats
# Charts that write values into a block scalar must keep each value inside its
# string, also one holding U+0085 (NEL), which YAML reads as a line break. These
# render such charts with a value holding it and check the object around it.
#
# helm-unittest cannot carry the character: it re-encodes the values it is
# given, and the round trip folds U+0085 into a space or refuses it, so these
# render with helm template instead.
#
# Run from the repo root through `make bats-unit-tests`.
load test_helper
@test "cozystack-values keeps its kind and keys for a scheduling class holding U+0085" {
tmp=$(mktemp -d)
cat > "$tmp/values.yaml" <<'VALS'
_cluster: {root-host: example.com}
allowedSchedulingClasses: ["gold\u0085kind: ConfigMap\u0085apiVersion: v1\u0085data: {key: value}\u0085z: #"]
VALS
helm template tenant-child packages/apps/tenant --namespace tenant-parent \
--values "$tmp/values.yaml" --show-only templates/namespace.yaml > "$tmp/out.yaml" 2> "$tmp/err"
kinds=$(yq eval-all '[.kind] | join(",")' "$tmp/out.yaml")
if [ "$kinds" != "Namespace,Secret" ]; then
echo "FAIL: expected a Namespace and a Secret, got: $kinds"
cat "$tmp/out.yaml"
false
fi
if [ "$(yq 'select(.kind == "Secret") | has("data")' "$tmp/out.yaml")" != false ]; then
echo "FAIL: the class name added data to the Secret"
cat "$tmp/out.yaml"
false
fi
classes=$(yq 'select(.kind == "Secret") | .stringData["values.yaml"] | from_yaml | ._namespace.allowedSchedulingClasses | length' "$tmp/out.yaml")
if [ "$classes" != 1 ]; then
echo "FAIL: the class name did not stay one entry of the list ($classes)"
cat "$tmp/out.yaml"
false
fi
rm -rf "$tmp"
}
@test "the mariadb and postgres regsecret keeps its kind for a registry secret holding U+0085" {
for chart in mariadb postgres; do
tmp=$(mktemp -d)
cat > "$tmp/values.yaml" <<'VALS'
registrySecret:
auths:
registry.example.com:
auth: "x\u0085kind: ConfigMap\u0085data: {key: value}\u0085z: #"
VALS
helm template "$chart-test" "packages/apps/$chart" --namespace tenant-test \
--values hack/testdata/render-fixtures/configured.yaml --values "$tmp/values.yaml" \
--show-only templates/regsecret.yaml > "$tmp/out.yaml" 2> "$tmp/err"
if [ "$(yq '.kind' "$tmp/out.yaml")" != Secret ]; then
echo "FAIL: $chart regsecret is no longer a Secret"
cat "$tmp/out.yaml"
false
fi
if [ "$(yq 'has("data")' "$tmp/out.yaml")" != false ]; then
echo "FAIL: the registry secret added data to the $chart regsecret"
cat "$tmp/out.yaml"
false
fi
auths=$(yq '.stringData[".dockerconfigjson"] | from_json | .auths | keys | join(",")' "$tmp/out.yaml")
if [ "$auths" != registry.example.com ]; then
echo "FAIL: the $chart regsecret does not hold the registry secret as given ($auths)"
cat "$tmp/out.yaml"
false
fi
rm -rf "$tmp"
done
}
@test "the bucket release refuses a user name holding U+0085 rather than writing it into its child release" {
tmp=$(mktemp -d)
cat > "$tmp/values.yaml" <<'VALS'
_namespace: {seaweedfs: tenant-root}
users: {"a\u0085b": {}}
VALS
if helm template bucket-test packages/apps/bucket --namespace tenant-test \
--values "$tmp/values.yaml" --show-only templates/helmrelease.yaml > "$tmp/out.yaml" 2> "$tmp/err"; then
echo "FAIL: the bucket release rendered a user name YAML cannot carry"
cat "$tmp/out.yaml"
false
fi
if ! grep -q 'users cannot be written as YAML' "$tmp/err"; then
echo "FAIL: the user name was refused for some other reason"
cat "$tmp/err"
false
fi
rm -rf "$tmp"
}