From 1f7ac843fd88ee5f2db776efebd2ef9db50a3a9b Mon Sep 17 00:00:00 2001 From: Ian Barwick Date: Tue, 1 Sep 2020 14:37:33 +0900 Subject: [PATCH] Consolidate role availability checking code --- dbutils.c | 45 ----------------------------------------- dbutils.h | 1 - repmgr-action-node.c | 2 +- repmgr-action-standby.c | 2 +- 4 files changed, 2 insertions(+), 48 deletions(-) diff --git a/dbutils.c b/dbutils.c index 55856da2..b7efb100 100644 --- a/dbutils.c +++ b/dbutils.c @@ -1858,51 +1858,6 @@ can_execute_pg_promote(PGconn *conn) } -bool -connection_has_pg_settings(PGconn *conn) -{ - bool has_pg_settings = false; - - /* superusers can always read pg_settings */ - if (is_superuser_connection(conn, NULL) == true) - { - has_pg_settings = true; - } - /* from PostgreSQL 10, a non-superuser may have been granted access */ - else if (PQserverVersion(conn) >= 100000) - { - PQExpBufferData query; - PGresult *res; - - initPQExpBuffer(&query); - appendPQExpBufferStr(&query, - " SELECT CASE " - " WHEN pg_catalog.pg_has_role('pg_monitor','MEMBER') " - " THEN TRUE " - " WHEN pg_catalog.pg_has_role('pg_read_all_settings','MEMBER') " - " THEN TRUE " - " ELSE FALSE " - " END AS has_pg_settings"); - - res = PQexec(conn, query.data); - - if (PQresultStatus(res) != PGRES_TUPLES_OK) - { - log_db_error(conn, query.data, - _("connection_has_pg_settings(): unable to query user roles")); - } - else - { - has_pg_settings = atobool(PQgetvalue(res, 0, 0)); - } - termPQExpBuffer(&query); - PQclear(res); - } - - return has_pg_settings; -} - - /* * Determine if the user associated with the current connection is * a member of the "pg_monitor" default role, or optionally one diff --git a/dbutils.h b/dbutils.h index 22b4aad0..4dd123de 100644 --- a/dbutils.h +++ b/dbutils.h @@ -453,7 +453,6 @@ TimeLineHistoryEntry *get_timeline_history(PGconn *repl_conn, TimeLineID tli); /* user/role information functions */ bool can_execute_pg_promote(PGconn *conn); -bool connection_has_pg_settings(PGconn *conn); bool connection_has_pg_monitor_role(PGconn *conn, const char *subrole); bool is_replication_role(PGconn *conn, char *rolname); bool is_superuser_connection(PGconn *conn, t_connection_user *userinfo); diff --git a/repmgr-action-node.c b/repmgr-action-node.c index 2e8ec06e..0696ded1 100644 --- a/repmgr-action-node.c +++ b/repmgr-action-node.c @@ -2045,7 +2045,7 @@ do_node_check_data_directory(PGconn *conn, OutputMode mode, t_node_info *node_in * Check actual data directory matches that in repmgr.conf; note this requires * a superuser connection */ - if (connection_has_pg_settings(conn) == true) + if (connection_has_pg_monitor_role(conn, "pg_read_all_settings") == true) { /* we expect to have a database connection */ if (get_pg_setting(conn, "data_directory", actual_data_directory) == false) diff --git a/repmgr-action-standby.c b/repmgr-action-standby.c index aaf73af9..27edb920 100644 --- a/repmgr-action-standby.c +++ b/repmgr-action-standby.c @@ -5784,7 +5784,7 @@ check_source_server() * This will check if the user is superuser or (from Pg10) is a member * of "pg_read_all_settings"/"pg_monitor" */ - if (connection_has_pg_settings(source_conn)) + if (connection_has_pg_monitor_role(source_conn, "pg_read_all_settings") == true) { SettingsUser = REPMGR_USER; }