226 lines
15 KiB
Python
226 lines
15 KiB
Python
#!/usr/bin/env python3
|
|
"""Read-only prerequisite snapshot for preserving Linux updater bootstrap.
|
|
|
|
No apply/start/stop/reenroll/package/policy operation is implemented here.
|
|
Credentials use the existing strict parser and independently pinned SSH host.
|
|
Private keys are stat'ed only; no private key or environment bytes are read.
|
|
"""
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import uuid
|
|
import remote_client_bootstrap as transport
|
|
ROOT=Path(__file__).resolve().parents[1]
|
|
|
|
def inspect_vm(payload):
|
|
import contextlib
|
|
import hashlib
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import sqlite3
|
|
import stat
|
|
import subprocess
|
|
import time
|
|
import uuid
|
|
require(payload=={'operation':'linux_updater_bootstrap_inspect_v1'} and os.geteuid()==0
|
|
and os.uname().machine=='x86_64','fixed read-only Linux target')
|
|
def digest(raw):return hashlib.sha256(raw).hexdigest()
|
|
def encode(value):return json.dumps(value,separators=(',',':'),ensure_ascii=False).encode()
|
|
def identity(s):return dict(device=s.st_dev,inode=s.st_ino,uid=s.st_uid,gid=s.st_gid,mode=s.st_mode,links=s.st_nlink)
|
|
def metadata(path):
|
|
path=Path(path)
|
|
try:s=path.lstat()
|
|
except FileNotFoundError:return None
|
|
require(not stat.S_ISLNK(s.st_mode),'no metadata symlink')
|
|
return dict(identity=identity(s),bytes=s.st_size)
|
|
def public(path,limit=65536):
|
|
path=Path(path);s=path.lstat()
|
|
require(stat.S_ISREG(s.st_mode) and s.st_uid==0 and s.st_nlink==1
|
|
and not s.st_mode&0o022 and s.st_size<=limit,'bounded protected public input')
|
|
with path.open('rb') as stream:raw=stream.read(limit+1)
|
|
after=path.lstat()
|
|
require(len(raw)==s.st_size and identity(after)==identity(s) and after.st_size==s.st_size
|
|
and after.st_mtime_ns==s.st_mtime_ns and after.st_ctime_ns==s.st_ctime_ns,'stable public input')
|
|
return raw
|
|
def proof(path):
|
|
raw=public(path,256*1024*1024);s=Path(path).lstat()
|
|
return dict(identity=identity(s),bytes=len(raw),sha256=digest(raw))
|
|
def run(args):
|
|
result=subprocess.run(args,stdin=subprocess.DEVNULL,stdout=subprocess.PIPE,stderr=subprocess.PIPE,
|
|
timeout=10,env={'PATH':'/usr/bin:/bin','LC_ALL':'C'})
|
|
require(result.returncode in (0,1) and len(result.stdout)<=65536,'bounded read-only systemd response')
|
|
return result.stdout.decode()
|
|
services=('rmm-agent-remote.service','rmm-helper-cloud.service','rmm-full-maintenance-helper.service',
|
|
'rmm-agent-updater.service','rmm-apt-helper.service','rmm-agent.service','rmm-helper-dev-test.service')
|
|
names=list(services)
|
|
for name in services:
|
|
names.extend((name[:-8]+'.socket',name[:-8]+'.timer'))
|
|
timers=run(['systemctl','list-units','--all','--no-legend','--plain','--no-pager','rmm-general-startup-rollback-*.timer'])
|
|
rollback_names=[]
|
|
for line in timers.splitlines():
|
|
name=line.split()[0]
|
|
require(name.startswith('rmm-general-startup-rollback-') and name.endswith('.timer'),'fixed rollback timer')
|
|
uuid.UUID(name[len('rmm-general-startup-rollback-'):-len('.timer')])
|
|
rollback_names.append(name)
|
|
names.extend(sorted(rollback_names))
|
|
properties=['Id','LoadState','ActiveState','SubState','MainPID','ControlPID','InvocationID','UnitFileState',
|
|
'FragmentPath','DropInPaths','Triggers','TriggeredBy','Job','ControlGroup']
|
|
units={}
|
|
for name in names:
|
|
output=run(['systemctl','show','--all','--property='+','.join(properties),'--',name])
|
|
fields={}
|
|
for line in output.splitlines():
|
|
key,value=line.split('=',1);require(key in properties and key not in fields,'exact service properties')
|
|
fields[key]=value
|
|
require(fields.get('Id')==name,'exact queried unit')
|
|
composition=[]
|
|
if fields.get('LoadState')=='loaded':
|
|
paths=[fields['FragmentPath']]+fields.get('DropInPaths','').split()
|
|
for path in paths:composition.append((path,proof(path)))
|
|
units[name]=dict(status=fields,composition=composition,unit_sha256=digest(encode(composition)))
|
|
binaries={}
|
|
for path in ('/usr/local/libexec/rmm-agent-remote','/usr/local/libexec/rmm-privileged-helper','/usr/local/libexec/rmm-agent-updater'):
|
|
binaries[path]=proof(path) if Path(path).exists() else None
|
|
configs={}
|
|
for name in ('helper-general.json','helper-full-maintenance.json'):
|
|
path=Path('/etc/rmm-remote')/name
|
|
if not path.exists():configs[name]=None;continue
|
|
raw=public(path);value=json.loads(raw)
|
|
configs[name]=dict(sha256=digest(raw),fields={key:value.get(key) for key in (
|
|
'profile','agent_uid','socket_gid','socket','state','signing_key_file','allow_reboot','allow_root_scripts',
|
|
'allowed_script_profiles','helper_binary_sha256')},binding={key:value['binding'][key] for key in (
|
|
'tenant_id','asset_id','agent_id','enrollment_generation','assignment_version')})
|
|
public_pins={}
|
|
for name in ('general-helper-proof.json','full-maintenance-helper-proof.json','helper-signing-key.json'):
|
|
path=Path('/etc/rmm-remote')/name
|
|
public_pins[name]=proof(path) if path.exists() else None
|
|
state_paths=[
|
|
'/var/lib/rmm-agent-remote','/var/lib/rmm-agent-remote/journal.sqlite',
|
|
'/var/lib/rmm-agent-remote/consumption.sqlite','/var/lib/rmm-agent-remote/agent.lock',
|
|
'/var/lib/rmm-agent-remote/binding.json','/var/lib/rmm-agent-remote/identity.json',
|
|
'/var/lib/rmm-agent-remote/job-signing-key.json','/var/lib/rmm-agent-remote/device.key',
|
|
'/var/lib/rmm-agent-remote/device.pem','/var/lib/rmm-agent-remote/certificates',
|
|
'/var/lib/rmm-helper','/var/lib/rmm-helper/consumption.sqlite','/var/lib/rmm-helper/executor.lock',
|
|
'/etc/rmm-agent-updater','/var/lib/rmm-agent-updater','/etc/rmm-remote/agent.env']
|
|
selector=Path('/var/lib/rmm-agent-remote/certificates/current.json')
|
|
if selector.exists():
|
|
# Public selector only; active private key is metadata-only below.
|
|
s=selector.lstat();require(stat.S_ISREG(s.st_mode) and s.st_size<=4096,'public TLS selector')
|
|
current=json.loads(selector.read_bytes());version=str(uuid.UUID(current['version']))
|
|
state_paths.extend((str(selector),str(selector.parent/'versions'/version),
|
|
str(selector.parent/'versions'/version/'device.key'),str(selector.parent/'versions'/version/'device.pem')))
|
|
state={path:metadata(path) for path in state_paths}
|
|
databases={}
|
|
for path in ('/var/lib/rmm-agent-remote/journal.sqlite','/var/lib/rmm-agent-remote/consumption.sqlite','/var/lib/rmm-helper/consumption.sqlite'):
|
|
require(not Path(path+'-wal').exists() and not Path(path+'-shm').exists(),'existing DELETE journals required')
|
|
with contextlib.closing(sqlite3.connect('file:'+path+'?mode=ro',uri=True,timeout=2)) as db:
|
|
db.execute('PRAGMA query_only=ON')
|
|
require(db.execute('PRAGMA journal_mode').fetchone()[0]=='delete','read-only DELETE journal')
|
|
tables={row[0] for row in db.execute("SELECT name FROM sqlite_master WHERE type='table'")}
|
|
counts={name:db.execute('SELECT count(*) FROM '+name).fetchone()[0] for name in (
|
|
'consumed','execution_evidence','helper_sync_decisions','job_attempts','reconcile_decisions') if name in tables}
|
|
unresolved={}
|
|
if 'consumed' in tables:unresolved['consumed']=db.execute('SELECT count(*) FROM consumed WHERE result IS NULL').fetchone()[0]
|
|
if 'job_attempts' in tables:unresolved['job_attempts']=db.execute("SELECT count(*) FROM job_attempts WHERE state NOT IN ('succeeded','failed','cancelled','expired')").fetchone()[0]
|
|
faults={name:db.execute('SELECT count(*) FROM '+name+' WHERE fault IS NOT NULL').fetchone()[0]
|
|
for name in ('helper_sync_meta','reconcile_meta') if name in tables}
|
|
# Hash original evidence bytes inside the target; no stdout/script
|
|
# body or reconciliation request/signature is exported.
|
|
evidence=[]
|
|
if 'execution_evidence' in tables:
|
|
columns={r[1] for r in db.execute('PRAGMA table_info(execution_evidence)')}
|
|
for grant,start,outcome in db.execute('SELECT grant_id,start,outcome FROM execution_evidence'):
|
|
evidence.append(dict(grant_id=grant,start_sha256=digest(start),outcome_sha256=None if outcome is None else digest(outcome)))
|
|
databases[path]=dict(counts=counts,unresolved=unresolved,faults=faults,
|
|
evidence_sha256=digest(encode(sorted(evidence,key=lambda r:r['grant_id']))),
|
|
integrity=db.execute('PRAGMA integrity_check').fetchone()[0])
|
|
require(not Path(path+'-wal').exists() and not Path(path+'-shm').exists(),'no SQLite sidecars introduced')
|
|
host={}
|
|
for label,path in (('proc','/proc'),('user_namespace','/proc/1/ns/user'),('pid_namespace','/proc/1/ns/pid'),('cgroup_namespace','/proc/1/ns/cgroup')):
|
|
s=Path(path).stat();host[label+'_device']=s.st_dev;host[label+'_inode']=s.st_ino
|
|
return dict(schema_version=1,operation=payload['operation'],read_only=True,utc_seconds=int(time.time()),
|
|
boot_id=Path('/proc/sys/kernel/random/boot_id').read_text().strip(),units=units,binaries=binaries,
|
|
configs=configs,public_pins=public_pins,state=state,databases=databases,host_context=host,
|
|
rollback_timers=rollback_names,updater_provisioned=Path('/etc/rmm-agent-updater/policy.json').exists())
|
|
|
|
def prepare(snapshot_path,agent,helper,updater,qualification):
|
|
import stat
|
|
def require(ok,code):
|
|
if not ok:raise RuntimeError(code)
|
|
def input_file(path,limit):
|
|
path=Path(path);meta=path.lstat()
|
|
require(stat.S_ISREG(meta.st_mode) and not path.is_symlink() and meta.st_nlink==1
|
|
and meta.st_uid==os.getuid() and not meta.st_mode&0o022 and 0<meta.st_size<=limit,
|
|
'bounded owned preparation input')
|
|
raw=path.read_bytes()
|
|
require(len(raw)==meta.st_size,'stable preparation input size')
|
|
return raw
|
|
snapshot_raw=input_file(snapshot_path,1024*1024);snapshot=json.loads(snapshot_raw)
|
|
require(snapshot['operation']=='linux_updater_bootstrap_inspect_v1' and snapshot['read_only'] is True
|
|
and snapshot['updater_provisioned'] is False,'actual unprovisioned read-only snapshot')
|
|
require(all(not any(db['unresolved'].values()) and not any(db['faults'].values())
|
|
and db['integrity']=='ok' for db in snapshot['databases'].values()),'closed preserved original state')
|
|
receipt=input_file(qualification,1024*1024)
|
|
folder=ROOT/'devstate/linux-updater-bootstrap'/str(uuid.uuid4());folder.mkdir(mode=0o700,parents=True)
|
|
artifacts={}
|
|
for label,path in (('agent',agent),('helper',helper),('updater',updater)):
|
|
raw=input_file(path,256*1024*1024)
|
|
require(raw[:6]==b'\x7fELF\x02\x01' and raw[18:20]==b'\x3e\x00','Linux x86-64 ELF only')
|
|
target=folder/(label+'.bin')
|
|
fd=os.open(target,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,0o600)
|
|
with os.fdopen(fd,'wb') as stream:stream.write(raw);stream.flush();os.fsync(stream.fileno())
|
|
artifacts[label]=dict(file=target.name,bytes=len(raw),sha256=hashlib.sha256(raw).hexdigest())
|
|
manifest=dict(schema_version=1,profile='linux_updater_compatible_bootstrap_preparation_v1',
|
|
target='116.203.20.85',server='discovery.ease-technologies.de',boot_id=snapshot['boot_id'],
|
|
snapshot_sha256=hashlib.sha256(snapshot_raw).hexdigest(),
|
|
provided_qualification_document_sha256=hashlib.sha256(receipt).hexdigest(),
|
|
artifacts=artifacts,updater_unit_sha256=hashlib.sha256((ROOT/'deploy/linux/rmm-agent-updater.service').read_bytes()).hexdigest(),
|
|
bootstrap_installed=False,apply_executed=False,keys_generated=False,
|
|
original_evidence={path:dict(identity=snapshot['state'][path]['identity'],**value)
|
|
for path,value in snapshot['databases'].items()},
|
|
phases=['fresh_exact_read_only_CAS','close_central_test_scope_without_approval',
|
|
'stop_Agent_General_Full_only','hold_same_root_and_agent_locks',
|
|
'retain_original_DB_PKI_identity_and_history','install_compatible_baseline_images_and_Full_pins',
|
|
'start_General_Full_Agent_once_and_verify_signed_sync',
|
|
'record_actual_compatible_baseline_qualification','provision_separate_public_trust_and_stable_updater',
|
|
'bind_actual_unit_inode_registry_then_signed_baseline_and_local_policy',
|
|
'start_updater_once_and_verify_passive_full_observer',
|
|
'publish_distinct_signed_candidate','MCP_pending_plan_then_actual_WebUI_approval',
|
|
'verify_original_once_only_Apply_and_independent_health'],
|
|
notes=['Preparation performs no target mutation and grants no RMM job authority.',
|
|
'Qualification document hash is provenance only; root must review actual contents.',
|
|
'Baseline is compatible newly installed source; old currently installed binaries are historical backup only.',
|
|
'All actual file IDs/merged-unit hashes and registry are recaptured after installation.',
|
|
'APT remains inactive/disabled with old proof/config/history; no APT enable, package operation or reboot.'])
|
|
for name,raw in (('snapshot.json',snapshot_raw),('qualification.json',receipt),
|
|
('manifest.json',json.dumps(manifest,sort_keys=True,indent=2).encode()+b'\n')):
|
|
fd=os.open(folder/name,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,0o600)
|
|
with os.fdopen(fd,'wb') as stream:stream.write(raw);stream.flush();os.fsync(stream.fileno())
|
|
fd=os.open(folder,os.O_RDONLY|os.O_DIRECTORY|os.O_NOFOLLOW)
|
|
try:os.fsync(fd)
|
|
finally:os.close(fd)
|
|
return folder
|
|
|
|
def main():
|
|
parser=argparse.ArgumentParser(description=__doc__)
|
|
commands=parser.add_subparsers(dest='operation',required=True)
|
|
commands.add_parser('inspect')
|
|
package=commands.add_parser('prepare')
|
|
for name in ('snapshot','agent','helper','updater','qualification'):package.add_argument('--'+name,required=True,type=Path)
|
|
args=parser.parse_args()
|
|
if args.operation=='prepare':
|
|
folder=prepare(args.snapshot,args.agent,args.helper,args.updater,args.qualification)
|
|
print(json.dumps(dict(package=str(folder),target_mutations=False)))
|
|
return
|
|
result=transport.rpc('linux-vm.conf',inspect_vm,{'operation':'linux_updater_bootstrap_inspect_v1'})
|
|
folder=ROOT/'devstate/linux-updater-bootstrap'/str(uuid.uuid4());folder.mkdir(mode=0o700,parents=True)
|
|
target=folder/'read-only.json'
|
|
fd=os.open(target,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,0o600)
|
|
with os.fdopen(fd,'w') as output:json.dump(result,output,sort_keys=True,indent=2);output.write('\n')
|
|
print(json.dumps(dict(report=str(target),read_only=True,boot_id=result['boot_id'],
|
|
updater_provisioned=result['updater_provisioned'],databases=result['databases'])))
|
|
if __name__=='__main__':main()
|