# Windows PowerShell 5.1+ version of scripts/init-secrets.sh (lab7, operator). # Creates local synthetic secrets in secrets\ (git-ignored) + .env, then the local PKI (CA, leaf certificates of every # service identity, CRL, negative-test material) in a one-shot container "pki" (no openssl needed on the host). # Idempotent: existing files are NOT overwritten. Values are never printed. # powershell -NoProfile -ExecutionPolicy Bypass -File scripts\init-secrets.ps1 $ErrorActionPreference = 'Stop' $root = Split-Path -Parent $PSScriptRoot Set-Location $root New-Item -ItemType Directory -Force -Path (Join-Path $root 'secrets') | Out-Null $ascii = New-Object System.Text.ASCIIEncoding $rng = [System.Security.Cryptography.RandomNumberGenerator]::Create() function New-Hex { $bytes = New-Object byte[] 24 $rng.GetBytes($bytes) ($bytes | ForEach-Object { $_.ToString('x2') }) -join '' } function New-KeyId { $alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' $bytes = New-Object byte[] 20 $rng.GetBytes($bytes) -join ($bytes | ForEach-Object { $alphabet[$_ % $alphabet.Length] }) } function Set-SecretFile([string]$name, [string]$value) { $path = Join-Path (Join-Path $root 'secrets') $name if (-not (Test-Path $path)) { # No BOM, no trailing CR/LF: the value is read verbatim inside Linux containers. [System.IO.File]::WriteAllText($path, $value, $ascii) } } foreach ($n in 'postgres_password', 'db_owner_password', 'db_api_a_password', 'db_worker_a_password', 'redis_api_password', 'redis_ops_password', 'grafana_admin_password') { Set-SecretFile $n (New-Hex) } foreach ($who in 'admin', 'api', 'worker') { Set-SecretFile ("s3_{0}_access_key_id" -f $who) (New-KeyId) Set-SecretFile ("s3_{0}_secret_access_key" -f $who) (New-Hex) } if (-not (Test-Path '.env')) { Copy-Item '.env.example' '.env' } $pyApp = Get-Command py -CommandType Application -ErrorAction SilentlyContinue | Select-Object -First 1 $pythonApp = Get-Command python -CommandType Application -ErrorAction SilentlyContinue | Select-Object -First 1 if ($pyApp) { $py = $pyApp.Source; $pre = @('-3') } elseif ($pythonApp) { $py = $pythonApp.Source; $pre = @() } else { throw 'Python 3.9+ not found (install from python.org, enable "Add to PATH")' } $s3cfg = Join-Path $root 'secrets\s3_config.json' if (-not (Test-Path $s3cfg)) { $bucket = 'messenger-attachments' $line = Select-String -Path '.env' -Pattern '^S3_BUCKET=(.+)$' | Select-Object -Last 1 if ($line) { $bucket = $line.Matches[0].Groups[1].Value.Trim() } $json = & $py @pre scripts\s3_config.py $bucket if ($LASTEXITCODE -ne 0) { throw 's3_config.py failed' } [System.IO.File]::WriteAllText($s3cfg, ($json -join "`n"), $ascii) } # PKI: CA + leaf certificates + CRL + test material (repeat = only missing ones). CA key stays in secrets\pki. & docker compose --env-file .env --profile tools build -q pki if ($LASTEXITCODE -ne 0) { throw 'docker compose build pki failed' } & docker compose --env-file .env --profile tools run --rm --no-deps -T pki init if ($LASTEXITCODE -ne 0) { throw 'pki init failed' } Write-Output 'secrets\ and .env are ready (values not shown).'