MySQL 8.3.0
Source Code Documentation
gcs_mysql_network_provider.h
Go to the documentation of this file.
1/* Copyright (c) 2016, 2023, 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 GCS_MYSQL_NETWORK_PROVIDER_INCLUDED
24#define GCS_MYSQL_NETWORK_PROVIDER_INCLUDED
25
26#include <map>
27
28#include "include/mysql.h"
29
30#include <mysql.h>
31
33
36#include "sql/sql_class.h"
37
38/**
39 * @brief IoC interface to allow abstraction of the retrieval of Security
40 * Credentials
41 *
42 */
44 public:
46
47 /**
48 * @brief Get the user credentials needed to establish MySQL connections.
49 *
50 * This interface is used either as a proxy for @see Replication_thread_api
51 * or to be injected by unit tests.
52 *
53 * @param username username for the mysql connection
54 * @param password password for the mysql connection
55 *
56 * @return the operation status
57 * @retval false OK
58 * @retval true Error, channel not found
59 */
60 virtual bool get_credentials(std::string &username,
61 std::string &password) = 0;
62};
63
64/**
65 * @brief Implementation of Gcs_mysql_network_provider_auth_interface
66 * that retrieves auth data from MySQL.
67 */
70 public:
72 : m_recovery_channel("group_replication_recovery") {}
74
75 /**
76 * @brief Get the user credentials needed to establish MySQL connections.
77 *
78 * @see Gcs_mysql_network_provider_auth_interface#get_credentials
79 */
80 bool get_credentials(std::string &username, std::string &password) override;
81
82 private:
84};
85
86/**
87 * @brief IoC interface to allow abstraction of MySQL Client API
88 *
89 */
91 public:
93
94 /**
95 * @brief Proxy method to mysql_real_connect from the MySQL client API
96 *
97 * @param mysql mysql client connection reference. Must have been
98 * initializaed with mysql_init
99 * @param host hostname to connect
100 * @param user username for the connection
101 * @param passwd password for the connection
102 * @param db database/schema to use
103 * @param port remote port to connect
104 * @param unix_socket unix socket file (if applicable)
105 * @param clientflag client flags
106 * @return MYSQL* a mysql client connection.
107 */
108 virtual MYSQL *mysql_real_connect(MYSQL *mysql, const char *host,
109 const char *user, const char *passwd,
110 const char *db, unsigned int port,
111 const char *unix_socket,
112 unsigned long clientflag) = 0;
113 /**
114 * @brief Proxy method to simple_command from the MySQL client API
115 *
116 * @param mysql an active MySQL connection
117 * @param command the command to send
118 * @param arg command arguments
119 * @param length length of the arguments
120 * @param skip_check skip checking the command
121 *
122 * @return true in case of error. false, otherwise
123 *
124 */
126 const unsigned char *arg, size_t length,
127 bool skip_check) = 0;
128
129 /**
130 * @brief Proxy method to mysql_init from the MySQL Client API
131 *
132 * @param sock the connection to initialize
133 */
134 virtual MYSQL *mysql_init(MYSQL *sock) = 0;
135
136 /**
137 * @brief Proxy method to mysql_close from the MySQL Client API
138 *
139 * @param sock the connection to close
140 */
141 virtual void mysql_close(MYSQL *sock) = 0;
142
143 /**
144 Method to get the network namespace configured for a channel
145
146 @param[out] net_ns The network namespace to extract
147
148 @return the operation status
149 @retval false OK
150 @retval true Error, channel not found
151 */
152 virtual int channel_get_network_namespace(std::string &net_ns) = 0;
153
154 /**
155 Set active network namespace specified by a name.
156
157 @param network_namespace the name of a network namespace to be set active
158
159 @return false on success, true on error
160 @note all opened descriptors used during function run are closed on error
161 */
162 virtual bool set_network_namespace(const std::string &network_namespace) = 0;
163
164 /**
165 Restore original network namespace used to be active before a new network
166 namespace has been set.
167
168 @return false on success, true on failure
169 */
171
172 /**
173 * @brief Proxy method to mysql_free from the MySQL Memory API
174 *
175 * @param ptr the pointer to free
176 */
177 virtual void mysql_free(void *ptr) = 0;
178
179 /**
180 * @brief Proxy method to mysql_options from the MySQL Memory API
181 *
182 * @param mysql connection to set an option
183 * @param option option to set
184 * @param arg value of the option to set
185 *
186 * @return int > 0 in case of error.
187 */
188 virtual int mysql_options(MYSQL *mysql, enum mysql_option option,
189 const void *arg) = 0;
190
191 /**
192 * @brief Proxy method to mysql_ssl_set from the MySQL Memory API
193 *
194 * @param mysql connection to set SSL options
195 * @param key connection key
196 * @param cert connection certificate
197 * @param ca connection CA
198 * @param capath the CA path
199 * @param cipher cipher to use
200 *
201 * @return true in case of error;
202 * @return false otherwise.
203 */
204 virtual bool mysql_ssl_set(MYSQL *mysql, const char *key, const char *cert,
205 const char *ca, const char *capath,
206 const char *cipher) = 0;
207};
208
209/**
210 * @brief Internal implementation of
211 * Gcs_mysql_network_provider_native_interface_impl that serves as a proxy
212 * for MySQL Client API functions.
213 *
214 */
218 public:
220 : m_recovery_channel("group_replication_recovery") {}
222
223 /**
224 * @brief Implementation of @see
225 * Gcs_mysql_network_provider_native_interface#mysql_real_connect
226 */
227 MYSQL *mysql_real_connect(MYSQL *mysql, const char *host, const char *user,
228 const char *passwd, const char *db,
229 unsigned int port, const char *unix_socket,
230 unsigned long clientflag) override;
231 /**
232 * @brief Implementation of @see
233 * Gcs_mysql_network_provider_native_interface#send_command
234 */
236 const unsigned char *arg, size_t length,
237 bool skip_check) override;
238
239 /**
240 * @brief Implementation of @see
241 * Gcs_mysql_network_provider_native_interface#mysql_init
242 */
243 MYSQL *mysql_init(MYSQL *sock) override;
244
245 /**
246 * @brief Implementation of @see
247 * Gcs_mysql_network_provider_native_interface#mysql_close
248 */
249 void mysql_close(MYSQL *sock) override;
250
251 /**
252 * @brief Implementation of @see
253 * Gcs_mysql_network_provider_native_interface#channel_get_network_namespace
254 */
255 int channel_get_network_namespace(std::string &net_ns) override;
256
257 /**
258 * @brief Implementation of @see
259 * Gcs_mysql_network_provider_native_interface#set_network_namespace
260 */
261 bool set_network_namespace(const std::string &network_namespace) override;
262
263 /**
264 * @brief Implementation of @see
265 * Gcs_mysql_network_provider_native_interface#restore_original_network_namespace
266 */
268
269 /**
270 * @brief Implementation of @see
271 * Gcs_mysql_network_provider_native_interface#my_free
272 */
273 void mysql_free(void *ptr) override;
274
275 /**
276 * @brief Implementation of @see
277 * Gcs_mysql_network_provider_native_interface#mysql_options
278 */
279 int mysql_options(MYSQL *mysql, enum mysql_option option,
280 const void *arg) override;
281
282 /**
283 * @brief Implementation of @see
284 * Gcs_mysql_network_provider_native_interface#mysql_ssl_set
285 */
286 bool mysql_ssl_set(MYSQL *mysql, const char *key, const char *cert,
287 const char *ca, const char *capath,
288 const char *cipher) override;
289
290 private:
292};
293
294/**
295 * @brief Implementation of a \see Network_provider that will manage MySQL
296 * protocol connection for GCS/XCOM.
297 */
299 private:
300 /**
301 * @brief A map that holds all open MySQL client connections.
302 *
303 * Since the public interface of Network Managers only knows about File
304 * Descriptors, this is the repository for all MySQL client connections. This
305 * object is required when using mysql_close.
306 *
307 * The map's index is the open connection's file descriptor.
308 */
309 std::map<int, MYSQL *> m_connection_map;
310
311 /**
312 * @brief A map that holds THD's for all open MySQL Server connections.
313 *
314 * We need to maintain this reference in order to call the appropriate closing
315 * mechanisms when destroying an incoming connection.
316 *
317 * The map's index is the open connection's file descriptor.
318 */
319 std::map<int, THD *> m_incoming_connection_map;
320
321 // Locking for the connection map
323
324 // Configuration parameters for this Provider
326
327 /**
328 * External IoC dependencies.
329 * - A provider for authentication parameters
330 * - A provider for all mysql native methods
331 */
334
335 public:
336 /**
337 * @brief Construct a new Gcs_mysql_network_provider
338 *
339 * @param auth_provider A provider interface implementation for authentication
340 * parameters.
341 *
342 * @param native_interface A provider interface for all mysql native methods.
343 */
354
355 m_auth_provider = auth_provider;
356 m_native_interface = native_interface;
359 }
360
361 virtual ~Gcs_mysql_network_provider() override {
362 /*Close all client connections*/
363 if (!m_connection_map.empty()) {
365 [this](const auto &client_connection) {
366 m_native_interface->mysql_close(client_connection.second);
367 m_native_interface->mysql_free(client_connection.second);
368 });
369 m_connection_map.clear();
370 }
372 }
373
374 /**
375 * @brief See @see Network_provider#start
376 */
377 std::pair<bool, int> start() override;
378
379 /**
380 * @brief See @see Network_provider#stop
381 */
382 std::pair<bool, int> stop() override;
383
384 /**
385 * @brief Get the communication stack implemented by this class
386 *
387 * @return a CommunicationStack enum value. In this case -> MYSQL_PROTOCOL
388 */
390 return MYSQL_PROTOCOL;
391 }
392
393 /**
394 * @brief See @see Network_provider#configure
395 */
396 bool configure(const Network_configuration_parameters &params) override;
397
398 /**
399 * @brief See @see Network_provider#configure_secure_connections
400 */
402 const Network_configuration_parameters &params) override;
403
405
407
408 /**
409 * @brief See @see Network_provider#open_connection
410 */
411 std::unique_ptr<Network_connection> open_connection(
412 const std::string &address, const unsigned short port,
413 const Network_security_credentials &security_credentials,
414 int 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#endif /* GCS_MYSQL_NETWORK_PROVIDER_INCLUDED */
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:250
Implementation of Gcs_mysql_network_provider_auth_interface that retrieves auth data from MySQL.
Definition: gcs_mysql_network_provider.h:69
Gcs_mysql_network_provider_auth_interface_impl()
Definition: gcs_mysql_network_provider.h:71
Replication_thread_api m_recovery_channel
Definition: gcs_mysql_network_provider.h:83
virtual ~Gcs_mysql_network_provider_auth_interface_impl() override
Definition: gcs_mysql_network_provider.h:73
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:40
IoC interface to allow abstraction of the retrieval of Security Credentials.
Definition: gcs_mysql_network_provider.h:43
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:45
Internal implementation of Gcs_mysql_network_provider_native_interface_impl that serves as a proxy fo...
Definition: gcs_mysql_network_provider.h:217
MYSQL * mysql_init(MYSQL *sock) override
Implementation of.
Definition: gcs_mysql_network_provider.cc:59
bool restore_original_network_namespace() override
Implementation of.
Definition: gcs_mysql_network_provider.cc:121
int channel_get_network_namespace(std::string &net_ns) override
Implementation of.
Definition: gcs_mysql_network_provider.cc:93
bool set_network_namespace(const std::string &network_namespace) override
Implementation of.
Definition: gcs_mysql_network_provider.cc:106
void mysql_free(void *ptr) override
Implementation of.
Definition: gcs_mysql_network_provider.cc:73
virtual ~Gcs_mysql_network_provider_native_interface_impl() override
Definition: gcs_mysql_network_provider.h:221
Gcs_mysql_network_provider_native_interface_impl()
Definition: gcs_mysql_network_provider.h:219
int mysql_options(MYSQL *mysql, enum mysql_option option, const void *arg) override
Implementation of.
Definition: gcs_mysql_network_provider.cc:77
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:82
Replication_thread_api m_recovery_channel
Definition: gcs_mysql_network_provider.h:291
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:45
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:53
void mysql_close(MYSQL *sock) override
Implementation of.
Definition: gcs_mysql_network_provider.cc:64
IoC interface to allow abstraction of MySQL Client API.
Definition: gcs_mysql_network_provider.h:90
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:92
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.
Implementation of a.
Definition: gcs_mysql_network_provider.h:298
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:319
Network_configuration_parameters m_config_parameters
Definition: gcs_mysql_network_provider.h:325
enum_transport_protocol get_communication_stack() const override
Get the communication stack implemented by this class.
Definition: gcs_mysql_network_provider.h:389
mysql_mutex_t m_GR_LOCK_connection_map_mutex
Definition: gcs_mysql_network_provider.h:322
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:333
Gcs_mysql_network_provider_auth_interface * m_auth_provider
External IoC dependencies.
Definition: gcs_mysql_network_provider.h:332
std::pair< bool, int > stop() override
See.
Definition: gcs_mysql_network_provider.cc:135
bool configure_secure_connections(const Network_configuration_parameters &params) override
See.
Definition: gcs_mysql_network_provider.cc:163
std::pair< bool, int > start() override
See.
Definition: gcs_mysql_network_provider.cc:129
bool finalize_secure_connections_context() override
Definition: gcs_mysql_network_provider.cc:192
bool configure(const Network_configuration_parameters &params) override
See.
Definition: gcs_mysql_network_provider.cc:158
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()) override
See.
Definition: gcs_mysql_network_provider.cc:196
virtual ~Gcs_mysql_network_provider() override
Definition: gcs_mysql_network_provider.h:361
void cleanup_secure_connections_context() override
Definition: gcs_mysql_network_provider.cc:186
Gcs_mysql_network_provider_native_interface * m_native_interface
Definition: gcs_mysql_network_provider.h:333
std::map< int, MYSQL * > m_connection_map
A map that holds all open MySQL client connections.
Definition: gcs_mysql_network_provider.h:309
void set_new_connection(THD *thd, Network_connection *connection)
Set the new connection coming form MySQL server.
Definition: gcs_mysql_network_provider.cc:366
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:344
Class that provides Network Namespace services.
Definition: network_provider.h:215
Base class for External Network Providers.
Definition: network_provider.h:268
static constexpr int default_connection_timeout()
Definition: network_provider.h:424
Definition: replication_threads_api.h:36
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:35
#define mysql_mutex_destroy(M)
Definition: mysql_mutex.h:45
#define mysql_mutex_init(K, M, A)
Definition: mysql_mutex.h:40
enum_server_command
A list of all MySQL protocol commands.
Definition: my_command.h:47
This file defines the client API to MySQL and also the ABI of the dynamically linked libmysqlclient.
mysql_option
Definition: mysql.h:167
static char * password
Definition: mysql_secure_installation.cc:57
char * user
Definition: mysqladmin.cc:64
const char * host
Definition: mysqladmin.cc:63
static MYSQL * sock
Definition: mysqlcheck.cc:56
void for_each(const Shards< COUNT > &shards, Function &&f) noexcept
Iterate over the shards.
Definition: ut0counter.h:322
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:75
Definition: instrumented_condition_variable.h:31
enum_transport_protocol
Enum that describes the available XCom Communication Stacks.
Definition: network_provider.h:44
@ MYSQL_PROTOCOL
Definition: network_provider.h:47
@ SSL_DISABLED
Definition: network_provider.h:56
PSI_mutex_key key_GR_LOCK_connection_map
Definition: plugin_psi.h:132
required string key
Definition: replication_asynchronous_connection_failover.proto:59
required string network_namespace
Definition: replication_asynchronous_connection_failover.proto:33
required uint64 port
Definition: replication_asynchronous_connection_failover.proto:32
Definition: mysql.h:297
Possible configuration parameters.
Definition: network_provider.h:153
struct ssl_parameters ssl_params
Definition: network_provider.h:156
Represents an open connection.
Definition: network_provider.h:163
Security credentials to establish a connection.
Definition: network_provider.h:96
An instrumented mutex structure.
Definition: mysql_mutex_bits.h:49
int ssl_mode
Definition: network_provider.h:134
#define MY_MUTEX_INIT_FAST
Definition: thr_mutex.h:67
command
Definition: version_token.cc:279