droboticflow-new/scripts/local/query_cutover_guard.py

103 lines
7.1 KiB
Python

"""Small, explicit authorization and result-evidence checks for query cutover.
No cluster writes, telemetry or production fallback at import time.
"""
import datetime as dt
import hashlib
import json
import subprocess
from pathlib import Path
MUTATING_COMMANDS = {'shadow-sync', 'cutover-api-flags', 'rollback-api-flags', 'watch-and-guard', 'patrol'}
ROLES = {'admin', 'supplier_manager', 'annotator', 'quality_reviewer', 'acceptance_reviewer'}
SCENARIOS = {'normal', 'legacy', 'date-boundary', 'deleted', 'rework', 'permission-revoked', 'cross-supplier', 'cross-organization'}
def utc(value):
return dt.datetime.fromisoformat(value.replace('Z', '+00:00'))
def approved_plan(args, client):
if args.command not in MUTATING_COMMANDS:
return None
path = getattr(args, 'approved_plan', None)
if not path or not getattr(args, 'approval_ref', ''):
raise RuntimeError('explicit approved plan and approval reference required; no writes performed')
raw = Path(path).read_bytes()
if hashlib.sha256(raw).hexdigest() != getattr(args, 'confirm_plan', ''):
raise RuntimeError('approved plan digest changed')
plan = json.loads(raw)
if args.command not in plan.get('operations', []) or dt.datetime.now(dt.timezone.utc) >= utc(plan['expiresAt']):
raise RuntimeError('operation outside approved scope/window')
if args.command == 'cutover-api-flags' and plan.get('allowProductionCutover') is not True:
raise RuntimeError('production entry is not authorized')
refs = [subprocess.check_output(['git', 'rev-parse', ref], text=True).strip() for ref in ('HEAD', 'dev', 'origin/dev')]
if not all((ref == plan.get('sourceCommit') for ref in refs)) or subprocess.check_output(['git', 'status', '--porcelain'], text=True).strip():
raise RuntimeError('clean synchronized release source required')
remote = subprocess.check_output(['git','ls-remote','--exit-code','origin','refs/heads/dev'],text=True,stderr=subprocess.DEVNULL).split()[0]
if remote != plan.get('sourceCommit'):
raise RuntimeError('remote dev changed since approved plan')
context = client.run(['config', 'current-context']).stdout.strip()
if context != plan['context']:
raise RuntimeError('cluster context changed')
server = client.run(['config', 'view', '--minify', '-o', 'jsonpath={.clusters[0].cluster.server}']).stdout.strip()
if hashlib.sha256(server.encode()).hexdigest() != plan['serverSHA256']:
raise RuntimeError('cluster endpoint changed')
for namespace, uid in plan['namespaceUIDs'].items():
if client.get_json(None, 'namespace', namespace)['metadata']['uid'] != uid:
raise RuntimeError('namespace identity changed')
if args.command == 'shadow-sync':
for field in ('replica_image', 'builder_image'):
if getattr(args, field) != plan.get(field) or '@sha256:' not in getattr(args, field):
raise RuntimeError('immutable candidate image differs from approved plan')
return plan
def bounded_shadow_args(plan, resume_nonce=''):
policy = plan['shadowPolicy']
if not policy.get('runID') or not all((type(policy.get(k)) is int and policy[k] > 0 for k in ('maxRecords', 'recordsPerSecond', 'bytesPerSecond', 'commandsPerSecond', 'batchSize'))):
raise RuntimeError('explicit finite shadow budgets required')
if utc(policy['maintenanceUntil']) > utc(plan['expiresAt']):
raise RuntimeError('shadow policy exceeds approved plan')
if dt.datetime.now(dt.timezone.utc) >= utc(policy['maintenanceUntil']):
raise RuntimeError('shadow maintenance window closed')
args = ['--writers-covered', '--bootstrap', '--interval=30s', '--maintenance-initialization', '--safety-run=' + policy['runID'], '--maintenance-until=' + policy['maintenanceUntil'], '--max-source-records=' + str(policy['maxRecords']), '--source-records-per-second=' + str(policy['recordsPerSecond']), '--source-bytes-per-second=' + str(policy['bytesPerSecond']), '--source-commands-per-second=' + str(policy['commandsPerSecond']), '--batch-size=' + str(policy['batchSize'])]
if resume_nonce:
args.append('--resume-pause=' + resume_nonce)
return args
def require_result_comparison(evidence, snapshot, plan, now=None):
now = now or dt.datetime.now(dt.timezone.utc)
if evidence.get('environment') != 'shadow-live' or evidence.get('queryDatabase') != 'drobodata_fanyu_query_shadow_20261114':
raise RuntimeError('live scoped comparison evidence required')
if evidence.get('sourceCommit') != plan.get('sourceCommit') or evidence.get('namespaceUID') != plan['namespaceUIDs']['droboticflow-query-shadow']:
raise RuntimeError('comparison identity mismatch')
age = (now - utc(evidence['observedAt'])).total_seconds()
if not 0 <= age <= 900:
raise RuntimeError('comparison evidence expired')
if evidence.get('cohortDigest') != hashlib.sha256(json.dumps(snapshot['global']['heads'], sort_keys=True, separators=(',', ':')).encode()).hexdigest():
raise RuntimeError('published comparison cohort changed')
if evidence.get('replicaFactEpoch') != snapshot.get('state', {}).get('factEpoch'):
raise RuntimeError('comparison replica epoch changed')
cases = evidence.get('cases') or []
for case in cases:
old, new = (case.get('authorityDigest', ''), case.get('queryDigest', ''))
if len(old) != 64 or old != new or any((c not in '0123456789abcdef' for c in old)):
raise RuntimeError('result comparison differs or is missing')
if case.get('authorityAllowed'):
heads = snapshot['global']['heads']
production = heads.get(case.get('productionHeadKey')) or {}
workforce = heads.get(case.get('workforceHeadKey')) or {}
if workforce.get('generation') != 'workforce-facts/v1' or not production.get('chunks'):
raise RuntimeError('scoped publication heads missing')
for field in ('asOf', 'sourceRevision', 'globalRevision'):
if production.get(field) is None or production[field] != workforce.get(field):
raise RuntimeError('mixed publication cohort')
if type(case.get('authorityAllowed')) is not bool or case['authorityAllowed'] != case.get('queryAllowed'):
raise RuntimeError('permission comparison differs')
if case.get('authorityScopeDigest') != case.get('queryScopeDigest') or not case.get('authorityScopeDigest'):
raise RuntimeError('organization/supplier scope comparison differs')
if not any((c.get('authorityAllowed') for c in cases)):
raise RuntimeError('successful business results not compared')
for scenario in ('permission-revoked', 'cross-supplier', 'cross-organization'):
if not any((c.get('scenario') == scenario and c.get('authorityAllowed') is False for c in cases)):
raise RuntimeError('permission denial coverage missing')
if not ROLES <= {c.get('role') for c in cases} or not SCENARIOS <= {c.get('scenario') for c in cases} or (not {'counts', 'dashboard', 'work-records'} <= {c.get('kind') for c in cases}):
raise RuntimeError('comparison coverage incomplete')