Files
mtusi2026lab7/infra/tls/bootstrap.ps1
T
Zyulina_Ekaterina 5b8c2a22e1
Skeleton contracts / contracts (pull_request) Blocked by required conditions
ЛР7 - БВТ2302 - Зюлина - на 3
2026-11-29 12:55:09 +03:00

76 lines
5.1 KiB
PowerShell

$ErrorActionPreference = 'Stop'
$root = (Resolve-Path (Join-Path $PSScriptRoot '..\..')).Path
$out = Join-Path $root 'secrets\tls'
New-Item -ItemType Directory -Force -Path $out | Out-Null
if (-not (Get-Command openssl -ErrorAction SilentlyContinue)) { throw 'Install OpenSSL and make openssl available on PATH.' }
function New-Ca($name) {
$key = Join-Path $out "$name-ca.key"
$crt = Join-Path $out "$name-ca.crt"
& openssl genrsa -out $key 3072
& openssl req -x509 -new -key $key -sha256 -days 365 -subj "/CN=VisPril-$name-CA" -out $crt
if ($LASTEXITCODE) { throw "Failed to create $name CA" }
}
function New-Leaf($name, $ca, $usage, $sans, $subject=$name) {
$key = Join-Path $out "$name.key"
$csr = Join-Path $out "$name.csr"
$crt = Join-Path $out "$name.crt"
$ext = Join-Path $out "$name.ext"
"basicConstraints=CA:FALSE`nextendedKeyUsage=$usage`nsubjectAltName=$sans`nkeyUsage=digitalSignature,keyEncipherment" | Set-Content -NoNewline $ext
& openssl genrsa -out $key 2048
& openssl req -new -key $key -subj "/CN=$subject" -out $csr
& openssl x509 -req -in $csr -CA (Join-Path $out "$ca-ca.crt") -CAkey (Join-Path $out "$ca-ca.key") -CAcreateserial -out $crt -days 90 -sha256 -extfile $ext
Remove-Item $csr,$ext -Force
if ($LASTEXITCODE) { throw "Failed to create $name certificate" }
}
New-Ca 'public'
New-Ca 'service'
New-Ca 'bao'
New-Ca 'lb'
New-Leaf 'ingress' 'public' 'serverAuth' 'DNS:localhost,IP:127.0.0.1'
New-Leaf 'api' 'service' 'serverAuth' 'DNS:api1,DNS:api2'
New-Leaf 'lb-client' 'lb' 'clientAuth' 'DNS:messenger-lb' 'messenger-lb'
New-Leaf 'imposter' 'lb' 'clientAuth' 'DNS:imposter'
New-Leaf 'foreign-client' 'public' 'clientAuth' 'DNS:foreign-client'
New-Leaf 'openbao' 'bao' 'serverAuth' 'DNS:openbao,DNS:localhost,IP:127.0.0.1'
New-Leaf 'minio' 'service' 'serverAuth' 'DNS:minio'
New-Leaf 'db' 'service' 'serverAuth' 'DNS:db'
New-Leaf 'redis-sessions' 'service' 'serverAuth' 'DNS:redis'
New-Leaf 'redis-broker' 'service' 'serverAuth' 'DNS:redis-broker'
New-Leaf 'otel-collector' 'service' 'serverAuth' 'DNS:otel-collector'
New-Leaf 'api-otel-client' 'service' 'clientAuth' 'DNS:messenger-api'
New-Leaf 'worker-otel-client' 'service' 'clientAuth' 'DNS:messenger-worker'
New-Leaf 'tempo' 'service' 'serverAuth' 'DNS:tempo'
New-Leaf 'loki' 'service' 'serverAuth' 'DNS:loki'
New-Leaf 'prometheus' 'service' 'serverAuth' 'DNS:prometheus'
New-Leaf 'prometheus-client' 'service' 'clientAuth' 'DNS:prometheus-scraper'
New-Item -ItemType Directory -Force -Path (Join-Path $root 'secrets\minio-certs') | Out-Null
New-Item -ItemType Directory -Force -Path (Join-Path $root 'secrets\minio-certs\CAs') | Out-Null
$targetDirs = @('ingress','api','api-proxy1','api-proxy2','worker','db','redis-sessions','redis-broker','minio-certs','openbao','openbao-api','openbao-worker','collector','loki','tempo','prometheus','grafana','test-client')
foreach ($dir in $targetDirs) { New-Item -ItemType Directory -Force -Path (Join-Path $root "secrets\$dir") | Out-Null }
function Copy-Tls($file, $dir, $name=$file) { Copy-Item (Join-Path $out $file) (Join-Path $root "secrets\$dir\$name") -Force }
Copy-Tls 'ingress.crt' ingress; Copy-Tls 'ingress.key' ingress; Copy-Tls 'public-ca.crt' ingress
Copy-Tls 'service-ca.crt' ingress; Copy-Tls 'lb-ca.crt' ingress; Copy-Tls 'lb-client.crt' ingress; Copy-Tls 'lb-client.key' ingress
Copy-Tls 'service-ca.crt' api; Copy-Tls 'bao-ca.crt' api
Copy-Tls 'api-otel-client.crt' api; Copy-Tls 'api-otel-client.key' api
Copy-Tls 'api.crt' api-proxy1; Copy-Tls 'api.key' api-proxy1; Copy-Tls 'lb-ca.crt' api-proxy1
Copy-Tls 'api.crt' api-proxy2; Copy-Tls 'api.key' api-proxy2; Copy-Tls 'lb-ca.crt' api-proxy2
Copy-Tls 'imposter.crt' test-client 'wrong-lb-client.crt'; Copy-Tls 'imposter.key' test-client 'wrong-lb-client.key'
Copy-Tls 'foreign-client.crt' test-client; Copy-Tls 'foreign-client.key' test-client
Copy-Tls 'service-ca.crt' worker; Copy-Tls 'bao-ca.crt' worker
Copy-Tls 'worker-otel-client.crt' worker; Copy-Tls 'worker-otel-client.key' worker
Copy-Tls 'db.crt' db; Copy-Tls 'db.key' db; Copy-Tls 'service-ca.crt' db
Copy-Tls 'redis-sessions.crt' redis-sessions; Copy-Tls 'redis-sessions.key' redis-sessions; Copy-Tls 'service-ca.crt' redis-sessions
Copy-Tls 'redis-broker.crt' redis-broker; Copy-Tls 'redis-broker.key' redis-broker; Copy-Tls 'service-ca.crt' redis-broker
Copy-Tls 'minio.crt' minio-certs 'public.crt'; Copy-Tls 'minio.key' minio-certs 'private.key'
Copy-Tls 'service-ca.crt' minio-certs 'CAs\service-ca.crt'
Copy-Tls 'openbao.crt' openbao; Copy-Tls 'openbao.key' openbao; Copy-Tls 'bao-ca.crt' openbao
Copy-Tls 'otel-collector.crt' collector; Copy-Tls 'otel-collector.key' collector; Copy-Tls 'service-ca.crt' collector
Copy-Tls 'loki.crt' loki; Copy-Tls 'loki.key' loki; Copy-Tls 'service-ca.crt' loki
Copy-Tls 'tempo.crt' tempo; Copy-Tls 'tempo.key' tempo; Copy-Tls 'service-ca.crt' tempo
Copy-Tls 'prometheus.crt' prometheus; Copy-Tls 'prometheus.key' prometheus; Copy-Tls 'service-ca.crt' prometheus
Copy-Tls 'prometheus-client.crt' prometheus; Copy-Tls 'prometheus-client.key' prometheus
Copy-Tls 'service-ca.crt' 'grafana'
Write-Host "Local certificates created in $out. Keep all *.key files out of Git."