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

92 lines
4.8 KiB
PowerShell

[CmdletBinding()]
param(
[string]$EnvFile = '.env',
[switch]$SkipLivePolicy
)
$ErrorActionPreference = 'Stop'
$projectRoot = (Resolve-Path (Join-Path $PSScriptRoot '..')).Path
Set-Location $projectRoot
function Inspect-One([string]$Service) {
$ids = @(& docker compose --env-file $EnvFile ps -q $Service)
if($ids.Count -ne 1){ throw "Expected one running $Service container, found $($ids.Count)" }
return ((& docker inspect $ids[0] | Out-String) | ConvertFrom-Json)[0]
}
function Environment-Names($Inspection) {
return @($Inspection.Config.Env | ForEach-Object { ($_ -split '=',2)[0] })
}
function Assert-NoOperatorMaterial($Inspection,[string]$Service) {
$forbidden = @('BAO_TOKEN','VAULT_TOKEN','OPENBAO_ROOT_TOKEN','ROOT_TOKEN','UNSEAL_KEY','UNSEAL_KEYS')
$present = @(Environment-Names $Inspection | Where-Object { $forbidden -contains $_ })
if($present.Count -gt 0){ throw "$Service contains operator-only environment variables: $($present -join ', ')" }
}
function Assert-RuntimeKeys([string]$ContainerId,[string]$Role) {
$expected = @('DATABASE_PASSWORD','RABBITMQ_PASSWORD','REDIS_PASSWORD','S3_ACCESS_KEY_ID','S3_SECRET_ACCESS_KEY') | Sort-Object
$actual = @(& docker exec $ContainerId sh -c "cut -d= -f1 /run/openbao/runtime.env" 2>$null | Where-Object { $_ } | Sort-Object)
if($LASTEXITCODE -ne 0){ throw "$Role cannot read its rendered runtime secret file" }
if(($actual -join ',') -ne ($expected -join ',')){
throw "$Role runtime secret file contains unexpected key names"
}
}
$status = ((& docker compose --env-file $EnvFile exec -T openbao bao status -format=json | Out-String) | ConvertFrom-Json)
if(-not $status.initialized -or $status.sealed){ throw 'OpenBao must be initialized and unsealed for this test' }
if($status.storage_type -ne 'raft'){ throw "OpenBao storage is $($status.storage_type), expected raft" }
$openbao = Inspect-One 'openbao'
$commandLine = (@($openbao.Config.Entrypoint) + @($openbao.Config.Cmd)) -join ' '
if($commandLine -match '(^|\s)(dev|server\s+-dev)(\s|$)') { throw 'OpenBao is running in dev mode' }
$storageMount = @($openbao.Mounts | Where-Object { $_.Destination -eq '/openbao/file' })
if($storageMount.Count -ne 1 -or $storageMount[0].Type -ne 'volume' -or -not $storageMount[0].RW){
throw 'OpenBao raft storage is not backed by one writable named volume at /openbao/file'
}
Assert-NoOperatorMaterial $openbao 'openbao'
$agents = @{}
foreach($role in 'api','worker'){
$agent = Inspect-One "openbao-agent-$role"
Assert-NoOperatorMaterial $agent "openbao-agent-$role"
$bootstrap = @($agent.Mounts | Where-Object { $_.Destination -eq '/run/bootstrap' })
$rendered = @($agent.Mounts | Where-Object { $_.Destination -eq '/run/rendered' })
if($bootstrap.Count -ne 1 -or $bootstrap[0].RW){ throw "$role agent bootstrap mount is missing or writable" }
if($rendered.Count -ne 1 -or $rendered[0].Type -ne 'volume'){ throw "$role agent has no dedicated rendered volume" }
$agents[$role] = @{ bootstrap = $bootstrap[0].Source; rendered = $rendered[0].Name }
}
if($agents.api.bootstrap -eq $agents.worker.bootstrap -or $agents.api.rendered -eq $agents.worker.rendered){
throw 'API and worker OpenBao agents share bootstrap or rendered-secret storage'
}
foreach($service in 'api','worker'){
$ids = @(& docker compose --env-file $EnvFile ps -q $service)
if($ids.Count -lt 1){ throw "No running $service container" }
foreach($id in $ids){
$inspection = ((& docker inspect $id | Out-String) | ConvertFrom-Json)[0]
Assert-NoOperatorMaterial $inspection $service
$runtime = @($inspection.Mounts | Where-Object { $_.Destination -eq '/run/openbao' })
if($runtime.Count -ne 1 -or $runtime[0].RW){ throw "$service runtime secret mount is missing or writable" }
if(@($inspection.Mounts | Where-Object { $_.Destination -in '/run/bootstrap','/openbao/file' }).Count -gt 0){
throw "$service can access AppRole bootstrap or OpenBao raft storage"
}
Assert-RuntimeKeys $id $service
}
}
$operatorArtifacts = @(Get-ChildItem -LiteralPath (Join-Path $projectRoot 'secrets') -Recurse -File -ErrorAction SilentlyContinue |
Where-Object { $_.Name -match '(?i)(root.*token|unseal)' })
if($operatorArtifacts.Count -gt 0){ throw 'Root token or unseal material exists inside the project secrets tree' }
if(-not $SkipLivePolicy) {
& "$PSScriptRoot\test-openbao-policies.ps1" -EnvFile $EnvFile
}
Write-Host 'PASS: OpenBao uses persistent raft storage outside dev mode; API/worker use separate AppRoles and read-only rendered files; operator material is absent.'
if($SkipLivePolicy) {
Write-Host 'INFO: live scoped-policy probe skipped; it requires a non-expired 24 h AppRole SecretID.'
} else {
Write-Host 'PASS: scoped OpenBao allow/deny policies were verified with fresh AppRole credentials.'
}