From 73efcb22c4ba4de1e44dd4c3baf022938bef2b53 Mon Sep 17 00:00:00 2001 From: Nils Dijk Date: Wed, 13 Jun 2018 11:33:34 +0200 Subject: [PATCH] Extract RoleSpecString and resolve role references --- .../distributed/executor/multi_utility.c | 59 +++++++++++++------ src/include/distributed/multi_utility.h | 1 + 2 files changed, 43 insertions(+), 17 deletions(-) diff --git a/src/backend/distributed/executor/multi_utility.c b/src/backend/distributed/executor/multi_utility.c index 54868f17c..ca744028f 100644 --- a/src/backend/distributed/executor/multi_utility.c +++ b/src/backend/distributed/executor/multi_utility.c @@ -3514,7 +3514,7 @@ PlanGrantStmt(GrantStmt *grantStmt) } } - /* deparse the privileges */ + /* deparse the grantees */ isFirst = true; foreach(granteeCell, grantStmt->grantees) { @@ -3526,22 +3526,7 @@ PlanGrantStmt(GrantStmt *grantStmt) } isFirst = false; - if (spec->roletype == ROLESPEC_CSTRING) - { - appendStringInfoString(&granteesString, quote_identifier(spec->rolename)); - } - else if (spec->roletype == ROLESPEC_CURRENT_USER) - { - appendStringInfoString(&granteesString, "CURRENT_USER"); - } - else if (spec->roletype == ROLESPEC_SESSION_USER) - { - appendStringInfoString(&granteesString, "SESSION_USER"); - } - else if (spec->roletype == ROLESPEC_PUBLIC) - { - appendStringInfoString(&granteesString, "PUBLIC"); - } + appendStringInfoString(&granteesString, RoleSpecString(spec)); } /* @@ -3671,6 +3656,46 @@ CollectGrantTableIdList(GrantStmt *grantStmt) } +/* + * RoleSpecString resolves the role specification to its string form that is suitable for transport to a worker node. + * This function resolves the following identifiers from the current context so they are safe to transfer. + * + * CURRENT_USER - resolved to the user name of the current role being used + * SESSION_USER - resolved to the user name of the user that opened the session + */ +const char * +RoleSpecString(RoleSpec *spec) +{ + switch (spec->roletype) + { + case ROLESPEC_CSTRING: + { + return quote_identifier(spec->rolename); + } + + case ROLESPEC_CURRENT_USER: + { + return quote_identifier(GetUserNameFromId(GetUserId(), false)); + } + + case ROLESPEC_SESSION_USER: + { + return quote_identifier(GetUserNameFromId(GetSessionUserId(), false)); + } + + case ROLESPEC_PUBLIC: + { + return "PUBLIC"; + } + + default: + { + elog(ERROR, "unexpected role type %d", spec->roletype); + } + } +} + + /* * ProcessDropTableStmt processes DROP TABLE commands for partitioned tables. * If we are trying to DROP partitioned tables, we first need to go to MX nodes diff --git a/src/include/distributed/multi_utility.h b/src/include/distributed/multi_utility.h index 7e1ec9fc2..a0f044244 100644 --- a/src/include/distributed/multi_utility.h +++ b/src/include/distributed/multi_utility.h @@ -50,5 +50,6 @@ extern void ErrorIfUnsupportedConstraint(Relation relation, char distributionMet extern Datum master_drop_all_shards(PG_FUNCTION_ARGS); extern Datum master_modify_multiple_shards(PG_FUNCTION_ARGS); +extern const char * RoleSpecString(RoleSpec *spec); #endif /* MULTI_UTILITY_H */