Защита внутренних соединений и сервисных прав

This commit is contained in:
EgorAnti
2026-11-27 09:22:26 +03:00
parent 3c412926e5
commit a7463797e6
15 changed files with 453 additions and 54 deletions
+29 -9
View File
@@ -2,17 +2,37 @@
param([string]$EnvFile = '.env')
$ErrorActionPreference = 'Stop'
Set-Location (Join-Path $PSScriptRoot '..')
function Invoke-Probe([string]$Target, [string]$TlsArguments) {
$script = "printf 'GET /health/ready HTTP/1.1\r\nHost: api\r\nConnection: close\r\n\r\n' | openssl s_client -quiet -connect ${Target}:8443 -servername api -CAfile /run/pki/service-ca/ca.crt $TlsArguments 2>&1"
$output = (& docker compose --env-file $EnvFile run --rm --no-deps --entrypoint /bin/sh nginx -c $script 2>&1)
return ($output -join "`n")
$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."
}
$published = (& docker compose --env-file $EnvFile port api 8443 2>$null) -join "`n"
if (-not [string]::IsNullOrWhiteSpace($published)) {
throw "mTLS bypass: API port is published directly at $published"
function Invoke-Probe([string]$Target, [string]$TlsArguments) {
$probeScript = "printf 'GET /health/ready HTTP/1.1\r\nHost: api\r\nConnection: close\r\n\r\n' | openssl s_client -quiet -connect ${Target}:8443 -servername api -CAfile /run/pki/service-ca/ca.crt $TlsArguments 2>&1"
# Rejected handshakes write to stderr; keep them from becoming terminating errors.
$previous = $ErrorActionPreference
$ErrorActionPreference = 'Continue'
try {
$output = @(& docker compose --env-file $EnvFile run --rm --no-deps --entrypoint /bin/sh nginx -c $probeScript 2>&1) -join "`n"
} finally {
$ErrorActionPreference = $previous
}
return $output
}
# A missing published port is expected: compose returns nonzero and writes
# "no port 8443/tcp ..." to stderr.
$previous = $ErrorActionPreference
$ErrorActionPreference = 'Continue'
try {
$published = @(& docker compose --env-file $EnvFile port api 8443 2>&1) -join "`n"
$portExit = $LASTEXITCODE
} finally {
$ErrorActionPreference = $previous
}
if ($portExit -eq 0 -and $published.Trim() -and $published -notmatch 'no port') {
throw "mTLS bypass: API port is published directly at $($published.Trim())"
}
$replicas = @(& docker compose --env-file $EnvFile ps -q api)