fix(release): gate generated Prisma client against live production catalog

This commit is contained in:
devops committed 2026-10-25 12:43:20 +00:00
1 parent 2a341d3969
commit cf9b80bbb3
3 files changed
+139 -2

No files matched your search

+80 -2
View File
@@ -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)