Skip to content

Add support for client-side direct TLS - #1359

Merged
JelteF merged 7 commits into
pgbouncer:masterfrom
enocom:master
Aug 13, 2025
Merged

JelteF merged 7 commits into
pgbouncer:masterfrom
enocom:master

Conversation

@enocom

@enocom enocom commented Jul 18, 2025 •

Copy link
Copy Markdown
Contributor

This commit allows clients to connect to pgbouncer using libpq's sslnegotiation=direct introduced in Postgres 17.

At a high level, this commit:

  • Configures the Postgres ALPN (i.e., postgresql) as part of the TLS server config.
  • Checks if the first byte of a connection is a TLS handshake, and if it is, proceeds to do a TLS handshake directly.

Fixes #1203

Comment thread .gitmodules Resolved
@enocom
enocom marked this pull request as draft July 18, 2025 22:34
@enocom

enocom commented Jul 18, 2025 •

Copy link
Copy Markdown
Contributor Author

~Converting to draft since there are some build failures. ~

EDIT: This is ready for review.

@enocom
enocom marked this pull request as ready for review July 19, 2025 16:53
Comment thread lib/usual/tls/tls_internal.h Outdated
int verify_depth;
int verify_name;
int verify_time;
struct tls_alpn_config *alpn_config;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of making an abstraction to configure this, how about we just hardcode the postgres ALPN support? Now that we have libusual being part of pgbouncer, I think it makes sense to couple them a bit more tightly in cases where we don't need abstractions.

@enocom enocom Aug 6, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. That simplifies things considerably. Done.

Comment thread src/sbuf.c Outdated

static ssize_t tls_sbufio_peek(struct SBuf *sbuf, void *buf, size_t len)
{
/* unused */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assert here would be nice

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@enocom
enocom requested a review from JelteF August 6, 2025 04:38
Comment thread lib/usual/tls/tls_server.c Outdated
Comment on lines +53 to +55
int
alpn_cb(SSL *ssl, const unsigned char **out, unsigned char *outlen,
const unsigned char *in, unsigned int inlen, void *userdata);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This definition is not necessary. I guess you added it because there was a warning. Let's just define the function as static.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment thread src/sbuf.c Outdated
client_accept_base = NULL;
}

static bool handle_tls_startup(SBuf *sbuf, bool is_unix)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
static bool handle_tls_startup(SBuf *sbuf, bool is_unix)
static bool handle_possible_direct_tls_startup(SBuf *sbuf, bool is_unix)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment thread src/sbuf.c Outdated
return sbuf_use_callback_once(sbuf, EV_READ, sbuf_tls_startup_cb);
}

static void sbuf_tls_startup_cb(evutil_socket_t fd, short flags, void *_sbuf)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
static void sbuf_tls_startup_cb(evutil_socket_t fd, short flags, void *_sbuf)
static void sbuf_possible_direct_tls_startup_cb(evutil_socket_t fd, short flags, void *_sbuf)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

enocom added 6 commits August 6, 2025 08:56
This commit allows clients to connect to pgbouncer using libpq's
sslnegotiation=direct introduced in Postgres 17.

At a high level, this commit:

- Uses a new API in libusual to configure the Postgres ALPN (i.e.,
  postgresql) as part of the TLS config when server_tls_sslmode is
  enabled.
- Checks if the first byte of a connection is a TLS handshake, and if it
  is, proceeded to do a TLS handshake directly.

Fixes #1203
Just hardcode the Postgres ALPN now that libusual is a part of
pgbouncer.

Also, address other PR comments.
- Rename functions for direct TLS support
- Use static and remove function prototype
@enocom
enocom requested a review from JelteF August 6, 2025 14:56
@enocom

enocom commented Aug 6, 2025

Copy link
Copy Markdown
Contributor Author

@JelteF I missed a spot in the rename but have fixed it now.

@nateschaferdesign nateschaferdesign left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

direct TLS in pgbouncer

4 participants