$ErrorActionPreference = 'Stop' $repo = Split-Path -Parent $PSScriptRoot $secretRoot = Join-Path $repo 'secrets' $pkiRoot = Join-Path $secretRoot 'pki' $openssl = (Get-Command openssl -ErrorAction SilentlyContinue | Select-Object -First 1 -ExpandProperty Source) if (-not $openssl) { $git = Get-Command git -ErrorAction SilentlyContinue | Select-Object -First 1 -ExpandProperty Source if ($git) { $candidate = Join-Path (Split-Path (Split-Path $git -Parent) -Parent) 'usr\bin\openssl.exe' if (Test-Path -LiteralPath $candidate) { $openssl = $candidate } } } if (-not $openssl) { throw 'OpenSSL is required. Install Git for Windows or OpenSSL and retry.' } New-Item -ItemType Directory -Force -Path $pkiRoot | Out-Null $rootKey = Join-Path $pkiRoot 'internal-ca.key.pem' $rootCert = Join-Path $pkiRoot 'internal-ca.crt' $hasRootKey = Test-Path -LiteralPath $rootKey $hasRootCert = Test-Path -LiteralPath $rootCert if ($hasRootKey -ne $hasRootCert) { throw 'Local CA is incomplete. Restore its matching key/certificate pair from backup.' } function Invoke-OpenSsl([string[]]$Arguments) { & $openssl @Arguments if ($LASTEXITCODE -ne 0) { throw "OpenSSL failed: $($Arguments[0])" } } function New-RandomHex([int]$Bytes) { $buffer = [byte[]]::new($Bytes) $generator = [System.Security.Cryptography.RandomNumberGenerator]::Create() try { $generator.GetBytes($buffer) } finally { $generator.Dispose() } return ([BitConverter]::ToString($buffer).Replace('-', '').ToLowerInvariant()) } function Save-Secret([string]$Name,[string]$Value) { $path = Join-Path $secretRoot $Name if (-not (Test-Path -LiteralPath $path)) { Set-Content -LiteralPath $path -Value $Value -NoNewline -Encoding ascii } } $postgresPassword = New-RandomHex 32 Save-Secret 'postgres_password' $postgresPassword Save-Secret 'database_url' "postgresql+psycopg://postgres:${postgresPassword}@db:5432/messenger?sslmode=verify-full&sslrootcert=/run/secrets/internal-ca.crt" Save-Secret 's3_root_access_key_id' (New-RandomHex 20) Save-Secret 's3_root_secret_access_key' (New-RandomHex 32) Save-Secret 's3_access_key_id' (New-RandomHex 20) Save-Secret 's3_secret_access_key' (New-RandomHex 32) Save-Secret 's3_worker_access_key_id' (New-RandomHex 20) Save-Secret 's3_worker_secret_access_key' (New-RandomHex 32) Save-Secret 'redis_api_password' (New-RandomHex 32) Save-Secret 'redis_dispatcher_password' (New-RandomHex 32) Save-Secret 'redis_worker_password' (New-RandomHex 32) Save-Secret 'grafana_admin_password' (New-RandomHex 32) $acl = @( 'user default off', "user api on >$(Get-Content (Join-Path $secretRoot 'redis_api_password') -Raw) ~session:v1:* ~login:v1:* +ping +get +set +del +eval +incr +expire +ttl", "user messenger-dispatcher on >$(Get-Content (Join-Path $secretRoot 'redis_dispatcher_password') -Raw) ~messenger:attachment-jobs:v1* ~lab5:failpoint:* +ping +set +xadd", "user messenger-worker on >$(Get-Content (Join-Path $secretRoot 'redis_worker_password') -Raw) ~messenger:attachment-jobs:v1* ~lab5:failpoint:* ~lab6:failpoint:* +ping +get +set +xadd +xgroup +xreadgroup +xack +xautoclaim" ) Set-Content -LiteralPath (Join-Path $secretRoot 'redis-users.acl') -Value $acl -Encoding ascii if (-not $hasRootKey) { Invoke-OpenSsl @('req','-x509','-newkey','rsa:4096','-sha256','-nodes','-days','3650', '-keyout',$rootKey,'-out',$rootCert,'-subj','/CN=MTUSI-Lab7-Local-Root', '-addext','basicConstraints=critical,CA:TRUE,pathlen:1', '-addext','keyUsage=critical,keyCertSign,cRLSign') } function New-Leaf([string]$Name,[string]$CommonName,[string]$Usage,[string[]]$DnsNames,[string[]]$IpNames=@()) { $key = Join-Path $pkiRoot "$Name.key.pem" $csr = Join-Path $pkiRoot "$Name.csr.pem" $cert = Join-Path $pkiRoot "$Name.crt" $ext = Join-Path $pkiRoot "$Name.ext" $hasKey = Test-Path -LiteralPath $key $hasCert = Test-Path -LiteralPath $cert if ($hasKey -and $hasCert) { return } if ($hasKey -or $hasCert) { throw "Leaf $Name is incomplete. Restore its matching key/certificate pair or remove both and regenerate." } $san = @($DnsNames | ForEach-Object { "DNS:$_" }) + @($IpNames | ForEach-Object { "IP:$_" }) @( 'basicConstraints=critical,CA:FALSE', 'keyUsage=critical,digitalSignature,keyEncipherment', "extendedKeyUsage=$Usage", "subjectAltName=$($san -join ',')" ) | Set-Content -LiteralPath $ext -Encoding ascii Invoke-OpenSsl @('req','-new','-newkey','rsa:2048','-sha256','-nodes','-keyout',$key,'-out',$csr,'-subj',"/CN=$CommonName") Invoke-OpenSsl @('x509','-req','-sha256','-in',$csr,'-CA',$rootCert,'-CAkey',$rootKey,'-CAcreateserial','-days','90','-extfile',$ext,'-out',$cert) Remove-Item -LiteralPath $csr,$ext -Force } # Only OpenBao's listener certificate is bootstrapped by the offline CA. # Internal leaves are issued by OpenBao's intermediate during its bootstrap. New-Leaf 'openbao-server' 'openbao' 'serverAuth' @('openbao','localhost') @('127.0.0.1') # Nginx enables CRL checking for the whole client certificate chain. Publish an # empty root CRL as well as OpenBao's intermediate CRL so the chain can be # checked without treating the absence of a root CRL as an authentication error. $rootCrlState = Join-Path $pkiRoot 'root-crl-state' New-Item -ItemType Directory -Force -Path $rootCrlState | Out-Null $rootCrlIndex = Join-Path $rootCrlState 'index.txt' $rootCrlNumber = Join-Path $rootCrlState 'crlnumber' $rootCrlConfig = Join-Path $rootCrlState 'openssl.cnf' $rootCrl = Join-Path $pkiRoot 'root-ca.crl.pem' if (-not (Test-Path -LiteralPath $rootCrlIndex)) { Set-Content -LiteralPath $rootCrlIndex -Value '' -NoNewline -Encoding ascii } if (-not (Test-Path -LiteralPath $rootCrlNumber)) { Set-Content -LiteralPath $rootCrlNumber -Value '01' -Encoding ascii } $rootCertConfigPath = ([System.IO.Path]::GetFullPath($rootCert)).Replace('\','/') $rootKeyConfigPath = ([System.IO.Path]::GetFullPath($rootKey)).Replace('\','/') $rootIndexConfigPath = ([System.IO.Path]::GetFullPath($rootCrlIndex)).Replace('\','/') $rootNumberConfigPath = ([System.IO.Path]::GetFullPath($rootCrlNumber)).Replace('\','/') $rootCrlConfigText = @( '[ ca ]', 'default_ca = CA_default', '[ CA_default ]', "database = `"$rootIndexConfigPath`"", "certificate = `"$rootCertConfigPath`"", "private_key = `"$rootKeyConfigPath`"", "crlnumber = `"$rootNumberConfigPath`"", 'default_md = sha256', 'default_days = 365', 'default_crl_days = 365' ) Set-Content -LiteralPath $rootCrlConfig -Value $rootCrlConfigText -Encoding ascii Invoke-OpenSsl @('ca','-batch','-config',$rootCrlConfig,'-gencrl','-out',$rootCrl) # Compose needs the source path to exist before the first OpenBao bootstrap. # The operator bootstrap replaces this marker with the signed issuer CRL before # any API container is started; an invalid placeholder therefore fails closed. $crlPath = Join-Path $pkiRoot 'pki-int.crl.pem' if (-not (Test-Path -LiteralPath $crlPath)) { Set-Content -LiteralPath $crlPath -Value '# OpenBao bootstrap must replace this placeholder with a signed CRL.' -Encoding ascii } # Test-only identity: correct LB CN, deliberately signed by a separate CA. $foreignCaKey = Join-Path $pkiRoot 'negative-test-ca.key.pem' $foreignCaCert = Join-Path $pkiRoot 'negative-test-ca.crt' $foreignClientKey = Join-Path $pkiRoot 'negative-test-client.key' $foreignClientCsr = Join-Path $pkiRoot 'negative-test-client.csr' $foreignClientCert = Join-Path $pkiRoot 'negative-test-client.crt' if (-not ((Test-Path -LiteralPath $foreignCaKey) -and (Test-Path -LiteralPath $foreignCaCert) -and (Test-Path -LiteralPath $foreignClientKey) -and (Test-Path -LiteralPath $foreignClientCert))) { Invoke-OpenSsl @('req','-x509','-newkey','rsa:2048','-sha256','-nodes','-days','7', '-keyout',$foreignCaKey,'-out',$foreignCaCert,'-subj','/CN=MTUSI-Lab7-Negative-Test-CA', '-addext','basicConstraints=critical,CA:TRUE,pathlen:0', '-addext','keyUsage=critical,keyCertSign,cRLSign') Invoke-OpenSsl @('req','-new','-newkey','rsa:2048','-sha256','-nodes', '-keyout',$foreignClientKey,'-out',$foreignClientCsr,'-subj','/CN=messenger-lb') @('basicConstraints=critical,CA:FALSE','keyUsage=critical,digitalSignature,keyEncipherment','extendedKeyUsage=clientAuth') | Set-Content -LiteralPath (Join-Path $pkiRoot 'negative-test-client.ext') -Encoding ascii Invoke-OpenSsl @('x509','-req','-sha256','-in',$foreignClientCsr,'-CA',$foreignCaCert,'-CAkey',$foreignCaKey, '-CAcreateserial','-days','7','-extfile',(Join-Path $pkiRoot 'negative-test-client.ext'),'-out',$foreignClientCert) Remove-Item -LiteralPath $foreignClientCsr,(Join-Path $pkiRoot 'negative-test-client.ext') -Force } Write-Host 'Local PKI created under secrets/pki. Private keys are excluded from Git; keep the CA key with the operator.'