feat: deploy cloud-only RMM and verify audited APT diagnostics
Rust development slice / linux (push) Failing after 7s
Rust development slice / linux (push) Failing after 7s
This commit is contained in:
@@ -0,0 +1,210 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Versioned cloud-VM helper upgrade and native readiness, never job approval.
|
||||
|
||||
Preserves existing device identities, journals, ledger inodes, archives and
|
||||
acceptance history. Refuses consumed/bound old helper state, active root helpers,
|
||||
unknown artifacts and partial previous installations. No package mutation,
|
||||
business test service setup, reboot, enrollment or database approval exists.
|
||||
"""
|
||||
import argparse
|
||||
import base64
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import uuid
|
||||
|
||||
import remote_client_bootstrap as transport
|
||||
import cloud_mvp_acceptance as acceptance
|
||||
|
||||
ROOT=transport.ROOT
|
||||
|
||||
|
||||
def install(payload):
|
||||
import sqlite3
|
||||
import fcntl
|
||||
import time
|
||||
os.umask(0o077)
|
||||
require(os.geteuid()==0,'root bootstrap required')
|
||||
require(str(uuid.UUID(payload['id']))==payload['id'],'fixed bootstrap id')
|
||||
account=pwd.getpwnam('rmm-agent');require((account.pw_uid,account.pw_gid)==(993,984),'agent account drift')
|
||||
unit='rmm-agent-remote.service'
|
||||
state=Path('/var/lib/rmm-agent-remote');ledger=Path('/var/lib/rmm-helper/consumption.sqlite')
|
||||
def run(args,**kwargs):
|
||||
return subprocess.run(args,capture_output=True,check=True,timeout=90,**kwargs).stdout
|
||||
def digest(data):return hashlib.sha256(data).hexdigest()
|
||||
def ledger_count(require_unbound=False):
|
||||
with sqlite3.connect(ledger.as_uri()+'?mode=ro',uri=True) as db:
|
||||
require(db.execute('PRAGMA integrity_check').fetchone()[0]=='ok','helper ledger corrupt')
|
||||
count=db.execute('SELECT count(*) FROM consumed').fetchone()[0]
|
||||
tables={row[0] for row in db.execute("SELECT name FROM sqlite_master WHERE type='table'")}
|
||||
if require_unbound:
|
||||
require('helper_sync_meta' not in tables or db.execute('SELECT count(*) FROM helper_sync_meta').fetchone()[0]==0,
|
||||
'existing helper identity binding; no rebind or reset')
|
||||
return count
|
||||
def manifest(path):
|
||||
values={}
|
||||
for item in path.rglob('*'):
|
||||
require(not item.is_symlink(),'state symlink')
|
||||
if item.is_file():values[str(item.relative_to(path))]=digest(item.read_bytes())
|
||||
return values
|
||||
def create(path,data,mode,uid=0,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(data);stream.flush();os.fsync(stream.fileno());os.fchown(stream.fileno(),uid,gid);os.fchmod(stream.fileno(),mode)
|
||||
for name in ('rmm-agent.service','rmm-helper-dev-test.service','rmm-helper-cloud.service','rmm-apt-helper.service'):
|
||||
require(run(['systemctl','show',name,'-p','ActiveState','--value']).strip()==b'inactive','old/root service not idle')
|
||||
require(ledger_count(require_unbound=True)==0,'consumed helper ledger; no implicit recovery')
|
||||
require(json.loads((state/'binding.json').read_text())==payload['binding'],'actual cloud binding mismatch')
|
||||
with sqlite3.connect((state/'journal.sqlite').as_uri()+'?mode=ro',uri=True) as db:
|
||||
require(db.execute("SELECT count(*) FROM job_attempts WHERE state NOT IN ('succeeded','failed','cancelled','expired')").fetchone()[0]==0,'nonterminal native jobs')
|
||||
base=Path('/var/lib/rmm-apt-transaction')
|
||||
require(not (base/'native-readiness.json').exists() and not list(base.glob('acceptance-*.json'))
|
||||
and not list(base.glob('bootstrap-attempt-*.json')) and not (base/'active').exists(),'existing probe/effect history requires reconciliation')
|
||||
old={
|
||||
'/usr/local/libexec/rmm-privileged-helper':'f867ac51e82fc7d0f0050cb7629e1ff92d8af9b493d6cb8ae4839accb5694e30',
|
||||
'/etc/systemd/system/rmm-apt-helper.service':'87c2fa242a604fbb9edb5612dd0ef1e703a362d8b465cb97fb3b7ccb5f781f36',
|
||||
'/usr/lib/rmm-apt-transaction/apt_transaction.py':payload['provider_sha256'],
|
||||
'/usr/lib/rmm-apt-transaction/hook':payload['hook_sha256'],
|
||||
}
|
||||
for name,pin in old.items():
|
||||
p=Path(name);require(not p.is_symlink() and p.stat().st_uid==0 and p.stat().st_mode&0o022==0
|
||||
and digest(p.read_bytes())==pin,'unexpected installed artifact')
|
||||
metadata=base/'install-metadata.json'
|
||||
old_metadata=json.loads(metadata.read_text())
|
||||
require(old_metadata==dict(schema_version=2,provider_sha256=payload['provider_sha256'],hook_sha256=payload['hook_sha256'],
|
||||
unit_sha256=old['/etc/systemd/system/rmm-apt-helper.service']),'unexpected provider metadata')
|
||||
destination={
|
||||
'key':'/etc/rmm-remote/helper-signing-key.json','general_config':'/etc/rmm-remote/helper-general.json',
|
||||
'general_pin':'/etc/rmm-remote/general-helper-proof.json','apt_pin':'/etc/rmm-remote/apt-helper-proof.json',
|
||||
'general_unit':'/etc/systemd/system/rmm-helper-cloud.service',
|
||||
'agent_dropin':'/etc/systemd/system/rmm-agent-remote.service.d/30-cloud-root.conf',
|
||||
'log_units':'/etc/rmm-remote/log-units.json',
|
||||
}
|
||||
for value in destination.values():require(not Path(value).exists() and not Path(value).is_symlink(),'existing cloud bootstrap path')
|
||||
test_unit=Path('/etc/systemd/system/rmm-mvp-test.service')
|
||||
require(test_unit.is_file() and not test_unit.is_symlink() and test_unit.stat().st_uid==0
|
||||
and test_unit.stat().st_size==0 and test_unit.stat().st_mode&0o777==0o600 and test_unit.stat().st_nlink==1,
|
||||
'expected precreated empty test unit required; no business setup over SSH')
|
||||
history=Path('/var/lib/rmm-bootstrap-history');require(history.is_dir() and not history.is_symlink()
|
||||
and history.stat().st_uid==0 and history.stat().st_mode&0o777==0o700,'unsafe bootstrap history')
|
||||
backup=history/('cloud-root-'+payload['id']);backup.mkdir(mode=0o700)
|
||||
report=dict(stage='validated',backups={},created=[],package_effects=False,rmm_jobs_executed=False)
|
||||
def save():
|
||||
p=backup/'.report-next.json'
|
||||
create(p,json.dumps(report,indent=2).encode(),0o600);os.replace(p,backup/'report.json')
|
||||
def retain(path):
|
||||
p=Path(path);target=backup/str(p).lstrip('/').replace('/','__')
|
||||
require(p.stat().st_dev==backup.stat().st_dev and not target.exists(),'backup must be exclusive same-filesystem')
|
||||
os.rename(p,target);report['backups'][str(p)]=str(target);save()
|
||||
agent_stopped=False
|
||||
try:
|
||||
save()
|
||||
raw=base64.b64decode(payload['binary'],validate=True)
|
||||
require(digest(raw)==payload['helper_sha256'] and raw[:6]==b'\x7fELF\x02\x01' and raw[18:20]==b'\x3e\x00','helper artifact mismatch')
|
||||
staged=backup/'rmm-privileged-helper';create(staged,raw,0o755)
|
||||
run(['/lib64/ld-linux-x86-64.so.2','--list',str(staged)])
|
||||
run(['systemctl','stop',unit]);agent_stopped=True
|
||||
report['agent_before']=manifest(state);report['old_agent_before']=manifest(Path('/var/lib/rmm-agent'))
|
||||
root_before=manifest(ledger.parent);ledger_inode=(ledger.stat().st_dev,ledger.stat().st_ino)
|
||||
for path in ('/usr/local/libexec/rmm-privileged-helper','/etc/systemd/system/rmm-apt-helper.service',
|
||||
'/etc/rmm/apt-helper.json',str(metadata)):retain(path)
|
||||
os.rename(staged,'/usr/local/libexec/rmm-privileged-helper')
|
||||
create(Path('/etc/systemd/system/rmm-apt-helper.service'),payload['apt_unit'].encode(),0o644)
|
||||
create(metadata,json.dumps(dict(old_metadata,unit_sha256=digest(payload['apt_unit'].encode()))).encode(),0o600)
|
||||
create(Path(destination['key']),json.dumps(payload['key']).encode(),0o600)
|
||||
create(Path(destination['general_unit']),payload['general_unit'].encode(),0o644)
|
||||
cmd=['/usr/bin/python3','-I','/usr/lib/rmm-apt-transaction/apt_transaction.py']
|
||||
runtime=json.loads(run(cmd+['inspect-runtime']));config_bytes=run(cmd+['print-config'])
|
||||
runtime_sha=digest(json.dumps(runtime,sort_keys=True,separators=(',',':'),ensure_ascii=True).encode())
|
||||
general=dict(profile='general',binding=dict(payload['binding'],public_key_hex=''),signing_key_file=destination['key'],
|
||||
agent_uid=993,socket_gid=984,socket='/run/rmm-helper-cloud/control.sock',state='/var/lib/rmm-helper',
|
||||
allowed_services=['rmm-mvp-test.service'],allow_root_scripts=True,allow_reboot=False,apt_provider=None)
|
||||
apt=dict(general,profile='apt_only',socket='/run/rmm-apt-helper/helper.sock',allowed_services=[],allow_root_scripts=False,
|
||||
apt_provider=dict(schema_version=1,execution_profile='dev-test',allow_fixture_bootstrap=True,
|
||||
allowed_recipes=['rmm-apt-fixture-install-1.0','rmm-apt-fixture-update-1.1'],runtime_sha256=runtime_sha,apt_config_sha256=digest(config_bytes)))
|
||||
create(Path(destination['general_config']),json.dumps(general).encode(),0o600)
|
||||
create(Path('/etc/rmm/apt-helper.json'),json.dumps(apt).encode(),0o600)
|
||||
pins={}
|
||||
for name,config,mode in [('general',destination['general_config'],'--inspect-general-profile'),('apt','/etc/rmm/apt-helper.json','--inspect-apt-profile')]:
|
||||
data=run(['/usr/local/libexec/rmm-privileged-helper',mode],env=dict(os.environ,RMM_HELPER_CONFIG=config))
|
||||
pin=json.loads(data);require(pin['identity']['agent_id']==payload['binding']['agent_id'] and
|
||||
pin['identity']['signing_key_id']==payload['key']['key_id'],'inspected authority differs')
|
||||
create(Path(destination[name+'_pin']),data,0o640,0,984);pins[name]=pin
|
||||
require(manifest(state)==report['agent_before'] and manifest(Path('/var/lib/rmm-agent'))==report['old_agent_before']
|
||||
and manifest(ledger.parent)==root_before and (ledger.stat().st_dev,ledger.stat().st_ino)==ledger_inode,
|
||||
'installation altered preserved identity/journals/ledger')
|
||||
report.update(stage='installed_preserved',pins=pins,runtime=runtime,runtime_sha256=runtime_sha,apt_config_sha256=digest(config_bytes),
|
||||
preserved_before_start=True);save()
|
||||
for name in ('rmm-helper-cloud.service','rmm-apt-helper.service'):
|
||||
run(['systemd-analyze','verify','/etc/systemd/system/'+name])
|
||||
run(['systemctl','daemon-reload'])
|
||||
report['stage']='native_apt_probe';save()
|
||||
start=subprocess.run(['systemctl','start','rmm-apt-helper.service'],capture_output=True,timeout=90)
|
||||
report['apt_start_exit']=start.returncode;report['native_readiness']=(base/'native-readiness.json').exists();save()
|
||||
require(start.returncode==0 and report['native_readiness'],'native APT readiness failed; no root jobs')
|
||||
run(['systemctl','start','rmm-helper-cloud.service'])
|
||||
# Type=simple acknowledges spawn before clock/sync initialization. A
|
||||
# returned start command is not daemon readiness or acceptance evidence.
|
||||
for _ in range(10):
|
||||
for name in ('rmm-helper-cloud.service','rmm-apt-helper.service'):
|
||||
status=run(['systemctl','show',name,'-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,
|
||||
'helper daemon not stably ready')
|
||||
time.sleep(1)
|
||||
with sqlite3.connect(ledger.as_uri()+'?mode=ro',uri=True) as db:
|
||||
require(db.execute('SELECT count(*) FROM helper_sync_meta').fetchone()[0]==1,'root ledger binding not initialized')
|
||||
dropin=('[Service]\nEnvironment=RMM_HELPER_SOCKET=/run/rmm-helper-cloud/control.sock\n'
|
||||
'Environment=RMM_HELPER_POLICY_FILE='+destination['general_pin']+'\n'
|
||||
'Environment=RMM_APT_HELPER_SOCKET=/run/rmm-apt-helper/helper.sock\n'
|
||||
'Environment=RMM_APT_HELPER_PROOF_FILE='+destination['apt_pin']+'\n'
|
||||
'Environment=RMM_LOG_UNITS_FILE='+destination['log_units']+'\n')
|
||||
create(Path(destination['log_units']),b'["rmm-mvp-test.service"]\n',0o600,993,984)
|
||||
create(Path(destination['agent_dropin']),dropin.encode(),0o644)
|
||||
run(['systemctl','daemon-reload'])
|
||||
report.update(stage='helpers_started_no_jobs',completed=True);save()
|
||||
except Exception as error:
|
||||
report.update(error_class=type(error).__name__,error_stage=report['stage'])
|
||||
# No effects have been authorized here. Stop only own helpers while the
|
||||
# ledger is still empty. Preserve every backup/probe/acceptance file.
|
||||
if ledger_count()==0:
|
||||
for name in ('rmm-apt-helper.service','rmm-helper-cloud.service'):
|
||||
subprocess.run(['systemctl','stop',name],capture_output=True,timeout=30)
|
||||
save()
|
||||
finally:
|
||||
if agent_stopped:run(['systemctl','start',unit])
|
||||
return dict(completed=report.get('completed',False),stage=report['stage'],error_class=report.get('error_class'),
|
||||
native_readiness=report.get('native_readiness',False),backup=str(backup),pins=report.get('pins'),
|
||||
runtime_sha256=report.get('runtime_sha256'),apt_config_sha256=report.get('apt_config_sha256'),
|
||||
preserved_before_start=report.get('preserved_before_start',False),root_jobs_executed=False)
|
||||
|
||||
|
||||
def main():
|
||||
parser=argparse.ArgumentParser(description=__doc__);parser.add_argument('--install-reviewed-helper',action='store_true',required=True);parser.parse_args()
|
||||
os.umask(0o077)
|
||||
installed=acceptance.contract.protected_json(ROOT/'devstate/remote-vm.json');identity=installed['identity']
|
||||
gate=transport.rpc('server.conf',acceptance.server,dict(kind='gate',tenant_id=transport.TENANT,agent_id=identity['agent_id']))
|
||||
require(gate['unresolved']==0,'unresolved central job')
|
||||
metadata=transport.rpc('server.conf',transport.server,dict(action='observe',agent_id=identity['agent_id']))
|
||||
material=transport.rpc('server.conf',transport.server,dict(action='material'))
|
||||
binding={k:metadata[k] for k in ('agent_id','asset_id','enrollment_generation','assignment_version')}
|
||||
binding.update(tenant_id=transport.TENANT,public_key_hex=material['key']['public_key_hex'])
|
||||
data=(ROOT/'devstate/cloud-root/rmm-privileged-helper').read_bytes()
|
||||
payload=dict(id=str(uuid.uuid4()),binding=binding,key=material['key'],binary=base64.b64encode(data).decode(),
|
||||
helper_sha256=hashlib.sha256(data).hexdigest(),
|
||||
provider_sha256=hashlib.sha256((ROOT/'tools/apt-transaction/apt_transaction.py').read_bytes()).hexdigest(),
|
||||
hook_sha256=hashlib.sha256((ROOT/'tools/apt-transaction/hook').read_bytes()).hexdigest(),
|
||||
apt_unit=(ROOT/'deploy/linux/rmm-apt-helper.service').read_text(),
|
||||
general_unit=(ROOT/'deploy/linux/rmm-helper-cloud.service').read_text())
|
||||
result=transport.rpc('linux-vm.conf',install,payload)
|
||||
p=ROOT/'devstate/cloud-root'/('install-'+payload['id']+'.json');p.write_text(json.dumps(result,indent=2));p.chmod(0o600)
|
||||
print(json.dumps({k:v for k,v in result.items() if k not in ('pins','backup')}))
|
||||
require(result['completed'],'bootstrap incomplete; preserve report and diagnose, never repeat installation')
|
||||
|
||||
|
||||
require=transport.require
|
||||
if __name__=='__main__':
|
||||
try:main()
|
||||
except Exception:
|
||||
print('CLOUD_ROOT_BOOTSTRAP_INCOMPLETE; private diagnostic report retained')
|
||||
raise SystemExit(2) from None
|
||||
Reference in New Issue
Block a user