Skip to content

Commit 26a6906

Browse files
Merge pull request #97 from julienloizelet/feat/test-ipv6
Feat/test ipv6
2 parents 41a01e7 + 9e7dba4 commit 26a6906

19 files changed

+423
-211
lines changed

.github/workflows/test-suite.yml

+47-25
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ jobs:
1717
name: Test suite
1818
runs-on: ubuntu-latest
1919
if: ${{ !contains(github.event.head_commit.message, 'chore(') }}
20+
env:
21+
EXTENSION_PATH: "my-own-modules/crowdsec-php-lib"
22+
JP_TEST_IP: "210.249.74.42"
23+
IPV6_TEST_IP: "2001:0db8:0000:85a3:0000:0000:ac1f:8001"
24+
IPV6_TEST_PROXY_IP: "2345:0425:2CA1:0000:0000:0567:5673:23b5"
2025

2126
steps:
2227
- name: Clone DDEV files
@@ -70,18 +75,18 @@ jobs:
7075
path: my-own-modules/crowdsec-php-lib
7176

7277
- name: Validate composer.json
73-
run: ddev composer validate --strict --working-dir ./my-own-modules/crowdsec-php-lib
78+
run: ddev composer validate --strict --working-dir ./${{env.EXTENSION_PATH}}
7479

7580
- name: Install CrowdSec lib dependencies
7681
run: |
77-
ddev composer update --working-dir ./my-own-modules/crowdsec-php-lib
82+
ddev composer update --working-dir ./${{env.EXTENSION_PATH}}
7883
7984
- name: Prepare PHP UNIT tests
8085
run: |
8186
ddev create-watcher PhpUnitTestMachine PhpUnitTestMachinePassword
82-
ddev maxmind-download DEFAULT GeoLite2-City /var/www/html/my-own-modules/crowdsec-php-lib/tests
83-
ddev maxmind-download DEFAULT GeoLite2-Country /var/www/html/my-own-modules/crowdsec-php-lib/tests
84-
cd my-own-modules/crowdsec-php-lib/tests
87+
ddev maxmind-download DEFAULT GeoLite2-City /var/www/html/${{env.EXTENSION_PATH}}/tests
88+
ddev maxmind-download DEFAULT GeoLite2-Country /var/www/html/${{env.EXTENSION_PATH}}/tests
89+
cd ${{env.EXTENSION_PATH}}/tests
8590
sha256sum -c GeoLite2-Country.tar.gz.sha256.txt
8691
sha256sum -c GeoLite2-City.tar.gz.sha256.txt
8792
tar -xf GeoLite2-Country.tar.gz
@@ -90,36 +95,38 @@ jobs:
9095
9196
- name: Run PHP UNIT tests (IP verification)
9297
run: |
93-
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/Integration/IpVerificationTest.php
98+
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
9499
95100
- name: Run PHP UNIT tests (Geolocation)
96101
run: |
97-
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/Integration/GeolocationTest.php
102+
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
98103
99104
- name: Prepare Standalone Bouncer end-to-end tests
100105
run: |
101106
ddev create-watcher
102107
cd ${{ github.workspace }}/.ddev
103108
ddev nginx-config custom_files/crowdsec-prepend-nginx-site.conf
104109
cd ${{ github.workspace }}
105-
cp .ddev/custom_files/crowdsec/cache-actions.php my-own-modules/crowdsec-php-lib/scripts/public/cache-actions.php
106-
cp .ddev/custom_files/crowdsec/geolocation-test.php my-own-modules/crowdsec-php-lib/scripts/public/geolocation-test.php
107-
cp .ddev/custom_files/crowdsec-lib-settings.php crowdsec-lib-settings.php
110+
cp ${{env.EXTENSION_PATH}}/tests/end-to-end/php-scripts/cache-actions.php.dist ${{env.EXTENSION_PATH}}/scripts/public/cache-actions.php
111+
cp ${{env.EXTENSION_PATH}}/tests/end-to-end/php-scripts/geolocation-test.php.dist ${{env.EXTENSION_PATH}}/scripts/public/geolocation-test.php
112+
cp ${{env.EXTENSION_PATH}}/tests/end-to-end/settings/base.php.dist crowdsec-lib-settings.php
108113
sed -i -e 's/REPLACE_API_KEY/${{ env.BOUNCER_KEY }}/g' crowdsec-lib-settings.php
109114
sed -i -e 's/REPLACE_PROXY_IP/${{ env.PROXY_IP }}/g' crowdsec-lib-settings.php
110-
mv crowdsec-lib-settings.php my-own-modules/crowdsec-php-lib/scripts/auto-prepend/settings.php
111-
cd ${{ github.workspace }}/my-own-modules/crowdsec-php-lib/tests/end-to-end/__scripts__
115+
sed -i -e 's/REPLACE_FORCED_IP//g' crowdsec-lib-settings.php
116+
sed -i -e 's/REPLACE_FORCED_FORWARDED_IP//g' crowdsec-lib-settings.php
117+
mv crowdsec-lib-settings.php ${{env.EXTENSION_PATH}}/scripts/auto-prepend/settings.php
118+
cd ${{ github.workspace }}/${{env.EXTENSION_PATH}}/tests/end-to-end/__scripts__
112119
chmod +x test-init.sh
113120
./test-init.sh
114121
chmod +x run-tests.sh
115122
116123
- name: Verify auto_prepend_file directive
117124
run: |
118125
cd ${{ github.workspace }}
119-
cp .ddev/custom_files/phpinfo.php my-own-modules/crowdsec-php-lib/scripts/public/phpinfo.php
120-
curl -v https://${{ env.PHP_VERSION_CODE }}.ddev.site/my-own-modules/crowdsec-php-lib/scripts/public/phpinfo.php
121-
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' '#')
122-
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#" ]]
126+
cp .ddev/custom_files/phpinfo.php ${{env.EXTENSION_PATH}}/scripts/public/phpinfo.php
127+
curl -v https://${{ env.PHP_VERSION_CODE }}.ddev.site/${{env.EXTENSION_PATH}}/scripts/public/phpinfo.php
128+
PREPENDVERIF=$(curl https://${{ env.PHP_VERSION_CODE }}.ddev.site/${{env.EXTENSION_PATH}}/scripts/public/phpinfo.php | grep -o -E "auto_prepend_file=(.*)php(.*)" | sed 's/<\/tr>//g; s/<\/td>//g;' | tr '\n' '#')
129+
if [[ $PREPENDVERIF == "auto_prepend_file=/var/www/html/${{env.EXTENSION_PATH}}/scripts/auto-prepend/bounce.php#auto_prepend_file=/var/www/html/my-own-modules/crowdsec-php-lib/scripts/auto-prepend/bounce.php#" ]]
123130
then
124131
echo "AUTO PREPEND FILE OK"
125132
else
@@ -130,27 +137,42 @@ jobs:
130137
131138
- name: Run Standalone Bouncer end-to-end test (live mode without geolocation)
132139
run: |
133-
cd ${{ github.workspace }}/my-own-modules/crowdsec-php-lib/tests/end-to-end/__scripts__
140+
cd ${{ github.workspace }}/${{env.EXTENSION_PATH}}
141+
cat scripts/auto-prepend/settings.php
142+
cd ${{ github.workspace }}/${{env.EXTENSION_PATH}}/tests/end-to-end/__scripts__
134143
./run-tests.sh ci "./__tests__/1-live-mode.js"
135144
136145
- name: Run Standalone Bouncer end-to-end test (live mode with geolocation)
137146
run: |
138-
cd ${{ github.workspace }}/my-own-modules/crowdsec-php-lib
147+
cd ${{ github.workspace }}/${{env.EXTENSION_PATH}}
139148
sed -i 's/\x27enabled\x27 => false/\x27enabled\x27 => true/g' scripts/auto-prepend/settings.php
140-
sed -i 's/\x27forced_test_ip\x27 => \x27\x27/\x27forced_test_ip\x27 => \x27210.249.74.42\x27/g' scripts/auto-prepend/settings.php
141-
cd ${{ github.workspace }}/my-own-modules/crowdsec-php-lib/tests/end-to-end/__scripts__
149+
sed -i 's/\x27forced_test_forwarded_ip\x27 => \x27\x27/\x27forced_test_forwarded_ip\x27 => \x27${{env.JP_TEST_IP}}\x27/g' scripts/auto-prepend/settings.php
150+
cat scripts/auto-prepend/settings.php
151+
cd ${{ github.workspace }}/${{env.EXTENSION_PATH}}/tests/end-to-end/__scripts__
142152
./run-tests.sh ci "./__tests__/2-live-mode-with-geolocation.js"
143153
144154
- name: Run Standalone Bouncer end-to-end test (stream mode without geolocation)
145155
run: |
146-
cd ${{ github.workspace }}/my-own-modules/crowdsec-php-lib
156+
cd ${{ github.workspace }}/${{env.EXTENSION_PATH}}
147157
sed -i 's/\x27enabled\x27 => true/\x27enabled\x27 => false/g' scripts/auto-prepend/settings.php
148-
sed -i 's/\x27forced_test_ip\x27 => \x27210.249.74.42\x27/\x27forced_test_ip\x27 => \x27\x27/g' scripts/auto-prepend/settings.php
158+
sed -i 's/\x27forced_test_forwarded_ip\x27 => \x27${{env.JP_TEST_IP}}\x27/\x27forced_test_forwarded_ip\x27 => \x27\x27/g' scripts/auto-prepend/settings.php
149159
sed -i 's/\x27stream_mode\x27 => false/\x27stream_mode\x27 => true/g' scripts/auto-prepend/settings.php
150-
cd ${{ github.workspace }}/my-own-modules/crowdsec-php-lib/tests/end-to-end/__scripts__
160+
cat scripts/auto-prepend/settings.php
161+
cd ${{ github.workspace }}/${{env.EXTENSION_PATH}}/tests/end-to-end/__scripts__
151162
./run-tests.sh ci "./__tests__/3-stream-mode.js"
152163
153164
- name: Run Standalone Bouncer end-to-end test (standalone geolocation)
154165
run: |
155-
cd ${{ github.workspace }}/my-own-modules/crowdsec-php-lib/tests/end-to-end/__scripts__
156-
./run-tests.sh ci "./__tests__/4-geolocation.js"
166+
cd ${{ github.workspace }}/${{env.EXTENSION_PATH}}/tests/end-to-end/__scripts__
167+
./run-tests.sh ci "./__tests__/4-geolocation.js"
168+
169+
- name: Run Standalone Bouncer end-to-end test (live mode with IPv6)
170+
run: |
171+
cd ${{ github.workspace }}/${{env.EXTENSION_PATH}}
172+
sed -i 's/\x27forced_test_forwarded_ip\x27 => \x27\x27/\x27forced_test_forwarded_ip\x27 => \x27${{env.IPV6_TEST_IP}}\x27/g' scripts/auto-prepend/settings.php
173+
sed -i 's/\x27forced_test_ip\x27 => \x27\x27/\x27forced_test_ip\x27 => \x27${{env.IPV6_TEST_PROXY_IP}}\x27/g' scripts/auto-prepend/settings.php
174+
sed -i -e 's/${{ env.PROXY_IP }}/${{env.IPV6_TEST_PROXY_IP}}/g' scripts/auto-prepend/settings.php
175+
sed -i 's/\x27stream_mode\x27 => true/\x27stream_mode\x27 => false/g' scripts/auto-prepend/settings.php
176+
cat scripts/auto-prepend/settings.php
177+
cd ${{ github.workspace }}/${{env.EXTENSION_PATH}}/tests/end-to-end/__scripts__
178+
./run-tests.sh ci "./__tests__/1-live-mode.js"

.github/workflows/tests.yml

-103
This file was deleted.

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ 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.23.0] - 2022-07-07
9+
10+
### Added
11+
- Add test configuration to mock IPs and proxy behavior
12+
813
## [0.22.1] - 2022-06-03
914

1015
### Fixed

docs/USER_GUIDE.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,12 @@ Here is the list of available settings:
134134

135135
- `display_errors`: true to stop the process and display errors on browser if any.
136136

137-
- `forced_test_ip`: Only for test or debug purpose. Default to empty. If not empty, it will be used for all remediation and geolocation processes.
137+
- `forced_test_ip`: Only for test or debug purpose. Default to empty. If not empty, it will be used instead of the
138+
real remote ip.
139+
140+
- `forced_test_forwarded_ip`: Only for test or debug purpose. Default to empty. If not empty, it will be used
141+
instead of the real forwarded ip. If set to `no_forward`, the x-forwarded-for mechanism will not be used at all.
142+
138143
##### Bouncer behavior
139144

140145
- `bouncing_level`: Select from `bouncing_disabled`, `normal_bouncing` or `flex_bouncing`. Choose if you want to apply CrowdSec directives (Normal bouncing) or be more permissive (Flex bouncing). With the `Flex mode`, it is impossible to accidentally block access to your site to people who don’t deserve it. This mode makes it possible to never ban an IP but only to offer a Captcha, in the worst-case scenario.

scripts/auto-prepend/settings.example.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,18 @@
3535

3636
/** Only for test or debug purpose. Default to empty.
3737
*
38-
* If not empty, it will be used for all remediation and geolocation processes.
38+
* If not empty, it will be used instead of the real remote ip.
3939
*/
4040
'forced_test_ip' => '',
4141

42+
/** Only for test or debug purpose. Default to empty.
43+
*
44+
* If not empty, it will be used instead of the real forwarded ip.
45+
* If set to "no_forward", the x-forwarded-for mechanism will not be used at all.
46+
*
47+
*/
48+
'forced_test_forwarded_ip' => '',
49+
4250
/** Select from 'bouncing_disabled', 'normal_bouncing' or 'flex_bouncing'.
4351
*
4452
* Choose if you want to apply CrowdSec directives (Normal bouncing) or be more permissive (Flex bouncing).

0 commit comments

Comments
 (0)