From f84ebd17f9f6c07806b1523c84de7f260cf22bf4 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Sun, 24 Nov 2024 17:24:29 -0500 Subject: [PATCH] experiment generating multiple login users for pgbouncer --- db/docker-compose.yml | 13 +++++++++++-- db/pgbouncer/Dockerfile | 6 ++++++ db/pgbouncer/generate-config.sh | 22 ++++++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 db/pgbouncer/Dockerfile create mode 100755 db/pgbouncer/generate-config.sh diff --git a/db/docker-compose.yml b/db/docker-compose.yml index 93ab291..c1b1444 100644 --- a/db/docker-compose.yml +++ b/db/docker-compose.yml @@ -37,11 +37,20 @@ services: - traefik pgbouncer: - image: edoburu/pgbouncer:latest + build: + context: ./pgbouncer container_name: ${DB_BOUNCER?DB_BOUNCER} restart: always environment: - DATABASE_URL: postgresql://${HASURA_DB_USER?HASURA_DB_USER}:${HASURA_DB_PASSWORD?HASURA_DB_PASSWORD}@${DB_HOST?DB_HOST}:${DB_PORT?DB_PORT}/${FERRY_DB_NAME?FERRY_DB_NAME} + DB_USER: ${DB_USER?DB_USER} + DB_ROOT_PASSWORD: ${DB_ROOT_PASSWORD?DB_ROOT_PASSWORD} + FERRY_DB_USER: ${FERRY_DB_USER?FERRY_DB_USER} + FERRY_DB_PASSWORD: ${FERRY_DB_PASSWORD?FERRY_DB_PASSWORD} + HASURA_DB_USER: ${HASURA_DB_USER?HASURA_DB_USER} + HASURA_DB_PASSWORD: ${HASURA_DB_PASSWORD?HASURA_DB_PASSWORD} + DB_NAME: ${FERRY_DB_NAME?FERRY_DB_NAME} + DB_HOST: ${DB_HOST?DB_HOST} + DB_PORT: ${DB_PORT?DB_PORT} AUTH_TYPE: scram-sha-256 QUERY_WAIT_TIMEOUT: 600 MAX_CLIENT_CONN: 5000 diff --git a/db/pgbouncer/Dockerfile b/db/pgbouncer/Dockerfile new file mode 100644 index 0000000..5666ce2 --- /dev/null +++ b/db/pgbouncer/Dockerfile @@ -0,0 +1,6 @@ +FROM edoburu/pgbouncer:latest + +# Custom step to generate all users from environment variables +COPY generate-config.sh / +# Run our script and then the default entrypoint +ENTRYPOINT ["/bin/sh", "-c", "/generate-config.sh && /entrypoint.sh /usr/bin/pgbouncer /etc/pgbouncer/pgbouncer.ini"] diff --git a/db/pgbouncer/generate-config.sh b/db/pgbouncer/generate-config.sh new file mode 100755 index 0000000..35109f1 --- /dev/null +++ b/db/pgbouncer/generate-config.sh @@ -0,0 +1,22 @@ +#!/bin/sh + +set -e + +output_file="/etc/pgbouncer/userlist.txt" + +> "$output_file" + +# See https://github.com/edoburu/docker-pgbouncer/blob/master/examples/generate-userlist; +# The MD5 should be formed from password+username +compute_md5() { + password="$1" + username="$2" + printf "md5%s" "$(printf "%s%s" "$password" "$username" | md5sum | awk '{print $1}')" +} + +echo "\"$DB_USER\" \"$(compute_md5 "$DB_ROOT_PASSWORD" "$DB_USER")\"" >> "$output_file" +echo "\"$FERRY_DB_USER\" \"$(compute_md5 "$FERRY_DB_PASSWORD" "$FERRY_DB_USER")\"" >> "$output_file" +echo "\"$HASURA_DB_USER\" \"$(compute_md5 "$HASURA_DB_PASSWORD" "$HASURA_DB_USER")\"" >> "$output_file" + +echo "Userlist generated in $output_file" +cat "$output_file"