1
0
mirror of https://github.com/mariadb-corporation/mariadb-columnstore-engine.git synced 2025-07-29 08:21:15 +03:00

MCOL-497 securing mysql connection with TLS in crossengine and querystats code.

This commit is contained in:
Roman Nozdrin
2017-12-19 08:21:55 +03:00
parent b795e6b781
commit fbd081dbf1
11 changed files with 302 additions and 221 deletions

View File

@ -0,0 +1,13 @@
include_directories( ${ENGINE_COMMON_INCLUDES} )
########### next target ###############
set(libmysql_client_LIB_SRCS libmysql_client.cpp)
add_library(libmysql_client SHARED ${libmysql_client_LIB_SRCS})
set_target_properties(libmysql_client PROPERTIES VERSION 1.0.0 SOVERSION 1)
install(TARGETS libmysql_client DESTINATION ${ENGINE_LIBDIR} COMPONENT libs)

View File

@ -0,0 +1,144 @@
/* Copyright (C) 2014 InfiniDB, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; version 2 of
the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA. */
#include <unistd.h>
//#define NDEBUG
#include <cassert>
#include <sstream>
#include <iomanip>
using namespace std;
#include "idberrorinfo.h"
using namespace logging;
#include "liboamcpp.h"
#include "libmysql_client.h"
namespace utils
{
LibMySQL::LibMySQL() : fCon(NULL), fRes(NULL)
{
}
LibMySQL::~LibMySQL()
{
if (fRes)
{
mysql_free_result(fRes);
}
fRes = NULL;
if (fCon)
{
mysql_close(fCon);
}
fCon = NULL;
}
int LibMySQL::init(const char* h, unsigned int p, const char* u, const char* w, const char* d)
{
int ret = 0;
fCon = mysql_init(NULL);
oam::Oam oam;
oam::oamModuleInfo_t moduleInfo;
moduleInfo = oam.getModuleInfo();
string moduleName = boost::get<0>(moduleInfo);
int serverTypeInstall = boost::get<5>(moduleInfo);
// This is single server installation so use um1 instead of pm1.
if ( serverTypeInstall == 2 )
moduleName.assign("um1");
oam::ModuleConfig moduleconfig;
oam.getSystemConfig(moduleName, moduleconfig);
if (!(moduleconfig.TLSCA.empty() || moduleconfig.TLSClientCert.empty() || moduleconfig.TLSClientKey.empty()))
{
mysql_ssl_set(fCon, moduleconfig.TLSClientKey.c_str(), moduleconfig.TLSClientCert.c_str(),
moduleconfig.TLSCA.c_str(), NULL, NULL);
}
if (fCon != NULL)
{
unsigned int tcp_option = MYSQL_PROTOCOL_TCP;
mysql_options(fCon, MYSQL_OPT_PROTOCOL, &tcp_option);
if (mysql_real_connect(fCon, h, u, w, d, p, NULL, 0) == NULL)
{
fErrStr = "fatal error running mysql_real_connect() in libmysql_client lib";
ret = mysql_errno(fCon);
}
else
{
mysql_set_character_set(fCon, "utf8");
}
}
else
{
fErrStr = "fatal error running mysql_init() in libmysql_client lib";
ret = -1;
}
return ret;
}
int LibMySQL::run(const char* query)
{
int ret = 0;
if (mysql_real_query(fCon, query, strlen(query)) != 0)
{
fErrStr = "fatal error runing mysql_real_query() in libmysql_client lib";
ret = -1;
}
fRes = mysql_use_result(fCon);
if (fRes == NULL)
{
fErrStr = "fatal error running mysql_use_result() or empty result set in libmysql_client lib";
ret = -1;
}
return ret;
}
void LibMySQL::handleMySqlError(const char* errStr, unsigned int errCode)
{
ostringstream oss;
oss << errStr << "(" << errCode << ")";
if (errCode == (unsigned int) - 1)
oss << "(null pointer)";
else
oss << "(" << errCode << ")";
throw IDBExcept(oss.str(), ERR_CROSS_ENGINE_CONNECT);
return;
}
} // namespace

View File

@ -0,0 +1,89 @@
/* Copyright (C) 2014 InfiniDB, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; version 2 of
the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA. */
#ifndef UTILS_LIBMYSQL_CL_H
#define UTILS LIBMYSQL_CL_H
#include <my_config.h>
#include <mysql.h>
#include <boost/scoped_array.hpp>
namespace utils
{
class LibMySQL
{
public:
LibMySQL();
~LibMySQL();
// init: host port username passwd db
int init(const char*, unsigned int, const char*, const char*, const char*);
// run the query
int run(const char* q);
void handleMySqlError(const char*, unsigned int);
MYSQL* getMySqlCon()
{
return fCon;
}
int getFieldCount()
{
return mysql_num_fields(fRes);
}
int getRowCount()
{
return mysql_num_rows(fRes);
}
char** nextRow()
{
char** row = mysql_fetch_row(fRes);
fieldLengths = mysql_fetch_lengths(fRes);
fFields = mysql_fetch_fields(fRes);
return row;
}
long getFieldLength(int field)
{
return fieldLengths[field];
}
MYSQL_FIELD* getField(int field)
{
return &fFields[field];
}
const std::string& getError()
{
return fErrStr;
}
private:
MYSQL* fCon;
MYSQL_RES* fRes;
MYSQL_FIELD* fFields;
std::string fErrStr;
unsigned long* fieldLengths;
};
} // namespace
#endif // UTILS_LIBMYSQL_CL_H
// vim:ts=4 sw=4: