Files
ExecuteR/scripts/New-LocalSecrets.ps1
Alphaeus Mote b242509675 feat(deploy): bundled Postgres on the internal network by default [skip ci]
docker-compose.yml gains a postgres service (EXECUTER-DB-00001, official
image pinned by major and digest like NATS, INTERNAL network only, no host
port, pg_isready health check, shm_size, runs as PUID:PGID, data in
${STACK_BINDMOUNTROOT}/${STACK_NAME}/Postgres/Data). It is in the "postgres"
Compose profile; COMPOSE_PROFILES in .env turns it on, and the backend waits
for it healthy (depends_on required: false, so SQLite and external databases
start without it). The backend defaults to DATABASE_PROVIDER=postgres with
DATABASE_CONNECTION_STRING and DATABASE_PASSWORD from .env.
docker-compose.postgres-port.yml publishes a host port, opt-in through
POSTGRES_BIND.

deploy.sh decides the database: what .env names when the operator set it;
else SQLite when API/Data/app.db exists (written to .env, with a note on how
to move); else the bundled Postgres, its password generated into .env (and
exported), never regenerated over an existing database. COMPOSE_PROFILES
follows. The decisions are functions that scripts/tests/Test-DeployDatabase.sh
runs on fixtures without Docker. Compose 2.20.2+ is checked; a new .env is
mode 600. New-LocalSecrets.ps1 generates the password for a new .env and
keeps an existing .env's database settings on -Force.

README: deploy, secrets, Compose, database providers, Postgres backups and
pg_restore, and Moving from SQLite to Postgres (migrate-database).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 06:01:01 -05:00

111 lines
5.6 KiB
PowerShell

<#
.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."
}