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