Commit 8d84b2fd authored by Michael Ott's avatar Michael Ott
Browse files

Enable Cassandra authentication

parent 7a109f45
......@@ -210,14 +210,16 @@ void usage() {
cout << endl;
cout << "Options:" << endl;
cout << " -m<host> MQTT listen address [default: " << LISTENHOST << ":" << LISTENPORT << "]" << endl;
cout << " -r<host> REST API listen address [default: " << RESTAPIHOST << ":" << RESTAPIPORT << "]" << endl;
cout << " -c<host> Cassandra host [default: " << CASSANDRAHOST << ":" << CASSANDRAPORT << "]" << endl;
cout << " -t<ttl> Cassandra insert TTL [default: " << TTL << "]" << endl;
cout << " -m<host> MQTT listen address [default: " << LISTENHOST << ":" << LISTENPORT << "]" << endl;
cout << " -r<host> REST API listen address [default: " << RESTAPIHOST << ":" << RESTAPIPORT << "]" << endl;
cout << " -c<host> Cassandra host [default: " << CASSANDRAHOST << ":" << CASSANDRAPORT << "]" << endl;
cout << " -u<username> Cassandra username [default: none]" << endl;
cout << " -p<password> Cassandra password [default: none]" << endl;
cout << " -t<ttl> Cassandra insert TTL [default: " << TTL << "]" << endl;
cout << endl;
cout << " -d Daemonize" << endl;
cout << " -s Print message statistics" << endl;
cout << " -h This help page" << endl;
cout << " -d Daemonize" << endl;
cout << " -s Print message statistics" << endl;
cout << " -h This help page" << endl;
cout << endl;
}
......@@ -239,6 +241,7 @@ int main(int argc, char* const argv[]) {
/* Parse command line */
int ret;
std::string listenHost, cassandraHost, restApiHost, ttl;
std::string cassandraUser, cassandraPassword;
std::string listenPort, cassandraPort, restApiPort;
listenHost = LISTENHOST;
......@@ -246,7 +249,8 @@ int main(int argc, char* const argv[]) {
restApiHost = RESTAPIHOST;
ttl = "0";
statistics = false;
while ((ret=getopt(argc, argv, "l:c:r:t:dDsh"))!=-1) {
while ((ret=getopt(argc, argv, "l:c:u:p:t:r:dDsh"))!=-1) {
switch(ret) {
case 'l':
listenHost = optarg;
......@@ -254,12 +258,24 @@ int main(int argc, char* const argv[]) {
case 'c':
cassandraHost = optarg;
break;
case 'r':
restApiHost = optarg;
case 'u':
cassandraUser = optarg;
break;
case 'p': {
cassandraPassword = optarg;
size_t pwdLen = strlen(optarg);
memset(optarg, 'x', (pwdLen >= 3) ? 3 : pwdLen);
if (pwdLen > 3) {
memset(optarg+3, 0, pwdLen-3);
}
break;
}
case 't':
ttl = optarg;
break;
case 'r':
restApiHost = optarg;
break;
case 'd':
case 'D':
dcdbdaemon();
......@@ -304,7 +320,7 @@ int main(int argc, char* const argv[]) {
* Allocate and initialize connection to Cassandra.
*/
DCDB::Connection* dcdbConn;
dcdbConn = new DCDB::Connection(cassandraHost, atoi(cassandraPort.c_str()));
dcdbConn = new DCDB::Connection(cassandraHost, atoi(cassandraPort.c_str()), cassandraUser, cassandraPassword);
if (!dcdbConn->connect()) {
std::cout << "Cannot connect to Cassandra!" << std::endl;
......
......@@ -90,6 +90,30 @@ public:
*/
uint16_t getPort();
/**
* @brief Set the username for the connection.
* @param username Username for connecting to the Cassandra front end node.
*/
void setUsername(std::string username);
/**
* @brief Return the current username of the connection.
* @return The username for connecting to the Cassandra front end node.
*/
std::string getUsername();
/**
* @brief Set the password for the connection.
* @param password The password for connecting to the Cassandra front end node.
*/
void setPassword(std::string password);
/**
* @brief Return the current password of the connection.
* @return The password for connecting to the Cassandra front end node.
*/
std::string getPassword();
/**
* @brief Establish a connection to the Cassandra database.
* @return True if the connection was successfully established, false otherwise.
......@@ -128,10 +152,15 @@ public:
Connection();
/**
* @brief Construct a Connection to the specific host and port.
* @brief Construct a Connection to the specific host and port without authentication
*/
Connection(std::string hostname, uint16_t port);
/**
* @brief Construct a Connection to the specific host and port and authenticate with given username and password.
*/
Connection(std::string hostname, uint16_t port, std::string username, std::string password);
/**
* @brief Standard destructor for Connections.
*/
......
......@@ -46,6 +46,8 @@ class ConnectionImpl
protected:
std::string hostname_; /**< The hostname of a DB front-end node. */
uint16_t port_; /**< The port of the DB front-end node. */
std::string username_; /**< The username for connecting to the DB front-end. */
std::string password_; /**< The password for connecting to the DB front-end. */
bool connected; /**< Indicates whether a connection has been established. */
CassCluster* cluster; /**< The Cassandra Cluster object (contains hostname, port, etc) */
......@@ -137,6 +139,26 @@ public:
*/
uint16_t getPort();
/**
* @brief The implementation function of Connection::setHostname().
*/
void setUsername(std::string username);
/**
* @brief The implementation function of Connection::getHostname().
*/
std::string getUsername();
/**
* @brief The implementation function of Connection::setHostname().
*/
void setPassword(std::string password);
/**
* @brief The implementation function of Connection::getHostname().
*/
std::string getPassword();
/**
* @brief The implementation function of Connection::connect().
*/
......
......@@ -62,6 +62,22 @@ uint16_t Connection::getPort() {
return impl->getPort();
}
void Connection::setUsername(std::string username) {
impl->setUsername(username);
}
std::string Connection::getUsername() {
return impl->getUsername();
}
void Connection::setPassword(std::string password) {
impl->setHostname(password);
}
std::string Connection::getPassword() {
return impl->getPassword();
}
bool Connection::connect() {
return impl->connect();
}
......@@ -93,6 +109,14 @@ Connection::Connection(std::string hostname, uint16_t port) {
impl->setPort(port);
}
Connection::Connection(std::string hostname, uint16_t port, std::string username, std::string password) {
impl = new ConnectionImpl();
impl->setHostname(hostname);
impl->setPort(port);
impl->setUsername(username);
impl->setPassword(password);
}
Connection::~Connection() {
delete impl;
}
......@@ -277,6 +301,24 @@ uint16_t ConnectionImpl::getPort() {
return port_;
}
void ConnectionImpl::setUsername(std::string username) {
if (!connected)
username_ = username;
}
std::string ConnectionImpl::getUsername() {
return username_;
}
void ConnectionImpl::setPassword(std::string password) {
if (!connected)
password_ = password;
}
std::string ConnectionImpl::getPassword() {
return password_;
}
/**
* @details
* This function connects to the selected Cassandra
......@@ -289,6 +331,9 @@ bool ConnectionImpl::connect() {
/* Set hostname and port */
cass_cluster_set_contact_points(cluster, hostname_.c_str());
cass_cluster_set_port(cluster, port_);
if (username_.size() && password_.size()) {
cass_cluster_set_credentials(cluster, username_.c_str(), password_.c_str());
}
/* Force protcol version to 1 */
cass_cluster_set_protocol_version(cluster, 1);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment