Bind Windows script contexts to the installed signed helper
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
# Exercises the first-contact installer transaction with no system services or network.
|
||||
$ErrorActionPreference = 'Stop'
|
||||
Set-StrictMode -Version Latest
|
||||
$path = Join-Path $PSScriptRoot '../crates/server/src/bootstrap/install.ps1'
|
||||
$errors = $null
|
||||
$ast = [Management.Automation.Language.Parser]::ParseFile((Resolve-Path $path), [ref]$null, [ref]$errors)
|
||||
if ($errors) { throw ($errors.Message -join ' ') }
|
||||
$setup = $ast.Find({
|
||||
param($node)
|
||||
$node -is [Management.Automation.Language.FunctionDefinitionAst] -and $node.Name -ceq 'Install-ScriptHelper'
|
||||
}, $false)
|
||||
$realSetup = [scriptblock]::Create($setup.Body.Extent.Text.Substring(1, $setup.Body.Extent.Text.Length - 2))
|
||||
# With strict mode, trying even to look up the installation directory would fail.
|
||||
$EnableAdministratorScripts = $false
|
||||
& $realSetup
|
||||
function Assert-AdminPath($Path, $StopAt) { }
|
||||
$function = $ast.Find({
|
||||
param($node)
|
||||
$node -is [Management.Automation.Language.FunctionDefinitionAst] -and $node.Name -ceq 'Start-ReleaseAfterFirstContact'
|
||||
}, $false)
|
||||
. ([scriptblock]::Create($function.Extent.Text))
|
||||
|
||||
$serviceName = 'RmmDiscoveryWindows'
|
||||
$releaseService = 'RmmDiscoveryReleasePull'
|
||||
$agentStatus = 'Stopped'
|
||||
$updaterStarts = 0
|
||||
$attempt = 0
|
||||
function Get-Service($name) {
|
||||
if ($name -cne $serviceName) { throw 'Unexpected SCM read' }
|
||||
return @{Status=$script:agentStatus}
|
||||
}
|
||||
function Get-CimInstance($class, $Filter) {
|
||||
if ($class -cne 'Win32_Service' -or $Filter -cne "Name='RmmDiscoveryWindows'") { throw 'Unexpected SCM registration' }
|
||||
return @{StartName='NT AUTHORITY\LocalService'}
|
||||
}
|
||||
function Start-Service($name) {
|
||||
if ($name -ceq $releaseService) { $script:updaterStarts++ }
|
||||
elseif ($name -ceq $serviceName) { $script:agentStatus = 'Running' }
|
||||
else { throw 'Unexpected SCM startup' }
|
||||
}
|
||||
function Install-ScriptHelper {
|
||||
$script:attempt++
|
||||
if ($script:attempt -eq 1) { throw 'simulated provisioning interruption' }
|
||||
}
|
||||
|
||||
$state = Join-Path ([IO.Path]::GetTempPath()) ('rmm-script-install-test-' + [guid]::NewGuid())
|
||||
[void][IO.Directory]::CreateDirectory($state)
|
||||
try {
|
||||
$EnableAdministratorScripts = $true
|
||||
$data = $state
|
||||
$cfg = Join-Path $data 'config.json'
|
||||
$policy = @{enable_jobs=$true;localservice_time_policy=(Join-Path $data 'job-time.json');windows_update_helper=$true} | ConvertTo-Json
|
||||
[IO.File]::WriteAllText($cfg, $policy)
|
||||
$lock = Join-Path $data 'release-job.lock'
|
||||
[IO.File]::WriteAllText($lock, '')
|
||||
$active = [IO.File]::Open($lock, 'Open', 'Read', 'Read')
|
||||
try {
|
||||
try { & $realSetup; throw 'Expected active job lock rejection' }
|
||||
catch { if ($_.Exception.GetBaseException() -isnot [IO.IOException]) { throw } }
|
||||
} finally { $active.Dispose() }
|
||||
try { & $realSetup; throw 'Expected active restricted helper rejection' }
|
||||
catch { if ($_.Exception.Message -notlike 'The existing restricted execution profile*') { throw } }
|
||||
if ([IO.File]::ReadAllText($cfg) -cne $policy) { throw 'Existing job scope changed.' }
|
||||
$identity = Join-Path $state 'identity.json'
|
||||
$firstContact = Join-Path $state 'release-first-contact.pending'
|
||||
$bytes = '{"agent_id":"8dc9094d-28f4-4aa0-86f8-abdef87d13b6"}'
|
||||
[IO.File]::WriteAllText($identity, $bytes)
|
||||
[IO.File]::WriteAllText((Join-Path $state 'job-readiness.json'), '{"agent_id":"8dc9094d-28f4-4aa0-86f8-abdef87d13b6","connection_generation":1}')
|
||||
[IO.File]::WriteAllText($firstContact, 'pending')
|
||||
try { Start-ReleaseAfterFirstContact; throw 'Expected interruption' }
|
||||
catch { if ($_.Exception.Message -cne 'simulated provisioning interruption') { throw } }
|
||||
if (!(Test-Path $firstContact) -or $updaterStarts -ne 1 -or $agentStatus -cne 'Running') {
|
||||
throw 'Interrupted enrollment must retain its retry marker and keep both ordinary services running.'
|
||||
}
|
||||
Start-ReleaseAfterFirstContact
|
||||
if ((Test-Path $firstContact) -or $updaterStarts -ne 2 -or $attempt -ne 2 -or [IO.File]::ReadAllText($identity) -cne $bytes) {
|
||||
throw 'Retry must complete with the existing identity before removing its marker.'
|
||||
}
|
||||
Write-Output 'Local opt-in, active job lock, restricted helper, interrupted setup and first-contact retry passed.'
|
||||
} finally { [IO.Directory]::Delete($state, $true) }
|
||||
Reference in New Issue
Block a user