Skip to content

Commit 31a1188

Browse files
Merge pull request #122 from julienloizelet/feat/prune-cache-script
feat(*): Use remediation-engine 3.0.0 and add prune cache script
2 parents f132937 + caf87d7 commit 31a1188

File tree

8 files changed

+57
-24
lines changed

8 files changed

+57
-24
lines changed

CHANGELOG.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,26 @@ All notable changes to this project will be documented in this file.
33

44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
55

6-
## Public API
6+
## SemVer public API
77

8-
The purpose of this section is to declare the public API of this library as required by [item 1 of semantic versioning specification](https://semver.org/spec/v2.0.0.html#spec-item-1).
8+
The [public API](https://semver.org/spec/v2.0.0.html#spec-item-1) of this library consists of all public or protected methods, properties and constants belonging to the `src` folder.
99

10-
The public API of this library consists of all public or protected methods, properties and constants belonging to the `src` folder.
10+
---
11+
12+
13+
## [1.2.0](https://github.com/crowdsecurity/php-cs-bouncer/releases/tag/v1.2.0) - 2023-03-09
14+
[_Compare with previous release_](https://github.com/crowdsecurity/php-cs-bouncer/compare/v1.1.1...v1.2.0)
15+
16+
17+
### Changed
18+
- Use `crowdsec/remediation` `^3.0.0` instead of `^2.0.0`
19+
20+
### Added
21+
- Add a script to prune cache with a cron job (Standalone bouncer)
1122

1223
---
1324

25+
1426
## [1.1.1](https://github.com/crowdsecurity/php-cs-bouncer/releases/tag/v1.1.1) - 2023-02-16
1527
[_Compare with previous release_](https://github.com/crowdsecurity/php-cs-bouncer/compare/v1.1.0...v1.1.1)
1628

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@
4040
],
4141
"require": {
4242
"php": ">=7.2.5",
43-
"crowdsec/remediation-engine": "^2.0.0",
44-
"crowdsec/common": "^1.2.0",
43+
"crowdsec/remediation-engine": "^3.0.0",
4544
"symfony/config": "^4.4.27 || ^5.2 || ^6.0",
4645
"twig/twig": "^3.4.2",
4746
"gregwar/captcha": "^1.1",

docs/DEVELOPER.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ and `GeoLite2-Country.mmdb`. You can download these databases by creating a MaxM
223223
Then, you can run:
224224

225225
```bash
226-
ddev exec BOUNCER_KEY=your-bouncer-key AGENT_TLS_PATH=/var/www/html/cfssl LAPI_URL=https://crowdsec:8080 /usr/bin/php ./my-own-modules/crowdsec-php-lib/vendor/bin/phpunit --testdox --colors --exclude-group ignore ./my-own-modules/crowdsec-php-lib/tests/Integration/GeolocationTest.php
226+
ddev exec BOUNCER_KEY=your-bouncer-key AGENT_TLS_PATH=/var/www/html/cfssl LAPI_URL=https://crowdsec:8080
227+
MEMCACHED_DSN=memcached://memcached:11211 REDIS_DSN=redis://redis:6379 /usr/bin/php ./my-own-modules/crowdsec-php-lib/vendor/bin/phpunit --testdox --colors --exclude-group ignore ./my-own-modules/crowdsec-php-lib/tests/Integration/GeolocationTest.php
227228
```
228229

229230
**N.B.**: If you want to test with `curl` instead of `file_get_contents` calls to LAPI, you have to add `USE_CURL=1` in

docs/USER_GUIDE.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,23 @@ and add the following line
192192

193193
In this example, cache is refreshed every minute, but you can modify the cron expression depending on your needs.
194194

195+
### Cache pruning cron task
196+
197+
To use the PHP file system as cache, you should prune the cache with a cron job:
198+
199+
```shell
200+
sudo -u www-data crontab -e
201+
```
202+
203+
and add the following line
204+
205+
```shell
206+
0 0 * * * /usr/bin/php /absolute/path/to/scripts/auto-prepend/prune-cache.php
207+
```
208+
209+
In this example, cache is pruned at midnight every day, but you can modify the cron expression depending on your needs.
210+
211+
195212
## Create your own bouncer
196213

197214
### Implementation

scripts/auto-prepend/prune-cache.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* This script is aimed to be called by a cron job.
4+
*
5+
* @see docs/USER_GUIDE.md
6+
*
7+
* @var $crowdSecStandaloneBouncerConfig
8+
*/
9+
require_once __DIR__ . '/../../vendor/autoload.php';
10+
require_once __DIR__ . '/settings.php';
11+
12+
use CrowdSecBouncer\StandaloneBouncer;
13+
14+
$bouncer = new StandaloneBouncer($crowdSecStandaloneBouncerConfig);
15+
$bouncer->pruneCache();

scripts/auto-prepend/refresh-cache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* This script is aimed to be called by an auto-prepend directive.
3+
* This script is aimed to be called by a cron job.
44
*
55
* @see docs/USER_GUIDE.md
66
*

src/AbstractBouncer.php

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -284,29 +284,18 @@ public function shouldBounceCurrentIp(): bool
284284
* @return void
285285
* @throws BouncerException
286286
* @throws InvalidArgumentException
287-
* @todo custom error handler should be in RemediationEngine (v3.0.0)
288287
*/
289288
public function testCacheConnection(): void
290289
{
291290
try {
292291
$cache = $this->getRemediationEngine()->getCacheStorage();
293-
if ($cache instanceof Memcached) {
294-
set_error_handler(function ($errno, $errstr) {
295-
$message = "Memcached error. (Error level: $errno) Original error was: $errstr";
296-
throw new CacheStorageException($message);
297-
});
298-
}
299292
$cache->getItem(AbstractCache::CONFIG);
300293
} catch (\Exception $e) {
301294
throw new BouncerException(
302295
'Error while testing cache connection: ' . $e->getMessage(),
303296
(int)$e->getCode(),
304297
$e
305298
);
306-
} finally {
307-
if (isset($cache) && $cache instanceof Memcached) {
308-
restore_error_handler();
309-
}
310299
}
311300
}
312301

@@ -690,7 +679,7 @@ private function handleCaptchaResolutionForm(string $ip): void
690679
['has_to_be_resolved' => false],
691680
$ip,
692681
$duration,
693-
Constants::CACHE_TAG_CAPTCHA
682+
[Constants::CACHE_TAG_CAPTCHA]
694683
);
695684
$unsetVariables = [
696685
'phrase_to_guess',
@@ -703,7 +692,7 @@ private function handleCaptchaResolutionForm(string $ip): void
703692
$unsetVariables,
704693
$ip,
705694
$duration,
706-
Constants::CACHE_TAG_CAPTCHA
695+
[Constants::CACHE_TAG_CAPTCHA]
707696
);
708697
$redirect = $cachedCaptchaVariables['resolution_redirect'] ?? '/';
709698
$this->redirectResponse($redirect);
@@ -714,7 +703,7 @@ private function handleCaptchaResolutionForm(string $ip): void
714703
['resolution_failed' => true],
715704
$ip,
716705
$duration,
717-
Constants::CACHE_TAG_CAPTCHA
706+
[Constants::CACHE_TAG_CAPTCHA]
718707
);
719708
}
720709
}
@@ -796,7 +785,7 @@ private function initCaptchaResolution(string $ip): void
796785
$captchaVariables,
797786
$ip,
798787
$duration,
799-
Constants::CACHE_TAG_CAPTCHA
788+
[Constants::CACHE_TAG_CAPTCHA]
800789
);
801790
}
802791

@@ -822,7 +811,7 @@ private function refreshCaptcha(string $ip): bool
822811
$captchaVariables,
823812
$ip,
824813
$duration,
825-
Constants::CACHE_TAG_CAPTCHA
814+
[Constants::CACHE_TAG_CAPTCHA]
826815
);
827816

828817
return true;

src/Constants.php

Lines changed: 1 addition & 1 deletion
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.1.1';
42+
public const VERSION = 'v1.2.0';
4343
/** @var string The "disabled" x-forwarded-for setting */
4444
public const X_FORWARDED_DISABLED = 'no_forward';
4545
}

0 commit comments

Comments
 (0)