#!/usr/bin/env bash # Authenticated CI host-side command only. No production argument or dynamic container name. set -euo pipefail set +x [[ $# == 4 && $1 =~ ^[0-9a-f]{40}$ && $2 =~ ^[a-z0-9-]{8,64}$ && $3 =~ ^[0-9]+$ && $4 =~ ^(open|close)$ ]] || exit 2 [[ $3 -ge 60 && $3 -le 3600 && ${STAGE_TEST_APPROVAL:-} == owner-authorized-stage-editorial && ${CI:-} == true ]] || exit 2 sha=$1 session=$2 ttl=$3 action=$4 [[ ! -e /.dockerenv && $(id -u) == 0 ]] || exit 2 exec 9>/opt/services/canina/stage-test-principal.lock flock -n 9 || exit 2 [[ $(docker exec canino_backend_stage cat /app/STAGE_TEST_ARTIFACT_SHA) == "$sha" ]] || exit 2 [[ $(docker inspect canino_backend_stage --format '{{.Name}}') == /canino_backend_stage ]] || exit 2 run() { docker exec -e APP_ENV=stage -e CI=true -e STAGE_TEST_PRINCIPAL_ENABLED=true \ -e STAGE_TEST_APPROVAL=owner-authorized-stage-editorial -e STAGE_TEST_SHA="$sha" \ -e STAGE_TEST_SESSION="$session" -e STAGE_TEST_TTL_SECONDS="$ttl" \ canino_backend_stage node dist/auth/stage-test-principal.cli.js "$1" } if [[ $action == open ]]; then # Arm cleanup BEFORE creating the account. systemd keeps this independent of CI loss. script="/opt/services/canina/releases/$sha/scripts/stage-test-principal-host.sh" [[ -f $script && ! -L $script ]] || exit 2 systemd-run --quiet --unit="canina-stage-principal-$session" --on-active="${ttl}s" \ --setenv=CI=true --setenv=STAGE_TEST_APPROVAL=owner-authorized-stage-editorial \ /bin/bash "$script" "$sha" "$session" "$ttl" close fi run "$action"