From f778495bb472ef97eecc8c9e1a345624fa101a39 Mon Sep 17 00:00:00 2001 From: devops Date: Sat, 24 Oct 2026 13:19:17 +0000 Subject: [PATCH] feat(ci): add single-branch bounded Stage credential window --- .gitea/workflows/stage-v4-acceptance.yml | 30 +++++++++++ scripts/STAGE_V4_ACCEPTANCE.md | 31 ++++++++++++ scripts/ci-stage-acceptance.sh | 30 +++++++++++ scripts/ci-stage-test-principal.sh | 10 +++- scripts/test_ci_stage_acceptance.py | 63 ++++++++++++++++++++++++ 5 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 .gitea/workflows/stage-v4-acceptance.yml create mode 100644 scripts/STAGE_V4_ACCEPTANCE.md create mode 100644 scripts/ci-stage-acceptance.sh create mode 100644 scripts/test_ci_stage_acceptance.py diff --git a/.gitea/workflows/stage-v4-acceptance.yml b/.gitea/workflows/stage-v4-acceptance.yml new file mode 100644 index 00000000..c4dcb9e2 --- /dev/null +++ b/.gitea/workflows/stage-v4-acceptance.yml @@ -0,0 +1,30 @@ +name: Bounded Stage v4 credential acceptance window +on: + push: + branches: + - ci/v4-stage-acceptance +jobs: + acceptance-window: + if: github.ref == 'refs/heads/ci/v4-stage-acceptance' + runs-on: canina + timeout-minutes: 12 + steps: + - name: Open fixed Stage session and finally close (no model calls) + shell: bash + env: + BRANCH: ${{ github.ref_name }} + DEPLOY_SHA: ${{ github.sha }} + GITHUB_RUN_ATTEMPT: ${{ github.run_attempt }} + DEPLOY_KEY: ${{ secrets.DEPLOY_KEY }} + DEPLOY_KNOWN_HOSTS: ${{ secrets.DEPLOY_KNOWN_HOSTS }} + run: | + set -euo pipefail + set +x + [[ "$BRANCH" == ci/v4-stage-acceptance && "$DEPLOY_SHA" =~ ^[a-f0-9]{40}$ && "$GITHUB_RUN_ATTEMPT" == 1 ]] || exit 2 + umask 077 + scratch=$(mktemp -d) + trap 'rm -rf "$scratch"' EXIT + for file in ci-stage-acceptance.sh ci-stage-test-principal.sh; do + wget --no-proxy -qO "$scratch/$file" "https://git.parsaaghayi.ir/parsa/canina/raw/commit/$DEPLOY_SHA/scripts/$file" + done + bash "$scratch/ci-stage-acceptance.sh" diff --git a/scripts/STAGE_V4_ACCEPTANCE.md b/scripts/STAGE_V4_ACCEPTANCE.md new file mode 100644 index 00000000..e97551a3 --- /dev/null +++ b/scripts/STAGE_V4_ACCEPTANCE.md @@ -0,0 +1,31 @@ +# Bounded Stage credential window (not full acceptance) + +Activation: parent reviews this commit and pushes `ci/v4-stage-acceptance` once. +Only that exact branch activates `stage-v4-acceptance.yml`; develop/main pushes +cannot activate it. No deploy is performed. Workflow scripts are downloaded from +the triggering immutable SHA; the existing reviewed host principal entrypoint +is pinned separately to deployed Stage artifact +`f7055111648906a6a328c973c354c90bd559ef65`. Existing host worker verifies the actual +container artifact SHA before opening and requires the durable cleanup timer. + +Fixed grant: `owner-authorized-stage-editorial`, session +`v4-ci-acceptance-20261024`, TTL 600 seconds. The driver waits 480 seconds for the +parent's authenticated HTTP acceptance, then closes in EXIT; partial/failed opens +also attempt close. Runner loss retains the already-reviewed durable host cleanup +journal/timer. CI reruns are rejected before opening. Do not push this branch +again or retry a failed paid orchestration attempt blindly. + +The existing CLI performs real persisted-account HTTP login and writes its private +handle in the Stage backend at +`/app/private-stage-test/v4-ci-acceptance-20261024.json`. Never print/copy this +handle into CI artifacts or stdout; use its token only process-locally for actual +authenticated HTTP tests. This workflow does NOT execute orchestration, create a +proposal, approve Gate1, run a model, or claim domain acceptance. + +Parent acceptance still required during the credential window: authenticated +recovery of stuck task `754d70d6-d4c5-4ebc-860b-c5c7584201a0` using its actual +runID; a new PMO proposal and independent Gate1 budget 20; runner as hermes; +artifact readback. Preserve the unknown prior 20. At most one additional bounded +attempt; stop on failure (including competitor timeout). No Gate2, Production, +cron, self-review, backend-role bypass or forged JWT. Read back cleanup after the +window; CI success proves credential-window lifecycle only, NOT acceptance. diff --git a/scripts/ci-stage-acceptance.sh b/scripts/ci-stage-acceptance.sh new file mode 100644 index 00000000..404cd140 --- /dev/null +++ b/scripts/ci-stage-acceptance.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash +# One bounded credential window only; no deploy, model call or orchestration mutation. +set -euo pipefail +set +x +[[ $# == 0 && ${GITHUB_ACTIONS:-${GITEA_ACTIONS:-}} == true && ${BRANCH:-} == ci/v4-stage-acceptance && ${GITHUB_RUN_ATTEMPT:-} == 1 && ${DEPLOY_SHA:-} =~ ^[a-f0-9]{40}$ ]] || exit 2 +# Separate workflow source SHA from the immutable, already-deployed Stage artifact. +export DEPLOY_SHA=f7055111648906a6a328c973c354c90bd559ef65 +export STAGE_TEST_APPROVAL=owner-authorized-stage-editorial +export STAGE_TEST_SESSION=v4-ci-acceptance-20261024 +export STAGE_TEST_TTL_SECONDS=600 +wrapper="$(dirname "${BASH_SOURCE[0]}")/ci-stage-test-principal.sh" +close() { + rc=$? + trap - EXIT HUP INT TERM + export STAGE_TEST_ACTION=close + if ! bash "$wrapper"; then + printf 'Stage close pending: durable 600-second cleanup intent retained\n' >&2 + rc=1 + fi + exit "$rc" +} +# Install before open: failed/partial open must also attempt generation-fenced close. +trap close EXIT +trap 'exit 130' INT +trap 'exit 143' TERM HUP +export STAGE_TEST_ACTION=open +bash "$wrapper" +printf 'Stage credential window opened; authenticated acceptance NOT performed by this workflow\n' +# Keep the reviewed principal available for parent HTTP tests; timer is a hard backstop. +sleep 480 diff --git a/scripts/ci-stage-test-principal.sh b/scripts/ci-stage-test-principal.sh index 7e6ab227..d2c87bd2 100644 --- a/scripts/ci-stage-test-principal.sh +++ b/scripts/ci-stage-test-principal.sh @@ -3,7 +3,15 @@ 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' +[[ ${GITHUB_ACTIONS:-${GITEA_ACTIONS:-}} == true && $# == 0 ]] || fail 'CI-only Stage operation' +case ${BRANCH:-} in + develop) ;; + ci/v4-stage-acceptance) + [[ ${DEPLOY_SHA:-} == f7055111648906a6a328c973c354c90bd559ef65 && ${STAGE_TEST_SESSION:-} == v4-ci-acceptance-20261024 && ${STAGE_TEST_TTL_SECONDS:-} == 600 ]] || fail 'fixed acceptance scope required' + ;; + *) fail 'Stage branch required; Production is blocked' ;; +esac +[[ ${STAGE_TEST_ACTION:-} != open || ${BRANCH:-} != ci/v4-stage-acceptance || ${GITHUB_RUN_ATTEMPT:-} == 1 ]] || fail 'acceptance reruns 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. diff --git a/scripts/test_ci_stage_acceptance.py b/scripts/test_ci_stage_acceptance.py new file mode 100644 index 00000000..5e6438a1 --- /dev/null +++ b/scripts/test_ci_stage_acceptance.py @@ -0,0 +1,63 @@ +import os +import pathlib +import subprocess +import tempfile +import unittest + +ROOT = pathlib.Path(__file__).resolve().parent +SHA = 'f7055111648906a6a328c973c354c90bd559ef65' + +class AcceptanceTests(unittest.TestCase): + def run_driver(self, branch='ci/v4-stage-acceptance', fail_open=False, attempt='1'): + with tempfile.TemporaryDirectory() as d: + p = pathlib.Path(d) + (p/'ssh').write_text('#!/bin/bash\nprintf "%s\\n" "${@: -1}" >> "$CALLS"\n[[ "$FAIL_OPEN" != true || "${@: -1}" != *" open" ]]\n') + (p/'ssh-keygen').write_text('#!/bin/bash\nexit 0\n') + (p/'sleep').write_text('#!/bin/bash\n[[ "$1" == 480 ]]\n') + for name in ('ssh','ssh-keygen','sleep'): + (p/name).chmod(0o755) + env = dict(os.environ, PATH=d+':'+os.environ['PATH'], GITHUB_ACTIONS='true', BRANCH=branch, + GITHUB_RUN_ATTEMPT=attempt, DEPLOY_SHA='a'*40, DEPLOY_KEY='NEVER-PRINT-KEY', + DEPLOY_KNOWN_HOSTS='test-host-pin', CALLS=str(p/'calls'), + FAIL_OPEN=str(fail_open).lower()) + result = subprocess.run(['bash',str(ROOT/'ci-stage-acceptance.sh')],env=env,capture_output=True,text=True) + calls = (p/'calls').read_text().splitlines() if (p/'calls').exists() else [] + self.assertNotIn('NEVER-PRINT-KEY',result.stdout+result.stderr) + return result,calls + + def test_fixed_stage_open_then_finally_close(self): + result,calls = self.run_driver() + self.assertEqual(result.returncode,0,result.stderr) + prefix = 'sudo -n env CI=true STAGE_TEST_APPROVAL=owner-authorized-stage-editorial bash /opt/services/canina/releases/'+SHA+'/scripts/stage-test-principal-host.sh '+SHA+' v4-ci-acceptance-20261024 600 ' + self.assertEqual(calls,[prefix+'open',prefix+'close']) + + def test_failed_open_still_closes(self): + result,calls = self.run_driver(fail_open=True) + self.assertNotEqual(result.returncode,0) + self.assertEqual(len(calls),2) + self.assertTrue(calls[0].endswith(' open')) + self.assertTrue(calls[1].endswith(' close')) + + def test_wrong_branch_and_rerun_have_no_host_effect(self): + for branch in ('main','develop','ci/other'): + result,calls = self.run_driver(branch=branch) + self.assertNotEqual(result.returncode,0) + self.assertEqual(calls,[]) + result,calls = self.run_driver(attempt='2') + self.assertNotEqual(result.returncode,0) + self.assertEqual(calls,[]) + + def test_transport_scope_and_pinned_ssh_contract(self): + text = (ROOT/'ci-stage-test-principal.sh').read_text() + for value in ('StrictHostKeyChecking=yes','IdentitiesOnly=yes','IdentityAgent=none','GlobalKnownHostsFile=/dev/null'): + self.assertIn(value,text) + for branch,sha,session,ttl in [('main',SHA,'v4-ci-acceptance-20261024','600'), + ('ci/v4-stage-acceptance','b'*40,'v4-ci-acceptance-20261024','600'), + ('ci/v4-stage-acceptance',SHA,'different-session','600'), + ('ci/v4-stage-acceptance',SHA,'v4-ci-acceptance-20261024','601')]: + env = dict(os.environ,GITHUB_ACTIONS='true',BRANCH=branch,DEPLOY_SHA=sha, + STAGE_TEST_SESSION=session,STAGE_TEST_TTL_SECONDS=ttl,STAGE_TEST_ACTION='open') + result = subprocess.run(['bash',str(ROOT/'ci-stage-test-principal.sh')],env=env,capture_output=True) + self.assertNotEqual(result.returncode,0) + +if __name__ == '__main__': unittest.main()