#!/usr/bin/env bash # Prepares a PostgreSQL service container for the wire-protocol tests that need more than the image's defaults # (BRIEF ยง6.3): one role per authentication method (MD5, cleartext, trust) with its pg_hba line ahead of the image's # catch-all SCRAM line, and TLS with a self-signed certificate valid for localhost and 127.0.0.1. # Usage: bash .github/scripts/postgres-ci-setup.sh CONTAINER SUPERUSER DATABASE CERTIFICATE_FOLDER # The certificate (server.crt) stays in CERTIFICATE_FOLDER: the verify-full test trusts it as its root. set -euo pipefail container=$1 superuser=$2 database=$3 certificates=$4 sql() { docker exec -i "$container" psql -v ON_ERROR_STOP=1 -U "$superuser" -d "$database" -Atq "$@"; } mkdir -p "$certificates" openssl req -x509 -newkey rsa:2048 -nodes -days 2 -subj "/CN=localhost" \ -addext "subjectAltName=DNS:localhost,IP:127.0.0.1" \ -keyout "$certificates/server.key" -out "$certificates/server.crt" 2>/dev/null data=$(sql -c "SHOW data_directory") hba=$(sql -c "SHOW hba_file") docker cp "$certificates/server.crt" "$container:$data/server.crt" docker cp "$certificates/server.key" "$container:$data/server.key" # The server refuses a key that others can read. docker exec "$container" sh -c "chown postgres:postgres '$data/server.crt' '$data/server.key' && chmod 600 '$data/server.key'" # The first matching line wins, so these go above the image's 'host all all all scram-sha-256'. docker exec "$container" sh -c "{ printf '%s\n' 'host all titano_md5 all md5' 'host all titano_clear all password' \ 'host all titano_trust all trust'; cat '$hba'; } > /tmp/pg_hba.conf && cat /tmp/pg_hba.conf > '$hba'" sql > /dev/null <<'SQL' SET password_encryption = 'md5'; CREATE ROLE titano_md5 LOGIN PASSWORD 'md5-ci'; RESET password_encryption; CREATE ROLE titano_clear LOGIN PASSWORD 'clear-ci'; CREATE ROLE titano_trust LOGIN; ALTER SYSTEM SET ssl = on; SELECT pg_reload_conf(); SQL # A reload that fails keeps the old settings silently: report the rejected lines, then wait for TLS to be on. errors=$(sql -c "SELECT line_number || ': ' || error FROM pg_hba_file_rules WHERE error IS NOT NULL UNION ALL SELECT name || ': ' || error FROM pg_file_settings WHERE error IS NOT NULL") if [ -n "$errors" ]; then echo "::error title=PostgreSQL setup::$errors" exit 1 fi for _ in $(seq 1 20); do if [ "$(sql -c "SHOW ssl")" = "on" ]; then echo "PostgreSQL in $container: MD5, cleartext and trust roles, TLS on" exit 0 fi sleep 0.5 done echo "::error title=PostgreSQL setup::TLS did not turn on after the reload" exit 1