MySQL 8.4.0
Source Code Documentation
sql_rewrite.h
Go to the documentation of this file.
1/* Copyright (c) 2011, 2024, 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 designed to work 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 either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef SQL_REWRITE_INCLUDED
25#define SQL_REWRITE_INCLUDED
26
27#include <set>
28#include "my_sqlcommand.h"
29#include "sql/table.h"
30
31/* Forward declarations */
32class THD;
33class LEX_GRANT_AS;
34/**
35 Target types where the rewritten query will be added. Query rewrite might
36 vary based on this type.
37*/
38enum class Consumer_type {
39 TEXTLOG, /* General log, slow query log and audit log */
40 BINLOG, /* Binary logs */
41 STDOUT /* Standard output */
42};
43
44/**
45 An interface to wrap the parameters required by specific Rewriter.
46 Parameters required by specific Rewriter must be added in the concrete
47 implementation.
48 Clients need to wrap the parameters in specific concrete object.
49 The Rewrite_params objects are not expected to change and are passed around as
50 const objects.
51*/
53 protected:
54 virtual ~Rewrite_params() = default;
55};
56
57/**
58 Wrapper object for user related parameters required by:
59 SET PASSWORD|CREATE USER|ALTER USER statements.
60*/
62 public:
63 User_params(std::set<LEX_USER *> *users_set)
64 : Rewrite_params(), users(users_set) {}
65 std::set<LEX_USER *> *users;
66};
67
68/**
69 Wrapper object for parameters required by SHOW CREATE USER statement.
70*/
72 public:
73 Show_user_params(bool hide_password_hash, bool print_identified_with_as_hex,
74 String *param_metadata_str)
77 print_identified_with_as_hex_(print_identified_with_as_hex),
78 metadata_str(param_metadata_str) {}
82};
83
84/**
85 Wrapper object for parameters required for GRANT statement.
86*/
88 public:
89 Grant_params(bool grant_as_specified, LEX_GRANT_AS *grant_as)
91 grant_as_provided(grant_as_specified),
92 grant_as_info(grant_as) {}
95};
96
97/**
98 Provides the default interface to rewrite the SQL statements to
99 to obfuscate passwords.
100 It either sets the thd->rewritten_query with a rewritten query,
101 or clears it if no rewriting took place.
102*/
104 const Rewrite_params *params = nullptr);
105/**
106 Provides the default interface to rewrite the ACL query.
107 If do_ps_instrument, it sets the thd->rewritten_query with
108 a rewritten query.
109*/
111 const Rewrite_params *params = nullptr,
112 bool do_ps_instrument = true);
113
114/**
115 An abstract base class to enable the implementation of various query
116 rewriters. It accepts a THD pointer and the intended target type where the
117 query will to be written. It either sets the thd->rewritten_query with a
118 rewritten query, or clears it if no rewriting took place. Concrete classes
119 must implement the rewrite() method to rewrite the query. Despite concrete
120 classes may accept additional parameters, it is recommended not to create
121 their objects directly.
122*/
124 public:
125 /* Constructors and destructors */
127 virtual ~I_rewriter();
128 /* Prohibit the copy of the object */
129 I_rewriter(const I_rewriter &) = delete;
130 const I_rewriter &operator=(const I_rewriter &) = delete;
131 I_rewriter(const I_rewriter &&) = delete;
132 const I_rewriter &operator=(const I_rewriter &&) = delete;
133 /* Reset the previous consumer type before rewriting the query */
135 /* Return the current consumer type */
137 /* Concrete classes must implement the logic to rewrite query here */
138 virtual bool rewrite(String &rlb) const = 0;
139
140 protected:
141 THD *const m_thd;
143};
144/**
145 Abstract base class to define the skeleton of rewriting the users, yet
146 deferring some steps to the concrete classes. The implementation in specific
147 steps might vary according to SQL or the consumer type.
148*/
149class Rewriter_user : public I_rewriter {
150 protected:
151 Rewriter_user(THD *thd, Consumer_type target_type);
152 /*
153 Provides the skeleton to rewrite the users. The actual user is rewritten
154 through the concrete implementation of private methods.
155 */
156 void rewrite_users(LEX *lex, String *str) const;
157 /* Append the literal value <secret> to the str */
158 void append_literal_secret(String *str) const;
159 /* Append the password hash to the output string */
160 virtual void append_auth_str(LEX_USER *lex, String *str) const;
161 /* Append the authentication plugin name for the user */
162 void append_plugin_name(const LEX_USER *user, String *str) const;
163 /* Append authentication plugin name from LEX_MFA for the user */
164 void append_mfa_plugin_name(const LEX_MFA *user, String *str) const;
165 /* Append the authentication string from LEX_MFA for the user */
166 void append_mfa_auth_str(const LEX_MFA *user, String *str) const;
167
168 /*
169 Rewrites some of the user specific properties which are common to
170 concrete classes.
171 */
172 bool rewrite(String &rlb) const override;
173 /*
174 Abstract method to be implemented by the concrete classes.
175 The implementation methods should add the user authID, plugin info and
176 auth str
177 */
178 virtual void append_user_auth_info(LEX_USER *user, bool comma,
179 String *str) const = 0;
180 /* Append the PASSWORD REUSE OPTIONS clause for users */
181 virtual void rewrite_password_history(const LEX *lex, String *str) const = 0;
182 /* Append the PASSWORD REUSE OPTIONS clause for users */
183 virtual void rewrite_password_reuse(const LEX *lex, String *str) const = 0;
184 /* Append the ATTRIBUTE or COMMENT clause for user */
186 String *str) const = 0;
187 /* Use LEX to reconstruct the ATTRIBUTE or COMMENT clauses */
189 String *str) const;
190
191 private:
192 /* Append the SSL OPTIONS clause for users */
193 void rewrite_ssl_properties(const LEX *lex, String *str) const;
194 /* Append the RESOURCES OPTIONS clause for users */
195 void rewrite_user_resources(const LEX *lex, String *str) const;
196 /* Append the ACCOUNT LOCK OPTIONS clause for users */
197 void rewrite_account_lock(const LEX *lex, String *str) const;
198 /* Append the PASSWORD EXPIRED OPTIONS clause for users */
199 void rewrite_password_expired(const LEX *lex, String *str) const;
200 /* Append the PASSWORD REQUIRE CURRENT clause for users */
202 /* Append FAILED_LOGIN_ATTEMPTS/PASSWORD_LOCK_TIME */
203 void rewrite_account_lock_state(LEX *lex, String *str) const;
204 /* Append the DEFAULT ROLE OPTIONS clause */
205 void rewrite_default_roles(const LEX *lex, String *str) const;
206};
207/** Rewrites the CREATE USER statement. */
210
211 public:
213 bool rewrite(String &rlb) const override;
215 String *str) const override;
216
217 private:
218 void append_user_auth_info(LEX_USER *user, bool comma,
219 String *str) const override;
220 void rewrite_password_history(const LEX *lex, String *str) const override;
221 void rewrite_password_reuse(const LEX *lex, String *str) const override;
222};
223/** Rewrites the ALTER USER statement. */
224class Rewriter_alter_user final : public Rewriter_user {
226
227 public:
229 bool rewrite(String &rlb) const override;
231 String *str) const override;
232
233 private:
234 void append_user_auth_info(LEX_USER *user, bool comma,
235 String *str) const override;
236 void rewrite_password_history(const LEX *lex, String *str) const override;
237 void rewrite_password_reuse(const LEX *lex, String *str) const override;
238};
239/** Rewrites the SHOW CREATE USER statement. */
242
243 public:
245 const Rewrite_params *params);
246 bool rewrite(String &rlb) const override;
248 String *str) const override;
249
250 protected:
251 /* Append the password hash to the output string */
252 void append_auth_str(LEX_USER *lex, String *str) const override;
253
254 private:
255 void append_user_auth_info(LEX_USER *user, bool comma,
256 String *str) const override;
257 void rewrite_password_history(const LEX *lex, String *str) const override;
258 void rewrite_password_reuse(const LEX *lex, String *str) const override;
260};
261
262/** Rewrites the SET statement. */
263class Rewriter_set : public I_rewriter {
264 public:
266 bool rewrite(String &rlb) const override;
267};
268/*
269 Rewrites the SET PASSWORD statement
270*/
273
274 public:
276 const Rewrite_params *params);
277 bool rewrite(String &rlb) const override;
278
279 private:
280 /* Name of the user whose password has to be changed */
281 std::set<LEX_USER *> *m_users = nullptr;
282};
283
284/** Rewrites the GRANT statement. */
285class Rewriter_grant final : public I_rewriter {
286 public:
287 Rewriter_grant(THD *thd, Consumer_type type, const Rewrite_params *params);
288 bool rewrite(String &rlb) const override;
289
290 private:
291 /* GRANT AS information */
292 const Grant_params *grant_params = nullptr;
293};
294
295/** Rewrites the CHANGE REPLICATION SOURCE statement. */
297 public:
299 bool rewrite(String &rlb) const override;
300};
301
302/** Rewrites the START REPLICA statement. */
303class Rewriter_replica_start final : public I_rewriter {
304 public:
306 bool rewrite(String &rlb) const override;
307};
308/** Base class for SERVER OPTIONS related statement */
310 public:
312
313 protected:
314 // Append the SERVER OPTIONS clause
315 void mysql_rewrite_server_options(const LEX *lex, String *str) const;
316};
317/** Rewrites the CREATE SERVER statement. */
320
321 public:
323 bool rewrite(String &rlb) const override;
324};
325/** Rewrites the ALTER SERVER statement. */
328
329 public:
331 bool rewrite(String &rlb) const override;
332};
333
334/** Rewrites the PREPARE statement.*/
335class Rewriter_prepare final : public I_rewriter {
336 public:
338 bool rewrite(String &rlb) const override;
339};
340
341/** Rewrites CLONE statement.*/
342class Rewriter_clone final : public I_rewriter {
343 public:
345 bool rewrite(String &rlb) const override;
346};
347
348/** Rewrites the START GROUP_REPLICATION command.*/
350 public:
352 bool rewrite(String &rlb) const override;
353};
354
355#endif /* SQL_REWRITE_INCLUDED */
Wrapper object for parameters required for GRANT statement.
Definition: sql_rewrite.h:87
Grant_params(bool grant_as_specified, LEX_GRANT_AS *grant_as)
Definition: sql_rewrite.h:89
bool grant_as_provided
Definition: sql_rewrite.h:93
LEX_GRANT_AS * grant_as_info
Definition: sql_rewrite.h:94
An abstract base class to enable the implementation of various query rewriters.
Definition: sql_rewrite.h:123
Consumer_type consumer_type()
Return the current consumer type set in the object.
Definition: sql_rewrite.cc:426
virtual ~I_rewriter()
const I_rewriter & operator=(const I_rewriter &&)=delete
I_rewriter(const I_rewriter &)=delete
virtual bool rewrite(String &rlb) const =0
Consumer_type m_consumer_type
Definition: sql_rewrite.h:142
I_rewriter(const I_rewriter &&)=delete
void set_consumer_type(Consumer_type type)
Reset the previous consumer type.
Definition: sql_rewrite.cc:418
const I_rewriter & operator=(const I_rewriter &)=delete
THD *const m_thd
Definition: sql_rewrite.h:141
I_rewriter(THD *thd, Consumer_type type)
Definition: sql_rewrite.cc:407
Definition: sql_lex.h:3738
An interface to wrap the parameters required by specific Rewriter.
Definition: sql_rewrite.h:52
virtual ~Rewrite_params()=default
Rewrites the ALTER SERVER statement.
Definition: sql_rewrite.h:326
Rewriter_alter_server(THD *thd, Consumer_type type)
Definition: sql_rewrite.cc:1717
bool rewrite(String &rlb) const override
Rewrite the query for the ALTER SERVER statement.
Definition: sql_rewrite.cc:1728
Rewrites the ALTER USER statement.
Definition: sql_rewrite.h:224
void rewrite_password_history(const LEX *lex, String *str) const override
Append the PASSWORD HISTORY clause for users iff it is specified.
Definition: sql_rewrite.cc:1019
void append_user_auth_info(LEX_USER *user, bool comma, String *str) const override
Append the authID, plugin and auth str of the user to output string :
Definition: sql_rewrite.cc:942
void rewrite_user_application_user_metadata(const LEX *lex, String *str) const override
Default implementation of the the rewriter for user applicatiton user metadata.
Definition: sql_rewrite.cc:495
void rewrite_password_reuse(const LEX *lex, String *str) const override
Append the PASSWORD REUSE clause for users iff it is specified.
Definition: sql_rewrite.cc:1031
Rewriter_alter_user(THD *thd, Consumer_type type=Consumer_type::TEXTLOG)
Definition: sql_rewrite.cc:912
bool rewrite(String &rlb) const override
Rewrite the query for the ALTER USER statement.
Definition: sql_rewrite.cc:922
Rewrites the CHANGE REPLICATION SOURCE statement.
Definition: sql_rewrite.h:296
bool rewrite(String &rlb) const override
Rewrite the query for the CHANGE REPLICATION SOURCE statement.
Definition: sql_rewrite.cc:1473
Rewriter_change_replication_source(THD *thd, Consumer_type)
Definition: sql_rewrite.cc:1461
Rewrites CLONE statement.
Definition: sql_rewrite.h:342
bool rewrite(String &rlb) const override
Rewrite the query for the CLONE statement to hide password.
Definition: sql_rewrite.cc:1775
Rewriter_clone(THD *thd, Consumer_type type)
Definition: sql_rewrite.cc:1764
Rewrites the CREATE SERVER statement.
Definition: sql_rewrite.h:318
Rewriter_create_server(THD *thd, Consumer_type type)
Definition: sql_rewrite.cc:1670
bool rewrite(String &rlb) const override
Rewrite the query for the CREATE SERVER statement.
Definition: sql_rewrite.cc:1699
Rewrites the CREATE USER statement.
Definition: sql_rewrite.h:208
void append_user_auth_info(LEX_USER *user, bool comma, String *str) const override
Append the authID, plugin and auth str of the user to output string :
Definition: sql_rewrite.cc:835
void rewrite_password_history(const LEX *lex, String *str) const override
Append the PASSWORD HISTORY clause for users iff it is specified.
Definition: sql_rewrite.cc:894
void rewrite_password_reuse(const LEX *lex, String *str) const override
Append the PASSWORD REUSE clause for users iff it is specified.
Definition: sql_rewrite.cc:906
void rewrite_user_application_user_metadata(const LEX *lex, String *str) const override
Default implementation of the the rewriter for user applicatiton user metadata.
Definition: sql_rewrite.cc:484
Rewriter_create_user(THD *thd, Consumer_type type)
Definition: sql_rewrite.cc:804
bool rewrite(String &rlb) const override
Rewrite the query for the CREATE USER statement.
Definition: sql_rewrite.cc:814
Rewrites the GRANT statement.
Definition: sql_rewrite.h:285
bool rewrite(String &rlb) const override
Rewrite the query for the GRANT statement.
Definition: sql_rewrite.cc:1291
Rewriter_grant(THD *thd, Consumer_type type, const Rewrite_params *params)
Definition: sql_rewrite.cc:1278
const Grant_params * grant_params
Definition: sql_rewrite.h:292
Rewrites the PREPARE statement.
Definition: sql_rewrite.h:335
Rewriter_prepare(THD *thd, Consumer_type type)
Definition: sql_rewrite.cc:1742
bool rewrite(String &rlb) const override
Rewrite the query for the PREPARE statement.
Definition: sql_rewrite.cc:1753
Rewrites the START REPLICA statement.
Definition: sql_rewrite.h:303
bool rewrite(String &rlb) const override
Rewrite the query for the SLAVE REPLICA statement.
Definition: sql_rewrite.cc:1606
Rewriter_replica_start(THD *thd, Consumer_type type)
Definition: sql_rewrite.cc:1596
Base class for SERVER OPTIONS related statement.
Definition: sql_rewrite.h:309
void mysql_rewrite_server_options(const LEX *lex, String *str) const
Append the SERVER OPTIONS clause.
Definition: sql_rewrite.cc:1678
Rewriter_server_option(THD *thd, Consumer_type type)
Definition: sql_rewrite.cc:1668
Definition: sql_rewrite.h:271
bool rewrite(String &rlb) const override
Rewrite the query for the SET PASSWORD statement.
Definition: sql_rewrite.cc:1229
std::set< LEX_USER * > * m_users
Definition: sql_rewrite.h:281
Rewriter_set_password(THD *thd, Consumer_type type, const Rewrite_params *params)
Definition: sql_rewrite.cc:1214
Rewrites the SET statement.
Definition: sql_rewrite.h:263
Rewriter_set(THD *thd, Consumer_type type)
Definition: sql_rewrite.cc:1184
bool rewrite(String &rlb) const override
Rewrite the query for the SET statement.
Definition: sql_rewrite.cc:1195
Rewrites the SHOW CREATE USER statement.
Definition: sql_rewrite.h:240
void rewrite_password_history(const LEX *lex, String *str) const override
Append the PASSWORD HISTORY clause for users.
Definition: sql_rewrite.cc:1119
void rewrite_password_reuse(const LEX *lex, String *str) const override
Append the PASSWORD REUSE clause for users.
Definition: sql_rewrite.cc:1129
Rewriter_show_create_user(THD *thd, Consumer_type type, const Rewrite_params *params)
Definition: sql_rewrite.cc:1037
void append_user_auth_info(LEX_USER *user, bool comma, String *str) const override
Append the authID, plugin name and suth str user to output string.
Definition: sql_rewrite.cc:1140
void rewrite_user_application_user_metadata(const LEX *lex, String *str) const override
Overrides implementation of the the rewriter for user application user metadata.
Definition: sql_rewrite.cc:1068
void append_auth_str(LEX_USER *lex, String *str) const override
A special rewriter override to make SHOW CREATE USER convert the string to hex if print_identified_wi...
Definition: sql_rewrite.cc:1087
bool rewrite(String &rlb) const override
Rewrite the query for the SHOW CREATE USER statement.
Definition: sql_rewrite.cc:1055
const Show_user_params * show_params_
Definition: sql_rewrite.h:259
Rewrites the START GROUP_REPLICATION command.
Definition: sql_rewrite.h:349
Rewriter_start_group_replication(THD *thd, Consumer_type type)
Definition: sql_rewrite.cc:1780
bool rewrite(String &rlb) const override
Rewrite the query for the START GROUP_REPLICATION command.
Definition: sql_rewrite.cc:1791
Abstract base class to define the skeleton of rewriting the users, yet deferring some steps to the co...
Definition: sql_rewrite.h:149
void append_mfa_auth_str(const LEX_MFA *user, String *str) const
Append the authentication string from LEX_MFA for the user.
Definition: sql_rewrite.cc:720
void rewrite_user_resources(const LEX *lex, String *str) const
Append the user resource clauses for users.
Definition: sql_rewrite.cc:569
void append_literal_secret(String *str) const
Append the literal <secret> in place of password to the output string.
Definition: sql_rewrite.cc:505
void rewrite_password_require_current(LEX *lex, String *str) const
Append the PASSWORD REQUIRE CURRENT clause for users.
Definition: sql_rewrite.cc:632
Rewriter_user(THD *thd, Consumer_type target_type)
Definition: sql_rewrite.cc:428
void rewrite_password_expired(const LEX *lex, String *str) const
Append the PASSWORD EXPIRE clause for users iff it is specified.
Definition: sql_rewrite.cc:611
virtual void rewrite_user_application_user_metadata(const LEX *lex, String *str) const =0
void rewrite_users(LEX *lex, String *str) const
Fetch the users from user_list in LEX struct and append them to the String.
Definition: sql_rewrite.cc:771
void rewrite_in_memory_user_application_user_metadata(const LEX *user, String *str) const
Use the LEX for reconstructing the ATTRIBUTE or COMMENT clause.
Definition: sql_rewrite.cc:460
void rewrite_account_lock_state(LEX *lex, String *str) const
Append the account lock state.
Definition: sql_rewrite.cc:661
virtual void rewrite_password_history(const LEX *lex, String *str) const =0
The default implementation is to append the PASSWORD HISTORY clause iff it is specified.
Definition: sql_rewrite.cc:739
virtual void append_user_auth_info(LEX_USER *user, bool comma, String *str) const =0
void rewrite_ssl_properties(const LEX *lex, String *str) const
Append the SSL clause for users iff it is specified.
Definition: sql_rewrite.cc:527
void append_mfa_plugin_name(const LEX_MFA *user, String *str) const
Append the authentication plugin name from LEX_MFA for the user.
Definition: sql_rewrite.cc:704
virtual void append_auth_str(LEX_USER *lex, String *str) const
Append the password hash to the output string.
Definition: sql_rewrite.cc:515
bool rewrite(String &rlb) const override
Appends the essential clauses for SHOW CREATE|CREATE|ALTER USER statements in the buffer rlb.
Definition: sql_rewrite.cc:439
void rewrite_account_lock(const LEX *lex, String *str) const
Append the ACCOUNT LOCK clause for users iff it is specified.
Definition: sql_rewrite.cc:596
void append_plugin_name(const LEX_USER *user, String *str) const
Append the authentication plugin name for the user.
Definition: sql_rewrite.cc:681
virtual void rewrite_password_reuse(const LEX *lex, String *str) const =0
The default implementation is to append the PASSWORD REUSE clause iff it is specified.
Definition: sql_rewrite.cc:755
void rewrite_default_roles(const LEX *lex, String *str) const
Append the DEFAULT ROLE clause for users iff it is specified.
Definition: sql_rewrite.cc:789
Wrapper object for parameters required by SHOW CREATE USER statement.
Definition: sql_rewrite.h:71
String * metadata_str
Definition: sql_rewrite.h:81
bool hide_password_hash
Definition: sql_rewrite.h:79
Show_user_params(bool hide_password_hash, bool print_identified_with_as_hex, String *param_metadata_str)
Definition: sql_rewrite.h:73
bool print_identified_with_as_hex_
Definition: sql_rewrite.h:80
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:167
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
Wrapper object for user related parameters required by: SET PASSWORD|CREATE USER|ALTER USER statement...
Definition: sql_rewrite.h:61
User_params(std::set< LEX_USER * > *users_set)
Definition: sql_rewrite.h:63
std::set< LEX_USER * > * users
Definition: sql_rewrite.h:65
char * user
Definition: mysqladmin.cc:66
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1073
required string type
Definition: replication_group_member_actions.proto:34
void mysql_rewrite_acl_query(THD *thd, String &rlb, Consumer_type type, const Rewrite_params *params=nullptr, bool do_ps_instrument=true)
Provides the default interface to rewrite the ACL query.
Definition: sql_rewrite.cc:391
void mysql_rewrite_query(THD *thd, Consumer_type type=Consumer_type::TEXTLOG, const Rewrite_params *params=nullptr)
Provides the default interface to rewrite the SQL statements to to obfuscate passwords.
Definition: sql_rewrite.cc:354
Consumer_type
Target types where the rewritten query will be added.
Definition: sql_rewrite.h:38
Definition: table.h:2621
Definition: table.h:2730
The LEX object currently serves three different purposes:
Definition: sql_lex.h:3822