Files
compras/scripts/install-private-audit-context-preservation.py
T
nala 47649bd46c
tests / ci (push) Canceled after 0s
commit compras1 3011
2026-11-30 09:33:36 +02:00

64 lines
3.5 KiB
Python

"""Preserve exact historical private audit archives during the approved context alignment."""
from pathlib import Path
import hashlib
import json
import os
import shutil
import subprocess
import zipfile
root = Path('/var/www/compras-deploy/current').resolve()
assert root == Path('/var/www/compras-deploy/releases/presentation-20261129')
old = json.loads((root/'compras-release.json').read_text())
assert old['release_id'] == 'compras-46427a85474ae277'
subprocess.run(['/usr/bin/php', str(root/'scripts/verify-compras-release.php'), str(root), '--installed'], check=True)
archive = Path('/root/compras-release-pilot-preserved.zip')
assert hashlib.sha256(archive.read_bytes()).hexdigest() == 'a7e4473e1417b444a42a484f1ddf4f0fc03b64874cb9c304ef74181b30745940'
z = zipfile.ZipFile(archive)
new = json.loads(z.read('compras-release.json'))
assert new['release_id'] == 'compras-47dfeee7485db877'
assert old['frontend'] == new['frontend'] and old['components'] == new['components']
changed = [name for name in old['source_files'] if old['source_files'][name] != new['source_files'][name]]
assert set(changed) == {'app/Services/PrivateAuditAccess.php', 'app/Services/PrivateAuditExporter.php',
'app/Http/Controllers/PrivateAuditController.php', 'config/private-audit.php'}
pins = json.loads(subprocess.check_output(['/usr/bin/php', str(root/'scripts/private-audit-transition-pins.php')]))
assert list(pins) == ['2'] and len(pins['2']) == 3
env = (root/'.env').resolve()
assert env == Path('/var/www/compras-deploy/demonstration-20261129/.env')
original = env.read_bytes()
assert b'COMPRAS_AUDIT_LEGACY_TRANSITIONS=' not in original
backup = Path('/var/backups/compras-presentation-20261129/context-audit-preservation')
backup.mkdir(mode=0o700, exist_ok=False)
shutil.copy2(env, backup/'selected.env.private')
(backup/'selected.env.private').chmod(0o600)
(backup/'transition-receipt.json').write_text(json.dumps(pins, indent=2)+'\n')
for name in changed+['compras-release.json']:
target = backup/name
target.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(root/name, target)
def save(path, contents):
attrs = path.stat()
temp = path.with_name(path.name+'.preservation-next')
temp.write_bytes(contents)
os.chown(temp, attrs.st_uid, attrs.st_gid)
temp.chmod(attrs.st_mode & 0o777)
os.replace(temp, path)
try:
for name in changed+['compras-release.json']:
save(root/name, z.read(name))
value = json.dumps(json.dumps(pins, separators=(',', ':'))).encode()
save(env, original.rstrip()+b'\nCOMPRAS_AUDIT_LEGACY_TRANSITIONS='+value+b'\n')
subprocess.run(['runuser', '-u', 'www-data', '--', '/usr/bin/php', 'artisan', 'config:cache'], cwd=root, check=True)
subprocess.run(['/usr/bin/php', str(root/'scripts/verify-compras-release.php'), str(root), '--installed'], check=True)
except BaseException:
save(env, original)
for name in changed+['compras-release.json']:
save(root/name, (backup/name).read_bytes())
subprocess.run(['runuser', '-u', 'www-data', '--', '/usr/bin/php', 'artisan', 'config:cache'], cwd=root, check=True)
raise
proof = {'state': 'installed', 'release_id': new['release_id'], 'backup': str(backup), 'exact_historical_archives': 3,
'archive_bytes_manifest_context_and_database_rows_changed': False, 'profile_permissions_changed': False,
'same_original_author_and_current_family_authorities_required': True, 'other_legacy_archives_allowed': False}
(backup/'result.json').write_text(json.dumps(proof, indent=2)+'\n')
print(json.dumps(proof))