Add support for OAuth 2.0 with PostgreSQL 18#693
Conversation
Since the buitt-in OAuth hooks in libpq can return timerfd and not jsut a socket when you ask for the current file descriptor we are waiting on we need to make sure to use the right Ruby class to wrap the file descriptor, if it is not a valid socket we should use IO.
Async is not supported yet,
b3f94d6 to
b606810
Compare
|
Hi Andreas, thank you for working on this issue! To be honest, I don't like that global hook |
|
I don't like the global hook either, not in the pg gem nor in libpq. The only reason I did it like this was to mirror libpq. I will take a stab at your suggestion and see if it works. |
7e5c760 to
89fb316
Compare
Hi, I have hacked on this patch for some time now and feel like now is a good time to get some external input so I do not waste too much time on things the project would not like. The patch is not yet in a state where it is ready to be merged, but I think it is ready for input on architecture and design decisions. I also have several open questions.
I do not expect you to have answers to all the questions below, I mostly include them so I do not forget them myself.
Background
PostgreSQL 18 added support for OAuth 2.0 in the server, libpq and the psql client. See documentation at [1] and [2] for some further information.
So since libpq has added support for it I also think we should expose that support in the pg gem.
Current state of the patch
The patch should work and be stable but is missing polish and and some features, and CI is not entirely happy yet. See the new test cases for more details on how it is used. But here is a simple example: As you can see the current interface is pretty rough and intentionally close to the API exposed by libpq. Returning true for example means that we do not fall through to the built-in default handler.
The first of the three patches just fixes the code so it is no longer broken with the builtin OAuth hooks, the second implements the actual feature and the third just tries to fix the CI a bit.
Since PostgreSQL does not ship with any OAuth validator module which can be used by driver authors I wrote my own quick dummy one which worked very well until I had to make it compile on the full CI matrix. 😅
Things left to do
Open questions
About interface/implementation
-1to signal error? E.g. by letting people returntrue|false|nil?About naming
PG.set_auth_data_hookactually bePG::Connection.set_auth_data_hook?pg.c?connection_spec,rb.And the test validator is surprisingly complex to build in the CI environment.
Trying it out
To try it out for real you need the install a validator module, and here I am only familiar with our own: pg_oic_validator which comes with a Docker Compose file for setting it all up. I know there are others like keycloak-oauth-validator but I do not know how easy or tricky they are to set up.