#!/usr/bin/env python3 """Separate source-bound Windows delegation build/native units; no deployment. Historical feature profiles stay unchanged. Unit execution is create-once; lost replies permit same-case readback only. No RMM requests or approvals. """ import argparse import base64 import hashlib import json import os from pathlib import Path import re import shutil import stat import subprocess import uuid import client_bootstrap as cb import runtime_feature_upgrade as feature import runtime_upgrade as previous import auto_transport_acceptance as auto require = cb.contract.require STATE = cb.EVIDENCE / 'system-delegation-acceptance' CORE = cb.ROOT / 'devstate/core-no-start/build-de37ee99-11bf-465b-9519-45ad8f6e5b87/manifest.json' CORE_SHA = '1bc082031fcad20d5a519b0004f63d499dbba2ea9ce25d56230d48d2556d474b' # Final source review must confirm these before a build. No minimum-count proof. COUNTS = {'lib': 23, 'bin': 7} NAMES = {'agent': 'rmm-agent-windows.exe', 'helper': 'rmm-helper-windows.exe', 'probe': 'RuntimeStateProbe.exe', 'system_probe': 'SystemStateProbe.exe', 'lib': 'windows-tests-lib.exe', 'bin': 'windows-tests-bin.exe'} TARGET = previous.TARGET def sha(path): return hashlib.sha256(Path(path).read_bytes()).hexdigest() def save(path, value): path.parent.mkdir(mode=0o700, parents=True, exist_ok=True) cb.contract.write_private(path, value) def sources(): result = feature.source_files() for path in (cb.ROOT / 'crates').glob('*/Cargo.toml'): result[str(path.relative_to(cb.ROOT))] = sha(path) for name in ('deploy/windows/system_delegation_acceptance.py', 'deploy/windows/client_bootstrap.py', 'deploy/windows/system_intent_bootstrap.py'): result[name] = sha(cb.ROOT / name) return dict(sorted(result.items())) def core_proof(): require(sha(CORE) == CORE_SHA, 'exact Linux-first core manifest') manifest = cb.contract.protected_json(CORE) result = {'manifest_sha256': CORE_SHA} for platform, count in (('linux', 80), ('windows', 58)): path = CORE.parent / (platform + '-passed.json') proof = cb.contract.protected_json(path) require(proof['verified'] is True and proof['platform'] == platform and proof['manifest_sha256'] == CORE_SHA, 'actual same-manifest native core proof') actual = proof['native']['reports'] require(set(actual) == set(manifest['test_counts'][platform]), 'all core artifacts') require(sum(len(v['names']) for v in actual.values()) == count, 'full native core lists') for kind, item in actual.items(): output = base64.b64decode(item['stdout_b64'], validate=True) n = manifest['test_counts'][platform][kind] require(item['exit_code'] == 0 and item['timed_out'] is False and item['names'] == manifest['test_names'][platform][kind] and item['sha256'] == manifest['artifacts'][platform][kind]['sha256'] and re.search(rb'test result: ok\. ' + str(n).encode() + rb' passed; 0 failed; 0 ignored; 0 measured; 0 filtered out;', output), 'actual complete native core results') result[platform] = {'proof_sha256': sha(path), 'case_id': proof['case_id']} for name, digest in manifest['source_files'].items(): if name.startswith(('crates/agent-core/', 'crates/domain/', 'crates/protocol/', 'proto/')): require(sha(cb.ROOT / name) == digest, 'native-qualified common source unchanged') return result def command(args, timeout=600): value = subprocess.run(args, capture_output=True, timeout=timeout) if value.returncode: folder = STATE / ('local-command-failure-' + str(uuid.uuid4())) folder.mkdir(mode=0o700, parents=True) save(folder / 'result.json', {'returncode': value.returncode, 'local_build_only': True}) for name, data in (('stdout.bin', value.stdout), ('stderr.bin', value.stderr)): descriptor = os.open(folder / name, os.O_CREAT | os.O_EXCL | os.O_WRONLY | os.O_NOFOLLOW, 0o600) with os.fdopen(descriptor, 'wb') as stream: stream.write(data) require(value.returncode == 0, 'local qualification command failed') return value.stdout def build(): os.umask(0o077) before = sources() common = core_proof() require(re.search(r'^pub const ADAPTER_QUALIFIED: bool = false;$', (cb.ROOT / 'crates/agent-windows/src/clock.rs').read_text(), re.M), 'production clock remains closed') folder = STATE / ('build-' + str(uuid.uuid4())) folder.mkdir(mode=0o700, parents=True) save(folder / 'source-before.json', before) target = STATE / 'target' target.mkdir(mode=0o700, exist_ok=True) image = command(['docker', 'image', 'inspect', '--format', '{{.Id}}', previous.IMAGE]).decode().strip() require(re.fullmatch(r'sha256:[a-f0-9]{64}', image), 'immutable build image') docker = ['docker', 'run', '--rm', '--network', 'none', '--read-only', '--tmpfs', '/tmp:rw,size=512m', '-v', str(cb.ROOT) + ':/src:ro', '-v', str(target) + ':/target', '-e', 'CARGO_TARGET_DIR=/target', '--entrypoint', 'cargo', image] options = ['--manifest-path', '/src/Cargo.toml', '--locked', '--offline', '--target', TARGET, '-p', 'rmm-agent-windows'] command(docker + ['clippy'] + options + ['--all-targets', '--', '-D', 'warnings']) command(docker + ['clippy'] + options + ['--features', 'system-helper', '--all-targets', '--', '-D', 'warnings']) command(docker + ['build'] + options + ['--features', 'system-helper', '--bins', '--example', 'runtime_state', '--example', 'system_state', '--release']) raw = command(docker + ['test'] + options + ['--features', 'system-helper', '--lib', '--bin', 'rmm-agent-windows', '--no-run', '--message-format=json']) units = {} for line in raw.splitlines(): try: item = json.loads(line) except ValueError: continue if item.get('reason') != 'compiler-artifact' or not item.get('profile', {}).get('test') or not item.get('executable'): continue if 'rmm-agent-windows' not in item['package_id']: continue require('system-helper' in item['features'], 'actual feature unit profile') kind = 'lib' if 'lib' in item['target']['kind'] else 'bin' require(kind not in units, 'exact distinct unit artifacts') units[kind] = target / Path(item['executable']).relative_to('/target') require(set(units) == set(COUNTS), 'both unit artifacts') selected = {'agent': (target / TARGET / 'release/rmm-agent-windows.exe', 'rmm-agent-windows.exe'), 'helper': (target / TARGET / 'release/rmm-helper-windows.exe', 'rmm-helper-windows.exe'), 'probe': (target / TARGET / 'release/examples/runtime_state.exe', 'RuntimeStateProbe.exe'), 'system_probe': (target / TARGET / 'release/examples/system_state.exe', 'SystemStateProbe.exe')} selected.update({kind: (path, 'windows-tests-' + kind + '.exe') for kind, path in units.items()}) artifacts = {} for kind, (original, name) in selected.items(): path = folder / name shutil.copyfile(original, path) path.chmod(0o600) linked_sha = sha(original) retained = folder / (name + '.linked') shutil.copyfile(original, retained) retained.chmod(0o600) if kind in COUNTS: command(['docker', 'run', '--rm', '--network', 'none', '--read-only', '-v', str(folder.resolve()) + ':/artifacts', '--entrypoint', 'x86_64-w64-mingw32-strip', image, '--strip-debug', '/artifacts/' + name]) require(sha(original) == sha(retained) == linked_sha, 'original link preserved') data = path.read_bytes() 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 AMD64 PE') artifacts[kind] = {'path': str(path.resolve()), 'name': name, 'sha256': sha(path), 'bytes': len(data), 'original_link_path': str(retained.resolve()), 'original_link_sha256': linked_sha} require(sources() == before and core_proof() == common, 'sources and common proofs unchanged throughout build') manifest = {'schema_version': 1, 'type': 'rmm.windows.system-delegation-build', 'source_files': before, 'source_before_after_equal': True, 'artifacts': artifacts, 'native_counts_expected': COUNTS, 'core_native_proofs': common, 'build_image_id': image, 'default_and_feature_alltarget_clippy': True, 'production_clock_qualified': False, 'native_windows_executed': False} save(folder / 'manifest.json', manifest) print(json.dumps({'manifest': str((folder / 'manifest.json').resolve()), 'native_executed': False})) SCRIPT = 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.manifest_sha256-notmatch'^[a-f0-9]{64}$'){throw 'Fixed native delegation unit case'} $root='C:\ProgramData\RustDiscoveryBootstrap';$case=Join-Path $root ('delegation-units-'+$p.id) function Safe($path,$dir){$i=Get-Item -LiteralPath $path -Force;if($i.PSIsContainer-ne$dir-or($i.Attributes-band[IO.FileAttributes]::ReparsePoint)){throw 'Private unit type'};$a=Get-Acl -LiteralPath $path;if($a.GetOwner([Security.Principal.SecurityIdentifier]).Value-notin@('S-1-5-18','S-1-5-32-544')){throw 'Unit owner'};$rules=@($a.GetAccessRules($true,$true,[Security.Principal.SecurityIdentifier]));if(!$rules.Count){throw 'Empty ACL'};foreach($r in $rules){if($r.AccessControlType-ne'Allow'-or$r.IdentityReference.Value-notin@('S-1-5-18','S-1-5-32-544')){throw 'Unit ACL'}}} function WriteNew($path,$value){$b=[Text.Encoding]::UTF8.GetBytes(($value|ConvertTo-Json -Depth 12 -Compress));$f=[IO.File]::Open($path,'CreateNew','Write','Read');try{$f.Write($b,0,$b.Length);$f.Flush($true)}finally{$f.Dispose()}} Safe $root $true if($p.action-eq'read'){Safe $case $true;Safe (Join-Path $case 'intent.json') $false;$i=Get-Content -LiteralPath (Join-Path $case 'intent.json') -Raw|ConvertFrom-Json;if($i.manifest_sha256-cne$p.manifest_sha256){throw 'Original native intent'};Safe (Join-Path $case 'result.json') $false;Get-Content -LiteralPath (Join-Path $case 'result.json') -Raw;exit 0} if(Test-Path -LiteralPath $case){throw 'Existing unit case: readback only'};New-Item -ItemType Directory -Path $case|Out-Null;Safe $case $true;WriteNew (Join-Path $case 'intent.json') $p function Run($exe,$arguments){$s=New-Object Diagnostics.ProcessStartInfo;$s.FileName=$exe;$s.Arguments=$arguments;$s.UseShellExecute=$false;$s.CreateNoWindow=$true;$s.RedirectStandardOutput=$true;$s.RedirectStandardError=$true;$c=New-Object Diagnostics.Process;$c.StartInfo=$s;if(!$c.Start()){throw 'Unit process start'};try{$o=$c.StandardOutput.ReadToEndAsync();$e=$c.StandardError.ReadToEndAsync();$timed=$false;if(!$c.WaitForExit(30000)){$timed=$true;$c.Kill();$c.WaitForExit()};if($o.Result.Length-gt65536-or$e.Result.Length-gt65536){throw 'Unit output bound'};return @{exit_code=$c.ExitCode;timed_out=$timed;stdout=$o.Result;stderr=$e.Result}}finally{$c.Dispose()}} WriteNew (Join-Path $case 'started.json') @{start_once=$true};$reports=@{} foreach($kind in @('lib','bin')){$a=$p.artifacts.$kind;if($a.sha256-notmatch'^[a-f0-9]{64}$'-or$a.name-cne('windows-tests-'+$kind+'.exe')){throw 'Fixed unit artifact'};$folder=Join-Path $root $a.sha256;Safe $folder $true;$exe=Join-Path $folder $a.name;Safe $exe $false;if((Get-FileHash -LiteralPath $exe -Algorithm SHA256).Hash.ToLowerInvariant()-cne$a.sha256){throw 'Exact native unit hash'};$listed=Run $exe '--list --format terse';$names=@($listed.stdout-split'\r?\n'|Where-Object{$_-match': test$'}|ForEach-Object{$_-replace': test$',''}|Sort-Object);if($listed.exit_code-ne0-or$listed.timed_out-or$names.Count-ne$p.counts.$kind){throw 'Exact native count before tests'};$result=Run $exe '--test-threads=1';$v=@{sha256=$a.sha256;names=$names;listed=$listed;result=$result};WriteNew (Join-Path $case ($kind+'-result.json')) $v;$reports[$kind]=$v} $value=@{manifest_sha256=$p.manifest_sha256;reports=$reports;bootstrap_administrator_units=$true;local_service_proof=$false;rmm_jobs=$false;product_state_writes=$false};WriteNew (Join-Path $case 'result.json') $value;$value|ConvertTo-Json -Depth 12 -Compress ''' def verified(path, current=True): value = cb.contract.protected_json(path) require(value['type'] == 'rmm.windows.system-delegation-build' and value['schema_version'] == 1 and value['source_before_after_equal'] is True and value['production_clock_qualified'] is False and value['default_and_feature_alltarget_clippy'] is True and value['native_counts_expected'] == COUNTS, 'exact delegation build profile') if current: require(value['source_files'] == sources() and value['core_native_proofs'] == core_proof(), 'source/native core binding') require(set(value['artifacts']) == set(NAMES), 'all deployable/test artifacts') for kind, item in value['artifacts'].items(): binary, linked = Path(item['path']), Path(item['original_link_path']) require(item['name'] == NAMES[kind] and binary == path.resolve().parent / NAMES[kind] and linked == path.resolve().parent / (NAMES[kind] + '.linked'), 'fixed artifact paths and names') for file in (binary, linked): info = file.lstat() require(not file.is_symlink() and stat.S_ISREG(info.st_mode) and info.st_uid == os.getuid() and stat.S_IMODE(info.st_mode) == 0o600, 'private regular retained artifact') require(binary.resolve().parent == path.resolve().parent and linked.resolve().parent == path.resolve().parent and sha(item['path']) == item['sha256'] and sha(item['original_link_path']) == item['original_link_sha256'], 'retained exact artifact/link') return value def native(path, ident=None): manifest = verified(path, current=ident is None) if ident is None: ident = str(uuid.uuid4()) folder = STATE / ident folder.mkdir(mode=0o700) save(folder / 'intent.json', {'id': ident, 'manifest_path': str(path.resolve()), 'manifest_sha256': sha(path), 'runner_sha256': sha(__file__), 'script_sha256': hashlib.sha256(SCRIPT.encode()).hexdigest()}) save(folder / 'server-before.json', {'counts': auto.scopes(), 'flags': auto.status()}) for kind in COUNTS: previous.upload(manifest['artifacts'][kind]) save(folder / 'started-local.json', {'start_once': True}) action = 'run' else: ident = str(uuid.UUID(ident)) folder = STATE / ident intent = cb.contract.protected_json(folder / 'intent.json') require(intent['manifest_sha256'] == sha(path) and intent['manifest_path'] == str(path.resolve()) and intent['runner_sha256'] == sha(__file__) and intent['script_sha256'] == hashlib.sha256(SCRIPT.encode()).hexdigest() and (folder / 'started-local.json').exists(), 'same exact started case; readback only') action = 'read' result = cb.powershell(SCRIPT, {'id': ident, 'action': action, 'manifest_sha256': sha(path), 'artifacts': {k: manifest['artifacts'][k] for k in COUNTS}, 'counts': COUNTS}, timeout=100) save(folder / ('readback-' + str(uuid.uuid4()) + '.json'), result) require(result['manifest_sha256'] == sha(path) and set(result['reports']) == set(COUNTS), 'exact native manifest/reports') for kind, actual in result['reports'].items(): run = actual['result'] listed = actual['listed'] require(actual['sha256'] == manifest['artifacts'][kind]['sha256'] and len(actual['names']) == COUNTS[kind] and listed['exit_code'] == run['exit_code'] == 0 and not listed['timed_out'] and not run['timed_out'] and len(set(actual['names'])) == COUNTS[kind] and re.search(r'test result: ok\. ' + str(COUNTS[kind]) + r' passed; 0 failed; 0 ignored; 0 measured; 0 filtered out;', run['stdout']), 'every native unit passes, none skipped/filtered') before = cb.contract.protected_json(folder / 'server-before.json') require(auto.scopes() == before['counts'] and auto.status() == before['flags'] and auto.flags_disabled(before['flags']), 'both target counts and closed policies unchanged') proof = {'verified': True, 'case_id': ident, 'manifest_path': str(path.resolve()), 'manifest_sha256': sha(path), 'native': result, 'no_jobs': True, 'clock_adapter_qualified': False, 'local_service_proof': False} destination = folder / 'passed.json' if destination.exists(): require(cb.contract.protected_json(destination) == proof, 'retain exact original native proof') else: save(destination, proof) print(json.dumps({'verified': True, 'case_id': ident, 'tests': COUNTS, 'proof': str(destination.resolve())})) def main(): os.umask(0o077) parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('phase', choices=('build-local', 'native-units', 'readback')) parser.add_argument('--manifest', type=Path) parser.add_argument('--case') args = parser.parse_args() if args.phase == 'build-local': require(not (args.manifest or args.case), 'local build only') build() else: require(args.manifest and ((args.phase == 'readback') == bool(args.case)), 'explicit manifest and original readback case') native(args.manifest, args.case) if __name__ == '__main__': try: main() except Exception as error: print('WINDOWS_DELEGATION_QUALIFICATION_INCOMPLETE:' + type(error).__name__) raise SystemExit(2) from None