Skip to content

Commit

Permalink
Merge pull request #23 from julienloizelet/fix/memcached-silent-error
Browse files Browse the repository at this point in the history
fix(memcached): Do not set logger in Memcached cache to avoid silent …
  • Loading branch information
julienloizelet authored Mar 24, 2023
2 parents b61058a + 353fb0e commit 83ff8ca
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 15 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ The [public API](https://semver.org/spec/v2.0.0.html#spec-item-1) of this libra

---

## [3.1.1](https://github.com/crowdsecurity/php-remediation-engine/releases/tag/v3.1.1) - 2023-03-24
[_Compare with previous release_](https://github.com/crowdsecurity/php-remediation-engine/compare/v3.1.0...v3.1.1)

### Fixed

- Do not set logger in Memcached cache to avoid silent error


---


## [3.1.0](https://github.com/crowdsecurity/php-remediation-engine/releases/tag/v3.1.0) - 2023-03-24
[_Compare with previous release_](https://github.com/crowdsecurity/php-remediation-engine/compare/v3.0.0...v3.1.0)

Expand Down
4 changes: 0 additions & 4 deletions src/CacheStorage/AbstractCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Psr\Cache\CacheException;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\InvalidArgumentException;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
Expand Down Expand Up @@ -83,9 +82,6 @@ public function __construct(array $configs, AdapterInterface $adapter, LoggerInt
$logger->pushHandler(new NullHandler());
}
$this->logger = $logger;
if ($this->adapter instanceof LoggerAwareInterface) {
$this->adapter->setLogger($this->logger);
}
$this->logger->debug('Instantiate cache', [
'type' => 'CACHE_INIT',
'configs' => $configs,
Expand Down
3 changes: 3 additions & 0 deletions src/CacheStorage/PhpFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public function __construct(array $configs, LoggerInterface $logger = null)
try {
$fileAdapter = new PhpFilesAdapter('', 0, $this->configs['fs_cache_path']);
$adapter = !empty($this->configs['use_cache_tags']) ? new TagAwareAdapter($fileAdapter) : $fileAdapter;
if ($logger) {
$adapter->setLogger($logger);
}
// @codeCoverageIgnoreStart
} catch (\Exception $e) {
$message = 'Error when creating to PhpFiles cache adapter:' . $e->getMessage();
Expand Down
3 changes: 3 additions & 0 deletions src/CacheStorage/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public function __construct(array $configs, LoggerInterface $logger = null)
$adapter = !empty($this->configs['use_cache_tags']) ?
new RedisTagAwareAdapter($connection) :
new RedisAdapter($connection);
if ($logger) {
$adapter->setLogger($logger);
}
// @codeCoverageIgnoreStart
} catch (\Exception $e) {
$message = 'Error when creating Redis cache adapter:' . $e->getMessage();
Expand Down
2 changes: 1 addition & 1 deletion src/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ class Constants extends CommonConstants
/**
* @var string The current version of this library
*/
public const VERSION = 'v3.1.0';
public const VERSION = 'v3.1.1';
}
2 changes: 1 addition & 1 deletion tests/Unit/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
* @uses \CrowdSec\RemediationEngine\Configuration\Cache\Memcached::getConfigTreeBuilder
* @uses \CrowdSec\RemediationEngine\Configuration\Cache\PhpFiles::getConfigTreeBuilder
* @uses \CrowdSec\RemediationEngine\Configuration\Cache\Redis::getConfigTreeBuilder
* @covers \CrowdSec\RemediationEngine\Configuration\AbstractCache::addCommonNodes
*
* @covers \CrowdSec\RemediationEngine\Configuration\AbstractCache::addCommonNodes
* @covers \CrowdSec\RemediationEngine\CacheStorage\Memcached::clear
* @covers \CrowdSec\RemediationEngine\CacheStorage\Memcached::commit
* @covers \CrowdSec\RemediationEngine\CacheStorage\Memcached::getItem
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/CapiRemediationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public function setUp(): void
$this->phpFileStorage =
$this->getCacheMock('PhpFilesAdapter', $cachePhpfilesConfigs, $this->logger, $mockedMethods);
$this->phpFileStorageWithTags =
$this->getCacheMock('PhpFilesAdapter', array_merge($cachePhpfilesConfigs,['use_cache_tags'=>true]),
$this->getCacheMock('PhpFilesAdapter', array_merge($cachePhpfilesConfigs, ['use_cache_tags' => true]),
$this->logger, $mockedMethods);
$cacheMemcachedConfigs = [
'memcached_dsn' => getenv('memcached_dsn') ?: 'memcached://memcached:11211',
Expand Down
11 changes: 5 additions & 6 deletions tests/Unit/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ public function testPhpFilesConfiguration()
$this->assertEquals(
[
'fs_cache_path' => 'fs_cache_path_test',
'use_cache_tags' => false
'use_cache_tags' => false,
],
$result,
'Should set default config'
Expand All @@ -430,13 +430,12 @@ public function testPhpFilesConfiguration()
$this->assertEquals(
[
'fs_cache_path' => 'fs_cache_path_test',
'use_cache_tags' => true
'use_cache_tags' => true,
],
$result,
'Should set default config'
);


// Test missing path
$error = '';
$configs = [];
Expand Down Expand Up @@ -479,7 +478,7 @@ public function testRedisConfiguration()
$this->assertEquals(
[
'redis_dsn' => 'redis_dsn_test',
'use_cache_tags' => false
'use_cache_tags' => false,
],
$result,
'Should set default config'
Expand All @@ -490,7 +489,7 @@ public function testRedisConfiguration()
$this->assertEquals(
[
'redis_dsn' => 'redis_dsn_test',
'use_cache_tags' => true
'use_cache_tags' => true,
],
$result
);
Expand All @@ -504,7 +503,7 @@ public function testRedisConfiguration()
$this->assertEquals(
[
'redis_dsn' => 'redis_dsn_test',
'use_cache_tags' => false
'use_cache_tags' => false,
],
$result,
'Should clean unexpected config'
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/LapiRemediationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public function setUp(): void
$this->phpFileStorage =
$this->getCacheMock('PhpFilesAdapter', $cachePhpfilesConfigs, $this->logger, $mockedMethods);
$this->phpFileStorageWithTags =
$this->getCacheMock('PhpFilesAdapter', array_merge($cachePhpfilesConfigs,['use_cache_tags'=>true]),
$this->getCacheMock('PhpFilesAdapter', array_merge($cachePhpfilesConfigs, ['use_cache_tags' => true]),
$this->logger, $mockedMethods);
$cacheMemcachedConfigs = [
'memcached_dsn' => getenv('memcached_dsn') ?: 'memcached://memcached:11211',
Expand Down Expand Up @@ -500,7 +500,7 @@ public function testGetIpRemediationInLiveMode($cacheType)
'Bypass should have been cached'
);
$this->assertEquals(
$expectedCleanTime,$cachedItem[0][AbstractCache::INDEX_EXP],
$expectedCleanTime, $cachedItem[0][AbstractCache::INDEX_EXP],
'Should return current time + clean ip duration config'
);
$this->assertEquals(
Expand Down

0 comments on commit 83ff8ca

Please sign in to comment.