Implement guarded Linux updater runtime and compatible baseline tooling
Rust development slice / linux (push) Failing after 8s
Rust development slice / linux (push) Failing after 8s
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
[Unit]
|
||||
Description=RMM stable updater for individually approved original code-pair jobs
|
||||
After=time-sync.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=root
|
||||
Group=root
|
||||
UMask=0077
|
||||
RuntimeDirectory=rmm-agent-updater
|
||||
RuntimeDirectoryMode=0700
|
||||
ExecStart=/usr/local/libexec/rmm-agent-updater --serve
|
||||
# An ambiguous handoff must be inspected, never automatically resumed by a
|
||||
# replacement updater process. A successful pass reloads its new record in the
|
||||
# same still-authenticated process for the next independently approved job.
|
||||
Restart=no
|
||||
NoNewPrivileges=yes
|
||||
PrivateTmp=yes
|
||||
PrivateNetwork=yes
|
||||
ProtectHome=yes
|
||||
ProtectSystem=strict
|
||||
ReadWritePaths=/usr/local/libexec /etc/rmm-remote /var/lib/rmm-agent-updater /var/lib/rmm-helper /var/lib/rmm-agent-remote
|
||||
RestrictAddressFamilies=AF_UNIX
|
||||
KillMode=control-group
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
@@ -0,0 +1,225 @@
|
||||
#!/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()
|
||||
@@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Seal the read-only/bootstrap public metadata SDK; no target operations."""
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import uuid
|
||||
import linux_updater_qualification as q
|
||||
ROOT=Path(__file__).resolve().parents[1]
|
||||
def main():
|
||||
os.umask(0o077);folder=ROOT/'devstate/linux-updater-bootstrap-sdk'/str(uuid.uuid4());folder.mkdir(mode=0o700,parents=True)
|
||||
names=q.public_files();before=q.capture(ROOT,names);source=folder/'source';source.mkdir(mode=0o700)
|
||||
for name in names:q.write(source/name,(ROOT/name).read_bytes())
|
||||
assert q.capture(source,names)==before and q.capture(ROOT,names)==before and q.public_files()==names,'SDK source drift'
|
||||
q.readonly(source);q.write(folder/'inputs.json',q.encode(before))
|
||||
cache=ROOT/'devstate/updater-integration-target'
|
||||
q.run(folder,source,cache,'sdk-clippy',['cargo','clippy','--locked','--offline','--jobs','2','-p','rmm-updater-worker','--all-targets','--','-D','warnings'])
|
||||
q.run(folder,source,cache,'sdk-release',['cargo','build','--locked','--offline','--jobs','2','--release','-p','rmm-updater-worker','--example','linux_updater_bootstrap'])
|
||||
assert q.capture(source,names)==before
|
||||
binary=q.copy_artifact(cache/'release/examples/linux_updater_bootstrap',folder/'sdk.bin')
|
||||
manifest={'schema_version':1,'profile':'linux_updater_bootstrap_metadata_sdk_v1','builder':q.BUILDER,'inputs_sha256':q.sha((folder/'inputs.json').read_bytes()),'source_files_count':len(names),'binary':binary,'clippy_all_targets_passed':True,'release_built':True,'native_vm_executed':False,'target_mutated':False,'baseline_runtime_binaries_unchanged':True,'windows_copied_only_not_qualified':True}
|
||||
q.write(folder/'manifest.json',q.encode(manifest));print(json.dumps({'manifest':str(folder/'manifest.json'),'manifest_sha256':q.sha((folder/'manifest.json').read_bytes()),'binary':binary}))
|
||||
if __name__=='__main__':main()
|
||||
@@ -0,0 +1,68 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Derive only qualified-observer capability markers from the original Linux pack.
|
||||
|
||||
Original sources/artifacts/units/cache are never modified. Only agent programs
|
||||
are rebuilt. Helper/updater/native contract executables remain exact originals.
|
||||
"""
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import uuid
|
||||
import linux_updater_qualification as q
|
||||
ROOT=Path(__file__).resolve().parents[1]
|
||||
BASE=ROOT/'devstate/linux-updater-qualification/21d20391-6102-4996-8a7a-efbd3e78bd7d'
|
||||
BASE_MANIFEST='9ed5f153fe76665150aaab298c4b21d74d7199b2312a28fa23aa9e30b13ddd08'
|
||||
MODULE='crates/agent/src/agent_update_apply.rs'
|
||||
MAIN='crates/agent/src/main.rs'
|
||||
|
||||
def main():
|
||||
os.umask(0o077)
|
||||
assert q.sha((BASE/'manifest.json').read_bytes())==BASE_MANIFEST
|
||||
old=json.loads((BASE/'manifest.json').read_text());case=ROOT/'devstate/linux-updater-capability-build'/str(uuid.uuid4());case.mkdir(mode=0o700,parents=True)
|
||||
fixed=(ROOT/MODULE).read_bytes();old_module=(BASE/'source0'/MODULE).read_bytes()
|
||||
# Closed byte transformation only, including the normal formatter's import wrap.
|
||||
source=old_module.decode().replace('agent_update::{ArtifactDescriptor, ArtifactRole, Platform},','agent_update::{\n APPLY_CAPABILITY, ArtifactDescriptor, ArtifactRole, CHECK_CAPABILITY, Platform,\n },')
|
||||
source=source.replace(' vec![\n "update_agent".into(),',' vec![\n APPLY_CAPABILITY.into(),\n CHECK_CAPABILITY.into(),\n "update_agent".into(),')
|
||||
assert source.encode()==fixed,'exact minimal agent capability transformation required'
|
||||
main=(ROOT/MAIN).read_bytes();old_main=(BASE/'source0'/MAIN).read_text()
|
||||
block=''' // Planning pins must be refreshed before the 90-second stored-data
|
||||
// planning limit. One inflight blocking collector bounds pressure;
|
||||
// heartbeats never perform this metadata work.
|
||||
if update_at.elapsed() >= Duration::from_secs(60) {
|
||||
self.queue_update_observation(generation)?;
|
||||
update_at = Instant::now();
|
||||
}
|
||||
'''
|
||||
needle=' if !busy && capability_at.elapsed()>=Duration::from_secs(60) {capability_at=Instant::now();if self.capabilities()!=advertised{return Ok(());}}'
|
||||
assert old_main.count(block)==1 and old_main.count(needle)==1
|
||||
moved='\n'.join(' '+line if line else line for line in block.splitlines())+'\n'
|
||||
assert old_main.replace(block,'').replace(needle,moved+needle).encode()==main,'exact existing observer poll move before capability check required'
|
||||
q.write(case/'entered.json',q.encode({'base_manifest_sha256':BASE_MANIFEST,'module_sha256':q.sha(fixed),'main_sha256':q.sha(main),'no_target_mutations':True,'original_case_unchanged':True}))
|
||||
cache=case/'cache';cache.mkdir(mode=0o700)
|
||||
# Copy, never hardlink: compiler output cannot change the original cache.
|
||||
shutil.copytree(BASE/'cache/release',cache/'release')
|
||||
artifacts={};input_hashes={}
|
||||
for version in ('source0','source1'):
|
||||
expected=json.loads((BASE/(version+'-inputs.json')).read_text());names=sorted(expected)
|
||||
assert q.capture(BASE/version,names)==expected
|
||||
target=case/version;shutil.copytree(BASE/version,target)
|
||||
module=target/MODULE;module.chmod(0o600);assert module.read_bytes()==old_module;module.write_bytes(fixed);module.chmod(0o444)
|
||||
entry=target/MAIN;entry.chmod(0o600);assert entry.read_bytes()==old_main.encode();entry.write_bytes(main);entry.chmod(0o444)
|
||||
actual=q.capture(target,names);assert {n for n in names if actual[n]!=expected[n]}=={MODULE,MAIN}
|
||||
q.readonly(target);q.write(case/(version+'-inputs.json'),q.encode(actual));input_hashes[version]=q.sha((case/(version+'-inputs.json')).read_bytes())
|
||||
q.run(case,target,cache,version+'-clippy',['cargo','clippy','--locked','--offline','--release','--jobs','2','-p','rmm-agent','--all-targets','--','-D','warnings'])
|
||||
q.run(case,target,cache,version+'-agent-release',['cargo','build','--locked','--offline','--release','--jobs','2','-p','rmm-agent','--bin','rmm-agent'])
|
||||
output=case/'artifacts'/version;output.mkdir(mode=0o700,parents=True)
|
||||
agent=q.copy_artifact(cache/'release/rmm-agent',output/'rmm-agent');assert agent['sha256']!=old['artifacts'][version]['agent']['sha256']
|
||||
artifacts[version]={**old['artifacts'][version],'agent':agent}
|
||||
assert q.capture(target,names)==actual
|
||||
assert artifacts['source0']['agent']['sha256']!=artifacts['source1']['agent']['sha256']
|
||||
for label,item in [('helper0',old['artifacts']['source0']['helper']),('helper1',old['artifacts']['source1']['helper']),('updater',old['stable_updater'])]:
|
||||
assert q.sha(Path(item['path']).read_bytes())==item['sha256'],label
|
||||
for unit in old['units'].values():assert q.sha(Path(unit['path']).read_bytes())==unit['sha256']
|
||||
manifest={'schema_version':1,'profile':'linux_compatible_updater_capability_derivative_v2','base_manifest_sha256':BASE_MANIFEST,'base_manifest':str(BASE/'manifest.json'),'changed_modules':{MODULE:q.sha(fixed),MAIN:q.sha(main)},'poll_harvest_before_capability_check':True,'sampler_frequency_unchanged':True,'source0_version':'0.1.0','source1_version':'0.1.1','source_input_hashes':input_hashes,'builder':q.BUILDER,'artifacts':artifacts,'stable_updater':old['stable_updater'],'units':old['units'],'old_sources_artifacts_units_unchanged':True,'only_agents_rebuilt':True,'source0_and_source1_clippy_passed':True,'native_vm_tested':False,'actual_qualified_capability_still_requires_native_baseline':True,'bootstrap_installed':False,'apply_executed':False}
|
||||
q.write(case/'manifest.json',q.encode(manifest));q.write(case/'completed.json',q.encode({'manifest_sha256':q.sha((case/'manifest.json').read_bytes())}))
|
||||
print(json.dumps({'case':str(case),'manifest_sha256':q.sha((case/'manifest.json').read_bytes()),'artifacts':artifacts}))
|
||||
if __name__=='__main__':main()
|
||||
@@ -0,0 +1,375 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Guarded compatible Linux bootstrap, explicitly coordinated separate phases.
|
||||
|
||||
No job approval, package operation, reenrollment or private-key transfer. This
|
||||
controller is prepared source; invoking mutations requires Root coordination.
|
||||
"""
|
||||
import argparse
|
||||
import base64
|
||||
import hashlib
|
||||
import inspect
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shlex
|
||||
import subprocess
|
||||
import tarfile
|
||||
import uuid
|
||||
import linux_updater_bootstrap as prerequisite
|
||||
import remote_client_bootstrap as transport
|
||||
ROOT=Path(__file__).resolve().parents[1]
|
||||
BASE=ROOT/'devstate/linux-updater-capability-build/9923a97f-4c8c-4e0a-bbad-4f1d88f0d43a/manifest.json'
|
||||
BASE_SHA='a41ad700b4b7527bf911bf00f2745914188760c631af0c3255364c05f1ef4fbc'
|
||||
BOOT='45aba743-be1d-4ae5-ba00-139fb9363bfe'
|
||||
|
||||
def sha(raw):return hashlib.sha256(raw).hexdigest()
|
||||
def once(path,raw):
|
||||
path.parent.mkdir(mode=0o700,parents=True,exist_ok=True)
|
||||
with path.open('xb') as stream:stream.write(raw);stream.flush();os.fsync(stream.fileno())
|
||||
path.chmod(0o600)
|
||||
def native_rpc(function,payload,config='linux-vm.conf',timeout=80):
|
||||
# Same strict credential parser and already-confirmed host as existing tooling.
|
||||
cfg=transport.access.load_config(ROOT/'credentials'/config)
|
||||
code='import json,os,subprocess\nfrom pathlib import Path\n'+inspect.getsource(transport.require)+'\n'+inspect.getsource(function)+'\ntry:\n print(json.dumps('+function.__name__+'('+repr(payload)+')))\nexcept Exception as error:\n import traceback\n print(json.dumps({"error_class":type(error).__name__,"frames":[{"function":f.name,"line":f.lineno}for f in traceback.extract_tb(error.__traceback__)]}))\n'
|
||||
ssh=['ssh','-F','/dev/null','-T','-p',cfg.get('port','22'),'-o','StrictHostKeyChecking=yes','-o','BatchMode=yes','-o','IdentitiesOnly=yes','-o','ForwardAgent=no','-o','ClearAllForwardings=yes','-o','ConnectTimeout=7','-i',str(Path(cfg['ssh_key_path']).expanduser()),cfg['username']+'@'+cfg['address'],'python3 -I -']
|
||||
result=subprocess.run(ssh,input=code.encode(),capture_output=True,timeout=timeout)
|
||||
if result.returncode:raise RuntimeError('Bootstrap transport failed; no private diagnostics printed')
|
||||
value=json.loads(result.stdout)
|
||||
if 'error_class'in value:raise RuntimeError('Bootstrap incomplete; retained native intent needs inspection: '+json.dumps(value))
|
||||
return value
|
||||
|
||||
def public_inputs(payload):
|
||||
import stat,json,hashlib,os
|
||||
from pathlib import Path
|
||||
require(payload=={'operation':'linux_compatible_public_inputs_v1'}and os.geteuid()==0,'fixed public inputs')
|
||||
result={}
|
||||
for name in ('helper-general.json','general-helper-proof.json','helper-full-maintenance.json','full-maintenance-helper-proof.json','helper-signing-key.json'):
|
||||
path=Path('/etc/rmm-remote')/name;meta=path.lstat();require(stat.S_ISREG(meta.st_mode)and meta.st_uid==0 and meta.st_nlink==1 and not meta.st_mode&0o022 and meta.st_size<=16384,'protected public input')
|
||||
raw=path.read_bytes();require(path.lstat().st_ino==meta.st_ino and path.lstat().st_mtime_ns==meta.st_mtime_ns,'public input changed');result[name]={'bytes':list(raw),'sha256':hashlib.sha256(raw).hexdigest()}
|
||||
general=json.loads(bytes(result['helper-general.json']['bytes']));pin=json.loads(bytes(result['general-helper-proof.json']['bytes']))
|
||||
require(general['profile']=='general'and general.get('helper_binary_sha256')is None and pin['schema_version']==1 and pin['profile']=='general'and pin.get('helper_binary_sha256')is None,'exact non-binary General identity/policy profile')
|
||||
return result
|
||||
|
||||
def central_closed(payload):
|
||||
import json,subprocess,datetime,uuid,hashlib
|
||||
from pathlib import Path
|
||||
agent=str(uuid.UUID(payload['agent_id']));tenant=str(uuid.UUID(payload['tenant_id']))
|
||||
def run(a):return subprocess.check_output(a,text=True,stderr=subprocess.DEVNULL,timeout=15)
|
||||
flags={}
|
||||
for role in ('api','gateway'):
|
||||
container=json.loads(run(['docker','inspect','rust-discovery-server-'+role+'-1']))[0];env=dict(row.split('=',1)for row in container['Config']['Env']);flag=env.get('RMM_DEV_ROOT_ENABLED','false')
|
||||
require(flag in ('true','false'),'known Root gate')
|
||||
expired=False
|
||||
if flag=='true':
|
||||
path=env.get('RMM_DEV_ROOT_POLICY_FILE');require(path=='/root-policy/policy.json','fixed public Root policy')
|
||||
mount=next(m for m in container['Mounts']if m['Destination']=='/root-policy');require(mount['RW']is False,'readonly protected policy')
|
||||
doc=json.loads((Path(mount['Source'])/'policy.json').read_text());expiry=datetime.datetime.fromisoformat(doc['expires_at'].replace('Z','+00:00'));require(expiry.tzinfo is not None,'absolute policy expiry');expired=expiry<=datetime.datetime.now(datetime.timezone.utc)
|
||||
require(flag=='false'or expired,'central Root scope must be closed before compatible bootstrap')
|
||||
require('RMM_AGENT_UPDATE_POLICY_FILE'not in env,'initial updater scope must remain absent')
|
||||
flags[role]={'root_enabled':flag=='true','root_policy_expired':expired,'container_id':container['Id']}
|
||||
query="BEGIN READ ONLY; SELECT count(*) FROM jobs WHERE tenant_id='"+tenant+"' AND agent_id='"+agent+"' AND state NOT IN ('succeeded','failed','cancelled','expired'); COMMIT;"
|
||||
rows=run(['docker','exec','rust-discovery-server-postgres-1','psql','-XqAt','-v','ON_ERROR_STOP=1','-U','postgres','-d','rmm','-c',query]).splitlines();require(rows==['0'],'no open or unknown Linux job')
|
||||
return {'operation':'linux_compatible_closed_scope_v1','tenant_id':tenant,'agent_id':agent,'observed_at':datetime.datetime.now(datetime.timezone.utc).isoformat(),'roles':flags,'open_jobs':0,'job_authority':False}
|
||||
|
||||
def passive_check(payload):
|
||||
import datetime,json,subprocess,uuid
|
||||
tenant=str(uuid.UUID(payload['tenant_id']));agent=str(uuid.UUID(payload['agent_id']))
|
||||
query="BEGIN READ ONLY; SELECT json_build_object('connection_generation',i.connection_generation,'capabilities',i.capabilities,'last_contact_at',i.last_contact_at,'observed_at',o.observed_at,'received_at',o.received_at,'data',o.data,'payload_sha256',o.payload_sha256) FROM agent_installations i JOIN LATERAL (SELECT * FROM observations WHERE tenant_id=i.tenant_id AND agent_id=i.agent_id AND source_id='agent.update' ORDER BY sequence DESC LIMIT 1)o ON true WHERE i.tenant_id='"+tenant+"' AND i.agent_id='"+agent+"' AND i.revoked_at IS NULL; COMMIT;"
|
||||
output=subprocess.check_output(['docker','exec','rust-discovery-server-postgres-1','psql','-XqAt','-v','ON_ERROR_STOP=1','-U','postgres','-d','rmm','-c',query],stderr=subprocess.DEVNULL,text=True,timeout=15).splitlines();require(len(output)==1,'one actual current installation/passive observation')
|
||||
observed=json.loads(output[0]);sample=observed['data']['agent_update'];now=datetime.datetime.now(datetime.timezone.utc)
|
||||
for text in (observed['last_contact_at'],observed['received_at'],sample['observed_at']):
|
||||
age=(now-datetime.datetime.fromisoformat(text.replace('Z','+00:00'))).total_seconds();require(0<=age<90,'fresh actual persisted passive evidence')
|
||||
require(sample['quality']=='valid'and sample['platform']=='x86_64-unknown-linux-gnu'and sample['activation_floor']==1 and not sample['rolled_back']and sample['helper_observation_sha256']and sample['signed_sync_decision_id'],'actual Full verified passive baseline')
|
||||
require(sample['predecessor']['installed_record_sha256']==payload['record_sha256']and sample['predecessor']['agent_sha256']==payload['agent_sha256']and sample['predecessor']['helper_sha256']==payload['helper_sha256'],'actual current predecessor')
|
||||
require(sample['updater']['binary_sha256']==payload['updater_sha256']and sample['updater']['policy_sha256']==payload['policy_sha256']and sample['updater']['keyring_sha256']==payload['keyring_sha256']and sample['updater']['health_key_pin_sha256']==payload['health_pin_sha256'],'actual protected updater trust bindings')
|
||||
required=['agent.update.apply/1','agent.update.check/1','update_agent','root:update_agent','helper.root.v1','rmm.sync.v1','helper.binary.sha256:'+payload['helper_sha256'],'helper.policy.sha256:'+payload['helper_policy_sha256'],'updater.binary.sha256:'+payload['updater_sha256'],'updater.policy.sha256:'+payload['policy_sha256']]
|
||||
require(all(cap in observed['capabilities']for cap in required),'actual current qualified runtime capabilities, no SQL override')
|
||||
return {'operation':'linux_compatible_passive_qualification_v1','tenant_id':tenant,'agent_id':agent,'sample':sample,'connection_generation':observed['connection_generation'],'capabilities':observed['capabilities'],'observation_payload_sha256':observed['payload_sha256'],'actual_passive_path':True,'database_written':False,'job_authority':False}
|
||||
|
||||
def stage(payload):
|
||||
import base64,hashlib,json,os,stat,uuid
|
||||
from pathlib import Path
|
||||
require(os.geteuid()==0 and os.uname().machine=='x86_64','root x86 Linux only')
|
||||
ident=str(uuid.UUID(payload['case_id']));base=Path('/var/lib/rmm-updater-bootstrap')
|
||||
if not base.exists():base.mkdir(mode=0o700)
|
||||
meta=base.lstat();require(stat.S_ISDIR(meta.st_mode)and meta.st_uid==0 and stat.S_IMODE(meta.st_mode)==0o700,'protected bootstrap root')
|
||||
folder=base/ident;folder.mkdir(mode=0o700)
|
||||
files=payload['files'];require(set(files)=={'agent.bin','helper.bin','updater.bin','sdk.bin','full-config.json','full-pin.json','updater.service'},'closed bootstrap artifact set')
|
||||
for name,entry in files.items():
|
||||
raw=base64.b64decode(entry['base64'],validate=True);require(len(raw)==entry['bytes']and hashlib.sha256(raw).hexdigest()==entry['sha256'],'exact public artifact')
|
||||
if name.endswith('.bin'):require(raw[:6]==b'\x7fELF\x02\x01'and raw[18:20]==b'\x3e\x00','Linux x86-64 ELF')
|
||||
fd=os.open(folder/name,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,0o500 if name.endswith('.bin') else 0o600)
|
||||
with os.fdopen(fd,'wb')as stream:stream.write(raw);stream.flush();os.fsync(stream.fileno())
|
||||
manifest={k:v for k,v in payload.items()if k!='files'};manifest['files']={n:{k:v for k,v in e.items()if k!='base64'}for n,e in files.items()}
|
||||
with(folder/'manifest.json').open('x')as stream:json.dump(manifest,stream,sort_keys=True);stream.flush();os.fsync(stream.fileno())
|
||||
return {'case_id':ident,'folder':str(folder),'staged':True,'services_changed':False,'job_authority':False}
|
||||
|
||||
def pair(payload):
|
||||
import base64,contextlib,fcntl,hashlib,json,os,select,sqlite3,stat,subprocess,time,uuid,datetime
|
||||
from pathlib import Path
|
||||
require(os.geteuid()==0 and os.uname().machine=='x86_64','root x86 Linux only')
|
||||
ident=str(uuid.UUID(payload['case_id']));folder=Path('/var/lib/rmm-updater-bootstrap')/ident;manifest=json.loads((folder/'manifest.json').read_text())
|
||||
require(Path('/proc/sys/kernel/random/boot_id').read_text().strip()==manifest['boot_id'],'original boot')
|
||||
def sha(raw):return hashlib.sha256(raw).hexdigest()
|
||||
def run(a,timeout=20):return subprocess.check_output(a,text=True,stderr=subprocess.DEVNULL,timeout=timeout).strip()
|
||||
def syncdir(path):
|
||||
fd=os.open(path,os.O_RDONLY|os.O_DIRECTORY)
|
||||
try:os.fsync(fd)
|
||||
finally:os.close(fd)
|
||||
def record(name,value):
|
||||
with(folder/name).open('x')as stream:json.dump(value,stream,sort_keys=True);stream.flush();os.fsync(stream.fileno())
|
||||
syncdir(folder)
|
||||
def public(path,maximum):
|
||||
path=Path(path);meta=path.lstat();require(stat.S_ISREG(meta.st_mode)and meta.st_uid==0 and meta.st_nlink==1 and not meta.st_mode&0o022 and meta.st_size<=maximum,'protected original public file');return path.read_bytes(),meta
|
||||
def identity(path):
|
||||
m=Path(path).lstat();require(not stat.S_ISLNK(m.st_mode),'no substituted path');return [m.st_dev,m.st_ino,m.st_uid,m.st_gid,m.st_mode,m.st_nlink]
|
||||
def status(unit):
|
||||
fields={k:v for k,v in(line.split('=',1)for line in run(['systemctl','show','--all','--property=Id,ActiveState,SubState,MainPID,ControlPID,InvocationID,Job,ControlGroup,UnitFileState','--',unit]).splitlines())};require(fields['Id']==unit,'exact service');return fields
|
||||
units=['rmm-agent-remote.service','rmm-helper-cloud.service','rmm-full-maintenance-helper.service']
|
||||
def databases():
|
||||
result={}
|
||||
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')
|
||||
with contextlib.closing(sqlite3.connect('file:'+path+'?mode=ro',uri=True,timeout=.5))as db:
|
||||
db.execute('PRAGMA query_only=ON');require(db.execute('PRAGMA integrity_check').fetchone()[0]=='ok','original journal integrity')
|
||||
tables={r[0]for r in db.execute("SELECT name FROM sqlite_master WHERE type='table'")};evidence=[]
|
||||
if 'consumed'in tables:
|
||||
require(db.execute('SELECT count(*) FROM consumed WHERE result IS NULL').fetchone()[0]==0,'no unresolved original effect')
|
||||
for row in 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'):
|
||||
evidence.append([v if isinstance(v,(str,int))or v is None else sha(v)for v in row])
|
||||
if 'job_attempts'in tables:require(db.execute("SELECT count(*) FROM job_attempts WHERE state NOT IN ('succeeded','failed','cancelled','expired')").fetchone()[0]==0,'no open original agent job')
|
||||
for name in ('helper_sync_meta','reconcile_meta'):
|
||||
if name in tables:require(db.execute('SELECT count(*) FROM '+name+' WHERE fault IS NOT NULL').fetchone()[0]==0,'no original journal fault')
|
||||
generation=db.execute('SELECT generation FROM reconcile_meta WHERE id=1').fetchone()[0]if 'reconcile_meta'in tables else None
|
||||
result[path]={'file_id':identity(path),'evidence_sha256':sha(json.dumps(evidence,separators=(',',':')).encode()),'consumptions':len(evidence),'generation':generation}
|
||||
return result
|
||||
def retained_metadata():
|
||||
result={}
|
||||
fixed=['/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','/etc/rmm-remote/agent.env','/etc/rmm-remote/helper-general.json','/etc/rmm-remote/general-helper-proof.json','/etc/rmm-remote/helper-signing-key.json']
|
||||
cert=Path('/var/lib/rmm-agent-remote/certificates')
|
||||
if cert.exists():fixed.extend(str(p)for p in cert.rglob('*')if p.is_file())
|
||||
for path in fixed:
|
||||
m=Path(path).lstat();require(stat.S_ISREG(m.st_mode)and m.st_nlink==1,'retained original metadata');result[path]=identity(path)+[m.st_size,m.st_mtime_ns,m.st_ctime_ns]
|
||||
return result
|
||||
for name,entry in manifest['files'].items():require(sha((folder/name).read_bytes())==entry['sha256'],'staged artifact changed')
|
||||
for unit in ('rmm-apt-helper.service','rmm-agent.service','rmm-helper-dev-test.service'):
|
||||
s=status(unit);require(s['ActiveState']=='inactive'and s['MainPID']in('','0')and s['ControlPID']in('','0')and s['Job']==''and s['UnitFileState']in('','disabled'),'APT and legacy remain closed')
|
||||
if payload['action']=='install':
|
||||
require(not(folder/'pair-install-intent.json').exists(),'existing intent means readback, never repeat')
|
||||
fence=payload['central_fence'];require(fence['operation']=='linux_compatible_closed_scope_v1'and fence['open_jobs']==0 and not fence['job_authority']and fence['tenant_id']==manifest['tenant_id']and fence['agent_id']==manifest['agent_id'],'closed central scope')
|
||||
at=datetime.datetime.fromisoformat(fence['observed_at']);require(0<=(datetime.datetime.now(datetime.timezone.utc)-at).total_seconds()<30,'fresh closed-scope readback')
|
||||
before={u:status(u)for u in units};pids=[]
|
||||
for u,s in before.items():
|
||||
require(s['ActiveState']=='active'and int(s['MainPID'])>1 and s['ControlPID']=='0'and s['Job']=='','exact active original pair')
|
||||
pid=int(s['MainPID']);fd=os.pidfd_open(pid);pids.append(fd)
|
||||
original=databases();retained=retained_metadata()
|
||||
for source,target,oldsha in [('agent.bin','/usr/local/libexec/rmm-agent-remote',manifest['old_agent_sha256']),('helper.bin','/usr/local/libexec/rmm-privileged-helper',manifest['old_helper_sha256']),('full-config.json','/etc/rmm-remote/helper-full-maintenance.json',manifest['old_config_sha256']),('full-pin.json','/etc/rmm-remote/full-maintenance-helper-proof.json',manifest['old_pin_sha256'])]:
|
||||
raw,_=public(target,256*1024*1024);require(sha(raw)==oldsha,'original target CAS')
|
||||
record('pair-install-intent.json',{'before':before,'databases':original,'retained':retained,'central_fence':fence,'files':manifest['files']})
|
||||
run(['systemctl','stop','--',units[0],units[2],units[1]],30)
|
||||
for fd in pids:
|
||||
poll=select.poll();poll.register(fd,select.POLLIN);require(poll.poll(1000),'original process not exited');os.close(fd)
|
||||
for u in units:
|
||||
s=status(u);require(s['ActiveState']=='inactive'and s['MainPID']=='0'and s['ControlPID']=='0'and not s['Job']and not s['ControlGroup'],'all original processes stopped')
|
||||
with contextlib.ExitStack()as held:
|
||||
for path in ('/var/lib/rmm-agent-remote/agent.lock','/var/lib/rmm-helper/executor.lock'):
|
||||
f=held.enter_context(open(path,'rb'));fcntl.flock(f,fcntl.LOCK_EX|fcntl.LOCK_NB)
|
||||
closed=databases()
|
||||
require(all(closed[p]['file_id']==v['file_id']and closed[p]['evidence_sha256']==v['evidence_sha256']for p,v in original.items()),'original effect history changed during stop')
|
||||
for source,target,oldsha in [('agent.bin','/usr/local/libexec/rmm-agent-remote',manifest['old_agent_sha256']),('helper.bin','/usr/local/libexec/rmm-privileged-helper',manifest['old_helper_sha256']),('full-config.json','/etc/rmm-remote/helper-full-maintenance.json',manifest['old_config_sha256']),('full-pin.json','/etc/rmm-remote/full-maintenance-helper-proof.json',manifest['old_pin_sha256'])]:
|
||||
target=Path(target);old,meta=public(target,256*1024*1024);require(sha(old)==oldsha,'stopped target CAS')
|
||||
previous=target.with_name(target.name+'.bootstrap-'+ident+'.previous');candidate=target.with_name(target.name+'.bootstrap-'+ident+'.candidate');require(not previous.exists()and not candidate.exists(),'create-once bootstrap names')
|
||||
fd=os.open(candidate,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,stat.S_IMODE(meta.st_mode))
|
||||
with os.fdopen(fd,'wb')as stream:os.fchown(stream.fileno(),meta.st_uid,meta.st_gid);stream.write((folder/source).read_bytes());stream.flush();os.fsync(stream.fileno())
|
||||
record(source+'-save-intent.json',{'target':str(target),'previous':str(previous),'old_sha256':oldsha});os.rename(target,previous);syncdir(target.parent)
|
||||
record(source+'-publish-intent.json',{'candidate':str(candidate),'target':str(target),'sha256':manifest['files'][source]['sha256']});os.rename(candidate,target);syncdir(target.parent)
|
||||
syncdir(target.parent)
|
||||
require(sha(target.read_bytes())==manifest['files'][source]['sha256']and sha(previous.read_bytes())==oldsha,'exact predecessor and replacement retained')
|
||||
require(retained_metadata()==retained,'original key identity General policy or PKI changed')
|
||||
record('pair-installed.json',{'databases':closed,'retained':retained,'old_generation':original['/var/lib/rmm-agent-remote/journal.sqlite']['generation'],'services_stopped':True,'job_authority':False})
|
||||
return {'case_id':ident,'compatible_files_installed':True,'services_stopped':True,'next':'separate reviewed unit setup and start phase','job_authority':False}
|
||||
elif payload['action']=='units':
|
||||
installed=json.loads((folder/'pair-installed.json').read_text());require(not(folder/'unit-install-intent.json').exists(),'existing unit intent means readback only')
|
||||
require(retained_metadata()==installed['retained'],'original metadata changed')
|
||||
for u in units:
|
||||
row=status(u);require(row['ActiveState']=='inactive'and row['MainPID']=='0'and row['ControlPID']=='0'and not row['Job'],'pair remains stopped before unit setup')
|
||||
paths={'/usr/local/libexec/rmm-agent-updater':('updater.bin',0o755),'/etc/systemd/system/rmm-agent-updater.service':('updater.service',0o644)}
|
||||
require(all(not Path(path).exists()for path in paths),'stable updater is create-only')
|
||||
require(not Path('/etc/rmm-agent-updater').exists()and not Path('/var/lib/rmm-agent-updater').exists(),'initial trust namespace only')
|
||||
override=Path('/etc/systemd/system/rmm-full-maintenance-helper.service.d/99-rmm-update-closed-start.conf');require(not override.exists(),'create-only closed restart override')
|
||||
record('unit-install-intent.json',{'paths':list(paths),'override':str(override),'restart':'no','services_started':False})
|
||||
for path,(name,mode)in paths.items():
|
||||
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((folder/name).read_bytes());stream.flush();os.fsync(stream.fileno())
|
||||
if not override.parent.exists():override.parent.mkdir(mode=0o755)
|
||||
require(override.parent.lstat().st_uid==0 and not override.parent.is_symlink()and not override.parent.lstat().st_mode&0o022,'protected Full override parent')
|
||||
with override.open('xb')as stream:stream.write(b'[Service]\nRestart=no\n');stream.flush();os.fsync(stream.fileno())
|
||||
override.chmod(0o644)
|
||||
for path,mode in [('/etc/rmm-agent-updater',0o755),('/var/lib/rmm-agent-updater',0o700),('/var/lib/rmm-agent-updater/records',0o700),('/var/lib/rmm-agent-updater/handoffs',0o700)]:Path(path).mkdir(mode=mode)
|
||||
binding=json.loads(Path('/var/lib/rmm-agent-remote/binding.json').read_text());key=json.loads(Path('/etc/rmm-remote/helper-signing-key.json').read_text())
|
||||
require(binding.get('public_key_hex')in('',key['public_key_hex']),'independent effective public key pin');binding['public_key_hex']=key['public_key_hex']
|
||||
fd=os.open('/etc/rmm-agent-updater/binding.json',os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,0o600)
|
||||
with os.fdopen(fd,'w')as stream:json.dump(binding,stream,separators=(',',':'));stream.flush();os.fsync(stream.fileno())
|
||||
run(['systemctl','daemon-reload'])
|
||||
require(run(['systemctl','show','--value','--property=Restart','--',units[2]])=='no','actual closed Full restart contract')
|
||||
updater=status('rmm-agent-updater.service');require(updater['ActiveState']=='inactive'and updater['MainPID']=='0'and updater['ControlPID']=='0'and not updater['Job']and updater['UnitFileState']=='disabled','updater remains stopped and disabled')
|
||||
record('units-installed.json',{'updater':updater,'restart_override_sha256':sha(override.read_bytes()),'namespace_provisioned':True,'updater_started':False})
|
||||
return {'case_id':ident,'updater_unit_installed_not_started':True,'full_restart_no_bound':True,'job_authority':False}
|
||||
elif payload['action']=='start':
|
||||
installed=json.loads((folder/'pair-installed.json').read_text());require((folder/'units-installed.json').is_file(),'separate reviewed unit setup required');require(not(folder/'pair-start-intent.json').exists(),'existing start means readback only')
|
||||
require(retained_metadata()==installed['retained'],'original metadata changed')
|
||||
for u in units:
|
||||
s=status(u);require(s['ActiveState']=='inactive'and s['MainPID']=='0'and s['ControlPID']=='0'and not s['Job'],'pair remains stopped')
|
||||
record('pair-start-intent.json',{'units':[units[1],units[2],units[0]],'only_once':True})
|
||||
for u in (units[1],units[2],units[0]):
|
||||
record(u+'-start-intent.json',{'unit':u});run(['systemctl','start','--',u],20);s=status(u);require(s['ActiveState']=='active'and int(s['MainPID'])>1,'single compatible start')
|
||||
record('pair-started.json',{'status':{u:status(u)for u in units},'job_authority':False})
|
||||
return {'case_id':ident,'compatible_services_started_once':True,'job_authority':False,'normal_signed_sync_qualification_still_required':True}
|
||||
elif payload['action']=='qualify':
|
||||
installed=json.loads((folder/'pair-installed.json').read_text());require((folder/'pair-started.json').is_file(),'compatible starts required')
|
||||
def pin(unit):
|
||||
raw=run(['systemctl','show','--all','--property=FragmentPath,DropInPaths','--',unit]);fields=dict(line.split('=',1)for line in raw.splitlines());paths=[fields['FragmentPath']]+fields['DropInPaths'].split();require(bool(paths[0]),'actual unit composition')
|
||||
composition=[]
|
||||
for path in paths:
|
||||
data,m=public(path,65536);composition.append([path,{'identity':dict(zip(('device','inode','uid','gid','mode','links'),identity(path))),'bytes':len(data),'sha256':sha(data)}])
|
||||
return {'unit':unit,'unit_sha256':sha(json.dumps(composition,separators=(',',':'),ensure_ascii=False).encode())}
|
||||
pins=[pin(u)for u in units]
|
||||
if run(['systemctl','show','--value','--property=LoadState','--','rmm-apt-helper.service'])=='loaded':pins.append(pin('rmm-apt-helper.service'))
|
||||
request={'operation':'qualify_pair','minimum_generation':installed['old_generation'],'expected_agent_sha256':manifest['files']['agent.bin']['sha256'],'expected_helper_sha256':manifest['files']['helper.bin']['sha256'],'unit_pins':pins}
|
||||
raw=subprocess.check_output([str(folder/'sdk.bin')],input=json.dumps(request).encode(),stderr=subprocess.DEVNULL,timeout=35);value=json.loads(raw)
|
||||
require(value['profile']=='linux_compatible_baseline_qualification_v1'and value['normal_signed_sync_observed']and value['ready']is False and value['helper_ready']is False and value['unlock']is False and value['job_authority']is False,'actual fresh normal qualification')
|
||||
after=databases();require(all(after[p]['file_id']==v['file_id']and after[p]['evidence_sha256']==v['evidence_sha256']for p,v in installed['databases'].items()),'all original consumption and evidence retained')
|
||||
require(retained_metadata()==installed['retained'],'original General identity key or PKI changed')
|
||||
with(folder/'qualification.json').open('xb')as stream:stream.write(raw);stream.flush();os.fsync(stream.fileno())
|
||||
record('pair-qualified.json',{'qualification_sha256':sha(raw),'unit_pins':pins,'databases':after,'job_authority':False})
|
||||
return {'case_id':ident,'qualification_sha256':sha(raw),'qualification_base64url':base64.urlsafe_b64encode(raw).rstrip(b'=').decode(),'sample':value,'services_restarted':False,'job_authority':False}
|
||||
elif payload['action']=='registry':
|
||||
receipt=json.loads((folder/'pair-qualified.json').read_text());raw=(folder/'qualification.json').read_bytes();require(sha(raw)==receipt['qualification_sha256'],'actual qualification bytes unchanged')
|
||||
require(not(folder/'registry.json').exists(),'create-only actual registry')
|
||||
request={'operation':'registry','qualification_sha256':receipt['qualification_sha256']}
|
||||
output=subprocess.check_output([str(folder/'sdk.bin')],input=json.dumps(request).encode(),stderr=subprocess.DEVNULL,timeout=35);value=json.loads(output)
|
||||
registry=bytes(value['registry_bytes']);require(sha(registry)==value['reader_registry_sha256'],'actual typed registry hash')
|
||||
with(folder/'registry.json').open('xb')as stream:stream.write(registry);stream.flush();os.fsync(stream.fileno())
|
||||
record('registry-prepared.json',value)
|
||||
return {'case_id':ident,'registry':value,'services_changed':False,'job_authority':False}
|
||||
elif payload['action']=='trust':
|
||||
import tempfile
|
||||
trust=payload['trust'];require(trust['operation']=='linux_compatible_trust_install_v1'and trust['case_id']==ident and trust['job_authority']is False,'fixed public trust package')
|
||||
receipt=json.loads((folder/'pair-qualified.json').read_text());require(trust['qualification_sha256']==receipt['qualification_sha256'],'original native qualification')
|
||||
require(not(folder/'trust-install-intent.json').exists(),'existing trust intent means readback only')
|
||||
expected={'policy.json','keyring.json','health-key.json','reader-registry.json','installed-record.json'};require(set(trust['files'])==expected,'closed trust document set')
|
||||
raw={}
|
||||
for name,entry in trust['files'].items():
|
||||
raw[name]=base64.b64decode(entry['base64'],validate=True);require(len(raw[name])==entry['bytes']and len(raw[name])<=65536 and sha(raw[name])==entry['sha256'],'exact bounded public trust bytes')
|
||||
require(raw['reader-registry.json']==(folder/'registry.json').read_bytes(),'actual measured reader registry')
|
||||
request={'operation':'validate_public_bundle','record':list(raw['installed-record.json']),'policy':list(raw['policy.json']),'keyring':list(raw['keyring.json']),'health_pin':list(raw['health-key.json'])}
|
||||
checked=json.loads(subprocess.check_output([str(folder/'sdk.bin')],input=json.dumps(request).encode(),stderr=subprocess.DEVNULL,timeout=20));require(checked==trust['shape_checks']and checked['shape_only_not_signature_verified']is True,'independent native typed shape check')
|
||||
policy=json.loads(raw['policy.json']);keys=json.loads(raw['keyring.json']);health=json.loads(raw['health-key.json']);baseline=policy['initial_baseline'];record_doc=json.loads(raw['installed-record.json']);envelope=record_doc['release_envelope']
|
||||
require(keys['generation']==1 and len(keys['keys'])==1 and keys['keys'][0]['platform']=='x86_64-unknown-linux-gnu'and keys['keys'][0]['first_sequence']==1 and keys['keys'][0]['last_sequence']==2 and not keys['keys'][0]['revoked'],'initial two-release trust scope')
|
||||
public_key=bytes.fromhex(keys['keys'][0]['public_key_hex']);job_key=json.loads(Path('/etc/rmm-remote/helper-signing-key.json').read_text())['public_key_hex'];require(len(public_key)==32 and public_key.hex()!=job_key and public_key.hex()!=health['public_key_hex']and health['public_key_hex']!=job_key,'separate updater release health and Job keys')
|
||||
require(sha(public_key)==baseline['key_id']==envelope['key_id'],'exact independently provisioned release key identity')
|
||||
def decode(value):
|
||||
decoded=base64.urlsafe_b64decode(value+'='*((-len(value))%4));require(base64.urlsafe_b64encode(decoded).rstrip(b'=').decode()==value,'canonical signed public bytes');return decoded
|
||||
with tempfile.TemporaryDirectory(prefix='rmm-bootstrap-verify-')as tmp:
|
||||
temp=Path(tmp);(temp/'public.der').write_bytes(bytes.fromhex('302a300506032b6570032100')+public_key)
|
||||
for role,document,prefix in [('baseline',baseline,b'rmm.agent-update-baseline.v1\0'),('release',envelope,b'rmm.agent-release.v1\0')]:
|
||||
message=decode(document['payload_bytes']);signature=decode(document['signature']);require(len(signature)==64,'original Ed25519 signature')
|
||||
(temp/'message').write_bytes(prefix+message);(temp/'signature').write_bytes(signature)
|
||||
subprocess.run(['openssl','pkeyutl','-verify','-rawin','-pubin','-keyform','DER','-inkey',str(temp/'public.der'),'-in',str(temp/'message'),'-sigfile',str(temp/'signature')],check=True,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL,timeout=10)
|
||||
if role=='baseline':require(message==raw['installed-record.json'],'exact initial signed record')
|
||||
else:
|
||||
release=json.loads(message);require(sha(message)==record_doc['manifest_sha256']and release['release_sequence']==1 and release['version']=='0.1.0'and release['platform']=='x86_64-unknown-linux-gnu','exact initial signed manifest')
|
||||
require([(a['role'],a['sha256'],a['bytes'])for a in release['artifacts']]==[(role,manifest['files'][name]['sha256'],manifest['files'][name]['bytes'])for role,name in [('agent','agent.bin'),('helper','helper.bin')]],'signed release binds exact installed compatible code pair')
|
||||
require(record_doc['activation_evidence']=={'kind':'bootstrap','qualification_receipt_sha256':receipt['qualification_sha256']}and policy['updater_binary_sha256']==manifest['files']['updater.bin']['sha256'],'actual qualification and stable updater')
|
||||
require(all(policy[k]==manifest[k]for k in ('tenant_id','asset_id','agent_id','enrollment_generation','assignment_version'))and policy['services']==json.loads((folder/'registry-prepared.json').read_text())['services'],'exact local identity and actual unit pins')
|
||||
retained=retained_metadata();specs=[]
|
||||
mutable={'/var/lib/rmm-agent-remote/journal.sqlite','/var/lib/rmm-agent-remote/consumption.sqlite','/var/lib/rmm-helper/consumption.sqlite'}
|
||||
paths=sorted(set(retained)|mutable|{'/var/lib/rmm-agent-remote/agent.lock','/var/lib/rmm-helper/executor.lock'})
|
||||
for path in paths:
|
||||
m=Path(path).lstat();require(stat.S_ISREG(m.st_mode)and not Path(path).is_symlink(),'retained state original regular file');parent=Path(path).parent.lstat();owner=993 if path.startswith('/var/lib/rmm-agent-remote/')else 0;require(parent.st_uid==owner and not parent.st_mode&0o022,'retained state protected parent')
|
||||
specs.append({'path':path,'parent_uid':owner,'identity':dict(zip(('device','inode','uid','gid','mode','links'),identity(path))),'retention':'mutable_database'if path in mutable else'immutable_metadata'})
|
||||
require(0<len(specs)<=64,'bounded retained metadata set');raw['retained-files.json']=json.dumps(specs,separators=(',',':')).encode()
|
||||
targets={name:Path('/var/lib/rmm-agent-updater/installed-record.json')if name=='installed-record.json'else Path('/etc/rmm-agent-updater')/name for name in raw}
|
||||
require(all(not p.exists()for p in targets.values()),'create-only trust installation')
|
||||
updater=status('rmm-agent-updater.service');require(updater['ActiveState']=='inactive'and updater['MainPID']=='0'and updater['ControlPID']=='0'and not updater['Job'],'updater remains stopped before trust')
|
||||
record('trust-install-intent.json',{'files':{n:sha(b)for n,b in raw.items()},'qualification_sha256':receipt['qualification_sha256'],'public_signatures_verified':True,'updater_started':False})
|
||||
for name,data in raw.items():
|
||||
target=targets[name];mode=0o600 if name=='installed-record.json'else 0o644;fd=os.open(target,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.chmod(target,mode);syncdir(target.parent)
|
||||
require(retained_metadata()==retained,'original identity metadata retained through public trust install')
|
||||
record('trust-installed.json',{'files':{n:sha(b)for n,b in raw.items()},'public_signatures_verified':True,'job_authority':False})
|
||||
return {'case_id':ident,'protected_trust_installed':True,'release_and_baseline_signatures_verified':True,'updater_started':False,'job_authority':False}
|
||||
elif payload['action']=='updater-start':
|
||||
trust=json.loads((folder/'trust-installed.json').read_text());require(not(folder/'updater-start-intent.json').exists(),'existing start intent means readback only')
|
||||
for name,wanted in trust['files'].items():
|
||||
target=Path('/var/lib/rmm-agent-updater/installed-record.json')if name=='installed-record.json'else Path('/etc/rmm-agent-updater')/name;require(sha(target.read_bytes())==wanted,'protected trust unchanged before single start')
|
||||
updater=status('rmm-agent-updater.service');require(updater['ActiveState']=='inactive'and updater['MainPID']=='0'and updater['ControlPID']=='0'and not updater['Job'],'fixed stopped updater before start')
|
||||
record('updater-start-intent.json',{'unit':'rmm-agent-updater.service','only_once':True,'job_authority':False});run(['systemctl','start','--','rmm-agent-updater.service'],20)
|
||||
updater=status('rmm-agent-updater.service');pid=int(updater['MainPID']);require(updater['ActiveState']=='active'and pid>1 and updater['ControlPID']=='0'and not updater['Job'],'actual stable updater started')
|
||||
fd=os.pidfd_open(pid)
|
||||
try:
|
||||
start=Path('/proc/'+str(pid)+'/stat').read_text().rsplit(') ',1)[1].split()[19];image=Path('/proc/'+str(pid)+'/exe').read_bytes();require(sha(image)==manifest['files']['updater.bin']['sha256'],'actual original stable updater image')
|
||||
require(status('rmm-agent-updater.service')==updater,'same updater process held through observation')
|
||||
finally:os.close(fd)
|
||||
record('updater-started.json',{'status':updater,'process_start_ticks':start,'image_sha256':sha(image),'job_authority':False})
|
||||
return {'case_id':ident,'stable_updater_started_once':True,'pid':pid,'image_sha256':sha(image),'actual_agent_passive_observation_still_required':True,'job_authority':False}
|
||||
elif payload['action']=='readback':
|
||||
names=[p.name for p in folder.glob('*')if p.name.endswith('.json')and p.name!='manifest.json'];public={}
|
||||
for name in ('qualification.json','registry-prepared.json','pair-qualified.json','trust-installed.json','updater-started.json'):
|
||||
path=folder/name
|
||||
if path.exists():
|
||||
raw=path.read_bytes();require(len(raw)<=192*1024,'bounded public readback');public[name]={'sha256':sha(raw),'base64url':base64.urlsafe_b64encode(raw).rstrip(b'=').decode()}
|
||||
return {'case_id':ident,'records':names,'public_evidence':public,'status':{u:status(u)for u in units+['rmm-agent-updater.service']},'databases':databases(),'job_authority':False}
|
||||
else:raise RuntimeError('unknown fixed pair action')
|
||||
|
||||
def prepare(sdk_manifest):
|
||||
require=lambda ok,msg:None if ok else(_ for _ in()).throw(RuntimeError(msg))
|
||||
require(sha(BASE.read_bytes())==BASE_SHA,'exact capability derivative V2 manifest')
|
||||
build=json.loads(BASE.read_text());sdk=json.loads(sdk_manifest.read_text());require(sdk['profile']=='linux_updater_bootstrap_metadata_sdk_v1'and sdk['release_built']and sdk['clippy_all_targets_passed']and not sdk['target_mutated'],'sealed metadata SDK')
|
||||
inputs=native_rpc(public_inputs,{'operation':'linux_compatible_public_inputs_v1'})
|
||||
snapshot=transport.rpc('linux-vm.conf',prerequisite.inspect_vm,{'operation':'linux_updater_bootstrap_inspect_v1'})
|
||||
require(snapshot['boot_id']==BOOT and not snapshot['updater_provisioned'],'original initial target')
|
||||
full=json.loads(bytes(inputs['helper-full-maintenance.json']['bytes']));key=json.loads(bytes(inputs['helper-signing-key.json']['bytes']))
|
||||
require(full['signing_key_file']=='/etc/rmm-remote/helper-signing-key.json','independent fixed Full keyfile')
|
||||
binary=Path(sdk['binary']['path']);require(sha(binary.read_bytes())==sdk['binary']['sha256'],'sealed SDK bytes')
|
||||
request={'operation':'render_full','config':inputs['helper-full-maintenance.json']['bytes'],'pin':inputs['full-maintenance-helper-proof.json']['bytes'],'job_public_key_hex':key['public_key_hex'],'new_helper_sha256':build['artifacts']['source0']['helper']['sha256']}
|
||||
command=['docker','run','--rm','-i','--network','none','--read-only','--tmpfs','/tmp:rw,size=64m','-v',str(binary)+':/sdk:ro','--entrypoint','/sdk','sha256:9a76ad3744ebfdddae96ca10001ce8291c1b332fb54c8df5eea0e8e999ac6fc6']
|
||||
rendered=subprocess.run(command,input=json.dumps(request).encode(),capture_output=True,check=True,timeout=30);transition=json.loads(rendered.stdout)
|
||||
ident=str(uuid.uuid4());folder=ROOT/'devstate/linux-updater-compatible'/ident;folder.mkdir(mode=0o700,parents=True)
|
||||
files={}
|
||||
for name,item in [('agent.bin',build['artifacts']['source0']['agent']),('helper.bin',build['artifacts']['source0']['helper']),('updater.bin',build['stable_updater']),('sdk.bin',sdk['binary'])]:
|
||||
raw=Path(item['path']).read_bytes();require(sha(raw)==item['sha256']and len(raw)==item['bytes'],'sealed exact code artifact');files[name]=raw
|
||||
files['full-config.json']=bytes(transition['config']);files['full-pin.json']=bytes(transition['pin'])
|
||||
unit=ROOT/'devstate/linux-updater-qualification/21d20391-6102-4996-8a7a-efbd3e78bd7d/source0/deploy/linux/rmm-agent-updater.service';files['updater.service']=unit.read_bytes()
|
||||
proof=transition['proof'];require(proof['new_helper_sha256']==build['artifacts']['source0']['helper']['sha256']and proof['old_config_sha256']==inputs['helper-full-maintenance.json']['sha256']and proof['old_pin_sha256']==inputs['full-maintenance-helper-proof.json']['sha256'],'typed exact Full transition')
|
||||
for name,raw in files.items():once(folder/name,raw)
|
||||
binding=full['binding'];payload={'schema_version':1,'profile':'linux_compatible_bootstrap_package_v1','case_id':ident,'boot_id':BOOT,'tenant_id':binding['tenant_id'],'agent_id':binding['agent_id'],'asset_id':binding['asset_id'],'enrollment_generation':binding['enrollment_generation'],'assignment_version':binding['assignment_version'],'build_manifest_sha256':BASE_SHA,'sdk_manifest_sha256':sha(sdk_manifest.read_bytes()),'old_agent_sha256':snapshot['binaries']['/usr/local/libexec/rmm-agent-remote']['sha256'],'old_helper_sha256':snapshot['binaries']['/usr/local/libexec/rmm-privileged-helper']['sha256'],'old_config_sha256':proof['old_config_sha256'],'old_pin_sha256':proof['old_pin_sha256'],'full_transition':proof,'files':{n:{'sha256':sha(raw),'bytes':len(raw),'base64':base64.b64encode(raw).decode()}for n,raw in files.items()},'job_authority':False}
|
||||
once(folder/'package.json',json.dumps(payload,sort_keys=True,separators=(',',':')).encode());once(folder/'readonly-before.json',json.dumps(snapshot,sort_keys=True,indent=2).encode());once(folder/'public-inputs.json',json.dumps(inputs,sort_keys=True,indent=2).encode())
|
||||
summary={k:v for k,v in payload.items()if k!='files'};summary['files']={n:{k:v for k,v in e.items()if k!='base64'}for n,e in payload['files'].items()};once(folder/'manifest.json',json.dumps(summary,sort_keys=True,indent=2).encode())
|
||||
print(json.dumps({'package':str(folder/'package.json'),'manifest':str(folder/'manifest.json'),'case_id':ident,'local_only_preparation':True,'target_staged':False,'services_changed':False,'full_policy_sha256':proof['new_policy_sha256']}))
|
||||
|
||||
def main():
|
||||
parser=argparse.ArgumentParser(description=__doc__);parser.add_argument('action',choices=('prepare','public-inputs','central-check','stage','install','units','start','qualify','registry','trust','updater-start','passive-check','readback'));parser.add_argument('--package',type=Path);parser.add_argument('--sdk-manifest',type=Path);parser.add_argument('--trust-package',type=Path);args=parser.parse_args()
|
||||
if args.action=='prepare':
|
||||
if args.sdk_manifest is None:parser.error('--sdk-manifest required for preparation')
|
||||
prepare(args.sdk_manifest);return
|
||||
if args.package is None:parser.error('--package required for fixed native phases')
|
||||
p=json.loads(args.package.read_text());require=lambda ok,msg:None if ok else(_ for _ in()).throw(RuntimeError(msg))
|
||||
require(p['schema_version']==1 and p['profile']=='linux_compatible_bootstrap_package_v1'and p['boot_id']==BOOT,'reviewed exact package')
|
||||
if args.action=='public-inputs':value=native_rpc(public_inputs,{'operation':'linux_compatible_public_inputs_v1'})
|
||||
elif args.action=='central-check':value=native_rpc(central_closed,{'agent_id':p['agent_id'],'tenant_id':p['tenant_id']},config='server.conf')
|
||||
elif args.action=='stage':value=native_rpc(stage,p,timeout=120)
|
||||
elif args.action=='passive-check':
|
||||
if args.trust_package is None:parser.error('--trust-package required for exact passive comparison')
|
||||
trust=json.loads(args.trust_package.read_text());files=trust['files'];value=native_rpc(passive_check,{'tenant_id':p['tenant_id'],'agent_id':p['agent_id'],'record_sha256':files['installed-record.json']['sha256'],'policy_sha256':files['policy.json']['sha256'],'keyring_sha256':files['keyring.json']['sha256'],'health_pin_sha256':files['health-key.json']['sha256'],'agent_sha256':p['files']['agent.bin']['sha256'],'helper_sha256':p['files']['helper.bin']['sha256'],'updater_sha256':p['files']['updater.bin']['sha256'],'helper_policy_sha256':p['full_transition']['new_policy_sha256']},config='server.conf')
|
||||
else:
|
||||
payload={'case_id':p['case_id'],'action':args.action}
|
||||
if args.action=='install':payload['central_fence']=native_rpc(central_closed,{'agent_id':p['agent_id'],'tenant_id':p['tenant_id']},config='server.conf')
|
||||
if args.action=='trust':
|
||||
if args.trust_package is None:parser.error('--trust-package required for protected trust installation')
|
||||
payload['trust']=json.loads(args.trust_package.read_text())
|
||||
value=native_rpc(pair,payload,timeout=120)
|
||||
print(json.dumps(value))
|
||||
if __name__=='__main__':main()
|
||||
@@ -0,0 +1,82 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Prepare public updater metadata using the sealed Rust codecs; never signs.
|
||||
|
||||
Every input is public evidence. A locally prepared document is neither a native
|
||||
qualification nor trusted until the existing native verifier accepts it.
|
||||
"""
|
||||
import argparse
|
||||
import base64
|
||||
import datetime
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import uuid
|
||||
import linux_updater_compatible_apply as bootstrap
|
||||
|
||||
SDK = bootstrap.ROOT / 'devstate/linux-updater-bootstrap-sdk/d309c949-3a70-4448-88da-49c710093221/sdk.bin'
|
||||
SDK_SHA = '6fcc543d538fca30ceb499364880e4dc3689700d50597f18bd11bfe3559a0021'
|
||||
|
||||
def require(condition):
|
||||
if not condition: raise RuntimeError('Public updater preparation contract failed')
|
||||
def digest(raw): return hashlib.sha256(raw).hexdigest()
|
||||
def encode(value): return json.dumps(value, separators=(',', ':'), ensure_ascii=False).encode()
|
||||
def b64(raw): return base64.urlsafe_b64encode(raw).rstrip(b'=').decode()
|
||||
def unb64(value):
|
||||
raw = base64.urlsafe_b64decode(value + '=' * (-len(value) % 4))
|
||||
require(b64(raw) == value)
|
||||
return raw
|
||||
def typed(value):
|
||||
require(digest(SDK.read_bytes()) == SDK_SHA)
|
||||
result = subprocess.run(['docker', 'run', '--rm', '-i', '--network', 'none', '--read-only', '--tmpfs', '/tmp:rw,size=64m', '-v', str(SDK)+':/sdk:ro', '--entrypoint', '/sdk', 'sha256:9a76ad3744ebfdddae96ca10001ce8291c1b332fb54c8df5eea0e8e999ac6fc6'], input=encode(value), capture_output=True, check=True, timeout=30)
|
||||
return json.loads(result.stdout)
|
||||
def prepare(action, data):
|
||||
signer = data['signer']
|
||||
require(signer['platform'] == 'linux' and signer['profile'] == 'agent_updater_separate_signers_v1')
|
||||
common = {'case_id': signer['case_id'], 'platform': 'linux', 'job_authority': False}
|
||||
if action == 'release-input':
|
||||
build = json.loads(bootstrap.BASE.read_text())
|
||||
require(digest(bootstrap.BASE.read_bytes()) == bootstrap.BASE_SHA)
|
||||
seq = data['sequence']; require(seq in (1, 2))
|
||||
published = int(datetime.datetime.now(datetime.timezone.utc).timestamp())
|
||||
manifest = {'schema_version': 1, 'release_id': str(uuid.uuid4()), 'release_sequence': seq, 'version': '0.1.0' if seq == 1 else '0.1.1', 'platform': 'x86_64-unknown-linux-gnu', 'published_at': published, 'expires_at': published + 604800, 'protocol_major': 1, 'state_schema': 1, 'minimum_updater_version': 1, 'artifacts': []}
|
||||
for role in ('agent', 'helper'):
|
||||
item = build['artifacts']['source'+str(seq-1)][role]
|
||||
manifest['artifacts'].append({'role': role, 'artifact_id': str(uuid.uuid4()), 'sha256': item['sha256'], 'bytes': item['bytes']})
|
||||
result = typed({'operation': 'canonical_release', 'manifest': manifest})
|
||||
raw = bytes(result['bytes'])
|
||||
return {**common, 'canonical_payload_base64url': b64(raw), 'payload_sha256': digest(raw)}
|
||||
qualification = unb64(data['qualification_base64url']); q = json.loads(qualification)
|
||||
require(q['profile'] == 'linux_compatible_baseline_qualification_v1' and q['normal_signed_sync_observed'])
|
||||
require(all(q[n] is False for n in ('ready', 'helper_ready', 'unlock', 'job_authority')))
|
||||
if action == 'record-input':
|
||||
release = data['signed_release']; manifest = json.loads(unb64(release['envelope']['payload_bytes']))
|
||||
require(manifest['release_sequence'] == 1 and manifest['version'] == '0.1.0')
|
||||
record = {'schema_version': 1, 'profile': 'decoder_compatible_handoff_v1', 'platform': manifest['platform'], 'release_envelope': release['envelope'], 'manifest_sha256': release['payload_sha256'], 'activation_floor': 1, 'activated_at': int(datetime.datetime.fromisoformat(q['observed_at'].replace('Z', '+00:00')).timestamp()), 'previous_record_sha256': None, 'activation_evidence': {'kind': 'bootstrap', 'qualification_receipt_sha256': digest(qualification)}, 'rolled_back': False}
|
||||
raw = bytes(typed({'operation': 'canonical_record', 'record': record})['bytes'])
|
||||
return {**common, 'canonical_payload_base64url': b64(raw), 'payload_sha256': digest(raw), 'qualification_base64url': b64(qualification)}
|
||||
require(action == 'trust-package')
|
||||
package = data['bootstrap_package']; registry = data['registry']; baseline = data['signed_baseline']
|
||||
record_raw = unb64(baseline['envelope']['payload_bytes']); record = json.loads(record_raw)
|
||||
require(record['activation_evidence'] == {'kind': 'bootstrap', 'qualification_receipt_sha256': digest(qualification)})
|
||||
registry_raw = bytes(registry['registry_bytes']); require(digest(registry_raw) == registry['reader_registry_sha256'])
|
||||
policy = {'schema_version': 1, 'profile': 'linux_general_full_v1', **{n: package[n] for n in ('tenant_id', 'asset_id', 'agent_id', 'enrollment_generation', 'assignment_version')}, 'ledger_path': '/var/lib/rmm-helper/consumption.sqlite', 'lock_path': '/var/lib/rmm-helper/executor.lock', 'installed_record_path': '/var/lib/rmm-agent-updater/installed-record.json', 'keyring_path': '/etc/rmm-agent-updater/keyring.json', 'health_pin_path': '/etc/rmm-agent-updater/health-key.json', 'registry_path': '/etc/rmm-agent-updater/reader-registry.json', 'reader_registry_sha256': registry['reader_registry_sha256'], 'services': registry['services'], 'updater_service': registry['updater_service'], 'updater_binary_sha256': package['files']['updater.bin']['sha256'], 'initial_baseline': baseline['envelope'], 'initial_record_sha256': digest(record_raw), 'initial_manifest_sha256': record['manifest_sha256'], 'initial_activation_floor': 1}
|
||||
policy_raw = bytes(typed({'operation': 'canonical_policy', 'policy': policy})['bytes'])
|
||||
keyring_raw = encode({'schema_version': 1, 'generation': 1, 'keys': [{'public_key_hex': signer['release_public_key_hex'], 'platform': 'x86_64-unknown-linux-gnu', 'first_sequence': 1, 'last_sequence': 2, 'revoked': False}]})
|
||||
require(keyring_raw == unb64(signer['keyring_base64url']) and digest(keyring_raw) == signer['keyring_sha256'])
|
||||
health_raw = encode(signer['health_pin'])
|
||||
shape = typed({'operation': 'validate_public_bundle', 'record': list(record_raw), 'policy': list(policy_raw), 'keyring': list(keyring_raw), 'health_pin': list(health_raw)})
|
||||
require(shape['shape_only_not_signature_verified'] is True)
|
||||
files = {'policy.json': policy_raw, 'keyring.json': keyring_raw, 'health-key.json': health_raw, 'reader-registry.json': registry_raw, 'installed-record.json': record_raw}
|
||||
return {'operation': 'linux_compatible_trust_install_v1', 'case_id': package['case_id'], 'signer_case_id': signer['case_id'], 'qualification_sha256': digest(qualification), 'files': {n: {'base64': base64.b64encode(raw).decode(), 'sha256': digest(raw), 'bytes': len(raw)} for n, raw in files.items()}, 'shape_checks': shape, 'signature_verified': False, 'job_authority': False}
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('action', choices=('release-input', 'record-input', 'trust-package'))
|
||||
parser.add_argument('--input', required=True, type=Path)
|
||||
parser.add_argument('--output', required=True, type=Path)
|
||||
args = parser.parse_args()
|
||||
result = prepare(args.action, json.loads(args.input.read_text()))
|
||||
bootstrap.once(args.output, encode(result))
|
||||
print(json.dumps({'output': str(args.output), 'sha256': digest(args.output.read_bytes()), 'local_only': True, 'target_changed': False}))
|
||||
if __name__ == '__main__': main()
|
||||
@@ -0,0 +1,211 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Freeze and build the Linux compatible baseline and version-only successor.
|
||||
|
||||
Offline read-only Docker source mounts, independent local cache, no credentials,
|
||||
VM/server command, job, daemon, signing-key generation or deployment. Windows
|
||||
sources are retained as provenance only, never qualified by this Linux run.
|
||||
"""
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import shutil
|
||||
import stat
|
||||
import subprocess
|
||||
import tarfile
|
||||
import time
|
||||
import uuid
|
||||
ROOT=Path(__file__).resolve().parents[1]
|
||||
STATE=ROOT/'devstate/linux-updater-qualification'
|
||||
BUILDER='sha256:9a76ad3744ebfdddae96ca10001ce8291c1b332fb54c8df5eea0e8e999ac6fc6'
|
||||
PACKAGES=['rmm-agent','rmm-agent-core','rmm-domain','rmm-privileged-helper','rmm-updater-linux','rmm-updater-worker','rmm-updater-handoff','rmm-update-history']
|
||||
UNIT_TARGETS=[('agent','rmm-agent',['--bin','rmm-agent']),('helper','rmm-privileged-helper',['--lib']),
|
||||
('core','rmm-agent-core',['--lib']),('helper-reconcile','rmm-agent-core',['--test','helper_reconcile']),
|
||||
('domain','rmm-domain',['--lib']),('updater-linux','rmm-updater-linux',['--lib']),
|
||||
('worker','rmm-updater-worker',['--lib']),('handoff','rmm-updater-handoff',['--test','handoff_contract'])]
|
||||
SAFE_FILTERS={
|
||||
'updater-linux':['services::status_tests::accepts_actual_seven_property_shape_and_denies_missing_or_extra_properties'],
|
||||
'helper':['update_admission::tests::typed_handoff_ack_is_not_an_outcome_or_a_new_start_lease'],
|
||||
'core':['update_health_sync::tests::same_floor_does_not_admit_a_one_nanosecond_early_or_late_apply'],
|
||||
'helper-reconcile':['update_handoff_consumption_start_and_stop_intent_are_one_transaction',
|
||||
'update_inflight_negative_control_keeps_every_start_closed_and_owner_loss_is_terminal',
|
||||
'native_stop_intent_is_original_head_bound_create_once_and_transactional',
|
||||
'restarted_update_can_only_schedule_control_from_its_verified_original_not_unknown'],
|
||||
}
|
||||
|
||||
def require(ok,code):
|
||||
if not ok:raise RuntimeError(code)
|
||||
|
||||
def sha(raw):return hashlib.sha256(raw).hexdigest()
|
||||
def encode(value):return json.dumps(value,sort_keys=True,indent=2).encode()+b'\n'
|
||||
def write(path,raw):
|
||||
path=Path(path);path.parent.mkdir(mode=0o700,parents=True,exist_ok=True)
|
||||
fd=os.open(path,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())
|
||||
|
||||
def public_files():
|
||||
names=subprocess.run(['git','ls-files','--cached','--others','--exclude-standard','-z'],cwd=ROOT,capture_output=True,check=True).stdout.split(b'\0')
|
||||
prefixes=('crates/','proto/','migrations/','deploy/','tools/','tests/')
|
||||
fixed={'Cargo.toml','Cargo.lock','rust-toolchain.toml','Rust_RMM_Implementierungsspezifikation.md','AGENTS.md'}
|
||||
result=[]
|
||||
for raw in names:
|
||||
if not raw:continue
|
||||
name=os.fsdecode(raw);path=Path(name)
|
||||
if name not in fixed and not name.startswith(prefixes):continue
|
||||
require(not path.is_absolute() and '..' not in path.parts,'source relative path')
|
||||
if '__pycache__' in path.parts or path.suffix in ('.pyc','.pyo') or path.name=='.env':continue
|
||||
full=ROOT/path
|
||||
require(not full.is_symlink() and full.is_file(),'public regular source only')
|
||||
meta=full.stat();require(meta.st_size<=16*1024*1024,'bounded compiler/public fixture')
|
||||
result.append(name)
|
||||
require({'Cargo.toml','Cargo.lock','crates/agent/src/main.rs','crates/privileged-helper/src/main.rs','crates/updater-worker/src/main.rs'}<=set(result),'complete Linux entrypoints')
|
||||
return sorted(set(result))
|
||||
|
||||
def capture(root,names):
|
||||
result={}
|
||||
for name in names:
|
||||
file=root/name;before=file.stat();raw=file.read_bytes();after=file.stat()
|
||||
require((before.st_dev,before.st_ino,before.st_size,before.st_mtime_ns,before.st_ctime_ns)==
|
||||
(after.st_dev,after.st_ino,after.st_size,after.st_mtime_ns,after.st_ctime_ns),'source changed while reading')
|
||||
result[name]=dict(bytes=len(raw),sha256=sha(raw))
|
||||
return result
|
||||
|
||||
def readonly(root):
|
||||
for path in root.rglob('*'):
|
||||
path.chmod(0o555 if path.is_dir() else 0o444)
|
||||
root.chmod(0o555)
|
||||
|
||||
def freeze(folder):
|
||||
names=public_files();before=capture(ROOT,names);source0=folder/'source0'
|
||||
source0.mkdir(mode=0o700)
|
||||
for name in names:write(source0/name,(ROOT/name).read_bytes())
|
||||
require(capture(source0,names)==before and public_files()==names and capture(ROOT,names)==before,'whole source input snapshot drift; preserve failed case, do not build')
|
||||
require(re.search(r'(?ms)^\[workspace\.package\]\s*version = "0\.1\.0"$',(source0/'Cargo.toml').read_text()),'exact baseline version0.1.0')
|
||||
readonly(source0);write(folder/'source0-inputs.json',encode(before))
|
||||
source1=folder/'source1';shutil.copytree(source0,source1)
|
||||
cargo=source1/'Cargo.toml';lock=source1/'Cargo.lock';cargo.chmod(0o600);lock.chmod(0o600)
|
||||
original=cargo.read_text();require(original.count('version = "0.1.0"')==1,'one workspace version field')
|
||||
cargo.write_text(original.replace('version = "0.1.0"','version = "0.1.1"',1))
|
||||
match=re.search(r'(?ms)^members\s*=\s*(\[.*?\])',original)
|
||||
require(match is not None,'literal workspace member list')
|
||||
members=json.loads(match[1]);internal=[]
|
||||
require(all(isinstance(member,str) and member.startswith('crates/') and '..' not in Path(member).parts for member in members),'closed workspace member paths')
|
||||
for member in members:
|
||||
package=(source1/member/'Cargo.toml').read_text()
|
||||
if re.search(r'(?m)^version\.workspace\s*=\s*true$',package):
|
||||
name=re.search(r'(?m)^name\s*=\s*"([a-z0-9-]+)"$',package)
|
||||
require(name is not None,'literal inherited package name');internal.append(name[1])
|
||||
text=lock.read_text()
|
||||
for name in internal:
|
||||
pattern='name = "'+name+'"\nversion = "0.1.0"'
|
||||
require(text.count(pattern)==1,'one inherited internal lock package')
|
||||
text=text.replace(pattern,'name = "'+name+'"\nversion = "0.1.1"',1)
|
||||
lock.write_text(text)
|
||||
# External crate packages and the excluded independent oidc-auth version
|
||||
# remain exact; only inherited workspace package versions change.
|
||||
inverse=text
|
||||
for name in internal:inverse=inverse.replace('name = "'+name+'"\nversion = "0.1.1"','name = "'+name+'"\nversion = "0.1.0"',1)
|
||||
require(inverse==(source0/'Cargo.lock').read_text(),'no change outside exact inherited internal version lines')
|
||||
cargo.chmod(0o444);lock.chmod(0o444);readonly(source1)
|
||||
after=capture(source1,names)
|
||||
require({name for name in names if before[name]!=after[name]}=={'Cargo.toml','Cargo.lock'},'successor only differs in version/lock metadata')
|
||||
write(folder/'source1-inputs.json',encode(after))
|
||||
return names,before,after,internal
|
||||
|
||||
def run(folder,source,cache,label,args):
|
||||
logfile=folder/'logs'/(label+'.log');logfile.parent.mkdir(mode=0o700,exist_ok=True)
|
||||
command=['docker','run','--rm','--network','none','--read-only','--tmpfs','/tmp:rw,size=512m',
|
||||
'--tmpfs','/run:rw,size=128m','--tmpfs','/var/lib:rw,size=128m',
|
||||
'-v',str(source)+':/src:ro','-v',str(cache)+':/target','-e','CARGO_TARGET_DIR=/target',
|
||||
'-w','/src','--entrypoint','/bin/sh',BUILDER,'-c','mkdir -p /tmp/opencode\nexec '+__import__('shlex').join(args)]
|
||||
write(folder/'logs'/(label+'-command.json'),encode(command))
|
||||
started=time.time();print(json.dumps(dict(stage=label,started=True)),flush=True)
|
||||
with logfile.open('xb') as stream:
|
||||
result=subprocess.run(command,stdout=stream,stderr=subprocess.STDOUT,timeout=1800)
|
||||
logfile.chmod(0o600)
|
||||
result_record=dict(command=command,exit_code=result.returncode,elapsed_seconds=round(time.time()-started,3),log_sha256=sha(logfile.read_bytes()))
|
||||
write(folder/'logs'/(label+'-result.json'),encode(result_record))
|
||||
require(result.returncode==0,'build/check failed: '+label+'; original log preserved')
|
||||
return logfile.read_bytes()
|
||||
|
||||
def copy_artifact(path,target):
|
||||
raw=path.read_bytes();require(raw[:6]==b'\x7fELF\x02\x01' and raw[18:20]==b'\x3e\x00','native Linux x86-64 ELF')
|
||||
write(target,raw);target.chmod(0o500)
|
||||
return dict(path=str(target),sha256=sha(raw),bytes=len(raw),platform='x86_64-unknown-linux-gnu')
|
||||
|
||||
def cargo_packages():
|
||||
return [part for package in PACKAGES for part in ('-p',package)]
|
||||
|
||||
def build():
|
||||
os.umask(0o077);ident=str(uuid.uuid4());folder=STATE/ident;folder.mkdir(mode=0o700,parents=True)
|
||||
write(folder/'entered.json',encode(dict(schema_version=1,case_id=ident,root=str(ROOT),builder=BUILDER,
|
||||
git_head=subprocess.run(['git','rev-parse','HEAD'],cwd=ROOT,capture_output=True,check=True,text=True).stdout.strip(),
|
||||
no_target_mutations=True,windows_not_qualified=True)))
|
||||
names,input0,input1,internal=freeze(folder)
|
||||
cache=folder/'cache';cache.mkdir(mode=0o700)
|
||||
results={};units={}
|
||||
for version,inputs in (('source0',input0),('source1',input1)):
|
||||
source=folder/version
|
||||
run(folder,source,cache,version+'-metadata',['cargo','metadata','--locked','--offline','--no-deps','--format-version=1'])
|
||||
run(folder,source,cache,version+'-clippy',['cargo','clippy','--locked','--offline',*cargo_packages(),'--all-targets','--','-D','warnings'])
|
||||
packages=['-p','rmm-agent','-p','rmm-privileged-helper']+(['-p','rmm-updater-worker'] if version=='source0' else [])
|
||||
run(folder,source,cache,version+'-release',['cargo','build','--locked','--offline','--release',*packages,'--bins'])
|
||||
output=folder/'artifacts'/version;output.mkdir(mode=0o700,parents=True)
|
||||
results[version]={}
|
||||
for label,binary in [('agent','rmm-agent'),('helper','rmm-privileged-helper')]+([('updater','rmm-agent-updater')] if version=='source0' else []):
|
||||
results[version][label]=copy_artifact(cache/'release'/binary,output/binary)
|
||||
require(capture(source,names)==inputs,'frozen source changed during build')
|
||||
if version=='source1':continue
|
||||
for label,package,selector in UNIT_TARGETS:
|
||||
raw=run(folder,source,cache,'units-'+label,['cargo','test','--locked','--offline','--no-run','--message-format=json','-p',package,*selector])
|
||||
found=[]
|
||||
for line in raw.splitlines():
|
||||
try:record=json.loads(line)
|
||||
except ValueError:continue
|
||||
if record.get('reason')=='compiler-artifact' and record.get('profile',{}).get('test') and record.get('executable'):
|
||||
found.append(record['executable'])
|
||||
require(len(found)==1,'one linked unit executable '+label)
|
||||
relative=Path(found[0]).relative_to('/target')
|
||||
artifact=copy_artifact(cache/relative,output/(label+'-tests'))
|
||||
listing=run(folder,source,cache,'list-'+label,[str(Path('/target')/relative),'--list','--format','terse']).decode()
|
||||
tests=[line[:-6] for line in listing.splitlines() if line.endswith(': test')]
|
||||
require(tests and len(set(tests))==len(tests),'complete unique native test names')
|
||||
allowed=SAFE_FILTERS.get(label,[])
|
||||
if label=='handoff':allowed=tests
|
||||
require(set(allowed)<=set(tests),'exact safe filter exists')
|
||||
artifact.update(all_tests=tests,allowed_native_tests=allowed,
|
||||
other_tests_not_authorized_by_this_pack=True,
|
||||
safety='Pure handoff/codec or own temporary portable SQLite fixture only; no product service/database/keys.' if allowed else 'Linked only; no VM execution authorized.')
|
||||
units[label]=artifact
|
||||
for index,name in enumerate(allowed):
|
||||
run(folder,source,cache,'focused-'+label+'-'+str(index),[str(Path('/target')/relative),'--exact',name,'--nocapture'])
|
||||
require(capture(source,names)==inputs,'frozen source changed during unit qualification')
|
||||
require(results['source0']['agent']['sha256']!=results['source1']['agent']['sha256']
|
||||
and results['source0']['helper']['sha256']!=results['source1']['helper']['sha256'],'real new codepair bytes required')
|
||||
manifest=dict(schema_version=1,case_id=ident,profile='linux_compatible_updater_buildpack_v1',
|
||||
builder=BUILDER,network='none',source_mount='read_only',cache=str(cache),
|
||||
source0_version='0.1.0',source1_version='0.1.1',changed_source1_files=['Cargo.toml','Cargo.lock'],
|
||||
inherited_packages=internal,external_dependencies_unchanged=True,
|
||||
source0_inputs_sha256=sha((folder/'source0-inputs.json').read_bytes()),
|
||||
source1_inputs_sha256=sha((folder/'source1-inputs.json').read_bytes()),
|
||||
source_files_count=len(names),artifacts=results,units=units,
|
||||
stable_updater=results['source0']['updater'],checks=dict(linux_source0_clippy=True,linux_source1_clippy=True,
|
||||
release_links=True,exact_test_lists=True,focused_local_contracts=True,frozen_inputs_after_build=True),
|
||||
windows_copied_only_not_qualified=True,native_vm_tested=False,
|
||||
bootstrap_installed=False,apply_executed=False,keys_generated=False,approvals_performed=False)
|
||||
write(folder/'manifest.json',encode(manifest))
|
||||
for version in ('source0','source1'):
|
||||
archive=folder/(version+'.tar.gz')
|
||||
with tarfile.open(archive,'w:gz') as output:
|
||||
for name in names:output.add(folder/version/name,arcname=name,recursive=False)
|
||||
archive.chmod(0o400)
|
||||
write(folder/'archive-hashes.json',encode({version:sha((folder/(version+'.tar.gz')).read_bytes()) for version in ('source0','source1')}))
|
||||
write(folder/'completed.json',encode(dict(manifest_sha256=sha((folder/'manifest.json').read_bytes()),native_vm_tested=False)))
|
||||
print(json.dumps(dict(completed=True,manifest=str(folder/'manifest.json'),manifest_sha256=sha((folder/'manifest.json').read_bytes()),artifacts=results)),flush=True)
|
||||
|
||||
def main():
|
||||
parser=argparse.ArgumentParser(description=__doc__);parser.add_argument('action',choices=['build'])
|
||||
parser.parse_args();build()
|
||||
if __name__=='__main__':main()
|
||||
@@ -0,0 +1,120 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Explicit server-only release/baseline signing bootstrap; never installs policy.
|
||||
|
||||
Private release and Health keys remain on the server. No job/time key reuse,
|
||||
service restart, catalog mutation, grant or approval is performed by this tool.
|
||||
Prepared source: mutation phases require coordination by the owning Root task.
|
||||
"""
|
||||
import argparse
|
||||
import base64
|
||||
import json
|
||||
from pathlib import Path
|
||||
import linux_updater_compatible_apply as bootstrap
|
||||
|
||||
def server(payload):
|
||||
import base64,datetime,hashlib,json,os,stat,subprocess,uuid
|
||||
from pathlib import Path
|
||||
require(os.geteuid()==0 and os.uname().machine=='aarch64','fixed Root ARM64 server')
|
||||
root=Path('/opt/rust-discovery/devstate');platform=payload.get('platform','linux');require(platform in('linux','windows'),'closed release key platform');release_dir=root/('agent-update-release-'+platform+'-v1');health_dir=root/'agent-update-health-v1'
|
||||
release_key=release_dir/'release.pem';health_key=health_dir/'health.pem';trust_dir=root/('agent-update-public-'+platform+'-v1')
|
||||
ident=str(uuid.UUID(payload['case_id']));case=root/('updater-signing-'+ident)
|
||||
def digest(raw):return hashlib.sha256(raw).hexdigest()
|
||||
def encode(value):return json.dumps(value,separators=(',',':'),ensure_ascii=False).encode()
|
||||
def run(args):return subprocess.check_output(args,stderr=subprocess.DEVNULL,timeout=10)
|
||||
def syncdir(path):
|
||||
fd=os.open(path,os.O_RDONLY|os.O_DIRECTORY)
|
||||
try:os.fsync(fd)
|
||||
finally:os.close(fd)
|
||||
def metadata(path):
|
||||
m=path.lstat();require(stat.S_ISREG(m.st_mode)and m.st_nlink==1 and stat.S_IMODE(m.st_mode)==0o600,'single protected key file');return [m.st_dev,m.st_ino,m.st_uid,m.st_gid,m.st_mode,m.st_nlink,m.st_size,m.st_mtime_ns,m.st_ctime_ns]
|
||||
def public(key):
|
||||
raw=run(['openssl','pkey','-in',str(key),'-pubout','-outform','DER']);require(len(raw)==44 and raw[:12]==bytes.fromhex('302a300506032b6570032100'),'Ed25519 public DER');return raw[12:]
|
||||
def store(name,value):
|
||||
path=case/name
|
||||
with path.open('xb')as stream:stream.write(encode(value));stream.flush();os.fsync(stream.fileno())
|
||||
syncdir(case)
|
||||
def b64(raw):return base64.urlsafe_b64encode(raw).rstrip(b'=').decode()
|
||||
def decode(text):
|
||||
raw=base64.urlsafe_b64decode(text+'='*((-len(text))%4));require(b64(raw)==text,'canonical Base64url');return raw
|
||||
if payload['action']=='public-separation':
|
||||
pin=Path('/etc/rmm-time-source/time-public-key.json');before=metadata(pin);require(before[2]==0 and before[6]<=8192,'original Root public time pin');raw=pin.read_bytes();require(metadata(pin)==before,'public time pin unchanged')
|
||||
time_key=json.loads(raw)['public_key_hex'];job_key=payload['job_public_key_hex'];require(all(len(v)==64 and set(v)<=set('0123456789abcdef')for v in(time_key,job_key))and time_key!=job_key,'independent actual Job/time public keys')
|
||||
return {'operation':'updater_signer_public_separation_v1','forbidden_public_key_sha256':sorted([digest(bytes.fromhex(v))for v in(time_key,job_key)]),'time_public_pin_sha256':digest(raw),'private_key_read':False,'services_changed':False,'job_authority':False}
|
||||
if payload['action']=='provision':
|
||||
forbidden=payload['forbidden_public_key_sha256'];require(len(forbidden)>=2 and len(forbidden)<=16 and len(set(forbidden))==len(forbidden)and all(len(v)==64 and set(v)<=set('0123456789abcdef')for v in forbidden),'independent Job/time public-key SHA256 values required, never role aliases')
|
||||
require(not release_dir.exists()and not trust_dir.exists()and not case.exists(),'create-only independent release signer namespace')
|
||||
owner=(root/'jobs/ed25519.pem').lstat().st_uid;require(owner==10001,'actual existing signer owner contract')
|
||||
case.mkdir(mode=0o700);store('provision-intent.json',{'release_dir':str(release_dir),'health_dir':str(health_dir),'forbidden_public_key_sha256':forbidden,'job_authority':False})
|
||||
health_origin=health_dir/'public-origin.json'
|
||||
reuse_health=health_dir.exists()
|
||||
if reuse_health:
|
||||
directory=health_dir.lstat();origin_meta=health_origin.lstat();require(stat.S_ISDIR(directory.st_mode)and directory.st_uid==owner and stat.S_IMODE(directory.st_mode)==0o700 and stat.S_ISREG(origin_meta.st_mode)and origin_meta.st_uid==0 and origin_meta.st_nlink==1 and stat.S_IMODE(origin_meta.st_mode)==0o600 and origin_meta.st_size<=4096,'original protected shared Health provenance')
|
||||
origin=json.loads(health_origin.read_text());require(origin['profile']=='shared_agent_update_health_v1'and metadata(health_key)==origin['private_metadata']and public(health_key).hex()==origin['public_key_hex'],'exact original shared Health key')
|
||||
for directory,key,uid in [(release_dir,release_key,0)]+([]if reuse_health else[(health_dir,health_key,owner)]):
|
||||
directory.mkdir(mode=0o700);fd=os.open(key,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,0o600);os.close(fd)
|
||||
run(['openssl','genpkey','-algorithm','Ed25519','-out',str(key)]);os.chmod(key,0o600);os.chown(key,uid,uid);os.chown(directory,uid,uid)
|
||||
fd=os.open(key,os.O_RDONLY|os.O_NOFOLLOW)
|
||||
try:os.fsync(fd)
|
||||
finally:os.close(fd)
|
||||
syncdir(directory);syncdir(root)
|
||||
release=public(release_key);health=public(health_key);require(release!=health and digest(release)not in forbidden and digest(health)not in forbidden,'independent key domains')
|
||||
if not reuse_health:
|
||||
with health_origin.open('x')as stream:json.dump({'profile':'shared_agent_update_health_v1','public_key_hex':health.hex(),'private_metadata':metadata(health_key)},stream,sort_keys=True);stream.flush();os.fsync(stream.fileno())
|
||||
health_origin.chmod(0o600)
|
||||
syncdir(health_dir)
|
||||
other=root/('agent-update-release-'+('windows'if platform=='linux'else'linux')+'-v1/release-public.json')
|
||||
if other.exists():
|
||||
other_meta=other.lstat();require(stat.S_ISREG(other_meta.st_mode)and other_meta.st_uid==0 and stat.S_IMODE(other_meta.st_mode)==0o600 and other_meta.st_nlink==1 and other_meta.st_size<=4096,'other platform original public provenance');require(json.loads(other.read_text())['public_key_hex']!=release.hex(),'distinct platform release keys')
|
||||
with(release_dir/'release-public.json').open('x')as stream:json.dump({'platform':platform,'public_key_hex':release.hex(),'key_id':digest(release)},stream,sort_keys=True);stream.flush();os.fsync(stream.fileno())
|
||||
os.chmod(release_dir/'release-public.json',0o600)
|
||||
syncdir(release_dir)
|
||||
keyring=encode({'schema_version':1,'generation':1,'keys':[{'public_key_hex':release.hex(),'platform':'x86_64-unknown-linux-gnu'if platform=='linux'else'x86_64-pc-windows-msvc','first_sequence':1,'last_sequence':2,'revoked':False}]})
|
||||
trust_dir.mkdir(mode=0o700);os.chown(trust_dir,owner,owner)
|
||||
fd=os.open(trust_dir/'keyring.json',os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,0o600)
|
||||
with os.fdopen(fd,'wb')as stream:os.fchown(stream.fileno(),owner,owner);stream.write(keyring);stream.flush();os.fsync(stream.fileno())
|
||||
syncdir(trust_dir);syncdir(root)
|
||||
result={'schema_version':1,'profile':'agent_updater_separate_signers_v1','platform':platform,'shared_health_key_reused':reuse_health,'case_id':ident,'release_key_id':digest(release),'release_public_key_hex':release.hex(),'keyring_base64url':b64(keyring),'keyring_sha256':digest(keyring),'public_keyring_host_path':str(trust_dir/'keyring.json'),'public_keyring_metadata':metadata(trust_dir/'keyring.json'),'keyring_recipient_roles':['api','gateway'],'health_pin':{'schema_version':1,'key_id':digest(health),'public_key_hex':health.hex()},'release_private_metadata':metadata(release_key),'health_private_metadata':metadata(health_key),'health_recipient_role':'gateway','health_recipient_uid':owner,'gateway_mount_pending':True,'services_changed':False,'job_authority':False}
|
||||
store('provision-result.json',result);return result
|
||||
require(case.is_dir(),'existing explicit signer case')
|
||||
provision=json.loads((case/'provision-result.json').read_text());require(provision['case_id']==ident and provision['platform']==platform and public(release_key).hex()==provision['release_public_key_hex']and public(health_key).hex()==provision['health_pin']['public_key_hex'],'original independent keys')
|
||||
require(metadata(release_key)==provision['release_private_metadata']and metadata(health_key)==provision['health_private_metadata'],'protected private key metadata unchanged')
|
||||
require(metadata(trust_dir/'keyring.json')==provision['public_keyring_metadata']and digest((trust_dir/'keyring.json').read_bytes())==provision['keyring_sha256'],'original protected public keyring')
|
||||
if payload['action']=='readback':return provision
|
||||
require(payload['action']in('sign-release','sign-baseline'),'fixed signing purpose only')
|
||||
require(platform=='linux','Windows signing awaits a separately qualified actual MSVC source pair; never relabel GNU')
|
||||
raw=decode(payload['canonical_payload_base64url']);require(len(raw)<=32768 and digest(raw)==payload['payload_sha256'],'exact typed public payload')
|
||||
doc=json.loads(raw)
|
||||
artifacts={1:[('7fef8d0f1e2bcf5cd6c373e567a0c17bfb08d8c780cd4d1925b6c1355e572547',16275360),('8fdc6ded02278eb18e64003d0bc7bf45d452cd48ded0450e6e900ca66e44d449',13325104)],2:[('4d212066bef226af3fa679ffe95f69c43a9668cbef7e665d53328f4e41aa3799',16273896),('89b28db3afe870c667297dff97ad7a042c550ccce1f712d69108e8de672ad450',13322944)]}
|
||||
if payload['action']=='sign-release':
|
||||
sequence=doc['release_sequence'];require(sequence in(1,2)and doc['schema_version']==1 and doc['version']==('0.1.0'if sequence==1 else'0.1.1')and doc['platform']=='x86_64-unknown-linux-gnu'and doc['protocol_major']==1 and doc['state_schema']==1 and doc['minimum_updater_version']==1,'exact compatible release profile')
|
||||
require(str(uuid.UUID(doc['release_id']))==doc['release_id']and doc['release_id']!='00000000-0000-0000-0000-000000000000','release identity')
|
||||
require(abs(int(datetime.datetime.now(datetime.timezone.utc).timestamp())-doc['published_at'])<300 and doc['expires_at']-doc['published_at']==604800,'bounded initial release dates')
|
||||
require(len(doc['artifacts'])==2 and [a['role']for a in doc['artifacts']]==['agent','helper']and [(a['sha256'],a['bytes'])for a in doc['artifacts']]==artifacts[sequence],'exact frozen source artifact hashes')
|
||||
ids=[str(uuid.UUID(a['artifact_id']))for a in doc['artifacts']];require(len(set(ids))==2 and all(v!='00000000-0000-0000-0000-000000000000'for v in ids),'distinct artifact identities')
|
||||
prefix=b'rmm.agent-release.v1\0';name='release-'+str(sequence)
|
||||
else:
|
||||
require(doc['schema_version']==1 and doc['profile']=='decoder_compatible_handoff_v1'and doc['platform']=='x86_64-unknown-linux-gnu'and doc['activation_floor']==1 and doc['previous_record_sha256']is None and doc['rolled_back']is False,'initial compatible baseline only')
|
||||
prior=json.loads((case/'release-1-result.json').read_text());require(doc['release_envelope']==prior['envelope']and doc['manifest_sha256']==prior['payload_sha256'],'exact original signed baseline release')
|
||||
qualification=decode(payload['qualification_base64url']);q=json.loads(qualification)
|
||||
require(q['profile']=='linux_compatible_baseline_qualification_v1'and q['normal_signed_sync_observed']and q['ready']is False and q['helper_ready']is False and q['unlock']is False and q['job_authority']is False,'independent actual compatible qualification, never a lease')
|
||||
require([s['service']for s in q['services']]==['agent','general','full']and [s['process']['image_sha256']for s in q['services']]==[artifacts[1][0][0],artifacts[1][1][0],artifacts[1][1][0]],'actual compatible initial code pair')
|
||||
require(doc['activation_evidence']=={'kind':'bootstrap','qualification_receipt_sha256':digest(qualification)},'exact public qualification hash')
|
||||
observed=datetime.datetime.fromisoformat(q['observed_at'].replace('Z','+00:00'));require(observed.tzinfo is not None and int(observed.timestamp())==doc['activated_at'],'actual baseline activation time')
|
||||
prefix=b'rmm.agent-update-baseline.v1\0';name='baseline'
|
||||
require(not(case/(name+'-intent.json')).exists(),'existing signing intent means readback only')
|
||||
store(name+'-intent.json',{'payload_sha256':digest(raw),'purpose':payload['action'],'job_authority':False})
|
||||
message=case/(name+'.message');signature=case/(name+'.signature')
|
||||
with message.open('xb')as stream:stream.write(prefix+raw);stream.flush();os.fsync(stream.fileno())
|
||||
run(['openssl','pkeyutl','-sign','-rawin','-inkey',str(release_key),'-in',str(message),'-out',str(signature)])
|
||||
signed=signature.read_bytes();require(len(signed)==64,'Ed25519 signature')
|
||||
public_der=case/(name+'.public.der')
|
||||
with public_der.open('xb')as stream:stream.write(bytes.fromhex('302a300506032b6570032100')+bytes.fromhex(provision['release_public_key_hex']));stream.flush();os.fsync(stream.fileno())
|
||||
run(['openssl','pkeyutl','-verify','-rawin','-pubin','-keyform','DER','-inkey',str(public_der),'-in',str(message),'-sigfile',str(signature)])
|
||||
result={'schema_version':1,'key_id':provision['release_key_id'],'payload_sha256':digest(raw),'envelope':{'schema_version':1,'key_id':provision['release_key_id'],'payload_bytes':b64(raw),'signature':b64(signed)},'job_authority':False}
|
||||
store(name+'-result.json',result);return result
|
||||
|
||||
def main():
|
||||
parser=argparse.ArgumentParser(description=__doc__);parser.add_argument('action',choices=('public-separation','provision','readback','sign-release','sign-baseline'));parser.add_argument('--input',type=Path,required=True);args=parser.parse_args()
|
||||
p=json.loads(args.input.read_text());p['action']=args.action
|
||||
print(json.dumps(bootstrap.native_rpc(server,p,config='server.conf')))
|
||||
if __name__=='__main__':main()
|
||||
Reference in New Issue
Block a user