pg.ddx.io pgbouncer/pgbouncer GitHub issues and pull requests
help / color / mirror / Atom feed[pgbouncer/pgbouncer] PR #1701: Add direct TLS negotiation for backend connections
3+ messages / 2 participants
[nested] [flat]
* [pgbouncer/pgbouncer] PR #1701: Add direct TLS negotiation for backend connections
@ 2026-11-13 04:04 "achanda (@achanda)" <noreply+achanda@github.com>
0 siblings, 0 replies; 3+ messages in thread
From: achanda (@achanda) @ 2026-11-13 04:04 UTC (permalink / raw)
To: pgbouncer/pgbouncer <noreply+pgbouncer-pgbouncer@github.com>
Add a new config option server_tls_sslnegotiation. Setting it to `direct` saves one round trip when opening a backend connection. Require server_tls_sslmode=require or stronger. This setup mostly follows what postgres 17 does. I ended up needing some extra changes for getting the tests to work.
inline patch 1: dev/format.sh, doc/config.md, etc/pgbouncer.ini and 14 more (+124/-15 lines) download inline-1.patch
diff --git a/dev/format.sh b/dev/format.sh
index 2b4f2f3d9fb5..009f5eac7357 100755
--- a/dev/format.sh
+++ b/dev/format.sh
@@ -52,7 +52,8 @@ ensure_uncrustify() {
"https://github.com/uncrustify/uncrustify/archive/refs/tags/$tarball"; \
--output "$tmp/$tarball"
tar xzf "$tmp/$tarball" -C "$tmp"
- cmake -S "$tmp/uncrustify-uncrustify-$UNCRUSTIFY_VERSION" -B "$tmp/build"
+ cmake -S "$tmp/uncrustify-uncrustify-$UNCRUSTIFY_VERSION" -B "$tmp/build" \
+ -DCMAKE_POLICY_VERSION_MINIMUM=3.5
cmake --build "$tmp/build"
cp "$tmp/build/uncrustify" "$root/uncrustify"
}
diff --git a/doc/config.md b/doc/config.md
index 70c3fbb88b99..ecdb3ad735a0 100644
--- a/doc/config.md
+++ b/doc/config.md
@@ -1018,6 +1018,17 @@ verify-full
according to `server_tls_ca_file`. Server host name must match
certificate information.
+### server_tls_sslnegotiation
+
+Controls how PgBouncer starts TLS when opening a PostgreSQL session over TCP.
+The default, `postgres`, sends a PostgreSQL SSLRequest before the TLS
+handshake. With `direct`, PgBouncer starts the handshake immediately after
+connecting, saving one network round trip.
+
+`direct` requires PostgreSQL 17 or later with TLS enabled and
+`server_tls_sslmode` set to `require`, `verify-ca`, or `verify-full`.
+PgBouncer requires the server to negotiate the `postgresql` ALPN protocol.
+
### server_tls_ca_file
Root certificate file to validate PostgreSQL server certificates.
diff --git a/etc/pgbouncer.ini b/etc/pgbouncer.ini
index 0e50c757955a..39679a006f6c 100644
--- a/etc/pgbouncer.ini
+++ b/etc/pgbouncer.ini
@@ -110,6 +110,9 @@ listen_port = 6432
;; disable, allow, prefer, require, verify-ca, verify-full
;server_tls_sslmode = prefer
+;; postgres, direct (PostgreSQL 17 or later, requires TLS)
+;server_tls_sslnegotiation = postgres
+
;; Path to that contains trusted CA certs
;server_tls_ca_file = <system default>
diff --git a/include/bouncer.h b/include/bouncer.h
index 88ea46eb35cf..5864eeec2249 100644
--- a/include/bouncer.h
+++ b/include/bouncer.h
@@ -127,6 +127,11 @@ enum SSLMode {
SSLMODE_VERIFY_FULL
};
+enum SSLNegotiation {
+ SSLNEGOTIATION_POSTGRES,
+ SSLNEGOTIATION_DIRECT
+};
+
enum PacketCallbackFlag {
/* no callback */
CB_NONE = 0,
@@ -923,6 +928,7 @@ extern char *cf_client_tls_dheparams;
extern char *cf_client_tls_ecdhecurve;
extern int cf_server_tls_sslmode;
+extern int cf_server_tls_sslnegotiation;
extern char *cf_server_tls_protocols;
extern char *cf_server_tls_ca_file;
extern char *cf_server_tls_cert_file;
diff --git a/include/sbuf.h b/include/sbuf.h
index fede6f537285..492d6e377363 100644
--- a/include/sbuf.h
+++ b/include/sbuf.h
@@ -115,6 +115,7 @@ extern int client_accept_sslmode;
* Same as client_accept_sslmode, but for server connections.
*/
extern int server_connect_sslmode;
+extern bool server_connect_direct_tls;
bool sbuf_tls_setup(void);
bool sbuf_tls_accept(SBuf *sbuf) _MUSTCHECK;
diff --git a/lib/usual/tls/tls.c b/lib/usual/tls/tls.c
index 6e806a6457be..6899785a1823 100644
--- a/lib/usual/tls/tls.c
+++ b/lib/usual/tls/tls.c
@@ -857,6 +857,8 @@ bool tls_config_equal(struct tls_config *tc1, struct tls_config *tc2)
return false;
if (tc1->verify_time != tc2->verify_time)
return false;
+ if (tc1->require_postgresql_alpn != tc2->require_postgresql_alpn)
+ return false;
return true;
}
diff --git a/lib/usual/tls/tls.h b/lib/usual/tls/tls.h
index 033d0d6f3144..d9303d2f7684 100644
--- a/lib/usual/tls/tls.h
+++ b/lib/usual/tls/tls.h
@@ -112,6 +112,7 @@ void tls_config_verify(struct tls_config *_config);
void tls_config_verify_client(struct tls_config *_config);
void tls_config_verify_client_optional(struct tls_config *_config);
+void tls_config_require_postgresql_alpn(struct tls_config *_config);
void tls_config_clear_keys(struct tls_config *_config);
int tls_config_parse_protocols(uint32_t *_protocols, const char *_protostr);
diff --git a/lib/usual/tls/tls_client.c b/lib/usual/tls/tls_client.c
index 111b3b02c82b..12ffeba12478 100644
--- a/lib/usual/tls/tls_client.c
+++ b/lib/usual/tls/tls_client.c
@@ -208,6 +208,14 @@ int tls_connect_fds(struct tls *ctx, int fd_read, int fd_write,
tls_set_errorx(ctx, "ssl connection failure");
goto err;
}
+ if (ctx->config->require_postgresql_alpn) {
+ static const unsigned char protocols[] = PG_ALPN_PROTOCOL_VECTOR;
+
+ if (SSL_set_alpn_protos(ctx->ssl_conn, protocols, sizeof(protocols) - 1) != 0) {
+ tls_set_errorx(ctx, "could not set PostgreSQL ALPN protocol");
+ goto err;
+ }
+ }
if (SSL_set_app_data(ctx->ssl_conn, ctx) != 1) {
tls_set_errorx(ctx, "ssl application data failure");
goto err;
@@ -258,6 +266,18 @@ int tls_handshake_client(struct tls *ctx)
goto err;
}
+ if (ctx->config->require_postgresql_alpn) {
+ const unsigned char *protocol;
+ unsigned int len;
+
+ SSL_get0_alpn_selected(ctx->ssl_conn, &protocol, &len);
+ if (protocol == NULL || len != sizeof(PG_ALPN_PROTOCOL) - 1 ||
+ memcmp(protocol, PG_ALPN_PROTOCOL, len) != 0) {
+ tls_set_errorx(ctx, "server did not negotiate PostgreSQL ALPN protocol");
+ goto err;
+ }
+ }
+
if (ctx->config->verify_name) {
cert = SSL_get_peer_certificate(ctx->ssl_conn);
if (cert == NULL) {
diff --git a/lib/usual/tls/tls_compat.c b/lib/usual/tls/tls_compat.c
index 9c24bd6275e4..99e6dcc520b0 100644
--- a/lib/usual/tls/tls_compat.c
+++ b/lib/usual/tls/tls_compat.c
@@ -575,6 +575,9 @@ void tls_config_verify(struct tls_config *_config)
void tls_config_verify_client(struct tls_config *_config)
{
}
+void tls_config_require_postgresql_alpn(struct tls_config *_config)
+{
+}
void tls_config_verify_client_optional(struct tls_config *_config)
{
}
diff --git a/lib/usual/tls/tls_config.c b/lib/usual/tls/tls_config.c
index 9c3232331858..ac261d654a1b 100644
--- a/lib/usual/tls/tls_config.c
+++ b/lib/usual/tls/tls_config.c
@@ -453,6 +453,11 @@ void tls_config_verify_client(struct tls_config *config)
config->verify_client = 1;
}
+void tls_config_require_postgresql_alpn(struct tls_config *config)
+{
+ config->require_postgresql_alpn = 1;
+}
+
void tls_config_verify_client_optional(struct tls_config *config)
{
config->verify_client = 2;
diff --git a/lib/usual/tls/tls_internal.h b/lib/usual/tls/tls_internal.h
index 0ecbd9db0fdd..f4476cdf7c2c 100644
--- a/lib/usual/tls/tls_internal.h
+++ b/lib/usual/tls/tls_internal.h
@@ -68,6 +68,7 @@ struct tls_config {
int verify_depth;
int verify_name;
int verify_time;
+ int require_postgresql_alpn;
};
struct tls_conninfo {
@@ -82,6 +83,9 @@ struct tls_conninfo {
time_t notafter;
};
+#define PG_ALPN_PROTOCOL "postgresql"
+#define PG_ALPN_PROTOCOL_VECTOR "\x0a" PG_ALPN_PROTOCOL
+
#define TLS_CLIENT (1 << 0)
#define TLS_SERVER (1 << 1)
#define TLS_SERVER_CONN (1 << 2)
diff --git a/lib/usual/tls/tls_server.c b/lib/usual/tls/tls_server.c
index bf12e0f3afe1..339b091af688 100644
--- a/lib/usual/tls/tls_server.c
+++ b/lib/usual/tls/tls_server.c
@@ -50,7 +50,6 @@ struct tls *tls_server_conn(struct tls *ctx)
return (conn_ctx);
}
-#define PG_ALPN_PROTOCOL_VECTOR { 10, 'p', 'o', 's', 't', 'g', 'r', 'e', 's', 'q', 'l' }
static const unsigned char alpn_protos[] = PG_ALPN_PROTOCOL_VECTOR;
/*
@@ -74,10 +73,10 @@ static int alpn_cb(SSL *ssl, const unsigned char **out, unsigned char *outlen,
Assert(in != NULL);
retval = SSL_select_next_proto((unsigned char **) out, outlen,
- alpn_protos, sizeof(alpn_protos),
+ alpn_protos, sizeof(alpn_protos) - 1,
in, inlen);
- if (*out == NULL || *outlen > sizeof(alpn_protos) || *outlen <= 0)
+ if (*out == NULL || *outlen > sizeof(alpn_protos) - 1 || *outlen <= 0)
return SSL_TLSEXT_ERR_NOACK;
if (retval == OPENSSL_NPN_NEGOTIATED) {
diff --git a/pyproject.toml b/pyproject.toml
index 4c21415a3286..d5dcdbcb9197 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -13,7 +13,7 @@ requires-python = ">=3.9"
# Needed to run the pytest-based test suite (`make check` / `pytest`).
dependencies = [
"pytest",
- "pytest-asyncio",
+ "pytest-asyncio>=0.24",
"pytest-timeout",
"pytest-xdist",
"psycopg",
diff --git a/src/main.c b/src/main.c
index f41130aeaf95..22b239e23e0b 100644
--- a/src/main.c
+++ b/src/main.c
@@ -195,6 +195,7 @@ char *cf_client_tls_dheparams;
char *cf_client_tls_ecdhecurve;
int cf_server_tls_sslmode;
+int cf_server_tls_sslnegotiation;
char *cf_server_tls_protocols;
char *cf_server_tls_ca_file;
char *cf_server_tls_cert_file;
@@ -247,6 +248,12 @@ const struct CfLookup sslmode_map[] = {
{ NULL }
};
+static const struct CfLookup sslnegotiation_map[] = {
+ { "postgres", SSLNEGOTIATION_POSTGRES },
+ { "direct", SSLNEGOTIATION_DIRECT },
+ { NULL }
+};
+
const struct CfLookup load_balance_hosts_map[] = {
{ "disable", LOAD_BALANCE_HOSTS_DISABLE },
{ "round-robin", LOAD_BALANCE_HOSTS_ROUND_ROBIN },
@@ -336,6 +343,7 @@ static const struct CfKey bouncer_params [] = {
CF_ABS("server_tls_key_file", CF_STR, cf_server_tls_key_file, 0, ""),
CF_ABS("server_tls_protocols", CF_STR, cf_server_tls_protocols, 0, "secure"),
CF_ABS("server_tls_sslmode", CF_LOOKUP(sslmode_map), cf_server_tls_sslmode, 0, "prefer"),
+ CF_ABS("server_tls_sslnegotiation", CF_LOOKUP(sslnegotiation_map), cf_server_tls_sslnegotiation, 0, "postgres"),
#ifdef WIN32
CF_ABS("service_name", CF_STR, cf_jobname, CF_NO_RELOAD, NULL), /* alias for job_name */
#endif
diff --git a/src/sbuf.c b/src/sbuf.c
index 37e2cf78dfbb..788828c1181d 100644
--- a/src/sbuf.c
+++ b/src/sbuf.c
@@ -79,6 +79,7 @@ static void sbuf_try_resync(SBuf *sbuf, bool release);
static bool sbuf_wait_for_data(SBuf *sbuf) _MUSTCHECK;
static bool sbuf_event_remove(SBuf *sbuf) _MUSTCHECK;
static void sbuf_main_loop(SBuf *sbuf, bool skip_recv);
+static void sbuf_start_tls_handshake(SBuf *sbuf);
static bool sbuf_call_proto(SBuf *sbuf, int event) /* _MUSTCHECK */;
static bool sbuf_actual_recv(SBuf *sbuf, size_t len) _MUSTCHECK;
static bool sbuf_after_connect_check(SBuf *sbuf) _MUSTCHECK;
@@ -1086,11 +1087,15 @@ static void sbuf_main_loop(SBuf *sbuf, bool skip_recv)
if (sbuf_is_empty(sbuf))
sbuf_call_proto(sbuf, SBUF_EV_FLUSH);
- if (sbuf->tls_state == SBUF_TLS_DO_HANDSHAKE) {
- sbuf->pkt_action = SBUF_TLS_IN_HANDSHAKE;
- if (!handle_tls_handshake(sbuf))
- sbuf_call_proto(sbuf, SBUF_EV_RECV_FAILED);
- }
+ if (sbuf->tls_state == SBUF_TLS_DO_HANDSHAKE)
+ sbuf_start_tls_handshake(sbuf);
+}
+
+static void sbuf_start_tls_handshake(SBuf *sbuf)
+{
+ sbuf->pkt_action = SBUF_TLS_IN_HANDSHAKE;
+ if (!handle_tls_handshake(sbuf))
+ sbuf_call_proto(sbuf, SBUF_EV_RECV_FAILED);
}
/* check if there is any error pending on socket */
@@ -1126,6 +1131,10 @@ static void sbuf_connect_cb(evutil_socket_t sock, short flags, void *arg)
goto failed;
if (!sbuf_call_proto(sbuf, SBUF_EV_CONNECT_OK))
return;
+ if (sbuf->tls_state == SBUF_TLS_DO_HANDSHAKE) {
+ sbuf_start_tls_handshake(sbuf);
+ return;
+ }
if (!sbuf_wait_for_data(sbuf))
goto failed;
return;
@@ -1199,6 +1208,7 @@ static struct tls_config *client_accept_conf;
int client_accept_sslmode;
static struct tls_config *server_connect_conf;
int server_connect_sslmode;
+bool server_connect_direct_tls;
/*
* TLS setup
@@ -1315,6 +1325,7 @@ static bool tls_change_requires_reconnect(struct tls_config *new_server_connect_
bool sbuf_tls_setup(void)
{
int err;
+ bool direct_tls = cf_server_tls_sslnegotiation == SSLNEGOTIATION_DIRECT;
/*
* These variables store the new TLS configurations, based on the latest
* settings provided by the user. Once they have been configured completely
@@ -1346,6 +1357,11 @@ bool sbuf_tls_setup(void)
return false;
}
+ if (direct_tls && cf_server_tls_sslmode < SSLMODE_REQUIRE) {
+ log_error("server_tls_sslnegotiation=direct requires server_tls_sslmode=require or stronger");
+ return false;
+ }
+
err = tls_init();
if (err)
fatal("tls_init failed");
@@ -1362,6 +1378,8 @@ bool sbuf_tls_setup(void)
cf_server_tls_key_file, cf_server_tls_cert_file,
cf_server_tls_ca_file, "", "", true))
goto failed;
+ if (direct_tls)
+ tls_config_require_postgresql_alpn(new_server_connect_conf);
}
if (cf_client_tls_sslmode != SSLMODE_DISABLED) {
@@ -1414,6 +1432,7 @@ bool sbuf_tls_setup(void)
client_accept_sslmode = cf_client_tls_sslmode;
server_connect_conf = new_server_connect_conf;
server_connect_sslmode = cf_server_tls_sslmode;
+ server_connect_direct_tls = direct_tls;
return true;
failed:
usual_tls_free(new_client_accept_base);
@@ -1487,10 +1506,10 @@ bool sbuf_tls_connect(SBuf *sbuf, const char *hostname)
struct tls *ctls;
int err;
- if (!sbuf_pause(sbuf))
+ if (sbuf->wait_type != W_NONE && !sbuf_pause(sbuf))
return false;
- if (cf_server_tls_sslmode != SSLMODE_VERIFY_FULL)
+ if (!server_connect_direct_tls && server_connect_sslmode != SSLMODE_VERIFY_FULL)
hostname = NULL;
ctls = tls_client();
@@ -1656,9 +1675,14 @@ static void sbuf_possible_direct_tls_startup_cb(evutil_socket_t fd, short flags,
int client_accept_sslmode = SSLMODE_DISABLED;
int server_connect_sslmode = SSLMODE_DISABLED;
+bool server_connect_direct_tls;
bool sbuf_tls_setup(void)
{
+ if (cf_server_tls_sslnegotiation == SSLNEGOTIATION_DIRECT) {
+ log_error("server_tls_sslnegotiation=direct requires a TLS-enabled build");
+ return false;
+ }
return true;
}
bool sbuf_tls_accept(SBuf *sbuf)
diff --git a/src/server.c b/src/server.c
index 5abbc985d937..a3e02394d992 100644
--- a/src/server.c
+++ b/src/server.c
@@ -723,7 +723,10 @@ static bool handle_connect(PgSocket *server)
disconnect_server(server, false, "peer server was not necessary anymore, because client cancel connection was already closed");
} else {
/* proceed with login */
- if (server_connect_sslmode > SSLMODE_DISABLED && !is_unix) {
+ if (server_connect_direct_tls && !is_unix) {
+ slog_noise(server, "starting direct TLS");
+ res = sbuf_tls_connect(&server->sbuf, server->host);
+ } else if (server_connect_sslmode > SSLMODE_DISABLED && !is_unix) {
slog_noise(server, "P: SSL request");
res = send_sslreq_packet(server);
if (res)
diff --git a/test/test_ssl.py b/test/test_ssl.py
index 6d70706eb22d..e211de6bef46 100644
--- a/test/test_ssl.py
+++ b/test/test_ssl.py
@@ -116,8 +116,21 @@ def test_server_ssl_set_enable(pg, bouncer_tls, cert_dir):
bouncer_tls.test()
-def test_server_ssl_verify(pg, bouncer_tls, cert_dir):
+@pytest.mark.parametrize(
+ "negotiation",
+ [
+ "postgres",
+ pytest.param(
+ "direct",
+ marks=pytest.mark.skipif(
+ not DIRECT_TLS_SUPPORT, reason="Direct TLS requires PostgreSQL 17"
+ ),
+ ),
+ ],
+)
+def test_server_ssl_verify(pg, bouncer_tls, cert_dir, negotiation):
bouncer_tls.admin("set server_tls_sslmode = 'verify-full'")
+ bouncer_tls.admin(f"set server_tls_sslnegotiation = '{negotiation}'")
root = cert_dir / "TestCA1" / "ca.crt"
wrong_root = cert_dir / "TestCA2" / "ca.crt"
bouncer_tls.admin(f"set server_tls_ca_file = '{wrong_root}'")
@@ -137,7 +150,12 @@ def test_server_ssl_verify(pg, bouncer_tls, cert_dir):
):
bouncer_tls.test(connect_timeout=4)
bouncer_tls.admin(f"set server_tls_ca_file = '{root}'")
- bouncer_tls.test()
+ bouncer_tls.admin("set verbose = 2")
+ startup = "starting direct TLS" if negotiation == "direct" else "P: SSL request"
+ with bouncer_tls.log_contains(startup):
+ assert bouncer_tls.sql_value(
+ "select ssl from pg_stat_ssl where pid = pg_backend_pid()"
+ )
bouncer_tls.psql_test(dbname="hostlistsslverify")
^ permalink raw [nested|flat] 3+ messages in thread
* Re: [pgbouncer/pgbouncer] PR #1701: Add direct TLS negotiation for backend connections
@ 2026-11-14 15:21 "cosgroveb (@cosgroveb)" <noreply+cosgroveb@github.com>
1 sibling, 0 replies; 3+ messages in thread
From: cosgroveb (@cosgroveb) @ 2026-11-14 15:21 UTC (permalink / raw)
To: pgbouncer/pgbouncer <noreply+pgbouncer-pgbouncer@github.com>
Thanks!
This work is already underway in #1584, including cancellation connections, ALPN validation, SNI handling, etc. Review of that in-progress implementation would be appreciated.
^ permalink raw [nested|flat] 3+ messages in thread
* Re: [pgbouncer/pgbouncer] PR #1701: Add direct TLS negotiation for backend connections
@ 2026-11-15 02:49 "achanda (@achanda)" <noreply+achanda@github.com>
1 sibling, 0 replies; 3+ messages in thread
From: achanda (@achanda) @ 2026-11-15 02:49 UTC (permalink / raw)
To: pgbouncer/pgbouncer <noreply+pgbouncer-pgbouncer@github.com>
@achanda closed this.
^ permalink raw [nested|flat] 3+ messages in thread
end of thread, other threads:[~2026-11-15 02:49 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-11-13 04:04 [pgbouncer/pgbouncer] PR #1701: Add direct TLS negotiation for backend connections "achanda (@achanda)" <noreply+achanda@github.com>
2026-11-14 15:21 ` "cosgroveb (@cosgroveb)" <noreply+cosgroveb@github.com>
2026-11-15 02:49 ` "achanda (@achanda)" <noreply+achanda@github.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox