All checks were successful
PR build (required check) / changes (pull_request) Successful in 6s
secrets-guard / encrypted (pull_request) Successful in 5s
PR build (required check) / build-backend (pull_request) Has been skipped
shell-lint / shellcheck (pull_request) Successful in 7s
PR build (required check) / build-frontend (pull_request) Has been skipped
PR build (required check) / validate-observability (pull_request) Successful in 18s
PR build (required check) / gate (pull_request) Successful in 2s
Environments stop being machines. Until now each box WAS an environment --
"beta" named both a deploy target and a host, "the desktop" named both the
operator's computer and the dev server -- so every path could assume one
environment per host and hardcode /opt/thermograph, /etc/thermograph.env and
ports 8137/8080. That assumption ends here:
vps1 75.119.132.91 Forgejo, Grafana/Loki, the portfolio site, and DEV
(own Postgres, mesh-only on 10.10.0.2:8137)
vps2 169.58.46.181 PROD and BETA as two Swarm stacks sharing one
TimescaleDB instance, plus Centralis, Postfix, backups
desktop AI model hosting + flex Swarm capacity, no environment
Nothing live has moved; the ordered cutover is in
infra/deploy/RUNBOOK-vps1-vps2-cutover.md.
deploy/env-topology.sh is the single source of truth: env -> host, checkout,
branch, deploy mode, stack name, env file, LB ports, DB role/database, service
prefix. THERMOGRAPH_ENV is the input, and on vps2 it is the only thing
distinguishing a beta deploy from a prod one -- deploy.sh refuses to run when it
disagrees with the checkout it was invoked from. The host-wide secrets-env and
deploy-mode markers survive only as a fallback for a single-environment box.
Beta gets its own stack file rather than an overlay (a merge cannot REMOVE the
db service, and beta having no database of its own is the point). Its services
are prefixed beta-*: Swarm registers a service's short name as a DNS alias on
every network it joins, so two stacks both calling a service `web` on the shared
data network would let prod's frontend resolve a beta task. Prod's stack, env
and LB config are untouched.
One Postgres, two databases with two roles: deploy/db/provision-env-db.sh
creates thermograph_beta (NOSUPERUSER, owns only its own database, CONNECT
revoked from PUBLIC) plus a <role>_ro for ad-hoc queries, and refuses to run for
the environment that owns the instance -- doing so would demote prod's bootstrap
superuser.
Fixes that co-residency would otherwise have broken silently:
- CI secrets are keyed by host (VPS1_SSH_*, VPS2_SSH_*). SSH_* meant "beta" and
also "the Forgejo box" because those shared a machine; that conflation is what
once had the prod backup dumping beta.
- The nightly backup dumps BOTH application databases to separate off-box
prefixes, and fails loudly on a missing one instead of skipping it.
- Alloy stopped deriving the `host` label from the node -- on vps2 that would
have filed every beta line as prod, feeding prod's alert rules with beta's
traffic. It is now derived per source, with a new `node` label for the machine,
and beta's log volume is mounted via a vps2-only overlay.
- ops/dbq.sh, ops/iceberg.sh and the secrets seed scripts derived their SSH
target and env-file path from the topology instead of hardcoding beta to
75.119.132.91 -- which is vps1's address now.
- Dev's overlay binds DEV_BIND_ADDR (loopback by default, the mesh address on
vps1) instead of 0.0.0.0: correct for a home LAN box, a public exposure of
unreviewed branches on a VPS.
Terraform's hosts variable is keyed by machine with a nested environments map,
and main.tf flattens (host, environment) pairs so two environments on one box
cannot share a checkout. Caddy's single reference config is split per host.
Docs, onboarding and the runbooks are updated throughout, including the security
rationales that co-residency makes false: separate SSH credentials no longer put
a host boundary between beta and prod, and the boundary that remains is the
database and the filesystem.
161 lines
7.7 KiB
Bash
Executable file
161 lines
7.7 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
# Give an environment its own role + database on the SHARED Postgres instance.
|
|
#
|
|
# sudo bash infra/deploy/db/provision-env-db.sh beta # run on vps2
|
|
#
|
|
# Since beta moved onto vps2 there is ONE TimescaleDB instance serving two
|
|
# environments. That is a capacity decision — one Postgres to tune, back up and
|
|
# keep on one extension build — and it is emphatically NOT a decision to let the
|
|
# two environments see each other's data. This script is what makes the second
|
|
# half true:
|
|
#
|
|
# * a role per environment (thermograph_beta), with its own password taken
|
|
# from that environment's own vault render,
|
|
# * a database per environment (thermograph_beta) OWNED by that role,
|
|
# * CONNECT revoked from PUBLIC on it, so the split is enforced by Postgres
|
|
# rather than by everyone remembering to use the right URL,
|
|
# * and NO superuser, NO CREATEDB, NO CREATEROLE on that role.
|
|
#
|
|
# The prod role keeps its own database and cannot be reached with beta's
|
|
# credentials; beta's role cannot connect to prod's database at all.
|
|
#
|
|
# Idempotent by construction — safe to re-run after a password rotation (it
|
|
# re-applies the password) or on a fresh instance. It never drops anything.
|
|
#
|
|
# WHY THIS IS NOT WIRED INTO deploy.sh: creating roles and databases is a
|
|
# privileged, once-per-environment act, and a deploy that can mint database
|
|
# roles is a deploy that can mint them wrongly at 3am. Run it by hand from the
|
|
# runbook when an environment is first stood up, and after a password rotation.
|
|
set -euo pipefail
|
|
|
|
ENV_NAME="${1:?usage: provision-env-db.sh <env> (beta|prod)}"
|
|
|
|
SELF_DIR=$(cd "$(dirname "$0")" && pwd)
|
|
# shellcheck source=infra/deploy/env-topology.sh
|
|
. "$SELF_DIR/../env-topology.sh"
|
|
thermograph_topology "$ENV_NAME"
|
|
|
|
if [ "$TG_DEPLOY_MODE" != stack ]; then
|
|
echo "!! $ENV_NAME does not use the shared instance (dev keeps its own db container)" >&2
|
|
exit 2
|
|
fi
|
|
|
|
# REFUSE to run for the environment that OWNS the instance.
|
|
#
|
|
# This script provisions a GUEST environment onto someone else's Postgres. Run
|
|
# for prod, it would target prod's `thermograph` role — which is the instance's
|
|
# bootstrap superuser, not a guest — and the `ALTER ROLE ... NOSUPERUSER` below
|
|
# would strip superuser from the role the whole instance is administered with.
|
|
# Prod's roles predate this script and are created by the container's own
|
|
# initdb; there is nothing here for prod to need.
|
|
if [ "$TG_DB_SERVICE" = "${TG_STACK_NAME}_db" ]; then
|
|
echo "!! '$ENV_NAME' OWNS this Postgres instance (${TG_DB_SERVICE} is its own stack's db)." >&2
|
|
echo "!! This script provisions a guest environment onto a shared instance." >&2
|
|
echo "!! Running it here would demote ${TG_DB_USER} from superuser. Refusing." >&2
|
|
exit 2
|
|
fi
|
|
|
|
# The database SERVER is prod's, wherever we are provisioning FOR.
|
|
DB_CID=$(docker ps -q --filter "label=com.docker.swarm.service.name=${TG_DB_SERVICE}" | head -1)
|
|
if [ -z "$DB_CID" ]; then
|
|
echo "!! no running task for ${TG_DB_SERVICE} on this host" >&2
|
|
echo "!! run this on vps2, with prod's stack up." >&2
|
|
exit 1
|
|
fi
|
|
|
|
# The new role's password is whatever that environment's vault says it is, so
|
|
# the database and the app can never disagree about it. Read from the rendered
|
|
# env file (a deploy of that environment writes it) rather than from sops here:
|
|
# this script should not need the age key.
|
|
if [ ! -r "$TG_ENV_FILE" ]; then
|
|
PW=$(sudo grep -m1 '^POSTGRES_PASSWORD=' "$TG_ENV_FILE" 2>/dev/null | cut -d= -f2- || true)
|
|
else
|
|
PW=$(grep -m1 '^POSTGRES_PASSWORD=' "$TG_ENV_FILE" | cut -d= -f2- || true)
|
|
fi
|
|
if [ -z "${PW:-}" ]; then
|
|
echo "!! no POSTGRES_PASSWORD in $TG_ENV_FILE" >&2
|
|
echo "!! deploy $ENV_NAME once first so the vault renders it, then re-run." >&2
|
|
exit 1
|
|
fi
|
|
|
|
echo "==> Provisioning role/database '${TG_DB_USER}'/'${TG_DB_NAME}' on ${TG_DB_SERVICE}"
|
|
|
|
# The bootstrap superuser of the instance is prod's POSTGRES_USER (`thermograph`),
|
|
# and psql runs inside the container over its local socket — the database is
|
|
# never exposed on a TCP port anyone outside the overlay can reach.
|
|
#
|
|
# The password reaches psql as an environment variable rather than a -v
|
|
# argument, so it is not in psql's argv inside the container. It IS briefly in
|
|
# the `docker exec` argv on the host; that is visible only to root on vps2, who
|
|
# can read the vault render anyway. It is never interpolated into SQL text:
|
|
# :'pw' is psql's quote-and-escape form, which is also what makes a password
|
|
# containing a quote safe.
|
|
#
|
|
# Note both statements below are generated and run via \gexec rather than a
|
|
# DO block. psql does NOT substitute :variables inside dollar-quoted strings,
|
|
# so a DO $$ ... :'role' ... $$ body would be sent to the server literally.
|
|
docker exec -i -e ENV_DB_PASSWORD="$PW" "$DB_CID" \
|
|
psql -v ON_ERROR_STOP=1 -U thermograph -d postgres \
|
|
-v role="$TG_DB_USER" -v dbname="$TG_DB_NAME" <<'SQL'
|
|
\set pw `echo "$ENV_DB_PASSWORD"`
|
|
|
|
-- Role: create if absent.
|
|
SELECT format('CREATE ROLE %I LOGIN', :'role')
|
|
WHERE NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = :'role')
|
|
\gexec
|
|
|
|
-- Always (re)apply the password and the negative privileges, so a vault
|
|
-- rotation is just "rotate, redeploy, re-run this" — and so a role that was
|
|
-- created by hand with more rights than it should have gets corrected.
|
|
ALTER ROLE :"role" WITH LOGIN PASSWORD :'pw' NOSUPERUSER NOCREATEDB NOCREATEROLE;
|
|
|
|
-- CREATE DATABASE cannot run inside a transaction block, hence \gexec here too.
|
|
SELECT format('CREATE DATABASE %I OWNER %I', :'dbname', :'role')
|
|
WHERE NOT EXISTS (SELECT 1 FROM pg_database WHERE datname = :'dbname')
|
|
\gexec
|
|
SQL
|
|
|
|
# Extension + privileges have to run INSIDE the new database, hence a second
|
|
# connection. CREATE EXTENSION needs superuser, which is why it is done here
|
|
# rather than left to the app's own migration to attempt as the env role.
|
|
docker exec -i "$DB_CID" \
|
|
psql -v ON_ERROR_STOP=1 -U thermograph -d "$TG_DB_NAME" \
|
|
-v role="$TG_DB_USER" -v dbname="$TG_DB_NAME" -v rorole="${TG_DB_USER}_ro" <<'SQL'
|
|
CREATE EXTENSION IF NOT EXISTS timescaledb;
|
|
|
|
-- Nobody but this environment's roles connects to this database. Without this,
|
|
-- PUBLIC retains CONNECT and any role on the instance could read it.
|
|
REVOKE CONNECT ON DATABASE :"dbname" FROM PUBLIC;
|
|
GRANT CONNECT ON DATABASE :"dbname" TO :"role";
|
|
|
|
-- The role owns the database but not necessarily the public schema, which is
|
|
-- owned by the bootstrap superuser on a fresh database; Alembic needs to create
|
|
-- tables in it.
|
|
ALTER SCHEMA public OWNER TO :"role";
|
|
|
|
-- The read-only role that ad-hoc queries use (ops/dbq.sh). Every environment
|
|
-- gets one, named <role>_ro, so a human or an agent poking at data cannot
|
|
-- write. Without it, beta's queries would have had to connect as the OWNER —
|
|
-- read-write on its own database — losing a guarantee prod and dev already had
|
|
-- purely because beta was newer.
|
|
SELECT format('CREATE ROLE %I LOGIN', :'rorole')
|
|
WHERE NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = :'rorole')
|
|
\gexec
|
|
ALTER ROLE :"rorole" WITH LOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE;
|
|
|
|
GRANT CONNECT ON DATABASE :"dbname" TO :"rorole";
|
|
GRANT USAGE ON SCHEMA public TO :"rorole";
|
|
GRANT SELECT ON ALL TABLES IN SCHEMA public TO :"rorole";
|
|
GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO :"rorole";
|
|
-- Future tables too: without this, every new migration would create a table the
|
|
-- read-only role cannot see, and the omission would only surface as a confusing
|
|
-- "permission denied" months later.
|
|
ALTER DEFAULT PRIVILEGES FOR ROLE :"role" IN SCHEMA public
|
|
GRANT SELECT ON TABLES TO :"rorole";
|
|
ALTER DEFAULT PRIVILEGES FOR ROLE :"role" IN SCHEMA public
|
|
GRANT SELECT ON SEQUENCES TO :"rorole";
|
|
SQL
|
|
|
|
echo "==> OK: ${TG_DB_USER} owns ${TG_DB_NAME} (timescaledb enabled, PUBLIC revoked)"
|
|
echo " Verify isolation:"
|
|
echo " docker exec $DB_CID psql -U ${TG_DB_USER} -d thermograph -c 'select 1' # must FAIL"
|