Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
MXS-2717 Add code for fetching and storing user account info
The data is not used yet.
  • Loading branch information
ekorh475 committed Oct 31, 2019
1 parent 4c1d632 commit f1c97ed
Show file tree
Hide file tree
Showing 9 changed files with 468 additions and 34 deletions.
3 changes: 2 additions & 1 deletion include/maxscale/protocol/mariadb/protocol_classes.hh
Expand Up @@ -295,7 +295,8 @@ public:

json_t* print_auth_users_json() override;

std::unique_ptr<mxs::UserAccountManager> create_user_data_manager() override;
std::unique_ptr<mxs::UserAccountManager>
create_user_data_manager(const std::string& service_name) override;

private:
std::unique_ptr<mxs::AuthenticatorModule> m_auth_module;
Expand Down
2 changes: 1 addition & 1 deletion include/maxscale/protocol2.hh
Expand Up @@ -110,7 +110,7 @@ public:
*/
virtual json_t* print_auth_users_json() = 0;

virtual std::unique_ptr<UserAccountManager> create_user_data_manager()
virtual std::unique_ptr<UserAccountManager> create_user_data_manager(const std::string& service_name)
{
return nullptr;
}
Expand Down
40 changes: 38 additions & 2 deletions maxutils/maxsql/include/maxsql/sqlite.hh
Expand Up @@ -111,12 +111,18 @@ public:
*/
std::unique_ptr<mxq::QueryResult> query(const std::string& query);

/**
* Prepare a query.
*
* @param query Query string
* @return The prepared statement object or null on failure
*/
std::unique_ptr<SQLiteStmt> prepare(const std::string& query);

private:
using CallbackVoid = int (*)(void* data, int n_columns, char** rows, char** field_names);
bool exec_impl(const std::string& sql, CallbackVoid cb, void* cb_data);

std::unique_ptr<SQLiteStmt> prepare(const std::string& query);

sqlite3* m_dbhandle {nullptr};
std::string m_errormsg;
int m_errornum {0};
Expand All @@ -138,6 +144,8 @@ public:
*/
bool step();

bool step_execute();

int column_count() const;

/**
Expand All @@ -147,6 +155,27 @@ public:
*/
bool reset();

int bind_parameter_index(const std::string& name);

/**
* Bind a string value to a parameter placeholder.
*
* @param ind Parameter index. Valid values start from 1.
* @param value Parameter value
*/
bool bind_string(int ind, const std::string& value);

/**
* Bind an integer value to a parameter placeholder.
*
* @param name Parameter name
* @param ind Parameter index. Valid values start from 1.
* @return
*/
bool bind_int(int ind, int value);

bool bind_bool(int ind, bool value);

std::vector<std::string> column_names() const;

/**
Expand All @@ -157,6 +186,13 @@ public:
*/
void row_cstr(const unsigned char* output[]);

/**
* Get latest error.
*
* @return Error string
*/
const char* error() const;

private:
sqlite3_stmt* m_stmt {nullptr};
int m_errornum {0};
Expand Down
66 changes: 64 additions & 2 deletions maxutils/maxsql/src/sqlite.cc
Expand Up @@ -128,9 +128,23 @@ bool SQLiteStmt::step()
return ret == SQLITE_ROW;
}

bool SQLiteStmt::step_execute()
{
int ret = sqlite3_step(m_stmt);
m_errornum = ret;
return ret == SQLITE_DONE;
}


bool SQLiteStmt::reset()
{
return sqlite3_reset(m_stmt) == SQLITE_OK && sqlite3_clear_bindings(m_stmt) == SQLITE_OK;
int ret = sqlite3_reset(m_stmt);
if (ret == SQLITE_OK)
{
ret = sqlite3_clear_bindings(m_stmt);
}
m_errornum = ret;
return ret == SQLITE_OK;
}

int SQLiteStmt::column_count() const
Expand Down Expand Up @@ -159,6 +173,52 @@ std::vector<std::string> SQLiteStmt::column_names() const
return rval;
}

int SQLiteStmt::bind_parameter_index(const std::string& name)
{
// Parameter names must start with ":" or "@". Add ":" if none found.
int rval = 0; // Valid indexes start at 1.
if (!name.empty())
{
auto front = name.front();
if (front != ':' && front != '@')
{
string fixedname = ":" + name;
rval = sqlite3_bind_parameter_index(m_stmt, fixedname.c_str());
}
else
{
rval = sqlite3_bind_parameter_index(m_stmt, name.c_str());
}
}
return rval;
}

bool SQLiteStmt::bind_string(int ind, const string& value)
{
int ret = sqlite3_bind_text(m_stmt, ind, value.c_str(), value.size(), nullptr);
m_errornum = ret;
return ret == SQLITE_OK;
}

bool SQLiteStmt::bind_int(int ind, int value)
{
int ret = sqlite3_bind_int(m_stmt, ind, value);
m_errornum = ret;
return ret == SQLITE_OK;
}

bool SQLiteStmt::bind_bool(int ind, bool value)
{
int ret = sqlite3_bind_int(m_stmt, ind, value ? 1 : 0);
m_errornum = ret;
return ret == SQLITE_OK;
}

const char* SQLiteStmt::error() const
{
return sqlite3_errstr(m_errornum);
}

std::unique_ptr<mxq::QueryResult> SQLite::query(const std::string& query)
{
using mxq::QueryResult;
Expand Down Expand Up @@ -199,10 +259,12 @@ std::unique_ptr<SQLiteStmt> SQLite::prepare(const std::string& query)
if (ret == SQLITE_OK)
{
// Compilation succeeded.
if (tail == nullptr)
if (!*tail)
{
rval.reset(new(std::nothrow) SQLiteStmt(new_stmt));
new_stmt = nullptr;
m_errormsg.clear();
m_errornum = 0;
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion server/core/listener.cc
Expand Up @@ -267,7 +267,7 @@ SListener Listener::create(const std::string& name,
std::unique_ptr<mxs::UserAccountManager> new_user_manager;
if (protocol_module->capabilities() & mxs::ProtocolModule::CAP_AUTHDATA)
{
new_user_manager = protocol_module->create_user_data_manager();
new_user_manager = protocol_module->create_user_data_manager(service->name());
if (new_user_manager)
{
auto service_usermanager = service->user_account_manager();
Expand Down
3 changes: 3 additions & 0 deletions server/core/service.cc
Expand Up @@ -1927,6 +1927,9 @@ void Service::set_user_account_manager(SAccountManager user_manager)
mxb_assert(!m_usermanager_exists.load(std::memory_order_acquire) && !m_usermanager);
m_usermanager = std::move(user_manager);
m_usermanager_exists.store(true, std::memory_order_release);
// TODO: update account manager settings whenever the matching service settings are updated
m_usermanager->set_credentials(m_config->user, m_config->password);
m_usermanager->set_backends(m_data->servers);
m_usermanager->start();
}

Expand Down
5 changes: 3 additions & 2 deletions server/modules/protocol/MariaDB/mariadb_client.cc
Expand Up @@ -2067,9 +2067,10 @@ json_t* MySQLProtocolModule::print_auth_users_json()
return m_auth_module->diagnostics_json();
}

std::unique_ptr<mxs::UserAccountManager> MySQLProtocolModule::create_user_data_manager()
std::unique_ptr<mxs::UserAccountManager>
MySQLProtocolModule::create_user_data_manager(const std::string& service_name)
{
return std::unique_ptr<mxs::UserAccountManager>(new MariaDBUserManager());
return std::unique_ptr<mxs::UserAccountManager>(new MariaDBUserManager(service_name));
}

MariaDBClientConnection::MariaDBClientConnection(MXS_SESSION* session, mxs::Component* component,
Expand Down

0 comments on commit f1c97ed

Please sign in to comment.