Skip to content

Commit 960d2aa

Browse files
Merge pull request #120 from julienloizelet/fix/test-connection-for-memcached
fix(memcached): Update testCacheConnetion method to throw an error if…
2 parents 302c31b + 749da1d commit 960d2aa

File tree

5 files changed

+82
-2
lines changed

5 files changed

+82
-2
lines changed

.github/workflows/release.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ jobs:
6666
fi
6767
6868
# Check top [_Compare with previous release_](GITHUB_URL/compare/vLAST_TAG...vVERSION_NUMBER) in CHANGELOG.md
69-
COMPARISON=$(grep -oP "\/compare\/\K(.*)$" CHANGELOG.md | head -1)
69+
COMPARISON=$(grep -oP "$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/compare/\K(.*)$" CHANGELOG.md | head -1)
7070
LAST_TAG=$(curl -Ls -o /dev/null -w %{url_effective} $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/releases/latest | grep -oP "\/tag\/\K(.*)$")
7171
if [[ $COMPARISON == "$LAST_TAG...v${{ env.VERSION_NUMBER }})" ]]
7272
then

CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ The purpose of this section is to declare the public API of this library as requ
99

1010
The public API of this library consists of all public or protected methods, properties and constants belonging to the `src` folder.
1111

12+
---
13+
14+
## [1.0.1](https://github.com/crowdsecurity/php-cs-bouncer/releases/tag/v1.0.0) - 2023-02-10
15+
[_Compare with previous release_](https://github.com/crowdsecurity/php-cs-bouncer/compare/v1.0.0...v1.0.1)
16+
17+
### Fixed
18+
- Update `AbstractBouncer::testCacheConnection` method to throw an exception for Memcached if necessary
19+
20+
1221
---
1322

1423
## [1.0.0](https://github.com/crowdsecurity/php-cs-bouncer/releases/tag/v1.0.0) - 2023-02-03

src/AbstractBouncer.php

+11
Original file line numberDiff line numberDiff line change
@@ -281,18 +281,29 @@ public function shouldBounceCurrentIp(): bool
281281
* @return void
282282
* @throws BouncerException
283283
* @throws InvalidArgumentException
284+
* @todo custom error handler should be in RemediationEngine (v3.0.0)
284285
*/
285286
public function testCacheConnection(): void
286287
{
287288
try {
288289
$cache = $this->getRemediationEngine()->getCacheStorage();
290+
if ($cache instanceof Memcached) {
291+
set_error_handler(function ($errno, $errstr) {
292+
$message = "Memcached error. (Error level: $errno) Original error was: $errstr";
293+
throw new CacheStorageException($message);
294+
});
295+
}
289296
$cache->getItem(AbstractCache::CONFIG);
290297
} catch (\Exception $e) {
291298
throw new BouncerException(
292299
'Error while testing cache connection: ' . $e->getMessage(),
293300
(int)$e->getCode(),
294301
$e
295302
);
303+
} finally {
304+
if (isset($cache) && $cache instanceof Memcached) {
305+
restore_error_handler();
306+
}
296307
}
297308
}
298309

src/Constants.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Constants extends RemConstants
3939
/** @var string Path for html templates folder (e.g. ban and captcha wall) */
4040
public const TEMPLATES_DIR = __DIR__ . "/templates";
4141
/** @var string The last version of this library */
42-
public const VERSION = 'v1.0.0';
42+
public const VERSION = 'v1.0.1';
4343
/** @var string The "disabled" x-forwarded-for setting */
4444
public const X_FORWARDED_DISABLED = 'no_forward';
4545
}

tests/Integration/IpVerificationTest.php

+60
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace CrowdSecBouncer\Tests\Integration;
66

77
use CrowdSec\Common\Logger\FileLog;
8+
use CrowdSec\RemediationEngine\CacheStorage\CacheStorageException;
89
use CrowdSecBouncer\Bouncer;
910
use CrowdSecBouncer\BouncerException;
1011
use CrowdSecBouncer\Constants;
@@ -186,6 +187,65 @@ private function addTlsConfig(&$bouncerConfigs, $tlsPath)
186187
$bouncerConfigs['tls_verify_peer'] = true;
187188
}
188189

190+
/**
191+
* @group integration
192+
* @dataProvider cacheAdapterConfigProvider
193+
*/
194+
public function testTestCacheConnexion($cacheAdapterName, $origCacheName)
195+
{
196+
$bouncer = new StandaloneBouncer(array_merge($this->configs,
197+
['cache_system'=> $cacheAdapterName]));
198+
$error = '';
199+
try {
200+
$bouncer->testCacheConnection();
201+
} catch (\Exception $e){
202+
$error = $e->getMessage();
203+
}
204+
$this->assertEquals('', $error);
205+
206+
// Test custom error handler for Memcached
207+
if($cacheAdapterName === 'memcached'){
208+
$bouncer2 = new StandaloneBouncer(array_merge($this->configs,
209+
[
210+
'cache_system'=> $cacheAdapterName,
211+
'memcached_dsn' => 'memcached://memcached:21',
212+
]));
213+
214+
$error = '';
215+
try {
216+
$bouncer2->testCacheConnection();
217+
} catch (BouncerException $e){
218+
$error = $e->getMessage();
219+
}
220+
PHPUnitUtil::assertRegExp(
221+
$this,
222+
'/Error while testing cache connection/',
223+
$error,
224+
'Should have throw an error'
225+
);
226+
}
227+
// Test bad dsn for redis
228+
if($cacheAdapterName === 'redis'){
229+
$error = '';
230+
try {
231+
$bouncer3 = new StandaloneBouncer(array_merge($this->configs,
232+
[
233+
'cache_system'=> $cacheAdapterName,
234+
'redis_dsn' => 'redis://redis:21'
235+
]));
236+
} catch (CacheStorageException $e){
237+
$error = $e->getMessage();
238+
}
239+
PHPUnitUtil::assertRegExp(
240+
$this,
241+
'/Error when creating/',
242+
$error,
243+
'Should have throw an error'
244+
);
245+
246+
}
247+
}
248+
189249
public function testConstructAndSomeMethods()
190250
{
191251
unset($_SERVER['REMOTE_ADDR'] );

0 commit comments

Comments
 (0)