56 lines
2.5 KiB
PowerShell
56 lines
2.5 KiB
PowerShell
[CmdletBinding()]
|
|
param([string]$EnvFile = '.env')
|
|
|
|
$ErrorActionPreference = 'Stop'
|
|
$projectRoot = (Resolve-Path (Join-Path $PSScriptRoot '..')).Path
|
|
Set-Location $projectRoot
|
|
if(-not (Test-Path (Join-Path $projectRoot 'compose.yaml'))) {
|
|
throw "Run the repository copy of this script: $projectRoot does not contain compose.yaml."
|
|
}
|
|
|
|
function Get-AppRoleToken([string]$Role) {
|
|
$dir = Join-Path $projectRoot (Join-Path 'secrets\openbao' $Role)
|
|
foreach($name in 'role-id','secret-id') {
|
|
$path = Join-Path $dir $name
|
|
if(-not (Test-Path -LiteralPath $path)) {
|
|
throw "Missing $path. Run .\scripts\bootstrap-openbao.ps1 with BAO_TOKEN first."
|
|
}
|
|
}
|
|
$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
|
|
$previous = $ErrorActionPreference
|
|
$ErrorActionPreference = 'Continue'
|
|
try {
|
|
$token = ($body | & docker compose --env-file $EnvFile exec -T openbao bao write -field=token auth/approle/login - 2>&1 | Out-String).Trim()
|
|
$loginExit = $LASTEXITCODE
|
|
} finally {
|
|
$ErrorActionPreference = $previous
|
|
}
|
|
if($loginExit -ne 0 -or [string]::IsNullOrWhiteSpace($token)) { throw "AppRole login failed for $Role" }
|
|
return $token
|
|
}
|
|
|
|
function Bao-WithToken([string]$Token,[Parameter(ValueFromRemainingArguments=$true)][string[]]$Arguments) {
|
|
# Expected denials write to stderr; do not let them become terminating errors.
|
|
$previous = $ErrorActionPreference
|
|
$ErrorActionPreference = 'Continue'
|
|
try {
|
|
& docker compose --env-file $EnvFile exec -T -e "BAO_TOKEN=$Token" openbao bao @Arguments 2>&1 | Out-Null
|
|
$exitCode = $LASTEXITCODE
|
|
} finally {
|
|
$ErrorActionPreference = $previous
|
|
}
|
|
return $exitCode
|
|
}
|
|
|
|
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.'
|