333 lines
25 KiB
Python
333 lines
25 KiB
Python
#!/usr/bin/env python3
|
|
"""Versioned Cloud runtime upgrade for the one signed, closed APT partial.
|
|
|
|
No package recovery, approval or enrollment. Original active hook, once markers,
|
|
signed histories and SQLite state survive the maintenance/probe byte-for-byte.
|
|
The APT endpoint remains absent; only the copied-database native probe runs.
|
|
"""
|
|
import base64
|
|
import argparse
|
|
import hashlib
|
|
import importlib.util
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import sys
|
|
import uuid
|
|
|
|
import cloud_apt_reconcile_upgrade as previous
|
|
|
|
cloud = previous.cloud
|
|
require = cloud.require
|
|
STATE = previous.STATE
|
|
PARTIAL_GRANT = '31ef5e5e-5dbd-4428-a9a1-cbd15de2f5a0'
|
|
PARTIAL_PROOF = 'dc3bf3870ea77c12f9291bac712c53ae72194eaec980a8646155a7fb5c0dbe21'
|
|
PINS = dict(helper='ab7bbc87c75f68c1aa3d35941db9a2ece8d398f92a67e655b1d420bbd3287bd9',
|
|
agent='44ae91151ff08a186ce4dff587697eb604cb7c0a22f0c9d618724a9ba8a67234',
|
|
provider='5ff260f75cb2959678f6be729d3a806a4ea29febd5ae4003317fed41afc25327')
|
|
|
|
|
|
def closed_policy(payload):
|
|
import datetime as dt
|
|
path = Path('/opt/rust-discovery/devstate/root-policy/policy.json')
|
|
require(not path.is_symlink() and path.stat().st_uid == 10001 and path.stat().st_mode & 0o777 == 0o600, 'safe Root policy required')
|
|
raw = path.read_bytes(); policy = json.loads(raw)
|
|
require(policy['schema_version'] == 1 and policy['profile'] == 'development' and
|
|
dt.datetime.fromisoformat(policy['expires_at']) <= dt.datetime.now(dt.timezone.utc), 'Root policy must stay closed')
|
|
return dict(closed=True, policy_sha256=hashlib.sha256(raw).hexdigest())
|
|
|
|
|
|
def installed_metadata(payload):
|
|
pins = {'/usr/local/libexec/rmm-privileged-helper': payload['helper'],
|
|
'/usr/local/libexec/rmm-agent-remote': payload['agent'],
|
|
'/usr/lib/rmm-apt-transaction/apt_transaction.py': payload['provider']}
|
|
names = list(pins) + ['/etc/rmm/apt-helper.json', '/etc/rmm-remote/apt-helper-proof.json',
|
|
'/var/lib/rmm-apt-transaction/install-metadata.json']
|
|
files = {}
|
|
for name in names:
|
|
p = Path(name)
|
|
require(not p.is_symlink() and p.is_file() and p.stat().st_uid == 0 and p.stat().st_mode & 0o022 == 0, 'safe installed file required')
|
|
files[name] = hashlib.sha256(p.read_bytes()).hexdigest()
|
|
require(all(files[name] == pin for name, pin in pins.items()), 'reviewed readonly runtime required')
|
|
return dict(files=files)
|
|
|
|
|
|
def upgrade(payload):
|
|
import ast
|
|
import fcntl
|
|
import sqlite3
|
|
import time
|
|
import traceback
|
|
os.umask(0o077)
|
|
base = Path('/var/lib/rmm-apt-transaction'); state = Path('/var/lib/rmm-agent-remote')
|
|
ledger = Path('/var/lib/rmm-helper/consumption.sqlite')
|
|
helper = Path('/usr/local/libexec/rmm-privileged-helper'); agent = Path('/usr/local/libexec/rmm-agent-remote')
|
|
provider = Path('/usr/lib/rmm-apt-transaction/apt_transaction.py')
|
|
config = Path('/etc/rmm/apt-helper.json'); proof_file = Path('/etc/rmm-remote/apt-helper-proof.json')
|
|
metadata = base/'install-metadata.json'; readiness_file = base/'native-readiness.json'
|
|
dropin = Path('/etc/systemd/system/rmm-agent-remote.service.d/30-cloud-root.conf')
|
|
reduced = ('[Service]\nEnvironment=RMM_HELPER_SOCKET=/run/rmm-helper-cloud/control.sock\n'
|
|
'Environment=RMM_HELPER_POLICY_FILE=/etc/rmm-remote/general-helper-proof.json\n'
|
|
'Environment=RMM_LOG_UNITS_FILE=/etc/rmm-remote/log-units.json\n')
|
|
def run(args, **kwargs):
|
|
return subprocess.run(args, capture_output=True, check=True, timeout=90, **kwargs).stdout
|
|
def sha(raw): return hashlib.sha256(raw).hexdigest()
|
|
def canonical(value): return json.dumps(value, sort_keys=True, separators=(',', ':'), ensure_ascii=True).encode()
|
|
def fsync_dir(path):
|
|
fd = os.open(path, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW)
|
|
try: os.fsync(fd)
|
|
finally: os.close(fd)
|
|
def create(path, raw, mode=0o600, gid=0):
|
|
fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, mode)
|
|
with os.fdopen(fd, 'wb') as stream:
|
|
stream.write(raw); stream.flush(); os.fchown(stream.fileno(), 0, gid)
|
|
os.fchmod(stream.fileno(), mode); os.fsync(stream.fileno())
|
|
fsync_dir(path.parent)
|
|
def manifest(path):
|
|
require(not path.is_symlink(), 'preserved directory symlink')
|
|
if not path.exists(): return {'__absent__':True}
|
|
require(path.is_dir(), 'preserved directory required')
|
|
result = {}
|
|
for p in [path, *sorted(path.rglob('*'))]:
|
|
s = p.lstat(); require(not p.is_symlink() and (p.is_dir() or p.is_file()), 'preserved regular state required')
|
|
result[str(p.relative_to(path))] = dict(dev=s.st_dev, inode=s.st_ino, mode=s.st_mode,
|
|
uid=s.st_uid, gid=s.st_gid, sha256=sha(p.read_bytes()) if p.is_file() else None)
|
|
return result
|
|
def guard():
|
|
require(payload['partial_grant'] == '31ef5e5e-5dbd-4428-a9a1-cbd15de2f5a0' and
|
|
payload['partial_proof_sha256'] == 'dc3bf3870ea77c12f9291bac712c53ae72194eaec980a8646155a7fb5c0dbe21', 'one reviewed closed partial only')
|
|
with sqlite3.connect(ledger.as_uri()+'?mode=ro', uri=True) as db:
|
|
require(db.execute('PRAGMA integrity_check').fetchone()[0] == 'ok', 'Root ledger integrity')
|
|
require({r[0] for r in db.execute('SELECT grant_id FROM consumed')} == set(payload['expected_grants']) and
|
|
db.execute('SELECT count(*) FROM consumed WHERE result IS NULL').fetchone()[0] == 0 and
|
|
db.execute('SELECT count(*) FROM helper_sync_meta WHERE fault IS NOT NULL').fetchone()[0] == 0, 'exact known closed Root attempts')
|
|
db.row_factory = sqlite3.Row
|
|
rows = db.execute('SELECT * FROM apt_partial_reconciliations').fetchall()
|
|
require(len(rows) == 1, 'one immutable partial proof required'); row = dict(rows[0])
|
|
expected = payload['proof_row']
|
|
for key in ('grant_id', 'original_request_sha256', 'previous_outcome_sha256', 'proof_sha256', 'checked_at_ms'):
|
|
require(row[key] == expected[key], 'immutable partial binding drift')
|
|
for key in ('previous_outcome', 'proof', 'classified_outcome'):
|
|
require(sha(row[key]) == expected[key]['sha256'] and len(row[key]) == expected[key]['size'], 'immutable partial bytes drift')
|
|
require(row['grant_id'] == payload['partial_grant'] and sha(row['proof']) == payload['partial_proof_sha256'], 'closed proof hash required')
|
|
proof = json.loads(row['proof']); result = db.execute('SELECT c.result,e.outcome,e.start FROM consumed c JOIN execution_evidence e USING(grant_id) WHERE grant_id=?', (payload['partial_grant'],)).fetchone()
|
|
classified = json.loads(row['classified_outcome'])
|
|
require(json.loads(result['result']) == json.loads(result['outcome']) == classified and
|
|
classified['state'] == 'failed' and classified['error_code'] == 'APT_PARTIAL_UNCONFIGURED_VERIFIED', 'normal signed RootSync closure required')
|
|
saved = json.loads(result['start'])
|
|
# save_start nests Request through serde_json::Value (sorted keys),
|
|
# whereas the immutable proof hashes serde's Request struct codec.
|
|
request_fields = ('job_bytes','permit_bytes','signature','plan_hash','authorization_id')
|
|
require(set(saved['request']) == set(request_fields), 'original Request fields required')
|
|
request_bytes = json.dumps({key:saved['request'][key] for key in request_fields},
|
|
separators=(',', ':'), ensure_ascii=False).encode()
|
|
require(sha(request_bytes) == row['original_request_sha256'], 'original signed request hash required')
|
|
require(proof['grant_id'] == payload['partial_grant'] and proof['attempt_id'] == payload['partial_attempt'] and
|
|
proof['snapshot']['rmm-mvp-test:all'] == {'version':'1.0','status':'install ok unpacked'}, 'exact partial fixture required')
|
|
with sqlite3.connect((state/'journal.sqlite').as_uri()+'?mode=ro', uri=True) as db:
|
|
require(db.execute('PRAGMA integrity_check').fetchone()[0] == 'ok' and
|
|
db.execute("SELECT count(*) FROM job_attempts WHERE state NOT IN ('succeeded','failed','cancelled','expired')").fetchone()[0] == 0 and
|
|
db.execute('SELECT count(*) FROM reconcile_meta WHERE fault IS NOT NULL').fetchone()[0] == 0, 'no unresolved or faulted agent attempt')
|
|
require(Path('/usr/share/rmm-mvp-test/version').read_bytes() == b'1.0\n', 'existing unpacked fixture payload required')
|
|
require(not list(base.glob('acceptance-*.json')) and not list(base.glob('pending-acceptance-*.json')) and
|
|
not list(base.glob('recovery-*.json')) and not list(base.glob('pending-recovery-*.json')), 'no existing acceptance or recovery attempt')
|
|
return proof
|
|
def bound_partial(proof):
|
|
keys = 'transaction_sha256 job_payload_sha256 effect_armed_ledger_id grant_id job_id attempt_id authorization_id plan_sha256'.split()
|
|
value = json.loads(run(['/usr/bin/python3','-I',str(provider),'verify-partial'], input=canonical({k:proof[k] for k in keys})))
|
|
require(value['effect_state'] == 'known_failed' and value['code'] == 'APT_PARTIAL_UNCONFIGURED_VERIFIED' and
|
|
{k:v for k,v in value['proof'].items() if k != 'checked_at_ms'} ==
|
|
{k:v for k,v in proof.items() if k != 'checked_at_ms'}, 'fresh original-bound partial state required')
|
|
require(json.loads((state/'identity.json').read_text())['agent_id'] == payload['agent_id'], 'permanent Cloud identity required')
|
|
require(not dropin.is_symlink() and dropin.read_text() == reduced, 'APT endpoint must remain absent')
|
|
require(run(['systemctl','show','rmm-apt-helper.service','-p','ActiveState','--value']).strip() == b'inactive', 'APT helper must be stopped')
|
|
for name, pin in payload['old_files'].items():
|
|
p = Path(name); require(not p.is_symlink() and p.stat().st_uid == 0 and p.stat().st_mode & 0o022 == 0 and sha(p.read_bytes()) == pin, 'installed file drift')
|
|
require(Path('/etc/systemd/system/rmm-apt-helper.service').read_text() == payload['unit'], 'exact reviewed APT service required')
|
|
markers = {base/'bootstrap-attempt-rmm-apt-fixture-install-1.0.json':'ec84aa4df8065f0b54b282bd273a6f342f07a6d47312289608389d05c7fb6053',
|
|
base/'bootstrap-retry-rmm-apt-fixture-install-1.0.json':'a118072812553dca1a8ffee84550be0b2d514d555ad7cbcfc4722675a211a0e3'}
|
|
require({p.name for p in base.glob('bootstrap-*.json')} == {p.name for p in markers} and
|
|
all(sha(p.read_bytes()) == pin for p,pin in markers.items()), 'original/retry tombstones required')
|
|
partial = guard(); bound_partial(partial)
|
|
raw = {name:base64.b64decode(payload[name], validate=True) for name in ('helper','agent','provider')}
|
|
pins = {'helper':'ab7bbc87c75f68c1aa3d35941db9a2ece8d398f92a67e655b1d420bbd3287bd9',
|
|
'agent':'44ae91151ff08a186ce4dff587697eb604cb7c0a22f0c9d618724a9ba8a67234',
|
|
'provider':'5ff260f75cb2959678f6be729d3a806a4ea29febd5ae4003317fed41afc25327'}
|
|
require(all(sha(raw[name]) == pin == payload[name+'_sha256'] for name,pin in pins.items()), 'qualified exact recovery runtime required')
|
|
for name in ('helper','agent'):
|
|
require(raw[name][:6] == b'\x7fELF\x02\x01' and raw[name][18:20] == b'\x3e\x00', 'Linux x64 binary required')
|
|
ast.parse(raw['provider'])
|
|
if payload.get('preflight_only') is True:
|
|
return dict(stage='preflight_validated', completed=False, preflight_only=True,
|
|
package_effects=False, maintenance_mutations=False, expected_grants=len(payload['expected_grants']),
|
|
partial_proof_sha256=payload['partial_proof_sha256'], apt_endpoint_enabled=False)
|
|
backup = Path('/var/lib/rmm-bootstrap-history')/('cloud-configure-recovery-'+payload['id']); backup.mkdir(mode=0o700)
|
|
report = dict(stage='validated', completed=False, backup=str(backup), package_effects=False,
|
|
apt_endpoint_enabled=False, partial_proof_sha256=payload['partial_proof_sha256'], backups={})
|
|
def save():
|
|
next_path = backup/'.report-next.json'; create(next_path,json.dumps(report,indent=2).encode())
|
|
os.replace(next_path,backup/'report.json'); fsync_dir(backup)
|
|
def replace(path, staged=None):
|
|
target = backup/str(path).lstrip('/').replace('/','__')
|
|
require(not path.is_symlink() and path.stat().st_dev == backup.stat().st_dev and not target.exists(), 'exclusive same-filesystem versioned replacement')
|
|
os.rename(path,target); fsync_dir(path.parent); fsync_dir(backup)
|
|
report['backups'][str(path)] = str(target); save()
|
|
if staged is not None:
|
|
os.rename(staged,path); fsync_dir(path.parent); fsync_dir(backup)
|
|
lock = None
|
|
try:
|
|
save()
|
|
for name in ('helper','agent','provider'): create(backup/(name+'-next'), raw[name], 0o755)
|
|
for name in ('helper','agent'): run(['/lib64/ld-linux-x86-64.so.2','--list',str(backup/(name+'-next'))])
|
|
run(['systemctl','stop','rmm-agent-remote.service','rmm-helper-cloud.service','rmm-apt-helper.service'])
|
|
lock = os.open(ledger.parent/'executor.lock', os.O_RDWR | os.O_NOFOLLOW)
|
|
fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
|
partial = guard(); bound_partial(partial)
|
|
preserved = {str(p):manifest(p) for p in (state,Path('/var/lib/rmm-agent'),ledger.parent)}
|
|
preserved_base = manifest(base)
|
|
witnesses = {}
|
|
for p in (Path('/var/lib/dpkg/status'),Path('/usr/share/rmm-mvp-test/version'),dropin,
|
|
Path('/etc/systemd/system/rmm-apt-helper.service'),Path('/usr/lib/rmm-apt-transaction/hook'),
|
|
Path('/etc/rmm-remote/general-helper-proof.json'),Path('/etc/rmm-remote/helper-general.json')):
|
|
s = p.stat(); witnesses[str(p)] = (s.st_dev,s.st_ino,sha(p.read_bytes()))
|
|
general_bytes = Path('/etc/rmm-remote/general-helper-proof.json').read_bytes()
|
|
general_config = Path('/etc/rmm-remote/helper-general.json').read_bytes()
|
|
old_metadata = json.loads(metadata.read_text()); old_config = json.loads(config.read_text())
|
|
require(old_metadata['schema_version'] == 2 and old_metadata['provider_sha256'] == sha(provider.read_bytes()) and
|
|
old_metadata['hook_sha256'] == sha(Path('/usr/lib/rmm-apt-transaction/hook').read_bytes()) and
|
|
old_metadata['unit_sha256'] == sha(payload['unit'].encode()), 'old install metadata required')
|
|
for path,name in ((helper,'helper'),(agent,'agent'),(provider,'provider')): replace(path,backup/(name+'-next'))
|
|
create(backup/'metadata-next',json.dumps(dict(old_metadata,provider_sha256=sha(raw['provider']))).encode()); replace(metadata,backup/'metadata-next')
|
|
command = ['/usr/bin/python3','-I',str(provider)]
|
|
config_bytes = run(command+['print-config']); config_sha = sha(config_bytes)
|
|
require(config_sha == payload['reviewed_config_sha256'] and all(line in config_bytes for line in (
|
|
b'DPkg::ConfigurePending "false";', b'DPkg::TriggersPending "false";', b'dpkg::selection::remove::approved "false";')), 'reviewed exact configure/no-pending settings')
|
|
runtime = json.loads(run(command+['inspect-runtime'])); runtime_sha = sha(canonical(runtime))
|
|
old_config['apt_provider'].update(runtime_sha256=runtime_sha,apt_config_sha256=config_sha)
|
|
create(backup/'config-next',json.dumps(old_config).encode()); replace(config,backup/'config-next')
|
|
pin = json.loads(run([str(helper),'--inspect-apt-profile'],env=dict(os.environ,RMM_HELPER_CONFIG=str(config))))
|
|
require(pin['identity'] == json.loads(general_bytes)['identity'], 'same helper identity required')
|
|
create(backup/'proof-next',json.dumps(pin).encode(),0o640,984); replace(proof_file,backup/'proof-next')
|
|
general = json.loads(run([str(helper),'--inspect-general-profile'],env=dict(os.environ,RMM_HELPER_CONFIG='/etc/rmm-remote/helper-general.json')))
|
|
require(general == json.loads(general_bytes) and Path('/etc/rmm-remote/helper-general.json').read_bytes() == general_config, 'General helper policy unchanged')
|
|
for name,values in preserved.items(): require(manifest(Path(name)) == values, 'identity/journal/Root ledger bytes or inode changed')
|
|
def preserved_original_base():
|
|
current = manifest(base)
|
|
require(all(current.get(name) == value for name,value in preserved_base.items() if name not in ('install-metadata.json','native-readiness.json')), 'original APT state/archive/hook/probe changed')
|
|
for name,value in witnesses.items():
|
|
p=Path(name);s=p.stat();require((s.st_dev,s.st_ino,sha(p.read_bytes())) == value, 'original package/control file bytes or inode changed')
|
|
return current
|
|
preserved_original_base(); bound_partial(guard())
|
|
report.update(stage='installed_preserved',preserved_state=True,helper_sha256=pins['helper'],agent_sha256=pins['agent'],
|
|
provider_sha256=pins['provider'],runtime_sha256=runtime_sha,apt_config_sha256=config_sha,apt_pin=pin,general_pin=general)
|
|
save()
|
|
# No client can reach this endpoint; it exists only for the service's
|
|
# ExecStartPre copied-database probe, then is stopped before restart.
|
|
run(['systemd-analyze','verify','/etc/systemd/system/rmm-apt-helper.service'])
|
|
replace(readiness_file)
|
|
# Daemon startup itself takes executor.lock to open the existing DB.
|
|
# Installation is complete; agent/General remain stopped during probe.
|
|
fcntl.flock(lock,fcntl.LOCK_UN);os.close(lock);lock=None
|
|
run(['systemctl','start','rmm-apt-helper.service'])
|
|
for _ in range(3):
|
|
status = run(['systemctl','show','rmm-apt-helper.service','-p','ActiveState','-p','SubState','-p','NRestarts']).decode()
|
|
require('ActiveState=active\n' in status and 'SubState=running\n' in status and 'NRestarts=0\n' in status, 'stable native probe helper required')
|
|
time.sleep(1)
|
|
run(['systemctl','stop','rmm-apt-helper.service'])
|
|
lock = os.open(ledger.parent/'executor.lock', os.O_RDWR | os.O_NOFOLLOW)
|
|
fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
|
ready = json.loads(readiness_file.read_text())
|
|
require(ready['runtime'] == runtime and ready['readiness_only'] is True and ready['native_acceptance'] is False and
|
|
ready['host_status_sha256'] == sha(Path('/var/lib/dpkg/status').read_bytes()), 'exact new copied-database probe required')
|
|
workspace = base/'probes'/str(uuid.UUID(ready['probe_id']))
|
|
require((workspace/'dpkg/status').read_bytes() == Path('/var/lib/dpkg/status').read_bytes() and
|
|
(workspace/'apt.conf').read_bytes().count(b'dpkg::selection::remove::approved "false";') == 1,
|
|
'partial copied status and configure fix required')
|
|
current = preserved_original_base()
|
|
require(all(name in preserved_base or name.startswith('probes/'+ready['probe_id']+'/') or name == 'probes/'+ready['probe_id']
|
|
for name in current), 'only generated readiness probe metadata may be added')
|
|
for name,values in preserved.items(): require(manifest(Path(name)) == values, 'journal/ledger bytes or inode changed during probe')
|
|
bound_partial(guard())
|
|
require(dropin.read_text() == reduced and Path('/etc/rmm-remote/general-helper-proof.json').read_bytes() == general_bytes, 'endpoint/General proof unchanged')
|
|
report.update(stage='native_probe_preserved',readiness_only=True,native_acceptance=False,
|
|
journals_preserved_before_restart=True,readiness_sha256=sha(readiness_file.read_bytes()),probe_id=ready['probe_id'])
|
|
save()
|
|
fcntl.flock(lock,fcntl.LOCK_UN);os.close(lock);lock=None
|
|
run(['systemctl','start','rmm-helper-cloud.service','rmm-agent-remote.service'])
|
|
require(run(['systemctl','show','rmm-apt-helper.service','-p','ActiveState','--value']).strip() == b'inactive', 'APT endpoint must remain stopped')
|
|
report.update(stage='qualified_runtime_started',completed=True);save()
|
|
except Exception as error:
|
|
report.update(error_class=type(error).__name__,frames=[dict(function=f.name,line=f.lineno) for f in traceback.extract_tb(error.__traceback__)])
|
|
save()
|
|
finally:
|
|
if lock is not None: fcntl.flock(lock,fcntl.LOCK_UN);os.close(lock)
|
|
subprocess.run(['systemctl','stop','rmm-apt-helper.service'],capture_output=True,timeout=30)
|
|
return report
|
|
|
|
|
|
def build_payload():
|
|
m = cloud.contract
|
|
checkpoint = m.protected_json(previous.apt.STATE/'apt_install_retry/checkpoint.json')
|
|
readonly = m.protected_json(STATE/'readonly-result-first.json')
|
|
require(readonly['unresolved'] == readonly['faults'] == 0 and readonly['original_result']['state'] == 'failed' and
|
|
readonly['original_result']['error_code'] == 'APT_PARTIAL_UNCONFIGURED_VERIFIED', 'actual closed partial evidence required')
|
|
require(len(readonly['proof']) == 1 and readonly['proof'][0]['grant_id'] == PARTIAL_GRANT and readonly['proof'][0]['proof_sha256'] == PARTIAL_PROOF, 'one reviewed actual partial proof')
|
|
folders = [cloud.ROOT/'devstate/cloud-root/cases'/n for n in ('service_setup','service_start','service_stop','apt_native_install')]
|
|
folders += [previous.apt.prior.STATE/n for n in ('diagnosis','diagnosis-acquisition','diagnosis-hook','diagnosis-local-hook')]
|
|
query = cloud.ROOT/'devstate/cloud-root/cases/query_logs/checkpoint.json'
|
|
if query.exists():
|
|
require(m.protected_json(query)['verified'] is True, 'actual completed query_logs checkpoint required')
|
|
folders.append(query.parent)
|
|
expected = [str(uuid.UUID(m.protected_json(p/'checkpoint.json')['result']['result']['grant_id'])) for p in folders] + [PARTIAL_GRANT]
|
|
require(len(expected) == len(set(expected)) and len(expected) in (9,10), 'exact known checkpoint grants required')
|
|
raw = {name:(STATE/filename).read_bytes() for name,filename in (
|
|
('helper','helper-configure-recovery'),('agent','agent-configure-recovery'),('provider','provider-configure-recovery.py'))}
|
|
require(all(hashlib.sha256(raw[name]).hexdigest() == pin for name,pin in PINS.items()), 'qualified artifacts changed')
|
|
require(raw['provider'] == (cloud.ROOT/'tools/apt-transaction/apt_transaction.py').read_bytes(), 'source/qualified provider mismatch')
|
|
spec = importlib.util.spec_from_file_location('reviewed_recovery_provider',cloud.ROOT/'tools/apt-transaction/apt_transaction.py')
|
|
module = importlib.util.module_from_spec(spec);sys.modules[spec.name]=module;spec.loader.exec_module(module)
|
|
return dict(id=str(uuid.uuid4()),agent_id=previous.apt.prior.identity()['agent_id'],expected_grants=expected,
|
|
partial_grant=PARTIAL_GRANT,partial_attempt=checkpoint['plan']['jobs'][0]['attempt_id'],partial_proof_sha256=PARTIAL_PROOF,
|
|
proof_row=readonly['proof'][0],unit=(cloud.ROOT/'deploy/linux/rmm-apt-helper.service').read_text(),
|
|
reviewed_config_sha256=hashlib.sha256(module.apt_config()).hexdigest(),
|
|
**{name:base64.b64encode(data).decode() for name,data in raw.items()}, **{name+'_sha256':pin for name,pin in PINS.items()})
|
|
|
|
|
|
def main():
|
|
parser=argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--preflight-only', action='store_true')
|
|
args=parser.parse_args()
|
|
os.umask(0o077);STATE.mkdir(mode=0o700,exist_ok=True)
|
|
require(not (STATE/'configure-runtime.json').exists(), 'existing qualified runtime retained; no implicit repeat')
|
|
require(previous.apt.prior.evidence('gate')['unresolved'] == 0, 'no unresolved central job')
|
|
policy = cloud.bootstrap.rpc('server.conf',closed_policy,{})
|
|
installed = cloud.contract.protected_json(cloud.ROOT/'devstate/remote-vm.json')
|
|
old = cloud.bootstrap.rpc('linux-vm.conf',installed_metadata,dict(
|
|
helper='5ad30cdf6e258df1efd05958ecdb6545e76568d84389a2800ed449b01118fc03',
|
|
agent=installed['binary_sha256'],provider='edd1950db26b3ca0b2c1335ff65bf88d5a83e3c946159cbcbb56d8f562252d70'))
|
|
payload = dict(build_payload(),old_files=old['files'],preflight_only=args.preflight_only)
|
|
report = cloud.bootstrap.rpc('linux-vm.conf',upgrade,payload)
|
|
if args.preflight_only:
|
|
require(report.get('preflight_only') is True and report.get('maintenance_mutations') is False, 'fixed preflight response required')
|
|
print(report);return
|
|
cloud.contract.write_private(STATE/('runtime-upgrade-'+payload['id']+'.json'),dict(report,closed_policy=policy))
|
|
require(report['completed'], 'preserve incomplete maintenance report; never repeat automatically')
|
|
previous.record_agent_upgrade(report,installed)
|
|
cloud.contract.write_private(STATE/'configure-runtime.json',report)
|
|
print({k:report[k] for k in ('completed','stage','preserved_state','journals_preserved_before_restart','readiness_only','apt_endpoint_enabled')})
|
|
|
|
|
|
if __name__ == '__main__':
|
|
try: main()
|
|
except Exception as error:
|
|
print('CLOUD_APT_RECOVERY_UPGRADE_INCOMPLETE:'+type(error).__name__)
|
|
prefix='remote bootstrap incomplete: '
|
|
if isinstance(error,RuntimeError) and str(error).startswith(prefix):
|
|
try:
|
|
detail=json.loads(str(error)[len(prefix):])
|
|
safe=dict(error_class=str(detail['error_class']),frames=[
|
|
dict(function=str(frame['function']),line=int(frame['line'])) for frame in detail['frames']])
|
|
print('REMOTE_BOOTSTRAP_INCOMPLETE:'+json.dumps(safe))
|
|
except (ValueError,TypeError,KeyError):pass
|
|
raise SystemExit(2) from None
|