Cache invalidation after authentication (on-the-fly role creation)

From: Thomas Munro <thomas(dot)munro(at)enterprisedb(dot)com>
To: Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Cache invalidation after authentication (on-the-fly role creation)
Date: 2018-07-03 21:58:17
Message-ID: CAEepm=3_h0_cgmz5PMyab4xk_OFrg6G5VCN=nF4chFXM9iFOqA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hello hackers,

I'd like to do this to postinit.c:

PerformAuthentication(MyProcPort);
+ AcceptInvalidationMessages();
InitializeSessionUserId(username, useroid);

Any objections? Motivation:

Many people use custom scripts, ldap2pg or other similar tools to
synchronise or manage their PostgreSQL roles from an LDAP server.
Doing that in a cron job works pretty well, but you have to wait for
the next cron job run after you make a change. Based on several
requests from the field (basically, ex-SQL Server shops, where they
love Active Directory group-based authorisation), I started wondering
if it would be crazy to do incremental per-user synchronisation at at
the moment of authentication. New hooks don't seem to be necessary,
since PAM is designed for things like that -- for example creating
your Unix home directory on demand. But I ran into one small problem:
PostgreSQL doesn't see catalog changes that happened during
authentication because of caching.

For example, you might put this in pg_hba.conf:

host all all all pam pamservice=postgresql

... and then put this in /etc/pam.d/postgresql:

auth required pam_ldap.so config=/path/to/ldap.conf
auth required pam_exec.so /path/to/ldap2pg --sync-pam-user
account required pam_permit.so

That's a fictional ldap2pg option that would synchronise only the user
passed to it in $PAM_USER. A high performance version could do
authentication and role synchronisation at the same time (perhaps
using the LDAP server's change notification/counter to skip having to
connect back to PostgreSQL to inspect catalogs in the common case that
nothing changed).

If you try things like that today, it works but any roles created
during authentication are (sometimes?) not visible to PostgreSQL until
your *next* attempt to log in.

--
Thomas Munro
http://www.enterprisedb.com

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Paquier 2018-07-03 22:32:20 Re: pgsql: Add wait event for fsync of WAL segments
Previous Message Alvaro Herrera 2018-07-03 20:12:23 Re: pgsql: Add wait event for fsync of WAL segments