Add Windows PatchMon enrollment wrapper
This commit is contained in:
@@ -117,6 +117,30 @@ Useful options:
|
||||
|
||||
Token note: keep the token secret out of Git. Store it in a password manager or a local `.env` and paste/use it at enrollment time only.
|
||||
|
||||
### Windows PatchMon enrollment
|
||||
|
||||
Enroll a Windows host using the same persistent PatchMon auto-enrollment token key/secret. Run PowerShell as Administrator.
|
||||
|
||||
Interactive one-liner, prompts for the token key and secret:
|
||||
|
||||
```powershell
|
||||
$u = "https://gitea.wheelytho.com/Wheelz/Docker-Install/raw/branch/main/patchmon-enroll-windows.ps1"; $s = "$env:TEMP\patchmon-enroll-windows.ps1"; Invoke-WebRequest -Uri $u -OutFile $s -UseBasicParsing; powershell.exe -NoProfile -ExecutionPolicy Bypass -File $s
|
||||
```
|
||||
|
||||
Non-interactive form:
|
||||
|
||||
```powershell
|
||||
$u = "https://gitea.wheelytho.com/Wheelz/Docker-Install/raw/branch/main/patchmon-enroll-windows.ps1"; $s = "$env:TEMP\patchmon-enroll-windows.ps1"; Invoke-WebRequest -Uri $u -OutFile $s -UseBasicParsing; powershell.exe -NoProfile -ExecutionPolicy Bypass -File $s -TokenKey "<key>" -TokenSecret "<secret>" -Force
|
||||
```
|
||||
|
||||
What it does:
|
||||
|
||||
- installs the Wheelz/Caddy local root CA into `LocalMachine\Root`
|
||||
- calls PatchMon `/api/v1/auto-enrollment/enroll` with the persistent token
|
||||
- downloads the Windows agent installer from `https://patchmon.wheelz.lab`
|
||||
- normalizes any generated raw-IP PatchMon URLs back to the FQDN
|
||||
- runs the PatchMon Windows installer
|
||||
|
||||
## Options
|
||||
|
||||
```text
|
||||
|
||||
@@ -0,0 +1,205 @@
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Enroll a Windows host into PatchMon using the Wheelz persistent PatchMon auto-enrollment token.
|
||||
|
||||
.DESCRIPTION
|
||||
This wrapper mirrors the Linux patchmon-enroll.sh flow for Windows:
|
||||
1. Installs the Wheelz/Caddy local root CA into LocalMachine\Root.
|
||||
2. Enrolls this Windows host through PatchMon's auto-enrollment API using a token key/secret.
|
||||
3. Downloads PatchMon's Windows agent installer using the returned host API credentials.
|
||||
4. Normalizes generated installer content to https://patchmon.wheelz.lab.
|
||||
5. Runs the generated installer.
|
||||
|
||||
Run from an elevated PowerShell session.
|
||||
#>
|
||||
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[string]$Server = "https://patchmon.wheelz.lab",
|
||||
[string]$TokenKey = $env:PATCHMON_AUTO_ENROLL_KEY,
|
||||
[string]$TokenSecret = $env:PATCHMON_AUTO_ENROLL_SECRET,
|
||||
[string]$FriendlyName = $env:COMPUTERNAME,
|
||||
[string]$CaUrl = "https://gitea.wheelytho.com/Wheelz/Docker-Install/raw/branch/main/caddy-local-root.crt",
|
||||
[switch]$NoCaddyCa,
|
||||
[switch]$Force,
|
||||
[switch]$PrintOnly
|
||||
)
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
$ProgressPreference = "SilentlyContinue"
|
||||
|
||||
function Write-Step {
|
||||
param([string]$Message)
|
||||
Write-Host "`n[PatchMon-Windows-Enroll] $Message" -ForegroundColor Cyan
|
||||
}
|
||||
|
||||
function Write-Warn {
|
||||
param([string]$Message)
|
||||
Write-Warning "[PatchMon-Windows-Enroll] $Message"
|
||||
}
|
||||
|
||||
function Assert-Admin {
|
||||
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
|
||||
$principal = New-Object Security.Principal.WindowsPrincipal($identity)
|
||||
if (-not $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
|
||||
throw "Run this script from an elevated PowerShell session because it installs a machine certificate and system agent."
|
||||
}
|
||||
}
|
||||
|
||||
function Read-SecretPlainText {
|
||||
param([string]$Prompt)
|
||||
$secure = Read-Host -Prompt $Prompt -AsSecureString
|
||||
$bstr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($secure)
|
||||
try {
|
||||
return [Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr)
|
||||
}
|
||||
finally {
|
||||
if ($bstr -ne [IntPtr]::Zero) {
|
||||
[Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function Invoke-PatchMonWebRequest {
|
||||
param(
|
||||
[Parameter(Mandatory=$true)][string]$Uri,
|
||||
[string]$Method = "GET",
|
||||
[hashtable]$Headers,
|
||||
[string]$Body,
|
||||
[string]$OutFile,
|
||||
[string]$ContentType = "application/json"
|
||||
)
|
||||
|
||||
$params = @{
|
||||
Uri = $Uri
|
||||
Method = $Method
|
||||
UseBasicParsing = $true
|
||||
}
|
||||
if ($Headers) { $params.Headers = $Headers }
|
||||
if ($Body) { $params.Body = $Body; $params.ContentType = $ContentType }
|
||||
if ($OutFile) { $params.OutFile = $OutFile }
|
||||
|
||||
try {
|
||||
return Invoke-WebRequest @params
|
||||
}
|
||||
catch {
|
||||
if ($_.Exception.Response -and $_.Exception.Response.GetResponseStream()) {
|
||||
try {
|
||||
$reader = New-Object System.IO.StreamReader($_.Exception.Response.GetResponseStream())
|
||||
$responseText = $reader.ReadToEnd()
|
||||
Write-Warn "PatchMon response: $responseText"
|
||||
}
|
||||
catch {}
|
||||
}
|
||||
throw
|
||||
}
|
||||
}
|
||||
|
||||
function Install-WheelzCaddyRootCa {
|
||||
if ($NoCaddyCa) {
|
||||
Write-Step "Skipping Caddy CA install because -NoCaddyCa was supplied"
|
||||
return
|
||||
}
|
||||
|
||||
Write-Step "Installing/updating Wheelz Caddy local root CA into LocalMachine\\Root"
|
||||
$caPath = Join-Path $env:TEMP "wheelz-caddy-local-root.crt"
|
||||
Invoke-WebRequest -Uri $CaUrl -OutFile $caPath -UseBasicParsing
|
||||
|
||||
$sha = (Get-FileHash -Algorithm SHA256 -Path $caPath).Hash.ToLowerInvariant()
|
||||
$expected = "d4b15cd45c797fd329df47b634d0fc7e9f858e9506ed572c64d00332ea5cf388"
|
||||
if ($sha -ne $expected) {
|
||||
throw "Downloaded Caddy root CA SHA256 mismatch. Expected $expected but got $sha"
|
||||
}
|
||||
|
||||
Import-Certificate -FilePath $caPath -CertStoreLocation Cert:\LocalMachine\Root | Out-Null
|
||||
Write-Host "Installed Wheelz Caddy local root CA. SHA256: $sha" -ForegroundColor Green
|
||||
}
|
||||
|
||||
function Get-MachineId {
|
||||
try {
|
||||
$guid = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Cryptography" -Name MachineGuid).MachineGuid
|
||||
if ($guid) { return "windows-$guid" }
|
||||
}
|
||||
catch {}
|
||||
return "windows-$env:COMPUTERNAME"
|
||||
}
|
||||
|
||||
function Normalize-InstallerScript {
|
||||
param([string]$Path, [string]$ServerUrl)
|
||||
$content = Get-Content -Raw -Path $Path
|
||||
$content = $content.Replace("http://192.168.20.232:3000", $ServerUrl.TrimEnd('/'))
|
||||
$content = $content.Replace("https://192.168.20.232:3000", $ServerUrl.TrimEnd('/'))
|
||||
Set-Content -Path $Path -Value $content -Encoding UTF8
|
||||
}
|
||||
|
||||
Assert-Admin
|
||||
$Server = $Server.TrimEnd('/')
|
||||
|
||||
if (-not $TokenKey) { $TokenKey = Read-Host -Prompt "PatchMon auto-enrollment token key" }
|
||||
if (-not $TokenSecret) { $TokenSecret = Read-SecretPlainText -Prompt "PatchMon auto-enrollment token secret" }
|
||||
if (-not $TokenKey) { throw "Token key cannot be empty" }
|
||||
if (-not $TokenSecret) { throw "Token secret cannot be empty" }
|
||||
if (-not $FriendlyName) { $FriendlyName = $env:COMPUTERNAME }
|
||||
|
||||
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
|
||||
|
||||
Install-WheelzCaddyRootCa
|
||||
|
||||
Write-Step "Testing PatchMon HTTPS access"
|
||||
Invoke-WebRequest -Uri $Server -UseBasicParsing | Out-Null
|
||||
|
||||
$machineId = Get-MachineId
|
||||
Write-Step "Enrolling $FriendlyName with PatchMon auto-enrollment token"
|
||||
$enrollHeaders = @{
|
||||
"X-Auto-Enrollment-Key" = $TokenKey
|
||||
"X-Auto-Enrollment-Secret" = $TokenSecret
|
||||
}
|
||||
$enrollBody = @{
|
||||
friendly_name = $FriendlyName
|
||||
machine_id = $machineId
|
||||
} | ConvertTo-Json -Compress
|
||||
|
||||
$enrollResponse = Invoke-PatchMonWebRequest -Uri "$Server/api/v1/auto-enrollment/enroll" -Method "POST" -Headers $enrollHeaders -Body $enrollBody
|
||||
$enrollJson = $enrollResponse.Content | ConvertFrom-Json
|
||||
$hostObj = $enrollJson.host
|
||||
if (-not $hostObj) { throw "PatchMon enrollment response did not contain host credentials." }
|
||||
|
||||
$apiId = $hostObj.api_id
|
||||
$apiKey = $hostObj.api_key
|
||||
if (-not $apiId -or -not $apiKey) { throw "PatchMon enrollment response did not include api_id/api_key." }
|
||||
Write-Host "Host enrolled successfully: $($hostObj.friendly_name)" -ForegroundColor Green
|
||||
|
||||
$installUrl = "$Server/api/v1/hosts/install?os=windows"
|
||||
if ($Force) { $installUrl = "$installUrl&force=true" }
|
||||
$installerPath = Join-Path $env:TEMP "patchmon-install-windows.ps1"
|
||||
Write-Step "Downloading PatchMon Windows agent installer"
|
||||
$installHeaders = @{
|
||||
"X-API-ID" = $apiId
|
||||
"X-API-KEY" = $apiKey
|
||||
}
|
||||
Invoke-PatchMonWebRequest -Uri $installUrl -Headers $installHeaders -OutFile $installerPath | Out-Null
|
||||
Normalize-InstallerScript -Path $installerPath -ServerUrl $Server
|
||||
|
||||
if ($PrintOnly) {
|
||||
Write-Step "Printing generated installer path instead of running it"
|
||||
Write-Host $installerPath
|
||||
exit 0
|
||||
}
|
||||
|
||||
Write-Step "Running PatchMon Windows agent installer"
|
||||
& powershell.exe -NoProfile -ExecutionPolicy Bypass -File $installerPath
|
||||
$exitCode = $LASTEXITCODE
|
||||
if ($exitCode -ne 0) {
|
||||
throw "PatchMon Windows installer exited with code $exitCode"
|
||||
}
|
||||
|
||||
Write-Step "Verifying PatchMon Windows service"
|
||||
$svc = Get-Service | Where-Object { $_.Name -match 'patchmon' -or $_.DisplayName -match 'PatchMon' } | Select-Object -First 1
|
||||
if ($svc) {
|
||||
Write-Host "Service: $($svc.Name) / $($svc.DisplayName) / $($svc.Status)" -ForegroundColor Green
|
||||
}
|
||||
else {
|
||||
Write-Warn "No PatchMon service found yet. Check the installer output above."
|
||||
}
|
||||
|
||||
Write-Step "Windows PatchMon enrollment wrapper completed"
|
||||
Reference in New Issue
Block a user