#!/usr/bin/env python3 """Frozen pure Domain lib units, Linux VM FIRST then Windows VM; no RMM jobs. Local builds never contact a VM. Native starts require an explicit flag. Lost replies permit same-case readback only; existing SharedCore profiles stay intact. """ import argparse import base64 import hashlib import json import os from pathlib import Path import re import shutil import subprocess import uuid import remote_client_bootstrap as linux import vm_mvp_acceptance as contract from windows import client_bootstrap as windows ROOT=Path(__file__).resolve().parents[1] STATE=ROOT/'devstate/domain-scm' IMAGE='rust-discovery-durable-final-build:local' JOBS_SHA='1566d849c23b3f2792f9e003ac589022525f94cf8b088176ccdae50b99162417' REMOTE='/var/lib/rmm-domain-unit-acceptance' COUNT=15 require=contract.require def sha(path):return hashlib.sha256(Path(path).read_bytes()).hexdigest() def store(folder,name,value): fd=os.open(folder/name,os.O_CREAT|os.O_EXCL|os.O_WRONLY|os.O_NOFOLLOW,0o600) with os.fdopen(fd,'w') as out:json.dump(value,out,indent=2);out.write('\n');out.flush();os.fsync(out.fileno()) def source_files(): paths=[ROOT/'Cargo.toml',ROOT/'Cargo.lock',ROOT/'rust-toolchain.toml',ROOT/'crates/domain/Cargo.toml'] paths.extend((ROOT/'crates/domain/src').rglob('*.rs')) require(sha(ROOT/'crates/domain/src/jobs.rs')==JOBS_SHA,'final reviewed Domain SYSTEM guard source required') return {str(p.relative_to(ROOT)):sha(p) for p in sorted(set(paths))} def command(args,timeout=300): p=subprocess.run(args,capture_output=True,timeout=timeout) if p.returncode:raise RuntimeError(p.stderr.decode(errors='replace')[-3000:]) return p.stdout def build(): os.umask(0o077);STATE.mkdir(mode=0o700,parents=True,exist_ok=True);folder=STATE/('build-'+str(uuid.uuid4()));folder.mkdir(mode=0o700) before=source_files();runner=sha(__file__);store(folder,'source-before.json',before) image=command(['docker','image','inspect','--format','{{.Id}}',IMAGE]).decode().strip();require(re.fullmatch('sha256:[0-9a-f]{64}',image),'immutable local build image') target=STATE/'target';target.mkdir(mode=0o700,exist_ok=True) common=['docker','run','--rm','--network','none','--read-only','--tmpfs','/tmp:rw,size=512m','-v',str(ROOT)+':/src:ro','-v',str(target)+':/target','-e','CARGO_TARGET_DIR=/target','--entrypoint','cargo',image] artifacts={} for platform,architecture in [('linux',None),('windows','x86_64-pc-windows-gnu')]: options=['--manifest-path','/src/Cargo.toml','--locked','--offline','-p','rmm-domain'] if architecture:options+=['--target',architecture] command(common+['clippy']+options+['--all-targets','--','-D','warnings']) raw=command(common+['test']+options+['--lib','--release','--no-run','--message-format=json']) linked=[] for line in raw.splitlines(): try:value=json.loads(line) except ValueError:continue if value.get('reason')=='compiler-artifact' and value.get('target',{}).get('name')=='rmm_domain' and value.get('profile',{}).get('test') and value.get('executable'): linked.append(target/Path(value['executable']).relative_to('/target')) require(len(linked)==1,'exact Domain lib-test executable') name='domain-tests'+('.exe' if platform=='windows' else '');copy=folder/name;shutil.copyfile(linked[0],copy);copy.chmod(0o600);original=sha(linked[0]) strip='x86_64-w64-mingw32-strip' if architecture else 'strip' command(['docker','run','--rm','--network','none','--read-only','-v',str(folder.resolve())+':/artifacts','--entrypoint',strip,image,'--strip-debug','/artifacts/'+name]) require(sha(linked[0])==original,'original linked unit executable preserved') data=copy.read_bytes() if platform=='linux':require(data[:6]==b'\x7fELF\x02\x01' and data[18:20]==b'\x3e\x00','Linux x64 ELF') else: offset=int.from_bytes(data[60:64],'little');require(data[:2]==b'MZ' and data[offset:offset+6]==b'PE\0\0\x64\x86','Windows x64 PE') artifacts[platform]=dict(path=str(copy.resolve()),sha256=sha(copy),bytes=len(data),original_link_sha256=original,debug_stripped_copy=True) run=['docker','run','--rm','--network','none','--read-only','--tmpfs','/tmp:rw,size=64m','-v',artifacts['linux']['path']+':/domain-tests:ro','--entrypoint','/bin/sh',image,'-c'] listed=command(run+['exec /lib64/ld-linux-x86-64.so.2 /domain-tests --list --format terse']) names=sorted(s.decode() for s in re.findall(rb'^(.+): test$',listed,re.M));require(len(names)==COUNT and len(set(names))==COUNT,'exact fifteen portable Domain lib units') local=command(run+['exec /lib64/ld-linux-x86-64.so.2 /domain-tests --test-threads=1']) require(b'15 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out;' in local,'all fifteen local units; no ignored/filtering') require(source_files()==before and sha(__file__)==runner,'frozen source before/after equality') libc=command(['docker','run','--rm','--network','none','--read-only','--entrypoint','ldd',image,'--version']).decode().splitlines()[0] manifest=dict(schema_version=1,profile='domain-scm-lib-15',runner_sha256=runner,source_files=before,source_before_after_equal=True,build_image_id=image,build_libc=libc, test_names=names,test_count=COUNT,artifacts=artifacts,clippy_both_targets_D_warnings=True,local_linux_tests_passed=COUNT,local_linux_stdout_sha256=hashlib.sha256(local).hexdigest(), native_linux_executed=False,native_windows_executed=False,database=False,services=False,network=False,rmm_jobs=False) store(folder,'manifest.json',manifest);print(json.dumps(dict(manifest=str((folder/'manifest.json').resolve()),artifacts=artifacts,test_count=COUNT,native_executed=False))) def verified(path): m=contract.protected_json(path) require(m['profile']=='domain-scm-lib-15' and m['runner_sha256']==sha(__file__) and m['source_files']==source_files() and m['source_before_after_equal'] is True,'same frozen source/runner') require(m['test_count']==COUNT and len(set(m['test_names']))==COUNT,'exact pure lib tests') for item in m['artifacts'].values():require(sha(item['path'])==item['sha256'] and Path(item['path']).stat().st_size==item['bytes'],'immutable unit delivery copy') return m def linux_case(p): import resource import stat require(os.geteuid()==0 and os.uname().machine=='x86_64','Linux x64 bootstrap unit qualification') ident=str(uuid.UUID(p['id']));require(ident==p['id'] and p['action'] in ('prepare','run','read'),'fixed native case') require(len(p['sha256'])==64 and all(c in '0123456789abcdef' for c in p['sha256']) and p['test_count']==15,'fixed Domain unit bytes/count') root=Path('/var/lib/rmm-domain-unit-acceptance') def safe(path,directory): s=path.lstat();require(s.st_uid==0 and s.st_mode&0o077==0 and not stat.S_ISLNK(s.st_mode) and (stat.S_ISDIR(s.st_mode) if directory else stat.S_ISREG(s.st_mode)),'private fixed unit path') def save(path,value): fd=os.open(path,os.O_CREAT|os.O_EXCL|os.O_WRONLY|os.O_NOFOLLOW,0o600) with os.fdopen(fd,'w') as f:json.dump(value,f);f.flush();os.fsync(f.fileno()) if not root.exists():root.mkdir(mode=0o700) safe(root,True);case=root/ident if p['action']=='prepare':case.mkdir(mode=0o700);save(case/'intent.json',p);return dict(prepared=True) safe(case,True);safe(case/'intent.json',False);intent=json.loads((case/'intent.json').read_bytes());require(all(p[k]==intent[k] for k in ('id','sha256','manifest_sha256','test_count')),'same original native case') if p['action']=='read':safe(case/'result.json',False);return json.loads((case/'result.json').read_bytes()) binary=case/'domain-tests';safe(binary,False);require(hashlib.sha256(binary.read_bytes()).hexdigest()==p['sha256'],'exact Linux lib unit artifact') save(case/'started.json',dict(start_once=True,sha256=p['sha256']));binary.chmod(0o500);tmp=case/'tmp';tmp.mkdir(mode=0o700) env={'PATH':'/usr/bin:/bin','LANG':'C.UTF-8','TMPDIR':str(tmp),'RUST_BACKTRACE':'0'} require(subprocess.run(['/usr/bin/unshare','--net','--','/usr/bin/true'],env=env,capture_output=True,timeout=3).returncode==0,'network namespace required') listed=subprocess.run(['/usr/bin/unshare','--net','--',str(binary),'--list','--format','terse'],env=env,capture_output=True,timeout=5) require(listed.returncode==0 and len(listed.stdout)<=32768,'bounded exact unit listing') def limits():resource.setrlimit(resource.RLIMIT_CORE,(0,0));resource.setrlimit(resource.RLIMIT_FSIZE,(131072,131072));resource.setrlimit(resource.RLIMIT_CPU,(10,10)) with (case/'stdout.bin').open('xb') as out,(case/'stderr.bin').open('xb') as err: os.chmod(out.name,0o600);os.chmod(err.name,0o600);timed=False try:code=subprocess.run(['/usr/bin/unshare','--net','--',str(binary),'--test-threads=1'],cwd=case,env=env,stdin=subprocess.DEVNULL,stdout=out,stderr=err,timeout=20,preexec_fn=limits).returncode except subprocess.TimeoutExpired:timed=True;code=None out.flush();err.flush();os.fsync(out.fileno());os.fsync(err.fileno()) raw=(case/'stdout.bin').read_bytes();errors=(case/'stderr.bin').read_bytes();require(len(raw)<=131072 and len(errors)<=131072,'bounded unit output') libc=subprocess.run(['/usr/bin/ldd','--version'],env=env,capture_output=True,timeout=3);require(libc.returncode==0 and len(libc.stdout)<=4096,'actual VM libc metadata') result=dict(id=ident,platform='linux',sha256=p['sha256'],manifest_sha256=p['manifest_sha256'],listed_stdout=listed.stdout.decode(),exit_code=code,timed_out=timed,stdout_b64=base64.b64encode(raw).decode(),stderr_b64=base64.b64encode(errors).decode(),actual_vm_libc=libc.stdout.decode().splitlines()[0],network_namespace=True,environment_cleared=True,no_rmm_job=True) save(case/'result.json',result);return result WINDOWS=r''' $ErrorActionPreference='Stop' $p=([Console]::In.ReadLine()|ConvertFrom-Json) if([string]([guid]$p.id)-cne$p.id-or$p.action-notin@('run','read')-or$p.sha256-notmatch'^[a-f0-9]{64}$'-or$p.linux_proof_sha256-notmatch'^[a-f0-9]{64}$'){throw 'Fixed Linux-first unit case'} $case='C:\ProgramData\RustDiscoveryBootstrap\domain-unit-'+$p.id $exe='C:\ProgramData\RustDiscoveryBootstrap\'+$p.sha256+'\windows-tests-lib.exe' function WriteNew($path,$v){$f=[IO.File]::Open($path,[IO.FileMode]::CreateNew,[IO.FileAccess]::Write,[IO.FileShare]::Read);try{$b=[Text.Encoding]::UTF8.GetBytes(($v|ConvertTo-Json -Depth 8 -Compress));$f.Write($b,0,$b.Length);$f.Flush($true)}finally{$f.Dispose()}} if($p.action-eq'read'){$i=Get-Content -LiteralPath (Join-Path $case 'intent.json') -Raw|ConvertFrom-Json;if($i.sha256-cne$p.sha256-or$i.manifest_sha256-cne$p.manifest_sha256-or$i.linux_proof_sha256-cne$p.linux_proof_sha256){throw 'Original case'};Get-Content -LiteralPath (Join-Path $case 'result.json') -Raw;exit 0} if((Get-FileHash -LiteralPath $exe -Algorithm SHA256).Hash.ToLowerInvariant()-cne$p.sha256-or(Test-Path -LiteralPath $case)){throw 'Exact fresh unit artifact/case'} New-Item -ItemType Directory -Path $case|Out-Null WriteNew (Join-Path $case 'intent.json') $p function RunUnit([string]$arguments){$si=New-Object Diagnostics.ProcessStartInfo;$si.FileName=$exe;$si.Arguments=$arguments;$si.UseShellExecute=$false;$si.CreateNoWindow=$true;$si.RedirectStandardOutput=$true;$si.RedirectStandardError=$true;$proc=New-Object Diagnostics.Process;$proc.StartInfo=$si;if(!$proc.Start()){throw 'Unit start'};try{$o=$proc.StandardOutput.ReadToEndAsync();$e=$proc.StandardError.ReadToEndAsync();if(!$proc.WaitForExit(30000)){$proc.Kill();throw 'Unit timeout'};if($o.Result.Length-gt 131072-or$e.Result.Length-gt 131072){throw 'Bounded unit output'};return @{exit_code=$proc.ExitCode;stdout=$o.Result;stderr=$e.Result}}finally{$proc.Dispose()}} WriteNew (Join-Path $case 'started.json') @{start_once=$true;sha256=$p.sha256} $list=RunUnit '--list --format terse';$result=RunUnit '--test-threads=1' $value=@{platform='windows';sha256=$p.sha256;manifest_sha256=$p.manifest_sha256;linux_proof_sha256=$p.linux_proof_sha256;listed_stdout=$list.stdout;list_exit_code=$list.exit_code;result=$result;bootstrap_administrator_unit_tests=$true;local_service_proof=$false;no_rmm_job=$true;no_service_change=$true} WriteNew (Join-Path $case 'result.json') $value;$value|ConvertTo-Json -Depth 8 -Compress ''' def check_result(m,platform,result): require(result['platform']==platform and result['sha256']==m['artifacts'][platform]['sha256'],'actual platform/artifact binding') names=sorted(re.findall(r'^(.+): test\r?$',result['listed_stdout'],re.M));require(names==m['test_names'],'all same fifteen actual lib tests') if platform=='linux': require(result['exit_code']==0 and result['timed_out'] is False,'actual Linux unit success');out=base64.b64decode(result['stdout_b64'],validate=True) else: require(result['list_exit_code']==result['result']['exit_code']==0,'actual Windows unit success');out=result['result']['stdout'].encode() require(b'15 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out;' in out,'fifteen executed; no skipped/filtered tests') def execute(manifest_path,platform,case=None): m=verified(manifest_path);os.umask(0o077) if case is None: case=str(uuid.uuid4());folder=STATE/case;folder.mkdir(mode=0o700) store(folder,'intent.json',dict(id=case,platform=platform,manifest_path=str(manifest_path.resolve()),manifest_sha256=sha(manifest_path))) else: case=str(uuid.UUID(case));folder=STATE/case;intent=contract.protected_json(folder/'intent.json');require(intent['platform']==platform and intent['manifest_sha256']==sha(manifest_path),'original platform/manifest case') read=(folder/'started-local.json').exists() item=m['artifacts'][platform];payload=dict(id=case,sha256=item['sha256'],manifest_sha256=sha(manifest_path),test_count=COUNT) if platform=='linux': if not read: linux.rpc('linux-vm.conf',linux_case,dict(payload,action='prepare')) cfg=linux.access.load_config(ROOT/'credentials/linux-vm.conf');args=['sftp','-F','/dev/null','-P',cfg.get('port','22'),'-o','StrictHostKeyChecking=yes','-o','BatchMode=yes','-o','IdentitiesOnly=yes','-o','ForwardAgent=no','-o','ConnectTimeout=10','-i',str(Path(cfg['ssh_key_path']).expanduser()),'-b','-',cfg['username']+'@'+cfg['address']] uploaded=subprocess.run(args,input=('put '+json.dumps(item['path'])+' '+json.dumps(REMOTE+'/'+case+'/domain-tests')+'\n').encode(),capture_output=True,timeout=120);store(folder,'upload.json',dict(returncode=uploaded.returncode));require(uploaded.returncode==0,'fixed Linux unit upload') if not read:store(folder,'started-local.json',dict(run_once=True)) result=linux.rpc('linux-vm.conf',linux_case,dict(payload,action='read' if read else 'run')) else: proof=contract.protected_json(Path(manifest_path).parent/'linux-passed.json');require(proof['verified'] is True and proof['manifest_sha256']==sha(manifest_path),'same-manifest real Linux proof before Windows') check_result(m,'linux',proof['native']);payload['linux_proof_sha256']=sha(Path(manifest_path).parent/'linux-passed.json') if not read:windows.upload(Path(item['path']),'windows-tests-lib.exe');store(folder,'started-local.json',dict(run_once=True,linux_proof_sha256=payload['linux_proof_sha256'])) result=windows.powershell(WINDOWS,dict(payload,action='read' if read else 'run'),timeout=75) store(folder,'readback-'+str(uuid.uuid4())+'.json',result);require(result['manifest_sha256']==sha(manifest_path),'native result bound to original manifest');check_result(m,platform,result) value=dict(verified=True,manifest_sha256=sha(manifest_path),case_id=case,platform=platform,native=result) path=Path(manifest_path).parent/(platform+'-passed.json') if path.exists():require(contract.protected_json(path)==value,'retain identical original native lib proof') else:store(path.parent,path.name,value) print(json.dumps(dict(verified=True,platform=platform,case_id=case,test_count=COUNT))) def main(): p=argparse.ArgumentParser(description=__doc__);p.add_argument('action',choices=('build-local','linux','windows','readback-linux','readback-windows'));p.add_argument('--manifest',type=Path);p.add_argument('--case');p.add_argument('--execute-native-tests',action='store_true');a=p.parse_args() if a.action=='build-local':require(not(a.manifest or a.case or a.execute_native_tests),'local build only');build() elif a.action.startswith('readback-'):require(a.manifest and a.case and not a.execute_native_tests,'same case readback only');require((STATE/str(uuid.UUID(a.case))/'started-local.json').exists(),'never start from a readback command');execute(a.manifest,a.action.removeprefix('readback-'),a.case) else:require(a.manifest and a.execute_native_tests and not a.case,'explicit one native platform start');execute(a.manifest,a.action) if __name__=='__main__':main()