#!/usr/bin/env bats # hack/values-injection on charts built for the purpose: a value rendered raw, # the same value quoted, and a quoted value that carries its line breaks into # an embedded config, which is what a field multi-line by design looks like. # Pinned here: what fails the check, what does not, and what the allowlist # can and cannot excuse. The comparison itself is covered by the Go tests # beside the check. # # Needs helm and go. Run from the repo root through `make bats-unit-tests`. load test_helper # Build once per file: the tests run in parallel under `bats -j`. setup_file() { go -C hack/values-injection build -o "$BATS_FILE_TMPDIR/values-injection" . } # make_chart make_chart() { dir="$1/packages/apps/$2" mkdir -p "$dir/templates" printf 'apiVersion: v2\nname: %s\nversion: 0.0.0\n' "$2" > "$dir/Chart.yaml" printf 'v: x\n' > "$dir/values.yaml" printf '{"type": "object", "properties": {"v": {"type": "string", "default": "x"}}}\n' > "$dir/values.schema.json" printf 'apiVersion: v1\nkind: ConfigMap\nmetadata:\n name: probe\ndata:\n %s\n' "$3" > "$dir/templates/configmap.yaml" } # check [chart dir…]: sets rc and out. check() { root=$1 allow=$2 shift 2 rc=0 out=$("$BATS_FILE_TMPDIR/values-injection" --root "$root" --allowlist "$allow" "$@" 2>&1) || rc=$? } @test "a value rendered raw is injectable and fails the check, the same value quoted passes" { tmp=$(mktemp -d) make_chart "$tmp" raw 'v: {{ .Values.v }}' make_chart "$tmp" quoted 'v: {{ .Values.v | quote }}' : > "$tmp/allow" check "$tmp" "$tmp/allow" packages/apps/raw if [ "$rc" -ne 1 ]; then echo "FAIL: the check exited $rc on a value rendered raw" printf '%s\n' "$out" false fi if ! printf '%s\n' "$out" | grep -q '^INJECTABLE packages/apps/raw: v$'; then echo "FAIL: the raw value was not reported injectable" printf '%s\n' "$out" false fi check "$tmp" "$tmp/allow" packages/apps/quoted if [ "$rc" -ne 0 ]; then echo "FAIL: the check exited $rc on a quoted value" printf '%s\n' "$out" false fi if ! printf '%s\n' "$out" | grep -qx 'fields: safe=1'; then echo "FAIL: the quoted value was not counted safe" printf '%s\n' "$out" false fi rm -rf "$tmp" } @test "a multi-line value passes only when listed, and a listed field that is not breakable fails" { tmp=$(mktemp -d) make_chart "$tmp" multiline 'conf: {{ printf "[main]\na = %s\n" .Values.v | quote }}' make_chart "$tmp" quoted 'v: {{ .Values.v | quote }}' : > "$tmp/none" check "$tmp" "$tmp/none" packages/apps/multiline if [ "$rc" -ne 1 ] || ! printf '%s\n' "$out" | grep -q '^BREAKABLE packages/apps/multiline: v$'; then echo "FAIL: an unlisted multi-line value did not fail the check as breakable (exit $rc)" printf '%s\n' "$out" false fi printf '# reason required\npackages/apps/multiline v an embedded config written whole\n' > "$tmp/allow" check "$tmp" "$tmp/allow" packages/apps/multiline if [ "$rc" -ne 0 ] || ! printf '%s\n' "$out" | grep -q '^ALLOWED packages/apps/multiline: v (an embedded config written whole)$'; then echo "FAIL: a listed multi-line value was not allowed (exit $rc)" printf '%s\n' "$out" false fi printf 'packages/apps/multiline v an embedded config written whole\npackages/apps/quoted v once multi-line\n' > "$tmp/allow" check "$tmp" "$tmp/allow" packages/apps/multiline packages/apps/quoted if [ "$rc" -ne 1 ] || ! printf '%s\n' "$out" | grep -q 'STALE .*packages/apps/quoted v is safe, not breakable$'; then echo "FAIL: an entry for a field that is safe did not fail the check (exit $rc)" printf '%s\n' "$out" false fi rm -rf "$tmp" } @test "the allowlist does not excuse an injectable field" { tmp=$(mktemp -d) make_chart "$tmp" raw 'v: {{ .Values.v }}' printf 'packages/apps/raw v listed by mistake\n' > "$tmp/allow" check "$tmp" "$tmp/allow" packages/apps/raw if [ "$rc" -ne 1 ] || ! printf '%s\n' "$out" | grep -q '^INJECTABLE packages/apps/raw: v$'; then echo "FAIL: a listed injectable field did not fail the check (exit $rc)" printf '%s\n' "$out" false fi rm -rf "$tmp" }