MySQL 8.2.0
Source Code Documentation
network_provider_manager.h
Go to the documentation of this file.
1/* Copyright (c) 2015, 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 NETWORK_PROVIDER_MANAGER_H
24#define NETWORK_PROVIDER_MANAGER_H
25
26#include <functional>
27#include <string>
28#include <unordered_map>
29
33
35#include "xcom/result.h"
36#include "xcom/xcom_common.h"
37
38/**
39 * @brief Manages all running instances of a network provider.
40 */
43 public:
45 static Network_provider_manager instance;
46 return instance;
47 }
48
50 delete; // Copy construct
52 delete; // Move construct
54 delete; // Copy assign
56 delete; // Move assign
57
58 /**
59 * @brief Initialize the network manager. It also creates the default XCom
60 * provider and adds it to the manager.
61 *
62 * @return true in case of error. false otherwise.
63 */
64 bool initialize() override;
65
66 /**
67 * @brief Finalize the network manager. It removes the default XCom
68 * provider,
69 *
70 * @return true in case of error. false otherwise.
71 */
72 bool finalize() override;
73
74 /**
75 * @brief Add a new Gcs_network_provider instance
76 *
77 * @param provider an already instantiated shared_ptr object of a
78 * Gcs_network_provider
79 */
81 std::shared_ptr<Network_provider> provider) override;
82
83 /**
84 * @brief Add a new Gcs_network_provider instance and start it.
85 *
86 * @param provider an already instantiated shared_ptr object of a
87 * Gcs_network_provider
88 */
90 std::shared_ptr<Network_provider> provider);
91
92 /**
93 * @brief Remove an active network provider
94 *
95 * @param provider_key a valid value of CommunicationStack of the provider
96 * that you want to remove.
97 */
98 void remove_network_provider(enum_transport_protocol provider_key) override;
99
100 /**
101 * @brief Removes all configured network providers
102 *
103 */
104 void remove_all_network_provider() override;
105
106 /**
107 * @brief Starts an already added network provider
108 *
109 * @param provider_key a valid value of CommunicationStack of the provider
110 * that you want to start.
111 *
112 * @return true In case of success.
113 * @return false In case of failure.
114 */
116
117 /**
118 * @brief Stops all network providers.
119 *
120 * @return true In case of success stopping ALL network providers
121 * @return false In case of failure in stopping AT LEAST ONE network provider
122 */
123 bool stop_all_network_providers() override;
124
125 /**
126 * @brief Stops a running network provider
127 *
128 * @param provider_key a valid value of CommunicationStack of the provider
129 * that you want to stop.
130 *
131 * @return true In case of success.
132 * @return false In case of failure.
133 */
135
136 /**
137 * @brief Sets the running Communication Stack, thus defining the active
138 * provider.
139 *
140 * In runtime, this is will change the way we establish connections.
141 *
142 * @param new_value value of the Communication Stack
143 */
145 m_running_protocol = new_value;
146 }
147
148 /**
149 * @brief Gets the configured running protocol
150 *
151 * It returns the value that is currently configured in the Running
152 * Communication Stack
153 *
154 * Since this value is dynamic, it can cause a mismatch from the provider
155 * that we are actively receiving connections and the provider that we use
156 * to establish new connections
157 *
158 * @return CommunicationStack value.
159 */
161 return m_running_protocol;
162 }
163
164 /**
165 * @brief Get the incoming connections Communication Stack
166 *
167 * This is the value that is used to report upwards the protocol in * which
168 * we are currently accepting connections.
169 *
170 * @return CommunicationStack
171 */
174 }
175
176 /**
177 * @brief Gets a configured provider
178 *
179 * @param provider const std::shared_ptr<Gcs_network_provider> a shared_ptr to
180 * the active provider.
181 * @return const std::shared_ptr<Network_provider>
182 */
183 const std::shared_ptr<Network_provider> get_provider(
184 enum_transport_protocol provider);
185
186 /**
187 * @brief Retrieves the active provider. This is determined by the value set
188 * in set_running_protocol.
189 *
190 * @return const std::shared_ptr<Gcs_network_provider> a shared_ptr to the
191 * active provider.
192 */
193 const std::shared_ptr<Network_provider> get_active_provider();
194
195 /**
196 * @brief Retrieves the active provider for incoming connections.
197 * This is determined by the value set in set_running_protocol when the active
198 * provider is started.
199 *
200 * @return const std::shared_ptr<Gcs_network_provider> a shared_ptr to the
201 * active provider for incoming connections.
202 */
203 const std::shared_ptr<Network_provider> get_incoming_connections_provider();
204
205 /**
206 * @brief Start the active provider.
207 *
208 * @return true In case of success.
209 * @return false In case of failure.
210 */
211 bool start_active_network_provider() override;
212
213 /**
214 * @brief Stops the active provider.
215 *
216 * @return true In case of success.
217 * @return false In case of failure.
218 */
219 bool stop_active_network_provider() override;
220
221 /**
222 * @brief Configures the active provider
223 *
224 * @param params configuration parameters.
225 *
226 * @return true In case of success.
227 * @return false In case of failure.
228 */
230 Network_configuration_parameters &params) override;
231
232 /**
233 * @brief Configures the active provider with all things needed to establish
234 * SSL connections
235 *
236 * @param params configuration parameters for SSL.
237 *
238 * @return true In case of success.
239 * @return false In case of failure.
240 */
242 Network_configuration_parameters &params) override;
243
244 // Interface methods...
245 /**
246 * @brief Method to call to get Server Connections that are waiting to be
247 * accepted.
248 *
249 * Loop over this method to accept connections. They will be accepted from
250 * the provider that is configured in the Incoming Connection protocol.
251 *
252 * @return connection_descriptor* a pointer to a connection_descriptor. If the
253 * pointer is nullptr, no new connections are
254 * available.
255 */
257
258 /**
259 * @brief Closes an open connection to another XCom endpoint served by the
260 * a Network provider.
261 *
262 * @param connection_handle an open and valid connection
263 * @return int an error code in case of error. 0, otherwise.
264 */
265 int close_xcom_connection(connection_descriptor *connection_handle);
266
267 /**
268 * @brief Opens a new connection to another XCom endpoint served by the same
269 * Network provider.
270 *
271 * @param server address of the remote endpoint
272 * @param port port of the remote endpoint
273 * @param use_ssl if this connection should use SSL
274 * @param connection_timeout optional connection timeout.
275 *
276 * @return connection_descriptor an established connection.
277 * nullptr in case of failure.
278 */
280 const char *server, xcom_port port, bool use_ssl,
281 int connection_timeout = Network_provider::default_connection_timeout());
282
283 // SSL RELATED OPERATIONS
284
285 /*
286 Return whether the SSL will be used to encrypt data or not.
287
288 Return 1 if it is enabled 0 otherwise.
289 */
290 int is_xcom_using_ssl() const override;
291
292 /*
293 Set the operation mode which might be the following:
294
295 . SSL_DISABLED (1): The SSL mode will be disabled and this is the default
296 value.
297
298 . SSL_PREFERRED (2): The SSL mode will be always disabled if this value is
299 provided and is only allowed to keep the solution compatibility with
300 MySQL server.
301
302 . SSL_REQUIRED (4): The SSL mode will be enabled but the verifications
303 described in the next modes are not performed.
304
305 . SSL_VERIFY_CA (4) - Verify the server TLS certificate against the
306 configured Certificate Authority (CA) certificates. The connection attempt
307 fails if no valid matching CA certificates are found.
308
309 . SSL_VERIFY_IDENTITY (5): Like VERIFY_CA, but additionally verify that the
310 server certificate matches the host to which the connection is attempted.
311
312 If a different value is provide, INVALID_SSL_MODE (-1) is returned.
313*/
314 int xcom_set_ssl_mode(int mode) override;
315
316 /*
317 Return the operation mode as an integer from an operation mode provided
318 as a string. Note that the string must be provided in upper case letters
319 and the possible values are: "DISABLED", "PREFERRED", "REQUIRED",
320 "VERIFY_CA" or "VERIFY_IDENTITY".
321
322 If a different value is provide, INVALID_SSL_MODE (-1) is returned.
323 */
324 int xcom_get_ssl_mode(const char *mode) override;
325
326 /*
327 Return the configured value into SSL mode
328 */
329 int xcom_get_ssl_mode() override;
330
331 /*
332 Set the operation fips mode which might be the following:
333
334 . SSL_FIPS_MODE_OFF (0): This will set openssl fips mode value to 0
335
336 . SSL_FIPS_MODE_ON (1): This will set openssl fips mode value to 1
337
338 . SSL_FIPS_MODE_STRICT (2): This will set openssl fips mode value to 2
339
340 If a different value is provide, INVALID_SSL_FIPS_MODE (-1) is returned.
341 */
342 int xcom_set_ssl_fips_mode(int mode) override;
343
344 /*
345 Return the operation fips mode as an integer from an operation fips mode
346 provided as a string. Note that the string must be provided in upper case
347 letters and the possible values are: "OFF", "ON", "STRICT",
348
349 If a different value is provide, INVALID_SSL_MODE (-1) is returned.
350 */
351 int xcom_get_ssl_fips_mode(const char *mode) override;
352
353 /*
354 Returns the configured FIPS mode
355 */
356 int xcom_get_ssl_fips_mode() override;
357
358 /**
359 * @brief Cleans up SSL context.
360 */
364
365 private:
371 virtual ~Network_provider_manager() override { m_network_providers.clear(); }
372
375 }
376
377 std::unordered_map<enum_transport_protocol, std::shared_ptr<Network_provider>,
378 std::hash<int>>
380
383
386
390
391 // Default provider. It is encapsulated in the Network Manager.
392 std::shared_ptr<Xcom_network_provider> m_xcom_network_provider;
393
394 // Clear SSL data function to be held after the last active network
395 // provider was still at work
396 std::function<void()> m_ssl_data_context_cleaner{nullptr};
397};
398
399/**
400 * @brief Proxy class to access funcionality in Network_provider_manager
401 *
402 * This way, we avoid spreading singleton calls in all the code, thus
403 * encapsulting all calls.
404 *
405 */
409 public:
412 }
413
414 virtual ~Network_Management_Interface() override = default;
415
417 delete; // Copy construct
419 Network_Management_Interface const &) = delete; // Copy assign
420
421 bool initialize() override { return m_get_manager().initialize(); }
422
423 bool finalize() override { return m_get_manager().finalize(); }
424
426 m_get_manager().set_running_protocol(new_value);
427 }
428
430 return m_get_manager().get_running_protocol();
431 }
432
434 return m_get_manager().get_incoming_connections_protocol();
435 }
437 std::shared_ptr<Network_provider> provider) override {
438 m_get_manager().add_network_provider(provider);
439 }
440
442 return m_get_manager().start_active_network_provider();
443 }
444
446 return m_get_manager().stop_all_network_providers();
447 }
448
450 return m_get_manager().stop_active_network_provider();
451 }
452
454 m_get_manager().remove_network_provider(provider_key);
455 }
456
458 m_get_manager().remove_all_network_provider();
459 }
460
462 Network_configuration_parameters &params) override {
463 return m_get_manager().configure_active_provider(params);
464 }
465
467 Network_configuration_parameters &params) override {
468 return m_get_manager().configure_active_provider_secure_connections(params);
469 }
470
471 int is_xcom_using_ssl() const override {
472 return m_get_manager().is_xcom_using_ssl();
473 }
474
475 int xcom_set_ssl_mode(int mode) override {
476 return m_get_manager().xcom_set_ssl_mode(mode);
477 }
478 int xcom_get_ssl_mode(const char *mode) override {
479 return m_get_manager().xcom_get_ssl_mode(mode);
480 }
481 int xcom_get_ssl_mode() override {
482 return m_get_manager().xcom_get_ssl_mode();
483 }
484 int xcom_set_ssl_fips_mode(int mode) override {
485 return m_get_manager().xcom_set_ssl_fips_mode(mode);
486 }
487 int xcom_get_ssl_fips_mode(const char *mode) override {
488 return m_get_manager().xcom_get_ssl_fips_mode(mode);
489 }
490 int xcom_get_ssl_fips_mode() override {
491 return m_get_manager().xcom_get_ssl_fips_mode();
492 }
494 m_get_manager().cleanup_secure_connections_context();
495 }
497 m_get_manager().delayed_cleanup_secure_connections_context();
498 }
500 m_get_manager().finalize_secure_connections_context();
501 }
502
503 private:
505};
506
507#endif // GCS_XCOM_NETWORK_PROVIDER_MANAGER_H
Proxy class to access funcionality in Network_provider_manager.
Definition: network_provider_manager.h:408
virtual ~Network_Management_Interface() override=default
Network_Management_Interface(Network_Management_Interface const &)=delete
bool stop_all_network_providers() override
Stops all network providers.
Definition: network_provider_manager.h:445
bool configure_active_provider(Network_configuration_parameters &params) override
Configures the active provider.
Definition: network_provider_manager.h:461
void set_running_protocol(enum_transport_protocol new_value) override
Sets the running Communication Stack, thus defining the active provider.
Definition: network_provider_manager.h:425
bool stop_active_network_provider() override
Stops the active provider.
Definition: network_provider_manager.h:449
void delayed_cleanup_secure_connections_context() override
Cleans up SSL context indirectly from the last active network provider.
Definition: network_provider_manager.h:496
int xcom_get_ssl_fips_mode() override
Returns the configured FIPS mode.
Definition: network_provider_manager.h:490
void add_network_provider(std::shared_ptr< Network_provider > provider) override
Add a new Gcs_network_provider instance.
Definition: network_provider_manager.h:436
bool start_active_network_provider() override
Start the active provider.
Definition: network_provider_manager.h:441
enum_transport_protocol get_running_protocol() const override
Gets the configured running protocol.
Definition: network_provider_manager.h:429
int is_xcom_using_ssl() const override
Return whether the SSL will be used to encrypt data or not.
Definition: network_provider_manager.h:471
int xcom_set_ssl_mode(int mode) override
Set the operation mode which might be the following:
Definition: network_provider_manager.h:475
void remove_network_provider(enum_transport_protocol provider_key) override
Definition: network_provider_manager.h:453
Network_Management_Interface & operator=(Network_Management_Interface const &)=delete
int xcom_get_ssl_fips_mode(const char *mode) override
Return the operation fips mode as an integer from an operation fips mode provided as a string.
Definition: network_provider_manager.h:487
void cleanup_secure_connections_context() override
Cleans up SSL context directly into the active network provider.
Definition: network_provider_manager.h:493
int xcom_get_ssl_mode(const char *mode) override
Return the operation mode as an integer from an operation mode provided as a string.
Definition: network_provider_manager.h:478
int xcom_set_ssl_fips_mode(int mode) override
Set the operation fips mode which might be the following:
Definition: network_provider_manager.h:484
Network_Management_Interface()
Definition: network_provider_manager.h:410
std::function< Network_provider_manager &()> m_get_manager
Definition: network_provider_manager.h:504
int xcom_get_ssl_mode() override
Return the configured value into SSL mode.
Definition: network_provider_manager.h:481
enum_transport_protocol get_incoming_connections_protocol() const override
Get the incoming connections Communication Stack.
Definition: network_provider_manager.h:433
bool finalize() override
Finalize the network manager.
Definition: network_provider_manager.h:423
bool initialize() override
Initialize the network manager.
Definition: network_provider_manager.h:421
void remove_all_network_provider() override
Definition: network_provider_manager.h:457
bool configure_active_provider_secure_connections(Network_configuration_parameters &params) override
COnfigures the active provider SSL parameters.
Definition: network_provider_manager.h:466
void finalize_secure_connections_context() override
Destroys all things SSL related.
Definition: network_provider_manager.h:499
Inversion of Control interface to manage Network providers.
Definition: network_management_interface.h:33
Manages all running instances of a network provider.
Definition: network_provider_manager.h:42
static Network_provider_manager & getInstance()
Definition: network_provider_manager.h:44
bool start_network_provider(enum_transport_protocol provider_key)
Starts an already added network provider.
Definition: network_provider_manager.cc:96
enum_transport_protocol get_incoming_connections_protocol() const override
Get the incoming connections Communication Stack.
Definition: network_provider_manager.h:172
bool finalize() override
Finalize the network manager.
Definition: network_provider_manager.cc:58
bool stop_network_provider(enum_transport_protocol provider_key)
Stops a running network provider.
Definition: network_provider_manager.cc:117
bool stop_active_network_provider() override
Stops the active provider.
Definition: network_provider_manager.cc:153
void cleanup_secure_connections_context() override
Cleans up SSL context.
Definition: network_provider_manager.cc:357
int xcom_get_ssl_mode() override
Return the configured value into SSL mode.
Definition: network_provider_manager.cc:349
Network_provider_manager & operator=(Network_provider_manager &&)=delete
int is_xcom_using_ssl() const override
Return whether the SSL will be used to encrypt data or not.
Definition: network_provider_manager.cc:294
Network_provider_manager(Network_provider_manager const &)=delete
int xcom_get_ssl_fips_mode() override
Returns the configured FIPS mode.
Definition: network_provider_manager.cc:322
void remove_all_network_provider() override
Removes all configured network providers.
Definition: network_provider_manager.cc:92
connection_descriptor * incoming_connection()
Method to call to get Server Connections that are waiting to be accepted.
Definition: network_provider_manager.cc:270
int close_xcom_connection(connection_descriptor *connection_handle)
Closes an open connection to another XCom endpoint served by the a Network provider.
Definition: network_provider_manager.cc:253
void delayed_cleanup_secure_connections_context() override
Cleans up SSL context indirectly from the last active network provider.
Definition: network_provider_manager.cc:351
void add_and_start_network_provider(std::shared_ptr< Network_provider > provider)
Add a new Gcs_network_provider instance and start it.
Definition: network_provider_manager.cc:79
enum_transport_protocol m_running_protocol
Definition: network_provider_manager.h:381
enum_transport_protocol m_incoming_connections_protocol
Definition: network_provider_manager.h:382
std::unordered_map< enum_transport_protocol, std::shared_ptr< Network_provider >, std::hash< int > > m_network_providers
Definition: network_provider_manager.h:379
const std::shared_ptr< Network_provider > get_incoming_connections_provider()
Retrieves the active provider for incoming connections.
Definition: network_provider_manager.cc:130
void add_network_provider(std::shared_ptr< Network_provider > provider) override
Add a new Gcs_network_provider instance.
Definition: network_provider_manager.cc:68
Network_configuration_parameters m_active_provider_secure_connections_configuration
Definition: network_provider_manager.h:389
const std::shared_ptr< Network_provider > get_provider(enum_transport_protocol provider)
Gets a configured provider.
Definition: network_provider_manager.cc:212
Network_provider_manager(Network_provider_manager &&)=delete
void set_incoming_connections_protocol(enum_transport_protocol value)
Definition: network_provider_manager.h:373
int m_ssl_fips_mode
Definition: network_provider_manager.h:385
bool stop_all_network_providers() override
Stops all network providers.
Definition: network_provider_manager.cc:103
bool configure_active_provider_secure_connections(Network_configuration_parameters &params) override
Configures the active provider with all things needed to establish SSL connections.
Definition: network_provider_manager.cc:184
void remove_network_provider(enum_transport_protocol provider_key) override
Remove an active network provider.
Definition: network_provider_manager.cc:87
virtual ~Network_provider_manager() override
Definition: network_provider_manager.h:371
bool configure_active_provider(Network_configuration_parameters &params) override
Configures the active provider.
Definition: network_provider_manager.cc:166
bool start_active_network_provider() override
Start the active provider.
Definition: network_provider_manager.cc:134
bool initialize() override
Initialize the network manager.
Definition: network_provider_manager.cc:50
void set_running_protocol(enum_transport_protocol new_value) override
Sets the running Communication Stack, thus defining the active provider.
Definition: network_provider_manager.h:144
int xcom_set_ssl_mode(int mode) override
Set the operation mode which might be the following:
Definition: network_provider_manager.cc:340
const std::shared_ptr< Network_provider > get_active_provider()
Retrieves the active provider.
Definition: network_provider_manager.cc:125
Network_provider_manager()
Definition: network_provider_manager.h:366
Network_configuration_parameters m_active_provider_configuration
Definition: network_provider_manager.h:387
connection_descriptor * open_xcom_connection(const char *server, xcom_port port, bool use_ssl, int connection_timeout=Network_provider::default_connection_timeout())
Opens a new connection to another XCom endpoint served by the same Network provider.
Definition: network_provider_manager.cc:221
int xcom_set_ssl_fips_mode(int mode) override
Set the operation fips mode which might be the following:
Definition: network_provider_manager.cc:298
enum_transport_protocol get_running_protocol() const override
Gets the configured running protocol.
Definition: network_provider_manager.h:160
void finalize_secure_connections_context() override
Destroys all things SSL related.
Definition: network_provider_manager.cc:366
Network_provider_manager & operator=(Network_provider_manager const &)=delete
std::function< void()> m_ssl_data_context_cleaner
Definition: network_provider_manager.h:396
int m_ssl_mode
Definition: network_provider_manager.h:384
std::shared_ptr< Xcom_network_provider > m_xcom_network_provider
Definition: network_provider_manager.h:392
Inversion of Control proxy interface to operate Network providers.
Definition: network_management_interface.h:210
static constexpr int default_connection_timeout()
Definition: network_provider.h:424
mode
Definition: file_handle.h:59
@ FIPS_MODE_OFF
Definition: network_provider.h:70
enum_transport_protocol
Enum that describes the available XCom Communication Stacks.
Definition: network_provider.h:44
@ XCOM_PROTOCOL
Definition: network_provider.h:46
@ SSL_DISABLED
Definition: network_provider.h:56
required uint64 port
Definition: replication_asynchronous_connection_failover.proto:32
Possible configuration parameters.
Definition: network_provider.h:153
Definition: node_connection.h:46
Definition: server_struct.h:38
unsigned short xcom_port
Definition: xcom_common.h:45