From 5465bd8bd09684fb137f66a1453c80c2d9f78c98 Mon Sep 17 00:00:00 2001 From: Jakub Onderka Date: Sat, 8 Oct 2022 18:16:54 +0200 Subject: [PATCH] chg: [internal] Cleanup Redis code --- app/Console/Command/AdminShell.php | 47 ++++++++---------------------- app/Lib/Tools/RedisTool.php | 23 +++++++++++++++ app/Model/Correlation.php | 3 +- app/Model/CorrelationExclusion.php | 8 ++--- app/Model/Warninglist.php | 28 +++++++++--------- 5 files changed, 54 insertions(+), 55 deletions(-) diff --git a/app/Console/Command/AdminShell.php b/app/Console/Command/AdminShell.php index 0c935b1b8..515054b6f 100644 --- a/app/Console/Command/AdminShell.php +++ b/app/Console/Command/AdminShell.php @@ -537,8 +537,7 @@ class AdminShell extends AppShell public function redisReady() { try { - $redis = $this->Server->setupRedisWithException(); - $redis->ping(); + RedisTool::init()->ping(); $this->out('Successfully connected to Redis.'); } catch (Exception $e) { $this->error('Redis connection is not available', $e->getMessage()); @@ -979,36 +978,14 @@ class AdminShell extends AppShell $this->out(__('New encryption key "%s" saved into config file.', $new)); } - /** - * @param Redis $redis - * @param string $prefix - * @return array[int, int] - */ - private function redisSize($redis, $prefix) - { - $keyCount = 0; - $size = 0; - $it = null; - while ($keys = $redis->scan($it, $prefix, 1000)) { - $redis->pipeline(); - foreach ($keys as $key) { - $redis->rawCommand("memory", "usage", $key); - } - $result = $redis->exec(); - $keyCount += count($keys); - $size += array_sum($result); - } - return [$keyCount, $size]; - } - public function redisMemoryUsage() { - $redis = $this->Server->setupRedisWithException(); + $redis = RedisTool::init(); $redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY); $output = []; - list($count, $size) = $this->redisSize($redis, 'misp:feed_cache:*'); + list($count, $size) = RedisTool::sizeByPrefix($redis, 'misp:feed_cache:*'); $output['feed_cache_count'] = $count; $output['feed_cache_size'] = $size; @@ -1029,7 +1006,7 @@ class AdminShell extends AppShell } } - list($count, $size) = $this->redisSize($redis, 'misp:server_cache:*'); + list($count, $size) = RedisTool::sizeByPrefix($redis, 'misp:server_cache:*'); $output['server_cache_count'] = $count; $output['server_cache_size'] = $size; @@ -1050,35 +1027,35 @@ class AdminShell extends AppShell } } - list($count, $size) = $this->redisSize($redis, 'misp:wlc:*'); + list($count, $size) = RedisTool::sizeByPrefix($redis, 'misp:wlc:*'); $output['warninglist_cache_count'] = $count; $output['warninglist_cache_size'] = $size; - list($count, $size) = $this->redisSize($redis, 'misp:warninglist_entries_cache:*'); + list($count, $size) = RedisTool::sizeByPrefix($redis, 'misp:warninglist_entries_cache:*'); $output['warninglist_entries_count'] = $count; $output['warninglist_entries_size'] = $size; - list($count, $size) = $this->redisSize($redis, 'misp:top_correlation'); + list($count, $size) = RedisTool::sizeByPrefix($redis, 'misp:top_correlation'); $output['top_correlation_count'] = $count; $output['top_correlation_size'] = $size; - list($count, $size) = $this->redisSize($redis, 'misp:correlation_exclusions'); + list($count, $size) = RedisTool::sizeByPrefix($redis, 'misp:correlation_exclusions'); $output['correlation_exclusions_count'] = $count; $output['correlation_exclusions_size'] = $size; - list($count, $size) = $this->redisSize($redis, 'misp:event_lock:*'); + list($count, $size) = RedisTool::sizeByPrefix($redis, 'misp:event_lock:*'); $output['event_lock_count'] = $count; $output['event_lock_size'] = $size; - list($count, $size) = $this->redisSize($redis, 'misp:user_ip:*'); + list($count, $size) = RedisTool::sizeByPrefix($redis, 'misp:user_ip:*'); $output['user_ip_count'] = $count; $output['user_ip_size'] = $size; - list($count, $size) = $this->redisSize($redis, 'misp:ip_user:*'); + list($count, $size) = RedisTool::sizeByPrefix($redis, 'misp:ip_user:*'); $output['user_ip_count'] += $count; $output['user_ip_size'] += $size; - list($count, $size) = $this->redisSize($redis, 'misp:authkey_usage:*'); + list($count, $size) = RedisTool::sizeByPrefix($redis, 'misp:authkey_usage:*'); $output['authkey_usage_count'] = $count; $output['authkey_usage_size'] = $size; diff --git a/app/Lib/Tools/RedisTool.php b/app/Lib/Tools/RedisTool.php index 93d2dd78f..240fd26ad 100644 --- a/app/Lib/Tools/RedisTool.php +++ b/app/Lib/Tools/RedisTool.php @@ -83,6 +83,29 @@ class RedisTool return $unlinkSupported ? $redis->unlink($keys) : $redis->del($keys); } + /** + * @param Redis $redis + * @param string $prefix + * @return array[int, int] + * @throws RedisException + */ + public static function sizeByPrefix(Redis $redis, $prefix) + { + $keyCount = 0; + $size = 0; + $it = null; + while ($keys = $redis->scan($it, $prefix, 1000)) { + $redis->pipeline(); + foreach ($keys as $key) { + $redis->rawCommand("memory", "usage", $key); + } + $result = $redis->exec(); + $keyCount += count($keys); + $size += array_sum($result); + } + return [$keyCount, $size]; + } + /** * @param mixed $data * @return string diff --git a/app/Model/Correlation.php b/app/Model/Correlation.php index 2e189d471..cc51e0fd7 100644 --- a/app/Model/Correlation.php +++ b/app/Model/Correlation.php @@ -397,8 +397,7 @@ class Correlation extends AppModel { if ($this->exclusions === null) { try { - $redis = $this->setupRedisWithException(); - $this->exclusions = $redis->sMembers('misp:correlation_exclusions'); + $this->exclusions = RedisTool::init()->sMembers('misp:correlation_exclusions'); } catch (Exception $e) { return false; } diff --git a/app/Model/CorrelationExclusion.php b/app/Model/CorrelationExclusion.php index 7d1bc890d..e46e7e88d 100644 --- a/app/Model/CorrelationExclusion.php +++ b/app/Model/CorrelationExclusion.php @@ -16,8 +16,6 @@ class CorrelationExclusion extends AppModel 'Containable', ); - private $__redis = null; - public $validate = [ 'value' => [ 'uniqueValue' => [ @@ -54,15 +52,15 @@ class CorrelationExclusion extends AppModel public function cacheValues() { try { - $this->__redis = $this->setupRedisWithException(); + $redis = RedisTool::init(); } catch (Exception $e) { return false; } - $this->__redis->del($this->key); + RedisTool::unlink($redis, $this->key); $exclusions = $this->find('column', [ 'fields' => ['value'] ]); - $this->__redis->sAddArray($this->key, $exclusions); + $redis->sAddArray($this->key, $exclusions); } public function cleanRouter($user) diff --git a/app/Model/Warninglist.php b/app/Model/Warninglist.php index bfa02a0e5..dd541edcf 100644 --- a/app/Model/Warninglist.php +++ b/app/Model/Warninglist.php @@ -489,20 +489,22 @@ class Warninglist extends AppModel private function cacheWarninglistEntries(array $warninglistEntries, $id) { - $redis = $this->setupRedis(); - if ($redis !== false) { - $key = 'misp:warninglist_entries_cache:' . $id; - $redis->del($key); - if (method_exists($redis, 'saddArray')) { - $redis->sAddArray($key, $warninglistEntries); - } else { - foreach ($warninglistEntries as $entry) { - $redis->sAdd($key, $entry); - } - } - return true; + try { + $redis = RedisTool::init(); + } catch (Exception $e) { + return false; } - return false; + + $key = 'misp:warninglist_entries_cache:' . $id; + RedisTool::unlink($redis, $key); + if (method_exists($redis, 'saddArray')) { + $redis->sAddArray($key, $warninglistEntries); + } else { + foreach ($warninglistEntries as $entry) { + $redis->sAdd($key, $entry); + } + } + return true; } /**