From 80f47bd146cb2a96395e8753daef9444f984396c Mon Sep 17 00:00:00 2001 From: Mark Friedrich Date: Sat, 7 Mar 2020 10:01:31 +0100 Subject: [PATCH] - Improved _Redis_ authentication. Added support for password param, closed #897 --- app/Controller/Setup.php | 40 +++++++++++++++++++++++++------- app/Lib/Api/AbstractClient.php | 3 +++ app/Lib/Config.php | 2 +- public/templates/view/setup.html | 8 +++++++ 4 files changed, 43 insertions(+), 10 deletions(-) diff --git a/app/Controller/Setup.php b/app/Controller/Setup.php index 5ec156d3..72db2a7f 100644 --- a/app/Controller/Setup.php +++ b/app/Controller/Setup.php @@ -712,10 +712,19 @@ class Setup extends Controller { // collection of DSN specific $conf array (host, port, db,..) $dsnData = []; + /** + * @param int $dbNum + * @param string $tag + * @return string + */ + $getDbLabel = function(int $dbNum, string $tag) : string { + return ' db(' . $dbNum . ') : ' . $tag; + }; + /** * get client information for a Redis client * @param \Redis $client - * @param array $conf + * @param array $conf * @return array */ $getClientInfo = function(\Redis $client, array $conf) : array { @@ -739,7 +748,7 @@ class Setup extends Controller { $getClientStats = function(\Redis $client) use ($f3) : array { $redisStats = []; - if($client->isConnected()){ + if($client->isConnected() && !$client->getLastError()){ $redisServerInfo = (array)$client->info('SERVER'); $redisClientsInfo = (array)$client->info('CLIENTS'); $redisMemoryInfo = (array)$client->info('MEMORY'); @@ -812,14 +821,14 @@ class Setup extends Controller { * @param string $tag * @return array */ - $getDatabaseStatus = function(\Redis $client, string $tag) : array { + $getDatabaseStatus = function(\Redis $client, string $tag) use ($getDbLabel) : array { $redisDatabases = []; - if($client->isConnected()){ + if($client->isConnected() && !$client->getLastError()){ $dbNum = $client->getDbNum(); $dbSize = $client->dbSize(); $redisDatabases = [ 'db_' . $dbNum => [ - 'label' => ' db(' . $dbNum . ') : ' . $tag, + 'label' => $getDbLabel($dbNum, $tag), 'version' => $dbSize . ' keys', 'check' => $dbSize > 0, 'tooltip' => 'Keys in db(' . $dbNum . ')', @@ -847,23 +856,28 @@ class Setup extends Controller { * build (modify) $redisConfig with DNS $conf data * @param array $conf */ - $buildRedisConfig = function(array $conf) use (&$redisConfig, $getClientInfo, $getClientStats, $getDatabaseStatus){ + $buildRedisConfig = function(array $conf) use (&$redisConfig, $getDbLabel, $getClientInfo, $getClientStats, $getDatabaseStatus){ if($conf['type'] == 'redis'){ // is Redis -> group all DNS by host:port - $client = new \Redis(); + $uid = $conf['host'] . ':' . $conf['port']; + $client = new \Redis(); try{ $client->pconnect($conf['host'], $conf['port'], 0.3); + if(!empty($conf['auth'])){ + $client->auth($conf['auth']); + } + if(isset($conf['db'])) { $client->select($conf['db']); } $conf['db'] = $client->getDbNum(); }catch(\RedisException $e){ - // connection failed + // connection failed, getLastError() is called further down } - if(!array_key_exists($uid = $conf['host'] . ':' . $conf['port'], $redisConfig)){ + if(!array_key_exists($uid, $redisConfig)){ $redisConfig[$uid] = $getClientInfo($client, $conf); $redisConfig[$uid]['status'] = $getClientStats($client) + $getDatabaseStatus($client, $conf['tag']); }elseif(!array_key_exists($uidDb = 'db_' . $conf['db'], $redisConfig[$uid]['status'])){ @@ -872,6 +886,13 @@ class Setup extends Controller { $redisConfig[$uid]['status'][$uidDb]['label'] .= '; ' . $conf['tag']; } + if($error = $client->getLastError()){ + $redisConfig[$uid]['errors'][] = [ + 'label' => $getDbLabel((int)$conf['db'], $conf['tag']), + 'error' => $error + ]; + } + $client->close(); } }; @@ -903,6 +924,7 @@ class Setup extends Controller { 'host' => $parts['host'], 'port' => $parts['port'], 'db' => !empty($params['database']) ? (int)$params['database'] : 0, + 'auth' => !empty($params['auth']) ? $params['auth'] : null, 'tag' => 'SESSION' ]; $dsnData[] = $conf; diff --git a/app/Lib/Api/AbstractClient.php b/app/Lib/Api/AbstractClient.php index 4a2ce932..6579fa52 100644 --- a/app/Lib/Api/AbstractClient.php +++ b/app/Lib/Api/AbstractClient.php @@ -134,6 +134,9 @@ abstract class AbstractClient extends \Prefab { Config::REDIS_OPT_READ_TIMEOUT ) ){ + if(!empty($poolConfig['auth'])){ + $client->auth($poolConfig['auth']); + } if(isset($poolConfig['tag'])){ $name = 'pathfinder|php|tag:' . strtolower($poolConfig['tag']) . '|pid:' . getmypid(); diff --git a/app/Lib/Config.php b/app/Lib/Config.php index e7659ed0..3938915e 100644 --- a/app/Lib/Config.php +++ b/app/Lib/Config.php @@ -596,7 +596,7 @@ class Config extends \Prefab { if($matches = (bool)preg_match('/^(\w+)\h*=\h*(.+)/', strtolower(trim($dsn)), $parts)){ $conf['type'] = $parts[1]; if($conf['type'] == 'redis'){ - [$conf['host'], $conf['port'], $conf['db']] = explode(':', $parts[2]) + [1 => 6379, 2 => null]; + [$conf['host'], $conf['port'], $conf['db'], $conf['auth']] = explode(':', $parts[2]) + [1 => 6379, 2 => null, 3 => null]; }elseif($conf['type'] == 'folder'){ $conf['folder'] = $parts[2]; } diff --git a/public/templates/view/setup.html b/public/templates/view/setup.html index 85ee08bc..30c3ca32 100644 --- a/public/templates/view/setup.html +++ b/public/templates/view/setup.html @@ -963,6 +963,14 @@ + + + {{ @redisError.label | raw}}: {{ @redisError.error }} + error + + + {{ @tplCounter('increment', 'cache_danger') }} +