diff --git a/CHANGELOG.md b/CHANGELOG.md index 78f45de06..dca350a5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ release's per-component sections are labelled beneath its heading. - Six foreign keys now have an index on the referencing column, so deleting a parent row looks its children up instead of scanning the child table: identity aliases and subjects, org memberships and agents, installed package requirements, and the task a handoff came from. - The control plane no longer polls the database once per watch subscriber: every watcher shares one version read per 250 ms, so an idle instance drops from roughly 180 transactions a second to a handful while a resource change still reaches every watcher on the same schedule. +- The bundled install now runs the services as a least-privilege database role that can read and write rows but cannot change the schema; a separate migration role owns the schema and applies migrations, and every connection names its service in `pg_stat_activity`. The installer provisions both roles, and re-running it moves an existing bundled install onto them, so a compromised service no longer holds a credential that owns the cluster. ## [1.62.0] - 2026-11-10 diff --git a/Taskfile.yml b/Taskfile.yml index a7a6f0f40..0462d26da 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -146,7 +146,7 @@ tasks: done dev:postgres: - desc: Start PostgreSQL and wait for readiness + desc: Start PostgreSQL, wait for readiness, provision the database roles cmds: - docker compose up -d postgres - | @@ -160,6 +160,15 @@ tasks: done echo "postgres did not become healthy within 60s" >&2 exit 1 + - | + # docker-compose.yml creates the dev database with a superuser named + # archie; provision the least-privilege roles deployments/dev.toml + # points at. Idempotent, so this also upgrades a volume created before + # the role split. + go run ./cmd/archie-state-store provision \ + -admin-url "postgres://archie:archie@127.0.0.1:5432/archie?sslmode=disable" \ + -runtime-url "postgres://archie_runtime:archie@127.0.0.1:5432/archie?sslmode=disable" \ + -migration-url "postgres://archie_migrator:archie@127.0.0.1:5432/archie?sslmode=disable" dev:wait-state: internal: true diff --git a/cmd/archie-state-store/main.go b/cmd/archie-state-store/main.go index 8c2568cdf..a3ee8bd25 100644 --- a/cmd/archie-state-store/main.go +++ b/cmd/archie-state-store/main.go @@ -19,10 +19,13 @@ import ( func main() { os.Exit(run()) } func run() int { - // A leading non-flag argument selects a recovery subcommand; anything else, + // A leading non-flag argument selects a subcommand; anything else, // including no arguments at all, serves the State Store contract. An - // unrecognised word is refused rather than ignored, so a mistyped recovery - // command never leaves an operator watching a server start instead. + // unrecognised word is refused rather than ignored, so a mistyped command + // never leaves an operator watching a server start instead. + if args := os.Args[1:]; statestore.IsProvisionArgs(args) { + return statestore.RunProvision(context.Background(), args[1:], os.Stdout, os.Stderr) + } if args := os.Args[1:]; statestore.IsAccessResetArgs(args) { return statestore.RunAccessReset(context.Background(), args[2:], os.Stderr) } @@ -36,7 +39,7 @@ func run() int { flag.StringVar(&options.Listen, "listen", "", "state store gRPC listen address (defaults to [services.state].listen, else 127.0.0.1:9090)") flag.StringVar(&options.ReadyAddr, "ready-addr", "", "optional readiness HTTP listen address (e.g. 127.0.0.1:9091)") flag.Usage = func() { - fmt.Fprintf(flag.CommandLine.Output(), "usage: archie-state-store [flags] # serve the State Store gRPC contract\n\noffline recovery: archie-state-store -h | access reset -h\n\n") + fmt.Fprintf(flag.CommandLine.Output(), "usage: archie-state-store [flags] # serve the State Store gRPC contract\n\ndatabase roles: archie-state-store provision -h\noffline recovery: archie-state-store -h | access reset -h\n\n") flag.PrintDefaults() } flag.Parse() diff --git a/config.example.toml b/config.example.toml index 1ab6dd86d..d226f32c3 100644 --- a/config.example.toml +++ b/config.example.toml @@ -7,7 +7,12 @@ # Secrets are never written here: each is a {engine, key} reference. The # built-in "env" engine reads key as an environment variable. -# database_url = "postgres://archie:archie@localhost:5432/archie?sslmode=disable" +# The URL the services connect as: a least-privilege role, created by +# `archie-state-store provision`. migration_database_url is the role that owns +# the schema and runs migrations; when it is absent, database_url does both, +# which is what a single-role install does. +# database_url = "postgres://archie_runtime:secret@localhost:5432/archie?sslmode=disable" +# migration_database_url = "postgres://archie_migrator:secret@localhost:5432/archie?sslmode=disable" # work_dir = "~/.local/share/archie/work" # state_dir = "~/.local/share/archie" diff --git a/deployments/INSTRUCTIONS.md b/deployments/INSTRUCTIONS.md index b50f1d56c..9c45883d8 100644 --- a/deployments/INSTRUCTIONS.md +++ b/deployments/INSTRUCTIONS.md @@ -30,7 +30,10 @@ that reference is on a registry that needs a login -- see () — `standalone.toml` (no forge, the simplest start), `single-forge-github.toml` and `docker-nats-stack.toml` cover the common cases. Every one needs - PostgreSQL 18 at `database_url`. Copy whichever fits to: + PostgreSQL 18 with the `archie_migrator` and `archie_runtime` roles created + once by `archie-state-store provision` (see `deployments/README.md`); + `docker compose up -d postgres` plus that command gives the local default. + Copy whichever fits to: ```bash mkdir -p ~/.config/archie diff --git a/deployments/README.md b/deployments/README.md index ea93cb4c5..456e691f5 100644 --- a/deployments/README.md +++ b/deployments/README.md @@ -42,7 +42,9 @@ systemctl --user start archied Start order does not matter: a service waits for its peers and reports them as degraded meanwhile. Every template needs PostgreSQL 18 at `database_url`; -`docker compose up -d postgres` provides the default one. +`docker compose up -d postgres` provides the default server. Its two login +roles are created once per database by `archie-state-store provision` -- see +[Database roles](#database-roles). The State Store service (`archie-state-store`) owns the task data in the PostgreSQL database `database_url` names and serves the `StateStore` gRPC @@ -54,6 +56,35 @@ gateway address, because a container cannot reach the host's loopback. A non-loopback address needs `[tls]` certificates from your own CA; see `config.example.toml`. +## Database roles + +The services do not connect as the database owner. `archie-state-store +provision` creates, or reconciles, two login roles in an existing PostgreSQL +database and hands the schema to one of them: + +- `archie_migrator` owns the schema and its objects and runs the goose + migrations; +- `archie_runtime` may read and write rows (SELECT, INSERT, UPDATE, DELETE, + sequence USAGE, function EXECUTE) and has no CREATE privilege, so it cannot + change the schema. + +Run it once per database with a role allowed to create roles and take ownership +of the schema (the database owner or a superuser), then point the templates at +the two roles: `database_url` is the runtime URL and `migration_database_url` +the migration one. The command is idempotent, so re-running it after an upgrade +reconciles objects a migration added. + +```bash +archie-state-store provision \ + -admin-url "postgres://owner:secret@db:5432/archie" \ + -runtime-url "postgres://archie_runtime:secret@db:5432/archie" \ + -migration-url "postgres://archie_migrator:secret@db:5432/archie" +``` + +The bundled installer and `task dev` run this for you. A configuration with only +`database_url` keeps working exactly as before: that one role applies the +schema and serves, which is what an install that has not split the roles does. + ## Running the dashboard The dashboard is served by the standalone UI Service (`archie-ui`), not by diff --git a/deployments/dev.toml b/deployments/dev.toml index 11dc77fff..0e6e9d7c7 100644 --- a/deployments/dev.toml +++ b/deployments/dev.toml @@ -9,12 +9,16 @@ # [web] entirely. # `task dev` runs archie-state-store against this overlay, and the State Store -# fails closed without a PostgreSQL URL. This must reach the `postgres` +# fails closed without a PostgreSQL URL. These must reach the `postgres` # service in docker-compose.yml, which `task dev` brings up first via -# dev:postgres -- keep the URL and that service in agreement. +# dev:postgres -- keep them and that service in agreement. dev:postgres also +# provisions the two roles: the services serve as archie_runtime and migrate +# as archie_migrator, while the Compose-created archie role is only the +# admin that created them. bot_user = "archie-dev" -database_url = "postgres://archie:archie@127.0.0.1:5432/archie?sslmode=disable" +database_url = "postgres://archie_runtime:archie@127.0.0.1:5432/archie?sslmode=disable" +migration_database_url = "postgres://archie_migrator:archie@127.0.0.1:5432/archie?sslmode=disable" [web] listen = "127.0.0.1:8484" diff --git a/deployments/docker-nats-stack.toml b/deployments/docker-nats-stack.toml index 071fc515e..644a7dddf 100644 --- a/deployments/docker-nats-stack.toml +++ b/deployments/docker-nats-stack.toml @@ -16,9 +16,12 @@ bot_user = "archie" work_dir = "~/.local/share/archie/work" state_dir = "~/.local/share/archie" # NATS store, task logs # The State Store's PostgreSQL connection (PostgreSQL 18, required). -# `docker compose up -d postgres` provides this local default; point it at -# your own Postgres 18 server otherwise. -database_url = "postgres://archie:archie@127.0.0.1:5432/archie?sslmode=disable" +# `docker compose up -d postgres` provides the local default; point it at +# your own Postgres 18 server otherwise. The services serve as archie_runtime +# and migrations run as archie_migrator; create both once with +# `archie-state-store provision -h` (deployments/README.md). +database_url = "postgres://archie_runtime:archie@127.0.0.1:5432/archie?sslmode=disable" +migration_database_url = "postgres://archie_migrator:archie@127.0.0.1:5432/archie?sslmode=disable" [services.gateway] target = "127.0.0.1:8585" diff --git a/deployments/single-forge-github.toml b/deployments/single-forge-github.toml index 95c112723..04efac69e 100644 --- a/deployments/single-forge-github.toml +++ b/deployments/single-forge-github.toml @@ -8,9 +8,12 @@ work_dir = "~/.local/share/archie/work" state_dir = "~/.local/share/archie" # NATS store, task logs # The State Store's PostgreSQL connection (PostgreSQL 18, required). -# `docker compose up -d postgres` provides this local default; point it at -# your own Postgres 18 server otherwise. -database_url = "postgres://archie:archie@127.0.0.1:5432/archie?sslmode=disable" +# `docker compose up -d postgres` provides the local default; point it at +# your own Postgres 18 server otherwise. The services serve as archie_runtime +# and migrations run as archie_migrator; create both once with +# `archie-state-store provision -h` (deployments/README.md). +database_url = "postgres://archie_runtime:archie@127.0.0.1:5432/archie?sslmode=disable" +migration_database_url = "postgres://archie_migrator:archie@127.0.0.1:5432/archie?sslmode=disable" poll_interval = "60s" label = "archie" bot_user = "archie-bot" diff --git a/deployments/standalone.toml b/deployments/standalone.toml index 8368a609e..53a0aeda8 100644 --- a/deployments/standalone.toml +++ b/deployments/standalone.toml @@ -15,9 +15,12 @@ bot_user = "archie" work_dir = "~/.local/share/archie/work" state_dir = "~/.local/share/archie" # NATS store, task logs # PostgreSQL 18 connection (required). -# `docker compose up -d postgres` provides this local default; point it at -# your own Postgres 18 server otherwise. -database_url = "postgres://archie:archie@127.0.0.1:5432/archie?sslmode=disable" +# `docker compose up -d postgres` provides the local default; point it at +# your own Postgres 18 server otherwise. The services serve as archie_runtime +# and migrations run as archie_migrator; create both once with +# `archie-state-store provision -h` (deployments/README.md). +database_url = "postgres://archie_runtime:archie@127.0.0.1:5432/archie?sslmode=disable" +migration_database_url = "postgres://archie_migrator:archie@127.0.0.1:5432/archie?sslmode=disable" [services.gateway] target = "127.0.0.1:8585" diff --git a/docker-compose.yml b/docker-compose.yml index de4026b40..4bc5958ba 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -19,8 +19,9 @@ services: # The standalone archie-state-store process's database. PostgreSQL 18 is # the required minimum and target; `postgres.Open` refuses anything older. # `task dev` brings this up before the State Store via dev:postgres. The - # credentials and database name below are the dev default mirrored by - # deployments/dev.toml's database_url -- change both together. + # This superuser is the dev database's admin: `task dev` runs archie-state-store + # provision after first start to create the least-privilege roles the + # mirroring deployments/dev.toml URLs name -- change them together. postgres: image: postgres:18 environment: diff --git a/e2e/stack.sh b/e2e/stack.sh index 5b458e771..296d3a308 100755 --- a/e2e/stack.sh +++ b/e2e/stack.sh @@ -72,7 +72,10 @@ start_pg() { 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" - sed "s#^database_url = .*#database_url = \"postgres://archie@127.0.0.1:$pgport/archie?sslmode=disable\"#" \ + # 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) @@ -101,6 +104,16 @@ 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 @@ -123,6 +136,7 @@ start() { 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 diff --git a/install.sh b/install.sh index 56dafa002..62a61325c 100755 --- a/install.sh +++ b/install.sh @@ -91,6 +91,17 @@ export ARCHIE_INSTANCE="${INSTANCE}" ARCHIE_CONFIG_DIR="${XDG_CONFIG_HOME}/${INSTANCE}" ARCHIE_DATA_DIR="${XDG_DATA_HOME}/${INSTANCE}" ENV_FILE="${ARCHIE_CONFIG_DIR}/env" +# The database roles a bundled install serves from: the least-privilege role +# the services connect as, and the role that owns the schema and runs +# migrations. The names mirror postgres.RuntimeRole/MigrationRole, which setup +# writes into the bundled configuration. +RUNTIME_ROLE=archie_runtime +MIGRATION_ROLE=archie_migrator +# The credential the bundled PostgreSQL is created with and the installer uses +# once, to create the two roles. It lives in this Compose-only file, which the +# systemd units never load, so no service holds a credential that owns the +# cluster. +PG_ADMIN_ENV="${ARCHIE_DATA_DIR}/postgres.env" # unit maps a base unit name to this instance's: unchanged for the default # instance, suffixed with the name after archie- otherwise. @@ -266,10 +277,55 @@ else echo " [SKIP] ${ARCHIE_CONFIG_DIR}/config.toml already exists. Re-run 'archied setup' to change it." fi +# provision_database reconciles the two database roles a bundled install +# serves from: an archie_migrator that owns the schema and runs goose, and an +# archie_runtime with row access only. It also moves the superuser password out +# of the env file the units load: a config written before the split connects as +# the bundled superuser, so its password is rotated to the Compose-only one +# once the roles exist. Idempotent, so re-running the installer reconciles an +# already-split install. +provision_database() { + local role_pw admin_pw connect_pw split=false + role_pw="$(sed -n "s/^PGPASSWORD='\([^']*\)'$/\1/p" "${ENV_FILE}" | tail -n1)" + admin_pw="$(sed -n "s/^POSTGRES_PASSWORD='\([^']*\)'$/\1/p" "${PG_ADMIN_ENV}" | tail -n1)" + if [ -z "${role_pw}" ] || [ -z "${admin_pw}" ]; then + echo " [WARN] Cannot provision database roles: ${ENV_FILE} needs PGPASSWORD and ${PG_ADMIN_ENV} needs POSTGRES_PASSWORD." >&2 + return + fi + if ! grep -q '^migration_database_url' "${ARCHIE_CONFIG_DIR}/config.toml"; then + split=true + fi + # A pre-split install authenticates as the superuser with the env file's + # PGPASSWORD; a split one authenticates with the Compose-only password. + if [ "${split}" = true ]; then connect_pw="${role_pw}"; else connect_pw="${admin_pw}"; fi + echo "==> Provisioning the database roles..." + if ! PGPASSWORD="${connect_pw}" ARCHIE_NEW_ADMIN_PASSWORD="${admin_pw}" \ + "${ARCHIE_BIN_DIR}/archie-state-store" provision \ + -admin-url "postgres://archie@/archie?host=${PG_SOCKET_DIR}" \ + -runtime-url "postgres://${RUNTIME_ROLE}:${role_pw}@/archie?host=${PG_SOCKET_DIR}" \ + -migration-url "postgres://${MIGRATION_ROLE}:${role_pw}@/archie?host=${PG_SOCKET_DIR}" \ + -rotate-admin-password-env ARCHIE_NEW_ADMIN_PASSWORD; then + echo " [WARN] Database role provisioning failed; not restarting the services until it succeeds. Re-run the installer once the database accepts the admin credential." >&2 + AUTO_START=false + return + fi + if [ "${split}" = true ]; then + if grep -q '^database_url = "postgres://archie@' "${ARCHIE_CONFIG_DIR}/config.toml"; then + sed -i "s#^database_url = \"postgres://archie@#database_url = \"postgres://${RUNTIME_ROLE}@#" "${ARCHIE_CONFIG_DIR}/config.toml" + sed -i "/^database_url = /a migration_database_url = \"postgres://${MIGRATION_ROLE}@/archie?host=${PG_SOCKET_DIR}\"" "${ARCHIE_CONFIG_DIR}/config.toml" + echo " [OK] config.toml now connects as ${RUNTIME_ROLE} and migrates as ${MIGRATION_ROLE}." + else + echo " [WARN] Could not move database_url to ${RUNTIME_ROLE}; set database_url and migration_database_url by hand." >&2 + fi + fi +} + # 4. PostgreSQL. Every service fails closed without it. The bundled database # has no network at all: the services reach it through its Unix socket, # mounted at PG_SOCKET_DIR, so it publishes no port and cannot collide with -# another PostgreSQL. Its password is the PGPASSWORD setup generated. +# another PostgreSQL. The superuser it is created with is only the installer's +# bootstrap credential; the services connect as the two provisioned roles and +# read their password from the env file's PGPASSWORD as before. PG_SOCKET_DIR="${ARCHIE_DATA_DIR}/postgres" PG_COMPOSE="${ARCHIE_DATA_DIR}/postgres.yml" if ! grep -qF "host=${PG_SOCKET_DIR}" "${ARCHIE_CONFIG_DIR}/config.toml"; then @@ -280,6 +336,15 @@ elif ! command -v docker &>/dev/null; then else echo "==> Starting the bundled PostgreSQL 18..." mkdir -p "${PG_SOCKET_DIR}" + # The superuser password is generated here rather than reused from setup: it + # is the one credential that must never be in the env file the units load. + # Written before the first start so a failed run can be retried with it. + if [ ! -f "${PG_ADMIN_ENV}" ]; then + previous_umask="$(umask)" + umask 077 + printf "POSTGRES_PASSWORD='%s'\n" "$(od -An -N24 -tx1 /dev/urandom | tr -d ' \n')" >"${PG_ADMIN_ENV}" + umask "${previous_umask}" + fi cat >"${PG_COMPOSE}" <<'EOF' services: postgres: @@ -287,7 +352,7 @@ services: network_mode: none environment: POSTGRES_USER: archie - POSTGRES_PASSWORD: ${PGPASSWORD:-} + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-} POSTGRES_DB: archie POSTGRES_INITDB_ARGS: --auth-local=scram-sha-256 PGDATA: /var/lib/postgresql/data/pgdata @@ -304,10 +369,12 @@ services: volumes: pg_data: EOF - if ! docker compose -p "${INSTANCE}" --env-file "${ENV_FILE}" -f "${PG_COMPOSE}" up -d --wait; then + if ! docker compose -p "${INSTANCE}" --env-file "${PG_ADMIN_ENV}" -f "${PG_COMPOSE}" up -d --wait; then echo " [WARN] The bundled PostgreSQL did not start, so the services are installed but not started." >&2 echo " Check: docker compose -p "${INSTANCE}" -f ${PG_COMPOSE} logs" >&2 AUTO_START=false + else + provision_database fi fi @@ -495,6 +562,9 @@ echo "Installation Details:" echo " - Binaries : ${ARCHIE_BIN_DIR}/{archied,archie-state-store,archie-gateway,archie-ui,archie-messaging}" echo " - Config : ${ARCHIE_CONFIG_DIR}/config.toml" echo " - Secrets : ${ENV_FILE}" +if [ -f "${PG_ADMIN_ENV}" ]; then + echo " - DB admin : ${PG_ADMIN_ENV} # compose-only, never loaded by the units" +fi echo " - Data : ${ARCHIE_DATA_DIR}/" echo "" diff --git a/internal/app/archiegateway/chat_runtime.go b/internal/app/archiegateway/chat_runtime.go index 195e16df4..5ae5042dd 100644 --- a/internal/app/archiegateway/chat_runtime.go +++ b/internal/app/archiegateway/chat_runtime.go @@ -21,7 +21,7 @@ import ( ) func (b *server) openChatSessions(ctx context.Context) error { - pool, err := postgres.OpenMigrated(ctx, b.cfg.DatabaseURL, "the conversation store") + pool, err := postgres.OpenMigrated(ctx, b.cfg.DatabaseURL, b.cfg.MigrationURL(), postgres.AppGateway) if err != nil { return fmt.Errorf("open conversation store: %w", err) } diff --git a/internal/app/statestore/pool.go b/internal/app/statestore/pool.go index 6f2e673aa..f3e82df7f 100644 --- a/internal/app/statestore/pool.go +++ b/internal/app/statestore/pool.go @@ -9,13 +9,15 @@ import ( // openStateStorePool opens the State Store process's one process-scoped // PostgreSQL pool, applies the schema and claims serve ownership of the // database for the process's life, so a second State Store against the same -// database refuses to start. A missing or invalid database_url or a migration -// failure is a startup failure. +// database refuses to start. The schema is applied through +// migration_database_url when the configuration splits the migration role from +// the serving one. A missing +// or invalid database_url or a migration failure is a startup failure. // // The pool is stored on boot.pg and closed at shutdown; nothing opens a second // pool per subsystem. func (b *server) openStateStorePool(ctx context.Context) error { - pool, err := postgres.OpenMigrated(ctx, b.cfg.DatabaseURL, "the State Store") + pool, err := postgres.OpenMigrated(ctx, b.cfg.DatabaseURL, b.cfg.MigrationURL(), postgres.AppStateStore) if err != nil { b.log.ErrorContext(ctx, "open state store postgres pool", "err", err) return err diff --git a/internal/app/statestore/provision.go b/internal/app/statestore/provision.go new file mode 100644 index 000000000..20207844a --- /dev/null +++ b/internal/app/statestore/provision.go @@ -0,0 +1,99 @@ +package statestore + +import ( + "context" + "errors" + "flag" + "fmt" + "io" + "os" + "strings" + + "github.com/samcharles93/archie-core/internal/infrastructure/postgres" +) + +// provisionCommand is the argv[1] token that runs the one-time database role +// setup instead of serving. Provisioning has to happen before the first +// migration and needs a credential the services never hold, so it cannot be a +// migration and is not part of any service's boot. +const provisionCommand = "provision" + +// provisionUsage is the whole flag surface of the provisioning command. +const provisionUsage = `usage: archie-state-store provision [flags] + +provision reconciles the PostgreSQL roles an install serves from: + + - a migration role that owns the schema and runs goose; + - a runtime role with row access only (SELECT, INSERT, UPDATE, DELETE, plus + sequence USAGE and function EXECUTE), which the services connect as. + +It creates the two roles when missing, hands every existing public-schema +object to the migration role, and sets the default privileges that keep the +objects future migrations create usable by the runtime role. It is idempotent: +re-running it on a provisioned database reconciles it back to that state. + +The admin URL needs a role that may create roles and take ownership of the +schema (the database owner or a superuser). It is a bootstrap credential: it is +not one of the two roles above and the services never read it. + +Passwords come from each URL's userinfo, or from PGPASSWORD when the URL does +not carry one. + + -admin-url URL role that creates and owns (database owner or superuser) + -runtime-url URL least-privilege role the services connect as + -migration-url URL role that owns the schema and runs migrations + -rotate-admin-password-env NAME + replace the admin role's password with the value of + environment variable NAME once provisioning succeeds +` + +// IsProvisionArgs reports whether args selects the role provisioning command. +func IsProvisionArgs(args []string) bool { + return len(args) > 0 && args[0] == provisionCommand +} + +// RunProvision performs the role provisioning and returns the process exit +// code: 0 provisioned, 1 the provisioning failed, 2 the command line was +// wrong. +func RunProvision(ctx context.Context, args []string, stdout, stderr io.Writer) int { + flags := flag.NewFlagSet(provisionCommand, flag.ContinueOnError) + flags.SetOutput(stderr) + flags.Usage = func() { fmt.Fprint(stderr, provisionUsage) } + var options postgres.ProvisionOptions + flags.StringVar(&options.AdminURL, "admin-url", "", "PostgreSQL URL of a role allowed to create roles and own the schema (database owner or superuser)") + flags.StringVar(&options.RuntimeURL, "runtime-url", "", "PostgreSQL URL of the least-privilege role the services connect as") + flags.StringVar(&options.MigrationURL, "migration-url", "", "PostgreSQL URL of the role that owns the schema and runs migrations") + rotateEnv := flags.String("rotate-admin-password-env", "", "name of an environment variable holding the admin role's new password") + if err := flags.Parse(args); err != nil { + // A requested help is not a wrong command line: the usage above is the + // answer. + if errors.Is(err, flag.ErrHelp) { + return 0 + } + return 2 + } + if flags.NArg() > 0 { + fmt.Fprintf(stderr, "provision: unexpected argument %q\n", flags.Arg(0)) + return 2 + } + if *rotateEnv != "" { + password, ok := os.LookupEnv(*rotateEnv) + if !ok || password == "" { + fmt.Fprintf(stderr, "provision: %s is unset or empty\n", *rotateEnv) + return 2 + } + options.NewAdminPassword = password + } + + result, err := postgres.Provision(ctx, options) + if err != nil { + fmt.Fprintf(stderr, "provision: %v\n", err) + return 1 + } + if len(result.RolesCreated) > 0 { + fmt.Fprintf(stdout, "provision: created %s\n", strings.Join(result.RolesCreated, ", ")) + } + fmt.Fprintf(stdout, "provision: %s owns the schema; %s may read and write rows but not change the schema\n", result.MigrationRole, result.RuntimeRole) + fmt.Fprintf(stdout, "provision: reconciled %d tables, %d sequences and %d functions\n", result.Tables, result.Sequences, result.Functions) + return 0 +} diff --git a/internal/app/statestore/recovery.go b/internal/app/statestore/recovery.go index 5f3c084c2..e629d82d4 100644 --- a/internal/app/statestore/recovery.go +++ b/internal/app/statestore/recovery.go @@ -4,12 +4,13 @@ // starting, and the in-band remedy -- replaying an earlier revision while the // State Store is up -- is unavailable in exactly the case that needs it // -// Every operation but import works on the PostgreSQL database database_url -// names (state_store_recovery_postgres.go). The ones that write refuse while a -// serving process owns the store. Backup is the exception and deliberately so: -// the update installer cannot stop the process that runs it, so the snapshot -// that makes a failed update recoverable is taken against a serving store -// through a consistent copy. +// Every operation but import works on the PostgreSQL database +// migration_database_url names, or database_url when the install has not split +// the roles (state_store_recovery_postgres.go). The ones that write refuse +// while a serving process owns the store. Backup is the exception and +// deliberately so: the update installer cannot stop the process that runs it, +// so the snapshot that makes a failed update recoverable is taken against a +// serving store through a consistent copy. // // The control-plane server both validate and rollback read through is built by // openStateStoreControlPlane -- the same root helper the served path uses -- so diff --git a/internal/app/statestore/recovery_postgres.go b/internal/app/statestore/recovery_postgres.go index 7ba6f5f5d..a18f74cfa 100644 --- a/internal/app/statestore/recovery_postgres.go +++ b/internal/app/statestore/recovery_postgres.go @@ -11,15 +11,18 @@ import ( ) // runPostgresRecovery performs one recovery operation on the PostgreSQL -// database the configuration names. There is no automatic schema rollback: -// goose Down drops tables, so the way back from a migration is restoring the -// snapshot taken before it, which loses every write made after that snapshot. +// database the configuration names. It connects as the migration role +// (migration_database_url, falling back to database_url): restoring and +// rolling back replace schema objects, which only the schema's owner may do. +// There is no automatic schema rollback: goose Down drops tables, so the way +// back from a migration is restoring the snapshot taken before it, which loses +// every write made after that snapshot. func runPostgresRecovery(ctx context.Context, options RecoveryOptions) (string, error) { cfg, err := bootConfig(ctx, options) if err != nil { return "", err } - url := cfg.DatabaseURL + url := cfg.MigrationURL() if url == "" { return "", errors.New("database_url is required: the configuration names no PostgreSQL database") } @@ -55,7 +58,7 @@ func runPostgresRecovery(ctx context.Context, options RecoveryOptions) (string, // openRecoveryPool opens the database without migrating it: recovery inspects // or repairs the schema it finds, and refuses one newer than this binary. func openRecoveryPool(ctx context.Context, url string) (*pgxpool.Pool, int64, error) { - pool, err := postgres.Open(ctx, url) + pool, err := postgres.Open(ctx, url, postgres.AppStateStore+"-recovery") if err != nil { return nil, 0, err } @@ -119,7 +122,9 @@ func SchemaVersion(ctx context.Context, options RecoveryOptions) (int64, error) if cfg.DatabaseURL == "" { return 0, errors.New("database_url is required: the configuration names no PostgreSQL database") } - pool, err := postgres.Open(ctx, cfg.DatabaseURL) + // The updater asks this while the services serve, so it reads the version + // through the runtime role: SELECT on goose's table is all it needs. + pool, err := postgres.Open(ctx, cfg.DatabaseURL, postgres.AppStateStore+"-schema") if err != nil { return 0, err } diff --git a/internal/config/config.go b/internal/config/config.go index dd0ee8ad8..d737b80e0 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -366,10 +366,15 @@ type Config struct { // embedded NATS store and its endpoint file, the task-log registry, and // the readiness disk probe's data target. Bootstrap-only. StateDir string `toml:"state_dir" yaml:"state_dir"` - // DatabaseURL is the PostgreSQL URL the State Store opens at boot. Required; + // DatabaseURL is the PostgreSQL URL the services open at boot. Required; // bootstrap-only. - DatabaseURL string `toml:"database_url" yaml:"database_url"` - PollInterval Duration `toml:"poll_interval" yaml:"poll_interval"` + DatabaseURL string `toml:"database_url" yaml:"database_url"` + // MigrationDatabaseURL is the PostgreSQL URL migrations and schema-changing + // recovery run under: a role that owns the schema, not the one serving + // requests. Empty falls back to DatabaseURL, which is how a single-role + // install keeps working. Bootstrap-only. + MigrationDatabaseURL string `toml:"migration_database_url" yaml:"migration_database_url"` + PollInterval Duration `toml:"poll_interval" yaml:"poll_interval"` // Label marks issues archie should pick up. Label string `toml:"label" yaml:"label"` BotUser string `toml:"bot_user" yaml:"bot_user"` @@ -428,6 +433,16 @@ type Config struct { Curators []CuratorDefinition `toml:"curators" yaml:"curators" json:"curators,omitempty"` } +// MigrationURL is the URL migrations and schema-changing recovery run under. +// It falls back to DatabaseURL so an install that has not split the runtime +// and migration roles behaves as it always has. +func (c Config) MigrationURL() string { + if c.MigrationDatabaseURL != "" { + return c.MigrationDatabaseURL + } + return c.DatabaseURL +} + // IdentityConfig is a per-identity configuration subset. Forge access comes // from the identity's organisation connections; runtime settings come from // the shared control plane. diff --git a/internal/infrastructure/configuration/denied_keys.go b/internal/infrastructure/configuration/denied_keys.go index 4a01c1729..d20586b71 100644 --- a/internal/infrastructure/configuration/denied_keys.go +++ b/internal/infrastructure/configuration/denied_keys.go @@ -4,6 +4,7 @@ package configuration // to the reason shown to the operator. var DeniedKeys = map[string]string{ "database_url": "Set in the config file. Needs a restart.", + "migration_database_url": "Set in the config file. Needs a restart.", "state_dir": "Set in the config file. Needs a restart.", "work_dir": "Set in the config file. Needs a restart.", "forge.type": "Needs a restart.", diff --git a/internal/infrastructure/configuration/setup/setup.go b/internal/infrastructure/configuration/setup/setup.go index bcb522158..977152798 100644 --- a/internal/infrastructure/configuration/setup/setup.go +++ b/internal/infrastructure/configuration/setup/setup.go @@ -12,6 +12,7 @@ import ( "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/infrastructure/configuration" "github.com/samcharles93/archie-core/internal/infrastructure/configuration/tomlwrite" + "github.com/samcharles93/archie-core/internal/infrastructure/postgres" ) // SecretSink receives the secret values setup collects. Put buffers; the @@ -82,11 +83,15 @@ func RunParams(ctx context.Context, p Prompter, discovery ModelDiscovery, secret } } - dbURL, err := stepDatabase(ctx, p, secrets, existing.DatabaseURL, params.DatabaseURL) + dbURL, migrationURL, err := stepDatabase(ctx, p, secrets, existing.DatabaseURL, params.DatabaseURL) if err != nil { return nil, err } - add(tableEdits{"": {"database_url": tomlwrite.String(dbURL)}}) + dbEdits := tableEdits{"": {"database_url": tomlwrite.String(dbURL)}} + if migrationURL != "" { + dbEdits[""]["migration_database_url"] = tomlwrite.String(migrationURL) + } + add(dbEdits) botUser := params.BotUser if strings.TrimSpace(botUser) == "" { @@ -135,14 +140,15 @@ func RunParams(ctx context.Context, p Prompter, discovery ModelDiscovery, secret return edits, nil } -// stepDatabase returns the PostgreSQL URL. A blank answer on a fresh install -// means the bundled database. On a re-run with an existing URL the bundled -// database is an explicit choice rather than something reachable only by -// typing its socket URL; keeping the existing URL is the first option, so -// the default answer is unchanged. -func stepDatabase(ctx context.Context, p Prompter, secrets SecretSink, existing, param string) (string, error) { +// stepDatabase returns the PostgreSQL URLs the install serves from: the +// runtime role's URL and, for the bundled database, the migration role's. A +// blank answer on a fresh install means the bundled database. On a re-run with +// an existing URL the bundled database is an explicit choice rather than +// something reachable only by typing its socket URL; keeping the existing URL +// is the first option, so the default answer is unchanged. +func stepDatabase(ctx context.Context, p Prompter, secrets SecretSink, existing, param string) (runtimeURL, migrationURL string, err error) { if strings.TrimSpace(param) != "" { - return param, nil + return param, "", nil } if strings.TrimSpace(existing) != "" { choice, err := p.Select(ctx, "Database:", []string{ @@ -151,48 +157,53 @@ func stepDatabase(ctx context.Context, p Prompter, secrets SecretSink, existing, "Enter a different PostgreSQL 18 URL", }) if err != nil { - return "", fmt.Errorf("setup: database choice: %w", err) + return "", "", fmt.Errorf("setup: database choice: %w", err) } switch choice { case 0: - return existing, nil + return existing, "", nil case 1: return bundledDatabase(secrets) default: url, err := p.ReadLine(ctx, "PostgreSQL 18 URL: ", "") if err != nil { - return "", fmt.Errorf("setup: database url: %w", err) + return "", "", fmt.Errorf("setup: database url: %w", err) } if strings.TrimSpace(url) == "" { // A blank answer keeps the existing URL, as it always has. - return existing, nil + return existing, "", nil } - return url, nil + return url, "", nil } } prompt := "PostgreSQL 18 URL (blank for the bundled database with a generated password): " url, err := p.ReadLine(ctx, prompt, "") if err != nil { - return "", fmt.Errorf("setup: database url: %w", err) + return "", "", fmt.Errorf("setup: database url: %w", err) } if strings.TrimSpace(url) != "" { - return url, nil + return url, "", nil } return bundledDatabase(secrets) } // bundledDatabase switches to the bundled database: no network, reached -// through its Unix socket, with a generated password that goes only to the -// env file as PGPASSWORD, which pgx and Compose both read, so config.toml -// holds no secret. -func bundledDatabase(secrets SecretSink) (string, error) { +// through its Unix socket. The services connect as the least-privilege runtime +// role and migrations run as the schema-owning migration role; one generated +// password goes only to the env file as PGPASSWORD, which pgx reads for both +// passwordless URLs, so config.toml holds no secret. The superuser credential +// the bundled server is created with lives in the installer's Compose-only +// env file, never here. +func bundledDatabase(secrets SecretSink) (runtimeURL, migrationURL string, err error) { pw := make([]byte, 24) if _, err := rand.Read(pw); err != nil { - return "", fmt.Errorf("setup: generate database password: %w", err) + return "", "", fmt.Errorf("setup: generate database password: %w", err) } password := hex.EncodeToString(pw) if err := secrets.Put("env", "PGPASSWORD", password); err != nil { - return "", fmt.Errorf("setup: store database password: %w", err) + return "", "", fmt.Errorf("setup: store database password: %w", err) } - return "postgres://archie@/archie?host=" + configuration.DefaultPostgresSocketDir(), nil + socket := configuration.DefaultPostgresSocketDir() + return "postgres://" + postgres.RuntimeRole + "@/archie?host=" + socket, + "postgres://" + postgres.MigrationRole + "@/archie?host=" + socket, nil } diff --git a/internal/infrastructure/configuration/setup/setup_test.go b/internal/infrastructure/configuration/setup/setup_test.go index aa8120eb0..59cf40fb0 100644 --- a/internal/infrastructure/configuration/setup/setup_test.go +++ b/internal/infrastructure/configuration/setup/setup_test.go @@ -77,7 +77,11 @@ const ( ) func bundledURL() string { - return "postgres://archie@/archie?host=" + configuration.DefaultPostgresSocketDir() + return "postgres://archie_runtime@/archie?host=" + configuration.DefaultPostgresSocketDir() +} + +func bundledMigrationURL() string { + return "postgres://archie_migrator@/archie?host=" + configuration.DefaultPostgresSocketDir() } // TestRunParamsDatabaseChoice pins the re-run database step at the setup API @@ -92,12 +96,13 @@ func TestRunParamsDatabaseChoice(t *testing.T) { // existing is the prefilled database_url; empty means fresh install. existing string // param is Params.DatabaseURL (the -database-url flag). - param string - selectAnswers map[string]int - lineAnswers map[string]string - wantURL string - wantPassword bool - wantChoice bool // a database Select menu must be presented + param string + selectAnswers map[string]int + lineAnswers map[string]string + wantURL string + wantMigrationURL string // empty means the flow must not write one + wantPassword bool + wantChoice bool // a database Select menu must be presented }{ { name: "re-run keep existing is the default choice", @@ -107,12 +112,13 @@ func TestRunParamsDatabaseChoice(t *testing.T) { wantChoice: true, }, { - name: "re-run switch to the bundled database", - existing: existing, - selectAnswers: map[string]int{databasePrompt: bundledChoice}, - wantURL: bundledURL(), - wantPassword: true, - wantChoice: true, + name: "re-run switch to the bundled database", + existing: existing, + selectAnswers: map[string]int{databasePrompt: bundledChoice}, + wantURL: bundledURL(), + wantMigrationURL: bundledMigrationURL(), + wantPassword: true, + wantChoice: true, }, { name: "re-run enter a different URL", @@ -131,12 +137,13 @@ func TestRunParamsDatabaseChoice(t *testing.T) { wantChoice: true, }, { - name: "fresh install blank answer means the bundled database", - existing: "", - lineAnswers: map[string]string{"PostgreSQL 18 URL (blank for the bundled database with a generated password): ": ""}, - wantURL: bundledURL(), - wantPassword: true, - wantChoice: false, + name: "fresh install blank answer means the bundled database", + existing: "", + lineAnswers: map[string]string{"PostgreSQL 18 URL (blank for the bundled database with a generated password): ": ""}, + wantURL: bundledURL(), + wantMigrationURL: bundledMigrationURL(), + wantPassword: true, + wantChoice: false, }, { name: "database-url flag wins without prompting", @@ -167,15 +174,28 @@ func TestRunParamsDatabaseChoice(t *testing.T) { t.Fatalf("RunParams: %v", err) } - var gotURL string + var gotURL, gotMigrationURL string for _, e := range edits { - if e.Table == "" && e.Key == "database_url" { + if e.Table != "" { + continue + } + switch e.Key { + case "database_url": gotURL = e.Value + case "migration_database_url": + gotMigrationURL = e.Value } } if gotURL != tomlwrite.String(tt.wantURL) { t.Errorf("database_url = %s, want %s", gotURL, tomlwrite.String(tt.wantURL)) } + wantMigrationURL := "" + if tt.wantMigrationURL != "" { + wantMigrationURL = tomlwrite.String(tt.wantMigrationURL) + } + if gotMigrationURL != wantMigrationURL { + t.Errorf("migration_database_url = %q, want %q", gotMigrationURL, wantMigrationURL) + } password, hasPassword := sink.puts["env/PGPASSWORD"] if hasPassword != tt.wantPassword { diff --git a/internal/infrastructure/postgres/maintenance.go b/internal/infrastructure/postgres/maintenance.go index bf211ce44..2d8676522 100644 --- a/internal/infrastructure/postgres/maintenance.go +++ b/internal/infrastructure/postgres/maintenance.go @@ -139,7 +139,7 @@ func Restore(ctx context.Context, databaseURL, from string) error { if err != nil { return err } - pool, err := Open(ctx, databaseURL) + pool, err := Open(ctx, databaseURL, AppStateStore+"-restore") if err != nil { return fmt.Errorf("postgres: restore cannot confirm every service is stopped: %w", err) } diff --git a/internal/infrastructure/postgres/postgres.go b/internal/infrastructure/postgres/postgres.go index f5d442ab2..e453c4a50 100644 --- a/internal/infrastructure/postgres/postgres.go +++ b/internal/infrastructure/postgres/postgres.go @@ -18,6 +18,18 @@ import ( // minServerVersion is PostgreSQL 18 in server_version_num form. const minServerVersion = 180000 +// The application_name each service sets on its connections, so +// pg_stat_activity says which service a connection belongs to. PostgreSQL +// truncates the value at 63 bytes, which every name below is inside. +const ( + AppStateStore = "archie-state-store" + AppGateway = "archie-gateway" +) + +// migrationSuffix marks the transient pool that applies migrations, so the +// activity view distinguishes it from the pool that serves traffic. +const migrationSuffix = "-migrate" + //go:embed migrations/*.sql var migrations embed.FS @@ -42,8 +54,21 @@ func checkServerVersion(num int) error { } // Open connects a pool to url and refuses a server older than PostgreSQL 18. -func Open(ctx context.Context, url string) (*pgxpool.Pool, error) { - pool, err := pgxpool.New(ctx, url) +// applicationName is the service name every connection reports to +// pg_stat_activity; it overrides any name the URL carries, so no connection +// arrives unnamed because a deployment template forgot one. +func Open(ctx context.Context, url, applicationName string) (*pgxpool.Pool, error) { + poolConfig, err := pgxpool.ParseConfig(url) + if err != nil { + return nil, fmt.Errorf("postgres: open: %w", err) + } + if applicationName != "" { + if poolConfig.ConnConfig.RuntimeParams == nil { + poolConfig.ConnConfig.RuntimeParams = map[string]string{} + } + poolConfig.ConnConfig.RuntimeParams["application_name"] = applicationName + } + pool, err := pgxpool.NewWithConfig(ctx, poolConfig) if err != nil { return nil, fmt.Errorf("postgres: open: %w", err) } @@ -60,20 +85,44 @@ func Open(ctx context.Context, url string) (*pgxpool.Pool, error) { } // OpenMigrated opens and migrates the pool a service owns its tables in. -// service names the owner in the error an empty database_url produces. -func OpenMigrated(ctx context.Context, url, service string) (*pgxpool.Pool, error) { - if url == "" { - return nil, fmt.Errorf("database_url is required: %s is Postgres-only", service) +// applicationName names the service in pg_stat_activity. +// +// runtimeURL is the pool the service serves from; migrationURL is the role +// that owns the schema and applies migrations. An empty migrationURL falls +// back to runtimeURL: an install that has not split the roles keeps working +// exactly as before, applying the schema through its one role. +func OpenMigrated(ctx context.Context, runtimeURL, migrationURL, applicationName string) (*pgxpool.Pool, error) { + if runtimeURL == "" { + return nil, fmt.Errorf("database_url is required: %s is PostgreSQL-only", applicationName) } - pool, err := Open(ctx, url) + if migrationURL == "" { + migrationURL = runtimeURL + } + if migrationURL == runtimeURL { + // One role for both jobs: migrate on the serving pool, as before. + pool, err := Open(ctx, runtimeURL, applicationName) + if err != nil { + return nil, err + } + if err := Migrate(ctx, pool, Migrations()); err != nil { + pool.Close() + return nil, err + } + return pool, nil + } + // The migration role is not the serving role: migrate on a short-lived + // pool of its own, then open the serving pool. The two do not overlap, so a + // runtime role with no schema privileges never carries goose's work. + migrationPool, err := Open(ctx, migrationURL, applicationName+migrationSuffix) if err != nil { return nil, err } - if err := Migrate(ctx, pool, Migrations()); err != nil { - pool.Close() - return nil, err + migrateErr := Migrate(ctx, migrationPool, Migrations()) + migrationPool.Close() + if migrateErr != nil { + return nil, migrateErr } - return pool, nil + return Open(ctx, runtimeURL, applicationName) } // Migrate applies the goose migrations in fsys. A session-level advisory lock diff --git a/internal/infrastructure/postgres/provision.go b/internal/infrastructure/postgres/provision.go new file mode 100644 index 000000000..c74354d24 --- /dev/null +++ b/internal/infrastructure/postgres/provision.go @@ -0,0 +1,534 @@ +package postgres + +import ( + "context" + "crypto/hmac" + "crypto/rand" + "crypto/sha256" + "encoding/base64" + "errors" + "fmt" + "net/url" + "os" + "strings" + + "github.com/jackc/pgx/v5" + "golang.org/x/crypto/pbkdf2" +) + +// The roles the bundled installer provisions. An install pointed at its own +// database may name different roles in its URLs; these are the names setup +// writes into the bundled configuration. +const ( + RuntimeRole = "archie_runtime" + MigrationRole = "archie_migrator" +) + +// provisionAdminSuffix names the provisioning connection in pg_stat_activity. +const provisionAdminSuffix = "-provision" + +// ProvisionOptions names the three roles one provision run reconciles. +type ProvisionOptions struct { + // AdminURL is a role allowed to create roles and take ownership of the + // schema: the database owner or a superuser. It is a bootstrap credential, + // used once into the database and never stored by the services, and it + // must not be one of the two roles being provisioned. + AdminURL string + // RuntimeURL is the least-privilege role the services connect as. + RuntimeURL string + // MigrationURL is the role that owns the schema and runs migrations. + MigrationURL string + // NewAdminPassword, when set, replaces the admin role's password at the end + // of a successful run. The installer uses it to move the superuser + // credential out of the env file the services load. + NewAdminPassword string +} + +// ProvisionResult reports what one provision run reconciled. +type ProvisionResult struct { + RuntimeRole string + MigrationRole string + AdminRole string + // RolesCreated lists the roles this run created; a re-run creates none. + RolesCreated []string + // Tables, Sequences and Functions count the public objects this run took + // ownership of. A fresh database has none yet; the migration role's default + // privileges cover the objects its first migration creates. + Tables int + Sequences int + Functions int +} + +// provisionRoles is the role set a run reconciles, with the password each +// login must have afterwards. +type provisionRoles struct { + admin provisionRole + runtime provisionRole + migration provisionRole +} + +type provisionRole struct { + name string + password string +} + +// Provision makes the database safe to serve from: it creates the migration +// role that owns the schema and the runtime role with only row access, then +// hands every existing object to the migration role and sets the default +// privileges that keep objects created by future migrations usable by the +// runtime. It is idempotent: running it on a provisioned database reconciles +// it back to that state. +// +// The connection URLs also carry each role's password (or PGPASSWORD when they +// do not). Provisioning is the only place a superuser password is needed; it +// is not stored anywhere the services read. +func Provision(ctx context.Context, opts ProvisionOptions) (ProvisionResult, error) { + roles, err := parseProvisionURLs(opts, os.Getenv) + if err != nil { + return ProvisionResult{}, err + } + pool, err := Open(ctx, opts.AdminURL, AppStateStore+provisionAdminSuffix) + if err != nil { + return ProvisionResult{}, err + } + defer pool.Close() + + tx, err := pool.Begin(ctx) + if err != nil { + return ProvisionResult{}, fmt.Errorf("postgres: provision: begin: %w", err) + } + defer func() { _ = tx.Rollback(ctx) }() + result, err := provisionTx(ctx, tx, roles, opts.NewAdminPassword) + if err != nil { + return ProvisionResult{}, err + } + if err := tx.Commit(ctx); err != nil { + return ProvisionResult{}, fmt.Errorf("postgres: provision: commit: %w", err) + } + return result, nil +} + +// provisionTx performs one provisioning run inside tx, so a failure at any +// step leaves the database as it was. +func provisionTx(ctx context.Context, tx pgx.Tx, roles provisionRoles, newAdminPassword string) (ProvisionResult, error) { + result := ProvisionResult{ + RuntimeRole: roles.runtime.name, + MigrationRole: roles.migration.name, + AdminRole: roles.admin.name, + } + for _, role := range []provisionRole{roles.runtime, roles.migration} { + created, err := ensureProvisionRole(ctx, tx, role) + if err != nil { + return ProvisionResult{}, err + } + if created { + result.RolesCreated = append(result.RolesCreated, role.name) + } + } + // The schema owner must change before the objects move: PostgreSQL only + // lets an object's owner (or a superuser) hand it to the migration role. + for _, statement := range schemaStatements(roles) { + if _, err := tx.Exec(ctx, statement); err != nil { + return ProvisionResult{}, fmt.Errorf("postgres: provision %s: %w", statement, err) + } + } + objects, err := schemaObjects(ctx, tx, roles.migration.name) + if err != nil { + return ProvisionResult{}, err + } + for _, object := range objects { + statement := object.ownerStatement(roles.migration.name) + if _, err := tx.Exec(ctx, statement); err != nil { + return ProvisionResult{}, fmt.Errorf("postgres: provision %s: %w", statement, err) + } + switch object.kind { + case objectTable, objectForeignTable, objectPartitionedTable: + result.Tables++ + case objectSequence: + result.Sequences++ + default: + result.Functions++ + } + } + if newAdminPassword != "" { + if err := setRolePassword(ctx, tx, roles.admin.name, newAdminPassword); err != nil { + return ProvisionResult{}, err + } + } + if err := verifyProvision(ctx, tx, roles); err != nil { + return ProvisionResult{}, err + } + return result, nil +} + +// ensureProvisionRole creates role as a non-superuser login when it is missing, +// and reconciles an existing role back to that shape. It refuses a role that +// is still a superuser: demoting a superuser is the operator's decision, not a +// side effect of an install. +func ensureProvisionRole(ctx context.Context, tx pgx.Tx, role provisionRole) (bool, error) { + verifier, err := scramVerifier(role.password) + if err != nil { + return false, err + } + var superuser bool + err = tx.QueryRow(ctx, "SELECT rolsuper FROM pg_roles WHERE rolname = $1", role.name).Scan(&superuser) + switch { + case errors.Is(err, pgx.ErrNoRows): + statement := "CREATE ROLE " + quoteIdent(role.name) + " LOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE PASSWORD " + quoteLiteral(verifier) + if _, err := tx.Exec(ctx, statement); err != nil { + return false, fmt.Errorf("postgres: provision create role %s: %w", role.name, err) + } + return true, nil + case err != nil: + return false, fmt.Errorf("postgres: provision read role %s: %w", role.name, err) + case superuser: + return false, fmt.Errorf("postgres: provision refuses role %q: it is a superuser; name a least-privilege role instead", role.name) + } + // An existing role may have been created by hand with more than it needs. + statement := "ALTER ROLE " + quoteIdent(role.name) + " LOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE PASSWORD " + quoteLiteral(verifier) + if _, err := tx.Exec(ctx, statement); err != nil { + return false, fmt.Errorf("postgres: provision update role %s: %w", role.name, err) + } + return false, nil +} + +// setRolePassword stores a new password for role without sending the plaintext +// to the server as SQL. +func setRolePassword(ctx context.Context, tx pgx.Tx, role, password string) error { + verifier, err := scramVerifier(password) + if err != nil { + return err + } + statement := "ALTER ROLE " + quoteIdent(role) + " PASSWORD " + quoteLiteral(verifier) + if _, err := tx.Exec(ctx, statement); err != nil { + return fmt.Errorf("postgres: provision rotate %s password: %w", role, err) + } + return nil +} + +// schemaStatements is the fixed part of a provision run: who owns the schema +// and what the runtime role may do in it. Every statement is idempotent, so a +// re-run is a reconciliation rather than a second install. +func schemaStatements(roles provisionRoles) []string { + runtime := quoteIdent(roles.runtime.name) + migration := quoteIdent(roles.migration.name) + return []string{ + // The migration role owns the namespace it manufactures the schema in. + "ALTER SCHEMA public OWNER TO " + migration, + // PostgreSQL 15 made this the default for a new database; an install + // created before that may still let every role create objects here. + "REVOKE CREATE ON SCHEMA public FROM PUBLIC", + "GRANT USAGE ON SCHEMA public TO " + runtime, + "GRANT USAGE, CREATE ON SCHEMA public TO " + migration, + // The runtime role reads and writes rows, and nothing else: no TRUNCATE, + // no ownership, no CREATE. The app's queries never ask for more. + "GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO " + runtime, + "GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO " + runtime, + "GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA public TO " + runtime, + // Tables and sequences a future migration creates are owned by the + // migration role, so its default privileges are what keeps the runtime + // role able to use them without a later grant. + "ALTER DEFAULT PRIVILEGES FOR ROLE " + migration + " IN SCHEMA public GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO " + runtime, + "ALTER DEFAULT PRIVILEGES FOR ROLE " + migration + " IN SCHEMA public GRANT USAGE, SELECT ON SEQUENCES TO " + runtime, + "ALTER DEFAULT PRIVILEGES FOR ROLE " + migration + " IN SCHEMA public GRANT EXECUTE ON FUNCTIONS TO " + runtime, + } +} + +// objectKind is a public-schema object class the migration role must own. +type objectKind string + +const ( + objectTable objectKind = "table" + objectPartitionedTable objectKind = "partitioned table" + objectSequence objectKind = "sequence" + objectView objectKind = "view" + objectMaterializedView objectKind = "materialized view" + objectForeignTable objectKind = "foreign table" + objectFunction objectKind = "function" + objectProcedure objectKind = "procedure" +) + +// schemaObject is one public-schema object whose ownership moves to the +// migration role. +type schemaObject struct { + kind objectKind + // reference is the object's name for a relation, or its regprocedure text + // for a function; both are engine-generated, never operator input. + reference string +} + +// ownerStatement renders the ALTER that hands object to role. Relations are +// quoted; a function reference is already the regprocedure form PostgreSQL +// renders, argument types included. +func (o schemaObject) ownerStatement(role string) string { + if o.kind == objectFunction || o.kind == objectProcedure { + return "ALTER " + o.kind.alterKeyword() + " " + o.reference + " OWNER TO " + quoteIdent(role) + } + return "ALTER " + o.kind.alterKeyword() + " " + pgx.Identifier{"public", o.reference}.Sanitize() + " OWNER TO " + quoteIdent(role) +} + +// alterKeyword is the object class's ALTER target. A partitioned table is +// altered as a table: PostgreSQL has no ALTER PARTITIONED TABLE. +func (k objectKind) alterKeyword() string { + switch k { + case objectTable, objectPartitionedTable: + return "TABLE" + case objectSequence: + return "SEQUENCE" + case objectView: + return "VIEW" + case objectMaterializedView: + return "MATERIALIZED VIEW" + case objectForeignTable: + return "FOREIGN TABLE" + case objectProcedure: + return "PROCEDURE" + default: + return "FUNCTION" + } +} + +// schemaObjects lists the public-schema objects someone other than the +// migration role still owns. Objects an extension owns are left alone, and +// indexes, row types and sequences owned by a table column follow the table's +// owner, so the relation loop does not repeat them. A partition has its own +// dependency on the partitioned table, so it is listed and transferred too. +func schemaObjects(ctx context.Context, tx pgx.Tx, migrationRole string) ([]schemaObject, error) { + rows, err := tx.Query(ctx, ` + SELECT c.relkind, c.relname + FROM pg_class c + JOIN pg_namespace n ON n.oid = c.relnamespace + WHERE n.nspname = 'public' + AND c.relkind IN ('r', 'p', 'S', 'v', 'm', 'f') + AND pg_get_userbyid(c.relowner) <> $1 + AND NOT EXISTS ( + SELECT 1 FROM pg_depend d + WHERE d.classid = 'pg_class'::regclass + AND d.objid = c.oid + AND d.deptype = 'e') + AND (c.relkind <> 'S' OR NOT EXISTS ( + SELECT 1 FROM pg_depend d + WHERE d.classid = 'pg_class'::regclass + AND d.objid = c.oid + AND d.refclassid = 'pg_class'::regclass + AND d.deptype IN ('a', 'i'))) + ORDER BY c.relkind, c.relname`, migrationRole) + if err != nil { + return nil, fmt.Errorf("postgres: provision list schema objects: %w", err) + } + var objects []schemaObject + for rows.Next() { + var relkind, name string + if err := rows.Scan(&relkind, &name); err != nil { + rows.Close() + return nil, fmt.Errorf("postgres: provision list schema objects: %w", err) + } + kind, ok := relationKinds[relkind] + if !ok { + // An object class this provisioner does not know how to hand over; + // leaving it behind is visible in verifyProvision's counts. + continue + } + objects = append(objects, schemaObject{kind: kind, reference: name}) + } + rows.Close() + if err := rows.Err(); err != nil { + return nil, fmt.Errorf("postgres: provision list schema objects: %w", err) + } + + rows, err = tx.Query(ctx, ` + SELECT p.prokind, p.oid::regprocedure::text + FROM pg_proc p + JOIN pg_namespace n ON n.oid = p.pronamespace + WHERE n.nspname = 'public' + AND p.prokind IN ('f', 'p') + AND pg_get_userbyid(p.proowner) <> $1 + AND NOT EXISTS ( + SELECT 1 FROM pg_depend d + WHERE d.classid = 'pg_proc'::regclass + AND d.objid = p.oid + AND d.deptype = 'e') + ORDER BY p.oid::regprocedure::text`, migrationRole) + if err != nil { + return nil, fmt.Errorf("postgres: provision list schema functions: %w", err) + } + defer rows.Close() + for rows.Next() { + var prokind, reference string + if err := rows.Scan(&prokind, &reference); err != nil { + return nil, fmt.Errorf("postgres: provision list schema functions: %w", err) + } + kind := objectFunction + if prokind == "p" { + kind = objectProcedure + } + objects = append(objects, schemaObject{kind: kind, reference: reference}) + } + if err := rows.Err(); err != nil { + return nil, fmt.Errorf("postgres: provision list schema functions: %w", err) + } + return objects, nil +} + +// relationKinds maps pg_class.relkind to the object class an ALTER names. +var relationKinds = map[string]objectKind{ + "r": objectTable, + "p": objectPartitionedTable, + "S": objectSequence, + "v": objectView, + "m": objectMaterializedView, + "f": objectForeignTable, +} + +// verifyProvision refuses a state that would leave the runtime role able to +// change the schema, or either role able to reach superuser powers through a +// role membership. +func verifyProvision(ctx context.Context, tx pgx.Tx, roles provisionRoles) error { + for _, role := range []provisionRole{roles.runtime, roles.migration} { + var superuser bool + if err := tx.QueryRow(ctx, "SELECT rolsuper FROM pg_roles WHERE rolname = $1", role.name).Scan(&superuser); err != nil { + return fmt.Errorf("postgres: provision verify role %s: %w", role.name, err) + } + if superuser { + return fmt.Errorf("postgres: provision left role %q a superuser", role.name) + } + if err := verifyNoSuperuserMembership(ctx, tx, role.name); err != nil { + return err + } + } + var canCreate bool + if err := tx.QueryRow(ctx, "SELECT has_schema_privilege($1, 'public', 'CREATE')", roles.runtime.name).Scan(&canCreate); err != nil { + return fmt.Errorf("postgres: provision verify runtime schema privileges: %w", err) + } + if canCreate { + return fmt.Errorf("postgres: provision left %q able to create schema objects; migrations would not be the migration role's alone", roles.runtime.name) + } + var migrationCanCreate bool + if err := tx.QueryRow(ctx, "SELECT has_schema_privilege($1, 'public', 'CREATE')", roles.migration.name).Scan(&migrationCanCreate); err != nil { + return fmt.Errorf("postgres: provision verify migration schema privileges: %w", err) + } + if !migrationCanCreate { + return fmt.Errorf("postgres: provision left %q unable to create schema objects", roles.migration.name) + } + return nil +} + +// verifyNoSuperuserMembership refuses a role that inherits a superuser's +// powers through role membership, which rolsuper alone does not show. +func verifyNoSuperuserMembership(ctx context.Context, tx pgx.Tx, role string) error { + rows, err := tx.Query(ctx, ` + SELECT parent.rolname + FROM pg_auth_members m + JOIN pg_roles parent ON parent.oid = m.roleid + JOIN pg_roles member ON member.oid = m.member + WHERE member.rolname = $1 AND parent.rolsuper`, role) + if err != nil { + return fmt.Errorf("postgres: provision verify role memberships: %w", err) + } + defer rows.Close() + if rows.Next() { + var parent string + if err := rows.Scan(&parent); err != nil { + return fmt.Errorf("postgres: provision verify role memberships: %w", err) + } + return fmt.Errorf("postgres: provision refuses role %q: it is a member of superuser role %q", role, parent) + } + return rows.Err() +} + +// parseProvisionURLs reads the three roles out of the URLs the installer +// passes, taking a password from the URL's userinfo or, for a passwordless +// URL, from PGPASSWORD. It refuses a plan that would leave the runtime role +// with powers the services never need. +func parseProvisionURLs(opts ProvisionOptions, getenv func(string) string) (provisionRoles, error) { + admin, err := roleFromURL(opts.AdminURL, "admin_url", getenv, false) + if err != nil { + return provisionRoles{}, err + } + runtime, err := roleFromURL(opts.RuntimeURL, "runtime_url", getenv, true) + if err != nil { + return provisionRoles{}, err + } + migration, err := roleFromURL(opts.MigrationURL, "migration_url", getenv, true) + if err != nil { + return provisionRoles{}, err + } + if runtime.name == migration.name { + return provisionRoles{}, fmt.Errorf("postgres: provision: runtime_url and migration_url name the same role %q; they must differ", runtime.name) + } + for _, role := range []provisionRole{runtime, migration} { + if role.name == admin.name { + return provisionRoles{}, fmt.Errorf("postgres: provision: %q is the admin role; runtime_url and migration_url must name the roles being provisioned", role.name) + } + } + return provisionRoles{admin: admin, runtime: runtime, migration: migration}, nil +} + +// roleFromURL extracts one login role from a PostgreSQL URL. requirePassword +// is set for the two provisioned roles, whose password must be known so the +// run can set it; the admin role may authenticate through the environment. +func roleFromURL(raw, label string, getenv func(string) string, requirePassword bool) (provisionRole, error) { + if strings.TrimSpace(raw) == "" { + return provisionRole{}, fmt.Errorf("postgres: provision: %s is required", label) + } + parsed, err := url.Parse(raw) + if err != nil || (parsed.Scheme != "postgres" && parsed.Scheme != "postgresql") { + return provisionRole{}, fmt.Errorf("postgres: provision: %s must be a postgres:// URL naming a login role", label) + } + if parsed.User == nil || parsed.User.Username() == "" { + return provisionRole{}, fmt.Errorf("postgres: provision: %s must name the role in its URL userinfo", label) + } + role := provisionRole{name: parsed.User.Username()} + if password, ok := parsed.User.Password(); ok && password != "" { + role.password = password + } else if password := getenv(databasePasswordEnv); password != "" { + role.password = password + } + if requirePassword && role.password == "" { + return provisionRole{}, fmt.Errorf("postgres: provision: %s carries no password and %s is unset", label, databasePasswordEnv) + } + return role, nil +} + +// databasePasswordEnv is the variable pgx and the PostgreSQL client tools read +// a connection password from when a URL does not carry one. +const databasePasswordEnv = "PGPASSWORD" + +// scramVerifier returns the SCRAM-SHA-256 verifier PostgreSQL stores for +// password. ALTER ROLE accepts a pre-computed verifier, so the plaintext never +// travels in a statement another session's statement log can capture. +func scramVerifier(password string) (string, error) { + if password == "" { + return "", errors.New("postgres: provision: a role password is required") + } + const iterations = 4096 + salt := make([]byte, 16) + if _, err := rand.Read(salt); err != nil { + return "", fmt.Errorf("postgres: provision: salt: %w", err) + } + salted := pbkdf2.Key([]byte(password), salt, iterations, sha256.Size, sha256.New) + storedKey := sha256.Sum256(hmacSHA256(salted, []byte("Client Key"))) + serverKey := hmacSHA256(salted, []byte("Server Key")) + return fmt.Sprintf("SCRAM-SHA-256$%d:%s$%s:%s", + iterations, + base64.StdEncoding.EncodeToString(salt), + base64.StdEncoding.EncodeToString(storedKey[:]), + base64.StdEncoding.EncodeToString(serverKey), + ), nil +} + +func hmacSHA256(key, message []byte) []byte { + mac := hmac.New(sha256.New, key) + mac.Write(message) + return mac.Sum(nil) +} + +// quoteIdent quotes an SQL identifier such as a role or table name. +func quoteIdent(name string) string { return pgx.Identifier{name}.Sanitize() } + +// quoteLiteral quotes an SQL string literal. scramVerifier's output is +// base64 and separators only, so it can never contain the quote being escaped; +// the escaping is here so a future caller cannot inject through the value. +func quoteLiteral(value string) string { + return "'" + strings.ReplaceAll(value, "'", "''") + "'" +} diff --git a/internal/infrastructure/postgres/provision_test.go b/internal/infrastructure/postgres/provision_test.go new file mode 100644 index 000000000..1c1e1fa8c --- /dev/null +++ b/internal/infrastructure/postgres/provision_test.go @@ -0,0 +1,264 @@ +package postgres + +import ( + "encoding/base64" + "regexp" + "strings" + "testing" +) + +// testProvisionRoles is the role set the statement tests reconcile. +func testProvisionRoles() provisionRoles { + return provisionRoles{ + admin: provisionRole{name: "archie", password: "admin-password"}, + runtime: provisionRole{name: RuntimeRole, password: "runtime-password"}, + migration: provisionRole{name: MigrationRole, password: "migration-password"}, + } +} + +// TestParseProvisionURLs pins what the installer may hand the provisioner: a +// role per URL, a password from the URL or PGPASSWORD, and a refusal for any +// plan that would leave the runtime role owning schema changes. +func TestParseProvisionURLs(t *testing.T) { + const ( + adminURL = "postgres://archie:admin@db.internal/archie" + runtimeURL = "postgres://archie_runtime:runtime@db.internal/archie" + migrationURL = "postgres://archie_migrator:migration@db.internal/archie" + ) + tests := []struct { + name string + opts ProvisionOptions + env map[string]string + want provisionRoles + wantErr string + }{ + { + name: "passwords come from the URL userinfo", + opts: ProvisionOptions{AdminURL: adminURL, RuntimeURL: runtimeURL, MigrationURL: migrationURL}, + want: provisionRoles{ + admin: provisionRole{name: "archie", password: "admin"}, + runtime: provisionRole{name: RuntimeRole, password: "runtime"}, + migration: provisionRole{name: MigrationRole, password: "migration"}, + }, + }, + { + name: "passwordless role URLs take PGPASSWORD", + opts: ProvisionOptions{ + AdminURL: "postgres://archie@/archie?host=/run/archie/postgres", + RuntimeURL: "postgres://archie_runtime@/archie?host=/run/archie/postgres", + MigrationURL: "postgres://archie_migrator@/archie?host=/run/archie/postgres", + }, + env: map[string]string{databasePasswordEnv: "from-env"}, + want: provisionRoles{ + admin: provisionRole{name: "archie", password: "from-env"}, + runtime: provisionRole{name: RuntimeRole, password: "from-env"}, + migration: provisionRole{name: MigrationRole, password: "from-env"}, + }, + }, + { + name: "an admin URL needs no password of its own", + opts: ProvisionOptions{ + AdminURL: "postgres://archie@/archie?host=/run/archie/postgres", + RuntimeURL: runtimeURL, + MigrationURL: migrationURL, + }, + want: provisionRoles{ + admin: provisionRole{name: "archie"}, + runtime: provisionRole{name: RuntimeRole, password: "runtime"}, + migration: provisionRole{name: MigrationRole, password: "migration"}, + }, + }, + { + name: "a runtime role with no password is refused", + opts: ProvisionOptions{AdminURL: adminURL, RuntimeURL: "postgres://archie_runtime@db.internal/archie", MigrationURL: migrationURL}, + wantErr: databasePasswordEnv, + }, + { + name: "the same role cannot be runtime and migration", + opts: ProvisionOptions{AdminURL: adminURL, RuntimeURL: runtimeURL, MigrationURL: "postgres://archie_runtime:other@db.internal/archie"}, + wantErr: "same role", + }, + { + name: "the admin role cannot be provisioned as runtime", + opts: ProvisionOptions{AdminURL: adminURL, RuntimeURL: adminURL, MigrationURL: migrationURL}, + wantErr: "admin role", + }, + { + name: "a non-PostgreSQL URL is refused", + opts: ProvisionOptions{AdminURL: "mysql://archie:admin@db.internal/archie", RuntimeURL: runtimeURL, MigrationURL: migrationURL}, + wantErr: "postgres:// URL", + }, + { + name: "a URL without a role is refused", + opts: ProvisionOptions{AdminURL: adminURL, RuntimeURL: "postgres://db.internal/archie", MigrationURL: migrationURL}, + wantErr: "userinfo", + }, + { + name: "a key=value connection string is refused, it names no role", + opts: ProvisionOptions{AdminURL: adminURL, RuntimeURL: "host=db.internal user=archie_runtime", MigrationURL: migrationURL}, + wantErr: "postgres:// URL", + }, + { + name: "a missing role URL is named in the error", + opts: ProvisionOptions{AdminURL: adminURL, RuntimeURL: runtimeURL}, + wantErr: "migration_url is required", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + getenv := func(key string) string { return tt.env[key] } + got, err := parseProvisionURLs(tt.opts, getenv) + if tt.wantErr != "" { + if err == nil || !strings.Contains(err.Error(), tt.wantErr) { + t.Fatalf("parseProvisionURLs error = %v, want one naming %q", err, tt.wantErr) + } + return + } + if err != nil { + t.Fatalf("parseProvisionURLs: %v", err) + } + if got != tt.want { + t.Errorf("parseProvisionURLs = %+v, want %+v", got, tt.want) + } + }) + } +} + +// TestSchemaStatements pins the privilege boundary: the runtime role reads and +// writes rows and nothing else, the migration role owns the schema, and the +// migration role's default privileges cover the objects future migrations +// create. A regression here is a security regression, not a formatting one. +func TestSchemaStatements(t *testing.T) { + roles := testProvisionRoles() + runtime := quoteIdent(roles.runtime.name) + migration := quoteIdent(roles.migration.name) + statements := schemaStatements(roles) + joined := strings.Join(statements, "\n") + + required := []string{ + "ALTER SCHEMA public OWNER TO " + migration, + "REVOKE CREATE ON SCHEMA public FROM PUBLIC", + "GRANT USAGE ON SCHEMA public TO " + runtime, + "GRANT USAGE, CREATE ON SCHEMA public TO " + migration, + "GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO " + runtime, + "GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO " + runtime, + "GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA public TO " + runtime, + "ALTER DEFAULT PRIVILEGES FOR ROLE " + migration + " IN SCHEMA public GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO " + runtime, + "ALTER DEFAULT PRIVILEGES FOR ROLE " + migration + " IN SCHEMA public GRANT USAGE, SELECT ON SEQUENCES TO " + runtime, + "ALTER DEFAULT PRIVILEGES FOR ROLE " + migration + " IN SCHEMA public GRANT EXECUTE ON FUNCTIONS TO " + runtime, + } + for _, statement := range required { + if !strings.Contains(joined, statement) { + t.Errorf("provisioning lacks %q", statement) + } + } + + // Every statement that mentions the runtime role must be a row-level grant + // or a default privilege: nothing may let it create, own, truncate or + // elevate. + forbiddenForRuntime := []string{" CREATE ", " SUPERUSER", " CREATEDB", " CREATEROLE", " OWNER TO ", "TRUNCATE", "ALL PRIVILEGES"} + for _, statement := range statements { + if !strings.Contains(statement, runtime) { + continue + } + for _, forbidden := range forbiddenForRuntime { + if strings.Contains(statement, forbidden) { + t.Errorf("statement grants the runtime role more than rows: %q", statement) + } + } + } + + // The default privileges are what make the next migration's tables usable + // without a hand-run grant; one per object class is the contract. + var defaults int + for _, statement := range statements { + if strings.HasPrefix(statement, "ALTER DEFAULT PRIVILEGES FOR ROLE "+migration) { + defaults++ + } + } + if defaults != 3 { + t.Errorf("default privileges for %s = %d statements, want tables, sequences and functions", roles.migration.name, defaults) + } +} + +// TestSchemaStatementsQuoteRoleNames pins that role names arriving in URLs are +// quoted as identifiers, not spliced into SQL. +func TestSchemaStatementsQuoteRoleNames(t *testing.T) { + roles := provisionRoles{ + runtime: provisionRole{name: `runtime"; DROP TABLE tasks; --`}, + migration: provisionRole{name: `migration"role`}, + } + joined := strings.Join(schemaStatements(roles), "\n") + if !strings.Contains(joined, `"runtime""; DROP TABLE tasks; --"`) { + t.Errorf("runtime role name is not quoted as one identifier:\n%s", joined) + } + if !strings.Contains(joined, `"migration""role"`) { + t.Errorf("migration role name is not quoted as one identifier:\n%s", joined) + } +} + +// TestOwnerStatement pins the ALTER an object gets when its ownership moves to +// the migration role, including PostgreSQL's keyword for each object class. +func TestOwnerStatement(t *testing.T) { + const role = MigrationRole + tests := []struct { + name string + object schemaObject + want string + }{ + {"table", schemaObject{kind: objectTable, reference: "tasks"}, `ALTER TABLE "public"."tasks" OWNER TO "archie_migrator"`}, + {"partitioned table alters as a table", schemaObject{kind: objectPartitionedTable, reference: "events"}, `ALTER TABLE "public"."events" OWNER TO "archie_migrator"`}, + {"sequence", schemaObject{kind: objectSequence, reference: "tasks_id_seq"}, `ALTER SEQUENCE "public"."tasks_id_seq" OWNER TO "archie_migrator"`}, + {"view", schemaObject{kind: objectView, reference: "open_tasks"}, `ALTER VIEW "public"."open_tasks" OWNER TO "archie_migrator"`}, + {"materialized view", schemaObject{kind: objectMaterializedView, reference: "summary"}, `ALTER MATERIALIZED VIEW "public"."summary" OWNER TO "archie_migrator"`}, + {"foreign table", schemaObject{kind: objectForeignTable, reference: "remote"}, `ALTER FOREIGN TABLE "public"."remote" OWNER TO "archie_migrator"`}, + {"function", schemaObject{kind: objectFunction, reference: "audit_json_diff(jsonb,jsonb)"}, `ALTER FUNCTION audit_json_diff(jsonb,jsonb) OWNER TO "archie_migrator"`}, + {"procedure", schemaObject{kind: objectProcedure, reference: "sweep(integer)"}, `ALTER PROCEDURE sweep(integer) OWNER TO "archie_migrator"`}, + {"a quoted relation name stays one identifier", schemaObject{kind: objectTable, reference: `bad"name`}, `ALTER TABLE "public"."bad""name" OWNER TO "archie_migrator"`}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := tt.object.ownerStatement(role); got != tt.want { + t.Errorf("ownerStatement = %q, want %q", got, tt.want) + } + }) + } +} + +// TestSCRAMVerifier pins that the provisioner stores PostgreSQL a SCRAM +// verifier rather than a plaintext password, and that the verifier has the +// shape the server accepts. +func TestSCRAMVerifier(t *testing.T) { + verifier, err := scramVerifier("correct horse battery staple") + if err != nil { + t.Fatalf("scramVerifier: %v", err) + } + if strings.Contains(verifier, "correct horse") { + t.Fatalf("verifier contains the plaintext password: %q", verifier) + } + shape := regexp.MustCompile(`^SCRAM-SHA-256\$4096:([A-Za-z0-9+/=]+)\$([A-Za-z0-9+/=]+):([A-Za-z0-9+/=]+)$`) + match := shape.FindStringSubmatch(verifier) + if match == nil { + t.Fatalf("verifier %q is not SCRAM-SHA-256$iterations:salt$stored:server", verifier) + } + for i, want := range map[int]int{1: 16, 2: 32, 3: 32} { + decoded, err := base64.StdEncoding.DecodeString(match[i]) + if err != nil { + t.Fatalf("part %d of the verifier does not decode: %v", i, err) + } + if len(decoded) != want { + t.Errorf("part %d decodes to %d bytes, want %d", i, len(decoded), want) + } + } + again, err := scramVerifier("correct horse battery staple") + if err != nil { + t.Fatalf("scramVerifier: %v", err) + } + if again == verifier { + t.Error("two verifiers of the same password are equal; the salt is not random") + } + + if _, err := scramVerifier(""); err == nil { + t.Error("scramVerifier accepted an empty password") + } +}