Закрыть внешний вход TLS и выдавать секреты через OpenBao.

LB ходит в API только с клиентским сертификатом нужной identity, остальные связи проверяют серверный сертификат, root token в приложение не попадает.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-11-26 19:48:38 +03:00
co-authored by Cursor
parent fb566d874c
commit 9b501b2020
33 changed files with 1243 additions and 221 deletions
+12
View File
@@ -0,0 +1,12 @@
#!/bin/sh
set -eu
python -m app.main &
app_pid=$!
python -m app.tls_proxy &
proxy_pid=$!
while kill -0 "$app_pid" 2>/dev/null && kill -0 "$proxy_pid" 2>/dev/null; do
sleep 1
done
kill "$app_pid" "$proxy_pid" 2>/dev/null || true
wait || true
exit 1
+100
View File
@@ -0,0 +1,100 @@
$ErrorActionPreference = "Stop"
$Root = Split-Path -Parent $PSScriptRoot
Set-Location $Root
function New-Secret([string]$Name) {
$path = Join-Path $Root "secrets\$Name"
if (-not (Test-Path $path)) {
$bytes = New-Object byte[] 24
$rng = [System.Security.Cryptography.RandomNumberGenerator]::Create()
$rng.GetBytes($bytes)
$text = -join ($bytes | ForEach-Object { $_.ToString("x2") })
[System.IO.File]::WriteAllText($path, $text)
}
}
New-Item -ItemType Directory -Force -Path (Join-Path $Root "secrets") | Out-Null
New-Item -ItemType Directory -Force -Path (Join-Path $Root "certs") | Out-Null
New-Item -ItemType Directory -Force -Path (Join-Path $Root "evidence") | Out-Null
@(
"db_super_password", "db_api_password", "db_worker_password", "redis_password",
"rabbit_api_password", "rabbit_worker_password", "grafana_password",
"s3_admin_secret", "s3_api_secret", "s3_worker_secret"
) | ForEach-Object { New-Secret $_ }
foreach ($pair in @(
@("s3_admin_access", "s3admin"),
@("s3_api_access", "s3api"),
@("s3_worker_access", "s3worker")
)) {
$path = Join-Path $Root ("secrets\" + $pair[0])
if (-not (Test-Path $path)) {
$suffix = ([guid]::NewGuid().ToString("N")).Substring(0, 12)
[System.IO.File]::WriteAllText($path, $pair[1] + $suffix)
}
}
foreach ($name in @("api", "worker", "migrate")) {
foreach ($suffix in @("role_id", "secret_id")) {
$path = Join-Path $Root "secrets\$name.$suffix"
if (-not (Test-Path $path)) {
[System.IO.File]::WriteAllText($path, "pending")
}
}
}
if (-not (Test-Path (Join-Path $Root ".env"))) {
Copy-Item (Join-Path $Root ".env.example") (Join-Path $Root ".env")
}
Get-ChildItem -Path (Join-Path $Root "scripts"), (Join-Path $Root "infra") -Recurse -Include *.sh,*.cfg,*.hcl |
ForEach-Object {
$text = [System.IO.File]::ReadAllText($_.FullName) -replace "`r`n", "`n" -replace "`r", "`n"
$utf8 = New-Object System.Text.UTF8Encoding $false
[System.IO.File]::WriteAllText($_.FullName, $text, $utf8)
}
Write-Host "creating certificates"
docker run --rm -v "${Root}\certs:/work" -v "${Root}\scripts\gen-certs.sh:/gen-certs.sh:ro" alpine:3.21.3 sh /gen-certs.sh
if ($LASTEXITCODE -ne 0) { throw "certificate generation failed" }
Write-Host "building application image"
docker compose --env-file .env build
if ($LASTEXITCODE -ne 0) { throw "image build failed" }
Write-Host "starting data services"
docker compose --env-file .env up -d postgres redis rabbitmq rabbit-mtls s3 s3-tls openbao otel-collector otel-mtls prometheus loki tempo grafana
if ($LASTEXITCODE -ne 0) { throw "data services failed to start" }
Write-Host "bootstrapping OpenBao"
docker compose --env-file .env --profile bootstrap run --rm openbao-bootstrap
if ($LASTEXITCODE -ne 0) { throw "OpenBao bootstrap failed" }
Write-Host "applying schema"
docker compose --env-file .env up -d migrate
if ($LASTEXITCODE -ne 0) { throw "migrate failed to start" }
$deadline = (Get-Date).AddMinutes(3)
do {
Start-Sleep -Seconds 2
$state = docker compose --env-file .env ps --format json migrate
if ($state -match '"ExitCode":0' -or $state -match '"ExitCode": 0') { break }
if ($state -match '"ExitCode":' -and $state -notmatch '"ExitCode":0' -and $state -notmatch '"State":"running"' -and $state -match '"ExitCode":[1-9]') {
throw "migrate failed"
}
} while ((Get-Date) -lt $deadline)
Write-Host "granting database roles"
docker compose --env-file .env exec -T postgres sh /etc/postgres/grant.sh
if ($LASTEXITCODE -ne 0) { throw "database grants failed" }
Write-Host "creating bucket"
docker compose --env-file .env up -d s3-init
if ($LASTEXITCODE -ne 0) { throw "s3-init failed to start" }
Write-Host "starting API and worker"
docker compose --env-file .env up -d --scale api=2 --no-recreate api worker lb
if ($LASTEXITCODE -ne 0) { throw "application services failed to start" }
Write-Host "stand is starting. Public URL: https://127.0.0.1:8443 CA: certs\ca.crt"
Write-Host "OpenBao stays sealed across its own restart. Unseal with: docker compose --env-file .env --profile bootstrap run --rm openbao-bootstrap"
+75
View File
@@ -0,0 +1,75 @@
#!/bin/sh
set -eu
apk add --no-cache openssl >/dev/null
cd /work
if [ -f ca.crt ] && [ -f allowed-lb.sha256 ] && [ -f lb.pem ]; then
echo "certificates already present"
exit 0
fi
openssl req -x509 -newkey rsa:3072 -days 30 -nodes \
-keyout ca.key -out ca.crt \
-subj "/CN=messenger-lab-ca" \
-addext basicConstraints=critical,CA:TRUE \
-addext keyUsage=critical,keyCertSign,cRLSign >/dev/null 2>&1
issue() {
name="$1"
cn="$2"
eku="$3"
san="$4"
cat > "/tmp/${name}.cnf" <<EOF
[req]
distinguished_name=dn
prompt=no
[dn]
CN=${cn}
[ext]
subjectAltName=${san}
keyUsage=digitalSignature,keyEncipherment
extendedKeyUsage=${eku}
basicConstraints=CA:FALSE
EOF
openssl req -newkey rsa:2048 -nodes -keyout "${name}.key" -out "/tmp/${name}.csr" -subj "/CN=${cn}" >/dev/null 2>&1
openssl x509 -req -in "/tmp/${name}.csr" -CA ca.crt -CAkey ca.key -CAcreateserial \
-out "${name}.crt" -days 7 -extfile "/tmp/${name}.cnf" -extensions ext >/dev/null 2>&1
cat "${name}.crt" "${name}.key" > "${name}.pem"
}
issue lb localhost serverAuth "DNS:localhost,IP:127.0.0.1"
issue api api serverAuth "DNS:api,DNS:localhost,IP:127.0.0.1"
issue lb-client lb clientAuth "DNS:lb,URI:spiffe://messenger/lb"
issue lb-revoked lb clientAuth "DNS:lb,URI:spiffe://messenger/lb"
issue stranger stranger clientAuth "URI:spiffe://messenger/stranger"
issue api-client api clientAuth "DNS:api-client,URI:spiffe://messenger/api"
issue worker-client worker clientAuth "DNS:worker-client,URI:spiffe://messenger/worker"
issue postgres postgres serverAuth "DNS:postgres"
issue redis redis serverAuth "DNS:redis,IP:127.0.0.1"
issue rabbitmq rabbitmq serverAuth "DNS:rabbitmq"
issue s3 s3 serverAuth "DNS:s3,DNS:localhost,IP:127.0.0.1"
issue otel otel-collector serverAuth "DNS:otel-collector"
issue prometheus prometheus serverAuth "DNS:prometheus"
issue loki loki serverAuth "DNS:loki"
issue tempo tempo serverAuth "DNS:tempo"
issue grafana localhost serverAuth "DNS:localhost,IP:127.0.0.1"
issue openbao openbao serverAuth "DNS:openbao,IP:127.0.0.1"
openssl req -x509 -newkey rsa:2048 -days 7 -nodes \
-keyout other-ca.key -out other-ca.crt \
-subj "/CN=other-ca" \
-addext basicConstraints=critical,CA:TRUE \
-addext keyUsage=critical,keyCertSign,cRLSign >/dev/null 2>&1
openssl req -newkey rsa:2048 -nodes -keyout other-client.key -out /tmp/other-client.csr -subj "/CN=lb" >/dev/null 2>&1
cat > /tmp/other-client.cnf <<EOF
subjectAltName=URI:spiffe://messenger/lb
keyUsage=digitalSignature,keyEncipherment
extendedKeyUsage=clientAuth
basicConstraints=CA:FALSE
EOF
openssl x509 -req -in /tmp/other-client.csr -CA other-ca.crt -CAkey other-ca.key -CAcreateserial \
-out other-client.crt -days 7 -extfile /tmp/other-client.cnf >/dev/null 2>&1
cat other-client.crt other-client.key > other-client.pem
openssl x509 -in lb-client.crt -outform DER | openssl dgst -sha256 -r | awk '{print $1}' > allowed-lb.sha256
find . -name '*.key' -exec chmod 600 {} \;
echo "certificates created"
+4 -1
View File
@@ -1,10 +1,13 @@
#!/bin/sh
# Restart API replicas and every store they use. Volumes are kept.
# Restart API replicas and the stores they use. Volumes stay. OpenBao is left running
# so it does not need another unseal; API logs in again with the AppRole files.
set -eu
ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
cd "$ROOT"
if [ -f .env ]; then
docker compose --env-file .env restart postgres redis s3 rabbitmq api worker
docker compose --env-file .env restart s3-tls rabbit-mtls
else
docker compose restart postgres redis s3 rabbitmq api worker
docker compose restart s3-tls rabbit-mtls
fi