Skip to content

Commit 1def6b7

Browse files
drilibojtolmer
authored andcommitted
Expose more information to audit plugin
Summary: We need some extra info for the shadowing and security logging. This is a simple first step of info that MariaDB actually also exposes. Now we would have the `query_id` and the database name for general events. Making as few changes as possible to accomplish it, so I'm just taking the information from the TDH and exposing it through `mysql_event_general` struct and as a argument to disconnect. Test Plan: I didn't find any proper testing for plugin changes, so I'm trusting Jenkins Reviewers: tianx, chip, santoshb, jtolmer Reviewed By: chip
1 parent d9cc1b5 commit 1def6b7

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

Diff for: include/mysql/plugin_audit.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
#define MYSQL_AUDIT_CLASS_MASK_SIZE 1
2727

28-
#define MYSQL_AUDIT_INTERFACE_VERSION 0x0301
28+
#define MYSQL_AUDIT_INTERFACE_VERSION 0x0302
2929

3030

3131
/*************************************************************************
@@ -63,6 +63,10 @@ struct mysql_event_general
6363
MYSQL_LEX_STRING general_sql_command;
6464
MYSQL_LEX_STRING general_external_user;
6565
MYSQL_LEX_STRING general_ip;
66+
/* Added in version 0x302 */
67+
long long query_id;
68+
const char *database;
69+
unsigned int database_length;
6670
};
6771

6872

Diff for: include/mysql/plugin_audit.h.pp

+3
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,9 @@
276276
MYSQL_LEX_STRING general_sql_command;
277277
MYSQL_LEX_STRING general_external_user;
278278
MYSQL_LEX_STRING general_ip;
279+
long long query_id;
280+
const char *database;
281+
unsigned int database_length;
279282
};
280283
struct mysql_event_connection
281284
{

Diff for: sql/sql_audit.cc

+3
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ static void general_class_handler(THD *thd, uint event_subtype, va_list ap)
8787
event.general_host= va_arg(ap, MYSQL_LEX_STRING);
8888
event.general_external_user= va_arg(ap, MYSQL_LEX_STRING);
8989
event.general_ip= va_arg(ap, MYSQL_LEX_STRING);
90+
event.database= va_arg(ap, const char *);
91+
event.database_length= va_arg(ap, unsigned int);
92+
event.query_id= (unsigned long long) thd->query_id;
9093
event_class_dispatch(thd, MYSQL_AUDIT_GENERAL_CLASS, &event);
9194
}
9295

Diff for: sql/sql_audit.h

+16-4
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ void mysql_audit_general_log(THD *thd, const char *cmd, uint cmdlen,
8181
const char *user= user_buff;
8282
uint userlen= make_user_name(thd, user_buff);
8383
time_t time= (time_t) thd->start_time.tv_sec;
84+
size_t databaselen;
85+
const char *database;
8486

8587
if (thd)
8688
{
@@ -108,21 +110,25 @@ void mysql_audit_general_log(THD *thd, const char *cmd, uint cmdlen,
108110
external_user.length= thd->security_ctx->get_external_user()->length();
109111
sql_command.str= (char *) sql_statement_names[thd->lex->sql_command].str;
110112
sql_command.length= sql_statement_names[thd->lex->sql_command].length;
113+
database= thd->db;
114+
databaselen= thd->db_length;
111115
}
112116
else
113117
{
114118
ip= empty;
115119
host= empty;
116120
external_user= empty;
117121
sql_command= empty;
122+
database= 0;
123+
databaselen= 0;
118124
}
119125
const CHARSET_INFO *clientcs= thd ? thd->variables.character_set_client
120126
: global_system_variables.character_set_client;
121127

122128
mysql_audit_notify(thd, MYSQL_AUDIT_GENERAL_CLASS, MYSQL_AUDIT_GENERAL_LOG,
123129
error_code, time, user, userlen, cmd, cmdlen, query.str,
124130
query.length, clientcs, rows, sql_command, host,
125-
external_user, ip);
131+
external_user, ip, database, databaselen);
126132
}
127133
#endif
128134
}
@@ -149,14 +155,15 @@ void mysql_audit_general(THD *thd, uint event_subtype,
149155
{
150156
time_t time= my_time(0);
151157
uint msglen= msg ? strlen(msg) : 0;
152-
uint userlen;
153-
const char *user;
158+
uint userlen, databaselen;
159+
const char *user, *database;
154160
char user_buff[MAX_USER_HOST_SIZE];
155161
CSET_STRING query;
156162
MYSQL_LEX_STRING ip, host, external_user, sql_command;
157163
ha_rows rows;
158164
static MYSQL_LEX_STRING empty= { C_STRING_WITH_LEN("") };
159165

166+
160167
if (thd)
161168
{
162169
if (!thd->rewritten_query.length())
@@ -178,6 +185,8 @@ void mysql_audit_general(THD *thd, uint event_subtype,
178185
external_user.length= thd->security_ctx->get_external_user()->length();
179186
sql_command.str= (char *) sql_statement_names[thd->lex->sql_command].str;
180187
sql_command.length= sql_statement_names[thd->lex->sql_command].length;
188+
database= thd->db;
189+
databaselen= thd->db_length;
181190
}
182191
else
183192
{
@@ -188,12 +197,15 @@ void mysql_audit_general(THD *thd, uint event_subtype,
188197
external_user= empty;
189198
sql_command= empty;
190199
rows= 0;
200+
database= 0;
201+
databaselen= 0;
191202
}
192203

193204
mysql_audit_notify(thd, MYSQL_AUDIT_GENERAL_CLASS, event_subtype,
194205
error_code, time, user, userlen, msg, msglen,
195206
query.str(), query.length(), query.charset(), rows,
196-
sql_command, host, external_user, ip);
207+
sql_command, host, external_user, ip, database,
208+
databaselen);
197209
}
198210
#endif
199211
}

0 commit comments

Comments
 (0)