Every service connected to PostgreSQL as the database owner, which is a superuser, so anything that compromised an app process owned the cluster, and every connection reached the server with an empty application_name, so pg_stat_activity could not say which service a session belonged to. Split the DB credential into two login roles and name every pool: - archie_migrator owns public and its objects and runs goose. Migrations run on a short-lived pool opened from the new migration_database_url, before the serving pool opens. It is not a superuser. - archie_runtime serves requests with SELECT, INSERT, UPDATE, DELETE, sequence USAGE/SELECT and function EXECUTE only. It has no CREATE privilege, so it cannot change the schema. - Every pool sets application_name: archie-state-store, archie-gateway, and a -migrate / -recovery / -schema / -provision suffix on the transient administrative connections. Existing one-URL installs keep working: an absent migration_database_url falls back to database_url and the migration runs on the serving pool exactly as before. Provisioning is a new idempotent `archie-state-store provision` subcommand: it needs an admin URL the services never hold (database owner or superuser), creates or reconciles both roles, hands every public-schema object to the migration role, revokes PUBLIC's CREATE on public, sets row-level grants, and sets the migration role's default privileges so tables future migrations create are usable without a hand-run grant. It stores SCRAM verifiers, refuses superuser roles, and verifies the boundary before committing. install.sh now provisions the bundled path and separates the credentials: the superuser password the Compose database is created with lives only in a compose-only postgres.env, while the env file the units load holds the non-superuser PGPASSWORD. A bundled install that predates the split is upgraded in place: the roles are created, the superuser password is rotated out of the env file, and config.toml is moved to the runtime and migration URLs. External databases keep their operator-managed credential; the deployments README documents the one-shot provision command. setup writes both bundled URLs, dev:postgres provisions the dev database, the e2e stack provisions its throwaway server, and the deployment templates point at the two roles.
191 lines
6.4 KiB
Bash
Executable File
191 lines
6.4 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Run the whole stack as local processes against a throwaway PostgreSQL 18,
|
|
# for manual and scripted checks. Never part of the gate. Nothing here reads
|
|
# the installed config: XDG paths point into the dev directory.
|
|
#
|
|
# e2e/stack.sh start | stop | restart <service> | status | reset
|
|
set -Eeuo pipefail
|
|
|
|
repo=$(cd "$(dirname "$0")/.." && pwd)
|
|
dir=${ARCHIE_DEV_DIR:-${XDG_STATE_HOME:-$HOME/.local/state}/archie-dev}
|
|
pgport=${ARCHIE_DEV_PGPORT:-55432}
|
|
services=(archie-state-store archie-gateway archied archie-messaging archie-ui)
|
|
|
|
# initdb refuses root, and postgres cannot traverse root's home.
|
|
if [ "$(id -u)" -eq 0 ]; then
|
|
pgdata=/var/lib/archie-dev/pg
|
|
as_pg() { runuser -u postgres -- "$@"; }
|
|
else
|
|
pgdata=$dir/pg
|
|
as_pg() { "$@"; }
|
|
fi
|
|
|
|
export XDG_CONFIG_HOME=$dir/config XDG_DATA_HOME=$dir/data XDG_STATE_HOME=$dir/state
|
|
|
|
pgbin() {
|
|
local d
|
|
for d in /usr/lib/postgresql/18/bin "$(dirname "$(command -v postgres 2>/dev/null || echo /x/x)")"; do
|
|
if [ -x "$d/postgres" ] && "$d/postgres" --version | grep -q ' 18\.'; then
|
|
echo "$d"
|
|
return
|
|
fi
|
|
done
|
|
echo "PostgreSQL 18 server not found" >&2
|
|
exit 1
|
|
}
|
|
|
|
check_process() {
|
|
local pid
|
|
if [ ! -f "$dir/pids/$1" ] || ! pid=$(cat "$dir/pids/$1") || ! kill -0 "$pid" 2>/dev/null; then
|
|
echo "$1 exited; see $dir/logs/$1.log" >&2
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
wait_port() {
|
|
for _ in $(seq 1 "${3:-60}"); do
|
|
check_process "$2" || return 1
|
|
(echo >"/dev/tcp/127.0.0.1/$1") 2>/dev/null && return 0
|
|
sleep 1
|
|
done
|
|
echo "$2 did not listen on $1; see $dir/logs/$2.log" >&2
|
|
return 1
|
|
}
|
|
|
|
start_pg() {
|
|
local bin
|
|
bin=$(pgbin)
|
|
if [ ! -f "$pgdata/PG_VERSION" ]; then
|
|
mkdir -p "$pgdata"
|
|
[ "$(id -u)" -ne 0 ] || chown postgres "$pgdata"
|
|
as_pg "$bin/initdb" -D "$pgdata" -U archie --auth=trust >/dev/null
|
|
fi
|
|
if ! as_pg "$bin/pg_ctl" -D "$pgdata" status >/dev/null 2>&1; then
|
|
as_pg "$bin/pg_ctl" -D "$pgdata" -w -l "$pgdata/server.log" \
|
|
-o "-p $pgport -k $pgdata -c listen_addresses=127.0.0.1" start >/dev/null
|
|
fi
|
|
"$bin/psql" -h 127.0.0.1 -p "$pgport" -U archie -d postgres -qtAc \
|
|
"SELECT 1 FROM pg_database WHERE datname = 'archie'" | grep -q 1 ||
|
|
"$bin/createdb" -h 127.0.0.1 -p "$pgport" -U archie archie
|
|
}
|
|
|
|
prepare() {
|
|
mkdir -p "$dir"/{bin,logs,pids} "$XDG_CONFIG_HOME/archie" "$XDG_DATA_HOME" "$XDG_STATE_HOME"
|
|
[ -f "$XDG_CONFIG_HOME/archie/config.toml" ] || cp "$repo/config.example.toml" "$XDG_CONFIG_HOME/archie/config.toml"
|
|
# The services serve from the least-privilege role and migrate as the schema
|
|
# owner; initdb's archie role is the throwaway server's superuser only.
|
|
sed -e "s#^database_url = .*#database_url = \"postgres://archie_runtime@127.0.0.1:$pgport/archie?sslmode=disable\"#" \
|
|
-e "s#^migration_database_url = .*#migration_database_url = \"postgres://archie_migrator@127.0.0.1:$pgport/archie?sslmode=disable\"#" \
|
|
"$repo/deployments/dev.toml" >"$dir/dev.toml"
|
|
local version
|
|
version=$(git -C "$repo" describe --tags --exact-match --match 'v[0-9]*' 2>/dev/null || true)
|
|
version=${version#v}
|
|
[ -n "$version" ] || version="dev+$(git -C "$repo" rev-parse --short=12 HEAD)"
|
|
task --dir "$repo" dev:tls
|
|
task --dir "$repo" --force build BIN_DIR="$dir/bin" GATEWAY_VERSION="$version" RUNTIME_VERSION="$version"
|
|
}
|
|
|
|
# spawn NAME DIR CMD...: a detached session leader, so stop takes its children.
|
|
spawn() {
|
|
local name=$1 cwd=$2
|
|
shift 2
|
|
rm -f "$dir/pids/$name"
|
|
setsid -f bash -c 'cd "$1" && echo $$ >"$2" && shift 2 && exec "$@"' _ "$cwd" "$dir/pids/$name" "$@" \
|
|
</dev/null >"$dir/logs/$name.log" 2>&1 9>&-
|
|
for _ in $(seq 1 50); do
|
|
[ ! -f "$dir/pids/$name" ] || { check_process "$name"; return; }
|
|
sleep 0.1
|
|
done
|
|
echo "$name did not start; see $dir/logs/$name.log" >&2
|
|
return 1
|
|
}
|
|
|
|
start_service() {
|
|
spawn "$1" "$repo" "$dir/bin/$1" -config-overlay "$dir/dev.toml"
|
|
}
|
|
|
|
# provision_roles creates the least-privilege roles the stack connects as. The
|
|
# throwaway server runs with trust auth, so the role URLs' passwords are never
|
|
# checked; they are here only because provision sets the role password it reads.
|
|
provision_roles() {
|
|
"$dir/bin/archie-state-store" provision \
|
|
-admin-url "postgres://archie@127.0.0.1:$pgport/archie?sslmode=disable" \
|
|
-runtime-url "postgres://archie_runtime:e2e@127.0.0.1:$pgport/archie?sslmode=disable" \
|
|
-migration-url "postgres://archie_migrator:e2e@127.0.0.1:$pgport/archie?sslmode=disable"
|
|
}
|
|
|
|
stop_pid() {
|
|
local f=$1 pid
|
|
[ -f "$f" ] || return 0
|
|
pid=$(cat "$f")
|
|
if kill -- "-$pid" 2>/dev/null; then
|
|
for _ in $(seq 1 20); do kill -0 "$pid" 2>/dev/null || break; sleep 0.5; done
|
|
kill -9 -- "-$pid" 2>/dev/null || true
|
|
fi
|
|
rm -f "$f"
|
|
}
|
|
|
|
start() {
|
|
local port
|
|
for port in 9091 8585 8586 8587 8485 8484 5173; do
|
|
if (echo >"/dev/tcp/127.0.0.1/$port") 2>/dev/null; then
|
|
echo "Port $port is already in use. Run task e2e:down before task e2e:up; if it stays occupied, inspect its owner with ss -ltnp." >&2
|
|
return 1
|
|
fi
|
|
done
|
|
trap 'trap - ERR; stop' ERR
|
|
prepare
|
|
start_pg 9>&-
|
|
provision_roles
|
|
start_service archie-state-store
|
|
wait_port 9091 archie-state-store 90
|
|
local s
|
|
for s in archie-gateway archied archie-messaging archie-ui; do start_service "$s"; done
|
|
wait_port 8585 archie-gateway
|
|
wait_port 8586 archie-messaging
|
|
wait_port 8485 archied
|
|
wait_port 8484 archie-ui
|
|
[ -d "$repo/ui/node_modules" ] || (cd "$repo/ui" && pnpm install --frozen-lockfile >/dev/null)
|
|
spawn vite "$repo/ui" pnpm exec vite --host 127.0.0.1 --port 5173 --strictPort
|
|
wait_port 5173 vite
|
|
for s in "${services[@]}" vite; do check_process "$s"; done
|
|
trap - ERR
|
|
echo "dashboard: http://localhost:5173 logs: $dir/logs"
|
|
}
|
|
|
|
stop() {
|
|
local f
|
|
for f in "$dir"/pids/*; do stop_pid "$f"; done
|
|
if [ -f "$pgdata/PG_VERSION" ]; then
|
|
as_pg "$(pgbin)/pg_ctl" -D "$pgdata" -m fast stop >/dev/null 2>&1 || true
|
|
fi
|
|
}
|
|
|
|
# Keep concurrent invocations from replacing each other's process records.
|
|
mkdir -p "$dir"
|
|
exec 9>"$dir/stack.lock"
|
|
flock -n 9 || { echo "Another e2e stack command is running" >&2; exit 1; }
|
|
|
|
case ${1:-} in
|
|
start) start ;;
|
|
stop) stop ;;
|
|
restart)
|
|
stop_pid "$dir/pids/${2:?service}"
|
|
start_service "$2"
|
|
;;
|
|
status)
|
|
for f in "$dir"/pids/*; do
|
|
[ -f "$f" ] || continue
|
|
if kill -0 "$(cat "$f")" 2>/dev/null; then echo "up $(basename "$f")"; else echo "down $(basename "$f")"; fi
|
|
done
|
|
;;
|
|
reset)
|
|
stop
|
|
rm -rf "$dir" "$pgdata"
|
|
;;
|
|
*)
|
|
echo "usage: $0 start | stop | restart <service> | status | reset" >&2
|
|
exit 2
|
|
;;
|
|
esac
|