Skip to main content

Azure Auth PgBouncer

Azure Auth PgBouncer is designed to make securing connections to Azure PostgreSQL databases with Entra ID a breeze.

It was inspired by the GCP Cloud SQL Auth Proxy, but instead of doing the hard work of proxying the traffic, it orchestrates PgBouncer to do it instead by rotating an access token and updating its configuration accordingly.

Installation

pipx

pipx install azure-auth-pgbouncer

Docker

docker pull ghcr.io/mmalecki/azure-auth-pgbouncer:latest

Usage

Locally

When starting Azure Auth PgBouncer locally, you will need to start the two processes (PgBouncer and token refresher) separately.

First, fire up the token refresher:

PID_FILE=pgbouncer.pid AUTH_FILE=users.txt PGUSER=<identity-name> azure-auth-pgbouncer

Then, once it's fetched its first token and users.txt appears in the directory, configure PgBouncer in a file named pgbouncer.ini, for example:

[databases]
* = host=<azure-postgresql-database-host>

[pgbouncer]
pool_mode = session
listen_port = 5432
listen_addr = 127.0.0.1
auth_type = trust
auth_file = users.txt
pidfile = pgbouncer.pid
server_tls_sslmode = verify-full

and launch it:

pgbouncer pgbouncer.ini

Please note that the database host needs to be accessible over network - this project only handles authentication, not network traversals. However, with server_tls_sslmode set to require (as opposed to verify-full), you should see no issues connecting to a proxy set up by, say, kubectl port-forward.

Additionally, PgBouncer limits the maximum password length to 2048 characters. The Azure CLI credential can be longer than that. In this case, you can expect to see the following error in PgBouncer logs:

ERROR password too long in auth file

With cloud use being the primary focus of this project, fixing this issue hasn't been a priority.

Docker

Due to the tight integration, PgBouncer and the token refresher come bundled in a single Docker container.

docker run -e PGHOST=<azure-postgresql-database-host> -e PGUSER=<identity-name> -it ghcr.io/mmalecki/azure-auth-pgbouncer:latest

No additional configuration is needed - the pgbouncer.ini file is generated by container's entrypoint.

On Kubernetes

As a sidecar

Add the following init container to your deployment/statefulset/...:

      initContainers:
        - name: azure-auth-pgbouncer
          image: ghcr.io/mmalecki/azure-auth-pgbouncer:v0.1.1
          imagePullPolicy: IfNotPresent
          restartPolicy: Always
          ports:
          - containerPort: 5432
            protocol: TCP
          env:
          - name: PGHOST
            value: <azure-postgresql-database-host>
          - name: PGUSER
            value: <identity-name>

As a deployment/statefulset/...

This method creates an unauthenticated PostgreSQL endpoint in your cluster, where only method of checking identity used is verifying the username. This may be fine for your use case, but you've been warned either way.

Metadata

Release files for azure-auth-pgbouncer 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for azure-auth-pgbouncer 0.2.1
File Size Uploaded
azure_auth_pgbouncer-0.2.1.tar.gz 2.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for azure-auth-pgbouncer 0.2.1
File Interpreter ABI Platform
azure_auth_pgbouncer-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 6.4 kB

Release files / azure_auth_pgbouncer-0.2.1.tar.gz

Download URL azure_auth_pgbouncer-0.2.1.tar.gz
Size 2.8 kB
Tags Source
SHA-256 checksum
How to use checksums
a59ab5b5b978c877b3a708736fb4be8544a73639ca209bb2b2f843e5787b8933
BLAKE2b-256 checksum
How to use checksums
9106ff599f9b0bd6d21c77fe90043519cc039ab590417bbf7a59f62c0160ae09
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.0.1 CPython/3.12.10 Linux/6.12.38+deb13-amd64

Release files / azure_auth_pgbouncer-0.2.1-py3-none-any.whl

Download URL azure_auth_pgbouncer-0.2.1-py3-none-any.whl
Size 3.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
eba6158757fb49dc093921665cb76c9fc9d04d08c2dc8a49876903bc964fb8f1
BLAKE2b-256 checksum
How to use checksums
1d6e3f107d8e9c97e4214384403d147755e3ac071ae443ad0d5c95a2490efe76
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.0.1 CPython/3.12.10 Linux/6.12.38+deb13-amd64

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page