Skip to content

Commit

Permalink
experiment generating multiple login users for pgbouncer
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena committed Nov 24, 2024
1 parent eafd428 commit dc22650
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
13 changes: 11 additions & 2 deletions db/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions db/pgbouncer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
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 && exec /entrypoint.sh"]

CMD ["/usr/bin/pgbouncer", "/etc/pgbouncer/pgbouncer.ini"]
20 changes: 20 additions & 0 deletions db/pgbouncer/generate-config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/sh

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"

0 comments on commit dc22650

Please sign in to comment.