186 lines
11 KiB
Python
186 lines
11 KiB
Python
"""Admit the first public-worker generation after a historical updater restart.
|
|
|
|
Historical installers already replaced the code pair before starting the new
|
|
daemon. Before that daemon runs host bootstrap, this installed release verifies
|
|
both published releases and freezes authenticated old code plus the still-live
|
|
runtime/configuration preimages. App-writable backup bytes alone are never
|
|
accepted as executable recovery authority.
|
|
"""
|
|
|
|
PUBLIC_RELEASE_BASE = 'https://source.archipelago-foundation.org/lfg2025/archy/releases/download'
|
|
LEGACY_MARKER = Path('/var/lib/archipelago/update-pending-verify.json')
|
|
LEGACY_BINARY = Path('/var/lib/archipelago/update-backup/archipelago')
|
|
LEGACY_FRONTEND = Path('/opt/archipelago/web-ui.bak')
|
|
|
|
|
|
def public_download(url, path, size=None, sha256=None):
|
|
from urllib.parse import urlsplit
|
|
parsed = urlsplit(url)
|
|
if (parsed.scheme != 'https' or parsed.netloc != 'source.archipelago-foundation.org'
|
|
or not parsed.path.startswith('/lfg2025/archy/releases/download/')
|
|
or parsed.query or parsed.fragment):
|
|
raise ValueError('legacy admission requires the fixed public release origin')
|
|
limit = size if size is not None else MAX_DOCUMENT
|
|
opener = urllib.request.build_opener(urllib.request.ProxyHandler({}))
|
|
digest = hashlib.sha256(); copied = 0; deadline = time.monotonic() + 180
|
|
with opener.open(url, timeout=30) as source, path.open('xb') as target:
|
|
final = urllib.parse.urlsplit(source.geturl())
|
|
if final.scheme != 'https' or final.netloc != parsed.netloc:
|
|
raise ValueError('public admission download changed authenticated origin')
|
|
while block := source.read(1024 * 1024):
|
|
if time.monotonic() > deadline:
|
|
raise TimeoutError('public admission download deadline exceeded')
|
|
copied += len(block)
|
|
if copied > limit:
|
|
raise ValueError('public admission download exceeds signed size')
|
|
digest.update(block); target.write(block)
|
|
target.flush(); os.fchmod(target.fileno(), 0o444); os.fsync(target.fileno())
|
|
if (size is not None and copied != size) or (sha256 is not None and digest.hexdigest() != sha256):
|
|
raise ValueError('public admission download differs from signed identity')
|
|
|
|
|
|
def historical_frontend_modes(path):
|
|
# Historical apply_update normalizes EVERY file to0644 and directory to0755.
|
|
# Reproduce that byte/mode domain before comparing the retained old tree.
|
|
for directory, _, files in os.walk(path):
|
|
for filename in files:
|
|
durable_mode(Path(directory) / filename, 0o444)
|
|
durable_mode(Path(directory), 0o555)
|
|
|
|
|
|
def retain_legacy_marker(tx, state):
|
|
expected = state.get('legacy_marker_sha256')
|
|
if expected is None or not os.path.lexists(LEGACY_MARKER):
|
|
return
|
|
if digest_file(LEGACY_MARKER) != expected:
|
|
raise ValueError('legacy verification marker changed; preserve it for reconciliation')
|
|
retained = tx / 'legacy-verification-marker.json'
|
|
if retained.exists():
|
|
if digest_file(retained) != expected:
|
|
raise ValueError('retained legacy verification marker differs')
|
|
LEGACY_MARKER.rename(tx / ('legacy-verification-marker-' + uuid.uuid4().hex + '.json'))
|
|
else:
|
|
LEGACY_MARKER.rename(retained)
|
|
sync_directory(tx); sync_directory(LEGACY_MARKER.parent)
|
|
|
|
|
|
def adopt_legacy_pair(bundle):
|
|
require_legacy_service()
|
|
if os.path.lexists(RUNTIME_RECORD):
|
|
raise ValueError('public runtime already has an owner; no legacy adoption')
|
|
with owner_lock():
|
|
_, previous = current()
|
|
if previous and previous['phase'] not in TERMINAL:
|
|
# A caller restart may observe its already queued independent job.
|
|
if previous.get('adoption'):
|
|
command('/usr/bin/systemctl', 'start', '--no-block', previous['worker_unit'])
|
|
return previous
|
|
raise ValueError('different public transaction pending')
|
|
check_owner_dropins(previous)
|
|
if not manager_adoptable():
|
|
raise ValueError('legacy admission requires the actual starting manager')
|
|
fd = os.open(LEGACY_MARKER, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK)
|
|
with os.fdopen(fd, 'rb') as stream:
|
|
info = os.fstat(stream.fileno())
|
|
if not stat.S_ISREG(info.st_mode) or info.st_size > MAX_DOCUMENT:
|
|
raise ValueError('invalid legacy verification marker')
|
|
marker_raw = stream.read(MAX_DOCUMENT + 1)
|
|
if len(marker_raw) > MAX_DOCUMENT:
|
|
raise ValueError('oversized legacy verification marker')
|
|
marker = json.loads(marker_raw, object_pairs_hook=pairs)
|
|
old_version, new_version = marker['previous_version'], marker['new_version']
|
|
if version(old_version) >= version(new_version):
|
|
raise ValueError('legacy marker is not an upgrade')
|
|
tx = TRANSACTIONS / uuid.uuid4().hex
|
|
tx.mkdir(mode=0o700)
|
|
current_doc = tx / 'signed-manifest.json'
|
|
public_download(f'{PUBLIC_RELEASE_BASE}/v{new_version}/manifest.json', current_doc)
|
|
raw = secure_read(current_doc)
|
|
installed = authenticate(raw)
|
|
document = json.loads(raw, object_pairs_hook=pairs)
|
|
if installed != new_version or document.get('version') != new_version:
|
|
raise ValueError('legacy marker does not identify this installed release')
|
|
components = validate_document(document, old_version)
|
|
if document.get('application_update') != RUNTIME_POLICY:
|
|
raise ValueError('first-generation migration requires the signed runtime transaction profile')
|
|
identity = hashlib.sha256(json.dumps([new_version, sorted([
|
|
[c['name'], c['new_version'], c['sha256'], c['size_bytes']] for c in components])],
|
|
separators=(',', ':'), ensure_ascii=True).encode()).hexdigest()
|
|
# Historical apply_update removes staging before its detached restart.
|
|
# Fetch the exact authenticated artifacts again into root custody.
|
|
for component in components:
|
|
public_download(component['download_url'], tx / component['name'],
|
|
component['size_bytes'], component['sha256'])
|
|
(tx / 'archipelago').rename(tx / 'new-binary')
|
|
durable_mode(tx / 'new-binary', 0o555)
|
|
extract(tx / f'archipelago-frontend-{new_version}.tar.gz', tx / 'new-frontend')
|
|
historical_frontend_modes(tx / 'new-frontend')
|
|
previous_doc = tx / 'previous-signed-manifest.json'
|
|
public_download(f'{PUBLIC_RELEASE_BASE}/v{new_version}/legacy-manifest-{old_version}.json', previous_doc)
|
|
old_raw = secure_read(previous_doc)
|
|
authenticate(old_raw)
|
|
old = json.loads(old_raw, object_pairs_hook=pairs)
|
|
if old.get('version') != old_version or old.get('signed_by') != RELEASE_ROOT or 'os_update' in old:
|
|
raise ValueError('previous release is not an authenticated application baseline')
|
|
old_binary = [c for c in old['components'] if c['name'] == 'archipelago']
|
|
old_frontend = [c for c in old['components'] if c['name'] == f'archipelago-frontend-{old_version}.tar.gz']
|
|
if len(old_binary) != 1 or len(old_frontend) != 1:
|
|
raise ValueError('previous signed code pair missing')
|
|
if LEGACY_BINARY.stat().st_size != old_binary[0]['size_bytes'] or digest_file(LEGACY_BINARY) != old_binary[0]['sha256']:
|
|
raise ValueError('untrusted historical backend backup; refusing executable recovery')
|
|
fd = os.open(LEGACY_BINARY, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK)
|
|
try:
|
|
copy_file_fd(fd, tx / 'old-binary', True)
|
|
finally:
|
|
os.close(fd)
|
|
if digest_file(tx / 'old-binary') != old_binary[0]['sha256']:
|
|
raise ValueError('historical backend changed while freezing its preimage')
|
|
component = old_frontend[0]
|
|
if (not re.fullmatch('[0-9a-f]{64}', component['sha256'])
|
|
or type(component['size_bytes']) is not int or not 0 < component['size_bytes'] <= MAX_COMPONENT):
|
|
raise ValueError('invalid previous frontend declaration')
|
|
public_download(component['download_url'], tx / 'previous-frontend.tar.gz',
|
|
component['size_bytes'], component['sha256'])
|
|
extract(tx / 'previous-frontend.tar.gz', tx / 'old-expected', require_declaration=False)
|
|
historical_frontend_modes(tx / 'old-expected')
|
|
# Retain the signed template when it is unchanged. Some prior daemons
|
|
# instead carry accepted root-owned routing into the frontend runtime;
|
|
# permit only that exact alternative, never an arbitrary local edit.
|
|
relative = Path('archipelago-runtime/image-recipe/configs/nginx-archipelago.conf')
|
|
routing = Path(RUNTIME_OBJECTS['routing'][0])
|
|
retained, expected = LEGACY_FRONTEND / relative, tx / 'old-expected' / relative
|
|
if (retained.is_file() and expected.is_file() and routing.exists()
|
|
and digest_file(retained) != digest_file(expected)):
|
|
atomic(expected, secure_read(routing, 1024**2), 0o444)
|
|
# Companion may have been independently updated to this same signed
|
|
# release's APK. Accept only the old or incoming signed artifact bytes.
|
|
for name in ('archipelago-companion.apk', 'archipelago-companion.json'):
|
|
relative = Path('packages') / name
|
|
retained, expected, incoming = LEGACY_FRONTEND / relative, tx / 'old-expected' / relative, tx / 'new-frontend' / relative
|
|
if retained.is_file() and incoming.is_file() and digest_file(retained) == digest_file(incoming):
|
|
atomic(expected, secure_read(incoming, MAX_COMPONENT), 0o444)
|
|
if tree(LEGACY_FRONTEND) != tree(tx / 'old-expected'):
|
|
raise ValueError('historical frontend backup contains unverified changes; preserve for review')
|
|
tree(LEGACY_FRONTEND, tx / 'old-frontend')
|
|
if tree(tx / 'old-frontend') != tree(tx / 'old-expected'):
|
|
raise ValueError('historical frontend changed while freezing its preimage')
|
|
# Historical apply preserves separately installed static UI extras.
|
|
# Their frozen old image is now authenticated; reproduce that merge
|
|
# before checking the pair it installed. No runtime script is exempt.
|
|
for name in ('aiui', 'archipelago-companion.apk'):
|
|
old_extra, incoming = tx / 'old-frontend' / name, tx / 'new-frontend' / name
|
|
if old_extra.exists() and not incoming.exists():
|
|
runtime_copy(old_extra, incoming)
|
|
historical_frontend_modes(tx / 'new-frontend')
|
|
initial = pair_identity(BINARY, FRONTEND)
|
|
if initial != pair_identity(tx / 'new-binary', tx / 'new-frontend'):
|
|
raise ValueError('historical installer did not install the exact signed incoming pair')
|
|
state = {'schema': 1, 'id': tx.name, 'release': identity, 'version': new_version,
|
|
'old_version': old_version, 'new_version': new_version, 'was_active': True,
|
|
'adoption': True, 'initial': initial, 'runtime_requested': True,
|
|
'legacy_marker_sha256': hashlib.sha256(marker_raw).hexdigest(),
|
|
'old': pair_identity(tx / 'old-binary', tx / 'old-frontend'),
|
|
'new': pair_identity(tx / 'new-binary', tx / 'new-frontend')}
|
|
custody_frontend_parent()
|
|
return schedule_transaction(tx, state, bundle)
|