Skip to content

Commit 009424b

Browse files
Merge pull request #66 from julienloizelet/feature/61-country-decision
Feature/61 country decision Fixes #61
2 parents 8be7c5c + bcc633d commit 009424b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+2727
-670
lines changed

.github/workflows/markdown.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
on:
2+
workflow_dispatch:
3+
4+
name: Markdown files test and update
5+
jobs:
6+
markdown-test-and-update:
7+
name: Markdown files test and update
8+
runs-on: ubuntu-latest
9+
steps:
10+
11+
- name: Clone sources
12+
uses: actions/checkout@v2
13+
with:
14+
path: extension
15+
16+
- name: Launch localhost server
17+
run: |
18+
sudo npm install --global http-server
19+
http-server ./extension &
20+
21+
- name: Set up Ruby 2.6
22+
uses: ruby/setup-ruby@v1
23+
with:
24+
ruby-version: 2.6
25+
26+
- name: Check links in Markdown files
27+
run: |
28+
gem install awesome_bot
29+
cd extension
30+
awesome_bot --files README.md,docs/ddev.md --allow-dupe --allow 401 --skip-save-results --white-list ddev.site,crowdsec --base-url http://localhost:8080/
31+
32+
- name: Generate table of contents
33+
uses: technote-space/toc-generator@v4
34+
with:
35+
MAX_HEADER_LEVEL: 5
36+
COMMIT_NAME: CrowdSec Dev Bot
37+
TARGET_PATHS: 'docs/ddev.md'
38+
CHECK_ONLY_DEFAULT_BRANCH: true

.github/workflows/test-suite.yml

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
name: Test suite
2+
on:
3+
push:
4+
pull_request:
5+
branches:
6+
- main
7+
workflow_dispatch:
8+
9+
jobs:
10+
test-suite:
11+
strategy:
12+
fail-fast: false
13+
matrix:
14+
php-version: ['7.2', '7.3', '7.4', '8.0', '8.1']
15+
16+
name: Test suite
17+
runs-on: ubuntu-latest
18+
19+
steps:
20+
- name: Clone DDEV files
21+
uses: actions/checkout@v2
22+
with:
23+
path: .ddev
24+
repository: julienloizelet/ddev-php
25+
26+
- name: Install DDEV
27+
env:
28+
DDEV_VERSION: v1.18.2
29+
run: |
30+
# @see https://ddev.readthedocs.io/en/stable/#installationupgrade-script-linux-and-macos-armarm64-and-amd64-architectures
31+
sudo apt-get -qq update
32+
sudo apt-get -qq -y install libnss3-tools
33+
curl -LO https://raw.githubusercontent.com/drud/ddev/master/scripts/install_ddev.sh
34+
bash install_ddev.sh ${{env.DDEV_VERSION}}
35+
ddev config global --instrumentation-opt-in=false --omit-containers=dba,ddev-ssh-agent
36+
rm install_ddev.sh
37+
38+
- name: Set PHP_VERSION_CODE env
39+
# used in some directory path and conventional file naming
40+
# Example : 7.4 => 74
41+
run: |
42+
echo "PHP_VERSION_CODE=$(echo php${{ matrix.php-version }} | sed 's/\.//g' )" >> $GITHUB_ENV
43+
44+
45+
- name: Start DDEV with PHP ${{ matrix.php-version }}
46+
run: |
47+
cp .ddev/config_overrides/config.${{ env.PHP_VERSION_CODE }}.yaml .ddev/config.${{ env.PHP_VERSION_CODE }}.yaml
48+
cp .ddev/additional_docker_compose/docker-compose.crowdsec.yaml .ddev/docker-compose.crowdsec.yaml
49+
cp .ddev/additional_docker_compose/docker-compose.playwright.yaml .ddev/docker-compose.playwright.yaml
50+
ddev start
51+
52+
- name: Set BOUNCER_KEY and PROXY_IP env
53+
run: |
54+
echo "BOUNCER_KEY=$(ddev create-bouncer)" >> $GITHUB_ENV
55+
echo "PROXY_IP=$(ddev find-ip ddev-router)" >> $GITHUB_ENV
56+
57+
- name: Some DEBUG information
58+
run: |
59+
ddev --version
60+
ddev exec php -v
61+
ddev exec -s crowdsec crowdsec -version
62+
63+
- name: Clone PHP lib Crowdsec files
64+
uses: actions/checkout@v2
65+
with:
66+
path: my-own-modules/crowdsec-php-lib
67+
68+
- name: Install CrowdSec lib dependencies
69+
run: |
70+
ddev composer update --working-dir ./my-own-modules/crowdsec-php-lib
71+
72+
- name: Prepare PHP UNIT tests
73+
run: |
74+
ddev create-watcher PhpUnitTestMachine PhpUnitTestMachinePassword
75+
ddev maxmind-download DEFAULT GeoLite2-City /var/www/html/my-own-modules/crowdsec-php-lib/tests
76+
ddev maxmind-download DEFAULT GeoLite2-Country /var/www/html/my-own-modules/crowdsec-php-lib/tests
77+
cd my-own-modules/crowdsec-php-lib/tests
78+
sha256sum -c GeoLite2-Country.tar.gz.sha256.txt
79+
sha256sum -c GeoLite2-City.tar.gz.sha256.txt
80+
tar -xf GeoLite2-Country.tar.gz
81+
tar -xf GeoLite2-City.tar.gz
82+
rm GeoLite2-Country.tar.gz GeoLite2-Country.tar.gz.sha256.txt GeoLite2-City.tar.gz GeoLite2-City.tar.gz.sha256.txt
83+
84+
- name: Run PHP UNIT tests
85+
run: |
86+
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 ./my-own-modules/crowdsec-php-lib/vendor/bin/phpunit --testdox --colors --exclude-group ignore ./my-own-modules/crowdsec-php-lib/tests/IpVerificationTest.php
87+
ddev exec BOUNCER_KEY=${{ env.BOUNCER_KEY }} LAPI_URL=http://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/GeolocationTest.php
88+
89+
- name: Prepare END TO END tests
90+
run: |
91+
ddev create-watcher
92+
cd ${{ github.workspace }}/.ddev
93+
ddev crowdsec-prepend-nginx
94+
cd ${{ github.workspace }}
95+
cp .ddev/custom_files/crowdsec-lib-settings.php crowdsec-lib-settings.php
96+
sed -i -e 's/REPLACE_API_KEY/${{ env.BOUNCER_KEY }}/g' crowdsec-lib-settings.php
97+
sed -i -e 's/REPLACE_PROXY_IP/${{ env.PROXY_IP }}/g' crowdsec-lib-settings.php
98+
mv crowdsec-lib-settings.php my-own-modules/crowdsec-php-lib/examples/auto-prepend/settings.php
99+
cd ${{ github.workspace }}/my-own-modules/crowdsec-php-lib/tests/end-to-end/__scripts__
100+
chmod +x test-init.sh
101+
./test-init.sh
102+
chmod +x run-tests.sh
103+
104+
- name: Run End to end test (live mode without geolocation)
105+
run: |
106+
cd ${{ github.workspace }}/my-own-modules/crowdsec-php-lib/tests/end-to-end/__scripts__
107+
./run-tests.sh ci "./__tests__/1-live-mode.js"
108+
109+
- name: Run End to end test (live mode with geolocation)
110+
run: |
111+
cd ${{ github.workspace }}/my-own-modules/crowdsec-php-lib
112+
sed -i 's/\x27enabled\x27 => false/\x27enabled\x27 => true/g' examples/auto-prepend/settings.php
113+
sed -i 's/\x27forced_test_ip\x27 => \x27\x27/\x27forced_test_ip\x27 => \x27210.249.74.42\x27/g' examples/auto-prepend/settings.php
114+
cd ${{ github.workspace }}/my-own-modules/crowdsec-php-lib/tests/end-to-end/__scripts__
115+
./run-tests.sh ci "./__tests__/2-live-mode-with-geolocation.js"
116+
117+
- name: Run End to end test (stream mode without geolocation)
118+
run: |
119+
cd ${{ github.workspace }}/my-own-modules/crowdsec-php-lib
120+
sed -i 's/\x27enabled\x27 => true/\x27enabled\x27 => false/g' examples/auto-prepend/settings.php
121+
sed -i 's/\x27forced_test_ip\x27 => \x27210.249.74.42\x27/\x27forced_test_ip\x27 => \x27\x27/g' examples/auto-prepend/settings.php
122+
sed -i 's/\x27stream_mode\x27 => false/\x27stream_mode\x27 => true/g' examples/auto-prepend/settings.php
123+
cd ${{ github.workspace }}/my-own-modules/crowdsec-php-lib/tests/end-to-end/__scripts__
124+
./run-tests.sh ci "./__tests__/3-stream-mode.js"
125+
126+
- name: Run End to end test (standalone geolocation)
127+
run: |
128+
cd ${{ github.workspace }}/my-own-modules/crowdsec-php-lib/tests/end-to-end/__scripts__
129+
./run-tests.sh ci "./__tests__/4-geolocation.js"

.gitignore

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ vendor
77

88
#Tools
99
super-linter.log
10-
.php_cs.cache
10+
.php-cs-fixer.cache
11+
.php-cs-fixer.php
1112

1213
# App
1314
/var/
@@ -17,4 +18,7 @@ super-linter.log
1718
# Auto prepend demo
1819
examples/auto-prepend/settings.php
1920
examples/auto-prepend/.logs
20-
examples/auto-prepend/.cache
21+
examples/auto-prepend/.cache
22+
23+
# MaxMind databases
24+
*.mmdb

.php_cs renamed to .php-cs-fixer.dist.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<?php
22

33
if (!file_exists(__DIR__.'/src')) {
4-
exit(0);
4+
exit(1);
55
}
66

7-
return PhpCsFixer\Config::create()
7+
$config = new PhpCsFixer\Config("crowdsec-php-lib");
8+
return $config
89
->setRules([
910
'@Symfony' => true,
1011
'@Symfony:risky' => true,
@@ -22,6 +23,6 @@
2223
->setFinder(
2324
PhpCsFixer\Finder::create()
2425
->in(__DIR__.'/src')
25-
->in(__DIR__.'/tests')
26+
->in(__DIR__.'/tests')->depth(0)
2627
)
2728
;

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
<p align="center">
2-
<img src="https://raw.githubusercontent.com/crowdsecurity/crowdsec-docs/main/docs/assets/images/crowdsec_logo.png" alt="CrowdSec" title="CrowdSec" width="200" height="120"/>
3-
</p>
1+
![CrowdSec Logo](docs/images/logo_crowdsec.png)
42

53
# PHP Bouncer Library
64

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,15 @@
3434
"symfony/cache": "^5.2",
3535
"monolog/monolog": "^1.17 || ^2.1",
3636
"gregwar/captcha": "^1.1",
37-
"mlocati/ip-lib": "^1.14"
37+
"mlocati/ip-lib": "^1.18",
38+
"geoip2/geoip2": "^2.12.2"
3839
},
3940
"require-dev": {
4041
"bramus/monolog-colored-line-formatter": "^3.0",
4142
"symfony/var-dumper": "^5.2",
4243
"phpunit/phpunit": "8.5.21",
43-
"clean/phpdoc-md": "^0.19.1"
44+
"clean/phpdoc-md": "^0.19.1",
45+
"phpmd/phpmd": "@stable",
46+
"squizlabs/php_codesniffer": "^3.6.2"
4447
}
4548
}

docs/api/ApiCache.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ The cache mecanism to store every decisions from LAPI/CAPI. Symfony Cache compon
7575
**Description**
7676

7777
```php
78-
public configure (bool $liveMode, string $apiUrl, int $timeout, string $userAgent, string $apiKey, int $cacheExpirationForCleanIp, int $cacheExpirationForBadIp, string $fallbackRemediation)
78+
public configure (bool $streamMode, string $apiUrl, int $timeout, string $userAgent, string $apiKey, int
79+
$cacheExpirationForCleanIp, int $cacheExpirationForBadIp, string $fallbackRemediation)
7980
```
8081

8182
Configure this instance.
@@ -84,8 +85,8 @@ Configure this instance.
8485

8586
**Parameters**
8687

87-
* `(bool) $liveMode`
88-
: If we use the live mode (else we use the stream mode)
88+
* `(bool) $streamMode`
89+
: If we use the stream mode (else we use the live mode)
8990
* `(string) $apiUrl`
9091
: The URL of the LAPI
9192
* `(int) $timeout`

docs/configuration-reference.md

Lines changed: 74 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Full configuration reference
22

3-
```bash
3+
```php
4+
use CrowdSecBouncer\StandAloneBounce;
45
$config = [
56
// Required. The bouncer api key to access LAPI or CAPI.
67
'api_key'=> 'YOUR_BOUNCER_API_KEY',
@@ -11,25 +12,88 @@
1112
// Optional. HTTP user agent used to call CAPI or LAPI. Default to this library name/current version.
1213
'api_user_agent'=> 'CrowdSec PHP Library/x.x.x',
1314

14-
// Optional. In seconds. The timeout when calling CAPI/LAPI. Defaults to 2 sec.
15-
'api_timeout'=> 2,
15+
// Optional. In seconds. The timeout when calling CAPI/LAPI. Defaults to 1 sec.
16+
'api_timeout'=> 1,
17+
18+
// Optional. Select from 'bouncing_disabled', 'normal_bouncing' or 'flex_bouncing'.
19+
'bouncing_level' => 'normal_bouncing',
20+
21+
// Optional. Absolute path to store log files.
22+
'log_directory_path' => __DIR__.'/.logs',
23+
24+
// Optional. Select from 'phpfs' (File system cache), 'redis' or 'memcached'.
25+
'cache_system' => 'phpfs',
26+
27+
// Optional. Will be used only if you choose File system as cache_system
28+
'fs_cache_path' => __DIR__.'/.cache',
29+
30+
// Optional. Will be used only if you choose Redis cache as cache_system
31+
'redis_dsn' => 'redis://localhost:6379',
32+
33+
// Optional. Will be used only if you choose Memcached as cache_system
34+
'memcached_dsn' => 'memcached://localhost:11211',
35+
36+
// Optional. If you use a CDN, a reverse proxy or a load balancer, set an array of IPs.
37+
// For other IPs, the bouncer will not trust the X-Forwarded-For header.
38+
'trust_ip_forward_array' => [],
1639

17-
// Optional. true to enable live mode, false to enable the stream mode. Default to true.
18-
'live_mode'=> true,
40+
// Optional. true to enable stream mode, true to enable the stream mode. Default to false.
41+
'stream_mode'=> false,
42+
43+
// Optional. true to enable verbose debug log.
44+
'debug_mode' => false,
45+
46+
// Optional. true to stop the process and display errors if any.
47+
'display_errors' => false,
1948

20-
// Optional. Cap the remediation to the selected one. Select from 'bypass' (minimum remediation), 'captcha' or 'ban' (maximum remediation). Defaults to 'ban'.
49+
// Optional. true to hide CrowdSec mentions on ban and captcha walls.
50+
'hide_mentions' => false,
51+
52+
// Optional. Only for test or debug purpose.
53+
// If not empty, it will be used for all remediation and geolocation processes.
54+
// Default to empty
55+
'forced_test_ip' => '1.2.3.4',
56+
57+
// Optional. Cap the remediation to the selected one.
58+
// Select from 'bypass' (minimum remediation),'captcha' or 'ban' (maximum remediation).
59+
// Default to 'ban'.
2160
'max_remediation_level'=> 'ban',
2261

23-
// Optional. Handle unknown remediations as. Select from 'bypass' (minimum remediation), 'captcha' or 'ban' (maximum remediation). Defaults to 'captcha'.
62+
// Optional. Handle unknown remediations as.
63+
// Select from 'bypass' (minimum remediation), 'captcha' or 'ban' (maximum remediation).
64+
// Default to 'captcha'.
2465
'fallback_remediation'=> 'captcha',
2566

2667
// Optional. Set the duration we keep in cache the fact that an IP is clean. In seconds. Defaults to 5.
2768
'cache_expiration_for_clean_ip'=> '5',
2869

2970
// Optional. Set the duration we keep in cache the fact that an IP is bad. In seconds. Defaults to 20.
3071
'cache_expiration_for_bad_ip'=> '20',
72+
73+
// Optional. Settings for geolocation remediation (i.e. country based remediation).
74+
'geolocation' => [
75+
// Optional. true to enable remediation based on country.
76+
// Default to false.
77+
'enabled' => false,
78+
// Optional. Geolocation system. Only 'maxmind' is available for the moment.
79+
// Default to 'maxmind'
80+
'type' => 'maxmind',
81+
// Optional. true to store the geolocalized country in session
82+
// Setting true will avoid multiple call to the geolocalized system (e.g. maxmind database)
83+
// Default to true.
84+
'save_in_session' => true,
85+
// Optional. MaxMind settings
86+
'maxmind' => [
87+
// Optional. Select from 'country' or 'city'.
88+
// These are the two available MaxMind database types.
89+
// Default to 'country'
90+
'database_type' => 'country',
91+
// Optional. Absolute path to the MaxMind database (mmdb file).
92+
'database_path' => '/some/path/GeoLite2-Country.mmdb',
93+
]
94+
]
3195
]
32-
$cacheAdapter = (...)
33-
$bouncer = new Bouncer($cacheAdapter);
34-
$bouncer->configure($config);
96+
$bouncer = new StandAloneBounce();
97+
$bouncer->init($config);
98+
$bouncer->safelyBounce();
3599
```

0 commit comments

Comments
 (0)