diff options
author | Parth Chandra <pchandra@maprtech.com> | 2015-04-03 16:32:32 -0700 |
---|---|---|
committer | Parth Chandra <pchandra@maprtech.com> | 2015-04-05 11:40:46 -0700 |
commit | 862ab91e93f56469c05412822ea308bba783d879 (patch) | |
tree | a023208f268b00da4fb39489ce37fca3ffea2316 /contrib/native/client/src/clientlib | |
parent | 4f213570f29a30c8609afacba0ca01cc33cdc7d0 (diff) |
DRILL-2672: C++ Client - Add support for authentication
Diffstat (limited to 'contrib/native/client/src/clientlib')
5 files changed, 56 insertions, 12 deletions
diff --git a/contrib/native/client/src/clientlib/drillClient.cpp b/contrib/native/client/src/clientlib/drillClient.cpp index 90aa55510..7162f63d1 100644 --- a/contrib/native/client/src/clientlib/drillClient.cpp +++ b/contrib/native/client/src/clientlib/drillClient.cpp @@ -122,7 +122,7 @@ logLevel_t DrillClientConfig::getLogLevel(){ //Using boost assign to initialize maps. const std::map<std::string, uint32_t> DrillUserProperties::USER_PROPERTIES=boost::assign::map_list_of - ( USERPROP_USERNAME, USERPROP_FLAGS_SERVERPROP|USERPROP_FLAGS_STRING ) + ( USERPROP_USERNAME, USERPROP_FLAGS_SERVERPROP|USERPROP_FLAGS_USERNAME|USERPROP_FLAGS_STRING ) ( USERPROP_PASSWORD, USERPROP_FLAGS_SERVERPROP|USERPROP_FLAGS_PASSWORD) ( USERPROP_SCHEMA, USERPROP_FLAGS_SERVERPROP|USERPROP_FLAGS_STRING) ( USERPROP_USESSL, USERPROP_FLAGS_BOOLEAN|USERPROP_FLAGS_SSLPROP) diff --git a/contrib/native/client/src/clientlib/drillClientImpl.cpp b/contrib/native/client/src/clientlib/drillClientImpl.cpp index dce5bdc30..a25382977 100644 --- a/contrib/native/client/src/clientlib/drillClientImpl.cpp +++ b/contrib/native/client/src/clientlib/drillClientImpl.cpp @@ -243,12 +243,15 @@ void DrillClientImpl::handleHandshake(ByteBuf_t _buf, exec::user::BitToUserHandshake b2u; b2u.ParseFromArray(msg.m_pbody.data(), msg.m_pbody.size()); this->m_handshakeVersion=b2u.rpc_version(); + this->m_handshakeStatus=b2u.status(); + this->m_handshakeErrorId=b2u.errorid(); + this->m_handshakeErrorMsg=b2u.errormessage(); }else{ // boost error if(error==boost::asio::error::eof){ // Server broke off the connection - handleConnError(CONN_HANDSHAKE_FAILED, - getMessage(ERR_CONN_NOHSHAKE, DRILL_RPC_VERSION, m_handshakeVersion)); + handleConnError(CONN_HANDSHAKE_FAILED, + getMessage(ERR_CONN_NOHSHAKE, DRILL_RPC_VERSION)); }else{ handleConnError(CONN_FAILURE, getMessage(ERR_CONN_RDFAIL, error.message().c_str())); } @@ -284,12 +287,13 @@ connectionStatus_t DrillClientImpl::validateHandshake(DrillUserProperties* prope u2b.set_support_listening(true); if(properties != NULL && properties->size()>0){ + std::string username; std::string err; if(!properties->validate(err)){ DRILL_LOG(LOG_INFO) << "Invalid user input:" << err << std::endl; } exec::user::UserProperties* userProperties = u2b.mutable_properties(); - + std::map<char,int>::iterator it; for(size_t i=0; i<properties->size(); i++){ std::map<std::string,uint32_t>::const_iterator it=DrillUserProperties::USER_PROPERTIES.find(properties->keyAt(i)); @@ -302,6 +306,13 @@ connectionStatus_t DrillClientImpl::validateHandshake(DrillUserProperties* prope exec::user::Property* connProp = userProperties->add_properties(); connProp->set_key(properties->keyAt(i)); connProp->set_value(properties->valueAt(i)); + //Username(but not the password) also needs to be set in UserCredentials + if(IS_BITSET((*it).second,USERPROP_FLAGS_USERNAME)){ + exec::shared::UserCredentials* creds = u2b.mutable_credentials(); + username=properties->valueAt(i); + creds->set_user_name(username); + //u2b.set_credentials(&creds); + } if(IS_BITSET((*it).second,USERPROP_FLAGS_PASSWORD)){ DRILL_LOG(LOG_INFO) << properties->keyAt(i) << ": ********** " << std::endl; }else{ @@ -324,11 +335,31 @@ connectionStatus_t DrillClientImpl::validateHandshake(DrillUserProperties* prope if(ret!=CONN_SUCCESS){ return ret; } - if(m_handshakeVersion != u2b.rpc_version()) { - DRILL_LOG(LOG_TRACE) << "Invalid rpc version. Expected " - << DRILL_RPC_VERSION << ", actual "<< m_handshakeVersion << "." << std::endl; - return handleConnError(CONN_HANDSHAKE_FAILED, - getMessage(ERR_CONN_NOHSHAKE, DRILL_RPC_VERSION, m_handshakeVersion)); + if(this->m_handshakeStatus != exec::user::SUCCESS){ + switch(this->m_handshakeStatus){ + case exec::user::RPC_VERSION_MISMATCH: + DRILL_LOG(LOG_TRACE) << "Invalid rpc version. Expected " + << DRILL_RPC_VERSION << ", actual "<< m_handshakeVersion << "." << std::endl; + return handleConnError(CONN_HANDSHAKE_FAILED, + getMessage(ERR_CONN_BAD_RPC_VER, DRILL_RPC_VERSION, + m_handshakeVersion, + this->m_handshakeErrorId.c_str(), + this->m_handshakeErrorMsg.c_str())); + case exec::user::AUTH_FAILED: + DRILL_LOG(LOG_TRACE) << "Authentication failed." << std::endl; + return handleConnError(CONN_HANDSHAKE_FAILED, + getMessage(ERR_CONN_AUTHFAIL, + this->m_handshakeErrorId.c_str(), + this->m_handshakeErrorMsg.c_str())); + case exec::user::UNKNOWN_FAILURE: + DRILL_LOG(LOG_TRACE) << "Unknown error during handshake." << std::endl; + return handleConnError(CONN_HANDSHAKE_FAILED, + getMessage(ERR_CONN_UNKNOWN_ERR, + this->m_handshakeErrorId.c_str(), + this->m_handshakeErrorMsg.c_str())); + default: + break; + } } // reset io_service after handshake is validated before running queries m_io_service.reset(); diff --git a/contrib/native/client/src/clientlib/drillClientImpl.hpp b/contrib/native/client/src/clientlib/drillClientImpl.hpp index 95fe92254..04d59c763 100644 --- a/contrib/native/client/src/clientlib/drillClientImpl.hpp +++ b/contrib/native/client/src/clientlib/drillClientImpl.hpp @@ -193,12 +193,13 @@ class DrillClientImpl{ DrillClientImpl(): m_coordinationId(1), m_handshakeVersion(0), + m_handshakeStatus(exec::user::SUCCESS), m_bIsConnected(false), m_pendingRequests(0), m_pError(NULL), m_pListenerThread(NULL), - m_socket(m_io_service), m_pWork(NULL), + m_socket(m_io_service), m_deadlineTimer(m_io_service), m_rbuf(NULL), m_wbuf(MAX_SOCK_RD_BUFSIZE) @@ -307,6 +308,9 @@ class DrillClientImpl{ int32_t m_coordinationId; int32_t m_handshakeVersion; + exec::user::HandshakeStatus m_handshakeStatus; + std::string m_handshakeErrorId; + std::string m_handshakeErrorMsg; bool m_bIsConnected; // number of outstanding read requests. diff --git a/contrib/native/client/src/clientlib/errmsgs.cpp b/contrib/native/client/src/clientlib/errmsgs.cpp index fa7272151..11661f8c4 100644 --- a/contrib/native/client/src/clientlib/errmsgs.cpp +++ b/contrib/native/client/src/clientlib/errmsgs.cpp @@ -32,7 +32,8 @@ static Drill::ErrorMessages errorMessages[]={ {ERR_CONN_RDFAIL, ERR_CATEGORY_CONN, 0, "Connection failed with error: %s."}, {ERR_CONN_WFAIL, ERR_CATEGORY_CONN, 0, "Synchronous socket write failed with error: %s."}, {ERR_CONN_ZOOKEEPER, ERR_CATEGORY_CONN, 0, "Zookeeper error. %s"}, - {ERR_CONN_NOHSHAKE, ERR_CATEGORY_CONN, 0, "Handshake failed: Expected RPC version %d, got %d."}, + {ERR_CONN_NOHSHAKE, ERR_CATEGORY_CONN, 0, "Handshake failed because the server killed the connection. " + "Expected RPC version %d."}, {ERR_CONN_ZKFAIL, ERR_CATEGORY_CONN, 0, "Failed to connect to Zookeeper."}, {ERR_CONN_ZKTIMOUT, ERR_CATEGORY_CONN, 0, "Timed out while waiting to connect."}, {ERR_CONN_ZKERR, ERR_CATEGORY_CONN, 0, "Error in reading from Zookeeper (error code: %d)."}, @@ -41,6 +42,11 @@ static Drill::ErrorMessages errorMessages[]={ {ERR_CONN_ZKNOAUTH, ERR_CATEGORY_CONN, 0, "Authentication failed."}, {ERR_CONN_ZKEXP, ERR_CATEGORY_CONN, 0, "Session expired."}, {ERR_CONN_HSHAKETIMOUT, ERR_CATEGORY_CONN, 0, "Handshake Timeout."}, + {ERR_CONN_BAD_RPC_VER, ERR_CATEGORY_CONN, 0, "Handshake failed because of a RPC version mismatch. " + "Expected RPC version %d, got %d. [Server message was: (%s) %s]"}, + {ERR_CONN_AUTHFAIL, ERR_CATEGORY_CONN, 0, "User authentication failed (please check the username and password)." + "[Server message was: (%s) %s]"}, + {ERR_CONN_UNKNOWN_ERR, ERR_CATEGORY_CONN, 0, "Handshake Failed due to an error on the server. [Server message was: (%s) %s]"}, {ERR_QRY_OUTOFMEM, ERR_CATEGORY_QRY, 0, "Out of memory."}, {ERR_QRY_COMMERR, ERR_CATEGORY_QRY, 0, "Communication error. %s"}, {ERR_QRY_INVREADLEN, ERR_CATEGORY_QRY, 0, "Internal Error: Received a message with an invalid read length."}, diff --git a/contrib/native/client/src/clientlib/errmsgs.hpp b/contrib/native/client/src/clientlib/errmsgs.hpp index 22e544f08..b82efaaa2 100644 --- a/contrib/native/client/src/clientlib/errmsgs.hpp +++ b/contrib/native/client/src/clientlib/errmsgs.hpp @@ -46,7 +46,10 @@ namespace Drill{ #define ERR_CONN_ZKNOAUTH DRILL_ERR_START+13 #define ERR_CONN_ZKEXP DRILL_ERR_START+14 #define ERR_CONN_HSHAKETIMOUT DRILL_ERR_START+15 -#define ERR_CONN_MAX DRILL_ERR_START+15 +#define ERR_CONN_BAD_RPC_VER DRILL_ERR_START+16 +#define ERR_CONN_AUTHFAIL DRILL_ERR_START+17 +#define ERR_CONN_UNKNOWN_ERR DRILL_ERR_START+18 +#define ERR_CONN_MAX DRILL_ERR_START+18 #define ERR_QRY_OUTOFMEM ERR_CONN_MAX+1 #define ERR_QRY_COMMERR ERR_CONN_MAX+2 |