|
| 1 | +#!/usr/bin/env bats |
| 2 | +# ----------------------------------------------------------------------------- |
| 3 | +# packages/apps/kubernetes: the OIDC bootstrap Job must not carry the users[] |
| 4 | +# list inside its container command. |
| 5 | +# |
| 6 | +# The Job's script reaches the container as one `sh -c` argument, and Linux caps |
| 7 | +# one exec argument at 128 KiB (MAX_ARG_STRLEN). With a ClusterRoleBinding per |
| 8 | +# user written into that script, about 240 users take it past the cap: the |
| 9 | +# container cannot start, the post-install/post-upgrade hook fails, and the |
| 10 | +# tenant cluster's HelmRelease stays unready. The bindings reach the Job through |
| 11 | +# a mounted ConfigMap instead, so the script is the same size for any list. |
| 12 | +# |
| 13 | +# The last three tests execute the rendered script against a fake kubectl, since |
| 14 | +# no assertion on the script's text says what the prune step does with the list: |
| 15 | +# a condition forced either way passes every helm-unittest case. |
| 16 | +# |
| 17 | +# Requires: helm, yq (mikefarah v4+), jq, like hack/admin-kubeconfig-invariant.bats. |
| 18 | +# ----------------------------------------------------------------------------- |
| 19 | + |
| 20 | +load test_helper |
| 21 | + |
| 22 | +USERS=300 |
| 23 | + |
| 24 | +# render_users <dir> <count> renders the chart in System mode with <count> |
| 25 | +# generated users and leaves one JSON document per object in <dir>/rendered.json. |
| 26 | +render_users() { |
| 27 | + jq -n --argjson n "$2" ' |
| 28 | + {oidc: {users: [range($n) | {email: "user\(.)@example.org", role: (if . % 7 == 0 then "admin" else "view" end)}]}}' \ |
| 29 | + > "$1/users.yaml" |
| 30 | + helm template bindings packages/apps/kubernetes \ |
| 31 | + --namespace tenant-test \ |
| 32 | + --values packages/apps/kubernetes/tests/values/oidc-system.yaml \ |
| 33 | + --values "$1/users.yaml" \ |
| 34 | + > "$1/rendered.yaml" |
| 35 | + yq --output-format=json eval-all '.' "$1/rendered.yaml" > "$1/rendered.json" |
| 36 | +} |
| 37 | + |
| 38 | +# render_job <dir> <users JSON> renders the chart in CustomConfig mode and unpacks |
| 39 | +# what the bootstrap Job runs and mounts into <dir>: script.sh is its `sh -c` |
| 40 | +# script, bindings/ holds the two files of the mounted ConfigMap. -j keeps an |
| 41 | +# empty value an empty file, as the kubelet writes it. |
| 42 | +render_job() { |
| 43 | + jq -n --argjson users "$2" ' |
| 44 | + {_cluster: {"oidc-enabled": "false"}, |
| 45 | + oidc: {mode: "CustomConfig", |
| 46 | + customConfig: {config: "apiVersion: apiserver.config.k8s.io/v1beta1\nkind: AuthenticationConfiguration\njwt: []\n"}, |
| 47 | + users: $users}}' > "$1/values.yaml" |
| 48 | + helm template bindings packages/apps/kubernetes \ |
| 49 | + --namespace tenant-test \ |
| 50 | + --values packages/apps/kubernetes/tests/values/oidc-system.yaml \ |
| 51 | + --values "$1/values.yaml" \ |
| 52 | + > "$1/rendered.yaml" |
| 53 | + yq --output-format=json eval-all '.' "$1/rendered.yaml" > "$1/rendered.json" |
| 54 | + jq -s -j '.[] | select(.kind == "Job" and .metadata.name == "bindings-oidc-bootstrap") |
| 55 | + | .spec.template.spec.containers[0].command[2]' "$1/rendered.json" > "$1/script.sh" |
| 56 | + mkdir -p "$1/bindings" |
| 57 | + jq -s -j '.[] | select(.kind == "ConfigMap" and .metadata.name == "bindings-oidc-bindings") |
| 58 | + | .data["crbs.yaml"]' "$1/rendered.json" > "$1/bindings/crbs.yaml" |
| 59 | + jq -s -j '.[] | select(.kind == "ConfigMap" and .metadata.name == "bindings-oidc-bindings") |
| 60 | + | .data["desired.txt"]' "$1/rendered.json" > "$1/bindings/desired.txt" |
| 61 | +} |
| 62 | + |
| 63 | +# fake_kubectl <dir> <names> puts a kubectl first on PATH that logs every call and |
| 64 | +# answers the script's list of existing bindings with <names>. |
| 65 | +fake_kubectl() { |
| 66 | + mkdir -p "$1/bin" |
| 67 | + printf '%s' "$2" > "$1/existing" |
| 68 | + cat > "$1/bin/kubectl" <<'KEOF' |
| 69 | +#!/bin/sh |
| 70 | +echo "$*" >> "$KLOG" |
| 71 | +case "$*" in |
| 72 | + *"get clusterrolebindings"*) cat "$EXISTING" ;; |
| 73 | +esac |
| 74 | +KEOF |
| 75 | + chmod +x "$1/bin/kubectl" |
| 76 | +} |
| 77 | + |
| 78 | +# run_job <dir> runs the unpacked script as the container does and leaves its |
| 79 | +# status in $rc and its output in <dir>/out. Run by bash, which has the pipefail |
| 80 | +# the script sets and which is what the unit lane runs under; the container's own |
| 81 | +# shell is not exercised. |
| 82 | +run_job() { |
| 83 | + : > "$1/kubectl.log" |
| 84 | + echo kubeconfig > "$1/kubeconfig" |
| 85 | + rc=0 |
| 86 | + env PATH="$1/bin:$PATH" KLOG="$1/kubectl.log" EXISTING="$1/existing" HOME="$1" \ |
| 87 | + KUBECONFIG_TENANT="$1/kubeconfig" RELEASE=bindings RELEASE_NAMESPACE=tenant-test \ |
| 88 | + OIDC_MODE=CustomConfig OIDC_BINDINGS_DIR="$1/bindings" \ |
| 89 | + bash "$1/script.sh" > "$1/out" 2>&1 || rc=$? |
| 90 | +} |
| 91 | + |
| 92 | +@test "the OIDC bootstrap Job script stays under the exec argument cap and carries no user" { |
| 93 | + dir=$(mktemp -d) |
| 94 | + render_users "$dir" "$USERS" |
| 95 | + jq -s -r '.[] | select(.kind == "Job" and .metadata.name == "bindings-oidc-bootstrap") |
| 96 | + | .spec.template.spec.containers[0].command[2]' "$dir/rendered.json" > "$dir/script" |
| 97 | + size=$(wc -c < "$dir/script") |
| 98 | + [ "$size" -gt 0 ] |
| 99 | + [ "$size" -lt 131072 ] |
| 100 | + if grep -q -E 'user[0-9]+@example\.org' "$dir/script"; then |
| 101 | + echo "the Job script still carries users[] entries" >&2 |
| 102 | + false |
| 103 | + fi |
| 104 | + rm -rf "$dir" |
| 105 | +} |
| 106 | + |
| 107 | +@test "every user keeps one ClusterRoleBinding with its old name and role in the bindings ConfigMap" { |
| 108 | + dir=$(mktemp -d) |
| 109 | + render_users "$dir" "$USERS" |
| 110 | + jq -s -r '.[] | select(.kind == "ConfigMap" and .metadata.name == "bindings-oidc-bindings") |
| 111 | + | .data["crbs.yaml"]' "$dir/rendered.json" > "$dir/crbs.yaml" |
| 112 | + jq -s -r '.[] | select(.kind == "ConfigMap" and .metadata.name == "bindings-oidc-bindings") |
| 113 | + | .data["desired.txt"]' "$dir/rendered.json" > "$dir/desired.txt" |
| 114 | + [ -s "$dir/crbs.yaml" ] |
| 115 | + yq --output-format=json eval-all '.' "$dir/crbs.yaml" | jq -s '.' > "$dir/crbs.json" |
| 116 | + |
| 117 | + [ "$(jq 'length' "$dir/crbs.json")" -eq "$USERS" ] |
| 118 | + [ "$(jq -r '[.[].subjects[0].name] | unique | length' "$dir/crbs.json")" -eq "$USERS" ] |
| 119 | + [ "$(jq -r '[.[] | select(.roleRef.name == "cluster-admin")] | length' "$dir/crbs.json")" -eq $(( (USERS + 6) / 7 )) ] |
| 120 | + |
| 121 | + # The name is what the prune step and every binding already in a tenant cluster |
| 122 | + # go by, so it must not move: cozystack-oidc- plus the first 16 hex digits of |
| 123 | + # sha256("<release>-<email>"). |
| 124 | + for i in 0 1 6 7; do |
| 125 | + email="user$i@example.org" |
| 126 | + name="cozystack-oidc-$(printf '%s' "bindings-$email" | sha256sum | cut -c1-16)" |
| 127 | + [ "$(jq -r --arg e "$email" '.[] | select(.subjects[0].name == $e) | .metadata.name' "$dir/crbs.json")" = "$name" ] |
| 128 | + grep -q -x -F "$name" "$dir/desired.txt" |
| 129 | + done |
| 130 | + |
| 131 | + # The list the prune step compares against is exactly the applied names. |
| 132 | + [ "$(jq -r '.[].metadata.name' "$dir/crbs.json" | sort)" = "$(grep . "$dir/desired.txt" | sort)" ] |
| 133 | + rm -rf "$dir" |
| 134 | +} |
| 135 | + |
| 136 | +@test "the Job applies the mounted bindings and deletes only the bindings no user has" { |
| 137 | + dir=$(mktemp -d) |
| 138 | + render_job "$dir" '[{"email":"[email protected]","role":"admin"},{"email":"[email protected]","role":"view"}]' |
| 139 | + alice= "cozystack-oidc-$(printf '%s' '[email protected]' | sha256sum | cut -c1-16)" |
| 140 | + bob= "cozystack-oidc-$(printf '%s' '[email protected]' | sha256sum | cut -c1-16)" |
| 141 | + fake_kubectl "$dir" "$alice cozystack-oidc-000000000000dead $bob cozystack-oidc-000000000000beef" |
| 142 | + run_job "$dir" |
| 143 | + if [ "$rc" -ne 0 ]; then |
| 144 | + cat "$dir/out" |
| 145 | + false |
| 146 | + fi |
| 147 | + grep -q -F -- "apply --filename $dir/bindings/crbs.yaml" "$dir/kubectl.log" |
| 148 | + grep -q -F -- "delete clusterrolebinding cozystack-oidc-000000000000dead --ignore-not-found" "$dir/kubectl.log" |
| 149 | + grep -q -F -- "delete clusterrolebinding cozystack-oidc-000000000000beef --ignore-not-found" "$dir/kubectl.log" |
| 150 | + if grep -F -e "delete clusterrolebinding $alice" -e "delete clusterrolebinding $bob" "$dir/kubectl.log"; then |
| 151 | + echo "the Job deleted a binding a user still has" >&2 |
| 152 | + false |
| 153 | + fi |
| 154 | + [ "$(grep -c 'delete clusterrolebinding' "$dir/kubectl.log")" -eq 2 ] |
| 155 | + rm -rf "$dir" |
| 156 | +} |
| 157 | + |
| 158 | +@test "a release with no users applies nothing and deletes every binding it left behind" { |
| 159 | + dir=$(mktemp -d) |
| 160 | + render_job "$dir" '[]' |
| 161 | + fake_kubectl "$dir" "cozystack-oidc-000000000000dead cozystack-oidc-000000000000beef" |
| 162 | + run_job "$dir" |
| 163 | + if [ "$rc" -ne 0 ]; then |
| 164 | + cat "$dir/out" |
| 165 | + false |
| 166 | + fi |
| 167 | + if grep -q -F -- ' apply ' "$dir/kubectl.log"; then |
| 168 | + echo "the Job ran kubectl apply with no bindings to apply" >&2 |
| 169 | + false |
| 170 | + fi |
| 171 | + [ "$(grep -c 'delete clusterrolebinding' "$dir/kubectl.log")" -eq 2 ] |
| 172 | + rm -rf "$dir" |
| 173 | +} |
| 174 | + |
| 175 | +@test "the Job stops before deleting anything when the desired list is not mounted" { |
| 176 | + dir=$(mktemp -d) |
| 177 | + render_job "$dir" '[{"email":"[email protected]","role":"admin"}]' |
| 178 | + fake_kubectl "$dir" "cozystack-oidc-000000000000dead cozystack-oidc-000000000000beef" |
| 179 | + rm "$dir/bindings/desired.txt" |
| 180 | + run_job "$dir" |
| 181 | + [ "$rc" -ne 0 ] |
| 182 | + if grep -q -E 'delete clusterrolebinding| apply ' "$dir/kubectl.log"; then |
| 183 | + cat "$dir/kubectl.log" |
| 184 | + false |
| 185 | + fi |
| 186 | + rm -rf "$dir" |
| 187 | +} |
0 commit comments