[CmdletBinding()] param([string]$EnvFile = '.env') $ErrorActionPreference = 'Stop' Set-Location (Join-Path $PSScriptRoot '..') function Get-AppRoleToken([string]$Role) { $dir = Join-Path 'secrets\openbao' $Role $roleId = [System.IO.File]::ReadAllText((Join-Path $dir 'role-id')).Trim() $secretId = [System.IO.File]::ReadAllText((Join-Path $dir 'secret-id')).Trim() $body = @{ role_id = $roleId; secret_id = $secretId } | ConvertTo-Json -Compress $token = ($body | & docker compose --env-file $EnvFile exec -T openbao bao write -field=token auth/approle/login - | Out-String).Trim() if($LASTEXITCODE -ne 0 -or [string]::IsNullOrWhiteSpace($token)) { throw "AppRole login failed for $Role" } return $token } function Bao-WithToken([string]$Token,[Parameter(ValueFromRemainingArguments=$true)][string[]]$Arguments) { & docker compose --env-file $EnvFile exec -T -e "BAO_TOKEN=$Token" openbao bao @Arguments 2>$null | Out-Null return $LASTEXITCODE } foreach($role in 'api','worker') { $other = if($role -eq 'api'){'worker'}else{'api'} $token = Get-AppRoleToken $role if((Bao-WithToken $token kv get -mount=secret "messenger/$role") -ne 0) { throw "$role cannot read its own KV path" } if((Bao-WithToken $token kv get -mount=secret "messenger/$other") -eq 0) { throw "$role unexpectedly read the $other KV path" } if((Bao-WithToken $token policy write forbidden /openbao/policies/api-policy.hcl) -eq 0) { throw "$role unexpectedly changed an OpenBao policy" } } Write-Host 'OpenBao policy checks passed: own path allowed; foreign path and policy changes denied.'