$ErrorActionPreference = 'Stop' $root = Split-Path -Parent $PSScriptRoot $certDir = Join-Path $root 'infra\certs' $openssl = 'C:\Program Files\Git\usr\bin\openssl.exe' New-Item -ItemType Directory -Force $certDir | Out-Null Set-Location $certDir if ((Test-Path ca.key) -and (Test-Path ca.crt) -and (Test-Path public.crt) -and (Test-Path lb.crt) -and (Test-Path api.crt) -and (Test-Path openbao.crt) -and (Test-Path minio.crt) -and (Test-Path postgres.crt) -and (Test-Path redis.crt) -and (Test-Path otel-collector.crt) -and (Test-Path loki.crt) -and (Test-Path tempo.crt)) { Write-Output 'Local TLS certificates already exist.' exit 0 } & $openssl genrsa -out ca.key 4096 & $openssl req -new -x509 -key ca.key -sha256 -days 3650 -subj '/CN=MTUSI Lab7 Root CA' -addext 'basicConstraints=critical,CA:true' -addext 'keyUsage=critical,keyCertSign,cRLSign' -out ca.crt function New-LeafCertificate { param([string]$Name, [string]$CommonName, [string]$SubjectAltName, [string]$ExtendedKeyUsage) & $openssl genrsa -out "$Name.key" 2048 & $openssl req -new -key "$Name.key" -subj "/CN=$CommonName" -out "$Name.csr" $ext = "subjectAltName=$SubjectAltName`nkeyUsage=digitalSignature,keyEncipherment`nextendedKeyUsage=$ExtendedKeyUsage`n" $extPath = Join-Path $certDir "$Name.ext" [IO.File]::WriteAllText($extPath, $ext, [Text.UTF8Encoding]::new($false)) & $openssl x509 -req -in "$Name.csr" -CA ca.crt -CAkey ca.key -CAcreateserial -out "$Name.crt" -days 365 -sha256 -extfile $extPath Remove-Item "$Name.csr", "$Name.ext" } New-LeafCertificate 'public' 'localhost' 'DNS:localhost,DNS:nginx,DNS:lb.lab.local' 'serverAuth' New-LeafCertificate 'lb' 'lb.lab.local' 'DNS:lb.lab.local,URI:spiffe://lab7/lb' 'clientAuth' New-LeafCertificate 'api' 'api.lab.local' 'DNS:api,DNS:api.lab.local,DNS:localhost,IP:127.0.0.1' 'serverAuth,clientAuth' New-LeafCertificate 'openbao' 'openbao.lab.local' 'DNS:openbao,DNS:openbao.lab.local,IP:127.0.0.1' 'serverAuth' New-LeafCertificate 'minio' 'minio.lab.local' 'DNS:minio,DNS:minio.lab.local,IP:127.0.0.1' 'serverAuth' New-LeafCertificate 'postgres' 'postgres.lab.local' 'DNS:postgres,DNS:postgres.lab.local' 'serverAuth' New-LeafCertificate 'redis' 'redis.lab.local' 'DNS:redis,DNS:redis.lab.local,IP:127.0.0.1' 'serverAuth' New-LeafCertificate 'otel-collector' 'otel-collector.lab.local' 'DNS:otel-collector,DNS:otel-collector.lab.local' 'serverAuth' New-LeafCertificate 'loki' 'loki.lab.local' 'DNS:loki,DNS:loki.lab.local' 'serverAuth' New-LeafCertificate 'tempo' 'tempo.lab.local' 'DNS:tempo,DNS:tempo.lab.local' 'serverAuth' New-Item -ItemType Directory -Force minio-certs | Out-Null Copy-Item minio.crt minio-certs/public.crt -Force Copy-Item minio.key minio-certs/private.key -Force Write-Output 'Created local CA and leaf certificates.'