diff --git a/deploy/cloud_finalize_bootstrap_trust.py b/deploy/cloud_finalize_bootstrap_trust.py index a922ac6..a27e3e7 100644 --- a/deploy/cloud_finalize_bootstrap_trust.py +++ b/deploy/cloud_finalize_bootstrap_trust.py @@ -125,3 +125,105 @@ def finalize_trust(payload): finally:fcntl.flock(lock,fcntl.LOCK_UN);os.close(lock) run(['systemctl','start','rmm-helper-cloud.service','rmm-agent-remote.service']) return report + + +def enable_general_autostart(payload): + """Enable the already running, qualified General unit; never start/reboot.""" + import contextlib + import sqlite3 + import stat + require(os.geteuid() == 0 and type(payload['apply']) is bool, 'explicit root preflight/apply') + state = Path('/var/lib/rmm-agent-remote'); root_state = Path('/var/lib/rmm-helper') + account = pwd.getpwnam('rmm-agent') + def owned(path,owner,limit): + require(not path.is_symlink(), 'fixed public configuration symlink') + s = path.stat() + require(stat.S_ISREG(s.st_mode) and s.st_uid == owner and not s.st_mode&0o022 and s.st_size <= limit, 'owned bounded public configuration') + return path.read_bytes() + unit = Path('/etc/systemd/system/rmm-helper-cloud.service') + require(hashlib.sha256(owned(unit,0,16384)).hexdigest() == payload['unit_sha256'] + and hashlib.sha256(owned(Path('/usr/local/libexec/rmm-privileged-helper'),0,16*1024*1024)).hexdigest() == payload['helper_sha256'], 'independently qualified General unit and binary') + identity = json.loads(owned(state/'identity.json',account.pw_uid,16384)) + binding = json.loads(owned(state/'binding.json',account.pw_uid,16384)) + cached = json.loads(owned(state/'job-signing-key.json',account.pw_uid,4096)) + public = bytes.fromhex(cached['public_key_hex']) + require(set(cached) == {'schema_version','algorithm','key_id','public_key_hex'} + and type(cached['schema_version']) is int and cached['schema_version'] == 1 + and cached['algorithm'] == 'Ed25519' and len(public) == 32 + and cached['key_id'] == hashlib.sha256(public).hexdigest() + and binding['public_key_hex'] == cached['public_key_hex'], 'cached pinned public trust') + require(identity['agent_id'] == binding['agent_id'] == payload['agent_id'] + and identity['asset_id'] == binding['asset_id'] == payload['asset_id'], 'permanent Cloud identity') + config = json.loads(owned(Path('/etc/rmm-remote/helper-general.json'),0,16384)) + pin = json.loads(owned(Path('/etc/rmm-remote/general-helper-proof.json'),0,16384)) + root_key = json.loads(owned(Path('/etc/rmm-remote/helper-signing-key.json'),0,4096)) + require(config['profile'] == pin['profile'] == 'general' and root_key == cached + and config['signing_key_file'] == '/etc/rmm-remote/helper-signing-key.json' + and config['binding']['public_key_hex'] in ('',cached['public_key_hex']) + and pin['identity']['signing_key_id'] == cached['key_id'] + and all(config['binding'][k] == binding[k] == pin['identity'][k] for k in + ('tenant_id','asset_id','agent_id','enrollment_generation','assignment_version')), 'independent General identity and public key match') + def services(): + result = {} + for name in ('rmm-agent-remote.service','rmm-helper-cloud.service','rmm-apt-helper.service'): + raw = subprocess.run(['systemctl','show',name,'-p','ActiveState','-p','MainPID','-p','UnitFileState','-p','FragmentPath','-p','NeedDaemonReload'],capture_output=True,check=True,timeout=10).stdout.decode() + result[name] = dict(line.split('=',1) for line in raw.splitlines()) + return result + before = services() + for name in ('rmm-agent-remote.service','rmm-helper-cloud.service'): + require(before[name]['ActiveState'] == 'active' and int(before[name]['MainPID']) > 1 + and before[name]['NeedDaemonReload'] == 'no', 'active qualified loaded service') + require(before['rmm-helper-cloud.service']['FragmentPath'] == str(unit) + and before['rmm-agent-remote.service']['UnitFileState'] == 'enabled' + and before['rmm-helper-cloud.service']['UnitFileState'] == 'disabled' + and before['rmm-apt-helper.service']['ActiveState'] == 'inactive', 'only existing General autostart gap') + endpoint = owned(Path('/etc/systemd/system/rmm-agent-remote.service.d/30-cloud-root.conf'),0,4096) + require(b'RMM_APT_HELPER_' not in endpoint, 'APT execution endpoint remains closed') + link = unit.parent/'multi-user.target.wants'/unit.name + require(not link.exists() and not link.is_symlink() and not link.parent.is_symlink() + and link.parent.stat().st_uid == 0 and not link.parent.stat().st_mode&0o022, 'new fixed enablement link only') + def retained(): + result = dict(boot_id=Path('/proc/sys/kernel/random/boot_id').read_text().strip(),inodes={}) + for path in (state/'journal.sqlite',state/'consumption.sqlite',root_state/'consumption.sqlite'): + require(not path.is_symlink(), 'fixed retained database') + owner = path.stat(); result['inodes'][str(path)] = [owner.st_dev,owner.st_ino] + sidecars = [Path(str(path)+suffix) for suffix in ('-wal','-shm')] + existing = {p for p in sidecars if p.exists()} + try: + with contextlib.closing(sqlite3.connect(path.as_uri()+'?mode=ro',uri=True)) as db: + require(db.execute('PRAGMA integrity_check').fetchone()[0] == 'ok', 'retained database integrity') + if path == state/'journal.sqlite': + require(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/faulted agent work') + else: + require(db.execute('SELECT count(*) FROM consumed WHERE result IS NULL').fetchone()[0] == 0, 'all effects definitively closed') + if path == root_state/'consumption.sqlite': + require(db.execute('SELECT count(*) FROM helper_sync_meta WHERE fault IS NOT NULL').fetchone()[0] == 0, 'no Root fault') + rows = db.execute('SELECT c.grant_id,c.agent,c.generation,c.job,c.attempt,c.result,e.start,e.outcome FROM consumed c LEFT JOIN execution_evidence e USING(grant_id) ORDER BY c.grant_id').fetchall() + values = [[dict(sha256=hashlib.sha256(v).hexdigest(),size=len(v)) if isinstance(v,bytes) else v for v in row] for row in rows] + result.update(root_consumed=len(rows),root_rows_sha256=hashlib.sha256(json.dumps(values,separators=(',',':')).encode()).hexdigest()) + finally: + for p in sidecars: + if p not in existing and p.exists(): + fd = os.open(p,os.O_RDONLY|os.O_NOFOLLOW) + try: + s = os.fstat(fd);require(stat.S_ISREG(s.st_mode) and s.st_nlink == 1 and s.st_uid in (0,owner.st_uid), 'only new fixed SQLite sidecar') + if s.st_uid == 0 and owner.st_uid != 0: os.fchown(fd,owner.st_uid,owner.st_gid) + finally: os.close(fd) + return result + original = retained() + report = dict(applied=False,preflight=True,public_pins_match=True,unit_sha256=payload['unit_sha256'], + helper_sha256=payload['helper_sha256'],retained=original,services_before=before, + jobs_created=False,service_restarted=False,reboot_requested=False) + if not payload['apply']: return report + subprocess.run(['systemctl','enable','rmm-helper-cloud.service'],capture_output=True,check=True,timeout=15) + require(link.is_symlink() and link.lstat().st_uid == 0 and link.resolve() == unit + and os.readlink(link) in (str(unit),'../'+unit.name), 'exact owned General multi-user target link') + require(hashlib.sha256(owned(unit,0,16384)).hexdigest() == payload['unit_sha256'], 'General unit bytes unchanged') + after = services() + require(after['rmm-helper-cloud.service']['UnitFileState'] == 'enabled' + and all(after[name]['MainPID'] == before[name]['MainPID'] + and after[name]['ActiveState'] == before[name]['ActiveState'] for name in before) + and retained() == original, 'enable only: same PIDs, boot, database inodes and Root consumption rows') + return dict(report,applied=True,preflight=False,services_after=after,general_autostart_enabled=True, + link_target=str(unit),state_preserved=True) diff --git a/docs/server-deployment.md b/docs/server-deployment.md index 1c563aa..004d588 100644 --- a/docs/server-deployment.md +++ b/docs/server-deployment.md @@ -4,6 +4,17 @@ Stand: 2026-11-13 · Spezifikation 1.13 · Linux-MVP weiterhin nicht abgenommen. ## Aktueller erhaltender Serverwechsel +**Spätere notwendige VM-Bootstrapfinalisierung:** Der vorhandene geprüfte +General-Helper war aktiv, aber disabled. Nach geschlossenem Offline-Ablauffall +und0offenen Agent-/Rootwirkungen ausschließlich`systemctl enable` dieser festen +Unit ausgeführt; kein`--now`, Dienststart/-restart oder Reboot. Tatsächlicher +vorheriger Readonly-Preflight und nachheriger Check bestätigten identische +öffentliche Schlüssel-/Gerätepins, Unit-/Helperbytes, Boot/PIDs, drei DBinodes +und Root-Verbrauchszeilen. Genau der root-owned Multiuser-Wants-Link zur +bekannten Unit wurde hinzugefügt. Agent bereits enabled; APTinactive/Endpunkt +weiter geschlossen. Private Reports`devstate/general-autostart-{preflight,enabled}.json`. +Dies ist ein Setup-/Autostartnachweis, kein ausgeführter Reboot. + Kandidat73aab983-fd45-4294-b812-03cb9823148b nativ auf ARM64 gebaut und explizit deployed. Build bewegte den Runtimetag nicht; bewusstes Deployment verwendete denselben qualifizierten Imagedigest. UI-Indexa3513f92 unverändert/CAS-geprüft,