[CmdletBinding()] param([string]$EnvFile = '.env') $ErrorActionPreference = 'Stop' $PSNativeCommandUseErrorActionPreference = $false $projectRoot = (Resolve-Path (Join-Path $PSScriptRoot '..')).Path Set-Location $projectRoot if ([string]::IsNullOrWhiteSpace($env:BAO_TOKEN)) { throw 'Set BAO_TOKEN to the operator root token only for this bootstrap process.' } $values = @{} foreach($line in [System.IO.File]::ReadAllLines((Resolve-Path $EnvFile))) { if($line -match '^(?[A-Z][A-Z0-9_]*)=(?.*)$') { $values[$Matches.name]=$Matches.value } } function Require-Value([string]$Name) { if(-not $values.ContainsKey($Name) -or [string]::IsNullOrWhiteSpace($values[$Name])) { throw "Missing $Name in $EnvFile" } return $values[$Name] } function Bao([Parameter(ValueFromRemainingArguments=$true)][string[]]$Arguments) { & docker compose --env-file $EnvFile exec -T -e "BAO_TOKEN=$env:BAO_TOKEN" openbao bao @Arguments if($LASTEXITCODE -ne 0){ throw "OpenBao command failed: bao $($Arguments -join ' ')" } } function Revoke-AppRoleSecretIds([string]$RoleName) { # Windows PowerShell 5 wraps native stderr as NativeCommandError. OpenBao # legitimately returns exit code 2 when this LIST collection is empty, so # suppress only that invocation and inspect its exit code explicitly. $previousErrorAction = $ErrorActionPreference try { $ErrorActionPreference = 'SilentlyContinue' $json = (& docker compose --env-file $EnvFile exec -T -e "BAO_TOKEN=$env:BAO_TOKEN" openbao bao list -format=json "auth/approle/role/$RoleName/secret-id" 2>$null | Out-String).Trim() $code = $LASTEXITCODE } finally { $ErrorActionPreference = $previousErrorAction } if($code -eq 2){ return } # OpenBao returns 2 when the collection is empty. if($code -ne 0){ throw "Failed to list existing SecretID accessors for $RoleName" } $parsed = $json | ConvertFrom-Json $accessors = if($parsed.PSObject.Properties.Name -contains 'keys'){ @($parsed.keys) } else { @($parsed) } foreach($accessor in $accessors) { if(-not [string]::IsNullOrWhiteSpace($accessor)) { Bao write "auth/approle/role/$RoleName/secret-id-accessor/destroy" "secret_id_accessor=$accessor" | Out-Null } } } $secretMounts = (Bao secrets list -format=json | Out-String) | ConvertFrom-Json if($secretMounts.PSObject.Properties.Name -notcontains 'secret/') { Bao secrets enable -path=secret -version=2 kv | Out-Null } $authMounts = (Bao auth list -format=json | Out-String) | ConvertFrom-Json if($authMounts.PSObject.Properties.Name -notcontains 'approle/') { Bao auth enable approle | Out-Null } Bao policy write messenger-api /openbao/policies/api-policy.hcl | Out-Null Bao policy write messenger-worker /openbao/policies/worker-policy.hcl | Out-Null Bao write auth/approle/role/messenger-api token_policies=messenger-api token_ttl=15m token_max_ttl=1h token_num_uses=0 secret_id_ttl=24h secret_id_num_uses=0 | Out-Null Bao write auth/approle/role/messenger-worker token_policies=messenger-worker token_ttl=15m token_max_ttl=1h token_num_uses=0 secret_id_ttl=24h secret_id_num_uses=0 | Out-Null $api = @{ database_password = Require-Value DATABASE_API_PASSWORD redis_password = Require-Value REDIS_API_PASSWORD rabbitmq_password = Require-Value RABBITMQ_API_PASSWORD s3_access_key_id = Require-Value S3_ACCESS_KEY_ID s3_secret_access_key = Require-Value S3_SECRET_ACCESS_KEY } | ConvertTo-Json -Compress $worker = @{ database_password = Require-Value DATABASE_WORKER_PASSWORD redis_password = Require-Value REDIS_WORKER_PASSWORD rabbitmq_password = Require-Value RABBITMQ_WORKER_PASSWORD s3_access_key_id = Require-Value S3_WORKER_ACCESS_KEY_ID s3_secret_access_key = Require-Value S3_WORKER_SECRET_ACCESS_KEY } | ConvertTo-Json -Compress $api | & docker compose --env-file $EnvFile exec -T -e "BAO_TOKEN=$env:BAO_TOKEN" openbao bao kv put -mount=secret messenger/api - | Out-Null if($LASTEXITCODE -ne 0){ throw 'Failed to write API secrets' } $worker | & docker compose --env-file $EnvFile exec -T -e "BAO_TOKEN=$env:BAO_TOKEN" openbao bao kv put -mount=secret messenger/worker - | Out-Null if($LASTEXITCODE -ne 0){ throw 'Failed to write worker secrets' } foreach($role in 'api','worker') { $roleName = "messenger-$role" $dir = Join-Path $projectRoot "secrets\openbao\$role" New-Item -ItemType Directory -Path $dir -Force | Out-Null # A previous interrupted bootstrap may have issued a SecretID without # persisting it. Revoke each old credential by its non-secret accessor. Revoke-AppRoleSecretIds $roleName $roleId = (Bao read -field=role_id "auth/approle/role/$roleName/role-id" | Out-String).Trim() $secretId = (Bao write -field=secret_id -f "auth/approle/role/$roleName/secret-id" | Out-String).Trim() [System.IO.File]::WriteAllText((Join-Path $dir 'role-id'),$roleId,[System.Text.Encoding]::ASCII) [System.IO.File]::WriteAllText((Join-Path $dir 'secret-id'),$secretId,[System.Text.Encoding]::ASCII) } Write-Host 'OpenBao policies, AppRoles, KV secrets and agent bootstrap files are ready. Secret values were not printed.'