MXS-2483: Return std::unique_ptr from SSLContext::create
Smart pointers are far nicer than raw pointers.
This commit is contained in:
@ -251,7 +251,7 @@ private:
|
||||
*/
|
||||
Listener(SERVICE* service, const std::string& name, const std::string& address, uint16_t port,
|
||||
const std::string& protocol, const std::string& authenticator,
|
||||
const std::string& auth_opts, void* auth_instance, mxs::SSLContext* ssl,
|
||||
const std::string& auth_opts, void* auth_instance, std::unique_ptr<mxs::SSLContext> ssl,
|
||||
const MXS_CONFIG_PARAMETER& params);
|
||||
|
||||
/**
|
||||
|
@ -528,9 +528,9 @@ public:
|
||||
}
|
||||
|
||||
protected:
|
||||
SERVER(mxs::SSLContext* ssl_context = nullptr)
|
||||
SERVER(std::unique_ptr<mxs::SSLContext> ssl_context)
|
||||
: m_response_time{0.04, 0.35, 500}
|
||||
, m_ssl_context{ssl_context}
|
||||
, m_ssl_context{std::move(ssl_context)}
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -74,7 +74,7 @@ public:
|
||||
*
|
||||
* @return A new SSL configuration or nullptr on error
|
||||
*/
|
||||
static SSLContext* create(const MXS_CONFIG_PARAMETER& params);
|
||||
static std::unique_ptr<SSLContext> create(const MXS_CONFIG_PARAMETER& params);
|
||||
|
||||
/**
|
||||
* Serialize the SSL configuration into a INI file section
|
||||
|
@ -2859,7 +2859,7 @@ bool config_can_modify_at_runtime(const char* name)
|
||||
bool config_create_ssl(const char* name,
|
||||
const MXS_CONFIG_PARAMETER& params,
|
||||
bool require_cert,
|
||||
mxs::SSLContext** dest)
|
||||
std::unique_ptr<mxs::SSLContext>* dest)
|
||||
{
|
||||
bool ok = true;
|
||||
*dest = nullptr;
|
||||
|
@ -394,15 +394,15 @@ bool runtime_destroy_server(Server* server)
|
||||
return rval;
|
||||
}
|
||||
|
||||
static mxs::SSLContext* create_ssl(const char* name,
|
||||
const char* key,
|
||||
const char* cert,
|
||||
const char* ca,
|
||||
const char* version,
|
||||
const char* depth,
|
||||
const char* verify)
|
||||
static std::unique_ptr<mxs::SSLContext> create_ssl(const char* name,
|
||||
const char* key,
|
||||
const char* cert,
|
||||
const char* ca,
|
||||
const char* version,
|
||||
const char* depth,
|
||||
const char* verify)
|
||||
{
|
||||
mxs::SSLContext* rval = NULL;
|
||||
std::unique_ptr<mxs::SSLContext> rval;
|
||||
CONFIG_CONTEXT* obj = config_context_create(name);
|
||||
|
||||
if (obj)
|
||||
|
@ -135,14 +135,14 @@ void config_remove_param(CONFIG_CONTEXT* obj, const char* name);
|
||||
* @param name Name of object being created (usually server or listener name)
|
||||
* @param params Parameters to create SSL from
|
||||
* @param require_cert Whether certificates are required
|
||||
* @param dest Pointer where initialized SSL structure is stored
|
||||
* @param dest Unique pointer where initialized SSL structure is stored
|
||||
*
|
||||
* @return True on success, false on error
|
||||
*/
|
||||
bool config_create_ssl(const char* name,
|
||||
const MXS_CONFIG_PARAMETER& params,
|
||||
bool require_cert,
|
||||
mxs::SSLContext** dest);
|
||||
std::unique_ptr<mxs::SSLContext>* dest);
|
||||
|
||||
/**
|
||||
* @brief Check if all SSL parameters are defined
|
||||
|
@ -31,8 +31,8 @@ public:
|
||||
Server(const std::string& name,
|
||||
const std::string& protocol = "",
|
||||
const std::string& authenticator = "",
|
||||
mxs::SSLContext* ssl = nullptr)
|
||||
: SERVER(ssl)
|
||||
std::unique_ptr<mxs::SSLContext> ssl = {})
|
||||
: SERVER(std::move(ssl))
|
||||
, m_name(name)
|
||||
{
|
||||
m_settings.protocol = protocol;
|
||||
|
@ -105,7 +105,7 @@ Listener::Listener(SERVICE* service,
|
||||
const std::string& authenticator,
|
||||
const std::string& auth_opts,
|
||||
void* auth_instance,
|
||||
mxs::SSLContext* ssl,
|
||||
std::unique_ptr<mxs::SSLContext> ssl,
|
||||
const MXS_CONFIG_PARAMETER& params)
|
||||
: MXB_POLL_DATA{Listener::poll_handler}
|
||||
, m_name(name)
|
||||
@ -116,7 +116,7 @@ Listener::Listener(SERVICE* service,
|
||||
, m_authenticator(authenticator)
|
||||
, m_auth_options(auth_opts)
|
||||
, m_auth_instance(auth_instance)
|
||||
, m_ssl_context(ssl)
|
||||
, m_ssl_context(std::move(ssl))
|
||||
, m_users(nullptr)
|
||||
, m_service(service)
|
||||
, m_proto_func(*(MXS_PROTOCOL*)load_module(protocol.c_str(), MODULE_PROTOCOL))
|
||||
@ -207,7 +207,7 @@ SListener Listener::create(const std::string& name,
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
mxs::SSLContext* ssl_info = NULL;
|
||||
std::unique_ptr<mxs::SSLContext> ssl_info;
|
||||
|
||||
if (!config_create_ssl(name.c_str(), params, true, &ssl_info))
|
||||
{
|
||||
@ -246,7 +246,8 @@ SListener Listener::create(const std::string& name,
|
||||
mxb_assert(proto_mod && auth_mod);
|
||||
|
||||
SListener listener(new(std::nothrow) Listener(service, name, address, port, protocol, auth,
|
||||
authenticator_options, auth_instance, ssl_info, params));
|
||||
authenticator_options, auth_instance,
|
||||
std::move(ssl_info), params));
|
||||
|
||||
if (listener)
|
||||
{
|
||||
|
@ -195,7 +195,7 @@ Server* Server::server_alloc(const char* name, const MXS_CONFIG_PARAMETER& param
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mxs::SSLContext* ssl = NULL;
|
||||
std::unique_ptr<mxs::SSLContext> ssl;
|
||||
|
||||
if (!config_create_ssl(name, params, false, &ssl))
|
||||
{
|
||||
@ -203,14 +203,13 @@ Server* Server::server_alloc(const char* name, const MXS_CONFIG_PARAMETER& param
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Server* server = new(std::nothrow) Server(name, protocol, authenticator, ssl);
|
||||
Server* server = new(std::nothrow) Server(name, protocol, authenticator, std::move(ssl));
|
||||
DCB** persistent = (DCB**)MXS_CALLOC(config_threadcount(), sizeof(*persistent));
|
||||
|
||||
if (!server || !persistent)
|
||||
{
|
||||
delete server;
|
||||
MXS_FREE(persistent);
|
||||
delete ssl;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
@ -200,7 +200,7 @@ namespace maxscale
|
||||
{
|
||||
|
||||
// static
|
||||
SSLContext* SSLContext::create(const MXS_CONFIG_PARAMETER& params)
|
||||
std::unique_ptr<SSLContext> SSLContext::create(const MXS_CONFIG_PARAMETER& params)
|
||||
{
|
||||
mxb_assert(access(params.get_string(CN_SSL_CA_CERT).c_str(), F_OK) == 0);
|
||||
mxb_assert(params.get_string(CN_SSL_CERT).empty()
|
||||
@ -208,18 +208,17 @@ SSLContext* SSLContext::create(const MXS_CONFIG_PARAMETER& params)
|
||||
mxb_assert(params.get_string(CN_SSL_KEY).empty()
|
||||
|| access(params.get_string(CN_SSL_KEY).c_str(), F_OK) == 0);
|
||||
|
||||
SSLContext* ssl = new(std::nothrow) SSLContext(params.get_string(CN_SSL_KEY),
|
||||
params.get_string(CN_SSL_CERT),
|
||||
params.get_string(CN_SSL_CA_CERT),
|
||||
(ssl_method_type_t)params.get_enum(CN_SSL_VERSION,
|
||||
ssl_version_values),
|
||||
params.get_integer(CN_SSL_CERT_VERIFY_DEPTH),
|
||||
params.get_bool(CN_SSL_VERIFY_PEER_CERTIFICATE));
|
||||
std::unique_ptr<SSLContext> ssl(
|
||||
new(std::nothrow) SSLContext(params.get_string(CN_SSL_KEY),
|
||||
params.get_string(CN_SSL_CERT),
|
||||
params.get_string(CN_SSL_CA_CERT),
|
||||
(ssl_method_type_t)params.get_enum(CN_SSL_VERSION, ssl_version_values),
|
||||
params.get_integer(CN_SSL_CERT_VERIFY_DEPTH),
|
||||
params.get_bool(CN_SSL_VERIFY_PEER_CERTIFICATE)));
|
||||
|
||||
if (ssl && !ssl->init())
|
||||
{
|
||||
delete ssl;
|
||||
ssl = nullptr;
|
||||
ssl.reset();
|
||||
}
|
||||
|
||||
return ssl;
|
||||
|
Reference in New Issue
Block a user