From cf9b80bbb3c35c17eb5b11be693f06b62158b398 Mon Sep 17 00:00:00 2001 From: devops Date: Sun, 25 Oct 2026 12:42:39 +0000 Subject: [PATCH] fix(release): gate generated Prisma client against live production catalog --- scripts/production-release.py | 82 +++++++++++++++++++++- scripts/rollout-production.sh | 3 + scripts/tests/test_release_schema_guard.py | 56 +++++++++++++++ 3 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 scripts/tests/test_release_schema_guard.py diff --git a/scripts/production-release.py b/scripts/production-release.py index c485a4dc..169e4405 100644 --- a/scripts/production-release.py +++ b/scripts/production-release.py @@ -11,6 +11,7 @@ import sys ALLOWED = {'nginx.conf', 'Dockerfile', 'backend/Dockerfile', '.dockerignore', '.gitea/workflows/deploy.yml', 'scripts/deploy.sh', 'scripts/ci-host-deploy.sh', 'scripts/rollout-production.sh', 'scripts/production-release.py', 'scripts/test_production_release.py', + 'scripts/tests/test_release_schema_guard.py', 'scripts/compose.stage.yml', 'scripts/rollout-stage.sh', 'scripts/revalidation-runtime.py', 'scripts/provision-revalidation-env.py', 'scripts/tests/test_revalidation_runtime.py', 'scripts/tests/test_ci_host_boundary.py', 'scripts/tests/test_stage_provision_wiring.py', @@ -75,12 +76,81 @@ def snapshot(items): c['services'][name]=s return c +def validate_catalog(models, catalog): + """Check every persisted scalar required by the generated target client.""" + if not models or not catalog: + raise ValueError('Empty generated model or production catalog evidence') + actual = {(r['table'], r['column']): r['type'] for r in catalog} + missing = [] + for model in models: + table = model.get('dbName') or model['name'] + for field in model['fields']: + if field['kind'] == 'object': + continue + column = field.get('dbName') or field['name'] + if (table, column) not in actual: + missing.append(table + '.' + column) + if missing: + raise ValueError('Incompatible production catalog: ' + ', '.join(missing)) + + +def schema_gate(target): + """Read only: inspect immutable target client and the actual running DB catalog. + + No checkout/schema diff can substitute for this check. A newly generated + client may require columns which were already in both source revisions. + """ + def read(args): + return json.loads(subprocess.check_output(args, text=True)) + before = read(['docker', 'inspect', 'canino_backend_prod'])[0] + if not before['State']['Running'] or not re.fullmatch(r'sha256:[0-9a-f]{64}', before['Image']): + raise ValueError('Running immutable production backend required') + image = read(['docker', 'image', 'inspect', target])[0]['Id'] + if not re.fullmatch(r'sha256:[0-9a-f]{64}', image): + raise ValueError('Immutable target image required') + # DMMF comes from the generated client in the built image, NOT schema.prisma. + models = read(['docker', 'run', '--rm', '--network', 'none', '--read-only', + '--cap-drop=ALL', '--security-opt=no-new-privileges', '--entrypoint', + 'node', image, '-e', + 'console.log(JSON.stringify(require("@prisma/client").Prisma.dmmf.datamodel.models))']) + # Use only the running backend's private connection; never print credentials. + # The transaction itself enforces READ ONLY even if runtime DB role can write. + probe = r'''(async()=>{const {PrismaClient}=require('@prisma/client');const p=new PrismaClient(); +try {const rows=await p.$transaction(async tx=>{ +await tx.$executeRawUnsafe('SET TRANSACTION READ ONLY'); +return await tx.$queryRawUnsafe(`SELECT c.relname AS "table", a.attname AS "column", t.typname AS "type" +FROM pg_catalog.pg_attribute a JOIN pg_catalog.pg_class c ON c.oid=a.attrelid +JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace +JOIN pg_catalog.pg_type t ON t.oid=a.atttypid +WHERE n.nspname='public' AND c.relkind IN ('r','p','v','m') AND a.attnum>0 AND NOT a.attisdropped`); +});console.log(JSON.stringify(rows));} finally {await p.$disconnect();} +})().catch(()=>{console.error('Read-only production catalog inspection failed');process.exitCode=1});''' + catalog = read(['docker', 'exec', before['Id'], 'node', '-e', probe]) + validate_catalog(models, catalog) + after = read(['docker', 'inspect', 'canino_backend_prod'])[0] + if after['Id'] != before['Id'] or after['Image'] != before['Image'] or not after['State']['Running']: + raise ValueError('Production runtime changed during compatibility check') + return {'target_image': image, 'runtime_image': before['Image'], + 'runtime_container': before['Id'], 'catalog_columns': len(catalog)} + + def main(): action=sys.argv[1] if action=='receipt': - sha=os.environ['DEPLOY_SHA']; base=subprocess.check_output(['git','-C','/opt/services/canina/prod','rev-parse','HEAD'],text=True).strip() - paths=subprocess.check_output(['git','diff','--name-only',base,sha],text=True).splitlines() + sha=os.environ['DEPLOY_SHA'] r=private_json('/opt/services/canina/production-acceptance/'+sha+'.json') + # The reviewer binds the source baseline to actual immutable runtime images. + # A shared checkout is not evidence of what the running client contains. + base=r.get('baseline', '') + if not re.fullmatch(r'[0-9a-f]{40}', base): + raise ValueError('Reviewed immutable source baseline required') + prod=json.loads(subprocess.check_output(['docker','inspect','canino_backend_prod','canino_frontend_prod'])) + actual_prod={x['Name'][1:]:x['Image'] for x in prod} + if (set(actual_prod) != {'canino_backend_prod','canino_frontend_prod'} + or r.get('production_images') != actual_prod + or not all(x['State']['Running'] and re.fullmatch(r'sha256:[0-9a-f]{64}',x['Image']) for x in prod)): + raise ValueError('Reviewed production immutable image provenance required') + paths=subprocess.check_output(['git','diff','--name-only',base,sha],text=True).splitlines() validate_receipt(r,sha,base,paths) # Bind audited evidence to the two currently running Stage artifacts. stage=json.loads(subprocess.check_output(['docker','inspect','canino_backend_stage','canino_frontend_stage'])) @@ -93,6 +163,14 @@ def main(): raise ValueError('Stage source SHA mismatch') marker=subprocess.check_output(['docker','exec','canino_backend_stage','cat','/app/STAGE_TEST_ARTIFACT_SHA'],text=True) if marker != sha: raise ValueError('Stage backend artifact SHA mismatch') + elif action=='schema-check': + path=Path('.prod-compose.json') + c=json.loads(path.read_text()) + evidence=schema_gate(c['services']['backend_prod']['image']) + # Prevent a mutable tag moving between compatibility check and replacement. + c['services']['backend_prod']['image']=evidence['target_image'] + path.write_text(json.dumps(c)) + Path('.prod-schema-evidence.json').write_text(json.dumps(evidence)) elif action=='snapshot': items=json.loads(subprocess.check_output(['docker','inspect','canino_backend_prod','canino_frontend_prod'])) c=snapshot(items) diff --git a/scripts/rollout-production.sh b/scripts/rollout-production.sh index 0576f6ec..a10e60d0 100644 --- a/scripts/rollout-production.sh +++ b/scripts/rollout-production.sh @@ -43,6 +43,9 @@ compose() { command docker compose -p prod --env-file /dev/null --project-direct compose .prod-compose.json config --quiet compose .prod-rollback.json config --quiet compose .prod-compose.json --progress plain build backend_prod frontend_prod +# Always check the generated target client against live read-only production catalog. +# This precedes even rollback arming: incompatible images never replace runtime. +python3 scripts/production-release.py schema-check # Snapshot uses immutable old image IDs, runtime environment, mounts and labels. # DB/Redis/support bot are not in this app-only model and must not be orphan-pruned. rollback() { diff --git a/scripts/tests/test_release_schema_guard.py b/scripts/tests/test_release_schema_guard.py new file mode 100644 index 00000000..9dd1ebde --- /dev/null +++ b/scripts/tests/test_release_schema_guard.py @@ -0,0 +1,56 @@ +"""Generated-client drift must block a release even with unchanged Git schema.""" +import importlib.util +from pathlib import Path +import unittest + +SPEC = importlib.util.spec_from_file_location('release', Path(__file__).parents[1]/'production-release.py') +m = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(m) + +class SchemaGate(unittest.TestCase): + def test_unchanged_source_baseline_new_client_drift(self): + # Both Git SHAs already contain these fields; the old runtime client did not. + models = [{'name':'User','dbName':'users','fields':[ + {'name':n,'dbName':n,'kind':'scalar','type':t,'isList':False} + for n,t in [('id','String'),('auth_expires_at','DateTime'), + ('stage_test_session','String'),('stage_test_generation','String')]]}, + {'name':'PrepareWorkflowOutbox','dbName':'prepare_workflow_outbox','fields':[ + {'name':'id','kind':'scalar','type':'String','isList':False}]}] + old_catalog = [{'table':'users','column':'id','type':'uuid'}] + with self.assertRaisesRegex(ValueError, 'auth_expires_at.*prepare_workflow_outbox'): + m.validate_catalog(models, old_catalog) + + def test_gate_pins_built_image_and_reads_live_catalog_before_replacement(self): + import json + from unittest.mock import patch + image = 'sha256:'+'c'*64 + runtime = {'Image':'sha256:'+'d'*64,'Id':'running-id','State':{'Running':True}} + models = [{'name':'User','dbName':'users','fields':[{'name':'id','kind':'scalar'}]}] + responses = [json.dumps([runtime]), json.dumps([{'Id':image}]), + json.dumps(models), json.dumps([{'table':'users','column':'id','type':'uuid'}]), + json.dumps([runtime])] + with patch.object(m.subprocess,'check_output',side_effect=responses) as run: + result = m.schema_gate('canina-production-backend_prod:'+'a'*40) + self.assertEqual(result['target_image'], image) + calls = [c.args[0] for c in run.call_args_list] + self.assertIn(image, calls[2]) + self.assertIn('--network', calls[2]) + self.assertIn('running-id', calls[3]) + self.assertIn('READ ONLY', calls[3][-1]) + script = Path(__file__).parents[1].joinpath('rollout-production.sh').read_text() + self.assertLess(script.index('production-release.py schema-check'), script.index('started=true')) + + def test_catalog_failure_never_falls_back_to_source_diff(self): + from unittest.mock import patch + import subprocess + with patch.object(m.subprocess,'check_output',side_effect=subprocess.CalledProcessError(1,['docker'])): + with self.assertRaises(subprocess.CalledProcessError): + m.schema_gate('target') + + def test_exact_guard_paths_allowed_and_receipt_not_bound_to_checkout(self): + self.assertIn('scripts/tests/test_release_schema_guard.py', m.ALLOWED) + source = Path(__file__).parents[1].joinpath('production-release.py').read_text() + self.assertNotIn("['git','-C','/opt/services/canina/prod','rev-parse','HEAD']", source) + self.assertIn("r.get('production_images')", source) + +if __name__ == '__main__': unittest.main()