Files
rust-discovery/deploy/cloud_finalize_bootstrap_trust.py
T
2026-11-13 21:24:35 +01:00

291 lines
22 KiB
Python

"""Finalize existing pinned public trust; no enrollment/key rotation/RMM job."""
def finalize_trust(payload):
import contextlib
import fcntl
import grp
import sqlite3
import stat
os.umask(0o077)
require(type(payload['apply']) is bool, 'explicit preflight/apply direction')
identifier = str(uuid.UUID(payload['id']))
state = Path('/var/lib/rmm-agent-remote'); root_state = Path('/var/lib/rmm-helper')
account = pwd.getpwnam('rmm-agent')
require(os.geteuid() == 0 and account.pw_uid == 993 and account.pw_gid > 0
and account.pw_gid == grp.getgrnam('rmm-agent').gr_gid, 'fixed named unprivileged agent')
def read(path,owner,limit=16384):
require(not path.is_symlink(), 'public trust/config file 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, 'bounded owned public trust/config file')
return path.read_bytes()
binding = json.loads(read(state/'binding.json',account.pw_uid))
identity = json.loads(read(state/'identity.json',account.pw_uid))
require(binding['agent_id'] == identity['agent_id'] == payload['agent_id']
and binding['asset_id'] == identity['asset_id'] == payload['asset_id'], 'same permanent provisioned identity')
def key(path,owner):
value = json.loads(read(path,owner,4096))
require(set(value) == {'schema_version','algorithm','key_id','public_key_hex'}
and type(value['schema_version']) is int and value['schema_version'] == 1
and value['algorithm'] == 'Ed25519', 'fixed public signing-key schema')
public = bytes.fromhex(value['public_key_hex'])
require(len(public) == 32 and value['key_id'] == hashlib.sha256(public).hexdigest(), 'valid public signing-key ID')
return value
cached = key(state/'job-signing-key.json',account.pw_uid)
helper_key = key(Path('/etc/rmm-remote/helper-signing-key.json'),0)
general_path = Path('/etc/rmm-remote/helper-general.json')
general = json.loads(read(general_path,0)); pin_path = Path('/etc/rmm-remote/general-helper-proof.json')
pin = json.loads(read(pin_path,0))
require(binding['public_key_hex'] == cached['public_key_hex'] and cached == helper_key
and general['profile'] == 'general' and general['agent_uid'] == account.pw_uid
and general['socket_gid'] == account.pw_gid
and general['signing_key_file'] == '/etc/rmm-remote/helper-signing-key.json'
and general['binding']['public_key_hex'] in ('',cached['public_key_hex']), 'independently provisioned public keys must agree')
fields = ('tenant_id','asset_id','agent_id','enrollment_generation','assignment_version')
require(all(general['binding'][k] == binding[k] == pin['identity'][k] for k in fields)
and pin['profile'] == 'general' and pin['identity']['signing_key_id'] == cached['key_id'], 'General public identity and signing pin must agree')
agent = Path('/usr/local/libexec/rmm-agent-remote'); helper = Path('/usr/local/libexec/rmm-privileged-helper')
require(hashlib.sha256(read(agent,0,32*1024*1024)).hexdigest() == payload['binary_sha256']
and hashlib.sha256(read(helper,0,16*1024*1024)).hexdigest() == payload['helper_sha256'], 'qualified current binaries')
env_path = Path('/etc/rmm-remote/agent.env'); environment = read(env_path,0).decode().splitlines()
require([line for line in environment if line.startswith('RMM_JOB_SIGNING_KEY_BOOTSTRAP=')] == ['RMM_JOB_SIGNING_KEY_BOOTSTRAP=1']
and 'RMM_JOB_BINDING_FILE=/var/lib/rmm-agent-remote/binding.json' in environment, 'original completed bootstrap environment')
dropin = Path('/etc/systemd/system/rmm-agent-remote.service.d/80-cloud-provisioned-trust.conf')
require(not dropin.exists() and not dropin.is_symlink() and not dropin.parent.is_symlink()
and dropin.parent.stat().st_uid == 0 and not dropin.parent.stat().st_mode&0o022, 'new owned trust-finalization dropin only')
def settled():
for path,sql in ((state/'journal.sqlite',"SELECT count(*) FROM job_attempts WHERE state NOT IN ('succeeded','failed','cancelled','expired')"),
(state/'consumption.sqlite','SELECT count(*) FROM consumed WHERE result IS NULL'),
(root_state/'consumption.sqlite','SELECT count(*) FROM consumed WHERE result IS NULL')):
require(not path.is_symlink(), 'fixed SQLite database')
owner = path.stat(); 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'
and db.execute(sql).fetchone()[0] == 0, 'no unresolved effect before trust finalization')
if path == state/'journal.sqlite':
require(db.execute('SELECT count(*) FROM reconcile_meta WHERE fault IS NOT NULL').fetchone()[0] == 0, 'no agent fault')
if path == root_state/'consumption.sqlite':
require(db.execute('SELECT count(*) FROM helper_sync_meta WHERE fault IS NOT NULL').fetchone()[0] == 0, 'no helper fault')
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), 'own newly created 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)
settled()
report = dict(preflight=True,applied=False,public_pins_match=True,signing_key_id=cached['key_id'],
jobs_created=False,keys_rotated=False,original_state_unchanged=True)
if not payload['apply']: return report
def run(command):return subprocess.run(command,capture_output=True,check=True,timeout=15).stdout
run(['systemctl','stop','rmm-agent-remote.service','rmm-helper-cloud.service'])
lock = os.open(root_state/'executor.lock',os.O_RDWR|os.O_NOFOLLOW)
fcntl.flock(lock,fcntl.LOCK_EX|fcntl.LOCK_NB)
try:
settled()
preserved = (state/'binding.json',state/'identity.json',state/'job-signing-key.json',
state/'journal.sqlite',state/'consumption.sqlite',root_state/'consumption.sqlite',
env_path,general_path,pin_path,Path('/etc/rmm-remote/helper-signing-key.json'),agent,helper)
def witnesses():
result = {}
for path in preserved:
require(not path.is_symlink(), 'preserved fixed state symlink')
s = path.stat()
result[str(path)] = dict(device=s.st_dev,inode=s.st_ino,size=s.st_size,
uid=s.st_uid,gid=s.st_gid,mode=stat.S_IMODE(s.st_mode),
sha256=hashlib.sha256(path.read_bytes()).hexdigest())
return result
before = witnesses()
folder = Path('/var/lib/rmm-bootstrap-history')/('cloud-provisioned-trust-'+identifier)
folder.mkdir(mode=0o700)
# EnvironmentFile overrides Environment. UnsetEnvironment is applied
# last by systemd, so the preserved original file cannot force HTTP.
raw = b'[Service]\nUnsetEnvironment=RMM_JOB_SIGNING_KEY_BOOTSTRAP\n'
fd = os.open(dropin,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,0o644)
with os.fdopen(fd,'wb') as stream:
stream.write(raw);stream.flush();os.fchmod(stream.fileno(),0o644);os.fsync(stream.fileno())
fd = os.open(dropin.parent,os.O_RDONLY|os.O_DIRECTORY|os.O_NOFOLLOW)
try:os.fsync(fd)
finally:os.close(fd)
run(['systemctl','daemon-reload'])
removed = run(['systemctl','show','rmm-agent-remote.service','-p','UnsetEnvironment','--value']).decode().split()
require('RMM_JOB_SIGNING_KEY_BOOTSTRAP' in removed and witnesses() == before, 'compiled final unset and original bytes/inodes unchanged')
report.update(preflight=False,applied=True,bootstrap_network_fetch_disabled=True,
preserved_before_restart=True,dropin_sha256=hashlib.sha256(raw).hexdigest(),
public_trust_sha256=hashlib.sha256(json.dumps({str(p):before[str(p)]['sha256'] for p in
(state/'binding.json',state/'job-signing-key.json',Path('/etc/rmm-remote/helper-signing-key.json'),general_path,pin_path)},sort_keys=True).encode()).hexdigest(),
backup=str(folder))
fd = os.open(folder/'report.json',os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,0o600)
with os.fdopen(fd,'w') as stream:json.dump(report,stream);stream.flush();os.fsync(stream.fileno())
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)
def legacy_helper_unit_metadata(payload):
import stat
p=Path('/etc/systemd/system/rmm-helper-dev-test.service');s=p.lstat()
require(stat.S_ISREG(s.st_mode) and s.st_uid==0 and not s.st_mode&0o022 and s.st_size<=16384,'owned historical unit')
raw=p.read_bytes();lines=[line.strip() for line in raw.decode().splitlines()]
commands=[line for line in lines if line.startswith('ExecStart=')]
shared='StateDirectory=rmm-helper' in lines
exact=commands==['ExecStart=/usr/local/libexec/rmm-privileged-helper']
require(exact and shared,'known historical helper sharing current Root state')
return dict(unit_sha256=hashlib.sha256(raw).hexdigest(),root_owned=True,
fixed_helper_binary=exact,shared_root_state=shared,
clock_query_filters=[line for line in lines if line in ('SystemCallFilter=adjtimex clock_adjtime','SystemCallFilter=adjtimex')])
def disable_legacy_helper_autostart(payload):
"""Disable only the stopped historical dev helper; never stop/start a unit."""
import contextlib
import sqlite3
import stat
require(os.geteuid()==0,'root bootstrap required')
unit=Path('/etc/systemd/system/rmm-helper-dev-test.service')
s=unit.lstat()
require(stat.S_ISREG(s.st_mode) and s.st_uid==0 and not s.st_mode&0o022
and hashlib.sha256(unit.read_bytes()).hexdigest()==payload['unit_sha256'],
'exact owned historical helper unit')
state=Path('/var/lib/rmm-agent-remote')
require(json.loads((state/'identity.json').read_text())['agent_id']==payload['agent_id'],
'permanent Cloud identity')
def inspect():
services={}
for name in ('rmm-helper-dev-test.service','rmm-agent-remote.service','rmm-helper-cloud.service','rmm-apt-helper.service'):
raw=subprocess.run(['systemctl','show',name,'-p','ActiveState','-p','MainPID','-p','UnitFileState'],capture_output=True,check=True,timeout=10).stdout.decode()
services[name]=dict(line.split('=',1) for line in raw.splitlines())
result=dict(services=services,boot_id=Path('/proc/sys/kernel/random/boot_id').read_text().strip(),inodes={})
for path in (state/'journal.sqlite',state/'consumption.sqlite',Path('/var/lib/rmm-helper/consumption.sqlite')):
m=path.stat();result['inodes'][str(path)]=[m.st_dev,m.st_ino]
with contextlib.closing(sqlite3.connect(path.as_uri()+'?mode=ro',uri=True)) as db:
require(db.execute('PRAGMA integrity_check').fetchone()[0]=='ok','preserved journal 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,'no active job during bootstrap')
else:require(db.execute('SELECT count(*) FROM consumed WHERE result IS NULL').fetchone()[0]==0,'no unresolved consumption')
if path.parent==Path('/var/lib/rmm-helper'):
rows=db.execute('SELECT c.grant_id,c.result,e.start,e.outcome FROM consumed c JOIN execution_evidence e USING(grant_id) ORDER BY c.grant_id').fetchall()
result['root_rows_sha256']=hashlib.sha256(json.dumps([[hashlib.sha256(v).hexdigest() if isinstance(v,bytes) else v for v in row] for row in rows],separators=(',',':')).encode()).hexdigest()
return result
before=inspect();old=before['services']['rmm-helper-dev-test.service']
require(old['ActiveState']=='inactive' and old['MainPID']=='0' and old['UnitFileState']=='enabled','only stopped enabled legacy helper')
require(all(before['services'][n]['ActiveState']=='active' and before['services'][n]['UnitFileState']=='enabled'
for n in ('rmm-agent-remote.service','rmm-helper-cloud.service'))
and before['services']['rmm-apt-helper.service']['ActiveState']=='inactive','current services already correct')
subprocess.run(['systemctl','disable','rmm-helper-dev-test.service'],capture_output=True,check=True,timeout=15)
after=inspect()
require(after['services']['rmm-helper-dev-test.service']['UnitFileState']=='disabled'
and all(after['services'][n]['MainPID']==v['MainPID'] and after['services'][n]['ActiveState']==v['ActiveState']
for n,v in before['services'].items())
and all(after[k]==before[k] for k in ('boot_id','inodes','root_rows_sha256'))
and hashlib.sha256(unit.read_bytes()).hexdigest()==payload['unit_sha256'],'disable only, original state preserved')
return dict(verified=True,before=before,after=after,jobs_created=False,service_restarted=False,
reboot_requested=False,legacy_autostart_disabled=True)