Bind Windows maintenance permission to update template and device identity probe

This commit is contained in:
2026-11-17 16:34:17 +01:00
parent 5c3f5b6a11
commit a799272d91
3 changed files with 68 additions and 12 deletions
+28 -9
View File
@@ -5,7 +5,7 @@ param(
[Parameter(Mandatory)][Uri]$BootstrapBase,
[Security.SecureString]$EnrollmentToken,
[ValidateSet('x86_64-pc-windows-msvc','x86_64-pc-windows-gnu')]
[string]$Platform = 'x86_64-pc-windows-msvc'
[string]$Platform = 'x86_64-pc-windows-msvc'
)
$ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest
@@ -66,15 +66,36 @@ function Set-Text([string]$Path, [string]$Value) {
function Get-Hash([string]$Path) {
return (Get-FileHash -LiteralPath $Path -Algorithm SHA256).Hash.ToLowerInvariant()
}
function Start-ReleaseAfterFirstContact {
$receipt = Join-Path $state 'job-readiness.json'
$until = [DateTime]::UtcNow.AddSeconds(120)
while ([DateTime]::UtcNow -lt $until) {
if ((Get-Service $serviceName).Status -ne 'Running') { throw 'Agent stopped before first gateway contact. Inspect the installed service.' }
if ((Test-Path -LiteralPath $identity -PathType Leaf) -and (Test-Path -LiteralPath $receipt -PathType Leaf)) {
# The runtime persists this generation only after authenticating the gateway and completing ClientHello.
$enrolled = [IO.File]::ReadAllText($identity) | ConvertFrom-Json
$hello = [IO.File]::ReadAllText($receipt) | ConvertFrom-Json
if ($hello.agent_id -ceq $enrolled.agent_id -and $hello.connection_generation -ge 1) {
Start-Service $releaseService
Remove-Item -LiteralPath $firstContact -Force
return
}
}
Start-Sleep -Seconds 2
}
throw 'First authenticated version report is pending. Rerun the installer to finish with this identity.'
}
$origin = Get-Origin $BootstrapBase
$app = Join-Path ([Environment]::GetFolderPath('ProgramFiles')) 'RustDiscovery'
$data = Join-Path ([Environment]::GetFolderPath('CommonApplicationData')) 'RustDiscovery'
$state = Join-Path $data 'state'
$identity = Join-Path $state 'identity.json'
$firstContact = Join-Path $data 'release-first-contact.pending'
$serviceName = 'RmmDiscoveryWindows'
$releaseService = 'RmmDiscoveryReleasePull'
$service = Get-Service -Name $serviceName -ErrorAction SilentlyContinue
if ($service -and (Test-Path -LiteralPath $identity -PathType Leaf)) {
if (Test-Path -LiteralPath $firstContact -PathType Leaf) { Start-ReleaseAfterFirstContact }
Write-Output "Rust Discovery is already enrolled. Agent service: $($service.Status)."
return
}
@@ -167,6 +188,7 @@ try {
Set-Text (Join-Path $dataStage 'config.json') ($config | ConvertTo-Json -Compress)
$baseline = [ordered]@{schema_version=1; tenant_id=$tenant; release_id=$releaseId; release_sequence=$verified.release_sequence; manifest_sha256=(Get-Hash (Join-Path $images 'manifest.json'))}
Set-Text (Join-Path $dataStage 'installed-release.json') ($baseline | ConvertTo-Json -Compress)
Set-Text (Join-Path $dataStage 'release-first-contact.pending') $releaseId
[IO.Directory]::Move($appStage, $app)
$madeApp = $true
[IO.Directory]::Move($dataStage, $data)
@@ -200,16 +222,13 @@ try {
$updaterBinary = '"' + (Join-Path $app 'rmm-updater-windows.exe') + '" --release-service'
New-Service -Name $releaseService -BinaryPathName $updaterBinary -StartupType Automatic -Description 'Rust Discovery signed Windows release polling' | Out-Null
$madeReleaseService = $true
& $sc failure $releaseService reset= 86400 actions= restart/15000/restart/60000/restart/300000 | Out-Null
if ($LASTEXITCODE -ne 0) { throw 'SCM release recovery configuration failed.' }
& $sc failureflag $releaseService 1 | Out-Null
if ($LASTEXITCODE -ne 0) { throw 'SCM release failure recovery flag failed.' }
$startRequested = $true
Start-Service $serviceName
$until = [DateTime]::UtcNow.AddSeconds(120)
while (-not (Test-Path -LiteralPath $identity -PathType Leaf)) {
if ([DateTime]::UtcNow -ge $until -or (Get-Service $serviceName).Status -ne 'Running') {
throw 'Enrollment has not completed. Installed configuration and device key are retained.'
}
Start-Sleep -Seconds 2
}
Start-Service $releaseService
Start-ReleaseAfterFirstContact
Write-Output "Enrolled Rust Discovery agent $($verified.version) over HTTPS."
} catch {
if (-not $startRequested) {