69 lines
4.7 KiB
PowerShell
69 lines
4.7 KiB
PowerShell
[CmdletBinding()]
|
|
param(
|
|
[Parameter(Mandatory)][string]$RequestPath,
|
|
[Parameter(Mandatory)][string]$ResultPath,
|
|
[Parameter(Mandatory)][string]$RepositoryRoot
|
|
)
|
|
|
|
Set-StrictMode -Version Latest
|
|
$ErrorActionPreference = 'Stop'
|
|
|
|
function Write-WorkerResult($Value) {
|
|
$temporary = "$ResultPath.$([guid]::NewGuid().ToString('N')).tmp"
|
|
$Value | ConvertTo-Json -Depth 16 | Set-Content -LiteralPath $temporary -Encoding utf8NoBOM
|
|
[System.IO.File]::Move($temporary, $ResultPath, $true)
|
|
}
|
|
|
|
try {
|
|
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
|
|
$principal = [Security.Principal.WindowsPrincipal]::new($identity)
|
|
if (-not $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { throw 'Elevated worker is not running as administrator.' }
|
|
if (-not (Test-Path -LiteralPath $RequestPath -PathType Leaf)) { throw 'Elevation request is missing.' }
|
|
$root = [System.IO.Path]::GetFullPath($RepositoryRoot)
|
|
$worker = [System.IO.Path]::GetFullPath($PSCommandPath)
|
|
if (-not $worker.StartsWith(($root.TrimEnd('\') + '\'), [System.StringComparison]::OrdinalIgnoreCase)) { throw 'Elevated worker is outside the repository snapshot.' }
|
|
$request = Get-Content -LiteralPath $RequestPath -Raw | ConvertFrom-Json -Depth 16
|
|
if ($request.schemaVersion -ne 1) { throw 'Unsupported elevation request version.' }
|
|
if ($request.requestType -eq 'packages') {
|
|
foreach ($operation in @($request.operations)) {
|
|
if ($operation.package.PSObject.Properties['provider'] -and $operation.package.provider -ne 'winget') { throw 'Only WinGet packages may use the elevated worker.' }
|
|
if ($operation.kind -ne 'package' -or $operation.action -notin @('Install', 'Upgrade', 'Uninstall', 'None', 'Warning')) { throw 'Elevation request contains an unsupported operation.' }
|
|
if ($operation.package.id -notmatch '^[A-Za-z0-9._+-]+$' -or $operation.package.source -notmatch '^[A-Za-z0-9._+-]+$') { throw 'Elevation request contains an unsafe package identifier.' }
|
|
if ($operation.package.PSObject.Properties['scope'] -and $operation.package.scope -ne 'machine') { throw 'Elevated operations must use machine scope.' }
|
|
}
|
|
Import-Module (Join-Path $root 'src/Packages.psm1') -Force
|
|
$results = Invoke-PackagePlan -Plan @($request.operations) -LogPath ([string]$request.logPath)
|
|
Write-WorkerResult ([ordered]@{ succeeded = -not [bool]($results | Where-Object status -eq 'Failed'); results = $results })
|
|
exit 0
|
|
}
|
|
if ($request.requestType -ne 'hosts') { throw 'Unknown elevation request type.' }
|
|
$operation = $request.operation
|
|
if ($operation.mode -ne 'hosts-block' -or $operation.name -notmatch '^[a-z0-9][a-z0-9-]*$') { throw 'Invalid elevated hosts operation.' }
|
|
$expectedTarget = [IO.Path]::GetFullPath((Join-Path ([Environment]::SystemDirectory) 'drivers\etc\hosts'))
|
|
if ([IO.Path]::GetFullPath([string]$operation.target) -ne $expectedTarget) { throw 'Elevated hosts target is not the Windows hosts file.' }
|
|
foreach ($line in @($operation.desired)) {
|
|
$parts = @(([string]$line).Trim() -split '\s+')
|
|
$address = $null
|
|
if ($parts.Count -lt 2 -or -not [Net.IPAddress]::TryParse($parts[0], [ref]$address)) { throw "Invalid elevated hosts entry '$line'." }
|
|
foreach ($hostName in $parts[1..($parts.Count - 1)]) { if ($hostName.StartsWith('#')) { break }; if ($hostName -notmatch '^[A-Za-z0-9._-]+$') { throw "Invalid elevated host name '$hostName'." } }
|
|
}
|
|
$backupFolder = Join-Path ([IO.Path]::GetFullPath([string]$request.backupRoot)) $operation.name
|
|
[void](New-Item -ItemType Directory -Path $backupFolder -Force)
|
|
$backup = Join-Path $backupFolder "$([DateTimeOffset]::UtcNow.ToString('yyyyMMddTHHmmssfffZ')).bak"
|
|
Copy-Item -LiteralPath $expectedTarget -Destination $backup -Force
|
|
Import-Module (Join-Path $root 'src/Adapters.psm1') -Force
|
|
$content = Set-HostsBlockContent -Target $expectedTarget -Name $operation.name -Lines @($operation.desired)
|
|
$temporary = Join-Path (Split-Path -Parent $expectedTarget) ".hosts.$([guid]::NewGuid().ToString('N')).tmp"
|
|
try {
|
|
[IO.File]::WriteAllText($temporary, $content, [Text.UTF8Encoding]::new($false))
|
|
[IO.File]::Move($temporary, $expectedTarget, $true)
|
|
}
|
|
finally { Remove-Item -LiteralPath $temporary -Force -ErrorAction SilentlyContinue }
|
|
Write-WorkerResult ([ordered]@{ succeeded = $true; result = [ordered]@{ name = $operation.name; status = 'Succeeded'; message = "Deployed; backup: $backup"; backup = $backup; deployedHash = $operation.sourceHash } })
|
|
exit 0
|
|
}
|
|
catch {
|
|
Write-WorkerResult ([ordered]@{ succeeded = $false; error = $_.Exception.Message; results = @() })
|
|
exit 1
|
|
}
|