Skip to content

Commit 41a2ccc

Browse files
mariadb-RuchaDeodharsjaakola
authored andcommitted
MDEV-35511: Backport fix for Audit log not reporting user in Galera cluster
Setting a nae for the the THD::security_ctx:user field for wsrep applier threads. With this, the audit log events related to wsrep applying will be written in the audit log. Using user name <cluster user> for wsrep appliers. This is for having identical look with async replication, which uses: <replication_user> user name. Another option for <cluster user> could be e.g. <wsrep user>. Hoever, using galera for user name is not a good pick, as the cluster may have (and soon will have) other GCS backends. Using same approach as async replication to replace the security_ctx user name with "system user" for processlist output. Commit has also mtr test galera.MDEV-35511, to vevrify wsrep applier audit logging. The test does not install/uninstall audit log plugin, bu loads the audit log plugin before the test. This is because uninstalling the audit log plugin gives a warning saying that plugin is busy and uninstall will be delayed until server shutdown. This anomaly must be because of the applier thread being active audit logger. Same problem with plugin unsinstall happens also with async relication workers. If plugn remains installed, the post test sanity check will complain of mismatching state of pre and post test states.
1 parent 759e352 commit 41a2ccc

File tree

8 files changed

+68
-6
lines changed

8 files changed

+68
-6
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
connection node_2;
2+
connection node_1;
3+
connection node_2;
4+
SET GLOBAL server_audit_logging=ON;
5+
connection node_1;
6+
CREATE TABLE t1(a INT);
7+
INSERT INTO t1 VALUES (1);
8+
connection node_2;
9+
# Now checking the audit log
10+
TIME,HOSTNAME,root,localhost,ID,ID,QUERY,test,'SET GLOBAL server_audit_logging=ON',0
11+
TIME,HOSTNAME,<cluster applier>,,ID,ID,CREATE,test,t1,
12+
TIME,HOSTNAME,<cluster applier>,,ID,ID,WRITE,test,t1,
13+
TIME,HOSTNAME,root,localhost,ID,ID,READ,test,t1,
14+
TIME,HOSTNAME,root,localhost,ID,ID,READ,mysql,table_stats,
15+
TIME,HOSTNAME,root,localhost,ID,ID,READ,mysql,column_stats,
16+
TIME,HOSTNAME,root,localhost,ID,ID,READ,mysql,index_stats,
17+
TIME,HOSTNAME,root,localhost,ID,ID,QUERY,test,'SELECT COUNT(*) = 1 FROM test.t1',0
18+
TIME,HOSTNAME,root,localhost,ID,ID,QUERY,test,'SELECT @@datadir',0
19+
# resetting the test state
20+
SET GLOBAL server_audit_logging=DEFAULT;
21+
connection node_1;
22+
DROP TABLE t1;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--plugin-load=server_audit
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--source include/galera_cluster.inc
2+
3+
if (!$SERVER_AUDIT_SO) {
4+
skip No SERVER_AUDIT plugin;
5+
}
6+
7+
# enable audit loggin in node 2
8+
--connection node_2
9+
SET GLOBAL server_audit_logging=ON;
10+
11+
# replicate CREATE and INSERT, these should be seen in the audit log
12+
--connection node_1
13+
CREATE TABLE t1(a INT);
14+
INSERT INTO t1 VALUES (1);
15+
16+
--connection node_2
17+
# make sure that the INSERT has been applied
18+
--let $wait_condition = SELECT COUNT(*) = 1 FROM test.t1;
19+
--source include/wait_condition.inc
20+
21+
--echo # Now checking the audit log
22+
let $MYSQLD_DATADIR= `SELECT @@datadir`;
23+
--replace_regex /[0-9]* [0-9][0-9]:[0-9][0-9]:[0-9][0-9]\,[^,]*\,/TIME,HOSTNAME,/ /\,[1-9][0-9]*\,/,1,/ /\,[1-9][0-9]*/,ID/
24+
cat_file $MYSQLD_DATADIR/server_audit.log;
25+
26+
--echo # resetting the test state
27+
SET GLOBAL server_audit_logging=DEFAULT;
28+
29+
--connection node_1
30+
DROP TABLE t1;
31+
32+
#UNINSTALL PLUGIN server_audit;
33+
34+
remove_file $MYSQLD_DATADIR/server_audit.log;

sql/mysqld.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,8 @@ static TYPELIB tc_heuristic_recover_typelib=
297297

298298
const char *first_keyword= "first";
299299
const char *my_localhost= "localhost",
300-
*delayed_user= "delayed", *slave_user= "<replication_slave>";
300+
*delayed_user= "delayed", *slave_user= "<replication_slave>",
301+
*cluster_user= "<cluster applier>";
301302

302303
bool opt_large_files= sizeof(my_off_t) > 4;
303304
static my_bool opt_autocommit; ///< for --autocommit command-line option

sql/mysqld.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ extern time_t server_start_time, flush_status_time;
263263
extern char *opt_mysql_tmpdir, mysql_charsets_dir[];
264264
extern size_t mysql_unpacked_real_data_home_len;
265265
extern MYSQL_PLUGIN_IMPORT MY_TMPDIR mysql_tmpdir_list;
266-
extern const char *first_keyword, *delayed_user, *slave_user;
266+
extern const char *first_keyword, *delayed_user, *slave_user, *cluster_user;
267267
extern MYSQL_PLUGIN_IMPORT const char *my_localhost;
268268
extern MYSQL_PLUGIN_IMPORT const char **errmesg; /* Error messages */
269269
extern const char *myisam_recover_options_str;

sql/sql_class.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1602,7 +1602,7 @@ class Security_context {
16021602
bool check_access(const privilege_t want_access, bool match_any = false);
16031603
bool is_priv_user(const char *user, const char *host);
16041604
bool is_user_defined() const
1605-
{ return user && user != delayed_user && user != slave_user; };
1605+
{ return user && user != delayed_user && user != slave_user && user != cluster_user; };
16061606
};
16071607

16081608

sql/sql_show.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2803,7 +2803,9 @@ static my_bool list_callback(THD *tmp, list_callback_arg *arg)
28032803

28042804
thd_info->thread_id=tmp->thread_id;
28052805
thd_info->os_thread_id=tmp->os_thread_id;
2806-
thd_info->user= arg->thd->strdup(tmp_sctx->user && tmp_sctx->user != slave_user ?
2806+
thd_info->user= arg->thd->strdup(tmp_sctx->user &&
2807+
(tmp_sctx->user != slave_user &&
2808+
tmp_sctx->user != cluster_user) ?
28072809
tmp_sctx->user :
28082810
(tmp->system_thread ?
28092811
"system user" : "unauthenticated user"));
@@ -3256,8 +3258,9 @@ static my_bool processlist_callback(THD *tmp, processlist_callback_arg *arg)
32563258
/* ID */
32573259
arg->table->field[0]->store((longlong) tmp->thread_id, TRUE);
32583260
/* USER */
3259-
val= tmp_sctx->user && tmp_sctx->user != slave_user ? tmp_sctx->user :
3260-
(tmp->system_thread ? "system user" : "unauthenticated user");
3261+
val= tmp_sctx->user && (tmp_sctx->user != slave_user &&
3262+
tmp_sctx->user != cluster_user) ? tmp_sctx->user :
3263+
(tmp->system_thread ? "system user" : "unauthenticated user");
32613264
arg->table->field[1]->store(val, strlen(val), cs);
32623265
/* HOST */
32633266
if (tmp->peer_port && (tmp_sctx->host || tmp_sctx->ip) &&

sql/wsrep_mysqld.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3801,6 +3801,7 @@ void* start_wsrep_THD(void *arg)
38013801

38023802
thd->system_thread= SYSTEM_THREAD_SLAVE_SQL;
38033803
thd->security_ctx->skip_grants();
3804+
thd->security_ctx->user = (char *)cluster_user;
38043805

38053806
/* handle_one_connection() again... */
38063807
thd->mark_connection_idle();

0 commit comments

Comments
 (0)