"""Create a private LocalService-only QA clone; never change destination ACLs/services.""" from pathlib import Path from datetime import datetime, timezone import json, hashlib, shutil, subprocess, zipfile, re R = Path(__file__).resolve().parent.parent P = R / 'storage/app/private/investmoz-v2/portal-audit' load = lambda p: json.loads(p.read_text(encoding='utf-8-sig')) sha = lambda p: hashlib.sha256(p.read_bytes()).hexdigest() plan = load(P / 'service-followup-latest.json') old = load(P / 'service-followup-backup-restoration-qa.json') assert old['restore_verified'] and old['files_verified'] and old['file_count'] == 191 assert old['target']['database'] == plan['target']['database'] and old['target']['port'] == 55441 assert sha(Path(old['dump_path'])) == old['dump_sha256'] assert sha(Path(old['files_path'])) == old['files_sha256'] source = Path(plan['source']) stamp = datetime.now(timezone.utc).strftime('%Y%m%d_%H%M%S') S = P / ('localservice-qa-' + stamp) assert not S.exists() S.mkdir() new_db = 'workflow_upgrade_' + stamp + '_test' assert re.fullmatch(r'workflow_upgrade_\d{8}_\d{6}_test', new_db) for name in ['app', 'bootstrap', 'config', 'database', 'resources', 'routes']: shutil.copytree(source/name, S/name) shutil.copytree((source/'vendor').resolve(), S/'vendor') for name in ['qa-autoload.php', 'composer.json', 'composer.lock']: shutil.copyfile(source/name, S/name) for name in ['tools', 'storage/app/private', 'storage/framework/cache/data', 'storage/framework/views', 'storage/framework/sessions', 'storage/logs', 'storage/qa-proof', 'bootstrap/cache', 'scanner-writable']: (S/name).mkdir(parents=True, exist_ok=True) with zipfile.ZipFile(old['files_path']) as z: for key, digest in old['file_manifest'].items(): assert not {'..', '.'}.intersection(Path(key).parts) data = z.read(key) assert hashlib.sha256(data).hexdigest() == digest if key.startswith('private/'): dest = S/'storage/app/private'/key[len('private/'):] elif key.startswith('build/'): dest = S/'public/build'/key[len('build/'):] else: continue dest.parent.mkdir(parents=True, exist_ok=True) dest.write_bytes(data) env = (source/'.env').read_text(encoding='utf-8') def env_set(text, key, value): line = key + '=' + value return re.sub(r'^'+re.escape(key)+r'=.*$', lambda m: line, text, flags=re.M) if re.search(r'^'+re.escape(key)+r'=', text, re.M) else text+'\n'+line+'\n' for key, value in {'DB_DATABASE':new_db, 'DB_PORT':'55441', 'DB_HOST':'127.0.0.1', 'DB_USERNAME':'postgres', 'DB_PASSWORD':'', 'DB_URL':'', 'DB_QUEUE':'localservice-qa', 'MAIL_MAILER':'log', 'PORTAL_NOTIFICATIONS_ENABLED':'false', 'PORTAL_NOTIFICATION_RECIPIENTS':'', 'SESSION_COOKIE':'investmoz_localservice_qa'}.items(): env = env_set(env, key, value) (S/'.env').write_text(env, encoding='utf-8') settings = load(source/'upgrade-qa.json') settings['target'] = {'database':new_db, 'port':55441} (S/'upgrade-qa.json').write_text(json.dumps(settings, indent=2), encoding='utf-8') shutil.copyfile(source/'tools/upgrade-runtime.php', S/'tools/upgrade-runtime.php') (S/'forbidden-qa-sentinel.txt').write_text('Synthetic forbidden data, not a credential.\n', encoding='utf-8') bin = Path('C:/Program Files/PostgreSQL/17/bin') def run(args): result = subprocess.run([str(a) for a in args], capture_output=True, timeout=300) assert result.returncode == 0, 'Isolated QA database command failed' run([bin/'createdb.exe', '-h','127.0.0.1','-p','55441','-U','postgres',new_db]) run([bin/'pg_restore.exe','-h','127.0.0.1','-p','55441','-U','postgres','-d',new_db, '--exit-on-error','--no-owner','--no-privileges',old['dump_path']]) proof = {'created_at':datetime.now(timezone.utc).isoformat(), 'source':str(S), 'target':settings['target'], 'queue':'localservice-qa', 'scanner_port':13310, 'restored_from_191_file_proof':old['verified_at'], 'existing_candidate_preserved':'98a8e0b70892', 'delta':'117d2d889701', 'active_writes':0, 'source_files_restored_by_hash': sum(k.startswith('private/') for k in old['file_manifest']), 'principal_required':'S-1-5-19', 'acl_changes_allowed_only_under':str(S)} (P/'consolidated-localservice-plan.json').write_text(json.dumps(proof, indent=2), encoding='utf-8') print(json.dumps(proof))