feat(stage): add opt-in bounded CI editorial test principal

This commit is contained in:
devops committed 2026-10-24 12:02:47 +00:00
1 parent ed77f87ab5
commit e6c1201bed
15 files changed
+340 -1

No files matched your search

+22
View File
@@ -0,0 +1,22 @@
#!/usr/bin/env bash
# CI job side only. Never execute host deploy logic in the runner filesystem.
set -euo pipefail
set +x
fail() { printf 'ERROR: %s\n' "$1" >&2; exit 2; }
[[ ${GITHUB_ACTIONS:-${GITEA_ACTIONS:-}} == true && ${BRANCH:-} == develop && $# == 0 ]] || fail 'CI-only Stage deployment; Production is blocked'
[[ ${DEPLOY_SHA:-} =~ ^[0-9a-f]{40}$ ]] || fail 'immutable DEPLOY_SHA required'
# Repository secret DEPLOY_KEY is the historical approved devops SSH credential.
# Never fall back to baked-in runner keys, agent forwarding or docker.sock.
[[ -n ${DEPLOY_KNOWN_HOSTS:-} ]] || fail 'provision DEPLOY_KNOWN_HOSTS: independently verified [87.248.133.138]:2234 host key; never use ssh-keyscan trust-on-first-use'
[[ -n ${DEPLOY_KEY:-} ]] || fail 'provision existing approved DEPLOY_KEY for devops@87.248.133.138:2234'
for tool in ssh ssh-keygen mktemp; do command -v "$tool" >/dev/null || fail "job context missing $tool"; done
umask 077
scratch=$(mktemp -d)
trap 'rm -rf "$scratch"' EXIT
printf '%s\n' "$DEPLOY_KNOWN_HOSTS" > "$scratch/known_hosts"
printf '%s\n' "$DEPLOY_KEY" > "$scratch/key"
unset DEPLOY_KEY DEPLOY_KNOWN_HOSTS
ssh-keygen -F '[87.248.133.138]:2234' -f "$scratch/known_hosts" >/dev/null || fail 'DEPLOY_KNOWN_HOSTS has no pinned deployment endpoint'
[[ ${STAGE_TEST_SESSION:-} =~ ^[a-z0-9-]{8,64}$ && ${STAGE_TEST_TTL_SECONDS:-} =~ ^[0-9]+$ && ${STAGE_TEST_ACTION:-} =~ ^(open|close)$ && ${STAGE_TEST_APPROVAL:-} == owner-authorized-stage-editorial ]] || fail 'explicit owner Stage test grant required'
[[ $STAGE_TEST_TTL_SECONDS -ge 60 && $STAGE_TEST_TTL_SECONDS -le 3600 ]] || exit 2
ssh -F /dev/null -T -p 2234 -i "$scratch/key" -o BatchMode=yes -o IdentitiesOnly=yes -o IdentityAgent=none -o StrictHostKeyChecking=yes -o UserKnownHostsFile="$scratch/known_hosts" -o GlobalKnownHostsFile=/dev/null -o ConnectTimeout=15 devops@87.248.133.138 "sudo -n env CI=true STAGE_TEST_APPROVAL=owner-authorized-stage-editorial bash /opt/services/canina/releases/$DEPLOY_SHA/scripts/stage-test-principal-host.sh $DEPLOY_SHA $STAGE_TEST_SESSION $STAGE_TEST_TTL_SECONDS $STAGE_TEST_ACTION"
+2
View File
@@ -88,6 +88,8 @@ for key, name in expected.items():
if config['networks']['traefik_public'].get('name') != 'traefik_public' or not config['networks']['traefik_public'].get('external'):
raise SystemExit('ERROR: Stage network identity mismatch')
backend = config['services']['backend_stage']
import os
backend['build'].setdefault('args', {})['STAGE_TEST_ARTIFACT_SHA'] = os.environ['DEPLOY_SHA']
if backend['container_name'] != 'canino_backend_stage' or config['services']['frontend_stage']['container_name'] != 'canino_frontend_stage':
raise SystemExit('ERROR: Stage container identity mismatch')
mounts = backend.get('volumes', [])
+27
View File
@@ -0,0 +1,27 @@
#!/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"