diff --git a/charts/trek/values.yaml b/charts/trek/values.yaml index 6abe14f01..8e20fb6d4 100644 --- a/charts/trek/values.yaml +++ b/charts/trek/values.yaml @@ -187,7 +187,10 @@ env: # its own Secret as TREK_DB_URL; with existingSecret, TREK reads TREK_DB_URL from that Secret # under existingSecretKey and the chart writes nothing. existingSecret wins over url. The # variable is required when either is set: a missing Secret key stops the pod instead of -# starting it on SQLite. +# starting it on SQLite. The role in the URL owns TREK's database and must not be a superuser +# (nor hold pg_execute_server_program or pg_write_server_files): a restore runs the backup's +# SQL as that role and refuses one that could reach the database host. The wiki page shows the +# SQL that creates such a role. PostgreSQL 14 to 18 is supported. # url: "postgres://trek:@postgres.example.internal:5432/trek?sslmode=require" database: url: "" diff --git a/docker-compose.postgres.yml b/docker-compose.postgres.yml index dc5aaa17f..4530d7cfd 100644 --- a/docker-compose.postgres.yml +++ b/docker-compose.postgres.yml @@ -1,12 +1,21 @@ # TREK on a Postgres database instead of the SQLite file. See the wiki page # Database-Postgres for what this changes, the requirements and the backups. # -# Set the database password in a .env file beside this file (or in the -# shell) before the first start: -# POSTGRES_PASSWORD= -# The password goes into TREK_DB_URL below, so keep it to URL-safe characters -# or percent-encode it. Without POSTGRES_PASSWORD the postgres image refuses to -# initialize its database, and TREK does not start. +# Set two passwords in a .env file beside this file (or in the shell) before +# the first start: +# POSTGRES_ADMIN_PASSWORD= the database's superuser, postgres +# POSTGRES_PASSWORD= TREK's own role, trek +# TREK connects as the role trek, which owns its database but is not a +# superuser: a backup TREK restores is a SQL script run by that role, and a +# superuser would let a crafted one run commands in the database container. +# The superuser only creates that role, on the database's first start +# (configs.trek-db-role below). TREK's password goes into TREK_DB_URL, so keep +# it to URL-safe characters or percent-encode it. Without either password the +# first start fails instead of creating a database nobody can reach. +# +# An install that started from an older version of this file runs TREK as the +# superuser trek; the wiki page Database-Postgres ("Moving a compose install +# off the superuser") shows how to move it to this layout. services: db: image: postgres:17 @@ -14,15 +23,21 @@ services: restart: unless-stopped environment: - POSTGRES_DB=${POSTGRES_DB:-trek} - - POSTGRES_USER=${POSTGRES_USER:-trek} - - POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-} + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=${POSTGRES_ADMIN_PASSWORD:-} + - TREK_DB_PASSWORD=${POSTGRES_PASSWORD:-} + configs: + - source: trek-db-role + target: /docker-entrypoint-initdb.d/10-trek-role.sh + # Executable, so the image runs it in a shell of its own instead of sourcing it. + mode: 0555 security_opt: - no-new-privileges:true # No ports are published: only the trek service on the compose network reaches the database. volumes: - trek-pgdata:/var/lib/postgresql/data healthcheck: - test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"] + test: ["CMD-SHELL", "pg_isready -U postgres -d $${POSTGRES_DB}"] interval: 10s timeout: 5s retries: 5 @@ -62,10 +77,10 @@ services: - TZ=${TZ:-UTC} - LOG_LEVEL=${LOG_LEVEL:-info} - ALLOWED_ORIGINS=${ALLOWED_ORIGINS:-} - # The database URL. Its host is the service name above; sslmode=disable is - # right on the compose network. Use sslmode=verify-full with sslrootcert= - # for a database reached over a network you do not control. - - TREK_DB_URL=postgres://${POSTGRES_USER:-trek}:${POSTGRES_PASSWORD:-}@db:5432/${POSTGRES_DB:-trek}?sslmode=disable + # The database URL, as the role trek. Its host is the service name above; + # sslmode=disable is right on the compose network. Use sslmode=verify-full + # with sslrootcert= for a database reached over a network you do not control. + - TREK_DB_URL=postgres://trek:${POSTGRES_PASSWORD:-}@db:5432/${POSTGRES_DB:-trek}?sslmode=disable # Scale-out is not supported: keep one trek container (see Database-Postgres). volumes: - ./data:/app/data @@ -83,5 +98,25 @@ services: retries: 3 start_period: 15s +# Run once by the postgres image, on the database's first start, as the +# superuser: creates the role trek, which owns TREK's database and its public +# schema, so it can create TREK's tables and its collation there, and nothing +# more. The password reaches psql as a variable, never spliced into the SQL. +configs: + trek-db-role: + content: | + #!/bin/sh + set -eu + if [ -z "$${TREK_DB_PASSWORD:-}" ]; then + echo 'POSTGRES_PASSWORD (the password of the role trek) is not set.' >&2 + exit 1 + fi + psql -v ON_ERROR_STOP=1 --no-psqlrc --username "$$POSTGRES_USER" --dbname "$$POSTGRES_DB" \ + --set=password="$$TREK_DB_PASSWORD" --set=database="$$POSTGRES_DB" <<'SQL' + CREATE ROLE trek LOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE PASSWORD :'password'; + ALTER DATABASE :"database" OWNER TO trek; + ALTER SCHEMA public OWNER TO trek; + SQL + volumes: trek-pgdata: