MySQL 8.0.37
Source Code Documentation
gcs_mysql_network_provider.h
Go to the documentation of this file.
1/* Copyright (c) 2016, 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 GCS_MYSQL_NETWORK_PROVIDER_INCLUDED
25#define GCS_MYSQL_NETWORK_PROVIDER_INCLUDED
26
27#include <map>
28
29#include "include/mysql.h"
30
31#include <mysql.h>
32
34
37#include "sql/sql_class.h"
38
39/**
40 * @brief IoC interface to allow abstraction of the retrieval of Security
41 * Credentials
42 *
43 */
45 public:
47
48 /**
49 * @brief Get the user credentials needed to establish MySQL connections.
50 *
51 * This interface is used either as a proxy for @see Replication_thread_api
52 * or to be injected by unit tests.
53 *
54 * @param username username for the mysql connection
55 * @param password password for the mysql connection
56 *
57 * @return the operation status
58 * @retval false OK
59 * @retval true Error, channel not found
60 */
61 virtual bool get_credentials(std::string &username,
62 std::string &password) = 0;
63};
64
65/**
66 * @brief Implementation of Gcs_mysql_network_provider_auth_interface
67 * that retrieves auth data from MySQL.
68 */
71 public:
73 : m_recovery_channel("group_replication_recovery") {}
75
76 /**
77 * @brief @see Gcs_mysql_network_provider_auth_interface#get_credentials
78 */
79 bool get_credentials(std::string &username, std::string &password) override;
80
81 private:
83};
84
85/**
86 * @brief IoC interface to allow abstraction of MySQL Client API
87 *
88 */
90 public:
92
93 /**
94 * @brief Proxy method to mysql_real_connect from the MySQL client API
95 *
96 * @param mysql mysql client connection reference. Must have been
97 * initializaed with mysql_init
98 * @param host hostname to connect
99 * @param user username for the connection
100 * @param passwd password for the connection
101 * @param db database/schema to use
102 * @param port remote port to connect
103 * @param unix_socket unix socket file (if applicable)
104 * @param clientflag client flags
105 * @return MYSQL* a mysql client connection.
106 */
107 virtual MYSQL *mysql_real_connect(MYSQL *mysql, const char *host,
108 const char *user, const char *passwd,
109 const char *db, unsigned int port,
110 const char *unix_socket,
111 unsigned long clientflag) = 0;
112 /**
113 * @brief Proxy method to simple_command from the MySQL client API
114 *
115 * @param mysql an active MySQL connection
116 * @param command the command to send
117 * @param arg command arguments
118 * @param length length of the arguments
119 * @param skip_check skip checking the command
120 *
121 * @return true in case of error. false, otherwise
122 *
123 */
125 const unsigned char *arg, size_t length,
126 bool skip_check) = 0;
127
128 /**
129 * @brief Proxy method to mysql_init from the MySQL Client API
130 *
131 * @param sock the connection to initialize
132 */
133 virtual MYSQL *mysql_init(MYSQL *sock) = 0;
134
135 /**
136 * @brief Proxy method to mysql_close from the MySQL Client API
137 *
138 * @param sock the connection to close
139 */
140 virtual void mysql_close(MYSQL *sock) = 0;
141
142 /**
143 Method to get the network namespace configured for a channel
144
145 @param[out] net_ns The network namespace to extract
146
147 @return the operation status
148 @retval false OK
149 @retval true Error, channel not found
150 */
151 virtual int channel_get_network_namespace(std::string &net_ns) = 0;
152
153 /**
154 Set active network namespace specified by a name.
155
156 @param network_namespace the name of a network namespace to be set active
157
158 @return false on success, true on error
159 @note all opened descriptors used during function run are closed on error
160 */
161 virtual bool set_network_namespace(const std::string &network_namespace) = 0;
162
163 /**
164 Restore original network namespace used to be active before a new network
165 namespace has been set.
166
167 @return false on success, true on failure
168 */
170
171 /**
172 * @brief Proxy method to mysql_free from the MySQL Memory API
173 *
174 * @param ptr the pointer to free
175 */
176 virtual void mysql_free(void *ptr) = 0;
177
178 /**
179 * @brief Proxy method to mysql_options from the MySQL Memory API
180 *
181 * @param mysql connection to set an option
182 * @param option option to set
183 * @param arg value of the option to set
184 *
185 * @return int > 0 in case of error.
186 */
187 virtual int mysql_options(MYSQL *mysql, enum mysql_option option,
188 const void *arg) = 0;
189
190 /**
191 * @brief Proxy method to mysql_ssl_set from the MySQL Memory API
192 *
193 * @param mysql connection to set SSL options
194 * @param key connection key
195 * @param cert connection certificate
196 * @param ca connection CA
197 * @param capath the CA path
198 * @param cipher cipher to use
199 *
200 * @return true in case of error;
201 * @return false otherwise.
202 */
203 virtual bool mysql_ssl_set(MYSQL *mysql, const char *key, const char *cert,
204 const char *ca, const char *capath,
205 const char *cipher) = 0;
206};
207
208/**
209 * @brief Internal implementation of
210 * Gcs_mysql_network_provider_native_interface_impl that serves as a proxy
211 * for MySQL Client API functions.
212 *
213 */
217 public:
219 : m_recovery_channel("group_replication_recovery") {}
221
222 /**
223 * @brief Implementation of @see
224 * Gcs_mysql_network_provider_native_interface#mysql_real_connect
225 */
226 MYSQL *mysql_real_connect(MYSQL *mysql, const char *host, const char *user,
227 const char *passwd, const char *db,
228 unsigned int port, const char *unix_socket,
229 unsigned long clientflag) override;
230 /**
231 * @brief Implementation of @see
232 * Gcs_mysql_network_provider_native_interface#send_command
233 */
235 const unsigned char *arg, size_t length,
236 bool skip_check) override;
237
238 /**
239 * @brief Implementation of @see
240 * Gcs_mysql_network_provider_native_interface#mysql_init
241 */
242 MYSQL *mysql_init(MYSQL *sock) override;
243
244 /**
245 * @brief Implementation of @see
246 * Gcs_mysql_network_provider_native_interface#mysql_close
247 */
248 void mysql_close(MYSQL *sock) override;
249
250 /**
251 * @brief Implementation of @see
252 * Gcs_mysql_network_provider_native_interface#channel_get_network_namespace
253 */
254 int channel_get_network_namespace(std::string &net_ns) override;
255
256 /**
257 * @brief Implementation of @see
258 * Gcs_mysql_network_provider_native_interface#set_network_namespace
259 */
260 bool set_network_namespace(const std::string &network_namespace) override;
261
262 /**
263 * @brief Implementation of @see
264 * Gcs_mysql_network_provider_native_interface#restore_original_network_namespace
265 */
267
268 /**
269 * @brief Implementation of @see
270 * Gcs_mysql_network_provider_native_interface#my_free
271 */
272 void mysql_free(void *ptr) override;
273
274 /**
275 * @brief Implementation of @see
276 * Gcs_mysql_network_provider_native_interface#mysql_options
277 */
278 int mysql_options(MYSQL *mysql, enum mysql_option option,
279 const void *arg) override;
280
281 /**
282 * @brief Implementation of @see
283 * Gcs_mysql_network_provider_native_interface#mysql_ssl_set
284 */
285 bool mysql_ssl_set(MYSQL *mysql, const char *key, const char *cert,
286 const char *ca, const char *capath,
287 const char *cipher) override;
288
289 private:
291};
292
293/**
294 * @brief Implementation of a \see Network_provider that will manage MySQL
295 * protocol connection for GCS/XCOM.
296 */
298 private:
299 /**
300 * @brief A map that holds all open MySQL client connections.
301 *
302 * Since the public interface of Network Managers only knows about File
303 * Descriptors, this is the repository for all MySQL client connections. This
304 * object is required when using mysql_close.
305 *
306 * The map's index is the open connection's file descriptor.
307 */
308 std::map<int, MYSQL *> m_connection_map;
309
310 /**
311 * @brief A map that holds THD's for all open MySQL Server connections.
312 *
313 * We need to maintain this reference in order to call the appropriate closing
314 * mechanisms when destroying an incoming connection.
315 *
316 * The map's index is the open connection's file descriptor.
317 */
318 std::map<int, THD *> m_incoming_connection_map;
319
320 // Locking for the connection map
322
323 // Configuration parameters for this Provider
325
326 /**
327 * External IoC dependencies.
328 * - A provider for authentication parameters
329 * - A provider for all mysql native methods
330 */
333
334 public:
335 /**
336 * @brief Construct a new Gcs_mysql_network_provider
337 *
338 * @param auth_provider A provider interface implementation for authentication
339 * parameters.
340 *
341 * @param native_interface A provider interface for all mysql native methods.
342 */
353
354 m_auth_provider = auth_provider;
355 m_native_interface = native_interface;
358 }
359
360 virtual ~Gcs_mysql_network_provider() override {
361 /*Close all client connections*/
362 if (!m_connection_map.empty()) {
364 [this](const auto &client_connection) {
365 m_native_interface->mysql_close(client_connection.second);
366 m_native_interface->mysql_free(client_connection.second);
367 });
368 m_connection_map.clear();
369 }
371 }
372
373 /**
374 * @brief See @see Network_provider#start
375 */
376 std::pair<bool, int> start() override;
377
378 /**
379 * @brief See @see Network_provider#stop
380 */
381 std::pair<bool, int> stop() override;
382
383 /**
384 * @brief Get the communication stack implemented by this class
385 *
386 * @return a CommunicationStack enum value. In this case -> MYSQL_PROTOCOL
387 */
389 return MYSQL_PROTOCOL;
390 }
391
392 /**
393 * @brief See @see Network_provider#configure
394 */
395 bool configure(const Network_configuration_parameters &params) override;
396
397 /**
398 * @brief See @see Network_provider#configure_secure_connections
399 */
401 const Network_configuration_parameters &params) override;
402
404
406
407 /**
408 * @brief See @see Network_provider#open_connection
409 */
410 std::unique_ptr<Network_connection> open_connection(
411 const std::string &address, const unsigned short port,
412 const Network_security_credentials &security_credentials,
413 int connection_timeout = Network_provider::default_connection_timeout(),
416
417 int close_connection(const Network_connection &connection) override;
418
419 /**
420 * @brief Set the new connection coming form MySQL server
421 *
422 * @param thd the THD to which the connection belongs to.
423 * @param connection the connection data itself.
424 */
425 void set_new_connection(THD *thd, Network_connection *connection);
426};
427
428/**
429 * @brief Utilitarian class for Gcs_mysql_network_provider
430 *
431 */
433 public:
434 // Out of range log value
435 static constexpr int OUT_OF_RANGE_LOG_LEVEL = 255;
436
437 private:
438 /**
439 * @brief Maps between Network Provider generic log level and MySQL error
440 * Log level
441 *
442 * @param net_provider_log_level Network Provider generic log level
443 * @return int MySQL error Log level if there is mapping
444 * OUT_OF_RANGE_LOG_LEVEL, otherwise
445 */
447 network_provider_dynamic_log_level net_provider_log_level) {
448 switch (net_provider_log_level) {
450 return SYSTEM_LEVEL;
451
453 return ERROR_LEVEL;
454
456 return WARNING_LEVEL;
457
459 return INFORMATION_LEVEL;
460
461 default:
462 // If there is no mapping present, we will return an out of range
463 // value in order to feed LogPluginErr.
464 // When provided a non-valid but non-negative number to LogPluginErr
465 // it means that such levels will result in suppression of the
466 // messages being logged
468 }
469 }
470
471 public:
472 /**
473 * @brief Converts from the intended developer fixed level to a dynamic
474 * level provided from the API call, based on runtime conditions.
475 *
476 * A developer might code that wants ERROR level to be written to the
477 * log, but a runtime condition might modify it.
478 *
479 * If log_level is PROVIDED, nothing changes and coded_log_level is
480 * used. If log_level is other than PROVIDED, we will do a mapping
481 * between log_level and MySQL log level.
482 *
483 * For more information about this mechanism @see
484 * network_provider_dynamic_log_level
485 *
486 * @param coded_log_level Developer intended log level
487 * @param log_level External API call log level
488 * @return int the actual runtime log level
489 */
491 int coded_log_level, network_provider_dynamic_log_level log_level) {
493 ? coded_log_level
496 }
497};
498
499#endif /* GCS_MYSQL_NETWORK_PROVIDER_INCLUDED */
Implementation of Gcs_mysql_network_provider_auth_interface that retrieves auth data from MySQL.
Definition: gcs_mysql_network_provider.h:70
Gcs_mysql_network_provider_auth_interface_impl()
Definition: gcs_mysql_network_provider.h:72
Replication_thread_api m_recovery_channel
Definition: gcs_mysql_network_provider.h:82
virtual ~Gcs_mysql_network_provider_auth_interface_impl() override
Definition: gcs_mysql_network_provider.h:74
bool get_credentials(std::string &username, std::string &password) override
Definition: gcs_mysql_network_provider.cc:41
IoC interface to allow abstraction of the retrieval of Security Credentials.
Definition: gcs_mysql_network_provider.h:44
virtual bool get_credentials(std::string &username, std::string &password)=0
Get the user credentials needed to establish MySQL connections.
virtual ~Gcs_mysql_network_provider_auth_interface()
Definition: gcs_mysql_network_provider.h:46
Internal implementation of Gcs_mysql_network_provider_native_interface_impl that serves as a proxy fo...
Definition: gcs_mysql_network_provider.h:216
MYSQL * mysql_init(MYSQL *sock) override
Implementation of.
Definition: gcs_mysql_network_provider.cc:60
bool restore_original_network_namespace() override
Implementation of.
Definition: gcs_mysql_network_provider.cc:122
int channel_get_network_namespace(std::string &net_ns) override
Implementation of.
Definition: gcs_mysql_network_provider.cc:94
bool set_network_namespace(const std::string &network_namespace) override
Implementation of.
Definition: gcs_mysql_network_provider.cc:107
void mysql_free(void *ptr) override
Implementation of.
Definition: gcs_mysql_network_provider.cc:74
virtual ~Gcs_mysql_network_provider_native_interface_impl() override
Definition: gcs_mysql_network_provider.h:220
Gcs_mysql_network_provider_native_interface_impl()
Definition: gcs_mysql_network_provider.h:218
int mysql_options(MYSQL *mysql, enum mysql_option option, const void *arg) override
Implementation of.
Definition: gcs_mysql_network_provider.cc:78
bool mysql_ssl_set(MYSQL *mysql, const char *key, const char *cert, const char *ca, const char *capath, const char *cipher) override
Implementation of.
Definition: gcs_mysql_network_provider.cc:83
Replication_thread_api m_recovery_channel
Definition: gcs_mysql_network_provider.h:290
MYSQL * mysql_real_connect(MYSQL *mysql, const char *host, const char *user, const char *passwd, const char *db, unsigned int port, const char *unix_socket, unsigned long clientflag) override
Implementation of.
Definition: gcs_mysql_network_provider.cc:46
bool send_command(MYSQL *mysql, enum enum_server_command command, const unsigned char *arg, size_t length, bool skip_check) override
Implementation of.
Definition: gcs_mysql_network_provider.cc:54
void mysql_close(MYSQL *sock) override
Implementation of.
Definition: gcs_mysql_network_provider.cc:65
IoC interface to allow abstraction of MySQL Client API.
Definition: gcs_mysql_network_provider.h:89
virtual MYSQL * mysql_init(MYSQL *sock)=0
Proxy method to mysql_init from the MySQL Client API.
virtual int channel_get_network_namespace(std::string &net_ns)=0
Method to get the network namespace configured for a channel.
virtual bool send_command(MYSQL *mysql, enum enum_server_command command, const unsigned char *arg, size_t length, bool skip_check)=0
Proxy method to simple_command from the MySQL client API.
virtual void mysql_close(MYSQL *sock)=0
Proxy method to mysql_close from the MySQL Client API.
virtual int mysql_options(MYSQL *mysql, enum mysql_option option, const void *arg)=0
Proxy method to mysql_options from the MySQL Memory API.
virtual bool restore_original_network_namespace()=0
Restore original network namespace used to be active before a new network namespace has been set.
virtual void mysql_free(void *ptr)=0
Proxy method to mysql_free from the MySQL Memory API.
virtual bool set_network_namespace(const std::string &network_namespace)=0
Set active network namespace specified by a name.
virtual MYSQL * mysql_real_connect(MYSQL *mysql, const char *host, const char *user, const char *passwd, const char *db, unsigned int port, const char *unix_socket, unsigned long clientflag)=0
Proxy method to mysql_real_connect from the MySQL client API.
virtual ~Gcs_mysql_network_provider_native_interface()
Definition: gcs_mysql_network_provider.h:91
virtual bool mysql_ssl_set(MYSQL *mysql, const char *key, const char *cert, const char *ca, const char *capath, const char *cipher)=0
Proxy method to mysql_ssl_set from the MySQL Memory API.
Utilitarian class for Gcs_mysql_network_provider.
Definition: gcs_mysql_network_provider.h:432
static constexpr int OUT_OF_RANGE_LOG_LEVEL
Definition: gcs_mysql_network_provider.h:435
static int log_level_adaptation(int coded_log_level, network_provider_dynamic_log_level log_level)
Converts from the intended developer fixed level to a dynamic level provided from the API call,...
Definition: gcs_mysql_network_provider.h:490
static int from_network_provider_dynamic_log_level_mapping(network_provider_dynamic_log_level net_provider_log_level)
Maps between Network Provider generic log level and MySQL error Log level.
Definition: gcs_mysql_network_provider.h:446
Implementation of a.
Definition: gcs_mysql_network_provider.h:297
std::map< int, THD * > m_incoming_connection_map
A map that holds THD's for all open MySQL Server connections.
Definition: gcs_mysql_network_provider.h:318
Network_configuration_parameters m_config_parameters
Definition: gcs_mysql_network_provider.h:324
enum_transport_protocol get_communication_stack() const override
Get the communication stack implemented by this class.
Definition: gcs_mysql_network_provider.h:388
mysql_mutex_t m_GR_LOCK_connection_map_mutex
Definition: gcs_mysql_network_provider.h:321
int close_connection(const Network_connection &connection) override
Closes an open connection to another XCom endpoint served by the same Network provider.
Definition: gcs_mysql_network_provider.cc:342
Gcs_mysql_network_provider_auth_interface * m_auth_provider
External IoC dependencies.
Definition: gcs_mysql_network_provider.h:331
std::pair< bool, int > stop() override
See.
Definition: gcs_mysql_network_provider.cc:136
bool configure_secure_connections(const Network_configuration_parameters &params) override
See.
Definition: gcs_mysql_network_provider.cc:164
std::pair< bool, int > start() override
See.
Definition: gcs_mysql_network_provider.cc:130
bool finalize_secure_connections_context() override
Definition: gcs_mysql_network_provider.cc:193
bool configure(const Network_configuration_parameters &params) override
See.
Definition: gcs_mysql_network_provider.cc:159
std::unique_ptr< Network_connection > open_connection(const std::string &address, const unsigned short port, const Network_security_credentials &security_credentials, int connection_timeout=Network_provider::default_connection_timeout(), network_provider_dynamic_log_level log_level=network_provider_dynamic_log_level::PROVIDED) override
See.
Definition: gcs_mysql_network_provider.cc:197
virtual ~Gcs_mysql_network_provider() override
Definition: gcs_mysql_network_provider.h:360
void cleanup_secure_connections_context() override
Definition: gcs_mysql_network_provider.cc:187
Gcs_mysql_network_provider_native_interface * m_native_interface
Definition: gcs_mysql_network_provider.h:332
std::map< int, MYSQL * > m_connection_map
A map that holds all open MySQL client connections.
Definition: gcs_mysql_network_provider.h:308
void set_new_connection(THD *thd, Network_connection *connection)
Set the new connection coming form MySQL server.
Definition: gcs_mysql_network_provider.cc:375
Gcs_mysql_network_provider(Gcs_mysql_network_provider_auth_interface *auth_provider, Gcs_mysql_network_provider_native_interface *native_interface)
Construct a new Gcs_mysql_network_provider.
Definition: gcs_mysql_network_provider.h:343
Class that provides Network Namespace services.
Definition: network_provider.h:253
Base class for External Network Providers.
Definition: network_provider.h:306
static constexpr int default_connection_timeout()
Definition: network_provider.h:466
Definition: replication_threads_api.h:37
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:34
#define mysql_mutex_destroy(M)
Definition: mysql_mutex.h:46
#define mysql_mutex_init(K, M, A)
Definition: mysql_mutex.h:41
Fido Client Authentication nullptr
Definition: fido_client_plugin.cc:222
enum_server_command
A list of all MySQL protocol commands.
Definition: my_command.h:48
@ WARNING_LEVEL
Definition: my_loglevel.h:44
@ ERROR_LEVEL
Definition: my_loglevel.h:43
@ INFORMATION_LEVEL
Definition: my_loglevel.h:45
@ SYSTEM_LEVEL
Definition: my_loglevel.h:42
This file defines the client API to MySQL and also the ABI of the dynamically linked libmysqlclient.
mysql_option
Definition: mysql.h:170
static char * password
Definition: mysql_secure_installation.cc:56
char * user
Definition: mysqladmin.cc:60
const char * host
Definition: mysqladmin.cc:59
static MYSQL * sock
Definition: mysqlcheck.cc:56
void for_each(const Shards< COUNT > &shards, Function &&f) noexcept
Iterate over the shards.
Definition: ut0counter.h:323
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:76
Definition: instrumented_condition_variable.h:32
enum_transport_protocol
Enum that describes the available XCom Communication Stacks.
Definition: network_provider.h:45
@ MYSQL_PROTOCOL
Definition: network_provider.h:48
@ SSL_DISABLED
Definition: network_provider.h:57
network_provider_dynamic_log_level
Dynamic log level enum values.
Definition: network_provider.h:103
PSI_mutex_key key_GR_LOCK_connection_map
Definition: plugin_psi.h:132
required string key
Definition: replication_asynchronous_connection_failover.proto:60
required string network_namespace
Definition: replication_asynchronous_connection_failover.proto:34
required uint64 port
Definition: replication_asynchronous_connection_failover.proto:33
Definition: mysql.h:299
Possible configuration parameters.
Definition: network_provider.h:191
struct ssl_parameters ssl_params
Definition: network_provider.h:194
Represents an open connection.
Definition: network_provider.h:201
Security credentials to establish a connection.
Definition: network_provider.h:134
An instrumented mutex structure.
Definition: mysql_mutex_bits.h:50
int ssl_mode
Definition: network_provider.h:172
#define MY_MUTEX_INIT_FAST
Definition: thr_mutex.h:68
command
Definition: version_token.cc:280