pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Re[2]: On login trigger: take three
3+ messages / 1 participants
[nested] [flat]

* Re[2]: On login trigger: take three
@ 2021-03-16 11:32  Ivan Panchenko <wao@mail.ru>
  1 sibling, 0 replies; 3+ messages in thread

From: Ivan Panchenko @ 2021-03-16 11:32 UTC (permalink / raw)
  To: Konstantin Knizhnik <k.knizhnik@postgrespro.ru>; +Cc: Pavel Stehule <pavel.stehule@gmail.com>; Greg Nancarrow <gregn4422@gmail.com>; pgsql-hackers; Amit Kapila <amit.kapila16@gmail.com>; Masahiko Sawada <sawada.mshk@gmail.com>


Hi,

Thank you, Konstantin, for this very good feature with numerous use cases.
Please find the modified patch attached.

I’ve added the ‘enable_client_connection_trigger’ GUC to the sample config file and also an additional example page to the docs.
Check world has passed and it is ready for committer.
  
>Четверг, 28 января 2021, 12:04 +03:00 от Konstantin Knizhnik <k.knizhnik@postgrespro.ru>:
> 
>
>
>On 28.01.2021 5:47, Amit Kapila wrote:
>> On Mon, Dec 28, 2020 at 5:46 PM Masahiko Sawada < sawada.mshk@gmail.com > wrote:
>>> On Sat, Dec 26, 2020 at 4:04 PM Pavel Stehule < pavel.stehule@gmail.com > wrote:
>>>>
>>>>
>>>> so 26. 12. 2020 v 8:00 odesílatel Pavel Stehule < pavel.stehule@gmail.com > napsal:
>>>>> Hi
>>>>>
>>>>>
>>>>>> Thank you.
>>>>>> I have applied all your fixes in on_connect_event_trigger-12.patch.
>>>>>>
>>>>>> Concerning enable_client_connection_trigger GUC, I think that it is really useful: it is the fastest and simplest way to disable login triggers in case
>>>>>> of some problems with them (not only for superuser itself, but for all users). Yes, it can be also done using "ALTER EVENT TRIGGER DISABLE".
>>>>>> But assume that you have a lot of databases with different login policies enforced by on-login event triggers. And you want temporary disable them all, for example for testing purposes.
>>>>>> In this case GUC is most convenient way to do it.
>>>>>>
>>>>> There was typo in patch
>>>>>
>>>>> diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
>>>>> index f810789..8861f1b 100644
>>>>> --- a/doc/src/sgml/config.sgml
>>>>> +++ b/doc/src/sgml/config.sgml
>>>>> @@ -1,4 +1,4 @@
>>>>> -<!-- doc/src/sgml/config.sgml -->
>>>>> +\<!-- doc/src/sgml/config.sgml -->
>>>>>
>>>>> I have not any objections against functionality or design. I tested the performance, and there are no negative impacts when this feature is not used. There is significant overhead related to plpgsql runtime initialization, but when this trigger will be used, then probably some other PLpgSQL procedures and functions will be used too, and then this overhead can be ignored.
>>>>>
>>>>> * make without warnings
>>>>> * make check-world passed
>>>>> * doc build passed
>>>>>
>>>>> Possible ToDo:
>>>>>
>>>>> The documentation can contain a note so usage connect triggers in environments with short life sessions and very short fast queries without usage PLpgSQL functions or procedures can have negative impact on performance due overhead of initialization of PLpgSQL engine.
>>>>>
>>>>> I'll mark this patch as ready for committers
>>>>
>>>> looks so this patch has not entry in commitfestapp 2021-01
>>>>
>>> Yeah, please register this patch before the next CommitFest[1] starts,
>>> 2021-01-01 AoE[2].
>>>
>> Konstantin, did you register this patch in any CF? Even though the
>> reviewer seems to be happy with the patch, I am afraid that we might
>> lose track of this unless we register it.
>> Yes, certainly:
>https://commitfest.postgresql.org/31/2900/
>
>--
>Konstantin Knizhnik
>Postgres Professional:  http://www.postgrespro.com
>The Russian Postgres Company
>
>  
 
 
 
 

Attachments:

on_connect_event_trigger-13a.patch (application/octet-stream, 35.1K) download diff --git a/doc/src/sgml/bki.sgml b/doc/src/sgml/bki.sgml index db4801c246..76ebb84735 100644 --- a/doc/src/sgml/bki.sgml +++ b/doc/src/sgml/bki.sgml @@ -185,7 +185,7 @@ { oid =&gt; '1', oid_symbol =&gt; 'TemplateDbOid', descr =&gt; 'database\'s default template', datname =&gt; 'template1', encoding =&gt; 'ENCODING', datcollate =&gt; 'LC_COLLATE', - datctype =&gt; 'LC_CTYPE', datistemplate =&gt; 't', datallowconn =&gt; 't', + datctype =&gt; 'LC_CTYPE', datistemplate =&gt; 't', datallowconn =&gt; 't', dathaslogontriggers =&gt; 'f', datconnlimit =&gt; '-1', datlastsysoid =&gt; '0', datfrozenxid =&gt; '0', datminmxid =&gt; '1', dattablespace =&gt; 'pg_default', datacl =&gt; '_null_' }, diff --git a/doc/src/sgml/catalogs.sgml b/doc/src/sgml/catalogs.sgml index 612cc69802..1ed1ffb8d7 100644 --- a/doc/src/sgml/catalogs.sgml +++ b/doc/src/sgml/catalogs.sgml @@ -2945,6 +2945,17 @@ SCRAM-SHA-256$<replaceable>&lt;iteration count&gt;</replaceable>:<replaceable>&l </para></entry> </row> + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + <structfield>dathaslogontriggers</structfield> <type>bool</type> + </para> + <para> + Indicates that there are client connection triggers defined for this database. + This flag is used to avoid extra lookup of pg_event_trigger table on each backend startup. + This flag is used internally by Postgres and should not be manually changed by DBA or application. + </para></entry> + </row> + <row> <entry role="catalog_table_entry"><para role="column_definition"> <structfield>datconnlimit</structfield> <type>int4</type> diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml index dd2778611f..960b628176 100644 --- a/doc/src/sgml/config.sgml +++ b/doc/src/sgml/config.sgml @@ -976,6 +976,24 @@ include_dir 'conf.d' </listitem> </varlistentry> + <varlistentry id="guc-enable-client-connection-trigger" xreflabel="enable_client_connection_trigger"> + <term><varname>enable_client_connection_trigger</varname> (<type>boolean</type>) + <indexterm> + <primary><varname>enable_client_connection_trigger</varname> configuration parameter</primary> + </indexterm> + </term> + <listitem> + <para> + Enables firing the <literal>client_connection</literal> + trigger when a client connects. This parameter is switched on by default. + Errors in trigger code can prevent user to login to the system. + In this case disabling this parameter in connection string can solve the problem: + <literal>psql "dbname=postgres options='-c enable_client_connection_trigger=false'".</literal> + Only superuser can change this variable. + </para> + </listitem> + </varlistentry> + </variablelist> </sect2> diff --git a/doc/src/sgml/ecpg.sgml b/doc/src/sgml/ecpg.sgml index 7266e229a4..752ffc2357 100644 --- a/doc/src/sgml/ecpg.sgml +++ b/doc/src/sgml/ecpg.sgml @@ -4663,6 +4663,7 @@ datdba = 10 (type: 1) encoding = 0 (type: 5) datistemplate = t (type: 1) datallowconn = t (type: 1) +dathaslogontriggers = f (type: 1) datconnlimit = -1 (type: 5) datlastsysoid = 11510 (type: 1) datfrozenxid = 379 (type: 1) @@ -4688,6 +4689,7 @@ datdba = 10 (type: 1) encoding = 0 (type: 5) datistemplate = f (type: 1) datallowconn = t (type: 1) +dathaslogontriggers = f (type: 1) datconnlimit = -1 (type: 5) datlastsysoid = 11510 (type: 1) datfrozenxid = 379 (type: 1) diff --git a/doc/src/sgml/event-trigger.sgml b/doc/src/sgml/event-trigger.sgml index 60366a950e..6f99909a74 100644 --- a/doc/src/sgml/event-trigger.sgml +++ b/doc/src/sgml/event-trigger.sgml @@ -28,6 +28,7 @@ An event trigger fires whenever the event with which it is associated occurs in the database in which it is defined. Currently, the only supported events are + <literal>client_connection</literal>, <literal>ddl_command_start</literal>, <literal>ddl_command_end</literal>, <literal>table_rewrite</literal> @@ -35,6 +36,29 @@ Support for additional events may be added in future releases. </para> + <para> + The <literal>client_connection</literal> event occurs when a client connection + to the server is established. + There are two mechanisms for dealing with any bugs in a trigger procedure for + this event which might prevent successful login to the system: + <itemizedlist> + <listitem> + <para> + The configuration parameter <literal>enable_client_connection_trigger</literal> + makes it possible to disable firing the <literal>client_connection</literal> + trigger when a client connects. + </para> + </listitem> + <listitem> + <para> + Errors in the <literal>client_connection</literal> trigger procedure are + ignored for superuser. An error message is delivered to the client as + <literal>NOTICE</literal> in this case. + </para> + </listitem> + </itemizedlist> + </para> + <para> The <literal>ddl_command_start</literal> event occurs just before the execution of a <literal>CREATE</literal>, <literal>ALTER</literal>, <literal>DROP</literal>, @@ -1140,7 +1164,7 @@ typedef struct EventTriggerData </sect1> <sect1 id="event-trigger-example"> - <title>A Complete Event Trigger Example</title> + <title>A C language Event Trigger Example</title> <para> Here is a very simple example of an event trigger function written in C. @@ -1280,6 +1304,64 @@ $$; CREATE EVENT TRIGGER no_rewrite_allowed ON table_rewrite EXECUTE FUNCTION no_rewrite(); +</programlisting> + </para> + </sect1> + + <sect1 id="event-trigger-database-client-connection-example"> + <title>A Database Client Connection Event Trigger Example</title> + + <para> + The event trigger on the <literal>client_connection</literal> event + can be useful for client connections logging, + for verifying the connection and assigning roles according to current circumstances, + or for some session data initialization. + </para> + + <para> + The following example demonstrates these options. +<programlisting> + +-- create test tables and roles +CREATE TABLE user_sessions_log ( + "user" text, + "session_start" timestamp with time zone +); +CREATE ROLE day_worker; +CREATE ROLE night_worker; + +-- the example trigger function +CREATE OR REPLACE FUNCTION init_session() + RETURNS event_trigger SECURITY DEFINER + LANGUAGE plpgsql AS +$$ +DECLARE + hour integer = EXTRACT('hour' FROM current_time); +BEGIN +-- 1) Assign some roles + IF hour BETWEEN 8 AND 20 THEN -- at daytime grant the day_worker role + EXECUTE 'REVOKE night_worker FROM ' || quote_ident(session_user); + EXECUTE 'GRANT day_worker TO ' || quote_ident(session_user); + ELSIF hour BETWEEN 2 AND 4 THEN + RAISE EXCEPTION 'Login forbidden'; -- do not allow to connect these hours + ELSE -- at other time grant the night_worker role + EXECUTE 'REVOKE day_worker FROM ' || quote_ident(session_user); + EXECUTE 'GRANT night_worker TO ' || quote_ident(session_user); + END IF; + +-- 2) Initialize some user session data + CREATE TEMP TABLE session_storage (x float, y integer); + +-- 3) Log the connection time + INSERT INTO user_sessions_log VALUES (session_user, current_timestamp); + +END; +$$; + +-- trigger definition +CREATE EVENT TRIGGER init_session + ON client_connection + EXECUTE FUNCTION init_session(); </programlisting> </para> </sect1> diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c index f27c3fe8c1..8646db7d3d 100644 --- a/src/backend/commands/dbcommands.c +++ b/src/backend/commands/dbcommands.c @@ -560,6 +560,7 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt) new_record[Anum_pg_database_datctype - 1] = DirectFunctionCall1(namein, CStringGetDatum(dbctype)); new_record[Anum_pg_database_datistemplate - 1] = BoolGetDatum(dbistemplate); + new_record[Anum_pg_database_dathaslogontriggers - 1] = BoolGetDatum(false); new_record[Anum_pg_database_datallowconn - 1] = BoolGetDatum(dballowconnections); new_record[Anum_pg_database_datconnlimit - 1] = Int32GetDatum(dbconnlimit); new_record[Anum_pg_database_datlastsysoid - 1] = ObjectIdGetDatum(src_lastsysoid); @@ -1627,7 +1628,7 @@ AlterDatabase(ParseState *pstate, AlterDatabaseStmt *stmt, bool isTopLevel) new_record[Anum_pg_database_datconnlimit - 1] = Int32GetDatum(dbconnlimit); new_record_repl[Anum_pg_database_datconnlimit - 1] = true; } - + new_record[Anum_pg_database_dathaslogontriggers - 1] = BoolGetDatum(datform->dathaslogontriggers); newtuple = heap_modify_tuple(tuple, RelationGetDescr(rel), new_record, new_record_nulls, new_record_repl); CatalogTupleUpdate(rel, &tuple->t_self, newtuple); diff --git a/src/backend/commands/event_trigger.c b/src/backend/commands/event_trigger.c index b6894cbf76..d3e3eafddd 100644 --- a/src/backend/commands/event_trigger.c +++ b/src/backend/commands/event_trigger.c @@ -20,6 +20,7 @@ #include "catalog/dependency.h" #include "catalog/indexing.h" #include "catalog/objectaccess.h" +#include "catalog/pg_database.h" #include "catalog/pg_event_trigger.h" #include "catalog/pg_namespace.h" #include "catalog/pg_opclass.h" @@ -43,11 +44,14 @@ #include "utils/builtins.h" #include "utils/evtcache.h" #include "utils/fmgroids.h" +#include "utils/inval.h" #include "utils/lsyscache.h" #include "utils/memutils.h" #include "utils/rel.h" #include "utils/syscache.h" +bool enable_client_connection_trigger; + typedef struct EventTriggerQueryState { /* memory context for this state's objects */ @@ -130,6 +134,7 @@ CreateEventTrigger(CreateEventTrigStmt *stmt) if (strcmp(stmt->eventname, "ddl_command_start") != 0 && strcmp(stmt->eventname, "ddl_command_end") != 0 && strcmp(stmt->eventname, "sql_drop") != 0 && + strcmp(stmt->eventname, "client_connection") != 0 && strcmp(stmt->eventname, "table_rewrite") != 0) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), @@ -293,6 +298,23 @@ insert_event_trigger_tuple(const char *trigname, const char *eventname, Oid evtO CatalogTupleInsert(tgrel, tuple); heap_freetuple(tuple); + if (strcmp(eventname, "client_connection") == 0) + { + Form_pg_database db; + Relation pg_db = table_open(DatabaseRelationId, RowExclusiveLock); + /* Set dathaslogontriggers flag in pg_database */ + tuple = SearchSysCacheCopy1(DATABASEOID, ObjectIdGetDatum(MyDatabaseId)); + if (!HeapTupleIsValid(tuple)) + elog(ERROR, "cache lookup failed for database %u", MyDatabaseId); + db = (Form_pg_database) GETSTRUCT(tuple); + if (!db->dathaslogontriggers) + { + db->dathaslogontriggers = true; + CatalogTupleUpdate(pg_db, &tuple->t_self, tuple); + } + table_close(pg_db, RowExclusiveLock); + } + /* Depend on owner. */ recordDependencyOnOwner(EventTriggerRelationId, trigoid, evtOwner); @@ -584,6 +606,9 @@ EventTriggerCommonSetup(Node *parsetree, ListCell *lc; List *runlist = NIL; + /* Get the command tag. */ + tag = parsetree ? CreateCommandTag(parsetree) : CMDTAG_CONNECT; + /* * We want the list of command tags for which this procedure is actually * invoked to match up exactly with the list that CREATE EVENT TRIGGER @@ -599,22 +624,18 @@ EventTriggerCommonSetup(Node *parsetree, * relevant command tag. */ #ifdef USE_ASSERT_CHECKING + if (event == EVT_DDLCommandStart || + event == EVT_DDLCommandEnd || + event == EVT_SQLDrop || + event == EVT_Connect) { - CommandTag dbgtag; - - dbgtag = CreateCommandTag(parsetree); - if (event == EVT_DDLCommandStart || - event == EVT_DDLCommandEnd || - event == EVT_SQLDrop) - { - if (!command_tag_event_trigger_ok(dbgtag)) - elog(ERROR, "unexpected command tag \"%s\"", GetCommandTagName(dbgtag)); - } - else if (event == EVT_TableRewrite) - { - if (!command_tag_table_rewrite_ok(dbgtag)) - elog(ERROR, "unexpected command tag \"%s\"", GetCommandTagName(dbgtag)); - } + if (!command_tag_event_trigger_ok(tag)) + elog(ERROR, "unexpected command tag \"%s\"", GetCommandTagName(tag)); + } + else if (event == EVT_TableRewrite) + { + if (!command_tag_table_rewrite_ok(tag)) + elog(ERROR, "unexpected command tag \"%s\"", GetCommandTagName(tag)); } #endif @@ -623,9 +644,6 @@ EventTriggerCommonSetup(Node *parsetree, if (cachelist == NIL) return NIL; - /* Get the command tag. */ - tag = CreateCommandTag(parsetree); - /* * Filter list of event triggers by command tag, and copy them into our * memory context. Once we start running the command triggers, or indeed @@ -822,6 +840,117 @@ EventTriggerSQLDrop(Node *parsetree) list_free(runlist); } +static bool +DatabaseHasLogonTriggers(void) +{ + bool has_logon_triggers; + HeapTuple tuple = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(MyDatabaseId)); + + if (!HeapTupleIsValid(tuple)) + elog(ERROR, "cache lookup failed for database %u", MyDatabaseId); + + has_logon_triggers = ((Form_pg_database) GETSTRUCT(tuple))->dathaslogontriggers; + ReleaseSysCache(tuple); + return has_logon_triggers; +} + +/* + * Fire connect triggers. + */ +void +EventTriggerOnConnect(void) +{ + List *runlist; + EventTriggerData trigdata; + + /* + * See EventTriggerDDLCommandStart for a discussion about why event + * triggers are disabled in single user mode. + */ + if (!IsUnderPostmaster + || !OidIsValid(MyDatabaseId) + || !enable_client_connection_trigger) + return; + + StartTransactionCommand(); + + if (DatabaseHasLogonTriggers()) + { + runlist = EventTriggerCommonSetup(NULL, + EVT_Connect, "connect", + &trigdata); + + if (runlist != NIL) + { + MemoryContext old_context = CurrentMemoryContext; + bool is_superuser = superuser(); + /* + * Make sure anything the main command did will be visible to the event + * triggers. + */ + CommandCounterIncrement(); + + /* Run the triggers. */ + PG_TRY(); + { + EventTriggerInvoke(runlist, &trigdata); + list_free(runlist); + } + PG_CATCH(); + { + ErrorData* error; + /* + * Try to ignore error for superuser to make it possible to login even in case of errors + * during trigger execution + */ + if (!is_superuser) + PG_RE_THROW(); + + MemoryContextSwitchTo(old_context); + error = CopyErrorData(); + FlushErrorState(); + elog(NOTICE, "client_connection trigger failed with message: %s", error->message); + AbortCurrentTransaction(); + return; + } + PG_END_TRY(); + } + else + { + /* Runtlist is empty: clear dathaslogontriggers flag + */ + Relation pg_db = table_open(DatabaseRelationId, RowExclusiveLock); + HeapTuple tuple = SearchSysCacheCopy1(DATABASEOID, ObjectIdGetDatum(MyDatabaseId)); + Form_pg_database db; + + if (!HeapTupleIsValid(tuple)) + elog(ERROR, "cache lookup failed for database %u", MyDatabaseId); + + db = (Form_pg_database) GETSTRUCT(tuple); + if (db->dathaslogontriggers) + { + db->dathaslogontriggers = false; + CatalogTupleUpdate(pg_db, &tuple->t_self, tuple); + /* + * There can be race condition: event trigger may be added after we have scanned + * pg_event_trigger table. Repeat this test nuder pg_database table lock. + */ + AcceptInvalidationMessages(); + runlist = EventTriggerCommonSetup(NULL, + EVT_Connect, "connect", + &trigdata); + if (runlist != NULL) /* if list is not empty, then restore the flag */ + { + db->dathaslogontriggers = true; + CatalogTupleUpdate(pg_db, &tuple->t_self, tuple); + } + } + table_close(pg_db, RowExclusiveLock); + } + } + CommitTransactionCommand(); +} + /* * Fire table_rewrite triggers. diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c index 174c72a14b..60954939c0 100644 --- a/src/backend/tcop/postgres.c +++ b/src/backend/tcop/postgres.c @@ -42,6 +42,7 @@ #include "catalog/pg_type.h" #include "commands/async.h" #include "commands/prepare.h" +#include "commands/event_trigger.h" #include "executor/spi.h" #include "jit/jit.h" #include "libpq/libpq.h" @@ -167,6 +168,9 @@ static ProcSignalReason RecoveryConflictReason; static MemoryContext row_description_context = NULL; static StringInfoData row_description_buf; +/* Hook for plugins to get control at start of session */ +client_connection_hook_type client_connection_hook = EventTriggerOnConnect; + /* ---------------------------------------------------------------- * decls for routines only used in this file * ---------------------------------------------------------------- @@ -4029,6 +4033,11 @@ PostgresMain(int argc, char *argv[], if (!IsUnderPostmaster) PgStartTime = GetCurrentTimestamp(); + if (client_connection_hook) + { + (*client_connection_hook) (); + } + /* * POSTGRES main processing loop begins here * diff --git a/src/backend/utils/cache/evtcache.c b/src/backend/utils/cache/evtcache.c index 73d091d1f6..d134aac7d4 100644 --- a/src/backend/utils/cache/evtcache.c +++ b/src/backend/utils/cache/evtcache.c @@ -169,6 +169,8 @@ BuildEventTriggerCache(void) event = EVT_SQLDrop; else if (strcmp(evtevent, "table_rewrite") == 0) event = EVT_TableRewrite; + else if (strcmp(evtevent, "client_connection") == 0) + event = EVT_Connect; else continue; diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c index 974235d705..a37c9a45a2 100644 --- a/src/backend/utils/misc/guc.c +++ b/src/backend/utils/misc/guc.c @@ -40,6 +40,7 @@ #include "commands/async.h" #include "commands/prepare.h" #include "commands/trigger.h" +#include "commands/event_trigger.h" #include "commands/user.h" #include "commands/vacuum.h" #include "commands/variable.h" @@ -931,6 +932,18 @@ static const unit_conversion time_unit_conversion_table[] = static struct config_bool ConfigureNamesBool[] = { + { + {"enable_client_connection_trigger", PGC_SU_BACKEND, DEVELOPER_OPTIONS, + gettext_noop("Enables the client_connection event trigger."), + gettext_noop("In case of errors in the ON client_connection EVENT TRIGGER procedure, " + "this parameter can be used to disable trigger activation " + "and provide access to the database."), + GUC_EXPLAIN + }, + &enable_client_connection_trigger, + true, + NULL, NULL, NULL + }, { {"enable_seqscan", PGC_USERSET, QUERY_TUNING_METHOD, gettext_noop("Enables the planner's use of sequential-scan plans."), diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/utils/misc/postgresql.conf.sample index 2663bccfda..92707ed703 100644 --- a/src/backend/utils/misc/postgresql.conf.sample +++ b/src/backend/utils/misc/postgresql.conf.sample @@ -72,6 +72,7 @@ # (change requires restart) #bonjour_name = '' # defaults to the computer name # (change requires restart) +#enable_client_connection_trigger = true # enables firing the client_connection trigger when a client connect # - TCP settings - # see "man tcp" for details diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index f8d99b7722..29a9f08ec9 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -2718,6 +2718,7 @@ dumpDatabase(Archive *fout) i_datacl, i_rdatacl, i_datistemplate, + i_dathaslogontriggers, i_datconnlimit, i_tablespace; CatalogId dbCatId; @@ -2730,6 +2731,7 @@ dumpDatabase(Archive *fout) *datacl, *rdatacl, *datistemplate, + *dathaslogontriggers, *datconnlimit, *tablespace; uint32 frozenxid, @@ -2748,7 +2750,7 @@ dumpDatabase(Archive *fout) * (pg_init_privs) are not supported on databases, so this logic cannot * make use of buildACLQueries(). */ - if (fout->remoteVersion >= 90600) + if (fout->remoteVersion >= 140000) { appendPQExpBuffer(dbQry, "SELECT tableoid, oid, datname, " "(%s datdba) AS dba, " @@ -2774,7 +2776,41 @@ dumpDatabase(Archive *fout) " AS permp(orig_acl) " " WHERE acl = orig_acl)) AS rdatacls) " " AS rdatacl, " - "datistemplate, datconnlimit, " + "datistemplate, datconnlimit, dathaslogontriggers, " + "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, " + "shobj_description(oid, 'pg_database') AS description " + + "FROM pg_database " + "WHERE datname = current_database()", + username_subquery); + } + else if (fout->remoteVersion >= 90600) + { + appendPQExpBuffer(dbQry, "SELECT tableoid, oid, datname, " + "(%s datdba) AS dba, " + "pg_encoding_to_char(encoding) AS encoding, " + "datcollate, datctype, datfrozenxid, datminmxid, " + "(SELECT array_agg(acl ORDER BY row_n) FROM " + " (SELECT acl, row_n FROM " + " unnest(coalesce(datacl,acldefault('d',datdba))) " + " WITH ORDINALITY AS perm(acl,row_n) " + " WHERE NOT EXISTS ( " + " SELECT 1 " + " FROM unnest(acldefault('d',datdba)) " + " AS init(init_acl) " + " WHERE acl = init_acl)) AS datacls) " + " AS datacl, " + "(SELECT array_agg(acl ORDER BY row_n) FROM " + " (SELECT acl, row_n FROM " + " unnest(acldefault('d',datdba)) " + " WITH ORDINALITY AS initp(acl,row_n) " + " WHERE NOT EXISTS ( " + " SELECT 1 " + " FROM unnest(coalesce(datacl,acldefault('d',datdba))) " + " AS permp(orig_acl) " + " WHERE acl = orig_acl)) AS rdatacls) " + " AS rdatacl, " + "datistemplate, datconnlimit, false as dathaslogontriggers, " "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, " "shobj_description(oid, 'pg_database') AS description " @@ -2788,7 +2824,7 @@ dumpDatabase(Archive *fout) "(%s datdba) AS dba, " "pg_encoding_to_char(encoding) AS encoding, " "datcollate, datctype, datfrozenxid, datminmxid, " - "datacl, '' as rdatacl, datistemplate, datconnlimit, " + "datacl, '' as rdatacl, datistemplate, datconnlimit, false as dathaslogontriggers" "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, " "shobj_description(oid, 'pg_database') AS description " @@ -2802,7 +2838,7 @@ dumpDatabase(Archive *fout) "(%s datdba) AS dba, " "pg_encoding_to_char(encoding) AS encoding, " "datcollate, datctype, datfrozenxid, 0 AS datminmxid, " - "datacl, '' as rdatacl, datistemplate, datconnlimit, " + "datacl, '' as rdatacl, datistemplate, datconnlimit, false as dathaslogontriggers, " "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, " "shobj_description(oid, 'pg_database') AS description " @@ -2816,7 +2852,7 @@ dumpDatabase(Archive *fout) "(%s datdba) AS dba, " "pg_encoding_to_char(encoding) AS encoding, " "NULL AS datcollate, NULL AS datctype, datfrozenxid, 0 AS datminmxid, " - "datacl, '' as rdatacl, datistemplate, datconnlimit, " + "datacl, '' as rdatacl, datistemplate, datconnlimit, false as dathaslogontriggers, " "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, " "shobj_description(oid, 'pg_database') AS description " @@ -2831,7 +2867,7 @@ dumpDatabase(Archive *fout) "pg_encoding_to_char(encoding) AS encoding, " "NULL AS datcollate, NULL AS datctype, datfrozenxid, 0 AS datminmxid, " "datacl, '' as rdatacl, datistemplate, " - "-1 as datconnlimit, " + "-1 as datconnlimit, false as dathaslogontriggers, " "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace " "FROM pg_database " "WHERE datname = current_database()", @@ -2853,6 +2889,7 @@ dumpDatabase(Archive *fout) i_rdatacl = PQfnumber(res, "rdatacl"); i_datistemplate = PQfnumber(res, "datistemplate"); i_datconnlimit = PQfnumber(res, "datconnlimit"); + i_dathaslogontriggers = PQfnumber(res, "dathaslogontriggers"); i_tablespace = PQfnumber(res, "tablespace"); dbCatId.tableoid = atooid(PQgetvalue(res, 0, i_tableoid)); @@ -2867,6 +2904,7 @@ dumpDatabase(Archive *fout) datacl = PQgetvalue(res, 0, i_datacl); rdatacl = PQgetvalue(res, 0, i_rdatacl); datistemplate = PQgetvalue(res, 0, i_datistemplate); + dathaslogontriggers = PQgetvalue(res, 0, i_dathaslogontriggers); datconnlimit = PQgetvalue(res, 0, i_datconnlimit); tablespace = PQgetvalue(res, 0, i_tablespace); @@ -3040,6 +3078,14 @@ dumpDatabase(Archive *fout) appendPQExpBufferStr(delQry, ";\n"); } + if (strcmp(dathaslogontriggers, "t") == 0) + { + appendPQExpBufferStr(creaQry, "UPDATE pg_catalog.pg_database " + "SET dathaslogontriggers = true WHERE datname = "); + appendStringLiteralAH(creaQry, datname, fout); + appendPQExpBufferStr(creaQry, ";\n"); + } + /* Add database-specific SET options */ dumpDatabaseConfig(fout, creaQry, datname, dbCatId.oid); diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index eb018854a5..c1c0cd6753 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -2871,7 +2871,8 @@ psql_completion(const char *text, int start, int end) COMPLETE_WITH("ON"); /* Complete CREATE EVENT TRIGGER <name> ON with event_type */ else if (Matches("CREATE", "EVENT", "TRIGGER", MatchAny, "ON")) - COMPLETE_WITH("ddl_command_start", "ddl_command_end", "sql_drop"); + COMPLETE_WITH("ddl_command_start", "ddl_command_end", + "client_connection", "sql_drop"); /* * Complete CREATE EVENT TRIGGER <name> ON <event_type>. EXECUTE FUNCTION diff --git a/src/include/catalog/pg_database.dat b/src/include/catalog/pg_database.dat index 21cd6604f3..4c10575a31 100644 --- a/src/include/catalog/pg_database.dat +++ b/src/include/catalog/pg_database.dat @@ -15,7 +15,7 @@ { oid => '1', oid_symbol => 'TemplateDbOid', descr => 'default template for new databases', datname => 'template1', encoding => 'ENCODING', datcollate => 'LC_COLLATE', - datctype => 'LC_CTYPE', datistemplate => 't', datallowconn => 't', + datctype => 'LC_CTYPE', datistemplate => 't', dathaslogontriggers => 'f', datallowconn => 't', datconnlimit => '-1', datlastsysoid => '0', datfrozenxid => '0', datminmxid => '1', dattablespace => 'pg_default', datacl => '_null_' }, diff --git a/src/include/catalog/pg_database.h b/src/include/catalog/pg_database.h index f623ee81b7..f60af37089 100644 --- a/src/include/catalog/pg_database.h +++ b/src/include/catalog/pg_database.h @@ -52,6 +52,9 @@ CATALOG(pg_database,1262,DatabaseRelationId) BKI_SHARED_RELATION BKI_ROWTYPE_OID /* new connections allowed? */ bool datallowconn; + /* database has on-login triggers */ + bool dathaslogontriggers; + /* max connections allowed (-1=no limit) */ int32 datconnlimit; diff --git a/src/include/commands/event_trigger.h b/src/include/commands/event_trigger.h index 28b352051b..b00bf66a98 100644 --- a/src/include/commands/event_trigger.h +++ b/src/include/commands/event_trigger.h @@ -21,6 +21,8 @@ #include "tcop/deparse_utility.h" #include "utils/aclchk_internal.h" +extern bool enable_client_connection_trigger; /* GUC */ + typedef struct EventTriggerData { NodeTag type; @@ -54,6 +56,7 @@ extern void EventTriggerDDLCommandStart(Node *parsetree); extern void EventTriggerDDLCommandEnd(Node *parsetree); extern void EventTriggerSQLDrop(Node *parsetree); extern void EventTriggerTableRewrite(Node *parsetree, Oid tableOid, int reason); +extern void EventTriggerOnConnect(void); extern bool EventTriggerBeginCompleteQuery(void); extern void EventTriggerEndCompleteQuery(void); diff --git a/src/include/tcop/cmdtaglist.h b/src/include/tcop/cmdtaglist.h index be94852bbd..988aa39f4b 100644 --- a/src/include/tcop/cmdtaglist.h +++ b/src/include/tcop/cmdtaglist.h @@ -80,6 +80,7 @@ PG_CMDTAG(CMDTAG_CLUSTER, "CLUSTER", false, false, false) PG_CMDTAG(CMDTAG_COMMENT, "COMMENT", true, false, false) PG_CMDTAG(CMDTAG_COMMIT, "COMMIT", false, false, false) PG_CMDTAG(CMDTAG_COMMIT_PREPARED, "COMMIT PREPARED", false, false, false) +PG_CMDTAG(CMDTAG_CONNECT, "CONNECT", true, false, false) PG_CMDTAG(CMDTAG_COPY, "COPY", false, false, true) PG_CMDTAG(CMDTAG_COPY_FROM, "COPY FROM", false, false, false) PG_CMDTAG(CMDTAG_CREATE_ACCESS_METHOD, "CREATE ACCESS METHOD", true, false, false) diff --git a/src/include/tcop/tcopprot.h b/src/include/tcop/tcopprot.h index bd30607b07..90376b22c7 100644 --- a/src/include/tcop/tcopprot.h +++ b/src/include/tcop/tcopprot.h @@ -30,6 +30,11 @@ extern PGDLLIMPORT const char *debug_query_string; extern int max_stack_depth; extern int PostAuthDelay; +/* Hook for plugins to get control at start and end of session */ +typedef void (*client_connection_hook_type) (void); + +extern PGDLLIMPORT client_connection_hook_type client_connection_hook; + /* GUC-configurable parameters */ typedef enum diff --git a/src/include/utils/evtcache.h b/src/include/utils/evtcache.h index bb1e39eb64..ab7e8c39b8 100644 --- a/src/include/utils/evtcache.h +++ b/src/include/utils/evtcache.h @@ -22,7 +22,8 @@ typedef enum EVT_DDLCommandStart, EVT_DDLCommandEnd, EVT_SQLDrop, - EVT_TableRewrite + EVT_TableRewrite, + EVT_Connect, } EventTriggerEvent; typedef struct diff --git a/src/test/recovery/t/001_stream_rep.pl b/src/test/recovery/t/001_stream_rep.pl index 778f11b28b..652995b021 100644 --- a/src/test/recovery/t/001_stream_rep.pl +++ b/src/test/recovery/t/001_stream_rep.pl @@ -43,6 +43,27 @@ $node_standby_2->start; $node_master->safe_psql('postgres', "CREATE TABLE tab_int AS SELECT generate_series(1,1002) AS a"); +$node_primary->safe_psql('postgres', q{ +CREATE ROLE regress_user LOGIN PASSWORD 'pass'; + +CREATE TABLE connects(id serial, who text); + +CREATE FUNCTION on_login_proc() RETURNS EVENT_TRIGGER AS $$ +BEGIN + IF NOT pg_is_in_recovery() THEN + INSERT INTO connects (who) VALUES (session_user); + END IF; + IF session_user = 'regress_hacker' THEN + RAISE EXCEPTION 'You are not welcome!'; + END IF; + RAISE NOTICE 'You are welcome!'; +END; +$$ LANGUAGE plpgsql SECURITY DEFINER; + +CREATE EVENT TRIGGER on_login_trigger ON client_connection EXECUTE FUNCTION on_login_proc(); +ALTER EVENT TRIGGER on_login_trigger ENABLE ALWAYS; +}); + # Wait for standbys to catch up $node_master->wait_for_catchup($node_standby_1, 'replay', $node_master->lsn('insert')); @@ -266,6 +287,9 @@ sub replay_check replay_check(); +$node_standby_1->safe_psql('postgres', "SELECT 1", extra_params => [ '-U', 'regress_user', '-w' ]); +$node_standby_2->safe_psql('postgres', "SELECT 2", extra_params => [ '-U', 'regress_user', '-w' ]); + note "enabling hot_standby_feedback"; # Enable hs_feedback. The slot should gain an xmin. We set the status interval diff --git a/src/test/regress/expected/event_trigger.out b/src/test/regress/expected/event_trigger.out index bdd0ffcdaf..bdfa3eefb8 100644 --- a/src/test/regress/expected/event_trigger.out +++ b/src/test/regress/expected/event_trigger.out @@ -536,3 +536,40 @@ NOTICE: DROP POLICY - ddl_command_end DROP EVENT TRIGGER start_rls_command; DROP EVENT TRIGGER end_rls_command; DROP EVENT TRIGGER sql_drop_command; +-- On session start triggers +create table connects(id serial, who text); +create function on_login_proc() returns event_trigger as $$ +begin + insert into connects (who) values ('I am'); + raise notice 'You are welcome!'; +end; +$$ language plpgsql; +create event trigger on_login_trigger on client_connection execute procedure on_login_proc(); +alter event trigger on_login_trigger enable always; +\c +NOTICE: You are welcome! +select * from connects; + id | who +----+------ + 1 | I am +(1 row) + +\c +NOTICE: You are welcome! +select * from connects; + id | who +----+------ + 1 | I am + 2 | I am +(2 rows) + +-- Test handing exeptions in client_connection trigger +drop table connects; +-- superuser should ignore error +\c +NOTICE: client_connection trigger failed with message: relation "connects" does not exist +-- suppress trigger firing +\c "dbname=regression options='-c enable_client_connection_trigger=false'" +-- Cleanup +drop event trigger on_login_trigger; +drop function on_login_proc(); diff --git a/src/test/regress/expected/sysviews.out b/src/test/regress/expected/sysviews.out index c3b988597c..dc672ab1db 100644 --- a/src/test/regress/expected/sysviews.out +++ b/src/test/regress/expected/sysviews.out @@ -77,27 +77,28 @@ select count(*) = 0 as ok from pg_stat_wal_receiver; -- This is to record the prevailing planner enable_foo settings during -- a regression test run. select name, setting from pg_settings where name like 'enable%'; - name | setting ---------------------------------+--------- - enable_bitmapscan | on - enable_gathermerge | on - enable_hashagg | on - enable_hashjoin | on - enable_incremental_sort | on - enable_indexonlyscan | on - enable_indexscan | on - enable_material | on - enable_mergejoin | on - enable_nestloop | on - enable_parallel_append | on - enable_parallel_hash | on - enable_partition_pruning | on - enable_partitionwise_aggregate | off - enable_partitionwise_join | off - enable_seqscan | on - enable_sort | on - enable_tidscan | on -(18 rows) + name | setting +----------------------------------+--------- + enable_bitmapscan | on + enable_client_connection_trigger | on + enable_gathermerge | on + enable_hashagg | on + enable_hashjoin | on + enable_incremental_sort | on + enable_indexonlyscan | on + enable_indexscan | on + enable_material | on + enable_mergejoin | on + enable_nestloop | on + enable_parallel_append | on + enable_parallel_hash | on + enable_partition_pruning | on + enable_partitionwise_aggregate | off + enable_partitionwise_join | off + enable_seqscan | on + enable_sort | on + enable_tidscan | on +(19 rows) -- Test that the pg_timezone_names and pg_timezone_abbrevs views are -- more-or-less working. We can't test their contents in any great detail diff --git a/src/test/regress/sql/event_trigger.sql b/src/test/regress/sql/event_trigger.sql index 18b2a267cb..abece80426 100644 --- a/src/test/regress/sql/event_trigger.sql +++ b/src/test/regress/sql/event_trigger.sql @@ -429,3 +429,31 @@ DROP POLICY p2 ON event_trigger_test; DROP EVENT TRIGGER start_rls_command; DROP EVENT TRIGGER end_rls_command; DROP EVENT TRIGGER sql_drop_command; + +-- On session start triggers +create table connects(id serial, who text); +create function on_login_proc() returns event_trigger as $$ +begin + insert into connects (who) values ('I am'); + raise notice 'You are welcome!'; +end; +$$ language plpgsql; +create event trigger on_login_trigger on client_connection execute procedure on_login_proc(); +alter event trigger on_login_trigger enable always; +\c +select * from connects; +\c +select * from connects; + +-- Test handing exeptions in client_connection trigger + +drop table connects; +-- superuser should ignore error +\c +-- suppress trigger firing +\c "dbname=regression options='-c enable_client_connection_trigger=false'" + + +-- Cleanup +drop event trigger on_login_trigger; +drop function on_login_proc();
^ permalink raw reply [nested|flat] 3+ messages in thread

* Re[2]: On login trigger: take three
@ 2021-11-03 16:15  Ivan Panchenko <wao@mail.ru>
  1 sibling, 0 replies; 3+ messages in thread

From: Ivan Panchenko @ 2021-11-03 16:15 UTC (permalink / raw)
  To: Teodor Sigaev <teodor@sigaev.ru>; +Cc: Ibrar Ahmed <ibrar.ahmad@gmail.com>; Konstantin Knizhnik <k.knizhnik@postgrespro.ru>; vignesh C <vignesh21@gmail.com>; Pavel Stehule <pavel.stehule@gmail.com>; pgsql-hackers; Amit Kapila <amit.kapila16@gmail.com>; Masahiko Sawada <sawada.mshk@gmail.com>; Greg Nancarrow <gregn4422@gmail.com>; Daniel Gustafsson <daniel@yesql.se>


Dear colleagues, 
Please see my suggestions below and the updated patch attached.
 
 
 
 
  
>Среда, 29 сентября 2021, 15:14 +03:00 от Teodor Sigaev < teodor@sigaev.ru >:
> 
>Hi!
>
>Nice feature, but, sorry, I see some design problem in suggested feature. AFAIK,
>there is two use cases for this feature:
>1 A permission / prohibition to login some users
>2 Just a logging of facts of user's login
>
>Suggested patch proposes prohibition of login only by failing of login trigger
>and it has at least two issues:
>1 In case of prohibition to login, there is no clean way to store information
>about unsuccessful login. Ok, it could be solved by dblink module but it seems
>to scary.
This is a common problem of logging errors and unsuccessful transactions of any kind. It can be solved by:
- logging to external log storage (stupid logging to files or syslog or whatever you can imagine with PL/Perl (sorry))
- logging inside the database by db_link or through background worker (like described in  https://dpavlin.wordpress.com/2017/05/09/david-rader-autonomous-transactions-using-pg_background/ )
- or by implementing autonomous transactions in PostgreSQL, which is already under development by some of my and Teodor’s colleagues.
 
So I propose not to invent another solution to this common problem here.
>2 For logging purpose failing of trigger will cause impossibility to login, it
>could be workarounded by catching error in trigger function, but it's not so
>obvious for DBA.
If the trigger contains an error, nobody can login. The database is bricked.
Previous variant of the patch proposed to fix this with going to single-user mode.
For faster recovery I propose to have also a GUC variable to turn on/off the login triggers. It should be 'on' by default.
>
>some other issues:
>3 It's easy to create security hole, see attachment where non-privileged user
>can close access to database even for superuser.
Such cases can be avoided by careful design of the login triggers and related permissions. Added such note to the documentation.
>4 Feature is not applicable for logging unsuccessful login with wrong
>username/password or not matched by pg_hba.conf. Oracle could operate with such
>cases. But I don't think that this issue is a stopper for the patch.
Yes. Btw, note that pg_hba functionality can be implemented completely inside the login trigger :) !
>
>May be, to solve that issues we could introduce return value of trigger and/or
>add something like IGNORE ERROR to CREATE EVENT TRIGGER command.
Any solutions which make syntax more complicated can lead Postgres to become Oracle (in the worst sense). I do not like this.

A new version of the patch is attached. It applies to the current master and contains the above mentioned GUC code, relevant tests and docs.
Regards,
Ivan Panchenko
>
>On 15.09.2021 16:32, Greg Nancarrow wrote:
>> On Wed, Sep 8, 2021 at 10:56 PM Daniel Gustafsson < daniel@yesql.se > wrote:
>>>
>>> I took a look at this, and while I like the proposed feature I think the patch
>>> has a bit more work required.
>>>
>>
>> Thanks for reviewing the patch.
>> I am not the original patch author (who no longer seems active) but
>> I've been contributing a bit and keeping the patch alive because I
>> think it's a worthwhile feature.
>>
>>>
>>> +
>>> +-- 2) Initialize some user session data
>>> + CREATE TEMP TABLE session_storage (x float, y integer);
>>> The example in the documentation use a mix of indentations, neither of which is
>>> the 2-space indentation used elsewhere in the docs.
>>>
>>
>> Fixed, using 2-space indentation.
>> (to be honest, the indentation seems inconsistent elsewhere in the
>> docs e.g. I'm seeing a nearby case of 2-space on 1st indent, 6-space
>> on 2nd indent)
>>
>>>
>>> + /* Get the command tag. */
>>> + tag = parsetree ? CreateCommandTag(parsetree) : CMDTAG_CONNECT;
>>> This is hardcoding knowledge that currently only this trigger wont have a
>>> parsetree, and setting the tag accordingly. This should instead check the
>>> event and set CMDTAG_UNKNOWN if it isn't the expected one.
>>>
>>
>> I updated that, but maybe not exactly how you expected?
>>
>>>
>>> + /* database has on-login triggers */
>>> + bool dathaslogontriggers;
>>> This patch uses three different names for the same thing: client connection,
>>> logontrigger and login trigger. Since this trigger only fires after successful
>>> authentication it’s not accurate to name it client connection, as that implies
>>> it running on connections rather than logins. The nomenclature used in the
>>> tree is "login" so the patch should be adjusted everywhere to use that.
>>>
>>
>> Fixed.
>>
>>>
>>> +/* Hook for plugins to get control at start of session */
>>> +client_connection_hook_type client_connection_hook = EventTriggerOnConnect;
>>> I don't see the reason for adding core functionality by hooks. Having a hook
>>> might be a useful thing on its own (to be discussed in a new thread, not hidden
>>> here), but core functionality should not depend on it IMO.
>>>
>>
>> Fair enough, I removed the hook.
>>
>>>
>>> + {"enable_client_connection_trigger", PGC_SU_BACKEND, DEVELOPER_OPTIONS,
>>> + gettext_noop("Enables the client_connection event trigger."),
>>> + gettext_noop("In case of errors in the ON client_connection EVENT TRIGGER procedure, "
>>> ..and..
>>> + /*
>>> + * Try to ignore error for superuser to make it possible to login even in case of errors
>>> + * during trigger execution
>>> + */
>>> + if (!is_superuser)
>>> + PG_RE_THROW();
>>> This patch adds two ways for superusers to bypass this event trigger in case of
>>> it being faulty, but for every other event trigger we've documented to restart
>>> in single-user mode and fixing it there. Why does this need to be different?
>>> This clearly has a bigger chance of being a footgun but I don't see that as a
>>> reason to add a GUC and a bypass that other footguns lack.
>>>
>>
>> OK, I removed those bypasses. We'll see what others think.
>>
>>>
>>> + elog(NOTICE, "client_connection trigger failed with message: %s", error->message);
>>> Calling elog() in a PG_CATCH() block isn't allowed is it?
>>>
>>
>> I believe it is allowed (e.g. there's a case in libpq), but I removed
>> this anyway as part of the superuser bypass.
>>
>>>
>>> + /* Runtlist is empty: clear dathaslogontriggers flag
>>> + */
>>> s/Runtlist/Runlist/ and also commenting style.
>>>
>>
>> Fixed.
>>
>>>
>>> The logic for resetting the pg_database flag when firing event trigger in case
>>> the trigger has gone away is messy and a problem waiting to happen IMO. For
>>> normal triggers we don't bother with that on the grounds of it being racy, and
>>> instead perform relhastrigger removal during VACUUM. Another approach is using
>>> a counter as propose upthread, since updating that can be made safer. The
>>> current coding also isn't instructing concurrent backends to perform relcache
>>> rebuild.
>>>
>>
>> I think there are pros and cons of each possible approach, but I think
>> I prefer the current way (which I have tweaked a bit) for similar
>> reasons to those explained by the original patch author when debating
>> whether to use reference-counting instead, in the discussion upthread
>> (e.g. it keeps it all in one place). Also, it seems to be more inline
>> with the documented reason why that pg_database flag was added in the
>> first place. I have debugged a few concurrent scenarios with the
>> current mechanism in place. If you still dislike the logic for
>> resetting the flag, please elaborate on the issues you foresee and one
>> of the alternative approaches can be tried.
>>
>> I've attached the updated patch.
>>
>> Regards,
>> Greg Nancarrow
>> Fujitsu Australia
>>
>--
>Teodor Sigaev E-mail:  teodor@sigaev.ru
>                                                    WWW:  http://www.sigaev.ru/
 

Attachments:

v20-0001-Add-a-new-login-event-and-login-trigger-support.patch (text/x-diff, 33.1K) download diff --git a/doc/src/sgml/bki.sgml b/doc/src/sgml/bki.sgml index f900a01b82..c312165f71 100644 --- a/doc/src/sgml/bki.sgml +++ b/doc/src/sgml/bki.sgml @@ -182,7 +182,7 @@ { oid =&gt; '1', oid_symbol =&gt; 'TemplateDbOid', descr =&gt; 'database\'s default template', datname =&gt; 'template1', encoding =&gt; 'ENCODING', datcollate =&gt; 'LC_COLLATE', - datctype =&gt; 'LC_CTYPE', datistemplate =&gt; 't', datallowconn =&gt; 't', + datctype =&gt; 'LC_CTYPE', datistemplate =&gt; 't', datallowconn =&gt; 't', dathaslogintriggers =&gt; 'f', datconnlimit =&gt; '-1', datlastsysoid =&gt; '0', datfrozenxid =&gt; '0', datminmxid =&gt; '1', dattablespace =&gt; 'pg_default', datacl =&gt; '_null_' }, diff --git a/doc/src/sgml/catalogs.sgml b/doc/src/sgml/catalogs.sgml index c1d11be73f..a2d75687c9 100644 --- a/doc/src/sgml/catalogs.sgml +++ b/doc/src/sgml/catalogs.sgml @@ -2979,6 +2979,17 @@ SCRAM-SHA-256$<replaceable>&lt;iteration count&gt;</replaceable>:<replaceable>&l </para></entry> </row> + <row> + <entry role="catalog_table_entry"><para role="column_definition"> + <structfield>dathaslogintriggers</structfield> <type>bool</type> + </para> + <para> + Indicates that there are login triggers defined for this database. + This flag is used to avoid extra lookups on the pg_event_trigger table during each backend startup. + This flag is used internally by Postgres and should not be manually changed by DBA or application. + </para></entry> + </row> + <row> <entry role="catalog_table_entry"><para role="column_definition"> <structfield>datconnlimit</structfield> <type>int4</type> diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml index de77f14573..4584c077a9 100644 --- a/doc/src/sgml/config.sgml +++ b/doc/src/sgml/config.sgml @@ -1035,6 +1035,22 @@ include_dir 'conf.d' </listitem> </varlistentry> + <varlistentry id="guc-login-triggers" xreflabel="login_triggers"> + <term><varname>login_triggers</varname> (<type>boolean</type>) + <indexterm> + <primary><varname>login_triggers</varname> configuration parameter</primary> + </indexterm> + </term> + + <listitem> + <para> + Enables the <xref linkend="event-trigger-login"/>. The default value is <literal>on</literal>. + </para> + <para> + This parameter can only be set by superusers. + </para> + </listitem> + </varlistentry> </variablelist> </sect2> diff --git a/doc/src/sgml/ecpg.sgml b/doc/src/sgml/ecpg.sgml index 04d4a6bdb2..565f88d298 100644 --- a/doc/src/sgml/ecpg.sgml +++ b/doc/src/sgml/ecpg.sgml @@ -4731,6 +4731,7 @@ datdba = 10 (type: 1) encoding = 0 (type: 5) datistemplate = t (type: 1) datallowconn = t (type: 1) +dathaslogintriggers = f (type: 1) datconnlimit = -1 (type: 5) datlastsysoid = 11510 (type: 1) datfrozenxid = 379 (type: 1) @@ -4756,6 +4757,7 @@ datdba = 10 (type: 1) encoding = 0 (type: 5) datistemplate = f (type: 1) datallowconn = t (type: 1) +dathaslogintriggers = f (type: 1) datconnlimit = -1 (type: 5) datlastsysoid = 11510 (type: 1) datfrozenxid = 379 (type: 1) diff --git a/doc/src/sgml/event-trigger.sgml b/doc/src/sgml/event-trigger.sgml index 60366a950e..2ff7ce75c3 100644 --- a/doc/src/sgml/event-trigger.sgml +++ b/doc/src/sgml/event-trigger.sgml @@ -28,6 +28,7 @@ An event trigger fires whenever the event with which it is associated occurs in the database in which it is defined. Currently, the only supported events are + <literal>login</literal>, <literal>ddl_command_start</literal>, <literal>ddl_command_end</literal>, <literal>table_rewrite</literal> @@ -35,6 +36,19 @@ Support for additional events may be added in future releases. </para> + <para id="event-trigger-login" xreflabel="login event triggers"> + The <literal>login</literal> event occurs when a user logs in to the + system. + If the trigger fails, user will not log in. The side effect of such behaviour is that a bug in a trigger procedure can prevent any user from logging to the system and bricking it. + If this happens, access can be restored by connecting to the system in + single-user mode (as event triggers are disabled in this mode) and fixing the buggy trigger + or by disabling login triggers by the <xref linkend="guc-login-triggers"/> GUC variable. + See the <xref linkend="app-postgres"/> reference page for details about + using single-user mode. + Such cases can be avoided by careful design of trigger procedures and related permissions. + See also <xref linkend="event-trigger-database-login-example"/>. + </para> + <para> The <literal>ddl_command_start</literal> event occurs just before the execution of a <literal>CREATE</literal>, <literal>ALTER</literal>, <literal>DROP</literal>, @@ -1140,7 +1154,7 @@ typedef struct EventTriggerData </sect1> <sect1 id="event-trigger-example"> - <title>A Complete Event Trigger Example</title> + <title>A C language Event Trigger Example</title> <para> Here is a very simple example of an event trigger function written in C. @@ -1280,6 +1294,63 @@ $$; CREATE EVENT TRIGGER no_rewrite_allowed ON table_rewrite EXECUTE FUNCTION no_rewrite(); +</programlisting> + </para> + </sect1> + + <sect1 id="event-trigger-database-login-example" xreflabel="a login trigger example"> + <title>A Database Login Event Trigger Example</title> + + <para> + The event trigger on the <literal>login</literal> event can be + useful for logging user logins, for verifying the connection and + assigning roles according to current circumstances, or for some session data + initialization. + </para> + + <para> + The following example demonstrates these options. +<programlisting> +-- create test tables and roles +CREATE TABLE user_login_log ( + "user" text, + "session_start" timestamp with time zone +); +CREATE ROLE day_worker; +CREATE ROLE night_worker; + +-- the example trigger function +CREATE OR REPLACE FUNCTION init_session() + RETURNS event_trigger SECURITY DEFINER + LANGUAGE plpgsql AS +$$ +DECLARE + hour integer = EXTRACT('hour' FROM current_time); +BEGIN +-- 1) Assign some roles +IF hour BETWEEN 8 AND 20 THEN -- at daytime grant the day_worker role + EXECUTE 'REVOKE night_worker FROM ' || quote_ident(session_user); + EXECUTE 'GRANT day_worker TO ' || quote_ident(session_user); +ELSIF hour BETWEEN 2 AND 4 THEN + RAISE EXCEPTION 'Login forbidden'; -- do not allow to login these hours +ELSE -- at other time grant the night_worker role + EXECUTE 'REVOKE day_worker FROM ' || quote_ident(session_user); + EXECUTE 'GRANT night_worker TO ' || quote_ident(session_user); +END IF; + +-- 2) Initialize some user session data +CREATE TEMP TABLE session_storage (x float, y integer); + +-- 3) Log the connection time +INSERT INTO user_login_log VALUES (session_user, current_timestamp); + +END; +$$; + +-- trigger definition +CREATE EVENT TRIGGER init_session + ON login + EXECUTE FUNCTION init_session(); </programlisting> </para> </sect1> diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c index 029fab48df..a658d476ca 100644 --- a/src/backend/commands/dbcommands.c +++ b/src/backend/commands/dbcommands.c @@ -530,6 +530,7 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt) new_record[Anum_pg_database_datctype - 1] = DirectFunctionCall1(namein, CStringGetDatum(dbctype)); new_record[Anum_pg_database_datistemplate - 1] = BoolGetDatum(dbistemplate); + new_record[Anum_pg_database_dathaslogintriggers - 1] = BoolGetDatum(false); new_record[Anum_pg_database_datallowconn - 1] = BoolGetDatum(dballowconnections); new_record[Anum_pg_database_datconnlimit - 1] = Int32GetDatum(dbconnlimit); new_record[Anum_pg_database_datlastsysoid - 1] = ObjectIdGetDatum(src_lastsysoid); @@ -1585,7 +1586,7 @@ AlterDatabase(ParseState *pstate, AlterDatabaseStmt *stmt, bool isTopLevel) new_record[Anum_pg_database_datconnlimit - 1] = Int32GetDatum(dbconnlimit); new_record_repl[Anum_pg_database_datconnlimit - 1] = true; } - + new_record[Anum_pg_database_dathaslogintriggers - 1] = BoolGetDatum(datform->dathaslogintriggers); newtuple = heap_modify_tuple(tuple, RelationGetDescr(rel), new_record, new_record_nulls, new_record_repl); CatalogTupleUpdate(rel, &tuple->t_self, newtuple); diff --git a/src/backend/commands/event_trigger.c b/src/backend/commands/event_trigger.c index df264329d8..34a95c7513 100644 --- a/src/backend/commands/event_trigger.c +++ b/src/backend/commands/event_trigger.c @@ -20,6 +20,7 @@ #include "catalog/dependency.h" #include "catalog/indexing.h" #include "catalog/objectaccess.h" +#include "catalog/pg_database.h" #include "catalog/pg_event_trigger.h" #include "catalog/pg_namespace.h" #include "catalog/pg_opclass.h" @@ -43,11 +44,15 @@ #include "utils/builtins.h" #include "utils/evtcache.h" #include "utils/fmgroids.h" +#include "utils/inval.h" #include "utils/lsyscache.h" #include "utils/memutils.h" #include "utils/rel.h" +#include "utils/snapmgr.h" #include "utils/syscache.h" +bool login_triggers_enabled = false; + typedef struct EventTriggerQueryState { /* memory context for this state's objects */ @@ -101,6 +106,8 @@ static void EventTriggerInvoke(List *fn_oid_list, EventTriggerData *trigdata); static const char *stringify_grant_objtype(ObjectType objtype); static const char *stringify_adefprivs_objtype(ObjectType objtype); + + /* * Create an event trigger. */ @@ -130,6 +137,7 @@ CreateEventTrigger(CreateEventTrigStmt *stmt) if (strcmp(stmt->eventname, "ddl_command_start") != 0 && strcmp(stmt->eventname, "ddl_command_end") != 0 && strcmp(stmt->eventname, "sql_drop") != 0 && + strcmp(stmt->eventname, "login") != 0 && strcmp(stmt->eventname, "table_rewrite") != 0) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), @@ -293,6 +301,26 @@ insert_event_trigger_tuple(const char *trigname, const char *eventname, Oid evtO CatalogTupleInsert(tgrel, tuple); heap_freetuple(tuple); + if (strcmp(eventname, "login") == 0) + { + Form_pg_database db; + Relation pg_db = table_open(DatabaseRelationId, RowExclusiveLock); + /* Set dathaslogintriggers flag in pg_database */ + tuple = SearchSysCacheCopy1(DATABASEOID, ObjectIdGetDatum(MyDatabaseId)); + if (!HeapTupleIsValid(tuple)) + elog(ERROR, "cache lookup failed for database %u", MyDatabaseId); + db = (Form_pg_database) GETSTRUCT(tuple); + if (!db->dathaslogintriggers) + { + db->dathaslogintriggers = true; + CatalogTupleUpdate(pg_db, &tuple->t_self, tuple); + } + else + CacheInvalidateRelcacheByTuple(tuple); + table_close(pg_db, RowExclusiveLock); + heap_freetuple(tuple); + } + /* Depend on owner. */ recordDependencyOnOwner(EventTriggerRelationId, trigoid, evtOwner); @@ -562,6 +590,9 @@ EventTriggerCommonSetup(Node *parsetree, ListCell *lc; List *runlist = NIL; + /* Get the command tag. */ + tag = (event == EVT_Login) ? CMDTAG_LOGIN : CreateCommandTag(parsetree); + /* * We want the list of command tags for which this procedure is actually * invoked to match up exactly with the list that CREATE EVENT TRIGGER @@ -577,22 +608,18 @@ EventTriggerCommonSetup(Node *parsetree, * relevant command tag. */ #ifdef USE_ASSERT_CHECKING + if (event == EVT_DDLCommandStart || + event == EVT_DDLCommandEnd || + event == EVT_SQLDrop || + event == EVT_Login) { - CommandTag dbgtag; - - dbgtag = CreateCommandTag(parsetree); - if (event == EVT_DDLCommandStart || - event == EVT_DDLCommandEnd || - event == EVT_SQLDrop) - { - if (!command_tag_event_trigger_ok(dbgtag)) - elog(ERROR, "unexpected command tag \"%s\"", GetCommandTagName(dbgtag)); - } - else if (event == EVT_TableRewrite) - { - if (!command_tag_table_rewrite_ok(dbgtag)) - elog(ERROR, "unexpected command tag \"%s\"", GetCommandTagName(dbgtag)); - } + if (!command_tag_event_trigger_ok(tag)) + elog(ERROR, "unexpected command tag \"%s\"", GetCommandTagName(tag)); + } + else if (event == EVT_TableRewrite) + { + if (!command_tag_table_rewrite_ok(tag)) + elog(ERROR, "unexpected command tag \"%s\"", GetCommandTagName(tag)); } #endif @@ -601,9 +628,6 @@ EventTriggerCommonSetup(Node *parsetree, if (cachelist == NIL) return NIL; - /* Get the command tag. */ - tag = CreateCommandTag(parsetree); - /* * Filter list of event triggers by command tag, and copy them into our * memory context. Once we start running the command triggers, or indeed @@ -800,6 +824,111 @@ EventTriggerSQLDrop(Node *parsetree) list_free(runlist); } +/* + * Return true if this database has login triggers, false otherwise. + */ +static bool +DatabaseHasLoginTriggers(void) +{ + bool has_login_triggers; + HeapTuple tuple = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(MyDatabaseId)); + + if (!HeapTupleIsValid(tuple)) + elog(ERROR, "cache lookup failed for database %u", MyDatabaseId); + + has_login_triggers = ((Form_pg_database) GETSTRUCT(tuple))->dathaslogintriggers; + ReleaseSysCache(tuple); + return has_login_triggers; +} + +/* + * Fire login triggers. + */ +void +EventTriggerOnLogin(void) +{ + List *runlist; + EventTriggerData trigdata; + + /* + * See EventTriggerDDLCommandStart for a discussion about why event + * triggers are disabled in single user mode. + */ + if (!IsUnderPostmaster || !OidIsValid(MyDatabaseId)) + return; + + StartTransactionCommand(); + + if (DatabaseHasLoginTriggers()) + { + runlist = EventTriggerCommonSetup(NULL, + EVT_Login, "login", + &trigdata); + + if (runlist != NIL) + { + if(login_triggers_enabled) { + /* + * Make sure anything the main command did will be visible to the event + * triggers. + */ + CommandCounterIncrement(); + + /* + * Event trigger execution may require an active snapshot. + */ + PushActiveSnapshot(GetTransactionSnapshot()); + + /* Run the triggers. */ + EventTriggerInvoke(runlist, &trigdata); + + /* Cleanup. */ + list_free(runlist); + + PopActiveSnapshot(); + } + } + else + { + /* + * Runlist is empty: clear dathaslogintriggers flag + */ + Relation pg_db = table_open(DatabaseRelationId, RowExclusiveLock); + HeapTuple tuple = SearchSysCacheCopy1(DATABASEOID, ObjectIdGetDatum(MyDatabaseId)); + Form_pg_database db; + + if (!HeapTupleIsValid(tuple)) + elog(ERROR, "cache lookup failed for database %u", MyDatabaseId); + + db = (Form_pg_database) GETSTRUCT(tuple); + if (db->dathaslogintriggers) + { + /* + * There can be a race condition: a login event trigger may have + * been added after the pg_event_trigger table was scanned, and + * we don't want to erroneously clear the dathaslogintriggers + * flag in this case. To be sure that this hasn't happened, + * repeat the scan under the pg_database table lock. + */ + AcceptInvalidationMessages(); + runlist = EventTriggerCommonSetup(NULL, + EVT_Login, "login", + &trigdata); + if (runlist == NULL) /* list is still empty, so clear the flag */ + { + db->dathaslogintriggers = false; + CatalogTupleUpdate(pg_db, &tuple->t_self, tuple); + } + else + CacheInvalidateRelcacheByTuple(tuple); + } + table_close(pg_db, RowExclusiveLock); + heap_freetuple(tuple); + } + } + CommitTransactionCommand(); +} + /* * Fire table_rewrite triggers. diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c index 0775abe35d..273611caf9 100644 --- a/src/backend/tcop/postgres.c +++ b/src/backend/tcop/postgres.c @@ -42,6 +42,7 @@ #include "catalog/pg_type.h" #include "commands/async.h" #include "commands/prepare.h" +#include "commands/event_trigger.h" #include "executor/spi.h" #include "jit/jit.h" #include "libpq/libpq.h" @@ -4178,6 +4179,9 @@ PostgresMain(const char *dbname, const char *username) initStringInfo(&row_description_buf); MemoryContextSwitchTo(TopMemoryContext); + /* Fire any defined login triggers, if appropriate */ + EventTriggerOnLogin(); + /* * POSTGRES main processing loop begins here * diff --git a/src/backend/utils/cache/evtcache.c b/src/backend/utils/cache/evtcache.c index 460b720a65..b9b935f1cc 100644 --- a/src/backend/utils/cache/evtcache.c +++ b/src/backend/utils/cache/evtcache.c @@ -167,6 +167,8 @@ BuildEventTriggerCache(void) event = EVT_SQLDrop; else if (strcmp(evtevent, "table_rewrite") == 0) event = EVT_TableRewrite; + else if (strcmp(evtevent, "login") == 0) + event = EVT_Login; else continue; diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c index e91d5a3cfd..a41c3cd7d5 100644 --- a/src/backend/utils/misc/guc.c +++ b/src/backend/utils/misc/guc.c @@ -129,7 +129,7 @@ */ #define REALTYPE_PRECISION 17 -/* XXX these should appear in other modules' header files */ +/* XXX these should appear in other modules or their header files */ extern bool Log_disconnections; extern int CommitDelay; extern int CommitSiblings; @@ -138,6 +138,7 @@ extern char *temp_tablespaces; extern bool ignore_checksum_failure; extern bool ignore_invalid_pages; extern bool synchronize_seqscans; +extern bool login_triggers_enabled; #ifdef TRACE_SYNCSCAN extern bool trace_syncscan; @@ -2119,6 +2120,15 @@ static struct config_bool ConfigureNamesBool[] = NULL, NULL, NULL }, + { + {"login_triggers", PGC_SIGHUP, CLIENT_CONN_OTHER, + gettext_noop("Enables login triggers."), + }, + &login_triggers_enabled, + true, + NULL, NULL, NULL + }, + /* End-of-list marker */ { {NULL, 0, 0, NULL, NULL}, NULL, false, NULL, NULL, NULL diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/utils/misc/postgresql.conf.sample index 1cbc9feeb6..28b8992cfc 100644 --- a/src/backend/utils/misc/postgresql.conf.sample +++ b/src/backend/utils/misc/postgresql.conf.sample @@ -89,6 +89,7 @@ #client_connection_check_interval = 0 # time between checks for client # disconnection while running queries; # 0 for never +#login_triggers = on # - Authentication - diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index b9635a95b6..bd7a21699a 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -2798,6 +2798,7 @@ dumpDatabase(Archive *fout) i_datacl, i_rdatacl, i_datistemplate, + i_dathaslogintriggers, i_datconnlimit, i_tablespace; CatalogId dbCatId; @@ -2810,6 +2811,7 @@ dumpDatabase(Archive *fout) *datacl, *rdatacl, *datistemplate, + *dathaslogintriggers, *datconnlimit, *tablespace; uint32 frozenxid, @@ -2828,7 +2830,7 @@ dumpDatabase(Archive *fout) * (pg_init_privs) are not supported on databases, so this logic cannot * make use of buildACLQueries(). */ - if (fout->remoteVersion >= 90600) + if (fout->remoteVersion >= 150000) { appendPQExpBuffer(dbQry, "SELECT tableoid, oid, datname, " "(%s datdba) AS dba, " @@ -2854,7 +2856,41 @@ dumpDatabase(Archive *fout) " AS permp(orig_acl) " " WHERE acl = orig_acl)) AS rdatacls) " " AS rdatacl, " - "datistemplate, datconnlimit, " + "datistemplate, datconnlimit, dathaslogintriggers, " + "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, " + "shobj_description(oid, 'pg_database') AS description " + + "FROM pg_database " + "WHERE datname = current_database()", + username_subquery); + } + else if (fout->remoteVersion >= 90600) + { + appendPQExpBuffer(dbQry, "SELECT tableoid, oid, datname, " + "(%s datdba) AS dba, " + "pg_encoding_to_char(encoding) AS encoding, " + "datcollate, datctype, datfrozenxid, datminmxid, " + "(SELECT array_agg(acl ORDER BY row_n) FROM " + " (SELECT acl, row_n FROM " + " unnest(coalesce(datacl,acldefault('d',datdba))) " + " WITH ORDINALITY AS perm(acl,row_n) " + " WHERE NOT EXISTS ( " + " SELECT 1 " + " FROM unnest(acldefault('d',datdba)) " + " AS init(init_acl) " + " WHERE acl = init_acl)) AS datacls) " + " AS datacl, " + "(SELECT array_agg(acl ORDER BY row_n) FROM " + " (SELECT acl, row_n FROM " + " unnest(acldefault('d',datdba)) " + " WITH ORDINALITY AS initp(acl,row_n) " + " WHERE NOT EXISTS ( " + " SELECT 1 " + " FROM unnest(coalesce(datacl,acldefault('d',datdba))) " + " AS permp(orig_acl) " + " WHERE acl = orig_acl)) AS rdatacls) " + " AS rdatacl, " + "datistemplate, datconnlimit, false as dathaslogintriggers, " "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, " "shobj_description(oid, 'pg_database') AS description " @@ -2868,7 +2904,7 @@ dumpDatabase(Archive *fout) "(%s datdba) AS dba, " "pg_encoding_to_char(encoding) AS encoding, " "datcollate, datctype, datfrozenxid, datminmxid, " - "datacl, '' as rdatacl, datistemplate, datconnlimit, " + "datacl, '' as rdatacl, datistemplate, datconnlimit, false as dathaslogintriggers" "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, " "shobj_description(oid, 'pg_database') AS description " @@ -2882,7 +2918,7 @@ dumpDatabase(Archive *fout) "(%s datdba) AS dba, " "pg_encoding_to_char(encoding) AS encoding, " "datcollate, datctype, datfrozenxid, 0 AS datminmxid, " - "datacl, '' as rdatacl, datistemplate, datconnlimit, " + "datacl, '' as rdatacl, datistemplate, datconnlimit, false as dathaslogintriggers, " "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, " "shobj_description(oid, 'pg_database') AS description " @@ -2896,7 +2932,7 @@ dumpDatabase(Archive *fout) "(%s datdba) AS dba, " "pg_encoding_to_char(encoding) AS encoding, " "NULL AS datcollate, NULL AS datctype, datfrozenxid, 0 AS datminmxid, " - "datacl, '' as rdatacl, datistemplate, datconnlimit, " + "datacl, '' as rdatacl, datistemplate, datconnlimit, false as dathaslogintriggers, " "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, " "shobj_description(oid, 'pg_database') AS description " @@ -2911,7 +2947,7 @@ dumpDatabase(Archive *fout) "pg_encoding_to_char(encoding) AS encoding, " "NULL AS datcollate, NULL AS datctype, datfrozenxid, 0 AS datminmxid, " "datacl, '' as rdatacl, datistemplate, " - "-1 as datconnlimit, " + "-1 as datconnlimit, false as dathaslogintriggers, " "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace " "FROM pg_database " "WHERE datname = current_database()", @@ -2933,6 +2969,7 @@ dumpDatabase(Archive *fout) i_rdatacl = PQfnumber(res, "rdatacl"); i_datistemplate = PQfnumber(res, "datistemplate"); i_datconnlimit = PQfnumber(res, "datconnlimit"); + i_dathaslogintriggers = PQfnumber(res, "dathaslogintriggers"); i_tablespace = PQfnumber(res, "tablespace"); dbCatId.tableoid = atooid(PQgetvalue(res, 0, i_tableoid)); @@ -2947,6 +2984,7 @@ dumpDatabase(Archive *fout) datacl = PQgetvalue(res, 0, i_datacl); rdatacl = PQgetvalue(res, 0, i_rdatacl); datistemplate = PQgetvalue(res, 0, i_datistemplate); + dathaslogintriggers = PQgetvalue(res, 0, i_dathaslogintriggers); datconnlimit = PQgetvalue(res, 0, i_datconnlimit); tablespace = PQgetvalue(res, 0, i_tablespace); @@ -3120,6 +3158,8 @@ dumpDatabase(Archive *fout) appendPQExpBufferStr(delQry, ";\n"); } + /* We do not restore the pg_database.dathaslogintriggers because it is set automatically on trigger creation */ + /* Add database-specific SET options */ dumpDatabaseConfig(fout, creaQry, datname, dbCatId.oid); diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index 8e01f54500..17e1a058a1 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -3099,7 +3099,8 @@ psql_completion(const char *text, int start, int end) COMPLETE_WITH("ON"); /* Complete CREATE EVENT TRIGGER <name> ON with event_type */ else if (Matches("CREATE", "EVENT", "TRIGGER", MatchAny, "ON")) - COMPLETE_WITH("ddl_command_start", "ddl_command_end", "sql_drop"); + COMPLETE_WITH("ddl_command_start", "ddl_command_end", + "login", "sql_drop"); /* * Complete CREATE EVENT TRIGGER <name> ON <event_type>. EXECUTE FUNCTION diff --git a/src/include/catalog/pg_database.dat b/src/include/catalog/pg_database.dat index b8aa1364a0..2b804a47c3 100644 --- a/src/include/catalog/pg_database.dat +++ b/src/include/catalog/pg_database.dat @@ -15,7 +15,7 @@ { oid => '1', oid_symbol => 'TemplateDbOid', descr => 'default template for new databases', datname => 'template1', encoding => 'ENCODING', datcollate => 'LC_COLLATE', - datctype => 'LC_CTYPE', datistemplate => 't', datallowconn => 't', + datctype => 'LC_CTYPE', datistemplate => 't', dathaslogintriggers => 'f', datallowconn => 't', datconnlimit => '-1', datlastsysoid => '0', datfrozenxid => '0', datminmxid => '1', dattablespace => 'pg_default', datacl => '_null_' }, diff --git a/src/include/catalog/pg_database.h b/src/include/catalog/pg_database.h index 43f3beb6a3..a6e0309bb3 100644 --- a/src/include/catalog/pg_database.h +++ b/src/include/catalog/pg_database.h @@ -52,6 +52,9 @@ CATALOG(pg_database,1262,DatabaseRelationId) BKI_SHARED_RELATION BKI_ROWTYPE_OID /* new connections allowed? */ bool datallowconn; + /* database has login triggers? */ + bool dathaslogintriggers; + /* max connections allowed (-1=no limit) */ int32 datconnlimit; diff --git a/src/include/commands/event_trigger.h b/src/include/commands/event_trigger.h index e765e67fd1..a9f9954597 100644 --- a/src/include/commands/event_trigger.h +++ b/src/include/commands/event_trigger.h @@ -54,6 +54,7 @@ extern void EventTriggerDDLCommandStart(Node *parsetree); extern void EventTriggerDDLCommandEnd(Node *parsetree); extern void EventTriggerSQLDrop(Node *parsetree); extern void EventTriggerTableRewrite(Node *parsetree, Oid tableOid, int reason); +extern void EventTriggerOnLogin(void); extern bool EventTriggerBeginCompleteQuery(void); extern void EventTriggerEndCompleteQuery(void); diff --git a/src/include/tcop/cmdtaglist.h b/src/include/tcop/cmdtaglist.h index 9ba24d4ca9..b0e7df3d81 100644 --- a/src/include/tcop/cmdtaglist.h +++ b/src/include/tcop/cmdtaglist.h @@ -186,6 +186,7 @@ PG_CMDTAG(CMDTAG_INSERT, "INSERT", false, false, true) PG_CMDTAG(CMDTAG_LISTEN, "LISTEN", false, false, false) PG_CMDTAG(CMDTAG_LOAD, "LOAD", false, false, false) PG_CMDTAG(CMDTAG_LOCK_TABLE, "LOCK TABLE", false, false, false) +PG_CMDTAG(CMDTAG_LOGIN, "LOGIN", true, false, false) PG_CMDTAG(CMDTAG_MOVE, "MOVE", false, false, true) PG_CMDTAG(CMDTAG_NOTIFY, "NOTIFY", false, false, false) PG_CMDTAG(CMDTAG_PREPARE, "PREPARE", false, false, false) diff --git a/src/include/utils/evtcache.h b/src/include/utils/evtcache.h index 58ddb71cb1..29d73afe3d 100644 --- a/src/include/utils/evtcache.h +++ b/src/include/utils/evtcache.h @@ -22,7 +22,8 @@ typedef enum EVT_DDLCommandStart, EVT_DDLCommandEnd, EVT_SQLDrop, - EVT_TableRewrite + EVT_TableRewrite, + EVT_Login, } EventTriggerEvent; typedef struct diff --git a/src/test/recovery/t/001_stream_rep.pl b/src/test/recovery/t/001_stream_rep.pl index c70c08e27b..59fa30a3ae 100644 --- a/src/test/recovery/t/001_stream_rep.pl +++ b/src/test/recovery/t/001_stream_rep.pl @@ -46,6 +46,26 @@ $node_standby_2->start; $node_primary->safe_psql('postgres', "CREATE TABLE tab_int AS SELECT generate_series(1,1002) AS a"); +$node_primary->safe_psql('postgres', q{ +CREATE ROLE regress_user LOGIN PASSWORD 'pass'; + +CREATE TABLE user_logins(id serial, who text); + +CREATE FUNCTION on_login_proc() RETURNS EVENT_TRIGGER AS $$ +BEGIN + IF NOT pg_is_in_recovery() THEN + INSERT INTO user_logins (who) VALUES (session_user); + END IF; + IF session_user = 'regress_hacker' THEN + RAISE EXCEPTION 'You are not welcome!'; + END IF; +END; +$$ LANGUAGE plpgsql SECURITY DEFINER; + +CREATE EVENT TRIGGER on_login_trigger ON login EXECUTE FUNCTION on_login_proc(); +ALTER EVENT TRIGGER on_login_trigger ENABLE ALWAYS; +}); + # Wait for standbys to catch up $node_primary->wait_for_catchup($node_standby_1, 'replay', $node_primary->lsn('insert')); @@ -371,6 +391,9 @@ sub replay_check replay_check(); +$node_standby_1->safe_psql('postgres', "SELECT 1", extra_params => [ '-U', 'regress_user', '-w' ]); +$node_standby_2->safe_psql('postgres', "SELECT 2", extra_params => [ '-U', 'regress_user', '-w' ]); + note "enabling hot_standby_feedback"; # Enable hs_feedback. The slot should gain an xmin. We set the status interval diff --git a/src/test/regress/expected/event_trigger.out b/src/test/regress/expected/event_trigger.out index 44d545de25..af3564d532 100644 --- a/src/test/regress/expected/event_trigger.out +++ b/src/test/regress/expected/event_trigger.out @@ -604,3 +604,84 @@ SELECT DROP EVENT TRIGGER start_rls_command; DROP EVENT TRIGGER end_rls_command; DROP EVENT TRIGGER sql_drop_command; +-- On login triggers +CREATE TABLE user_logins(id serial, who text); +GRANT SELECT ON user_logins TO public; +CREATE USER login_tester; +CREATE FUNCTION on_login_proc() RETURNS event_trigger SECURITY DEFINER AS $$ +BEGIN + INSERT INTO user_logins (who) VALUES (SESSION_USER); + RAISE NOTICE 'You are welcome!'; +END; +$$ LANGUAGE plpgsql; +CREATE EVENT TRIGGER on_login_trigger ON LOGIN EXECUTE PROCEDURE on_login_proc(); +ALTER EVENT TRIGGER on_login_trigger ENABLE ALWAYS; +\c +NOTICE: You are welcome! +SELECT count(*) FROM user_logins; + count +------- + 1 +(1 row) + +\set superuser :USER +\c - login_tester +NOTICE: You are welcome! +SELECT count(*) FROM user_logins; + count +------- + 2 +(1 row) + +CREATE EVENT TRIGGER on_login_trigger ON LOGIN EXECUTE PROCEDURE on_login_proc(); -- non-superuser should not be able to! +ERROR: permission denied to create event trigger "on_login_trigger" +HINT: Must be superuser to create an event trigger. +\c - :superuser +NOTICE: You are welcome! +SELECT id, CASE WHEN who = :'superuser' THEN 'superuser' ELSE who END AS who FROM user_logins; + id | who +----+-------------- + 1 | superuser + 2 | login_tester + 3 | superuser +(3 rows) + +-- Turn off login triggers and make an erroneous one. We check that it will not be called. +ALTER SYSTEM SET login_triggers = 'off'; +CREATE FUNCTION on_login_fail() RETURNS event_trigger SECURITY DEFINER AS $$ +BEGIN + RAISE EXCEPTION 'login trigger failure'; +END; +$$ LANGUAGE plpgsql; +CREATE EVENT TRIGGER on_login_fail ON LOGIN EXECUTE PROCEDURE on_login_fail(); +ALTER EVENT TRIGGER on_login_fail ENABLE ALWAYS; +SELECT pg_reload_conf(); + pg_reload_conf +---------------- + t +(1 row) + +\c +SELECT id, CASE WHEN who = :'superuser' THEN 'superuser' ELSE who END AS who FROM user_logins; + id | who +----+-------------- + 1 | superuser + 2 | login_tester + 3 | superuser +(3 rows) + +-- check dathaslogintriggers in system catalog +SELECT dathaslogintriggers FROM pg_database WHERE datname= :'DBNAME'; + dathaslogintriggers +--------------------- + t +(1 row) + +-- Cleanup +DROP TABLE user_logins; +DROP EVENT TRIGGER on_login_trigger; +DROP EVENT TRIGGER on_login_fail; +DROP FUNCTION on_login_proc(); +DROP FUNCTION on_login_fail(); +DROP USER login_tester; +-- End diff --git a/src/test/regress/sql/event_trigger.sql b/src/test/regress/sql/event_trigger.sql index 1446cf8cc8..b54b4d33ce 100644 --- a/src/test/regress/sql/event_trigger.sql +++ b/src/test/regress/sql/event_trigger.sql @@ -462,3 +462,48 @@ SELECT DROP EVENT TRIGGER start_rls_command; DROP EVENT TRIGGER end_rls_command; DROP EVENT TRIGGER sql_drop_command; + +-- On login triggers +CREATE TABLE user_logins(id serial, who text); +GRANT SELECT ON user_logins TO public; +CREATE USER login_tester; +CREATE FUNCTION on_login_proc() RETURNS event_trigger SECURITY DEFINER AS $$ +BEGIN + INSERT INTO user_logins (who) VALUES (SESSION_USER); + RAISE NOTICE 'You are welcome!'; +END; +$$ LANGUAGE plpgsql; +CREATE EVENT TRIGGER on_login_trigger ON LOGIN EXECUTE PROCEDURE on_login_proc(); +ALTER EVENT TRIGGER on_login_trigger ENABLE ALWAYS; +\c +SELECT count(*) FROM user_logins; +\set superuser :USER +\c - login_tester +SELECT count(*) FROM user_logins; +CREATE EVENT TRIGGER on_login_trigger ON LOGIN EXECUTE PROCEDURE on_login_proc(); -- non-superuser should not be able to! +\c - :superuser +SELECT id, CASE WHEN who = :'superuser' THEN 'superuser' ELSE who END AS who FROM user_logins; + +-- Turn off login triggers and make an erroneous one. We check that it will not be called. +ALTER SYSTEM SET login_triggers = 'off'; +CREATE FUNCTION on_login_fail() RETURNS event_trigger SECURITY DEFINER AS $$ +BEGIN + RAISE EXCEPTION 'login trigger failure'; +END; +$$ LANGUAGE plpgsql; +CREATE EVENT TRIGGER on_login_fail ON LOGIN EXECUTE PROCEDURE on_login_fail(); +ALTER EVENT TRIGGER on_login_fail ENABLE ALWAYS; +SELECT pg_reload_conf(); +\c +SELECT id, CASE WHEN who = :'superuser' THEN 'superuser' ELSE who END AS who FROM user_logins; +-- check dathaslogintriggers in system catalog +SELECT dathaslogintriggers FROM pg_database WHERE datname= :'DBNAME'; +-- Cleanup +DROP TABLE user_logins; +DROP EVENT TRIGGER on_login_trigger; +DROP EVENT TRIGGER on_login_fail; +DROP FUNCTION on_login_proc(); +DROP FUNCTION on_login_fail(); +DROP USER login_tester; +-- End +
^ permalink raw reply [nested|flat] 3+ messages in thread

* Re[2]: On login trigger: take three
@ 2022-03-30 11:21  Ivan Panchenko <wao@mail.ru>
  0 siblings, 0 replies; 3+ messages in thread

From: Ivan Panchenko @ 2022-03-30 11:21 UTC (permalink / raw)
  To: Andres Freund <andres@anarazel.de>; +Cc: a.sokolov@postgrespro.ru, Tom Lane <tgl@sss.pgh.pa.us>; Greg Nancarrow <gregn4422@gmail.com>; Teodor Sigaev <teodor@sigaev.ru>; Ibrar Ahmed <ibrar.ahmad@gmail.com>; vignesh C <vignesh21@gmail.com>; Pavel Stehule <pavel.stehule@gmail.com>; pgsql-hackers; Amit Kapila <amit.kapila16@gmail.com>; Masahiko Sawada <sawada.mshk@gmail.com>; Daniel Gustafsson <daniel@yesql.se>


 
Hi, 
>Tue, March 29, 2022, 0:31 +03:00 from Andres Freund <andres@anarazel.de>:
> 
>Hi,
>
>On 2022-03-28 23:27:56 +0200, Daniel Gustafsson wrote:
>> > On 28 Mar 2022, at 19:10, Andres Freund < andres@anarazel.de > wrote:
>> > On 2022-03-28 15:57:37 +0300,  a.sokolov@postgrespro.ru wrote:
>>
>> >> + data initialization. It is vital that any event trigger using the
>> >> + <literal>login</literal> event checks whether or not the database is in
>> >> + recovery.
>> »
 
>> >> Does any trigger really have to contain a pg_is_in_recovery() call?
>> >
>> > Not *any* trigger, just any trigger that writes.
>>
>> Thats correct, the docs should be updated with something like the below I
>> reckon.
>>
>> It is vital that event trigger using the <literal>login</literal> event
>> which has side-effects checks whether or not the database is in recovery to
>> ensure they are not performing modifications to hot standby nodes.
>
>Maybe side-effects is a bit too general? Emitting a log message, rejecting a
>login, setting some GUCs, etc are all side-effects too.
Something like this:
 
<important>
    <para>
      The <literal>login</literal> triggers fire also on standby servers.
      To keep them from becoming inaccessible, such triggers should
      avoid writing anything to the database when running on a standby.
      This can be achieved by checking <function>pg_is_in_recovery</function>(), see an example below.
    </para>
</important>
> 
Also, please fix a typo in doc/src/sgml/ref/create_event_trigger.sgml :
 
- single-user mode and you'll be able to do that. Even triggers can also be
+ single-user mode and you'll be able to do that. Event triggers can also be
 
Regarding the trigger function example:
It does not do anything if run on a standby. To show that it can do something on a standby to, I propose to move throwing the night exception to the beginning.
So it will be:
 
CREATE OR REPLACE FUNCTION init_session() 
RETURNS event_trigger SECURITY DEFINER LANGUAGE plpgsql AS 
$$ 
DECLARE 
  hour integer = EXTRACT('hour' FROM current_time); 
  rec boolean;
BEGIN

-- 1) Forbid logging in late:
IF hour BETWEEN 2 AND 4 THEN
  RAISE EXCEPTION 'Login forbidden';  -- do not allow to login these hours
END IF;

-- The remaining stuff cannot be done on standbys,
-- so ensure the database is not in recovery
SELECT pg_is_in_recovery() INTO rec;
IF rec THEN
  RETURN;
END IF

-- 2) Assign some roles

IF hour BETWEEN 8 AND 20 THEN         -- at daytime grant the day_worker role
  EXECUTE 'REVOKE night_worker FROM ' || quote_ident(session_user);
  EXECUTE 'GRANT    day_worker TO '   || quote_ident(session_user);
ELSE                                  -- at other time grant the night_worker role
  EXECUTE 'REVOKE day_worker FROM ' || quote_ident(session_user);
  EXECUTE 'GRANT  night_worker TO ' || quote_ident(session_user);
END IF;

-- 3) Initialize some user session data

CREATE TEMP TABLE session_storage (x float, y integer);

-- 4) Log the connection time

INSERT INTO user_login_log VALUES (session_user, current_timestamp);

END;
$$;
Finally, let me propose to append to the regression test the following:
 
 
\c 
SELECT dathasloginevt FROM pg_database WHERE datname= :'DBNAME';
 
which should output:
 dathasloginevt 
----------------
 f
(1 row)
 
So we can check that removal of the event trigger resets this flag in pg_database. Note that reconnect (\c) is necessary here.
 
Regards,
Ivan
 
>
>> >> In this message
>> >> ( https://www.postgresql.org/message-id/20220312024652.lvgehszwke4hhove%40alap3.anarazel.de )
>> >> it was only about triggers on hot standby, which run not read-only queries
>> >
>> > The problem precisely is that the login triggers run on hot standby nodes, and
>> > that if they do writes, you can't login anymore.
>>
>> Do you think this potential foot-gun is scary enough to reject this patch?
>> There are lots of creative ways to cause Nagios alerts from ones database, but
>> this has the potential to do so with a small bug in userland code. Still, I
>> kind of like the feature so I'm indecisive.
>
>It does seem like a huge footgun. But also kinda useful. So I'm really +-0.
>
>Greetings,
>
>Andres Freund
 

^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2022-03-30 11:21 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-03-16 11:32 ` Ivan Panchenko <wao@mail.ru>
2021-11-03 16:15 ` Ivan Panchenko <wao@mail.ru>
2022-03-30 11:21 Re[2]: On login trigger: take three Ivan Panchenko <wao@mail.ru>

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