Skip to content

Commit 8ad9a09

Browse files
committed
WL#14267 Audit filters: log either the digest or the raw query text
Extension that allows to replace SQL query plain text in the audit log file with the query digest text: SELECT ? = ? INSERT INTO `test`.`test_table` VALUES (?) Both XML and JSON formats are supported. Query digest text can be written, only when the JSON filtering is used. Basing configuration is done using the following JSON syntax: print: { field: { name: general_query.str, print: false, replace : { function: { name: query_digest } } } } RB: 25947 Reviewed-by: Georgi 'Joro' Kodinov <[email protected]> Reviewed-by: Ivan Svaljek <[email protected]>
1 parent 695f90b commit 8ad9a09

File tree

6 files changed

+263
-0
lines changed

6 files changed

+263
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* Copyright (c) 2021, Oracle and/or its affiliates.
2+
3+
This program is free software; you can redistribute it and/or modify
4+
it under the terms of the GNU General Public License, version 2.0,
5+
as published by the Free Software Foundation.
6+
7+
This program is also distributed with certain software (including
8+
but not limited to OpenSSL) that is licensed under separate terms,
9+
as designated in a particular file or component or in included license
10+
documentation. The authors of MySQL hereby grant you an additional
11+
permission to link the program and your derivative works with the
12+
separately licensed software that they have included with MySQL.
13+
14+
This program is distributed in the hope that it will be useful,
15+
but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
GNU General Public License, version 2.0, for more details.
18+
19+
You should have received a copy of the GNU General Public License
20+
along with this program; if not, write to the Free Software
21+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22+
23+
#ifndef THD_BITS_H
24+
#define THD_BITS_H
25+
26+
#ifdef __cplusplus
27+
class THD;
28+
#define MYSQL_THD THD *
29+
#else
30+
#define MYSQL_THD void *
31+
#endif
32+
33+
#endif /* THD_BITS_H */
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/* Copyright (c) 2021, Oracle and/or its affiliates.
2+
3+
This program is free software; you can redistribute it and/or modify
4+
it under the terms of the GNU General Public License, version 2.0,
5+
as published by the Free Software Foundation.
6+
7+
This program is also distributed with certain software (including
8+
but not limited to OpenSSL) that is licensed under separate terms,
9+
as designated in a particular file or component or in included license
10+
documentation. The authors of MySQL hereby grant you an additional
11+
permission to link the program and your derivative works with the
12+
separately licensed software that they have included with MySQL.
13+
14+
This program is distributed in the hope that it will be useful,
15+
but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
GNU General Public License, version 2.0, for more details.
18+
19+
You should have received a copy of the GNU General Public License
20+
along with this program; if not, write to the Free Software
21+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22+
23+
#ifndef MYSQL_THD_ATTRIBUTES_H
24+
#define MYSQL_THD_ATTRIBUTES_H
25+
26+
#include <mysql/components/service.h>
27+
#include <mysql/components/services/bits/thd.h>
28+
29+
/**
30+
@ingroup group_components_services_inventory
31+
32+
THD Attributes service allows to obtain data associated with the THD
33+
object, which keeps various attributes of the user session.
34+
35+
Currently, following attributes are supported:
36+
37+
- Query Digest Text
38+
39+
@section Initialization
40+
41+
The service can be instantiated using the registry service with the
42+
"mysql_thd_attributes" name.
43+
44+
@code
45+
SERVICE_TYPE(registry) *registry = mysql_plugin_registry_acquire();
46+
my_service<SERVICE_TYPE(mysql_thd_attributes)>
47+
svc("mysql_thd_attributes", registry);
48+
if (svc.is_valid()) {
49+
// The service is ready to be used
50+
}
51+
@endcode
52+
53+
@section Query Digest Text
54+
55+
Query Digest represents converted SQL statement to normalized form. The code
56+
below demonstrates how query digest can be obtained from the service.
57+
58+
@code
59+
my_h_string str;
60+
61+
mysql_thd_attributes->get(m_thd, "query_digest",
62+
reinterpret_cast<void *>(&str));
63+
@endcode
64+
65+
The buffer can be fetched using the code below:
66+
67+
@code
68+
char buf[1024]; // buffer must be big enough to store the digest
69+
70+
mysql_string_converter->convert_to_buffer(str, buf, sizeof(buf), "utf8");
71+
@endcode
72+
73+
After the string content has been copied into another buffer, it must be
74+
destroyed:
75+
76+
@code
77+
mysql_string_factory->destroy(str);
78+
@endcode
79+
*/
80+
BEGIN_SERVICE_DEFINITION(mysql_thd_attributes)
81+
82+
/**
83+
Get THD attribute.
84+
85+
Currently, following attributes are supported:
86+
87+
- Query Digest Text ("query_digest" of the returned my_h_string type).
88+
89+
@param thd Session THD object.
90+
@param name Name of the attribute to be set.
91+
@param[out] inout_pvalue Iterator pointer.
92+
93+
@return
94+
@retval FALSE Succeeded.
95+
@retval TRUE Failed.
96+
*/
97+
DECLARE_BOOL_METHOD(get, (MYSQL_THD thd, const char *name, void *inout_pvalue));
98+
99+
/**
100+
Set THD attribute.
101+
102+
Currently the implementation does not support setting of any attribute.
103+
104+
@param thd Session THD object.
105+
@param name Name of the attribute to be set.
106+
@param[out] inout_pvalue Iterator pointer.
107+
108+
@return The function always fail and return TRUE value.
109+
*/
110+
DECLARE_BOOL_METHOD(set, (MYSQL_THD thd, const char *name, void *inout_pvalue));
111+
112+
END_SERVICE_DEFINITION(mysql_thd_attributes)
113+
114+
#endif /* MYSQL_THD_ATTRIBUTES_H */

sql/server_component/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ SET(MYSQL_SERVER_COMPONENT_SOURCES
5656
mysql_query_attributes_imp.cc
5757
mysql_server_keyring_lockable_imp.cc
5858
mysql_system_variable_update_imp.cc
59+
mysql_thd_attributes_imp.cc
5960
)
6061

6162
# This static library is used to build mysqld binary and in some unit test cases
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* Copyright (c) 2021, Oracle and/or its affiliates.
2+
3+
This program is free software; you can redistribute it and/or modify
4+
it under the terms of the GNU General Public License, version 2.0,
5+
as published by the Free Software Foundation.
6+
7+
This program is also distributed with certain software (including
8+
but not limited to OpenSSL) that is licensed under separate terms,
9+
as designated in a particular file or component or in included license
10+
documentation. The authors of MySQL hereby grant you an additional
11+
permission to link the program and your derivative works with the
12+
separately licensed software that they have included with MySQL.
13+
14+
This program is distributed in the hope that it will be useful,
15+
but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
GNU General Public License, version 2.0, for more details.
18+
19+
You should have received a copy of the GNU General Public License
20+
along with this program; if not, write to the Free Software
21+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22+
23+
#include "mysql_thd_attributes_imp.h"
24+
25+
#include <mysql/components/minimal_chassis.h>
26+
#include <mysql/components/services/mysql_string.h>
27+
#include "sql/current_thd.h"
28+
#include "sql/sql_class.h"
29+
#include "sql/sql_digest.h"
30+
31+
DEFINE_BOOL_METHOD(mysql_thd_attributes_imp::get,
32+
(MYSQL_THD thd, const char *name, void *inout_pvalue)) {
33+
try {
34+
if (inout_pvalue) {
35+
if (!strcmp(name, "query_digest")) {
36+
THD *t = static_cast<THD *>(thd);
37+
38+
if (t == nullptr || t->m_digest == nullptr) return true;
39+
40+
String *res = new String[1];
41+
42+
compute_digest_text(&t->m_digest->m_digest_storage, res);
43+
44+
/* compute_digest_text returns string as to utf8. */
45+
res->set_charset(&my_charset_utf8_bin);
46+
47+
*((my_h_string *)inout_pvalue) = (my_h_string)res;
48+
} else
49+
return true; /* invalid option */
50+
}
51+
return false;
52+
} catch (...) {
53+
mysql_components_handle_std_exception(__func__);
54+
}
55+
return true;
56+
}
57+
58+
DEFINE_BOOL_METHOD(mysql_thd_attributes_imp::set,
59+
(MYSQL_THD thd MY_ATTRIBUTE((unused)),
60+
const char *name MY_ATTRIBUTE((unused)),
61+
void *inout_pvalue MY_ATTRIBUTE((unused)))) {
62+
return true;
63+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* Copyright (c) 2021, Oracle and/or its affiliates.
2+
3+
This program is free software; you can redistribute it and/or modify
4+
it under the terms of the GNU General Public License, version 2.0,
5+
as published by the Free Software Foundation.
6+
7+
This program is also distributed with certain software (including
8+
but not limited to OpenSSL) that is licensed under separate terms,
9+
as designated in a particular file or component or in included license
10+
documentation. The authors of MySQL hereby grant you an additional
11+
permission to link the program and your derivative works with the
12+
separately licensed software that they have included with MySQL.
13+
14+
This program is distributed in the hope that it will be useful,
15+
but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
GNU General Public License, version 2.0, for more details.
18+
19+
You should have received a copy of the GNU General Public License
20+
along with this program; if not, write to the Free Software
21+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22+
23+
#ifndef MYSQL_THD_ATTRIBUTES_IMP_H
24+
#define MYSQL_THD_ATTRIBUTES_IMP_H
25+
26+
#include <mysql/components/component_implementation.h>
27+
#include <mysql/components/services/mysql_thd_attributes.h>
28+
29+
/**
30+
An implementation of mysql_thd_attributes service methods
31+
*/
32+
class mysql_thd_attributes_imp {
33+
public:
34+
/**
35+
Reads a named THD attribute and retuns its value.
36+
*/
37+
static DEFINE_BOOL_METHOD(get, (MYSQL_THD thd, const char *name,
38+
void *inout_pvalue));
39+
40+
/**
41+
Empty implementation.
42+
*/
43+
static DEFINE_BOOL_METHOD(set, (MYSQL_THD thd, const char *name,
44+
void *inout_pvalue));
45+
};
46+
#endif /* MYSQL_THD_ATTRIBUTES_IMP_H */

sql/server_component/server_component.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
7070
#include "mysql_server_runnable_imp.h"
7171
#include "mysql_string_service_imp.h"
7272
#include "mysql_system_variable_update_imp.h"
73+
#include "mysql_thd_attributes_imp.h"
7374
#include "mysqld_error.h"
7475
#include "persistent_dynamic_loader_imp.h"
7576
#include "security_context_imp.h"
@@ -407,6 +408,10 @@ Keyring_writer_service_impl::store,
407408
BEGIN_SERVICE_IMPLEMENTATION(mysql_server, mysql_system_variable_update_string)
408409
mysql_system_variable_update_string_imp::set END_SERVICE_IMPLEMENTATION();
409410

411+
BEGIN_SERVICE_IMPLEMENTATION(mysql_server, mysql_thd_attributes)
412+
mysql_thd_attributes_imp::get,
413+
mysql_thd_attributes_imp::set END_SERVICE_IMPLEMENTATION();
414+
410415
BEGIN_COMPONENT_PROVIDES(mysql_server)
411416
PROVIDES_SERVICE(mysql_server_path_filter, dynamic_loader_scheme_file),
412417
PROVIDES_SERVICE(mysql_server, persistent_dynamic_loader),
@@ -531,6 +536,7 @@ PROVIDES_SERVICE(mysql_server_path_filter, dynamic_loader_scheme_file),
531536
PROVIDES_SERVICE(mysql_server, field_integer_access_v1),
532537
PROVIDES_SERVICE(mysql_server, field_varchar_access_v1),
533538
PROVIDES_SERVICE(mysql_server, field_any_access_v1),
539+
PROVIDES_SERVICE(mysql_server, mysql_thd_attributes),
534540
END_COMPONENT_PROVIDES();
535541

536542
static BEGIN_COMPONENT_REQUIRES(mysql_server) END_COMPONENT_REQUIRES();

0 commit comments

Comments
 (0)