From c25b10f7f3fa963776d6c33fa569964a8198f8c1 Mon Sep 17 00:00:00 2001 From: devops Date: Sun, 25 Oct 2026 13:03:44 +0000 Subject: [PATCH] fix(ci): trigger approved auth schema operation on exact Gitea push refs --- .gitea/workflows/auth-schema-reconcile.yml | 31 +++++---- docs/auth-schema-reconciliation.md | 7 ++- scripts/test_auth_schema_push.py | 73 ++++++++++++++++++++++ 3 files changed, 94 insertions(+), 17 deletions(-) create mode 100644 scripts/test_auth_schema_push.py diff --git a/.gitea/workflows/auth-schema-reconcile.yml b/.gitea/workflows/auth-schema-reconcile.yml index e0b2af77..79f1509a 100644 --- a/.gitea/workflows/auth-schema-reconcile.yml +++ b/.gitea/workflows/auth-schema-reconcile.yml @@ -1,17 +1,9 @@ name: Approved narrow auth schema reconciliation on: - workflow_dispatch: - inputs: - environment: - description: 'stage or production (requires separate explicit schema approval)' - required: true - default: stage - sql_sha256: - description: 'Exact reviewed scripts/auth-schema-reconcile.sql SHA256' - required: true - schema_approval: - description: 'Approval record identifier; production requires separately signed authorization' - required: true + push: + branches: + - ops/auth-schema-stage + - ops/auth-schema-production jobs: reconcile: runs-on: canina @@ -19,9 +11,10 @@ jobs: - name: Authenticated host narrow schema operation (no deployment) shell: bash env: - TARGET_ENV: ${{ github.event.inputs.environment }} - SQL_SHA: ${{ github.event.inputs.sql_sha256 }} - APPROVAL_ID: ${{ github.event.inputs.schema_approval }} + CI_EVENT: ${{ github.event_name }} + CI_REF: ${{ github.ref }} + SQL_SHA: 8a22f3e89efb3dacc24f3ed9337ae87d0b4345da003a607bc1994478f69c6feb + APPROVAL_ID: CEO-AUTH-SCHEMA-20261025 DEPLOY_SHA: ${{ github.sha }} DEPLOY_KEY: ${{ secrets.DEPLOY_KEY }} DEPLOY_KNOWN_HOSTS: ${{ secrets.DEPLOY_KNOWN_HOSTS }} @@ -31,7 +24,12 @@ jobs: run: | set -euo pipefail set +x - [[ "$TARGET_ENV" == stage || "$TARGET_ENV" == production ]] + [[ "$CI_EVENT" == push ]] + case "$CI_REF" in + refs/heads/ops/auth-schema-stage) TARGET_ENV=stage ;; + refs/heads/ops/auth-schema-production) TARGET_ENV=production ;; + *) exit 2 ;; + esac [[ "$SQL_SHA" =~ ^[0-9a-f]{64}$ && "$DEPLOY_SHA" =~ ^[0-9a-f]{40}$ ]] [[ "$APPROVAL_ID" =~ ^[A-Za-z0-9_-]{4,100}$ ]] [[ "$REVIEWED_SCHEMA_COMMIT" == "$DEPLOY_SHA" ]] @@ -62,6 +60,7 @@ jobs: git -C "$work" fetch --quiet "$remote" "$sha" git -C "$work" checkout --quiet --detach "$sha" cd "$work" + [[ $(git rev-parse HEAD) == "$sha" ]] if [[ $target == production ]]; then [[ $4 == yes && $5 == yes ]] export PRODUCTION_SCHEMA_APPROVED="$sha:$sql_sha" STAGE_AUTH_ACCEPTANCE_APPROVED="$sha:$sql_sha" diff --git a/docs/auth-schema-reconciliation.md b/docs/auth-schema-reconciliation.md index 33323651..88a37bad 100644 --- a/docs/auth-schema-reconciliation.md +++ b/docs/auth-schema-reconciliation.md @@ -8,12 +8,17 @@ Read-only production PostgreSQL 16.14 catalogs verified all three users columns Historical principal migration at e036c5d has two columns, SHA256 8b4fe1fdedd2ae459b720f78479cfcb13da465eec821b422ece5e9bb41dc47ae; 80a2e18 edited it to three, SHA256 a458a94329b1ad078298b042ae0bd2e25439c1f0bb5a086beb55c517d9e0aa28. Read-only Stage ledger records the latter, not the former. Preserve both historical files unchanged here. This operation is separately journaled by CI/backup receipt, not invented Prisma history. Broader baseline/history repair remains separate work. ## Execution contract -Dedicated manual workflow `auth-schema-reconcile.yml` uses DEPLOY_KEY and pinned DEPLOY_KNOWN_HOSTS, fixed SSH endpoint, isolated immutable Git checkout; it does not deploy applications. It requires REVIEWED_AUTH_SCHEMA_COMMIT matching dispatch SHA. Production additionally requires PRODUCTION_AUTH_SCHEMA_APPROVAL = SHA:SQL_SHA256:APPROVAL_ID and STAGE_AUTH_SCHEMA_ACCEPTANCE = SHA:SQL_SHA256, provisioned only after real review and Stage auth acceptance. These are explicit approval records, not automatically issued receipts. The repository's workflow/secret permissions must restrict dispatch/ref mutation to trusted maintainers; unrestricted malicious workflow edits can bypass any workflow-local gate. Review/protect this boundary before provisioning production approval. +Dedicated push workflow `auth-schema-reconcile.yml` uses DEPLOY_KEY and pinned DEPLOY_KNOWN_HOSTS, fixed SSH endpoint, isolated immutable Git checkout; it does not deploy applications. It accepts only exact branches `ops/auth-schema-stage` and `ops/auth-schema-production`, mapping their full refs to Stage and production respectively. It requires REVIEWED_AUTH_SCHEMA_COMMIT matching the event SHA and checks detached checkout HEAD equals that SHA. SQL digest is fixed to `8a22f3e89efb3dacc24f3ed9337ae87d0b4345da003a607bc1994478f69c6feb`, approval ID to `CEO-AUTH-SCHEMA-20261025`; neither is push-supplied input. Production additionally requires PRODUCTION_AUTH_SCHEMA_APPROVAL = SHA:SQL_SHA256:APPROVAL_ID and STAGE_AUTH_SCHEMA_ACCEPTANCE = SHA:SQL_SHA256, provisioned only after real review and Stage auth acceptance. These are explicit approval records, not automatically issued receipts. The repository's workflow/secret permissions must restrict dedicated-ref mutation to trusted maintainers; unrestricted malicious workflow edits can bypass any workflow-local gate. Review/protect this boundary before provisioning production approval. Stage already has the DDL: Stage mode backs up production read-only, restores into isolated network-none temporary PostgreSQL, applies exact SQL there, compares resulting catalog with Stage read-only, and writes a SHA-bound structural receipt only. Production requires that receipt, repeats fresh full backup and restore/patch rehearsal, then executes only exact approved SQL. Full dumps remain mode600 host-only under /opt/backups/canina-auth-reconcile; never upload/log PII. Backup nonempty and pg_restore TOC checked. Restore errors/capacity failure stop before DDL. tmpfs is capped at 1GiB: if full restore exceeds capacity, separately review capacity rather than skipping restore. Existing read-only schema backup was 94,694 bytes / 67 tables, no PII copied; its restored PostgreSQL rehearsal is genuine, not a full data-backup recovery claim. SQL lock_timeout 3s, statement_timeout 30s, idle transaction 60s; advisory lock plus host flock. DDL transactional; lock/contention failure aborts all. No automatic retries. Re-execution with existing outbox fails closed pending comparison, rather than silently accepting drift. Stage/production outputs compare columns/types/nullability/defaults, all outbox constraints/indexes to fresh rehearsed expected schema. +## Gitea 1.22.6 trigger semantics and operator sequence +Gitea [1.22 comparison](https://docs.gitea.com/1.22/usage/actions/comparison) explicitly marks `workflow_dispatch` ignored. The [v1.22.6 notifier implementation](https://github.com/go-gitea/gitea/blob/v1.22.6/services/actions/notifier_helper.go) resolves the pushed ref's commit, detects workflows at that commit and records `Ref` and `CommitSHA` on the run. Push is not default-branch-only. The dedicated branch must therefore contain this workflow and point to the **final integrated reviewed commit**, not a new trigger-only commit. Existing app deployment listens only to `main`/`develop`, not either operations branch. + +After independent review/authorization, a trusted operator can push `:refs/heads/ops/auth-schema-stage` to origin. Inspect that CI run's SHA and structural receipt, then complete real Stage auth tests. Only after separate production and Stage acceptance approval records exist for the **same** SHA/digest may the operator push `:refs/heads/ops/auth-schema-production`. No empty commits, merges, arbitrary ref inputs, secret updates by this workflow, automatic acceptance, or app promotion. A no-op push does not retrigger; do not change the reviewed SHA to manufacture a run. Any SHA change requires fresh review, approval bindings and Stage evidence. Workflow preparation has not pushed either branch, changed secrets/protections or executed the operation. + ## Acceptance and rollback 1. Parent DBA review exact commit/diff plus SQL hash and workflow trusted-ref/secret permissions. Approve read-only production full backup + isolated full restore scope separately from production DDL. 2. Run Stage workflow from exact commit; inspect successful backup/restore/catalog comparison. Existing Stage backend must pass ordinary admin password login + protected dashboard, OTP flow with authorized principal, refresh, and expiry/session/generation rejection tests. Do not manufacture acceptance if no approved credential/test principal is available. Record actual test run identifiers before setting Stage acceptance approval. diff --git a/scripts/test_auth_schema_push.py b/scripts/test_auth_schema_push.py new file mode 100644 index 00000000..18b3420a --- /dev/null +++ b/scripts/test_auth_schema_push.py @@ -0,0 +1,73 @@ +"""Offline executable contract for the Gitea 1.22 push entrypoint; no SSH/DB calls.""" +import hashlib +import os +import pathlib +import subprocess +import textwrap +import unittest + +ROOT = pathlib.Path(__file__).resolve().parents[1] + + +class PushContract(unittest.TestCase): + def setUp(self): + self.workflow = (ROOT / '.gitea/workflows/auth-schema-reconcile.yml').read_text() + self.shell = textwrap.dedent(self.workflow.split(' run: |\n', 1)[1]) + self.gate = self.shell.split('[[ -n "$DEPLOY_KEY"', 1)[0] + self.sha = 'a' * 40 + self.digest = '8a22f3e89efb3dacc24f3ed9337ae87d0b4345da003a607bc1994478f69c6feb' + + def invoke(self, **changes): + env = dict(os.environ, CI_EVENT='push', CI_REF='refs/heads/ops/auth-schema-stage', + DEPLOY_SHA=self.sha, REVIEWED_SCHEMA_COMMIT=self.sha, + SQL_SHA=self.digest, APPROVAL_ID='CEO-AUTH-SCHEMA-20261025', + PRODUCTION_SCHEMA_APPROVAL='', STAGE_AUTH_ACCEPTANCE='') + env.update(changes) + return subprocess.run(['bash', '-c', self.gate + '\nprintf "%s:%s:%s" "$TARGET_ENV" "$schema_ok" "$auth_ok"'], + env=env, capture_output=True, text=True) + + def test_only_supported_exact_push_branches(self): + self.assertEqual(self.workflow.split('on:\n', 1)[1].split('jobs:', 1)[0], + ' push:\n branches:\n - ops/auth-schema-stage\n - ops/auth-schema-production\n') + self.assertNotIn('workflow_dispatch', self.workflow) + deploy = (ROOT / '.gitea/workflows/deploy.yml').read_text() + self.assertEqual(deploy.split('branches:\n', 1)[1].split('jobs:', 1)[0].strip(), + '- main\n - develop') + + def test_stage_and_production_require_existing_approvals(self): + stage = self.invoke() + self.assertEqual((stage.returncode, stage.stdout), (0, 'stage:no:no'), stage.stderr) + self.assertNotEqual(self.invoke(CI_REF='refs/heads/ops/auth-schema-production').returncode, 0) + env = dict(CI_REF='refs/heads/ops/auth-schema-production', + PRODUCTION_SCHEMA_APPROVAL=f'{self.sha}:{self.digest}:CEO-AUTH-SCHEMA-20261025', + STAGE_AUTH_ACCEPTANCE=f'{self.sha}:{self.digest}') + accepted = self.invoke(**env) + self.assertEqual((accepted.returncode, accepted.stdout), (0, 'production:yes:yes'), accepted.stderr) + for changes in [dict(PRODUCTION_SCHEMA_APPROVAL=''), dict(STAGE_AUTH_ACCEPTANCE=''), + dict(STAGE_AUTH_ACCEPTANCE='b' * 40 + ':' + self.digest), + dict(PRODUCTION_SCHEMA_APPROVAL=f'{self.sha}:{self.digest}:OTHER-APPROVAL'), + dict(PRODUCTION_SCHEMA_APPROVAL=f'{self.sha}:'+ '0' * 64 + ':CEO-AUTH-SCHEMA-20261025'), + dict(REVIEWED_SCHEMA_COMMIT='b' * 40)]: + self.assertNotEqual(self.invoke(**(env | changes)).returncode, 0) + + def test_invalid_event_ref_sha_cannot_reach_ssh(self): + for changes in [dict(CI_EVENT='workflow_dispatch'), dict(CI_EVENT='pull_request'), + dict(CI_REF='refs/heads/main'), dict(CI_REF='refs/tags/ops/auth-schema-stage'), + dict(CI_REF='refs/heads/ops/auth-schema-stage-extra'), + dict(CI_REF='refs/heads/ops/auth-schema-stage; exit 0'), + dict(DEPLOY_SHA='$(exit 0)'), dict(DEPLOY_SHA=''), dict(REVIEWED_SCHEMA_COMMIT='')]: + self.assertNotEqual(self.invoke(**changes).returncode, 0, changes) + + def test_no_expression_interpolation_and_exact_checkout(self): + self.assertNotIn('${{', self.shell) + self.assertIn('CI_REF: ${{ github.ref }}', self.workflow) + self.assertIn('CI_EVENT: ${{ github.event_name }}', self.workflow) + self.assertIn('[[ $(git rev-parse HEAD) == "$sha" ]]', self.shell) + self.assertEqual(hashlib.sha256((ROOT / 'scripts/auth-schema-reconcile.sql').read_bytes()).hexdigest(), self.digest) + self.assertIn('SQL_SHA: ' + self.digest, self.workflow) + self.assertIn('APPROVAL_ID: CEO-AUTH-SCHEMA-20261025', self.workflow) + subprocess.run(['bash', '-n'], input=self.shell, text=True, check=True) + + +if __name__ == '__main__': + unittest.main(verbosity=2)