Skip to content

Commit 49ca206

Browse files
Merge pull request #100 from julienloizelet/feat/refacto-construct
Feat/refacto construct
2 parents ebaf946 + da4c09b commit 49ca206

27 files changed

+490
-598
lines changed

.github/workflows/test-suite.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,19 @@ jobs:
9393
tar -xf GeoLite2-City.tar.gz
9494
rm GeoLite2-Country.tar.gz GeoLite2-Country.tar.gz.sha256.txt GeoLite2-City.tar.gz GeoLite2-City.tar.gz.sha256.txt
9595
96-
- name: Run PHP UNIT tests (IP verification)
96+
- name: Run "IP verification with file_get_contents" test
9797
run: |
9898
ddev exec BOUNCER_KEY=${{ env.BOUNCER_KEY }} LAPI_URL=http://crowdsec:8080 MEMCACHED_DSN=memcached://memcached:11211 REDIS_DSN=redis://redis:6379 /usr/bin/php ./${{env.EXTENSION_PATH}}/vendor/bin/phpunit --testdox --colors --exclude-group ignore ./${{env.EXTENSION_PATH}}/tests/Integration/IpVerificationTest.php
9999
100-
- name: Run PHP UNIT tests (IP verification with cURL)
100+
- name: Run "IP verification with cURL" test
101101
run: |
102102
ddev exec BOUNCER_KEY=${{ env.BOUNCER_KEY }} USE_CURL=1 LAPI_URL=http://crowdsec:8080 MEMCACHED_DSN=memcached://memcached:11211 REDIS_DSN=redis://redis:6379 /usr/bin/php ./${{env.EXTENSION_PATH}}/vendor/bin/phpunit --testdox --colors --exclude-group ignore ./${{env.EXTENSION_PATH}}/tests/Integration/IpVerificationTest.php
103103
104-
- name: Run PHP UNIT tests (Geolocation)
104+
- name: Run "Geolocation with file_get_contents" test
105105
run: |
106106
ddev exec BOUNCER_KEY=${{ env.BOUNCER_KEY }} LAPI_URL=http://crowdsec:8080 /usr/bin/php ./${{env.EXTENSION_PATH}}/vendor/bin/phpunit --testdox --colors --exclude-group ignore ./${{env.EXTENSION_PATH}}/tests/Integration/GeolocationTest.php
107107
108-
- name: Run PHP UNIT tests (Geolocation with cURL)
108+
- name: Run "Geolocation with cURL" test
109109
run: |
110110
ddev exec BOUNCER_KEY=${{ env.BOUNCER_KEY }} USE_CURL=1 LAPI_URL=http://crowdsec:8080 /usr/bin/php ./${{env.EXTENSION_PATH}}/vendor/bin/phpunit --testdox --colors --exclude-group ignore ./${{env.EXTENSION_PATH}}/tests/Integration/GeolocationTest.php
111111

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ 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.26.0] - 2022-07-28
9+
10+
### Changed
11+
- *Breaking change*: Modify all constructors (`Bouncer`, `ApiCache`, `ApiClient`, `RestClient`) to use only
12+
configurations and logger as parameters
13+
- Use `shouldBounceCurrentIp` method of Standalone before bouncer instantiation
14+
- Modify logger constructor
15+
816
## [0.25.0] - 2022-07-22
917

1018
### Added

docs/DEVELOPER.md

+21-42
Original file line numberDiff line numberDiff line change
@@ -502,12 +502,12 @@ Fortunately, this library allows you to cap the remediation to a certain level.
502502
Let's add the `max_remediation_level` configuration with `captcha` value:
503503

504504
```php
505-
$bouncer->configure([
505+
$configs = [
506506
'api_key' => $bouncerKey,
507507
'api_url' => 'http://crowdsec:8080',
508-
'max_remediation_level' => 'captcha' // <== ADD THIS LINE!
509-
]
510-
);
508+
'fs_cache_path' => __DIR__ . '/.cache'
509+
'max_remediation_level' => 'captcha' // <== ADD THIS LINE
510+
];
511511
```
512512

513513
Now if you call one more time:
@@ -526,52 +526,33 @@ Now update the `check-ip.php`script to replace the `PhpFilesAdapter` with the `R
526526
Replace:
527527

528528
```php
529-
<?php
530-
531-
require __DIR__ . '/vendor/autoload.php';
532-
533-
use CrowdSecBouncer\Bouncer;
534-
use Symfony\Component\Cache\Adapter\RedisAdapter;
535-
536-
// Init cache adapter
537-
$cacheAdapter = new TagAwareAdapter(new PhpFilesAdapter('', 0, __DIR__.'/.cache'));
538-
539-
...
529+
$configs = [
530+
'api_key' => $bouncerKey,
531+
'api_url' => 'http://crowdsec:8080',
532+
'fs_cache_path' => __DIR__ . '/.cache',
533+
];
540534
```
541535

542536
with:
543537

544538
```php
545-
<?php
546-
547-
require __DIR__ . '/vendor/autoload.php';
548-
549-
use CrowdSecBouncer\Bouncer;
550-
use Symfony\Component\Cache\Adapter\RedisAdapter;
551-
552-
// Init cache adapter
553-
554-
$cacheAdapter = new RedisTagAwareAdapter(RedisAdapter::createConnection('redis://redis:6379'));
555-
556-
...
539+
$configs = [
540+
'api_key' => $bouncerKey,
541+
'api_url' => 'http://crowdsec:8080',
542+
'cache_system' => 'redis',
543+
'redis_dsn' => 'redis://redis:6379'
544+
];
557545
```
558546

559547
Or, if `Memcached` is more adapted than `Redis` to your needs:
560548

561549
```php
562-
<?php
563-
564-
require __DIR__ . '/vendor/autoload.php';
565-
566-
use CrowdSecBouncer\Bouncer;
567-
use Symfony\Component\Cache\Adapter\MemcachedAdapter;
568-
569-
// Init cache adapter
570-
571-
$cacheAdapter =
572-
new TagAwareAdapter(new MemcachedAdapter(MemcachedAdapter::createConnection('memcached://memcached:11211')));
573-
574-
...
550+
$configs = [
551+
'api_key' => $bouncerKey,
552+
'api_url' => 'http://crowdsec:8080',
553+
'cache_system' => 'memcached',
554+
'memcached_dsn' => 'memcached://memcached:11211'
555+
];
575556
```
576557

577558
You will still be able to verify IPs, but the cache system will be more efficient.
@@ -580,8 +561,6 @@ You will still be able to verify IPs, but the cache system will be more efficien
580561
ddev exec php my-own-modules/crowdsec-php-lib/scripts/check-ip.php 1.2.3.4 <BOUNCER_KEY>
581562
```
582563

583-
> Note: You can try more cache systems but we did not test them for now (Apcu, Filesystem, Doctrine, Couchbase, Pdo). The [full list is here](https://symfony.com/doc/current/components/cache.html#available-cache-adapters).
584-
585564
### Clear cache script
586565

587566
To clear your LAPI cache, you can use the [`clear-php`](../scripts/clear-cache.php) script:

docs/USER_GUIDE.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ Here is the list of available settings:
124124
- `api_url`: Define the URL to your LAPI server, default to `http://localhost:8080`.
125125

126126
- `api_timeout`: In seconds. The timeout when calling LAPI. Must be greater or equal than 1. Defaults to 1 sec.
127-
- `api_user_agent`: HTTP user agent used to call CLAPI. Default to this library name/current version.
127+
128128
- `use_curl`: By default, this lib call the REST LAPI using `file_get_contents` method (`allow_url_fopen` is required).
129129
You can set `use_curl` to `true` in order to use `cURL` request instead (`curl` is in then required)
130130

scripts/auto-prepend/refresh-cache.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
}elseif($crowdSecStandaloneBouncerConfig['bouncing_level'] === 'flex_boucing'){
1717
$crowdSecStandaloneBouncerConfig['bouncing_level'] = 'flex_bouncing';
1818
}
19-
19+
$bounce->initLogger($crowdSecStandaloneBouncerConfig);
2020
$bouncer = $bounce->init($crowdSecStandaloneBouncerConfig);
2121
$bouncer->refreshBlocklistCache();
2222
echo 'Cache has been refreshed'.PHP_EOL;

scripts/auto-prepend/settings.example.php

-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@
2525
*/
2626
'use_curl' => false,
2727

28-
// HTTP user agent used to call LAPI. Default to this library name/current version.
29-
'api_user_agent'=> 'CrowdSec PHP Library/x.x.x',
30-
3128
// true to enable verbose debug log.
3229
'debug_mode' => false,
3330

scripts/check-ip.php

+3-10
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,12 @@
33
require_once __DIR__ . '/../vendor/autoload.php';
44

55
use CrowdSecBouncer\Bouncer;
6-
use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
7-
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
86
use Monolog\Formatter\LineFormatter;
97
use Monolog\Handler\RotatingFileHandler;
108
use Monolog\Handler\StreamHandler;
119
use Monolog\Logger;
1210

1311

14-
// Init cache adapter
15-
16-
$cacheAdapter = new TagAwareAdapter(new PhpFilesAdapter('', 0, __DIR__ . '/.cache'));
17-
1812
// Parse argument
1913

2014
$requestedIp = $argv[1];
@@ -35,13 +29,12 @@
3529
$logger->pushHandler($fileHandler);
3630

3731
// Init
38-
$bouncer = new Bouncer($cacheAdapter, $logger);
39-
40-
$config = [
32+
$configs = [
4133
'api_key' => $bouncerKey,
4234
'api_url' => 'http://crowdsec:8080',
35+
'fs_cache_path' => __DIR__ . '/.cache',
4336
];
44-
$bouncer->configure($config);
37+
$bouncer = new Bouncer($configs, $logger);
4538

4639
// Ask remediation to LAPI
4740

scripts/clear-cache.php

+6-11
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
use Monolog\Handler\RotatingFileHandler;
88
use Monolog\Handler\StreamHandler;
99
use Monolog\Logger;
10-
use Symfony\Component\Cache\Adapter\RedisTagAwareAdapter;
11-
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
1210

1311

1412

@@ -25,13 +23,6 @@
2523

2624
// Configure paths
2725
$logPath = __DIR__.'/.crowdsec.log';
28-
$cachePath = __DIR__ . '/.cache';
29-
30-
// Instantiate the "PhpFilesAdapter" cache adapter
31-
$cacheAdapter = new TagAwareAdapter(new Symfony\Component\Cache\Adapter\PhpFilesAdapter('', 0, $cachePath));
32-
// 0Or Redis: $cacheAdapter = new RedisTagAwareAdapter(RedisAdapter::createConnection('redis://your-redis-host:6379'));
33-
// Or Memcached: $cacheAdapter = new TagAwareAdapter(new MemcachedAdapter(MemcachedAdapter::createConnection
34-
//('memcached://your-memcached-host:11211')));
3526

3627
// Instantiate the Stream logger with info level(optional)
3728
$logger = new Logger('example');
@@ -46,8 +37,12 @@
4637
$logger->pushHandler($fileHandler);
4738

4839
// Instantiate the bouncer
49-
$bouncer = new Bouncer($cacheAdapter, $logger);
50-
$bouncer->configure(['api_key' => $bouncerApiKey, 'api_url' => $apiUrl]);
40+
$configs = [
41+
'api_key' => $bouncerApiKey,
42+
'api_url' => 'http://crowdsec:8080',
43+
'fs_cache_path' => __DIR__ . '/.cache',
44+
];
45+
$bouncer = new Bouncer($configs, $logger);
5146

5247
// Clear the cache.
5348
$bouncer->clearCache();

scripts/full-example-live-mode.php

+8-15
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
use Monolog\Handler\RotatingFileHandler;
88
use Monolog\Handler\StreamHandler;
99
use Monolog\Logger;
10-
use Symfony\Component\Cache\Adapter\RedisTagAwareAdapter;
11-
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
1210

1311
// Parse arguments
1412
$bouncerApiKey = $argv[1]; // required
@@ -22,14 +20,7 @@
2220
echo "\nVerify $requestedIp with $apiUrl...\n";
2321

2422
// Configure paths
25-
$logPath = __DIR__.'/../crowdsec.log';
26-
$cachePath = __DIR__ . '/../.cache';
27-
28-
// Instantiate the "PhpFilesAdapter" cache adapter
29-
$cacheAdapter = new TagAwareAdapter(new Symfony\Component\Cache\Adapter\PhpFilesAdapter('', 0, $cachePath));
30-
// 0Or Redis: $cacheAdapter = new RedisTagAwareAdapter(RedisAdapter::createConnection('redis://your-redis-host:6379'));
31-
// Or Memcached: $cacheAdapter = new TagAwareAdapter(new MemcachedAdapter(MemcachedAdapter::createConnection
32-
//('memcached://your-memcached-host:11211')));
23+
$logPath = __DIR__.'/crowdsec.log';
3324

3425
// Instantiate the Stream logger with info level(optional)
3526
$logger = new Logger('example');
@@ -44,17 +35,19 @@
4435
$logger->pushHandler($fileHandler);
4536

4637
// Instantiate the bouncer
47-
$bouncer = new Bouncer($cacheAdapter, $logger);
48-
$bouncer->configure([
38+
$configs = [
4939
'api_key' => $bouncerApiKey,
5040
'api_url' => $apiUrl,
5141
'api_user_agent' => 'MyCMS CrowdSec Bouncer/1.0.0',
5242
'api_timeout' => 1,
5343
'stream_mode' => false,
5444
'max_remediation_level' => 'ban',
55-
'cache_expiration_for_clean_ip' => 300,
56-
'cache_expiration_for_bad_ip' => 30,
57-
]);
45+
'clean_ip_cache_duration' => 300,
46+
'bad_ip_cache_duration' => 30,
47+
'fs_cache_path' => __DIR__ . '/../.cache'
48+
];
49+
$bouncer = new Bouncer($configs, $logger);
50+
5851

5952
// Ask remediation to LAPI
6053
$remediation = $bouncer->getRemediationForIp($requestedIp);

scripts/refresh-cache.php

+8-18
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,9 @@
55
use CrowdSecBouncer\Bouncer;
66
use Monolog\Handler\RotatingFileHandler;
77
use Monolog\Logger;
8-
use Symfony\Component\Cache\Adapter\RedisTagAwareAdapter;
9-
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
108

119
// Configure paths
12-
$logPath = __DIR__.'/.crowdsec.log';
13-
$cachePath = __DIR__ . '/.cache';
14-
15-
// Instantiate the "PhpFilesAdapter" cache adapter
16-
$cacheAdapter = new TagAwareAdapter(new Symfony\Component\Cache\Adapter\PhpFilesAdapter('', 0, $cachePath));
17-
// Or Redis: $cacheAdapter = new RedisTagAwareAdapter(RedisAdapter::createConnection('redis://your-redis-host:6379'));
18-
// Or Memcached: $cacheAdapter = new TagAwareAdapter(new MemcachedAdapter(MemcachedAdapter::createConnection
19-
//('memcached://your-memcached-host:11211')));
20-
// Parse argument
10+
$logPath = __DIR__ . '/.crowdsec.log';
2111

2212
$bouncerKey = $argv[1];
2313
if (!$bouncerKey) {
@@ -26,16 +16,16 @@
2616

2717
// Instantiate the Stream logger with info level(optional)
2818
$logger = new Logger('example');
29-
$fileHandler = new RotatingFileHandler(__DIR__.'/crowdsec.log', 0, Logger::WARNING);
19+
$fileHandler = new RotatingFileHandler(__DIR__ . '/crowdsec.log', 0, Logger::WARNING);
3020
$logger->pushHandler($fileHandler);
3121

3222
// Instantiate the bouncer
33-
$bouncer = new Bouncer($cacheAdapter, $logger);
34-
$bouncer->configure([
35-
'api_key' => $bouncerKey,
36-
'api_url' => 'http://crowdsec:8080'
37-
]
38-
);
23+
$configs = [
24+
'api_key' => $bouncerKey,
25+
'api_url' => 'http://crowdsec:8080',
26+
'fs_cache_path' => __DIR__ . '/.cache'
27+
];
28+
$bouncer = new Bouncer($configs, $logger);
3929

4030
// Refresh the blocklist cache
4131
$bouncer->refreshBlocklistCache();

0 commit comments

Comments
 (0)