forked from mtusi2026/mtusi2026lab7
Защита внутренних соединений и сервисных прав
This commit is contained in:
@@ -2,21 +2,46 @@
|
||||
param([string]$EnvFile = '.env')
|
||||
|
||||
$ErrorActionPreference = 'Stop'
|
||||
Set-Location (Join-Path $PSScriptRoot '..')
|
||||
$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 'secrets\openbao' $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
|
||||
$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" }
|
||||
$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) {
|
||||
& docker compose --env-file $EnvFile exec -T -e "BAO_TOKEN=$Token" openbao bao @Arguments 2>$null | Out-Null
|
||||
return $LASTEXITCODE
|
||||
# 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') {
|
||||
|
||||
Reference in New Issue
Block a user