#!/usr/bin/env python3 """Fixed native shared-core unit qualification: Linux FIRST, then Windows. No RMM jobs, enrollment, runtime replacement, daemon changes or server contact. A lost execution reply permits readback only, never an automatic test rerun. """ import argparse import base64 import contextlib import hashlib import io import json import os from pathlib import Path import re import stat 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/shared-core/acceptance' PROFILES = { 'original': {'linux': {'contracts':5, 'reconcile':20}, 'windows': {'contracts':4, 'reconcile':20}}, 'helper-sync-v1': {'linux': {'contracts':5, 'reconcile':21, 'helper-reconcile':22, 'linux-helper':21}, 'windows': {'contracts':4, 'reconcile':21, 'helper-reconcile':22}}, } KINDS = ('contracts', 'reconcile') # Historical original qualification only. REMOTE_LINUX = '/var/lib/rmm-shared-core-acceptance' def sha(data): return hashlib.sha256(data).hexdigest() def write_new(path, value): path.parent.mkdir(mode=0o700, parents=True, exist_ok=True) contract.write_private(path, value) def source_paths(profile): contract.require(profile in PROFILES, 'fixed qualification profile') sources = [ROOT/'Cargo.toml', ROOT/'Cargo.lock', ROOT/'rust-toolchain.toml'] crates = ['agent-core', 'domain', 'protocol'] if profile == 'helper-sync-v1': crates.extend(('agent', 'agent-linux', 'privileged-helper')) for name in crates: sources.extend(p for p in (ROOT/'crates'/name).rglob('*') if p.is_file() and (p.suffix == '.rs' or p.name == 'Cargo.toml')) sources.extend((ROOT/'proto').glob('*.proto')) if profile == 'helper-sync-v1': sources.extend(ROOT/p for p in ('tools/apt-transaction/apt_transaction.py', 'tools/apt-transaction/hook', 'deploy/linux/rmm-apt-helper.service', 'tests/apt_transaction/fixtures/planning-canonical.json')) return sources def qualified(platform, profile='original'): folder = ROOT/'devstate'/('shared-core' if platform == 'linux' else 'windows-core') if profile != 'original': folder = folder/profile manifest = contract.protected_json(folder/'manifest.json') contract.require(manifest.get('qualification_profile', 'original') == profile, 'bound qualification profile') actual = {str(p.relative_to(ROOT)): sha(p.read_bytes()) for p in source_paths(profile)} contract.require(manifest['source_files'] == actual and manifest['source_before_after_equal'] is True, 'qualified source bindings changed') target = 'x86_64-unknown-linux-gnu' if platform == 'linux' else 'x86_64-pc-windows-gnu' contract.require(manifest['schema_version'] == 1 and manifest['target'] == target and manifest['os_effects'] is False and manifest['native_vm_executed'] is False, 'fixed test-only qualification') contract.require(all(manifest['checks'][n] is True for n in ('rustfmt_check', 'linux_all_target_clippy_warnings_denied', 'windows_all_target_clippy_warnings_denied')), 'qualified checks') artifacts = {} for kind, item in zip(PROFILES[profile][platform], manifest['artifacts'], strict=True): path = folder/(kind + ('.exe' if platform == 'windows' else '')) contract.require(item['path'] == str(path.relative_to(ROOT)) and not path.is_symlink() and path.is_file(), 'fixed artifact path') contract.require(stat.S_IMODE(path.stat().st_mode) == 0o600 and path.stat().st_uid == os.getuid(), 'private artifact owner/mode') contract.require(item['binary_sha256'] == sha(path.read_bytes()) and item['size_bytes'] == path.stat().st_size, 'artifact hash/size') contract.require(item['test_count'] == PROFILES[profile][platform][kind], 'fixed test count') artifacts[kind] = item return dict(manifest_sha256=sha((folder/'manifest.json').read_bytes()), artifacts=artifacts) # Functions sent by the existing pinned SSH/bootstrap RPC. Only fixed test files # are touched, never production journals, keys, packages, services or endpoints. def linux_case(payload): import stat root = Path('/var/lib/rmm-shared-core-acceptance') ident = str(uuid.UUID(payload['id'])) require(ident == payload['id'] and payload['action'] in ('prepare', 'run', 'read'), 'fixed case') entries = payload['artifacts'] profile=payload.get('profile','original') profiles={'original':{'contracts':5,'reconcile':20},'helper-sync-v1':{'contracts':5,'reconcile':21,'helper-reconcile':22,'linux-helper':21}} require(profile in profiles and set(entries)==set(profiles[profile]), 'fixed qualification artifacts') for kind, item in entries.items(): require(len(item['binary_sha256']) == 64 and all(c in '0123456789abcdef' for c in item['binary_sha256']), 'fixed hash') require(item['test_count'] == profiles[profile][kind], 'fixed test count') require(os.geteuid() == 0, 'bootstrap root only') def safe(path, directory): m = path.lstat() require(not stat.S_ISLNK(m.st_mode) and m.st_uid == 0 and not m.st_mode & 0o022 and (stat.S_ISDIR(m.st_mode) if directory else stat.S_ISREG(m.st_mode)), 'private fixed test path') def save(path, value): fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o600) with os.fdopen(fd, 'w') as out: json.dump(value, out); out.flush(); os.fsync(out.fileno()) if not root.exists(): root.mkdir(mode=0o700) safe(root, True) case = root/ident if payload['action'] == 'prepare': case.mkdir(mode=0o700) save(case/'intent.json', dict(id=ident, profile=profile, artifacts=entries, linux_manifest_sha256=payload['linux_manifest_sha256'], windows_manifest_sha256=payload['windows_manifest_sha256'], test_only=True)) for item in entries.values(): (case/item['binary_sha256']).mkdir(mode=0o700) return dict(prepared=True) safe(case, True); safe(case/'intent.json', False) intent = json.loads((case/'intent.json').read_bytes()) require(intent.get('profile','original') == profile and intent['artifacts'] == entries and intent['linux_manifest_sha256'] == payload['linux_manifest_sha256'] and intent['windows_manifest_sha256'] == payload['windows_manifest_sha256'], 'same native intent') kind = payload['kind']; require(kind in profiles[profile], 'fixed test kind') result_path = case/(kind+'-result.json') if payload['action'] == 'read': safe(result_path, False) return json.loads(result_path.read_bytes()) save(case/(kind+'-started.json'), dict(kind=kind, binary_sha256=entries[kind]['binary_sha256'])) path = case/entries[kind]['binary_sha256']/kind safe(path.parent, True); safe(path, False) require(hashlib.sha256(path.read_bytes()).hexdigest() == entries[kind]['binary_sha256'], 'pinned test hash') path.chmod(0o500) outputs = [] for label in ('stdout', 'stderr'): outputs.append(open(case/(kind+'-'+label+'.bin'), 'xb')) os.chmod(outputs[-1].name, 0o600) timed_out = False try: try: result = subprocess.run([str(path), '--test-threads=1'], stdin=subprocess.DEVNULL, stdout=outputs[0], stderr=outputs[1], timeout=25) code = result.returncode except subprocess.TimeoutExpired: timed_out = True; code = None finally: for out in outputs: out.flush(); os.fsync(out.fileno()); out.close() raw = [(case/(kind+'-'+label+'.bin')).read_bytes() for label in ('stdout', 'stderr')] require(all(len(b) <= 1024*1024 for b in raw), 'fixed unit output bound') report = dict(kind=kind, binary_sha256=entries[kind]['binary_sha256'], exit_code=code, timed_out=timed_out, stdout_b64=base64.b64encode(raw[0]).decode(), stderr_b64=base64.b64encode(raw[1]).decode(), native_platform='linux') save(result_path, report) return report WINDOWS = r''' $ErrorActionPreference='Stop' $p=([Console]::In.ReadLine() | ConvertFrom-Json) if (!$p.profile) {$p | Add-Member -NotePropertyName profile -NotePropertyValue 'original'} $kinds=@('contracts','reconcile');if ($p.profile -eq 'helper-sync-v1') {$kinds+=@('helper-reconcile')} if ($p.profile -notin @('original','helper-sync-v1')) {throw 'Fixed qualification profile'} if ([string]([guid]$p.id) -cne $p.id -or $p.kind -notin $kinds -or $p.action -notin @('run','read') -or $p.sha256 -notmatch '^[0-9a-f]{64}$' -or $p.linux_evidence_sha256 -notmatch '^[0-9a-f]{64}$') {throw 'Fixed native core case'} $root='C:\ProgramData\RustDiscoveryBootstrap' $case=Join-Path $root ('shared-core-'+$p.id) function Safe([string]$path,[bool]$directory) { $i=Get-Item -LiteralPath $path -Force if ($i.PSIsContainer -ne $directory -or ($i.Attributes -band [IO.FileAttributes]::ReparsePoint)) {throw 'Fixed test path type'} $a=Get-Acl -LiteralPath $path if ($a.GetOwner([Security.Principal.SecurityIdentifier]).Value -notin @('S-1-5-18','S-1-5-32-544')) {throw 'Private test owner'} $rules=@($a.GetAccessRules($true,$true,[Security.Principal.SecurityIdentifier])) if (!$rules.Count) {throw 'Empty test DACL'} foreach($r in $rules) {if ($r.AccessControlType -ne 'Allow' -or $r.IdentityReference.Value -notin @('S-1-5-18','S-1-5-32-544')) {throw 'Broad test DACL'}} } function Write-New([string]$path,$value) { $b=[Text.Encoding]::UTF8.GetBytes(($value | ConvertTo-Json -Compress -Depth 8)) $f=[IO.File]::Open($path,[IO.FileMode]::CreateNew,[IO.FileAccess]::Write,[IO.FileShare]::Read) try {$f.Write($b,0,$b.Length);$f.Flush($true)} finally {$f.Dispose()} } Safe $root $true if (!(Test-Path -LiteralPath $case)) { if ($p.action -ne 'run' -or $p.kind -ne 'contracts') {throw 'Native intent missing'} New-Item -ItemType Directory -Path $case | Out-Null Write-New (Join-Path $case 'intent.json') @{id=$p.id;profile=$p.profile;linux_evidence_sha256=$p.linux_evidence_sha256;test_only=$true} } Safe $case $true $intent=Get-Content -LiteralPath (Join-Path $case 'intent.json') -Raw | ConvertFrom-Json if ((!$intent.profile -and $p.profile -ne 'original') -or ($intent.profile -and $intent.profile -cne $p.profile) -or $intent.id -cne $p.id -or $intent.linux_evidence_sha256 -cne $p.linux_evidence_sha256) {throw 'Bound Linux-first evidence'} $result=Join-Path $case ($p.kind+'-result.json') if ($p.action -eq 'read') {Safe $result $false;Get-Content -LiteralPath $result -Raw;exit 0} Write-New (Join-Path $case ($p.kind+'-started.json')) @{kind=$p.kind;sha256=$p.sha256} $folder=Join-Path $root $p.sha256;Safe $folder $true $exe=Join-Path $folder ('windows-core-'+$p.kind+'.exe');Safe $exe $false if ((Get-FileHash -LiteralPath $exe -Algorithm SHA256).Hash.ToLowerInvariant() -cne $p.sha256) {throw 'Pinned unit hash'} $out=Join-Path $case ($p.kind+'-stdout.bin');$err=Join-Path $case ($p.kind+'-stderr.bin') if ((Test-Path -LiteralPath $out) -or (Test-Path -LiteralPath $err)) {throw 'Existing native output'} $proc=Start-Process -FilePath $exe -ArgumentList @('--test-threads=1') -PassThru -WindowStyle Hidden -RedirectStandardOutput $out -RedirectStandardError $err $handle=$proc.Handle;$timeout=$false if (!$proc.WaitForExit(25000)) {$timeout=$true;$proc.Kill();$proc.WaitForExit()} $proc.Refresh();$code=$proc.ExitCode if ((Get-Item -LiteralPath $out).Length -gt 1048576 -or (Get-Item -LiteralPath $err).Length -gt 1048576) {throw 'Fixed unit output bound'} $value=@{kind=$p.kind;binary_sha256=$p.sha256;exit_code=$code;timed_out=$timeout;stdout_b64=[Convert]::ToBase64String([IO.File]::ReadAllBytes($out));stderr_b64=[Convert]::ToBase64String([IO.File]::ReadAllBytes($err));native_platform='windows'} Write-New $result $value $value | ConvertTo-Json -Compress -Depth 8 ''' def upload_linux(case, kind, item): 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']] path = ROOT/item['path'] remote = REMOTE_LINUX+'/'+case.name+'/'+item['binary_sha256']+'/'+kind batch = 'put '+json.dumps(str(path))+' '+json.dumps(remote)+'\n' result = subprocess.run(args, input=batch.encode(), capture_output=True, timeout=120) write_new(case/('linux-'+kind+'-sftp.json'), dict(returncode=result.returncode, stdout_b64=base64.b64encode(result.stdout).decode(), stderr_b64=base64.b64encode(result.stderr).decode())) contract.require(result.returncode == 0, 'private Linux SFTP failure; inspect case') def payload(intent, action, kind=None): return dict(id=intent['id'], profile=intent.get('profile','original'), action=action, kind=kind, artifacts=intent['linux']['artifacts'], linux_manifest_sha256=intent['linux']['manifest_sha256'], windows_manifest_sha256=intent['windows']['manifest_sha256']) def verify_result(platform, kind, result, expected): contract.require(result['native_platform'] == platform and result['kind'] == kind and result['binary_sha256'] == expected['binary_sha256'] and result['exit_code'] == 0 and result['timed_out'] is False, 'native test exit/hash') out = base64.b64decode(result['stdout_b64'], validate=True) err = base64.b64decode(result['stderr_b64'], validate=True) contract.require(len(out) <= 1024*1024 and len(err) <= 1024*1024, 'private unit output limit') pattern = rb'test result: ok\. '+str(expected['test_count']).encode()+rb' passed; 0 failed; 0 ignored; 0 measured; 0 filtered out;' contract.require(re.search(pattern, out) is not None, 'exact native unit counts, no skipped cases') def main(): os.umask(0o077) parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('action', choices=('linux', 'windows', 'finish-linux', 'finish-windows')) parser.add_argument('--case-id') parser.add_argument('--profile', choices=tuple(PROFILES), default='original') args = parser.parse_args() if args.action == 'linux': contract.require(args.case_id is None, 'new Linux case chooses fresh UUID') ident = str(uuid.uuid4()); case = STATE/ident intent = dict(id=ident, linux=qualified('linux',args.profile), windows=qualified('windows',args.profile)) if args.profile != 'original': intent['profile']=args.profile write_new(case/'intent.json', intent) else: contract.require(args.case_id is not None and str(uuid.UUID(args.case_id)) == args.case_id, 'fixed existing case UUID') case = STATE/args.case_id intent = contract.protected_json(case/'intent.json') contract.require(intent['id'] == args.case_id and intent.get('profile','original')==args.profile and intent['linux'] == qualified('linux',args.profile) and intent['windows'] == qualified('windows',args.profile), 'same qualified case source/artifacts') platform = 'linux' if args.action.endswith('linux') else 'windows' if platform == 'windows': proof = contract.protected_json(case/'linux-passed.json') contract.require(proof['id'] == intent['id'] and proof['passed'] is True and proof['qualification'] == intent['linux'], 'Linux-first native qualification required') for kind in PROFILES[args.profile]['linux']: verify_result('linux', kind, proof['results'][kind], intent['linux']['artifacts'][kind]) linux_sha = sha((case/'linux-passed.json').read_bytes()) else: linux_sha = None finish = args.action.startswith('finish-') if not finish: write_new(case/(platform+'-started.json'), dict(id=intent['id'], profile=args.profile, linux_evidence_sha256=linux_sha)) if args.action == 'linux': linux.rpc('linux-vm.conf', linux_case, payload(intent, 'prepare')) for kind in PROFILES[args.profile]['linux']: upload_linux(case, kind, intent['linux']['artifacts'][kind]) results = {} for kind in PROFILES[args.profile][platform]: if platform == 'linux': result = linux.rpc('linux-vm.conf', linux_case, payload(intent, 'read' if finish else 'run', kind)) else: item = intent['windows']['artifacts'][kind] # Existing bootstrap prints only sanitized failures; keep even these # in the protected case so this CLI never prints remote diagnostics. captured = io.StringIO() try: with contextlib.redirect_stdout(captured): if not finish: windows.upload(ROOT/item['path'], 'windows-core-'+kind+'.exe') result = windows.powershell(WINDOWS, dict(id=intent['id'], profile=args.profile, kind=kind, action='read' if finish else 'run', sha256=item['binary_sha256'], linux_evidence_sha256=linux_sha), timeout=40) finally: write_new(case/('windows-'+kind+'-transport-'+uuid.uuid4().hex+'.json'), dict(sanitized_bootstrap_output=captured.getvalue())) write_new(case/(platform+'-'+kind+'-readback-'+uuid.uuid4().hex+'.json'), result) verify_result(platform, kind, result, intent[platform]['artifacts'][kind]); results[kind] = result proof = dict(id=intent['id'], passed=True, native_platform=platform, qualification=intent[platform], linux_evidence_sha256=linux_sha, results=results) destination = case/(platform+'-passed.json') if destination.exists(): contract.require(contract.protected_json(destination) == proof, 'identical original native proof') else: write_new(destination, proof) print(json.dumps(dict(result='PASS', native_platform=platform, case_id=intent['id'], evidence_sha256=sha(destination.read_bytes()), artifact_sha256={k:v['binary_sha256'] for k,v in intent[platform]['artifacts'].items()}))) if __name__ == '__main__': try: main() except Exception as error: print('SHARED_CORE_ACCEPTANCE_INCOMPLETE:'+type(error).__name__) raise SystemExit(2) from None