forked from mtusi2026/mtusi2026lab7
57 lines
2.6 KiB
PowerShell
57 lines
2.6 KiB
PowerShell
[CmdletBinding()]
|
|
param(
|
|
[string]$EnvFile = '.env',
|
|
[string]$OldCertificate = 'secrets/pki/revoked/lb/tls.crt',
|
|
[string]$OldKey = 'secrets/pki/revoked/lb/tls.key',
|
|
[string]$NewCertificate = 'secrets/pki/lb/tls.crt',
|
|
[string]$NewKey = 'secrets/pki/lb/tls.key'
|
|
)
|
|
|
|
$ErrorActionPreference = 'Stop'
|
|
$projectRoot = (Resolve-Path (Join-Path $PSScriptRoot '..')).Path
|
|
Set-Location $projectRoot
|
|
foreach ($path in @($OldCertificate, $OldKey, $NewCertificate, $NewKey)) {
|
|
if (-not (Test-Path -LiteralPath $path)) { throw "Missing certificate material: $path" }
|
|
}
|
|
|
|
function Convert-ToContainerPath([string]$Path) {
|
|
if ($Path -match '^(secrets[\\/])pki[\\/](.+)$') { return '/run/pki/' + $Matches[2].Replace('\','/') }
|
|
return $Path.Replace('\','/')
|
|
}
|
|
$OldCertificate = Convert-ToContainerPath $OldCertificate
|
|
$OldKey = Convert-ToContainerPath $OldKey
|
|
$NewCertificate = Convert-ToContainerPath $NewCertificate
|
|
$NewKey = Convert-ToContainerPath $NewKey
|
|
|
|
foreach ($path in @($OldCertificate, $OldKey, $NewCertificate, $NewKey)) {
|
|
if ($path -notlike '/run/pki/*') { throw "Certificate path must be under secrets/pki: $path" }
|
|
}
|
|
|
|
function Invoke-Probe([string]$Target, [string]$Certificate, [string]$Key) {
|
|
$probeScript = "printf 'GET /health/ready HTTP/1.1\r\nHost: api\r\nConnection: close\r\n\r\n' | openssl s_client -quiet -connect ${Target}:8443 -servername api -CAfile /run/pki/service-ca/ca.crt -cert ${Certificate} -key ${Key} 2>&1"
|
|
$previous = $ErrorActionPreference
|
|
$ErrorActionPreference = 'Continue'
|
|
try {
|
|
return @(& docker compose --env-file $EnvFile run --rm --no-deps --entrypoint /bin/sh nginx -c $probeScript 2>&1) -join "`n"
|
|
} finally {
|
|
$ErrorActionPreference = $previous
|
|
}
|
|
}
|
|
|
|
$replicas = @(& docker compose --env-file $EnvFile ps -q api)
|
|
if ($replicas.Count -lt 2) { throw 'At least two running API replicas are required for rotation evidence' }
|
|
|
|
foreach ($replica in $replicas) {
|
|
$target = (& docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $replica).Trim()
|
|
if ([string]::IsNullOrWhiteSpace($target)) { throw "No internal address found for API replica $replica" }
|
|
$oldResult = Invoke-Probe $target $OldCertificate $OldKey
|
|
if ($oldResult -notmatch 'HTTP/1.1 403') {
|
|
throw "Revoked leaf was accepted by API replica $replica"
|
|
}
|
|
$newResult = Invoke-Probe $target $NewCertificate $NewKey
|
|
if ($newResult -notmatch 'HTTP/1.1 200') {
|
|
throw "Rotated leaf was rejected by API replica $replica"
|
|
}
|
|
}
|
|
Write-Host "PASS: $($replicas.Count) API replicas reject the revoked LB leaf with 403 and accept the rotated LB leaf with 200."
|