MySQL 9.0.0
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 Get the user credentials needed to establish MySQL connections.
78 *
79 * @see Gcs_mysql_network_provider_auth_interface#get_credentials
80 */
81 bool get_credentials(std::string &username, std::string &password) override;
82
83 private:
85};
86
87/**
88 * @brief IoC interface to allow abstraction of MySQL Client API
89 *
90 */
92 public:
94
95 /**
96 * @brief Proxy method to mysql_real_connect from the MySQL client API
97 *
98 * @param mysql mysql client connection reference. Must have been
99 * initializaed with mysql_init
100 * @param host hostname to connect
101 * @param user username for the connection
102 * @param passwd password for the connection
103 * @param db database/schema to use
104 * @param port remote port to connect
105 * @param unix_socket unix socket file (if applicable)
106 * @param clientflag client flags
107 * @return MYSQL* a mysql client connection.
108 */
109 virtual MYSQL *mysql_real_connect(MYSQL *mysql, const char *host,
110 const char *user, const char *passwd,
111 const char *db, unsigned int port,
112 const char *unix_socket,
113 unsigned long clientflag) = 0;
114 /**
115 * @brief Proxy method to simple_command from the MySQL client API
116 *
117 * @param mysql an active MySQL connection
118 * @param command the command to send
119 * @param arg command arguments
120 * @param length length of the arguments
121 * @param skip_check skip checking the command
122 *
123 * @return true in case of error. false, otherwise
124 *
125 */
127 const unsigned char *arg, size_t length,
128 bool skip_check) = 0;
129
130 /**
131 * @brief Proxy method to mysql_init from the MySQL Client API
132 *
133 * @param sock the connection to initialize
134 */
135 virtual MYSQL *mysql_init(MYSQL *sock) = 0;
136
137 /**
138 * @brief Proxy method to mysql_close from the MySQL Client API
139 *
140 * @param sock the connection to close
141 */
142 virtual void mysql_close(MYSQL *sock) = 0;
143
144 /**
145 Method to get the network namespace configured for a channel
146
147 @param[out] net_ns The network namespace to extract
148
149 @return the operation status
150 @retval false OK
151 @retval true Error, channel not found
152 */
153 virtual int channel_get_network_namespace(std::string &net_ns) = 0;
154
155 /**
156 Set active network namespace specified by a name.
157
158 @param network_namespace the name of a network namespace to be set active
159
160 @return false on success, true on error
161 @note all opened descriptors used during function run are closed on error
162 */
163 virtual bool set_network_namespace(const std::string &network_namespace) = 0;
164
165 /**
166 Restore original network namespace used to be active before a new network
167 namespace has been set.
168
169 @return false on success, true on failure
170 */
172
173 /**
174 * @brief Proxy method to mysql_free from the MySQL Memory API
175 *
176 * @param ptr the pointer to free
177 */
178 virtual void mysql_free(void *ptr) = 0;
179
180 /**
181 * @brief Proxy method to mysql_options from the MySQL Memory API
182 *
183 * @param mysql connection to set an option
184 * @param option option to set
185 * @param arg value of the option to set
186 *
187 * @return int > 0 in case of error.
188 */
189 virtual int mysql_options(MYSQL *mysql, enum mysql_option option,
190 const void *arg) = 0;
191
192 /**
193 * @brief Proxy method to mysql_ssl_set from the MySQL Memory API
194 *
195 * @param mysql connection to set SSL options
196 * @param key connection key
197 * @param cert connection certificate
198 * @param ca connection CA
199 * @param capath the CA path
200 * @param cipher cipher to use
201 *
202 * @return true in case of error;
203 * @return false otherwise.
204 */
205 virtual bool mysql_ssl_set(MYSQL *mysql, const char *key, const char *cert,
206 const char *ca, const char *capath,
207 const char *cipher) = 0;
208};
209
210/**
211 * @brief Internal implementation of
212 * Gcs_mysql_network_provider_native_interface_impl that serves as a proxy
213 * for MySQL Client API functions.
214 *
215 */
219 public:
221 : m_recovery_channel("group_replication_recovery") {}
223
224 /**
225 * @brief Implementation of @see
226 * Gcs_mysql_network_provider_native_interface#mysql_real_connect
227 */
228 MYSQL *mysql_real_connect(MYSQL *mysql, const char *host, const char *user,
229 const char *passwd, const char *db,
230 unsigned int port, const char *unix_socket,
231 unsigned long clientflag) override;
232 /**
233 * @brief Implementation of @see
234 * Gcs_mysql_network_provider_native_interface#send_command
235 */
237 const unsigned char *arg, size_t length,
238 bool skip_check) override;
239
240 /**
241 * @brief Implementation of @see
242 * Gcs_mysql_network_provider_native_interface#mysql_init
243 */
244 MYSQL *mysql_init(MYSQL *sock) override;
245
246 /**
247 * @brief Implementation of @see
248 * Gcs_mysql_network_provider_native_interface#mysql_close
249 */
250 void mysql_close(MYSQL *sock) override;
251
252 /**
253 * @brief Implementation of @see
254 * Gcs_mysql_network_provider_native_interface#channel_get_network_namespace
255 */
256 int channel_get_network_namespace(std::string &net_ns) override;
257
258 /**
259 * @brief Implementation of @see
260 * Gcs_mysql_network_provider_native_interface#set_network_namespace
261 */
262 bool set_network_namespace(const std::string &network_namespace) override;
263
264 /**
265 * @brief Implementation of @see
266 * Gcs_mysql_network_provider_native_interface#restore_original_network_namespace
267 */
269
270 /**
271 * @brief Implementation of @see
272 * Gcs_mysql_network_provider_native_interface#my_free
273 */
274 void mysql_free(void *ptr) override;
275
276 /**
277 * @brief Implementation of @see
278 * Gcs_mysql_network_provider_native_interface#mysql_options
279 */
280 int mysql_options(MYSQL *mysql, enum mysql_option option,
281 const void *arg) override;
282
283 /**
284 * @brief Implementation of @see
285 * Gcs_mysql_network_provider_native_interface#mysql_ssl_set
286 */
287 bool mysql_ssl_set(MYSQL *mysql, const char *key, const char *cert,
288 const char *ca, const char *capath,
289 const char *cipher) override;
290
291 private:
293};
294
295/**
296 * @brief Implementation of a \see Network_provider that will manage MySQL
297 * protocol connection for GCS/XCOM.
298 */
300 private:
301 /**
302 * @brief A map that holds all open MySQL client connections.
303 *
304 * Since the public interface of Network Managers only knows about File
305 * Descriptors, this is the repository for all MySQL client connections. This
306 * object is required when using mysql_close.
307 *
308 * The map's index is the open connection's file descriptor.
309 */
310 std::map<int, MYSQL *> m_connection_map;
311
312 /**
313 * @brief A map that holds THD's for all open MySQL Server connections.
314 *
315 * We need to maintain this reference in order to call the appropriate closing
316 * mechanisms when destroying an incoming connection.
317 *
318 * The map's index is the open connection's file descriptor.
319 */
320 std::map<int, THD *> m_incoming_connection_map;
321
322 // Locking for the connection map
324
325 // Configuration parameters for this Provider
327
328 /**
329 * External IoC dependencies.
330 * - A provider for authentication parameters
331 * - A provider for all mysql native methods
332 */
335
336 public:
337 /**
338 * @brief Construct a new Gcs_mysql_network_provider
339 *
340 * @param auth_provider A provider interface implementation for authentication
341 * parameters.
342 *
343 * @param native_interface A provider interface for all mysql native methods.
344 */
355
356 m_auth_provider = auth_provider;
357 m_native_interface = native_interface;
360 }
361
362 virtual ~Gcs_mysql_network_provider() override {
363 /*Close all client connections*/
364 if (!m_connection_map.empty()) {
366 [this](const auto &client_connection) {
367 m_native_interface->mysql_close(client_connection.second);
368 m_native_interface->mysql_free(client_connection.second);
369 });
370 m_connection_map.clear();
371 }
373 }
374
375 /**
376 * @brief See @see Network_provider#start
377 */
378 std::pair<bool, int> start() override;
379
380 /**
381 * @brief See @see Network_provider#stop
382 */
383 std::pair<bool, int> stop() override;
384
385 /**
386 * @brief Get the communication stack implemented by this class
387 *
388 * @return a CommunicationStack enum value. In this case -> MYSQL_PROTOCOL
389 */
391 return MYSQL_PROTOCOL;
392 }
393
394 /**
395 * @brief See @see Network_provider#configure
396 */
397 bool configure(const Network_configuration_parameters &params) override;
398
399 /**
400 * @brief See @see Network_provider#configure_secure_connections
401 */
403 const Network_configuration_parameters &params) override;
404
406
408
409 /**
410 * @brief See @see Network_provider#open_connection
411 */
412 std::unique_ptr<Network_connection> open_connection(
413 const std::string &address, const unsigned short port,
414 const Network_security_credentials &security_credentials,
415 int connection_timeout = Network_provider::default_connection_timeout(),
418
419 int close_connection(const Network_connection &connection) override;
420
421 /**
422 * @brief Set the new connection coming form MySQL server
423 *
424 * @param thd the THD to which the connection belongs to.
425 * @param connection the connection data itself.
426 */
427 void set_new_connection(THD *thd, Network_connection *connection);
428};
429
430/**
431 * @brief Utilitarian class for Gcs_mysql_network_provider
432 *
433 */
435 public:
436 // Out of range log value
437 static constexpr int OUT_OF_RANGE_LOG_LEVEL = 255;
438
439 private:
440 /**
441 * @brief Maps between Network Provider generic log level and MySQL error
442 * Log level
443 *
444 * @param net_provider_log_level Network Provider generic log level
445 * @return int MySQL error Log level if there is mapping
446 * OUT_OF_RANGE_LOG_LEVEL, otherwise
447 */
449 network_provider_dynamic_log_level net_provider_log_level) {
450 switch (net_provider_log_level) {
452 return SYSTEM_LEVEL;
453
455 return ERROR_LEVEL;
456
458 return WARNING_LEVEL;
459
461 return INFORMATION_LEVEL;
462
463 default:
464 // If there is no mapping present, we will return an out of range
465 // value in order to feed LogPluginErr.
466 // When provided a non-valid but non-negative number to LogPluginErr
467 // it means that such levels will result in suppression of the
468 // messages being logged
470 }
471 }
472
473 public:
474 /**
475 * @brief Converts from the intended developer fixed level to a dynamic
476 * level provided from the API call, based on runtime conditions.
477 *
478 * A developer might code that wants ERROR level to be written to the
479 * log, but a runtime condition might modify it.
480 *
481 * If log_level is PROVIDED, nothing changes and coded_log_level is
482 * used. If log_level is other than PROVIDED, we will do a mapping
483 * between log_level and MySQL log level.
484 *
485 * For more information about this mechanism @see
486 * network_provider_dynamic_log_level
487 *
488 * @param coded_log_level Developer intended log level
489 * @param log_level External API call log level
490 * @return int the actual runtime log level
491 */
493 int coded_log_level, network_provider_dynamic_log_level log_level) {
495 ? coded_log_level
498 }
499};
500
501#endif /* GCS_MYSQL_NETWORK_PROVIDER_INCLUDED */
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:251
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:84
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
Get the user credentials needed to establish MySQL connections.
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:218
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:222
Gcs_mysql_network_provider_native_interface_impl()
Definition: gcs_mysql_network_provider.h:220
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:292
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:91
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:93
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:434
static constexpr int OUT_OF_RANGE_LOG_LEVEL
Definition: gcs_mysql_network_provider.h:437
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:492
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:448
Implementation of a.
Definition: gcs_mysql_network_provider.h:299
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:320
Network_configuration_parameters m_config_parameters
Definition: gcs_mysql_network_provider.h:326
enum_transport_protocol get_communication_stack() const override
Get the communication stack implemented by this class.
Definition: gcs_mysql_network_provider.h:390
mysql_mutex_t m_GR_LOCK_connection_map_mutex
Definition: gcs_mysql_network_provider.h:323
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:333
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:362
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:334
std::map< int, MYSQL * > m_connection_map
A map that holds all open MySQL client connections.
Definition: gcs_mysql_network_provider.h:310
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:345
Class that provides Network Namespace services.
Definition: network_provider.h:242
Base class for External Network Providers.
Definition: network_provider.h:295
static constexpr int default_connection_timeout()
Definition: network_provider.h:457
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:36
#define mysql_mutex_destroy(M)
Definition: mysql_mutex.h:46
#define mysql_mutex_init(K, M, A)
Definition: mysql_mutex.h:41
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:58
char * user
Definition: mysqladmin.cc:66
const char * host
Definition: mysqladmin.cc:65
static MYSQL * sock
Definition: mysqlcheck.cc:57
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
static loglevel log_level(const Sql_condition *condition)
Definition: histogram.cc:1644
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:133
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:300
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