Files
mtusi2026lab7/scripts/test-openbao-rotation-boundaries.ps1
T

81 lines
4.4 KiB
PowerShell

[CmdletBinding()]
param(
[string]$EnvFile = '.env',
[string]$ApplicationImage = 'hla-lab7-app:local',
[int]$Port = 8443
)
$ErrorActionPreference = 'Stop'
$projectRoot = (Resolve-Path (Join-Path $PSScriptRoot '..')).Path
Set-Location $projectRoot
$publicCa = Join-Path $projectRoot 'secrets\pki\public-ca\ca.crt'
if (-not (Test-Path -LiteralPath $publicCa)) { throw "Missing public CA: $publicCa" }
function Invoke-Quiet([scriptblock]$Command) {
$previous = $ErrorActionPreference
$ErrorActionPreference = 'Continue'
try {
$output = & $Command 2>&1 | Out-String
$code = $LASTEXITCODE
} finally {
$ErrorActionPreference = $previous
}
return [pscustomobject]@{ Code = $code; Output = $output }
}
foreach ($name in 'agent-api.hcl','agent-worker.hcl') {
$config = [System.IO.File]::ReadAllText((Join-Path $projectRoot "infra\openbao\$name"))
if ($config -notmatch 'static_secret_render_interval\s*=\s*"5m"') { throw "$name does not renew rendered secrets every 5 minutes" }
if ($config -notmatch 'exit_on_retry_failure\s*=\s*false') { throw "$name exits when OpenBao retries fail" }
}
$serverConfig = [System.IO.File]::ReadAllText((Join-Path $projectRoot 'infra\openbao\openbao.hcl'))
if ($serverConfig -notmatch 'default_lease_ttl\s*=\s*"15m"' -or
$serverConfig -notmatch 'max_lease_ttl\s*=\s*"1h"') {
throw 'OpenBao lease TTLs are not bounded'
}
$imageConfig = (Invoke-Quiet { docker image inspect --format '{{range .Config.Env}}{{println .}}{{end}}' $ApplicationImage }).Output
if ($imageConfig -match '(?i)BAO_TOKEN|UNSEAL|ROOT_TOKEN|SECRET_ID') {
throw 'Application image environment contains an operator credential name'
}
Invoke-Quiet { docker run --rm --entrypoint sh $ApplicationImage -c 'test ! -e /app/.env && test ! -e /app/secrets && test ! -e /app/runtime.env && ! find /app -type f \( -name "*.key" -o -name "*.p12" -o -name "*.jks" \) -print -quit | grep -q .' } | Out-Null
if ($LASTEXITCODE -ne 0) { throw 'Application image contains forbidden secret artifacts' }
# The operator seals OpenBao (or stops the server) with the unseal shares held
# offline. This script verifies the resulting boundary; it never handles the
# shares itself and never tries to unseal.
$statusDuringSeal = (Invoke-Quiet { docker compose --env-file $EnvFile exec -T openbao bao status }).Output
if ($statusDuringSeal -notmatch 'Sealed\s+true') {
throw 'Run this check while OpenBao is sealed so the boundary is actually exercised'
}
foreach ($service in 'openbao-agent-api','openbao-agent-worker') {
$running = (Invoke-Quiet { docker inspect --format '{{.State.Running}}' "hla-lab7-$($service)-1" }).Output.Trim()
if ($running -ne 'true') { throw "$service stopped while OpenBao was unavailable" }
}
$agentId = (Invoke-Quiet { docker compose --env-file $EnvFile ps -q openbao-agent-api }).Output.Trim()
$keys = (Invoke-Quiet { docker exec $agentId sh -c 'test -s /run/rendered/runtime.env && cut -d= -f1 /run/rendered/runtime.env | sort | tr "\n" ","' }).Output.Trim()
if ($keys -notmatch 'DATABASE_PASSWORD' -or $keys -notmatch 'REDIS_PASSWORD') {
throw 'Cached rendered runtime secrets are unavailable during the seal test'
}
$health = Invoke-Quiet { curl.exe -sS -o NUL -w '%{http_code}' --cacert $publicCa --ssl-no-revoke --connect-timeout 5 --max-time 10 "https://localhost:$Port/health/ready" }
if ($health.Output.Trim() -ne '200') { throw 'Application readiness failed while OpenBao was unavailable' }
$watch = [Diagnostics.Stopwatch]::StartNew()
$fresh = Invoke-Quiet {
docker run --rm --entrypoint messenger-entrypoint `
-e OPENBAO_RENDERED_ENV=/run/missing/runtime.env `
-e OPENBAO_STARTUP_TIMEOUT_SECONDS=2 $ApplicationImage
}
$watch.Stop()
if ($fresh.Code -eq 0 -or $fresh.Output -notmatch 'did not render runtime credentials') {
throw 'Fresh startup did not fail closed on a missing rendered secret file'
}
if ($watch.Elapsed.TotalSeconds -gt 10) { throw 'Fresh startup exceeded the bounded fail-closed deadline' }
$elapsed = [math]::Round($watch.Elapsed.TotalSeconds, 1)
Write-Host 'PASS: OpenBao seal/unavailability does not stop agents or running API; cached runtime secrets keep readiness at 200.'
Write-Host "PASS: fresh startup with no rendered env failed closed in ${elapsed}s (configured deadline 2s)."
Write-Host 'PASS: AppRole token TTL is 15m/1h, templates renew every 5m, and no permanent operator secret is embedded in the image.'