diff options
| author | Michael Paquier | 2022-09-29 06:05:40 +0000 |
|---|---|---|
| committer | Michael Paquier | 2022-09-29 06:05:40 +0000 |
| commit | 0823d061b0b7f1e20fbfd48bef3c2e093493dbd4 (patch) | |
| tree | 49b915201d8f0b273a3adc7abfa5ef814fdda17e /src/backend | |
| parent | 5ac9e869191148741539e626b84ba7e77dc71670 (diff) | |
Introduce SYSTEM_USER
SYSTEM_USER is a reserved keyword of the SQL specification that,
roughly described, is aimed at reporting some information about the
system user who has connected to the database server. It may include
implementation-specific information about the means by the user
connected, like an authentication method.
This commit implements SYSTEM_USER as of auth_method:identity, where
"auth_method" is a keyword about the authentication method used to log
into the server (like peer, md5, scram-sha-256, gss, etc.) and
"identity" is the authentication identity as introduced by 9afffcb (peer
sets authn to the OS user name, gss to the user principal, etc.). This
format has been suggested by Tom Lane.
Note that thanks to d951052, SYSTEM_USER is available to parallel
workers.
Bump catalog version.
Author: Bertrand Drouvot
Reviewed-by: Jacob Champion, Joe Conway, Álvaro Herrera, Michael Paquier
Discussion: https://postgr.es/m/7e692b8c-0b11-45db-1cad-3afc5b57409f@amazon.com
Diffstat (limited to 'src/backend')
| -rw-r--r-- | src/backend/access/transam/parallel.c | 8 | ||||
| -rw-r--r-- | src/backend/parser/gram.y | 11 | ||||
| -rw-r--r-- | src/backend/utils/adt/ruleutils.c | 4 | ||||
| -rw-r--r-- | src/backend/utils/init/miscinit.c | 50 | ||||
| -rw-r--r-- | src/backend/utils/init/postinit.c | 4 |
5 files changed, 76 insertions, 1 deletions
diff --git a/src/backend/access/transam/parallel.c b/src/backend/access/transam/parallel.c index 8cba8882239..ee0985c7edd 100644 --- a/src/backend/access/transam/parallel.c +++ b/src/backend/access/transam/parallel.c @@ -1496,6 +1496,14 @@ ParallelWorkerMain(Datum main_arg) false); RestoreClientConnectionInfo(clientconninfospace); + /* + * Initialize SystemUser now that MyClientConnectionInfo is restored. + * Also ensure that auth_method is actually valid, aka authn_id is not NULL. + */ + if (MyClientConnectionInfo.authn_id) + InitializeSystemUser(MyClientConnectionInfo.authn_id, + hba_authname(MyClientConnectionInfo.auth_method)); + /* Attach to the leader's serializable transaction, if SERIALIZABLE. */ AttachSerializableXact(fps->serializable_xact_handle); diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index 0d8d2928509..94d5142a4a0 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -743,7 +743,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query); SERIALIZABLE SERVER SESSION SESSION_USER SET SETS SETOF SHARE SHOW SIMILAR SIMPLE SKIP SMALLINT SNAPSHOT SOME SQL_P STABLE STANDALONE_P START STATEMENT STATISTICS STDIN STDOUT STORAGE STORED STRICT_P STRIP_P - SUBSCRIPTION SUBSTRING SUPPORT SYMMETRIC SYSID SYSTEM_P + SUBSCRIPTION SUBSTRING SUPPORT SYMMETRIC SYSID SYSTEM_P SYSTEM_USER TABLE TABLES TABLESAMPLE TABLESPACE TEMP TEMPLATE TEMPORARY TEXT_P THEN TIES TIME TIMESTAMP TO TRAILING TRANSACTION TRANSFORM @@ -15239,6 +15239,13 @@ func_expr_common_subexpr: { $$ = makeSQLValueFunction(SVFOP_SESSION_USER, -1, @1); } + | SYSTEM_USER + { + $$ = (Node *) makeFuncCall(SystemFuncName("system_user"), + NIL, + COERCE_SQL_SYNTAX, + @1); + } | USER { $$ = makeSQLValueFunction(SVFOP_USER, -1, @1); @@ -17120,6 +17127,7 @@ reserved_keyword: | SESSION_USER | SOME | SYMMETRIC + | SYSTEM_USER | TABLE | THEN | TO @@ -17500,6 +17508,7 @@ bare_label_keyword: | SYMMETRIC | SYSID | SYSTEM_P + | SYSTEM_USER | TABLE | TABLES | TABLESAMPLE diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index 2b7b1b0c0f6..c4184035374 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -10317,6 +10317,10 @@ get_func_sql_syntax(FuncExpr *expr, deparse_context *context) appendStringInfoChar(buf, ')'); return true; + case F_SYSTEM_USER: + appendStringInfoString(buf, "SYSTEM_USER"); + return true; + case F_XMLEXISTS: /* XMLEXISTS ... extra parens because args are c_expr */ appendStringInfoString(buf, "XMLEXISTS(("); diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/miscinit.c index 4d341d3f7f1..dfdcd3d78eb 100644 --- a/src/backend/utils/init/miscinit.c +++ b/src/backend/utils/init/miscinit.c @@ -477,6 +477,7 @@ static Oid AuthenticatedUserId = InvalidOid; static Oid SessionUserId = InvalidOid; static Oid OuterUserId = InvalidOid; static Oid CurrentUserId = InvalidOid; +static const char *SystemUser = NULL; /* We also have to remember the superuser state of some of these levels */ static bool AuthenticatedUserIsSuperuser = false; @@ -549,6 +550,16 @@ SetSessionUserId(Oid userid, bool is_superuser) } /* + * Return the system user representing the authenticated identity. + * It is defined in InitializeSystemUser() as auth_method:authn_id. + */ +const char * +GetSystemUser(void) +{ + return SystemUser; +} + +/* * GetAuthenticatedUserId - get the authenticated user ID */ Oid @@ -818,6 +829,45 @@ InitializeSessionUserIdStandalone(void) SetSessionUserId(BOOTSTRAP_SUPERUSERID, true); } +/* + * Initialize the system user. + * + * This is built as auth_method:authn_id. + */ +void +InitializeSystemUser(const char *authn_id, const char *auth_method) +{ + char *system_user; + + /* call only once */ + Assert(SystemUser == NULL); + + /* + * InitializeSystemUser should be called only when authn_id is not NULL, + * meaning that auth_method is valid. + */ + Assert(authn_id != NULL); + + system_user = psprintf("%s:%s", auth_method, authn_id); + + /* Store SystemUser in long-lived storage */ + SystemUser = MemoryContextStrdup(TopMemoryContext, system_user); + pfree(system_user); +} + +/* + * SQL-function SYSTEM_USER + */ +Datum +system_user(PG_FUNCTION_ARGS) +{ + const char *sysuser = GetSystemUser(); + + if (sysuser) + PG_RETURN_DATUM(CStringGetTextDatum(sysuser)); + else + PG_RETURN_NULL(); +} /* * Change session auth ID while running diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c index 4a207a73917..31b7e1de5df 100644 --- a/src/backend/utils/init/postinit.c +++ b/src/backend/utils/init/postinit.c @@ -904,6 +904,10 @@ InitPostgres(const char *in_dbname, Oid dboid, Assert(MyProcPort != NULL); PerformAuthentication(MyProcPort); InitializeSessionUserId(username, useroid); + /* ensure that auth_method is actually valid, aka authn_id is not NULL */ + if (MyClientConnectionInfo.authn_id) + InitializeSystemUser(MyClientConnectionInfo.authn_id, + hba_authname(MyClientConnectionInfo.auth_method)); am_superuser = superuser(); } |
