Skip to content

Commit 5ccafe8

Browse files
Merge pull request #92 from julienloizelet/fix/custom-handler-memcached-tag-aware
Fix/custom handler memcached tag aware
2 parents 82af253 + 408dca7 commit 5ccafe8

File tree

8 files changed

+73
-33
lines changed

8 files changed

+73
-33
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

77

8+
## [0.22.1] - 2022-06-03
9+
10+
### Fixed
11+
- Handle custom error handler for Memcached tag aware adapter
12+
813
## [0.22.0] - 2022-06-02
914

1015
### Added

scripts/refresh-cache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
// Instantiate the "PhpFilesAdapter" cache adapter
1616
$cacheAdapter = new TagAwareAdapter(new Symfony\Component\Cache\Adapter\PhpFilesAdapter('', 0, $cachePath));
17-
// 0Or Redis: $cacheAdapter = new RedisTagAwareAdapter(RedisAdapter::createConnection('redis://your-redis-host:6379'));
17+
// Or Redis: $cacheAdapter = new RedisTagAwareAdapter(RedisAdapter::createConnection('redis://your-redis-host:6379'));
1818
// Or Memcached: $cacheAdapter = new TagAwareAdapter(new MemcachedAdapter(MemcachedAdapter::createConnection
1919
//('memcached://your-memcached-host:11211')));
2020
// Parse argument

src/ApiCache.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace CrowdSecBouncer;
66

7+
use CrowdSecBouncer\Fixes\Memcached\TagAwareAdapter as MemcachedTagAwareAdapter;
78
use DateTime;
89
use Exception;
910
use IPLib\Address\AddressInterface;
@@ -13,7 +14,6 @@
1314
use Psr\Cache\InvalidArgumentException;
1415
use Psr\Log\LoggerInterface;
1516
use Symfony\Component\Cache\Adapter\AbstractAdapter;
16-
use Symfony\Component\Cache\Adapter\MemcachedAdapter;
1717
use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
1818
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
1919
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
@@ -826,7 +826,7 @@ public function prune(): bool
826826
*/
827827
private function setCustomErrorHandler(): void
828828
{
829-
if ($this->adapter instanceof MemcachedAdapter) {
829+
if ($this->adapter instanceof MemcachedTagAwareAdapter) {
830830
set_error_handler(function ($errno, $errstr) {
831831
throw new BouncerException("Error when connecting to Memcached. (Error level: $errno) Please fix the Memcached DSN or select another cache technology. Original message was: $errstr");
832832
});
@@ -838,7 +838,7 @@ private function setCustomErrorHandler(): void
838838
* */
839839
private function unsetCustomErrorHandler(): void
840840
{
841-
if ($this->adapter instanceof MemcachedAdapter) {
841+
if ($this->adapter instanceof MemcachedTagAwareAdapter) {
842842
restore_error_handler();
843843
}
844844
}

src/Constants.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Constants
2020
public const DEFAULT_LAPI_URL = 'http://localhost:8080';
2121

2222
/** @var string The last version of this library */
23-
public const VERSION = 'v0.22.0';
23+
public const VERSION = 'v0.22.1';
2424

2525
/** @var string The user agent used to send request to LAPI */
2626
public const BASE_USER_AGENT = 'PHP CrowdSec Bouncer/'.self::VERSION;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace CrowdSecBouncer\Fixes\Memcached;
4+
5+
use Symfony\Component\Cache\Adapter\TagAwareAdapter as SymfonyTagAwareAdapter;
6+
// This class is used only to know explicitly that we instantiiate a Memcached adapter
7+
class TagAwareAdapter extends SymfonyTagAwareAdapter
8+
{
9+
10+
}

src/StandaloneBounce.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace CrowdSecBouncer;
44

5+
use CrowdSecBouncer\Fixes\Memcached\TagAwareAdapter as MemcachedTagAwareAdapter;
56
use ErrorException;
67
use Exception;
78
use IPLib\Factory;
@@ -92,7 +93,7 @@ private function getCacheAdapterInstance(bool $forceReload = false): TagAwareAda
9293
throw new BouncerException('The selected cache technology is Memcached.'.' Please set a Memcached DSN or select another cache technology.');
9394
}
9495

95-
$this->cacheAdapter = new TagAwareAdapter(
96+
$this->cacheAdapter = new MemcachedTagAwareAdapter(
9697
new MemcachedAdapter(MemcachedAdapter::createConnection($memcachedDsn)));
9798
break;
9899

tests/IpVerificationTest.php

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,30 @@ public function testCanVerifyIpInLiveModeWithCacheSystem($cacheAdapter, $origCac
6060
$bouncer = new Bouncer(null, $this->logger, $apiCache);
6161
$bouncer->configure($bouncerConfig);
6262

63-
if (in_array($origCacheName, ['PhpFilesAdapter', 'MemcachedAdapter'])) {
64-
$this->assertEquals(
65-
'Symfony\Component\Cache\Adapter\TagAwareAdapter',
66-
get_class($cacheAdapter),
67-
'Tested adapter should be correct'
68-
);
69-
} elseif ('RedisAdapter' == $origCacheName) {
70-
$this->assertEquals(
71-
'Symfony\Component\Cache\Adapter\RedisTagAwareAdapter',
72-
get_class($cacheAdapter),
73-
'Tested adapter should be correct'
74-
);
63+
switch ($origCacheName) {
64+
case 'PhpFilesAdapter':
65+
$this->assertEquals(
66+
'Symfony\Component\Cache\Adapter\TagAwareAdapter',
67+
get_class($cacheAdapter),
68+
'Tested adapter should be correct'
69+
);
70+
break;
71+
case 'MemcachedAdapter':
72+
$this->assertEquals(
73+
'CrowdSecBouncer\Fixes\Memcached\TagAwareAdapter',
74+
get_class($cacheAdapter),
75+
'Tested adapter should be correct'
76+
);
77+
break;
78+
case 'RedisAdapter':
79+
$this->assertEquals(
80+
'Symfony\Component\Cache\Adapter\RedisTagAwareAdapter',
81+
get_class($cacheAdapter),
82+
'Tested adapter should be correct'
83+
);
84+
break;
85+
default:
86+
break;
7587
}
7688

7789
// At the end of test, we should have exactly 3 "cache miss")
@@ -167,20 +179,31 @@ public function testCanVerifyIpInStreamModeWithCacheSystem($cacheAdapter, $origC
167179
$bouncer = new Bouncer(null, $this->logger, $apiCache);
168180
$bouncer->configure($bouncerConfig);
169181

170-
if (in_array($origCacheName, ['PhpFilesAdapter', 'MemcachedAdapter'])) {
171-
$this->assertEquals(
172-
'Symfony\Component\Cache\Adapter\TagAwareAdapter',
173-
get_class($cacheAdapter),
174-
'Tested adapter should be correct'
175-
);
176-
} elseif ('RedisAdapter' == $origCacheName) {
177-
$this->assertEquals(
178-
'Symfony\Component\Cache\Adapter\RedisTagAwareAdapter',
179-
get_class($cacheAdapter),
180-
'Tested adapter should be correct'
181-
);
182+
switch ($origCacheName) {
183+
case 'PhpFilesAdapter':
184+
$this->assertEquals(
185+
'Symfony\Component\Cache\Adapter\TagAwareAdapter',
186+
get_class($cacheAdapter),
187+
'Tested adapter should be correct'
188+
);
189+
break;
190+
case 'MemcachedAdapter':
191+
$this->assertEquals(
192+
'CrowdSecBouncer\Fixes\Memcached\TagAwareAdapter',
193+
get_class($cacheAdapter),
194+
'Tested adapter should be correct'
195+
);
196+
break;
197+
case 'RedisAdapter':
198+
$this->assertEquals(
199+
'Symfony\Component\Cache\Adapter\RedisTagAwareAdapter',
200+
get_class($cacheAdapter),
201+
'Tested adapter should be correct'
202+
);
203+
break;
204+
default:
205+
break;
182206
}
183-
184207
// As we are in stream mode, no live call should be done to the API.
185208

186209
/** @var MockObject $apiClientMock */
@@ -269,7 +292,7 @@ public function testCanVerifyIpInStreamModeWithCacheSystem($cacheAdapter, $origC
269292
$this->assertEquals(
270293
'ban',
271294
$bouncer->getRemediationForIp(TestHelpers::NEWLY_BAD_IP),
272-
'The cache warm up should be stored across each instanciation'
295+
'The cache warm up should be stored across each instantiation'
273296
);
274297

275298
$this->logger->info('', ['message' => 'set "Large IPV4 range banned" + "IPV6 range banned" state']);

tests/TestHelpers.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
declare(strict_types=1);
44

5+
use CrowdSecBouncer\Fixes\Memcached\TagAwareAdapter as MemcachedTagAwareAdapter;
56
use Monolog\Formatter\LineFormatter;
67
use Monolog\Handler\StreamHandler;
78
use Monolog\Logger;
@@ -56,7 +57,7 @@ public static function cacheAdapterProvider(): array
5657

5758
/** @var string */
5859
$memcachedCacheAdapterDsn = getenv('MEMCACHED_DSN');
59-
$memcachedAdapter = new TagAwareAdapter(
60+
$memcachedAdapter = new MemcachedTagAwareAdapter(
6061
new MemcachedAdapter(MemcachedAdapter::createConnection($memcachedCacheAdapterDsn)));
6162

6263
return [

0 commit comments

Comments
 (0)