diff --git a/.github/workflows/e2e-proxmox.yaml b/.github/workflows/e2e-proxmox.yaml index b5b19756d..00fd1a384 100644 --- a/.github/workflows/e2e-proxmox.yaml +++ b/.github/workflows/e2e-proxmox.yaml @@ -49,11 +49,6 @@ jobs: COZY_PVE_SSH: ${{ vars.PROXMOX_SSH }} COZY_PVE_SSH_KEY: ${{ github.workspace }}/.ssh-pve COZY_KEEP_TENANT: ${{ inputs.keep_tenant == true }} - # Where run-proxmox.sh keeps the tenant's super-admin kubeconfig for the - # run. runner.temp is emptied by the runner between jobs, and the cleanup - # step below removes the file as well; see the script for why neither is - # left to /tmp. - COZY_PROXMOX_WORKDIR: ${{ runner.temp }} steps: - name: Checkout uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4 @@ -162,6 +157,8 @@ jobs: - name: Remove staged credentials if: always() - # The tenant kubeconfig is the suite's, fetched by run-proxmox.sh; its - # finally removes it too, but a run killed mid-step never reaches that. - run: rm -f "$GITHUB_WORKSPACE/.kubeconfig" "$COZY_PVE_SSH_KEY" "$COZY_PROXMOX_WORKDIR/proxmox-e2e-tenant.kubeconfig" + # The tenant kubeconfig is the suite's, fetched by run-proxmox.sh into + # RUNNER_TEMP, which the runner exports into every step (the runner + # context is not allowed in job-level env); its finally removes it too, + # but a run killed mid-step never reaches that. + run: rm -f "$GITHUB_WORKSPACE/.kubeconfig" "$COZY_PVE_SSH_KEY" "$RUNNER_TEMP/proxmox-e2e-tenant.kubeconfig" diff --git a/hack/e2e-chainsaw/_lib/run-proxmox.sh b/hack/e2e-chainsaw/_lib/run-proxmox.sh index 4191c4418..6916edd44 100755 --- a/hack/e2e-chainsaw/_lib/run-proxmox.sh +++ b/hack/e2e-chainsaw/_lib/run-proxmox.sh @@ -26,9 +26,10 @@ STORAGE="${COZY_PROXMOX_STORAGE:?COZY_PROXMOX_STORAGE is required, the PVE stora # a fixed path under /tmp: the file is a credential for the whole tenant, so it # must not outlive the run on a shared runner, and dump_diagnostics reads # whatever is at this path, so a file left by a previous run would be reported -# as this run's tenant. The workflow points this at runner.temp, which the -# runner empties between jobs; apply-tenant removes a leftover before it does -# anything else, and the suite's finally removes the file when the run ends. +# as this run's tenant. On the workflow's runner this is RUNNER_TEMP, which the +# runner exports into every step and empties between jobs; apply-tenant removes +# a leftover before it does anything else, and the suite's finally removes the +# file when the run ends. WORKDIR="${COZY_PROXMOX_WORKDIR:-${RUNNER_TEMP:-${TMPDIR:-/tmp}}}" TENANT_KUBECONFIG="$WORKDIR/proxmox-e2e-tenant.kubeconfig" FIXTURE="$(cd "$(dirname "$0")/.." && pwd)/kubernetes-proxmox/tenant.yaml" diff --git a/hack/proxmox-e2e-fixture_test.bats b/hack/proxmox-e2e-fixture_test.bats index 0d2df3e5a..4e78d8378 100755 --- a/hack/proxmox-e2e-fixture_test.bats +++ b/hack/proxmox-e2e-fixture_test.bats @@ -61,3 +61,63 @@ RUNNER="$REPO_ROOT/hack/e2e-chainsaw/_lib/run-proxmox.sh" rm -rf "$stub" case "$out" in *"is gone from main-pool"*) ;; *) echo "$out" >&2; exit 1 ;; esac } + +# The tenant's super-admin kubeconfig is a credential for the whole tenant, kept +# under RUNNER_TEMP for one run. The workflow sets no path of its own, so these +# cases go through the same RUNNER_TEMP fallback the runner provides, with the +# override cleared. +KUBECONFIG_NAME=proxmox-e2e-tenant.kubeconfig +SUITE="$REPO_ROOT/hack/e2e-chainsaw/kubernetes-proxmox/chainsaw-test.yaml.disabled" + +@test "tenant-node writes the tenant kubeconfig without group or other bits under umask 022" { + work="$(mktemp -d)" + stub="$(mktemp -d)" + # The secret read answers a kubeconfig and the first tenant read fails, which + # ends the check right after the fetch with the file in place. + cat > "$stub/kubectl" <<'STUB' +#!/bin/sh +case "$*" in *"get secret"*) printf '%s' YXBpVmVyc2lvbjogdjEK; exit 0 ;; esac +exit 1 +STUB + chmod +x "$stub/kubectl" + rc=0 + (umask 022; PATH="$stub:$PATH" RUNNER_TEMP="$work" COZY_PROXMOX_WORKDIR= COZY_PVE_SSH=x COZY_PROXMOX_STORAGE=s "$RUNNER" tenant-node) >/dev/null 2>&1 || rc=$? + mode="$(ls -l "$work/$KUBECONFIG_NAME" | cut -c1-10)" + rm -rf "$work" "$stub" + [ "$rc" -ne 0 ] + [ "$mode" = "-rw-------" ] || { echo "FAIL: the tenant kubeconfig was written $mode" >&2; exit 1; } +} + +@test "apply-tenant removes a tenant kubeconfig an earlier run left behind" { + work="$(mktemp -d)" + stub="$(mktemp -d)" + printf 'an earlier run\n' > "$work/$KUBECONFIG_NAME" + printf '#!/bin/sh\ncat >/dev/null\n' > "$stub/kubectl" + chmod +x "$stub/kubectl" + PATH="$stub:$PATH" RUNNER_TEMP="$work" COZY_PROXMOX_WORKDIR= COZY_PVE_SSH=x COZY_PROXMOX_STORAGE=s "$RUNNER" apply-tenant + left=no + [ ! -e "$work/$KUBECONFIG_NAME" ] || left=yes + rm -rf "$work" "$stub" + [ "$left" = no ] || { echo "FAIL: apply-tenant left the earlier run's kubeconfig in place" >&2; exit 1; } +} + +@test "the suite's finally removes the tenant kubeconfig, so diagnostics after it prints no tenant section" { + # keep_tenant exits the finally before its deletes, and the kubeconfig has to + # be gone on that path as well. Diagnostics reads whatever is at the path, so + # a file left there would be reported as the next run's tenant. + work="$(mktemp -d)" + stub="$(mktemp -d)" + printf 'this run\n' > "$work/$KUBECONFIG_NAME" + printf '#!/bin/sh\necho "stub $*"\n' > "$stub/kubectl" + printf '#!/bin/sh\nexit 0\n' > "$stub/ssh" + chmod +x "$stub/kubectl" "$stub/ssh" + finally="$(yq -r '.spec.steps[] | select(.name == "replacement-converges-unattended") | .finally[0].script.content' "$SUITE")" + [ -n "$finally" ] + PATH="$stub:$PATH" RUNNER_TEMP="$work" COZY_PROXMOX_WORKDIR= COZY_KEEP_TENANT=true sh -c "$finally" >/dev/null + out="$(PATH="$stub:$PATH" RUNNER_TEMP="$work" COZY_PROXMOX_WORKDIR= COZY_PVE_SSH=x COZY_PROXMOX_STORAGE=s "$RUNNER" diagnostics 2>&1)" + left=no + [ ! -e "$work/$KUBECONFIG_NAME" ] || left=yes + rm -rf "$work" "$stub" + [ "$left" = no ] || { echo "FAIL: the suite's finally left the tenant kubeconfig in place" >&2; exit 1; } + case "$out" in *"tenant nodes and pods"*) echo "$out" >&2; exit 1 ;; esac +}