Files
2026-11-14 02:39:39 +01:00

494 lines
36 KiB
Python

#!/usr/bin/env python3
"""Qualified passive-only Windows runtime replacement, never a Windows RMM job.
Native test qualification and binary replacement are separate explicit phases.
Only the parent/orchestrator executes native phases after source review.
"""
import argparse
import hashlib
import json
import os
from pathlib import Path
import re
import shutil
import subprocess
import time
import uuid
import client_bootstrap as cb
import activate_auto_transport as activation
import auto_transport_acceptance as auto
base=auto.base
require=cb.contract.require
STATE=cb.EVIDENCE/'runtime-upgrade'
TARGET='x86_64-pc-windows-gnu'
IMAGE='rust-discovery-windows-clock-full:local'
ACTIVATION='422562e8-18eb-4865-977d-ab2af65f510b'
TESTS=r'''
$ErrorActionPreference='Stop'
$p=([Console]::In.ReadLine() | ConvertFrom-Json)
if ($p.name -notin @('windows-tests-lib.exe','windows-tests-bin.exe') -or $p.sha256 -notmatch '^[0-9a-f]{64}$') {throw 'Fixed native test artifact'}
$binary='C:\ProgramData\RustDiscoveryBootstrap\'+$p.sha256+'\'+$p.name
if ((Get-FileHash -LiteralPath $binary -Algorithm SHA256).Hash.ToLowerInvariant() -cne $p.sha256) {throw 'Exact native test artifact hash'}
function Run-Own([string]$arguments) {
$si=New-Object Diagnostics.ProcessStartInfo;$si.FileName=$binary;$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 'Native selftest start'}
try {
$stdout=$proc.StandardOutput.ReadToEndAsync();$stderr=$proc.StandardError.ReadToEndAsync()
if (!$proc.WaitForExit(120000)) {$proc.Kill();throw 'Native selftest timeout'}
$out=$stdout.Result;$err=$stderr.Result
if ($out.Length -gt 32768 -or $err.Length -gt 32768) {throw 'Bounded native selftest output'}
return [pscustomobject]@{exit_code=$proc.ExitCode;stdout=$out;stderr=$err}
} finally {$proc.Dispose()}
}
$list=Run-Own '--list --format terse'
if ($list.exit_code -ne 0) {throw 'Native selftest listing failed'}
$result=Run-Own '--test-threads=1'
[pscustomobject]@{listed=$list;result=$result;native_windows=$true;execution_context='bootstrap_administrator_unit_tests';rmm_jobs_created=$false;clock_service_changed=$false} | ConvertTo-Json -Depth 6 -Compress
'''
UPGRADE=r'''
$ErrorActionPreference='Stop'
$p=$global:RmmRuntimePayload
if ($null -eq $p -or [string]([guid]$p.id) -cne $p.id -or $p.action -notin @('state','enter','finish','rollback')) {throw 'Fixed passive runtime intent'}
foreach($n in @('old_sha256','new_sha256','probe_sha256','config_sha256')) {if ($p.$n -notmatch '^[0-9a-f]{64}$') {throw 'Qualified hash binding'}}
if ($p.old_sha256 -cne '31e360e50b177b4d5729d24e436d4f25aeed7b1a7bd35f2a129841ba1abffd37' -or $p.new_sha256 -ceq $p.old_sha256) {throw 'Known old passive runtime'}
$name='RmmDiscoveryWindows';$root='C:\ProgramData\RustDiscovery';$state=Join-Path $root 'state';$config=Join-Path $root 'config.json'
$binary='C:\Program Files\RustDiscovery\rmm-agent-windows.exe'
$folder='C:\ProgramData\RustDiscoveryBootstrap\runtime-'+$p.id
$active='C:\ProgramData\RustDiscoveryBootstrap\runtime-active-intent.json'
$uploaded='C:\ProgramData\RustDiscoveryBootstrap\'+$p.new_sha256+'\rmm-agent-windows.exe'
$probe='C:\ProgramData\RustDiscoveryBootstrap\'+$p.probe_sha256+'\RuntimeStateProbe.exe'
$sid=(New-Object Security.Principal.NTAccount('NT SERVICE',$name)).Translate([Security.Principal.SecurityIdentifier]).Value
$allowed=@('S-1-5-18','S-1-5-32-544',$sid)
function Check([string]$path,[bool]$directory) {
$item=Get-Item -LiteralPath $path -Force
if ($item.PSIsContainer -ne $directory -or ($item.Attributes -band [IO.FileAttributes]::ReparsePoint)) {throw 'Fixed regular path'}
$acl=Get-Acl -LiteralPath $path;$owner=$acl.GetOwner([Security.Principal.SecurityIdentifier]).Value
if ($owner -notin $allowed -and ($directory -or $owner -cne 'S-1-5-19')) {throw 'Protected owner'}
$rules=@($acl.GetAccessRules($true,$true,[Security.Principal.SecurityIdentifier]));if (!$rules.Count) {throw 'Empty DACL'}
foreach($r in $rules) {if ($r.AccessControlType -ne 'Allow' -or $r.IdentityReference.Value -notin $allowed) {throw 'Unexpected DACL'}}
}
function Hash-Bytes([byte[]]$bytes) {
$hash=[Security.Cryptography.SHA256]::Create()
try {return ([BitConverter]::ToString($hash.ComputeHash($bytes))).Replace('-','').ToLowerInvariant()} finally {$hash.Dispose()}
}
function Hash([string]$path) {Check $path $false;return (Get-FileHash -LiteralPath $path -Algorithm SHA256).Hash.ToLowerInvariant()}
function Acl-Hash([string]$path) {return Hash-Bytes ([Text.Encoding]::UTF8.GetBytes((Get-Acl -LiteralPath $path).Sddl))}
function Write-New([string]$path,$value) {
$bytes=[Text.Encoding]::UTF8.GetBytes(($value | ConvertTo-Json -Depth 24 -Compress))
$file=[IO.File]::Open($path,[IO.FileMode]::CreateNew,[IO.FileAccess]::Write,[IO.FileShare]::Read)
try {$file.Write($bytes,0,$bytes.Length);$file.Flush($true)} finally {$file.Dispose()}
}
function Probe {
if ((Hash $probe) -cne $p.probe_sha256) {throw 'Exact readonly witness artifact'}
$si=New-Object Diagnostics.ProcessStartInfo;$si.FileName=$probe;$si.UseShellExecute=$false;$si.CreateNoWindow=$true;$si.RedirectStandardOutput=$true;$si.RedirectStandardError=$true
$proc=New-Object Diagnostics.Process;$proc.StartInfo=$si
if (!$proc.Start()) {throw 'Readonly witness start'}
try {
$stdout=$proc.StandardOutput.ReadToEndAsync();$stderr=$proc.StandardError.ReadToEndAsync()
if (!$proc.WaitForExit(20000)) {$proc.Kill();throw 'Readonly witness timeout'}
if ($proc.ExitCode -ne 0 -or $stdout.Result.Length -gt 32768 -or $stderr.Result.Length) {throw 'Readonly witness refusal'}
return ($stdout.Result | ConvertFrom-Json)
} finally {$proc.Dispose()}
}
function Snapshot([string]$expected) {
foreach($dir in @('C:\Program Files\RustDiscovery',$root,$state)) {Check $dir $true}
Check $config $false
if ((Get-Item -LiteralPath $config).Length -gt 32768 -or (Hash $config) -cne $p.config_sha256) {throw 'Exact immutable auto configuration'}
$cfg=Get-Content -LiteralPath $config -Raw | ConvertFrom-Json
if ((($cfg.PSObject.Properties.Name|Sort-Object)-join ',') -cne 'ca_file,connect_address,enrollment_token_file,server_name,state_directory,transport' -or $cfg.transport -cne 'auto' -or $cfg.server_name -cne 'discovery.ease-technologies.de' -or $cfg.state_directory -cne $state -or $cfg.ca_file -cne (Join-Path $root 'server-ca.pem') -or $cfg.enrollment_token_file -cne (Join-Path $state 'enrollment.token')) {throw 'Original six-field passive configuration'}
if ((Test-Path -LiteralPath $cfg.enrollment_token_file) -or (Test-Path -LiteralPath (Join-Path $root 'binding.json')) -or (Test-Path -LiteralPath (Join-Path $state 'consumption.sqlite')) -or (Test-Path -LiteralPath (Join-Path $state 'jobs'))) {throw 'Passive only: no token binding ledger or jobs directory'}
$svc=Get-CimInstance Win32_Service -Filter "Name='RmmDiscoveryWindows'" -OperationTimeoutSec 10
$command='"C:\Program Files\RustDiscovery\rmm-agent-windows.exe" --service --config "C:\ProgramData\RustDiscovery\config.json"'
if ($svc.PathName -cne $command -or $svc.StartName -cne 'NT AUTHORITY\LocalService' -or $svc.StartMode -cne 'Auto') {throw 'Unchanged LocalService deployment'}
$owner=$null
if ($svc.State -eq 'Running') {
$process=Get-CimInstance Win32_Process -Filter ('ProcessId='+$svc.ProcessId) -OperationTimeoutSec 10
$owner=(Invoke-CimMethod -InputObject $process -MethodName GetOwnerSid -OperationTimeoutSec 10).Sid
if ($owner -cne 'S-1-5-19') {throw 'Actual LocalService identity'}
}
$identity=Get-Content -LiteralPath (Join-Path $state 'identity.json') -Raw | ConvertFrom-Json
if ($identity.agent_id -cne '63ea15b2-0643-403d-b5b4-cba1288dbea3' -or $identity.asset_id -cne 'a176520e-8a91-4e93-a78b-8e7620f430e0') {throw 'Original registered identity'}
if ((Hash $binary) -cne $expected) {throw 'Pinned actual binary'}
$public=[ordered]@{};$acls=[ordered]@{}
foreach($pair in @(@('ca',$cfg.ca_file),@('identity',(Join-Path $state 'identity.json')),@('certificate',(Join-Path $state 'device.pem')))) {$public[$pair[0]]=Hash $pair[1];$acls[$pair[0]]=Acl-Hash $pair[1]}
$key=Join-Path $state 'device.key';Check $key $false;$item=Get-Item -LiteralPath $key
$acls.private_key=Acl-Hash $key
return [pscustomobject]@{agent_id=$identity.agent_id;asset_id=$identity.asset_id;binary_sha256=$expected;binary_acl_sha256=(Acl-Hash $binary);config_sha256=(Hash $config);config_acl_sha256=(Acl-Hash $config);public_hashes=$public;identity_acls=$acls;private_key_metadata=[ordered]@{length=$item.Length;creation_ticks=$item.CreationTimeUtc.Ticks;last_write_ticks=$item.LastWriteTimeUtc.Ticks};private_key_read=$false;service=[ordered]@{state=$svc.State;start_name=$svc.StartName;start_mode=$svc.StartMode;path=$svc.PathName;process_id=$svc.ProcessId;owner_sid=$owner};journal=(Probe)}
}
function Preserved($before,$after,[bool]$frozen) {
foreach($field in @('agent_id','asset_id','config_sha256','config_acl_sha256','binary_acl_sha256')) {if ($before.$field -cne $after.$field) {throw 'Preserved deployment metadata'}}
foreach($field in @('public_hashes','identity_acls','private_key_metadata')) {if (($before.$field|ConvertTo-Json -Compress) -cne ($after.$field|ConvertTo-Json -Compress)) {throw 'Preserved identity metadata'}}
foreach($field in @('database_file_id','database_creation')) {if (($before.journal.$field|ConvertTo-Json -Compress) -cne ($after.journal.$field|ConvertTo-Json -Compress)) {throw 'Original database file identity'}}
if ($after.journal.integrity -cne 'ok' -or $after.journal.sequence -lt $before.journal.sequence -or $after.journal.identity_sha256 -cne $before.journal.identity_sha256 -or $after.journal.consumption_exists -or $after.journal.jobs_directory_exists) {throw 'Preserved passive journal'}
if ($frozen) {foreach($field in @('metadata_sha256','events_sha256')) {if ($before.journal.$field -cne $after.journal.$field) {throw 'Stopped original tables changed'}}}
foreach($row in $before.journal.schema) {
$found=$false
foreach($candidate in $after.journal.schema) {
if ($candidate[0] -ceq $row[0] -and $candidate[1] -ceq $row[1]) {
if ($found -or $candidate[2] -cne $row[2]) {throw 'Original schema changed'}
$found=$true
}
}
if (!$found) {throw 'Original schema missing'}
}
foreach($field in $after.journal.core_counts.PSObject.Properties) {if ($field.Value -ne 0) {throw 'Unexpected job or Sync rows'}}
}
function New-Intent {
if ((Test-Path -LiteralPath $folder) -or (Test-Path -LiteralPath $active)) {throw 'Existing native runtime intent: read finish, never repeat enter'}
New-Item -ItemType Directory -Path $folder | Out-Null
$acl=New-Object Security.AccessControl.DirectorySecurity;$acl.SetAccessRuleProtection($true,$false);$acl.SetOwner((New-Object Security.Principal.SecurityIdentifier('S-1-5-32-544')))
foreach($identity in @('S-1-5-18','S-1-5-32-544')) {$acl.AddAccessRule((New-Object Security.AccessControl.FileSystemAccessRule((New-Object Security.Principal.SecurityIdentifier($identity)),'FullControl','ContainerInherit,ObjectInherit','None','Allow')))}
Set-Acl -LiteralPath $folder -AclObject $acl
}
function Read-Intent {
Check $folder $true
$intent=Get-Content -LiteralPath (Join-Path $folder 'intent.json') -Raw | ConvertFrom-Json
foreach($field in @('id','old_sha256','new_sha256','probe_sha256','config_sha256')) {if ($intent.payload.$field -cne $p.$field) {throw 'Exact existing native intent'}}
return $intent
}
function Stop-Own {Stop-Service -Name $name;(Get-Service -Name $name).WaitForStatus([ServiceProcess.ServiceControllerStatus]::Stopped,[TimeSpan]::FromSeconds(20))}
function Start-Own {Start-Service -Name $name;(Get-Service -Name $name).WaitForStatus([ServiceProcess.ServiceControllerStatus]::Running,[TimeSpan]::FromSeconds(20))}
if ($p.action -eq 'state') {Snapshot $p.old_sha256|ConvertTo-Json -Depth 24 -Compress;exit 0}
if ($p.action -eq 'finish') {
$intent=Read-Intent
$restored=Test-Path -LiteralPath (Join-Path $folder 'rollback-intent.json')
$expected=$p.new_sha256;if ($restored) {$expected=$p.old_sha256}
$after=Snapshot $expected;Preserved $intent.before $after $false
if ($after.service.state -cne 'Running') {throw 'Selected runtime not running; no repeated start'}
[pscustomobject]@{read_only=$true;completed=$true;restored=$restored;id=$p.id;native=$after;original=$intent.before;stop_receipt=(Test-Path -LiteralPath (Join-Path $folder 'stopped.json'));entered_receipt=(Test-Path -LiteralPath (Join-Path $folder 'entered.json'));no_jobs=$true;no_clock_change=$true;no_reboot=$true} | ConvertTo-Json -Depth 24 -Compress;exit 0
}
try {
if ($p.action -eq 'enter') {
$before=Snapshot $p.old_sha256
if ($before.service.state -cne 'Running' -or $before.journal.pending_inventory_events -ne 0) {throw 'Settled original runtime required'}
if ((Hash $uploaded) -cne $p.new_sha256) {throw 'New qualified binary upload'}
New-Intent
$intent=[pscustomobject]@{payload=$p;before=$before;binary_sddl=(Get-Acl -LiteralPath $binary).Sddl}
Write-New $active @{id=$p.id;old_sha256=$p.old_sha256;new_sha256=$p.new_sha256;no_jobs=$true}
Write-New (Join-Path $folder 'intent.json') $intent
Write-New (Join-Path $folder 'stop-attempt.json') @{stop_once=$true;no_jobs=$true}
Stop-Own
$stopped=Snapshot $p.old_sha256;Preserved $before $stopped $false
if ($stopped.journal.pending_inventory_events -ne 0) {throw 'Unacknowledged legacy inventory: preserve and inspect'}
Write-New (Join-Path $folder 'stopped.json') $stopped
$candidate=Join-Path $folder 'candidate.exe';[IO.File]::Copy($uploaded,$candidate,$false)
if ((Hash $candidate) -cne $p.new_sha256) {throw 'Exact staged candidate'}
[IO.File]::Replace($candidate,$binary,(Join-Path $folder 'original-binary.exe'))
$acl=New-Object Security.AccessControl.FileSecurity;$acl.SetSecurityDescriptorSddlForm($intent.binary_sddl);Set-Acl -LiteralPath $binary -AclObject $acl
if ((Hash (Join-Path $folder 'original-binary.exe')) -cne $p.old_sha256) {throw 'Versioned original binary retained'}
$swapped=Snapshot $p.new_sha256;Preserved $stopped $swapped $true
Write-New (Join-Path $folder 'swapped.json') $swapped
Write-New (Join-Path $folder 'start-attempt.json') @{start_once=$true;no_jobs=$true}
Start-Own
$after=Snapshot $p.new_sha256;Preserved $before $after $false
if ($after.service.state -cne 'Running') {throw 'New runtime stopped after start; no automatic retry'}
Write-New (Join-Path $folder 'entered.json') $after
[pscustomobject]@{entered=$true;id=$p.id;native=$after;no_jobs=$true;no_clock_change=$true;no_reboot=$true}|ConvertTo-Json -Depth 24 -Compress
} else {
$intent=Read-Intent
if (Test-Path -LiteralPath (Join-Path $folder 'rollback-intent.json')) {throw 'Rollback already requested; readback only, no repeated start'}
$actual=Hash $binary
if ($actual -cne $p.old_sha256 -and $actual -cne $p.new_sha256) {throw 'Unknown runtime; refuse rollback overwrite'}
$current=Snapshot $actual;Preserved $intent.before $current $false
Write-New (Join-Path $folder 'rollback-intent.json') @{id=$p.id;actual_sha256=$actual;restore_sha256=$p.old_sha256;no_jobs=$true}
if ($current.service.state -eq 'Running') {Stop-Own} elseif ($current.service.state -ne 'Stopped') {throw 'Unexpected service transition'}
if ($actual -cne $p.old_sha256) {
$backup=Join-Path $folder 'original-binary.exe';if ((Hash $backup) -cne $p.old_sha256) {throw 'Exact original backup'}
$candidate=Join-Path $folder 'rollback-candidate.exe';[IO.File]::Copy($backup,$candidate,$false)
[IO.File]::Replace($candidate,$binary,(Join-Path $folder 'rollback-new-binary.exe'))
$acl=New-Object Security.AccessControl.FileSecurity;$acl.SetSecurityDescriptorSddlForm($intent.binary_sddl);Set-Acl -LiteralPath $binary -AclObject $acl
}
Write-New (Join-Path $folder 'rollback-start-attempt.json') @{start_once=$true;no_jobs=$true}
Start-Own
$after=Snapshot $p.old_sha256;Preserved $intent.before $after $false
Write-New (Join-Path $folder 'rollback-result.json') $after
[pscustomobject]@{restored=$true;id=$p.id;native=$after;no_jobs=$true;no_reboot=$true}|ConvertTo-Json -Depth 24 -Compress
}
} catch {
if (Test-Path -LiteralPath $folder) {Write-New (Join-Path $folder ('error-'+[guid]::NewGuid().ToString()+'.json')) @{incomplete=$true;error_type=$_.Exception.GetType().FullName;line=$_.InvocationInfo.ScriptLineNumber;no_retry_start=$true}}
throw
}
'''
def digest(path):return hashlib.sha256(Path(path).read_bytes()).hexdigest()
def write(folder,name,value):
descriptor=os.open(folder/name,os.O_CREAT|os.O_EXCL|os.O_WRONLY|os.O_NOFOLLOW,0o600)
with os.fdopen(descriptor,'w') as stream:json.dump(value,stream,indent=2);stream.write('\n');stream.flush();os.fsync(stream.fileno())
def source_files():
names=['Cargo.toml','Cargo.lock','rust-toolchain.toml','crates/agent-windows/Cargo.toml','crates/agent-windows/build.rs']
for name in ['main','lib','runtime','native','inventory','journal','service','process','actions','clock']:
names.append('crates/agent-windows/src/'+name+'.rs')
names+=['crates/agent-windows/examples/runtime_state.rs']
names+=['crates/agent/src/'+name+'.rs' for name in ['https_channel','transport','wire_channel']]
for crate in ['agent-core','domain','protocol']:
names.append('crates/'+crate+'/Cargo.toml')
if crate=='agent-core':names.extend('crates/agent-core/src/'+name+'.rs' for name in ['lib','auth','crypto','jobs','ledger','reconcile'])
else:names.extend(str(path.relative_to(cb.ROOT)) for path in sorted((cb.ROOT/'crates'/crate/'src').glob('*.rs')))
names.extend(str(path.relative_to(cb.ROOT)) for path in sorted((cb.ROOT/'crates/agent-windows/native').glob('*')) if path.is_file())
names.append('proto/agent.proto')
require(all((cb.ROOT/name).is_file() for name in names),'all default-profile build sources must exist/freeze first')
return {name:digest(cb.ROOT/name) for name in sorted(set(names))}
def stable(manifest):
require(manifest['architecture']==TARGET and manifest['default_features'] is True
and manifest['system_helper_feature'] is False,'qualified default unprivileged Windows profile')
require(all((cb.ROOT/name).is_file() and digest(cb.ROOT/name)==sha for name,sha in manifest['source_files'].items()),'qualified source drift')
for item in manifest['artifacts'].values():
path=Path(item['path']);require(path.is_file() and digest(path)==item['sha256'],'qualified artifact changed')
def command(arguments,timeout=300):
result=subprocess.run(arguments,capture_output=True,timeout=timeout)
if result.returncode:raise RuntimeError('LOCAL_BUILD_FAILED_'+str(result.returncode))
return result.stdout
def build():
"""Local cross-build only; source must already have been frozen by owners."""
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();write(folder,'source-before.json',before)
target=cb.ROOT/'devstate/windows-runtime/target';target.mkdir(mode=0o700,parents=True,exist_ok=True)
script='cargo clippy --locked --offline --target '+TARGET+' -p rmm-agent-windows --all-targets -- -D warnings && cargo build --locked --offline --target '+TARGET+' -p rmm-agent-windows --bin rmm-agent-windows --example runtime_state --release && cargo test --locked --offline --target '+TARGET+' -p rmm-agent-windows --lib --bin rmm-agent-windows --no-run --message-format=json'
raw=command(['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','/bin/sh',IMAGE,'-c',script])
require(source_files()==before,'source changed during local build; do not qualify')
units={}
for line in raw.decode().splitlines():
try:item=json.loads(line)
except ValueError:continue
if item.get('reason')=='compiler-artifact' and 'rmm-agent-windows' in item.get('package_id',''):
require('system-helper' not in item.get('features',[]),'system helper excluded from passive qualification')
if item.get('reason')=='compiler-artifact' and item.get('profile',{}).get('test') and item.get('executable'):
kind=item['target']['kind'];key='lib' if 'lib' in kind else 'bin' if 'bin' in kind else None
if key:units[key]=target/Path(item['executable']).relative_to('/target')
require(set(units)=={'lib','bin'},'both actual compiled native test executables')
artifacts={}
for key,path,name,release in [('agent',target/TARGET/'release/rmm-agent-windows.exe','rmm-agent-windows.exe',True),
('probe',target/TARGET/'release/examples/runtime_state.exe','RuntimeStateProbe.exe',True),
('lib',units['lib'],'windows-tests-lib.exe',False),('bin',units['bin'],'windows-tests-bin.exe',False)]:
destination=folder/name;shutil.copyfile(path,destination);destination.chmod(0o600)
original_sha=digest(path)
if key in ('lib','bin'):
# Strip only the private delivery copy. Preserve Cargo's original
# linked executable and all prior manifests/native observations.
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(digest(path)==original_sha,'original native unit link preserved')
data=destination.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','native x86-64 PE artifact')
artifacts[key]=dict(path=str(destination.resolve()),name=name,sha256=digest(destination),bytes=len(data),release_build=release)
if key in ('lib','bin'):
artifacts[key].update(debug_stripped=True,original_link_path=str(path.resolve()),original_link_sha256=original_sha)
require(source_files()==before,'source changed during artifact preparation; do not qualify')
manifest=dict(schema_version=1,architecture=TARGET,default_features=True,system_helper_feature=False,
source_files=before,artifacts=artifacts,checks=dict(cross_clippy_D_warnings=True,release_build=True,windows_unit_link=True),native_vm_tested=False)
write(folder,'manifest.json',manifest)
print(json.dumps(dict(manifest=str((folder/'manifest.json').resolve()),artifacts={k:v['sha256'] for k,v in artifacts.items()},native_vm_tested=False)))
def upload(item):
pointer=cb.EVIDENCE/(item['name']+'.latest.json')
if pointer.exists() and cb.contract.protected_json(pointer)['sha256']==item['sha256']:
return cb.powershell(cb.VERIFY_UPLOAD,dict(name=item['name'],sha256=item['sha256']))
return cb.upload(item['path'],item['name'])
def qualify(manifest_path):
manifest=cb.contract.protected_json(manifest_path);stable(manifest)
os.umask(0o077);STATE.mkdir(mode=0o700,parents=True,exist_ok=True)
folder=STATE/('qualification-'+str(uuid.uuid4()));folder.mkdir(mode=0o700)
write(folder,'intent.json',dict(manifest_path=str(manifest_path.resolve()),manifest_sha256=digest(manifest_path),source_files=manifest['source_files'],test_runner_sha256=hashlib.sha256(TESTS.encode()).hexdigest()))
before=auto.scopes();flags=auto.status();write(folder,'server-before.json',dict(counts=before,flags=flags))
reports={}
for key,minimum in [('lib',12),('bin',6)]:
item=manifest['artifacts'][key];upload(item)
report=cb.powershell(TESTS,dict(name=item['name'],sha256=item['sha256']),timeout=150)
write(folder,key+'-native.json',report)
require(report['listed']['exit_code']==report['result']['exit_code']==0,'native unit executable success')
listed=re.findall(r'^(.+): test\r?$',report['listed']['stdout'],re.M)
summary=re.search(r'test result: ok\. (\d+) passed; 0 failed; 0 ignored; 0 measured; 0 filtered out;',report['result']['stdout'])
require(summary and len(listed)>=minimum and int(summary.group(1))==len(listed),'all actually listed native tests passed with zero skipped/filtered')
reports[key]=dict(artifact_sha256=item['sha256'],passed=len(listed),test_names=listed,native_context='bootstrap_administrator_unit_tests')
stable(manifest)
after=auto.scopes();after_flags=auto.status();require(after==before and flags==after_flags and auto.flags_disabled(flags),'native units changed no job counts or server policy/hooks')
write(folder,'result.json',dict(verified=True,completed=True,native_unit_tests=reports,manifest_path=str(manifest_path.resolve()),manifest_sha256=digest(manifest_path),source_files=manifest['source_files'],server_counts=after,server_flags=flags,
no_jobs=True,no_clock_service_changes=True,local_service_runtime_test=False,clock_adapter_qualified=False))
print(json.dumps(dict(verified=True,qualification=str((folder/'result.json').resolve()),native_tests={k:v['passed'] for k,v in reports.items()},clock_adapter_qualified=False)))
PREPARE_SOURCE=base.PREPARE_SCRIPT.replace('TransportAcceptance.ps1','RuntimeUpgrade.ps1')
LAUNCH_SOURCE=base.LAUNCH_SCRIPT.replace('TransportAcceptance.ps1','RuntimeUpgrade.ps1').replace('RmmTransportPayload','RmmRuntimePayload')
def stage_source():
raw=UPGRADE.encode();sha=hashlib.sha256(raw).hexdigest()
value=cb.powershell(PREPARE_SOURCE,dict(sha256=sha))
if not value['already_uploaded']:
folder=STATE/'source';folder.mkdir(mode=0o700,parents=True,exist_ok=True)
path=folder/(sha+'.ps1')
if not path.exists():
fd=os.open(path,os.O_CREAT|os.O_EXCL|os.O_WRONLY|os.O_NOFOLLOW,0o600)
with os.fdopen(fd,'wb') as stream:stream.write(raw);stream.flush();os.fsync(stream.fileno())
require(digest(path)==sha,'fixed staged source bytes')
prefix=cb.contract.protected_json(cb.ROOT/'credentials/windows-dev-access.json')['ssh_prefix']
arguments=['sftp']+[v for v in prefix[1:-1] if v!='-T']+['-b','-',prefix[-1]]
target='/C:/ProgramData/RustDiscoveryBootstrap/'+sha+'/RuntimeUpgrade.ps1'
result=subprocess.run(arguments,input=('put '+json.dumps(str(path.resolve()))+' '+json.dumps(target)+'\n').encode(),capture_output=True,timeout=60)
require(result.returncode==0,'fixed runtime bootstrap transfer')
require(cb.powershell(PREPARE_SOURCE,dict(sha256=sha))['already_uploaded'],'staged source hash verification')
return sha
def remote(intent,action):
require(intent['source_sha256']==hashlib.sha256(UPGRADE.encode()).hexdigest(),'preserved exact runtime bootstrap source')
payload={**intent['remote_payload'],'action':action}
return cb.powershell(LAUNCH_SOURCE,dict(sha256=intent['source_sha256'],payload=payload),timeout=100,remote_signed=True)
def manifest_and_qualification(manifest_path,qualification_path):
manifest=cb.contract.protected_json(manifest_path);stable(manifest)
qualified=cb.contract.protected_json(qualification_path)
require(qualified['verified'] is True and qualified['completed'] is True
and qualified['manifest_sha256']==digest(manifest_path)
and qualified['manifest_path']==str(manifest_path.resolve())
and qualified['source_files']==manifest['source_files']
and qualified['native_unit_tests']['lib']['artifact_sha256']==manifest['artifacts']['lib']['sha256']
and qualified['native_unit_tests']['bin']['artifact_sha256']==manifest['artifacts']['bin']['sha256']
and qualified['native_unit_tests']['lib']['passed']>=12 and qualified['native_unit_tests']['bin']['passed']>=6
and qualified['no_jobs'] is True and qualified['clock_adapter_qualified'] is False,'source-bound completed native library/bin qualification')
return manifest,qualified
def upgrade(manifest_path,qualification_path):
manifest,qualified=manifest_and_qualification(manifest_path,qualification_path)
current=cb.contract.protected_json(activation.CURRENT)
require(current['type']=='rmm.windows.current-transport' and current['case_id']==ACTIVATION
and current['transport']=='auto' and current['binary_sha256']==base.BINARY,'known original native automatic transport index')
derived=activation.transition(ACTIVATION)
require(derived['original_config_sha256']==base.ORIGINAL_CONFIG
and derived['expected_auto_config_sha256']==current['config_sha256'],'independently derived original auto bytes')
os.umask(0o077);STATE.mkdir(mode=0o700,parents=True,exist_ok=True)
ident=str(uuid.uuid4());folder=STATE/ident;folder.mkdir(mode=0o700)
for key in ['agent','probe']:upload(manifest['artifacts'][key])
source=stage_source()
payload=dict(id=ident,old_sha256=base.BINARY,new_sha256=manifest['artifacts']['agent']['sha256'],
probe_sha256=manifest['artifacts']['probe']['sha256'],config_sha256=current['config_sha256'])
intent=dict(id=ident,type='passive_runtime_upgrade',started_at=cb.contract.now().isoformat(),source_sha256=source,
manifest_path=str(manifest_path.resolve()),manifest_sha256=digest(manifest_path),qualification_path=str(qualification_path.resolve()),
qualification_sha256=digest(qualification_path),remote_payload=payload,config_derivation=derived,
original_transport_index=dict(path=str(activation.CURRENT.resolve()),sha256=digest(activation.CURRENT)),
no_jobs=True,no_binding_or_policy_change=True,no_clock_change=True,no_reboot=True)
write(folder,'intent.json',intent)
before=dict(native=remote(intent,'state'),central=base.observe(ident),counts=auto.scopes(),server=auto.status())
require(before['counts']==qualified['server_counts'] and before['server']==qualified['server_flags']
and auto.flags_disabled(before['server']),'unchanged settled job/policy state after qualification')
require(before['native']['service']['state']=='Running' and before['native']['journal']['pending_inventory_events']==0,'settled original service and acknowledged inventory')
write(folder,'before.json',before)
write(folder,'enter-intent.json',dict(id=ident,stop_swap_start_once=True,source_sha256=source))
try:write(folder,'entered.json',remote(intent,'enter'))
except Exception as error:
write(folder,'enter-reply-incomplete.json',dict(error_type=type(error).__name__,continuation='finish reads only; explicit rollback is separate, never repeat enter'))
raise
print('Passive runtime transition '+ident,flush=True)
finish(ident)
def load_case(ident):
ident=str(uuid.UUID(ident));folder=STATE/ident
intent=cb.contract.protected_json(folder/'intent.json')
require(intent['id']==ident and intent['type']=='passive_runtime_upgrade','same retained runtime case')
require(digest(intent['manifest_path'])==intent['manifest_sha256'] and digest(intent['qualification_path'])==intent['qualification_sha256'],'historical qualification files unchanged')
manifest,_=manifest_and_qualification(Path(intent['manifest_path']),Path(intent['qualification_path']))
return folder,intent,manifest
def publish_current(folder,result_name,binary):
result=folder/result_name
value=dict(schema_version=1,type='rmm.windows.current-runtime',case_id=folder.name,agent_id=base.AGENT,
asset_id=base.ASSET,binary_sha256=binary,transport='auto',evidence=dict(path=str(result.resolve()),sha256=digest(result)),
windows_jobs_enabled=False,clock_adapter_qualified=False)
path=cb.EVIDENCE/'current-runtime.json'
if path.exists():
previous=cb.contract.protected_json(path)
if previous==value:return
require(previous['case_id']==folder.name,'do not overwrite another current runtime transition')
backup=folder/('previous-index-'+str(uuid.uuid4())+'.json');write(folder,backup.name,previous)
temporary=path.with_name('.runtime-'+str(uuid.uuid4())+'.json')
write(temporary.parent,temporary.name,value);os.replace(temporary,path)
def finish(ident):
folder,intent,manifest=load_case(ident)
restored=(folder/'rollback-intent.json').exists()
before=cb.contract.protected_json(folder/'before.json')
observed=remote(intent,'finish');write(folder,'finish-readback-'+str(uuid.uuid4())+'.json',observed)
expected=base.BINARY if restored else manifest['artifacts']['agent']['sha256']
require(observed['completed'] is True and observed['read_only'] is True and observed['restored']==restored and observed['native']['binary_sha256']==expected,'actual retained selected runtime; no second start')
started=cb.contract.protected_json(folder/'rollback-intent.json')['started_at'] if restored else intent['started_at']
online=base.wait_mode(ident,'wss',cb.contract.timestamp(started),before['central']['device'])
after=remote(intent,'finish');write(folder,'after-readback-'+str(uuid.uuid4())+'.json',after)
native=after['native'];require(native['journal']['pending_inventory_events']==0 and native['journal']['sequence']>before['native']['journal']['sequence'],'actual new process heartbeat sequence and concrete inventory ACK')
device=base.read_device();denied=base.foreign_denied()
require(cb.contract.timestamp(device['observed_at'])>=cb.contract.timestamp(intent['started_at']),'fresh upgraded inventory stored independently')
observations={o['scope']:o for o in device['inventory'].get('observations',[])}
hardware=observations.get('windows.hardware',{})
if not restored:require(hardware.get('quality')=='valid' and hardware['data']['logical_processors_visible']>0
and hardware['data']['memory_total_bytes']>0,'actual unprivileged native hardware observation')
counts=auto.scopes();flags=auto.status();require(counts==before['counts'] and flags==before['server'] and auto.flags_disabled(flags),'both target job counts/policy/fault hooks preserved')
stable(manifest)
result_name='rollback-completed.json' if restored else 'result.json'
result=folder/result_name
value=dict(verified=True,completed=True,restored=restored,id=ident,native=native,online=online,stored_inventory=device,foreign_denied=denied,
manifest_path=intent['manifest_path'],manifest_sha256=intent['manifest_sha256'],qualification_sha256=intent['qualification_sha256'],
counts=counts,server=flags,no_jobs=True,no_reboot=True,no_clock_change=True,windows_jobs_enabled=False,
clock_adapter_qualified=False,private_key_read=False,configuration_unchanged=True,transport='auto',preferred_wire='wss')
if result.exists():require(cb.contract.protected_json(result)['verified'] is True,'retain prior successful runtime result')
else:write(folder,result_name,value)
publish_current(folder,result_name,expected)
print(json.dumps(dict(verified=True,id=ident,passive_only=True,clock_adapter_qualified=False,windows_jobs_enabled=False)))
def rollback(ident):
folder,intent,_=load_case(ident)
before=cb.contract.protected_json(folder/'before.json')
require(auto.scopes()==before['counts'] and auto.status()==before['server'],'no intervening job/policy changes before explicit rollback')
write(folder,'rollback-intent.json',dict(id=ident,old_binary_sha256=base.BINARY,started_at=cb.contract.now().isoformat(),start_once=True,no_jobs=True))
try:value=remote(intent,'rollback')
except Exception as error:
write(folder,'rollback-reply-incomplete.json',dict(error_type=type(error).__name__,readback_required=True,no_repeated_start=True));raise
write(folder,'rollback-result.json',value)
require(value['restored'] is True and value['native']['binary_sha256']==base.BINARY,'actual preserved original binary restored')
finish(ident)
def main():
parser=argparse.ArgumentParser(description=__doc__)
parser.add_argument('phase',choices=['build-local','qualify-native','upgrade-passive','finish','rollback'])
parser.add_argument('--manifest',type=Path);parser.add_argument('--qualification',type=Path);parser.add_argument('--id')
args=parser.parse_args()
if args.phase=='build-local':require(not(args.manifest or args.qualification or args.id),'build creates a fresh immutable manifest');build()
elif args.phase=='qualify-native':require(args.manifest and not(args.qualification or args.id),'one qualified local manifest');qualify(args.manifest)
elif args.phase=='upgrade-passive':require(args.manifest and args.qualification and not args.id,'native qualified same manifest, no supplied new case ID');upgrade(args.manifest,args.qualification)
else:require(args.id and not(args.manifest or args.qualification),'same original transition ID only');{'finish':finish,'rollback':rollback}[args.phase](args.id)
if __name__=='__main__':
try:main()
except Exception as error:
print('WINDOWS_RUNTIME_UPGRADE_INCOMPLETE:'+type(error).__name__)
raise SystemExit(2) from None