<# .SYNOPSIS Generates strong local development secrets: - .env (from .env.example); INITIAL_ADMIN_PASSWORD stays empty, so the backend generates the one-time admin password at its first start and records it in its data volume (/data/initial-admin-password.txt) - Real Ed25519 NATS operator/account/signing NKeys and JWTs via the backend's nats-auth CLI (NATS.NKeys / NATS.Jwt — no fake seeds) - secrets/nats/resolver.conf for the NATS container - DATABASE_PASSWORD for the bundled Postgres (a new .env only; an existing .env's database settings are kept on -Force) Idempotent: existing secrets are kept unless -Force is specified. #> [CmdletBinding()] param( [switch]$Force ) $ErrorActionPreference = 'Stop' $repoRoot = Split-Path -Parent $PSScriptRoot $envFile = Join-Path $repoRoot '.env' $envExample = Join-Path $repoRoot '.env.example' $natsSecretsDir = Join-Path $repoRoot 'secrets/nats' if ((Test-Path $envFile) -and (Test-Path (Join-Path $natsSecretsDir 'resolver.conf')) -and -not $Force) { Write-Host 'Secrets already exist. Use -Force to regenerate.' return } New-Item -ItemType Directory -Force $natsSecretsDir | Out-Null # ── NATS operator/account keys via backend CLI ──────────────────────────── Write-Host 'Generating NATS operator/account keys and resolver preload...' dotnet run --project (Join-Path $repoRoot 'src/Backend') -- nats-auth init --output $natsSecretsDir if ($LASTEXITCODE -ne 0) { throw "nats-auth init failed ($LASTEXITCODE)." } $accountSigningSeed = (Get-Content (Join-Path $natsSecretsDir 'account-signing.nk') -Raw).Trim() $accountPublic = (Get-Content (Join-Path $natsSecretsDir 'account.pub') -Raw).Trim() $operatorPublic = (Get-Content (Join-Path $natsSecretsDir 'operator.pub') -Raw).Trim() # ── .env ────────────────────────────────────────────────────────────────── # INITIAL_ADMIN_PASSWORD is left as .env.example has it (empty): generate and # record at the first start. An existing .env's value is kept on -Force, so a # deployment that chose its own keeps it. $existingAdminPassword = if (Test-Path $envFile) { (Get-Content $envFile | Where-Object { $_ -match '^INITIAL_ADMIN_PASSWORD=' } | Select-Object -First 1) -replace '^INITIAL_ADMIN_PASSWORD=', '' } # The database settings an existing .env has are kept on -Force (its database # was initialised with that password; an older .env's SQLite stays SQLite). # A new .env gets the bundled Postgres with a generated DATABASE_PASSWORD. $databaseKeys = 'DATABASE_PROVIDER', 'DATABASE_CONNECTION_STRING', 'DATABASE_PASSWORD', 'COMPOSE_PROFILES' $existingDatabase = [ordered]@{} if (Test-Path $envFile) { foreach ($line in Get-Content $envFile) { foreach ($key in $databaseKeys) { if ($line -match "^$key=(.*)$" -and -not $existingDatabase.Contains($key)) { $existingDatabase[$key] = $Matches[1] } } } } if (-not $existingDatabase.Contains('DATABASE_PROVIDER')) { $alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' $existingDatabase['DATABASE_PASSWORD'] = -join (1..32 | ForEach-Object { $alphabet[[System.Security.Cryptography.RandomNumberGenerator]::GetInt32($alphabet.Length)] }) $generatedDatabasePassword = $true } # A line of the template set to a value, taken literally (no regex substitution in it). function Set-EnvLine([string[]]$Lines, [string]$Key, [string]$Value) { $Lines | ForEach-Object { if ($_ -match "^$Key=") { "$Key=$Value" } else { $_ } } } $content = Get-Content $envExample if ($existingAdminPassword) { $content = Set-EnvLine $content 'INITIAL_ADMIN_PASSWORD' $existingAdminPassword } foreach ($key in $existingDatabase.Keys) { $content = Set-EnvLine $content $key $existingDatabase[$key] } if ($existingDatabase.Contains('DATABASE_PROVIDER') -and -not $existingDatabase.Contains('COMPOSE_PROFILES')) { # An .env from before the bundled Postgres: its provider decides, the profile follows. $content = Set-EnvLine $content 'COMPOSE_PROFILES' ($(if ($existingDatabase['DATABASE_PROVIDER'] -match '^\s*postgres') { 'postgres' } else { '' })) } if ($existingDatabase.Contains('DATABASE_PROVIDER') -and -not $existingDatabase.Contains('DATABASE_CONNECTION_STRING')) { $content = Set-EnvLine $content 'DATABASE_CONNECTION_STRING' '' } $content = $content -replace '^NATS_ACCOUNT_SIGNING_SEED=.*$', "NATS_ACCOUNT_SIGNING_SEED=$accountSigningSeed" $content = $content -replace '^NATS_ACCOUNT_PUBLIC_KEY=.*$', "NATS_ACCOUNT_PUBLIC_KEY=$accountPublic" $content = $content -replace '^NATS_OPERATOR_PUBLIC_KEY=.*$', "NATS_OPERATOR_PUBLIC_KEY=$operatorPublic" $content = $content -replace '^PUBLIC_SERVER_URL=.*$', 'PUBLIC_SERVER_URL=http://localhost:8080' $content = $content -replace '^TRUSTED_HOSTS=.*$', 'TRUSTED_HOSTS=localhost' Set-Content -Path $envFile -Value $content -Encoding utf8NoBOM Write-Host '' Write-Host "Wrote $envFile" if ($existingAdminPassword) { Write-Host 'Initial admin: admin with the INITIAL_ADMIN_PASSWORD kept from the existing .env' } else { Write-Host 'Initial admin: admin, one-time password recorded at the first start:' Write-Host ' docker exec EXECUTER-API-00001 cat /data/initial-admin-password.txt' } Write-Host 'NATS material written below secrets/nats (seeds are SECRET).' if ($generatedDatabasePassword) { Write-Host 'Database: the bundled Postgres, with a generated DATABASE_PASSWORD in .env (SECRET).' } else { Write-Host "Database: $($existingDatabase['DATABASE_PROVIDER']), settings kept from the existing .env." }