Files
rust-discovery/crates/agent-windows/src/release_service_switch.ps1
T

94 lines
5.3 KiB
PowerShell

$ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest
function Get-Image([string]$Id) {
$image = Join-Path ([Environment]::GetFolderPath('ProgramFiles')) ('RustDiscovery\releases\' + $Id + '\rmm-agent-windows.exe')
return '"' + $image + '" --service --config "' + $configPath + '"'
}
function Get-HelperImage([string]$Id) {
Join-Path ([Environment]::GetFolderPath('ProgramFiles')) ('RustDiscovery\releases\' + $Id + '\rmm-helper-windows.exe')
}
function Wait-Stopped([string]$Name) {
$control = Get-Service $Name
if ($control.Status -ne 'Stopped') {
$control.Stop()
$control.WaitForStatus('Stopped', [TimeSpan]::FromSeconds(45))
}
}
function Select-Image($Service, [string]$Image) {
if ($Service.PathName -cne $Image) {
$result = Invoke-CimMethod -InputObject $Service -MethodName Change -Arguments @{PathName=$Image}
if ($result.ReturnValue -ne 0) { throw 'SCM path change failed.' }
}
}
function Replace-Pin([string]$Path, [string]$Value) {
$temp = "$Path.next-$([Guid]::NewGuid())"
try {
[IO.File]::WriteAllText($temp, $Value, [Text.UTF8Encoding]::new($false))
[IO.File]::Replace($temp, $Path, [NullString]::Value)
} finally {
if (Test-Path -LiteralPath $temp) { Remove-Item -LiteralPath $temp }
}
}
$configPath = Join-Path ([Environment]::GetFolderPath('CommonApplicationData')) 'RustDiscovery\config.json'
$from = Get-Image 'FROM_UUID'
$to = Get-Image 'TO_UUID'
$service = Get-CimInstance -ClassName Win32_Service -Filter "Name='RmmDiscoveryWindows'"
$config = Get-Content -LiteralPath $configPath -Raw | ConvertFrom-Json
if ($config.PSObject.Properties['enable_jobs'] -and $config.enable_jobs) {
if (-not $config.PSObject.Properties['localservice_time_policy'] -or
$config.localservice_time_policy -cne (Join-Path (Split-Path $configPath) 'job-time.json')) { throw 'Job-enabled pair requires a coordinated helper handoff.' }
}
$managed = $config.PSObject.Properties['managed_script_helper'] -and $config.managed_script_helper
$helper = Get-CimInstance -ClassName Win32_Service -Filter "Name='RmmDiscoverySystemHelper'"
if (-not $service -or $service.StartName -ine 'NT AUTHORITY\LocalService' -or
$service.PathName -cnotin @($from,$to) -or ([bool]$helper -ne [bool]$managed)) { throw 'Unexpected service configuration.' }
if ($managed) {
$helperExe = Get-HelperImage 'TO_UUID'
$helperTo = '"' + $helperExe + '" --service'
$helperFrom = '"' + (Get-HelperImage 'FROM_UUID') + '" --service'
if ($helper.StartName -ine 'LocalSystem' -or $helper.PathName -cnotin @($helperFrom,$helperTo)) { throw 'Unexpected managed helper service.' }
$policyRoot = Join-Path ([Environment]::GetFolderPath('CommonApplicationData')) 'RustDiscoverySystem'
$settingsPath = Join-Path $policyRoot 'helper.json'
$settings = Get-Content -LiteralPath $settingsPath -Raw | ConvertFrom-Json
if ($settings.schema_version -ne 5) { throw 'Managed helper settings required.' }
# Old renderers require this input; the successor preserves the complete
# existing scope, including an absent long Windows Update permission.
$longRuntime = 3600
if ($settings.PSObject.Properties['windows_update_max_runtime_seconds']) { $longRuntime = $settings.windows_update_max_runtime_seconds }
$rendered = ([ordered]@{binding=$settings.binding; release_id='TO_UUID'
agent_sha256='SELECTED_AGENT_SHA256'; helper_sha256='SELECTED_HELPER_SHA256'
max_runtime_seconds=$longRuntime; managed_scripts=$true} | ConvertTo-Json -Depth 6 -Compress) |
& $helperExe --prepare-enrolled-windows-update
if ($LASTEXITCODE -ne 0) { throw 'Signed successor helper could not render its pins.' }
$pins = $rendered | ConvertFrom-Json
# Rollback may select an earlier renderer. Compare the complete protected
# policy, allowing only the two release image paths and digests to change.
$scope = $pins.helper_settings | ConvertFrom-Json
if ($scope.agent.binary -cne (Join-Path (Split-Path $helperExe) 'rmm-agent-windows.exe') -or
$scope.helper.binary -cne $helperExe -or $scope.agent.binary_sha256 -cne 'SELECTED_AGENT_SHA256' -or
$scope.helper.binary_sha256 -cne 'SELECTED_HELPER_SHA256') { throw 'Rendered helper image pins differ from the selected release.' }
foreach ($role in @('agent','helper')) {
$scope.$role.binary = $settings.$role.binary
$scope.$role.binary_sha256 = $settings.$role.binary_sha256
}
if (($scope | ConvertTo-Json -Depth 12 -Compress) -cne ($settings | ConvertTo-Json -Depth 12 -Compress)) {
throw 'Agent release cannot change the managed script authorization.'
}
Wait-Stopped 'RmmDiscoveryWindows'
Wait-Stopped 'RmmDiscoverySystemHelper'
Replace-Pin $settingsPath $pins.helper_settings
Replace-Pin (Join-Path $policyRoot 'agent-pin.json') $pins.agent_pin
Select-Image $helper $helperTo
Select-Image $service $to
Start-Service 'RmmDiscoverySystemHelper'
(Get-Service 'RmmDiscoverySystemHelper').WaitForStatus('Running', [TimeSpan]::FromSeconds(45))
} elseif ($service.PathName -cne $to) {
Wait-Stopped 'RmmDiscoveryWindows'
Select-Image $service $to
}
$control = Get-Service 'RmmDiscoveryWindows'
if ($control.Status -ne 'Running') {
$control.Start()
$control.WaitForStatus('Running',[TimeSpan]::FromSeconds(45))
}