diff --git a/EXE_Redis.cpp b/EXE_Redis.cpp index 7d88629..1830f17 100644 --- a/EXE_Redis.cpp +++ b/EXE_Redis.cpp @@ -35,8 +35,14 @@ using namespace std ; // --------------------------------------------------------------------------- // Definizione variabili generali Redis // --------------------------------------------------------------------------- -static int REDIS_MIN_DB = 0 ; -static int REDIS_MAX_DB = 15 ; +static const int REDIS_MIN_DB = 0 ; +static const int REDIS_MAX_DB = 15 ; +static const int DEFAULT_PORT = 6379 ; +static const int DEFAULT_SENTINEL_MASTER_PORT = 26379 ; +static const string DEFAULT_USER = "default" ; +static const int DEFAULT_DATABASE = 0 ; +static const int DEFAULT_TIMEOUT = 15000 ; // [ms] +static const int DEFAULT_ALIVE_TIME = 180 ; // [s] // Contesto Sincrono static redisContext* s_pRedisContext = nullptr ; @@ -45,6 +51,7 @@ static redisAsyncContext* s_pRedisAsyncSubContext = nullptr ; static redisAsyncContext* s_pRedisAsyncPubContext = nullptr ; static atomic s_nPendingDataBase = REDIS_MIN_DB ; // Default static string s_sPassword = "" ; // Default +static string s_sUser = DEFAULT_USER ; // Default static atomic s_bSubConnected = false ; static atomic s_bPubConnected = false ; static atomic s_bPubLoopRunning = false ; @@ -61,27 +68,22 @@ static const string KEY_TYPE_ZSET = "zset" ; // non usato static const string KEY_TYPE_JSON = "ReJSON-RL" ; // non usato, disponibile se versione >= 8 ( noi usiamo la 6) // struttura per parametri della stringa di Connessione -static const int DEFAULT_PORT = 6379 ; -static const int DEFAULT_SENTINEL_MASTER_PORT = 26379 ; -static const int DEFAULT_DATABASE = 0 ; -static const int DEFAULT_TIMEOUT = 15000 ; // [ms] -static const int DEFAULT_ALIVE_TIME = 180 ; // [s] struct RedisConnectionInfo { - string sHost, sServiceName, sPassword ; + string sHost, sServiceName, sUser, sPassword ; int nPort, nDefaultDataBase, nKeepAlive, nConnectTimeout, nSyncTimeout, nAsyncTimeout ; bool bAbortConnect, bSsl, bAllowAdmin ; RedisConnectionInfo() { - sHost = "" ; sServiceName = "" ; sPassword = "" ; + sHost = "" ; sServiceName = "" ; sUser = DEFAULT_USER ; sPassword = "" ; nPort = DEFAULT_PORT ; nDefaultDataBase = DEFAULT_DATABASE ; nKeepAlive = DEFAULT_ALIVE_TIME ; nConnectTimeout = DEFAULT_TIMEOUT ; nSyncTimeout = DEFAULT_TIMEOUT ; nAsyncTimeout = DEFAULT_TIMEOUT ; bAbortConnect = false ; bSsl = false ; bAllowAdmin = false ; } ; - RedisConnectionInfo( string sMyHost, string sMyServiceName, string sMyPassword, int nMyPort, + RedisConnectionInfo( string sMyHost, string sMyServiceName, string sMyUser, string sMyPassword, int nMyPort, int nMyDefaultDataBase, int dMyKeepAlive, int dMyConnectTimeout, int dMySyncTimeout, int dMyAsyncTimeout, bool bMyAbortConnect, bool bMySsl, bool bMyAllowAdmin) - : sHost( sMyHost), sServiceName( sMyServiceName), sPassword( sMyPassword), nPort( nMyPort), + : sHost( sMyHost), sServiceName( sMyServiceName), sUser( sMyUser), sPassword( sMyPassword), nPort( nMyPort), nDefaultDataBase( nMyDefaultDataBase), nKeepAlive( dMyKeepAlive), nConnectTimeout( dMyConnectTimeout), nSyncTimeout( dMySyncTimeout), nAsyncTimeout( dMyAsyncTimeout), bAbortConnect( bMyAbortConnect), bSsl( bMySsl), bAllowAdmin( bMyAllowAdmin) {} @@ -124,6 +126,7 @@ GetParamsFromConnectionString( const string& sConnection, RedisConnectionInfo& C // Recupero i parametri ( rimuovo tutti gli spazi ) sParams.erase( remove( sParams.begin(), sParams.end(), ' '), sParams.end()) ; GetValInNotes( sParams, "serviceName", ",", ConnectionInfo.sServiceName) ; + GetValInNotes( sParams, "user", ",", ConnectionInfo.sUser) ; GetValInNotes( sParams, "password", ",", ConnectionInfo.sPassword) ; GetValInNotes( sParams, "DefaultDatabase", ",", ConnectionInfo.nDefaultDataBase) ; GetValInNotes( sParams, "keepAlive", ",", ConnectionInfo.nKeepAlive) ; @@ -233,7 +236,7 @@ ExeRedisConnect( const string& sConnection) // Verifico se richiesta autenticazione if ( ! ConnectionInfo.sPassword.empty()) { - redisReply* reply = ( redisReply*)redisCommand( s_pRedisContext, "AUTH %s", ConnectionInfo.sPassword.c_str()) ; + redisReply* reply = ( redisReply*)redisCommand( s_pRedisContext, "AUTH %s %s", ConnectionInfo.sUser.c_str(), ConnectionInfo.sPassword.c_str()) ; if ( reply == nullptr || reply->type == REDIS_REPLY_ERROR) { LOG_INFO(GetCmdLogger(), "Error : Authentication failed") ; if ( reply == nullptr) @@ -638,10 +641,28 @@ GetPendingDataBase() return s_nPendingDataBase ; } +// --------------------------------------------------------------------------- +// Funzione per Memorizzare l'utente di autenticazione [Callback] +// --------------------------------------------------------------------------- +static void +SetPendingUser( string sUsr) +{ + s_sUser = sUsr ; +} + +// -------------------------------------------------------------------------- +// Funzione per ottenere l'utente di autenticazione [Callback] +// -------------------------------------------------------------------------- +static string +GetPendingUser() +{ + return s_sUser ; +} + // --------------------------------------------------------------------------- // Funzione per Memorizzare la Password di autenticazione [Callback] // --------------------------------------------------------------------------- -static bool +static void SetPendingPassword( string sPsw) { s_sPassword = sPsw ; @@ -709,8 +730,10 @@ ExeRedisAsyncConnect( const string& sConnection) if ( s_pRedisAsyncPubContext != nullptr || s_pRedisAsyncSubContext != nullptr) ExeRedisAsyncDisconnect() ; - // Memorizzo il valore del DataBase, la selezione del DataBase deve avvenire una volta - // che la connessione sia effettivamente stabilita e corretta ( -> CallBack di connessione) + // Memorizzo il valore dello User, della Password e del DataBase, serviranno una volta + // che la connessione รจ effettivamente stabilita e corretta ( -> CallBack di connessione) + SetPendingUser( ConnectionInfo.sUser) ; + SetPendingPassword( ConnectionInfo.sPassword) ; if ( ! SetPendingDataBase( ConnectionInfo.nDefaultDataBase)) { LOG_INFO( GetCmdLogger(), "Error : DataBase number must be in range [0,15]") ; return false ; @@ -796,7 +819,7 @@ ExeRedisAsyncConnect( const string& sConnection) nullptr, "SELECT %d", GetPendingDataBase() ) ; }, - nullptr, "AUTH %s", GetPendingPassword().c_str() + nullptr, "AUTH %s %s", GetPendingUser().c_str(), GetPendingPassword().c_str() ) ; } // Se nessuna autenticazione -> SELECT @@ -857,32 +880,7 @@ ExeRedisAsyncConnect( const string& sConnection) nullptr, "SELECT %d", GetPendingDataBase() ) ; }, - nullptr, "SELECT %d", GetPendingDataBase() - ) ; - // --- Autenticazione richiesta - redisAsyncCommand( - s_pRedisAsyncSubContext, - []( redisAsyncContext* ctx, void* r, void*) { - redisReply* reply = static_cast( r) ; - if ( reply == nullptr) { - // --- Nulla - LOG_INFO( GetCmdLogger(), "Error : Null Authentication reply") ; - return ; - } - if ( reply->type == REDIS_REPLY_STATUS && strcmp( reply->str, "OK") == 0) { - // --- Valida - LOG_INFO( GetCmdLogger(), "Valid Authentication for Pub connection") - s_bPubConnected = true ; - return ; - } - else { - // --- Errore - string sErrMsg = ( reply->str != nullptr ? reply->str : "Null asnwer") ; - LOG_INFO( GetCmdLogger(), ( string{ "Error : Authentication -> "} + sErrMsg).c_str()) - return ; - } - }, - nullptr, "AUTH %s", GetPendingPassword().c_str() + nullptr, "AUTH %s %s", GetPendingUser().c_str(), GetPendingPassword().c_str() ) ; } // Se nessuna autenticazione -> SELECT