Skip to content

Commit 629345b

Browse files
Merge pull request #72 from julienloizelet/feature/refactor-logic-and-scripts
Feature/refactor logic and scripts
2 parents bb513d1 + 7e3de55 commit 629345b

29 files changed

+727
-391
lines changed

.github/workflows/test-suite.yml

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ jobs:
2929
with:
3030
path: .ddev
3131
repository: julienloizelet/ddev-php
32+
ref: "main"
3233

3334
- name: Install DDEV
3435
env:
@@ -72,6 +73,9 @@ jobs:
7273
with:
7374
path: my-own-modules/crowdsec-php-lib
7475

76+
- name: Validate composer.json
77+
run: ddev composer validate --strict --working-dir ./my-own-modules/crowdsec-php-lib
78+
7579
- name: Install CrowdSec lib dependencies
7680
run: |
7781
ddev composer update --working-dir ./my-own-modules/crowdsec-php-lib
@@ -93,7 +97,7 @@ jobs:
9397
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
9498
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
9599
96-
- name: Prepare END TO END tests
100+
- name: Prepare Standalone Bouncer end-to-end tests
97101
run: |
98102
ddev create-watcher
99103
cd ${{ github.workspace }}/.ddev
@@ -102,35 +106,49 @@ jobs:
102106
cp .ddev/custom_files/crowdsec-lib-settings.php crowdsec-lib-settings.php
103107
sed -i -e 's/REPLACE_API_KEY/${{ env.BOUNCER_KEY }}/g' crowdsec-lib-settings.php
104108
sed -i -e 's/REPLACE_PROXY_IP/${{ env.PROXY_IP }}/g' crowdsec-lib-settings.php
105-
mv crowdsec-lib-settings.php my-own-modules/crowdsec-php-lib/examples/auto-prepend/settings.php
109+
mv crowdsec-lib-settings.php my-own-modules/crowdsec-php-lib/scripts/auto-prepend/settings.php
106110
cd ${{ github.workspace }}/my-own-modules/crowdsec-php-lib/tests/end-to-end/__scripts__
107111
chmod +x test-init.sh
108112
./test-init.sh
109113
chmod +x run-tests.sh
114+
115+
- name: Verify auto_prepend_file directive
116+
run: |
117+
cd ${{ github.workspace }}
118+
cp .ddev/custom_files/phpinfo.php my-own-modules/crowdsec-php-lib/scripts/public/phpinfo.php
119+
PREPENDVERIF=$(curl https://${{ env.PHP_VERSION_CODE }}.ddev.site/my-own-modules/crowdsec-php-lib/scripts/public/phpinfo.php | grep -o -E "auto_prepend_file=(.*)php(.*)" | sed 's/<\/tr>//g; s/<\/td>//g;' | tr '\n' '#')
120+
if [[ $PREPENDVERIF == "auto_prepend_file=/var/www/html/my-own-modules/crowdsec-php-lib/scripts/auto-prepend/bounce.php#auto_prepend_file=/var/www/html/my-own-modules/crowdsec-php-lib/scripts/auto-prepend/bounce.php#" ]]
121+
then
122+
echo "AUTO PREPEND FILE OK"
123+
else
124+
echo "AUTO PREPEND FILE KO"
125+
echo $PREPENDVERIF
126+
exit 1
127+
fi
110128
111-
- name: Run End to end test (live mode without geolocation)
129+
- name: Run Standalone Bouncer end-to-end test (live mode without geolocation)
112130
run: |
113131
cd ${{ github.workspace }}/my-own-modules/crowdsec-php-lib/tests/end-to-end/__scripts__
114132
./run-tests.sh ci "./__tests__/1-live-mode.js"
115133
116-
- name: Run End to end test (live mode with geolocation)
134+
- name: Run Standalone Bouncer end-to-end test (live mode with geolocation)
117135
run: |
118136
cd ${{ github.workspace }}/my-own-modules/crowdsec-php-lib
119-
sed -i 's/\x27enabled\x27 => false/\x27enabled\x27 => true/g' examples/auto-prepend/settings.php
120-
sed -i 's/\x27forced_test_ip\x27 => \x27\x27/\x27forced_test_ip\x27 => \x27210.249.74.42\x27/g' examples/auto-prepend/settings.php
137+
sed -i 's/\x27enabled\x27 => false/\x27enabled\x27 => true/g' scripts/auto-prepend/settings.php
138+
sed -i 's/\x27forced_test_ip\x27 => \x27\x27/\x27forced_test_ip\x27 => \x27210.249.74.42\x27/g' scripts/auto-prepend/settings.php
121139
cd ${{ github.workspace }}/my-own-modules/crowdsec-php-lib/tests/end-to-end/__scripts__
122140
./run-tests.sh ci "./__tests__/2-live-mode-with-geolocation.js"
123141
124-
- name: Run End to end test (stream mode without geolocation)
142+
- name: Run Standalone Bouncer end-to-end test (stream mode without geolocation)
125143
run: |
126144
cd ${{ github.workspace }}/my-own-modules/crowdsec-php-lib
127-
sed -i 's/\x27enabled\x27 => true/\x27enabled\x27 => false/g' examples/auto-prepend/settings.php
128-
sed -i 's/\x27forced_test_ip\x27 => \x27210.249.74.42\x27/\x27forced_test_ip\x27 => \x27\x27/g' examples/auto-prepend/settings.php
129-
sed -i 's/\x27stream_mode\x27 => false/\x27stream_mode\x27 => true/g' examples/auto-prepend/settings.php
145+
sed -i 's/\x27enabled\x27 => true/\x27enabled\x27 => false/g' scripts/auto-prepend/settings.php
146+
sed -i 's/\x27forced_test_ip\x27 => \x27210.249.74.42\x27/\x27forced_test_ip\x27 => \x27\x27/g' scripts/auto-prepend/settings.php
147+
sed -i 's/\x27stream_mode\x27 => false/\x27stream_mode\x27 => true/g' scripts/auto-prepend/settings.php
130148
cd ${{ github.workspace }}/my-own-modules/crowdsec-php-lib/tests/end-to-end/__scripts__
131149
./run-tests.sh ci "./__tests__/3-stream-mode.js"
132150
133-
- name: Run End to end test (standalone geolocation)
151+
- name: Run Standalone Bouncer end-to-end test (standalone geolocation)
134152
run: |
135153
cd ${{ github.workspace }}/my-own-modules/crowdsec-php-lib/tests/end-to-end/__scripts__
136154
./run-tests.sh ci "./__tests__/4-geolocation.js"

.gitignore

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ super-linter.log
1616
.cache
1717

1818
# Auto prepend demo
19-
examples/auto-prepend/settings.php
20-
examples/auto-prepend/.logs
21-
examples/auto-prepend/.cache
22-
examples/**/*.log
19+
scripts/auto-prepend/settings.php
20+
scripts/auto-prepend/.logs
21+
scripts/auto-prepend/.cache
22+
scripts/**/*.log
2323

2424
# MaxMind databases
2525
*.mmdb

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
{
2727
"name": "Lucas Cherifi",
2828
"email": "[email protected]"
29+
},
30+
{
31+
"name": "Julien Loizelet",
32+
"homepage": "https://github.com/julienloizelet/",
33+
"role": "Developer"
2934
}
3035
],
3136
"require": {
@@ -38,9 +43,7 @@
3843
"geoip2/geoip2": "^2.12.2"
3944
},
4045
"require-dev": {
41-
"symfony/var-dumper": "^5.2",
4246
"phpunit/phpunit": "8.5.21",
43-
"clean/phpdoc-md": "^0.19.1",
4447
"phpmd/phpmd": "@stable",
4548
"squizlabs/php_codesniffer": "^3.6.2"
4649
}

docs/DEVELOPER.md

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,16 @@ For a quick start, follow the below steps.
6666

6767
#### DDEV installation
6868

69-
Please follow the [official instructions](https://ddev.readthedocs.io/en/stable/#installation). On a Linux
70-
distribution, this should be as simple as
71-
72-
sudo apt-get install linuxbrew-wrapper
73-
brew tap drud/ddev && brew install ddev
69+
This project is fully compatible with DDEV 1.18.2 and it is recommended to use this specific version.
70+
For the DDEV installation, please follow the [official instructions](https://ddev.readthedocs.io/en/stable/#installation).
71+
On a Linux distribution, you can run:
72+
```
73+
sudo apt-get -qq update
74+
sudo apt-get -qq -y install libnss3-tools
75+
curl -LO https://raw.githubusercontent.com/drud/ddev/master/scripts/install_ddev.sh
76+
bash install_ddev.sh v1.18.2
77+
rm install_ddev.sh
78+
```
7479

7580

7681
#### Prepare DDEV PHP environment
@@ -165,6 +170,27 @@ Run:
165170
ddev composer update --working-dir ./my-own-modules/crowdsec-php-lib
166171
```
167172

173+
#### Find IP of your docker services
174+
175+
In most cases, you will test to bounce your current IP. As we are running on a docker stack, this is the local host IP.
176+
177+
To find it, just run:
178+
179+
```
180+
ddev find-ip
181+
```
182+
183+
You will have to know also the IP of the `ddev-router` container as it acts as a proxy, and you should set it in the
184+
`trust_ip_forward_array` setting.
185+
186+
To find this IP, just run:
187+
188+
```
189+
ddev find-ip ddev-router
190+
```
191+
192+
193+
168194
#### Unit test
169195

170196
First, create a bouncer and keep the result key.
@@ -201,12 +227,12 @@ ddev exec BOUNCER_KEY=your-bouncer-key LAPI_URL=http://crowdsec:8080 /usr/bin/p
201227
#### Auto-prepend mode (standalone mode)
202228

203229
Before using the bouncer in a standalone mode (i.e. with an auto-prepend directive), you should copy the
204-
`examples/auto-prepend/settings.example.php` file to a `examples/auto-prepend/settings.php` and edit it depending on
205-
your needs.
230+
[`scripts/auto-prepend/settings.example.php`](../scripts/auto-prepend/settings.example.php) file to a `scripts/auto-prepend/settings.
231+
php` and edit it depending on your needs.
206232

207233

208234
Then, to configure the Nginx service in order that it uses an auto-prepend directive pointing to the
209-
`examples/auto-prepend/scripts/bounce-via-auto-prepend.php` script, please run the
235+
[`scripts/auto-prepend/bounce.php`](../scripts/auto-prepend/bounce.php) script, please run the
210236
following command from the `.ddev` folder:
211237

212238
```bash
@@ -219,7 +245,7 @@ be bounce.
219245
For example, you should try to browse the following url:
220246

221247
```
222-
https://phpXX.ddev.site/my-own-modules/crowdsec-php-lib/examples/auto-prepend/public/protected-page.php
248+
https://phpXX.ddev.site/my-own-modules/crowdsec-php-lib/scripts/public/protected-page.php
223249
```
224250

225251
#### End-to-end tests
@@ -304,7 +330,7 @@ ddev phpcbf ./my-own-modules/crowdsec-php-lib/vendor/bin/phpcs my-own-modules/cr
304330

305331
We use a post-start DDEV hook to:
306332
- Create a bouncer
307-
- Set bouncer key, api url and other needed values in the `examples/auto-prepend/settings.php` file (useful to test
333+
- Set bouncer key, api url and other needed values in the `scripts/auto-prepend/settings.php` file (useful to test
308334
standalone mode).
309335
- Create a watcher that we use in end-to-end tests
310336

@@ -323,25 +349,25 @@ ddev restart
323349
> - the cap remediation level
324350
> - how to get the logged events
325351
326-
You will find some php scripts in the `examples` folder.
352+
You will find some php scripts in the `scripts` folder.
327353

328354
**N.B** : If you are not using DDEV, you can replace all `ddev exec php ` by `php` and specify the right script paths.
329355

330356
### Check IP script
331357

332-
The `check-ip` script will get the remediation (`bypass`, `captcha` or `ban`) for some IP.
358+
The [`check-ip`](../scripts/check-ip.php) script will get the remediation (`bypass`, `captcha` or `ban`) for some IP.
333359

334360
To run this script, you have to know your bouncer key `<BOUNCER_KEY>` and run
335361
```bash
336-
ddev exec php my-own-modules/crowdsec-php-lib/examples/check-ip.php <IP> <BOUNCER_KEY>
362+
ddev exec php my-own-modules/crowdsec-php-lib/scripts/check-ip.php <IP> <BOUNCER_KEY>
337363
```
338364

339365
As a reminder, your bouncer key is returned by the `ddev create-bouncer` command.
340366

341367
For example, run the php script:
342368

343369
```bash
344-
ddev exec php my-own-modules/crowdsec-php-lib/examples/check-ip.php 1.2.3.4 <BOUNCER_KEY>
370+
ddev exec php my-own-modules/crowdsec-php-lib/scripts/check-ip.php 1.2.3.4 <BOUNCER_KEY>
345371
```
346372

347373
As your CrowdSec instance contains no decisions, you received the result "bypass".
@@ -355,7 +381,7 @@ ddev exec -s crowdsec cscli decisions add --range 1.2.3.4/30 --duration 4h --typ
355381
Now, if you run the php script against the `1.2.3.4` IP:
356382

357383
```bash
358-
ddev exec php my-own-modules/crowdsec-php-lib/examples/check-ip.php 1.2.3.4 <BOUNCER_KEY>
384+
ddev exec php my-own-modules/crowdsec-php-lib/scripts/check-ip.php 1.2.3.4 <BOUNCER_KEY>
359385
```
360386

361387
LAPI will advise you to ban this IP as it's within the 1.2.3.4/30 range.
@@ -380,7 +406,7 @@ $bouncer->configure([
380406
Now if you call one more time:
381407

382408
```bash
383-
ddev exec php my-own-modules/crowdsec-php-lib/examples/check-ip.php 1.2.3.4 <BOUNCER_KEY>
409+
ddev exec php my-own-modules/crowdsec-php-lib/scripts/check-ip.php 1.2.3.4 <BOUNCER_KEY>
384410
```
385411

386412
The library will cap the value to `captcha` level.
@@ -443,24 +469,24 @@ $cacheAdapter = new MemcachedAdapter(MemcachedAdapter::createConnection('memcach
443469
You will still be able to verify IPs, but the cache system will be more efficient.
444470

445471
```bash
446-
ddev exec php my-own-modules/crowdsec-php-lib/examples/check-ip.php 1.2.3.4 <BOUNCER_KEY>
472+
ddev exec php my-own-modules/crowdsec-php-lib/scripts/check-ip.php 1.2.3.4 <BOUNCER_KEY>
447473
```
448474

449475
> 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).
450476
451477
### Clear cache script
452478

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

455481
```bash
456-
ddev exec php my-own-modules/crowdsec-php-lib/examples/clear-cache.php <BOUNCER_KEY>
482+
ddev exec php my-own-modules/crowdsec-php-lib/scripts/clear-cache.php <BOUNCER_KEY>
457483
```
458484

459485
### Full Live mode example
460486

461487
This example demonstrates how the PHP Lib works with cache when you are using the live mode.
462488

463-
Let's get started and follow the guide!
489+
We will use here the [`full-example-live-mode.php`](../scripts/full-example-live-mode.php).
464490

465491
#### Set up the context
466492

@@ -482,7 +508,7 @@ Try with the `full-example-live-mode.php` file:
482508

483509

484510
```bash
485-
ddev exec php my-own-modules/crowdsec-php-lib/examples/live-mode/full-example-live-mode.php <YOUR_BOUNCER_KEY> 1.2.3.4 http://crowdsec:8080
511+
ddev exec php my-own-modules/crowdsec-php-lib/scripts/full-example-live-mode.php <YOUR_BOUNCER_KEY> 1.2.3.4 http://crowdsec:8080
486512
```
487513

488514
#### Simulate LAPI down by using a bad url
@@ -491,7 +517,7 @@ If you run this script twice, LAPI will not be called, the cache system will rel
491517
You can this behaviour by testing with a bad LAPI url.
492518

493519
```bash
494-
ddev exec php my-own-modules/crowdsec-php-lib/examples/live-mode/full-example-live-mode.php <YOUR_BOUNCER_KEY> 1.2.3.4 http://crowdsec:BAD
520+
ddev exec php my-own-modules/crowdsec-php-lib/scripts/full-example-live-mode.php <YOUR_BOUNCER_KEY> 1.2.3.4 http://crowdsec:BAD
495521
```
496522

497523
As you can see, you can check the API event if LAPI is down. This is because of the caching system.
@@ -507,13 +533,13 @@ ddev exec -s crowdsec cscli decisions add --range 1.2.3.4/30 --duration 12h --ty
507533
Clear the cache:
508534

509535
```bash
510-
ddev exec php my-own-modules/crowdsec-php-lib/examples/clear-cache.php <YOUR_BOUNCER_KEY>
536+
ddev exec php my-own-modules/crowdsec-php-lib/scripts/clear-cache.php <YOUR_BOUNCER_KEY>
511537
```
512538

513539
One more time, get the remediation for the IP "1.2.3.4":
514540

515541
```bash
516-
ddev exec php my-own-modules/crowdsec-php-lib/examples/live-mode/full-example-live-mode.php <YOUR_BOUNCER_KEY> 1.2.3.4 http://crowdsec:8080
542+
ddev exec php my-own-modules/crowdsec-php-lib/scripts/full-example-live-mode.php <YOUR_BOUNCER_KEY> 1.2.3.4 http://crowdsec:8080
517543
```
518544

519545
This is a ban (and cache miss) as you can see in your terminal logs.

0 commit comments

Comments
 (0)