Files
ianitor/deploy/windows/install.ps1
T

209 lines
9.4 KiB
PowerShell

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
#
# Ianitor Domi on Windows Server, Caddy mode (Fabrica docs/32_coherence.md §2.5, T4).
# Pingora's Windows support is preliminary upstream, so Windows runs ianitor-control with Caddy
# as the data plane: same UI, same TOML, compiled to Caddy JSON, kept honest by the
# differential suite (which runs on Windows with Windows paths).
#
# From the unpacked release zip, in an elevated PowerShell:
#
# .\install.ps1 -AdminHost ianitor.example.org [-Tls letsencrypt|domi] [-DomiRoot <crt>]
# [-PreviewDomain preview.example.org -PreviewUpstreams "10.0.0.0/8,127.0.0.1:20000-29999"]
# [-RoutesRemote https://github.com/team/routes.git -RoutesHost github]
#
# It installs the binaries to %ProgramFiles%\Ianitor, Caddy (pinned, SHA-512 checked) next to
# them, the state to %ProgramData%\Ianitor (ACL: Administrators, SYSTEM, LocalService), writes
# control.toml once, registers three services as LocalService (IanitorCaddy, IanitorControl,
# IanitorGuard; restart on failure), starts them and prints the first admin's sign-in link.
# -NoService runs the same setup but starts nothing (tests, or a manual start).
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)][string]$AdminHost,
[ValidateSet('letsencrypt', 'domi')][string]$Tls = 'letsencrypt',
[string]$DomiRoot = '',
[string]$PreviewDomain = '',
[string]$PreviewUpstreams = '',
[ValidateSet('', 'letsencrypt', 'domi')][string]$PreviewTls = '',
[string]$RoutesRemote = '',
[ValidateSet('gitea', 'github', 'gitlab')][string]$RoutesHost = 'gitea',
[string]$Root = (Join-Path $env:ProgramData 'Ianitor'),
[string]$Bin = (Join-Path $env:ProgramFiles 'Ianitor'),
[int]$HttpPort = 80,
[int]$HttpsPort = 443,
[int]$ApiPort = 7878,
[int]$CaddyAdminPort = 2019,
[string]$Caddy = '',
[switch]$NoService
)
$ErrorActionPreference = 'Stop'
$CaddyVersion = '2.11.7'
$CaddySha512 = 'c308154504e53755958ffc62d8a2657ab17eefd6dd25c45dbb2c3d93d29519971f77ca652f7d0f2bcb7f980b9ba3822478865e29969d0272d7613550a43152b1'
function Fail($m) { Write-Error "ianitor install: $m"; exit 1 }
function Plain($v, $name) {
if ($v -match '["\\''`$\r\n]') { Fail "$name contains a character that is not allowed" }
}
# Paths in TOML with forward slashes (control and Caddy read C:/ProgramData/... as is).
function Fwd($p) { return ($p -replace '\\', '/') }
if (-not $NoService) {
$me = [Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()
if (-not $me.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { Fail 'run it in an elevated PowerShell (Run as administrator)' }
}
Plain $AdminHost 'AdminHost'; Plain $PreviewDomain 'PreviewDomain'; Plain $PreviewUpstreams 'PreviewUpstreams'
Plain $RoutesRemote 'RoutesRemote'; Plain $DomiRoot 'DomiRoot'
$git = (Get-Command git.exe -ErrorAction SilentlyContinue)
if (-not $git) { Fail 'install Git for Windows first (https://git-scm.com/download/win); control keeps the routes in git' }
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
if (-not (Test-Path (Join-Path $here 'ianitor-control.exe'))) { Fail "ianitor-control.exe is not next to install.ps1 ($here)" }
# ---------------------------------------------------------------- files
New-Item -ItemType Directory -Force $Bin | Out-Null
Copy-Item (Join-Path $here 'ianitor-control.exe') $Bin -Force
if (Test-Path (Join-Path $here 'web')) {
if (Test-Path (Join-Path $Bin 'web')) { Remove-Item -Recurse -Force (Join-Path $Bin 'web') }
Copy-Item -Recurse (Join-Path $here 'web') (Join-Path $Bin 'web')
}
$caddyExe = Join-Path $Bin 'caddy.exe'
if ($Caddy) {
Copy-Item $Caddy $caddyExe -Force
} elseif (-not (Test-Path $caddyExe)) {
$zip = Join-Path $env:TEMP "caddy_$CaddyVersion.zip"
$url = "https://github.com/caddyserver/caddy/releases/download/v$CaddyVersion/caddy_${CaddyVersion}_windows_amd64.zip"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -UseBasicParsing -Uri $url -OutFile $zip
$got = (Get-FileHash -Algorithm SHA512 $zip).Hash.ToLower()
if ($got -ne $CaddySha512) { Remove-Item $zip; Fail "Caddy $CaddyVersion does not match its pinned SHA-512" }
$x = Join-Path $env:TEMP "caddy_$CaddyVersion"
Expand-Archive -Force $zip $x
Copy-Item (Join-Path $x 'caddy.exe') $caddyExe -Force
Remove-Item -Recurse -Force $zip, $x
}
foreach ($d in 'control', 'edge', 'caddy', 'certs', 'caddy-state', 'logs', 'credentials') {
New-Item -ItemType Directory -Force (Join-Path $Root $d) | Out-Null
}
# Only Administrators, SYSTEM and the services' account (LocalService) see the state.
icacls $Root /inheritance:r /grant:r '*S-1-5-32-544:(OI)(CI)F' '*S-1-5-18:(OI)(CI)F' '*S-1-5-19:(OI)(CI)M' | Out-Null
function Secret($name) {
$f = Join-Path $Root "credentials\$name"
if (-not (Test-Path $f) -or (Get-Item $f).Length -eq 0) {
$b = New-Object byte[] 32
[Security.Cryptography.RandomNumberGenerator]::Create().GetBytes($b)
[IO.File]::WriteAllText($f, (($b | ForEach-Object { $_.ToString('x2') }) -join ''))
}
}
Secret 'share-link-key'; Secret 'pki-password'
$engine = Join-Path $Root 'engine'
if (-not (Test-Path $engine)) { [IO.File]::WriteAllText($engine, "caddy`n") }
$rev = Join-Path $Root 'edge\share-revocations.json'
if (-not (Test-Path $rev)) { [IO.File]::WriteAllText($rev, '{"epoch":0,"deny":[]}') }
# Caddy starts with only its admin API; control loads the rest and keeps caddy.json current.
$caddyJson = Join-Path $Root 'caddy\caddy.json'
if (-not (Test-Path $caddyJson)) {
[IO.File]::WriteAllText($caddyJson, "{`"admin`":{`"listen`":`"127.0.0.1:$CaddyAdminPort`"}}")
}
# ---------------------------------------------------------------- control.toml (once)
$config = Join-Path $Root 'control.toml'
if (Test-Path $config) {
Write-Host "ianitor install: $config exists; leaving it as it is."
} else {
$R = Fwd $Root
if (-not $PreviewTls) { $PreviewTls = $Tls }
$routes = ''
if ($RoutesRemote) {
$user = @{ github = 'x-access-token'; gitlab = 'oauth2'; gitea = 'ianitor' }[$RoutesHost]
$label = @{ github = 'GitHub'; gitlab = 'GitLab'; gitea = 'Gitea' }[$RoutesHost]
$routes = "remote = `"$RoutesRemote`"`nremote_name = `"$label`"`npush_username = `"$user`"`npush_credential = `"routes-token`""
}
$pki = ''
if ($Tls -eq 'domi' -or $PreviewTls -eq 'domi') {
if (-not $DomiRoot) { Fail 'tls domi needs -DomiRoot (the Domi root certificate; docs/pki.md)' }
$pki = "[pki]`nroot_cert = `"$(Fwd $DomiRoot)`""
}
$previews = ''
if ($PreviewDomain) {
$list = ($PreviewUpstreams -split ',' | Where-Object { $_.Trim() } | ForEach-Object { "`"$($_.Trim())`"" }) -join ', '
$previews = "[previews]`ndomain = `"$PreviewDomain`"`ntls = `"$PreviewTls`"`nupstreams = [$list]"
}
$text = @"
# Written by Ianitor's Windows installer on $(Get-Date -Format yyyy-MM-dd). Edit freely; the
# installer never rewrites it. Secrets are files in $R/credentials, never values here.
listen = "127.0.0.1:$ApiPort"
state_dir = "$R/control"
credentials_dir = "$R/credentials"
git = "$(Fwd $git.Source)"
[routes]
repo = "$R/routes.git"
$routes
[edge]
config = "$R/edge/edge.json"
share_revocations = "$R/edge/share-revocations.json"
[caddy]
config = "$R/caddy/caddy.json"
[compile]
http_listen = "0.0.0.0:$HttpPort"
https_listen = "0.0.0.0:$HttpsPort"
cert_dir = "$R/certs"
state_dir = "$R/caddy-state"
access_log = "$R/logs/access.log"
caddy_credentials_dir = "$R/credentials"
caddy_admin = "127.0.0.1:$CaddyAdminPort"
[engine]
file = "$R/engine"
units = "none"
[ui]
hostname = "$AdminHost"
static_dir = "$(Fwd (Join-Path $Bin 'web'))"
admin_tls = "$Tls"
$pki
$previews
"@
[IO.File]::WriteAllText($config, $text)
Write-Host "ianitor install: wrote $config"
}
& (Join-Path $Bin 'ianitor-control.exe') check --config $config
if ($LASTEXITCODE -ne 0) { Fail "the config does not check out: $config" }
if ($NoService) {
Write-Host "ianitor install: set up in $Root; no services started (-NoService)."
exit 0
}
# ---------------------------------------------------------------- services
$ctl = Join-Path $Bin 'ianitor-control.exe'
$log = Join-Path $Root 'logs'
$services = @(
@{ Name = 'IanitorCaddy'; Display = 'Ianitor Caddy (data plane)'; Path = "`"$caddyExe`" run --config `"$caddyJson`"" },
@{ Name = 'IanitorControl'; Display = 'Ianitor control (API and UI on loopback)'; Path = "`"$ctl`" service control --config `"$config`" --log-file `"$log\control.log`"" },
@{ Name = 'IanitorGuard'; Display = 'Ianitor Caddy trial guard'; Path = "`"$ctl`" service caddy-guard --config `"$config`" --log-file `"$log\guard.log`"" }
)
foreach ($s in $services) {
if (Get-Service $s.Name -ErrorAction SilentlyContinue) {
Stop-Service $s.Name -Force -ErrorAction SilentlyContinue
sc.exe config $s.Name binPath= $s.Path | Out-Null
} else {
sc.exe create $s.Name binPath= $s.Path start= auto obj= 'NT AUTHORITY\LocalService' DisplayName= $s.Display | Out-Null
}
sc.exe failure $s.Name reset= 86400 actions= restart/2000/restart/5000/restart/30000 | Out-Null
}
foreach ($s in $services) { Start-Service $s.Name }
Write-Host ''
Write-Host 'Ianitor is running (Caddy mode). Open this link to sign in as the first admin:'
& $ctl team setup-link --config $config