#!/usr/bin/env bash set -euo pipefail ROOT="$(cd "$(dirname "$0")/../.." && pwd)" source "$ROOT/tests/kind/lib.sh" USER_KUBECONFIG_BEFORE="$(user_kubeconfig_digest)" SUCCEEDED=false OWNS_FIXTURE=false APP_IMAGE=nuthatch-auth:local cleanup() { local status=$? trap - EXIT INT TERM if [[ "${NUTHATCH_TESTBED_RETAIN:-0}" = 1 ]] && [[ "$SUCCEEDED" != true ]]; then printf 'retained kubeconfig: %s\n' "$KUBECONFIG_PATH" printf 'retained context: %s\n' "$CONTEXT" exit "$status" fi if [[ "$OWNS_FIXTURE" = true ]]; then destroy_fixture || status=1 fi if [[ "$(user_kubeconfig_digest)" != "$USER_KUBECONFIG_BEFORE" ]]; then printf 'user kubeconfig changed unexpectedly\n' >&2 status=1 fi exit "$status" } trap cleanup EXIT trap 'exit 130' INT trap 'exit 143' TERM render_application() { local lane="$1" local profile_file="$2" local oidc_secret oidc_secret="$(<"$profile_file.oidc-secret")" sed \ -e "s|@@LANE@@|$lane|g" \ -e "s|@@APP_IMAGE@@|$APP_IMAGE|g" \ -e "s|@@PG_CONN@@|$(sed -n 's/^NUTHATCH_PG_CONN=//p' "$profile_file")|g" \ -e "s|@@REDIS_URL@@|$(sed -n 's/^NUTHATCH_REDIS_URL=//p' "$profile_file")|g" \ -e "s|@@OIDC_ISSUER@@|$(sed -n 's/^NUTHATCH_OIDC_ISSUER=//p' "$profile_file")|g" \ -e "s|@@OIDC_CLIENT_ID@@|$(sed -n 's/^NUTHATCH_OIDC_CLIENT_ID=//p' "$profile_file")|g" \ -e "s|@@OIDC_ADDITIONAL_AUDIENCES@@|$(sed -n 's/^NUTHATCH_OIDC_ADDITIONAL_AUDIENCES=//p' "$profile_file")|g" \ -e "s|@@OIDC_CLIENT_SECRET@@|$oidc_secret|g" \ -e "s|@@S3_ENDPOINT@@|$(sed -n 's/^NUTHATCH_S3_ENDPOINT=//p' "$profile_file")|g" \ -e "s|@@S3_REGION@@|$(sed -n 's/^NUTHATCH_S3_REGION=//p' "$profile_file")|g" \ -e "s|@@S3_BUCKET@@|$(sed -n 's/^NUTHATCH_S3_BUCKET=//p' "$profile_file")|g" \ -e "s|@@S3_ACCESS_KEY@@|$(sed -n 's/^NUTHATCH_S3_ACCESS_KEY=//p' "$profile_file")|g" \ -e "s|@@S3_SECRET_KEY@@|$(sed -n 's/^NUTHATCH_S3_SECRET_KEY=//p' "$profile_file")|g" \ "$ROOT/deploy/kubernetes/nuthatch.yaml.in" >"$RUNTIME/auth-app-$lane.yaml" } cleanup_application() { local lane="$1" kube --namespace "$FIXTURE_NAMESPACE" delete \ "deployment/nuthatch-$lane" \ "service/nuthatch-$lane" \ "configmap/nuthatch-$lane" \ "secret/nuthatch-$lane" \ --ignore-not-found >/dev/null rm -f "$RUNTIME/auth-app-$lane.yaml" } acquire_fixture_lock OWNS_FIXTURE=true setup_fixture durable coordination oidc target source "$RUNTIME/fixture.env" "$NUTHATCH_REAL_NERDCTL" --namespace "$NERDCTL_NAMESPACE" build \ --file "$ROOT/Dockerfile" \ --target test-runtime \ --tag "$APP_IMAGE" \ "$ROOT" load_local_image "$APP_IMAGE" "$NERDCTL_NAMESPACE" while IFS=$'\t' read -r lane coordination oidc target; do profile="$(write_lane_profile "$lane" "$coordination" "$oidc" "$target")" render_application "$lane" "$RUNTIME/$profile.env" kube apply --filename "$RUNTIME/auth-app-$lane.yaml" >/dev/null kube --namespace "$FIXTURE_NAMESPACE" rollout status \ "deployment/nuthatch-$lane" --timeout=180s if [[ "${NUTHATCH_APP_INTERACTIVE:-0}" = 1 ]]; then SUCCEEDED=true OWNS_FIXTURE=false printf 'interactive kubeconfig: %s\n' "$KUBECONFIG_PATH" printf 'interactive context: %s\n' "$CONTEXT" printf 'interactive namespace: %s\n' "$FIXTURE_NAMESPACE" printf 'interactive service: nuthatch-%s:8080\n' "$lane" exit 0 fi job="auth-app-$lane" render_lane_job \ "$job" \ "$TESTBED/application-probe-job.yaml.in" \ "$APP_IMAGE" \ "$profile" sed -i \ -e "s|@@LANE@@|$lane|g" \ -e 's|@@COMPONENT@@|auth|g' \ -e 's|@@PROBE@@|nuthatch-auth-probe|g' \ -e 's|@@PROBE_ARGS@@||g' \ -e 's|@@DEADLINE_SECONDS@@|300|g' \ "$RUNTIME/$job.yaml" printf 'Running auth lane %s: coordination=%s oidc=%s target=%s\n' \ "$lane" "$coordination" "$oidc" "$target" run_job_manifest "$job" "$RUNTIME/$job.yaml" cleanup_application "$lane" cleanup_lane_state "$lane" "$coordination" "$oidc" "$target" cleanup_lane_resources "$job" "$profile" done < <(expand_fixture_matrix coordination oidc target) SUCCEEDED=true printf 'Nuthatch authentication gate passed\n'