fix(deploy): Upgrade-MxGatewayService.ps1 safe on the wonder layout, guards fail closed (#230)
ci / windows-x86 (push) Successful in 2m41s
ci / nightly-windev (push) Has been skipped
ci / portable (push) Failing after 12m34s
ci / java (push) Failing after 14m50s

The process guards matched only ZB.MOM.WW.MxGateway.*; the wonder boxes run
MxGateway.Server.exe / MxGateway.Worker.exe, so the one-gateway guard, the
pre-start OrphanWorkerTerminator guard and the STOP_PENDING kill passed vacuously.

- Image names come from the installed exes (NSSM Application, Worker env path) plus
  both known schemes; processes are also matched by exact path (Win32_Process).
- One-gateway guard fails closed: a RUNNING service whose gateway is not visible, at
  the Application path, as the service host's child, is an abort.
- STOP_PENDING kill is scoped to this install's processes.
- Staged checks use the installed exe names; staged Worker must be x86; a staged
  self-contained runtime older than the live one is refused (-AllowRuntimeDowngrade).
- Carry-over: appsettings*.json and certificates/keys (dashboard.pfx) always keep the
  live copy; other live-only non-binary top-level files are carried; hash-verified.
- Env block read from the registry and fingerprinted (names + SHA-256 prefixes, never
  values) after stop, swap, start and the stability window; any change aborts.
  Sensitive values are redacted from all output and notes.
- File-based master key (vd03) accepted and backed up; a missing env master key
  (windev) is a warning. Backup dir is ACL-restricted before anything is written.
- Rollback accepts .bak-<stamp> and .bak.<yyyyMMddTHHmmss> sets and refuses a
  mixed Server/Worker stamp; auto-rollback no longer fails on the old SHA.
- -WhatIf now works on Windows PowerShell 5.1 (Get-FileHash returned nothing under
  WhatIfPreference); an empty Win32_Service result no longer reads as "unavailable".
- Install-MxGatewayService.ps1 process guard matches both schemes.
- Dry-run harness scripts/deploy/tests (fake box in both layouts, 140 assertions,
  passes on pwsh/macOS and Windows PowerShell 5.1) plus a mutation runner (12/12 killed).
- Runbook updated.

Claude-Session: https://claude.ai/code/session_01GT67ACmpMR7b9LGRPLijMi
This commit is contained in:
Joseph Doherty
2026-11-12 22:23:14 -05:00
parent 453fe01525
commit 4890a3d16e
7 changed files with 1192 additions and 218 deletions
+75
View File
@@ -0,0 +1,75 @@
# Fakes for Test-UpgradeMxGatewayService.ps1. Dot-sourced AFTER Upgrade-MxGatewayService.ps1 so these replace
# its environment seams. All state lives in $script:Box (built by New-FakeBox in the test script).
# Nothing here touches the real service manager, registry, process table or network.
function Invoke-Nssm {
param([string[]]$NssmArgs, [switch]$AllowFail)
$b = $script:Box
$b.NssmCalls.Add(($NssmArgs -join ' '))
$verb = $NssmArgs[0]
switch ($verb) {
'status' {
if ($b.State -eq 'MISSING') { return [pscustomobject]@{ Code = 3; Text = "Can't open service!" } }
if ($b.State -eq 'SERVICE_STOP_PENDING' -and -not @($b.Procs | Where-Object { $_.Role -eq 'Server' }).Count) { $b.State = 'SERVICE_STOPPED' }
return [pscustomobject]@{ Code = 0; Text = $b.State }
}
'get' {
switch ($NssmArgs[2]) {
'Application' { return [pscustomobject]@{ Code = 0; Text = $b.Application } }
'AppDirectory' { return [pscustomobject]@{ Code = 0; Text = $b.AppDirectory } }
'AppEnvironmentExtra' { return [pscustomobject]@{ Code = 0; Text = ($b.Env -join "`r`n") } }
}
}
'stop' {
if ($b.StopHangs) { $b.State = 'SERVICE_STOP_PENDING'; return [pscustomobject]@{ Code = 0; Text = '' } }
$b.State = 'SERVICE_STOPPED'
$b.Procs = @($b.Procs | Where-Object { $_.Role -eq 'Other' -or $b.Survivors -contains $_.Id })
return [pscustomobject]@{ Code = 0; Text = '' }
}
'start' {
$b.Starts++
$b.StartedWith.Add($(if ((Get-Content -LiteralPath $b.Application -Raw) -match 'sha=([0-9a-f]+)') { $Matches[1] } else { '?' }))
$b.State = 'SERVICE_RUNNING'
$sid = 7000 + $b.Starts * 10
$b.Procs = @($b.Procs) + @(
[pscustomobject]@{ Id = $sid; ParentId = $b.HostPid; Name = [IO.Path]::GetFileNameWithoutExtension($b.Application); Path = $b.Application; Role = 'Server' },
[pscustomobject]@{ Id = $sid + 1; ParentId = $sid; Name = [IO.Path]::GetFileNameWithoutExtension($b.WorkerExe); Path = $b.WorkerExe; Role = 'Worker' })
if ($b.OnStart) { & $b.OnStart $b }
return [pscustomobject]@{ Code = 0; Text = '' }
}
}
throw "fake nssm: unexpected call $($NssmArgs -join ' ')"
}
function Read-EnvBlockLines { @($script:Box.Env) }
# Return copies so the script's Add-Member (Role) cannot leak back into the fake state.
function Get-ProcessTable { @($script:Box.Procs | ForEach-Object { [pscustomobject]@{ Id = $_.Id; ParentId = $_.ParentId; Name = $_.Name; Path = $_.Path } }) }
function Get-ServiceHostPid { $script:Box.HostPid }
function Get-OtherGatewayServices([string]$ExeName) { , @($script:Box.OtherServices) }
function Stop-ProcessById([int]$Id) {
$script:Box.Killed.Add($Id)
$script:Box.Procs = @($script:Box.Procs | Where-Object { $_.Id -ne $Id })
}
function Get-HttpStatus([string]$Url) { $script:Box.HttpCalls.Add($Url); $script:Box.Health }
function Get-ProductSha([string]$Exe) {
if (-not (Test-Path -LiteralPath $Exe)) { return $null }
if ((Get-Content -LiteralPath $Exe -Raw) -match 'sha=([0-9a-f]+)') { return $Matches[1] }
$null
}
function Get-ExeArchitecture([string]$Exe) {
if ((Get-Content -LiteralPath $Exe -Raw) -match 'arch=(\w+)') { return $Matches[1] }
$null
}
function Protect-Directory([string]$Path) { $script:Box.Protected.Add($Path) }
function Export-ServiceKey([string]$Destination) { Set-Content -LiteralPath $Destination -Value 'fake reg export' }
function Get-DirSddl([string]$Path) { $null }
function Copy-ExplicitAcl([string]$From, [string]$To) { }
# Fake clock: Start-Sleep advances it, so timeouts expire instantly and deterministically.
function Start-Sleep { param([int]$Seconds, [int]$Milliseconds) $script:FakeNow = $script:FakeNow.AddSeconds([math]::Max($Seconds, 1)) }
function Get-Date {
param([string]$Format)
if ($Format) { return (Microsoft.PowerShell.Utility\Get-Date -Format $Format) }
$script:FakeNow
}