Skip to content

Commit cc6a8ee

Browse files
Merge pull request #114 from julienloizelet/feat/timeout-and-php-8-2
Feat/timeout and php 8 2
2 parents 2334a9c + 694b375 commit cc6a8ee

File tree

16 files changed

+72
-42
lines changed

16 files changed

+72
-42
lines changed

.github/workflows/coding-standards.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
strategy:
1313
fail-fast: false
1414
matrix:
15-
php-version: ['7.4', '8.0', '8.1']
15+
php-version: ['7.4', '8.0', '8.1', '8.2']
1616

1717
name: Coding standards
1818
runs-on: ubuntu-latest
@@ -30,7 +30,7 @@ jobs:
3030

3131
- name: Install DDEV
3232
env:
33-
DDEV_VERSION: v1.21.2
33+
DDEV_VERSION: v1.21.4
3434
run: |
3535
# @see https://ddev.readthedocs.io/en/stable/#installationupgrade-script-linux-and-macos-armarm64-and-amd64-architectures
3636
sudo apt-get -qq update

.github/workflows/markdown.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
steps:
1010

1111
- name: Clone sources
12-
uses: actions/checkout@v2
12+
uses: actions/checkout@v3
1313
with:
1414
path: extension
1515

.github/workflows/release.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
echo "VERSION_NUMBER=$(echo ${{ github.event.inputs.tag_name }} | sed 's/v//g' )" >> $GITHUB_ENV
3838
3939
- name: Clone sources
40-
uses: actions/checkout@v2
40+
uses: actions/checkout@v3
4141

4242
- name: Check version ${{ env.VERSION_NUMBER }} consistency in files
4343
# Check src/Constants.php and CHANGELOG.md
@@ -79,11 +79,11 @@ jobs:
7979
fi
8080
8181
- name: Create Tag ${{ github.event.inputs.tag_name }}
82-
uses: actions/github-script@v3
82+
uses: actions/github-script@v6
8383
with:
8484
github-token: ${{ github.token }}
8585
script: |
86-
github.git.createRef({
86+
github.rest.git.createRef({
8787
owner: context.repo.owner,
8888
repo: context.repo.repo,
8989
ref: "refs/tags/${{ github.event.inputs.tag_name }}",

.github/workflows/test-suite.yml

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
strategy:
1313
fail-fast: false
1414
matrix:
15-
php-version: ['7.2', '7.3', '7.4', '8.0', '8.1']
15+
php-version: ['7.2', '7.3', '7.4', '8.0', '8.1', '8.2']
1616

1717
name: Test suite
1818
runs-on: ubuntu-latest
@@ -33,7 +33,7 @@ jobs:
3333

3434
- name: Install DDEV
3535
env:
36-
DDEV_VERSION: v1.21.2
36+
DDEV_VERSION: v1.21.4
3737
run: |
3838
# @see https://ddev.readthedocs.io/en/stable/#installationupgrade-script-linux-and-macos-armarm64-and-amd64-architectures
3939
sudo apt-get -qq update
@@ -315,3 +315,38 @@ jobs:
315315
echo "There are pending tests: $PENDING_TESTS (KO)"
316316
exit 1
317317
fi
318+
319+
320+
- name: Run "stream mode with TLS auth and cURL and Redis" test
321+
run: |
322+
cd ${{ github.workspace }}/${{env.EXTENSION_PATH}}
323+
sed -i 's/\x27cache_system\x27 => \x27Constants::CACHE_SYSTEM_PHPFS\x27/\x27cache_system\x27 => \x27Constants::CACHE_SYSTEM_REDIS\x27/g' scripts/auto-prepend/settings.php
324+
cat scripts/auto-prepend/settings.php
325+
cd ${{ github.workspace }}/${{env.EXTENSION_PATH}}/tests/end-to-end/
326+
./__scripts__/run-tests.sh ci "./__tests__/3-stream-mode.js"
327+
PENDING_TESTS=$(grep -oP '"numPendingTests":\K(.*),"numRuntimeErrorTestSuites"' .test-results.json | sed 's/,"numRuntimeErrorTestSuites"//g')
328+
if [[ $PENDING_TESTS == "0" ]]
329+
then
330+
echo "No pending tests: OK"
331+
else
332+
echo "There are pending tests: $PENDING_TESTS (KO)"
333+
exit 1
334+
fi
335+
336+
337+
- name: Run "stream mode with TLS auth and cURL and Memcached" test
338+
run: |
339+
cd ${{ github.workspace }}/${{env.EXTENSION_PATH}}
340+
sed -i 's/\x27cache_system\x27 => \x27Constants::CACHE_SYSTEM_REDIS\x27/\x27cache_system\x27 => \x27Constants::CACHE_SYSTEM_MEMCACHED\x27/g' scripts/auto-prepend/settings.php
341+
cat scripts/auto-prepend/settings.php
342+
cd ${{ github.workspace }}/${{env.EXTENSION_PATH}}/tests/end-to-end/
343+
./__scripts__/run-tests.sh ci "./__tests__/3-stream-mode.js"
344+
PENDING_TESTS=$(grep -oP '"numPendingTests":\K(.*),"numRuntimeErrorTestSuites"' .test-results.json | sed 's/,"numRuntimeErrorTestSuites"//g')
345+
if [[ $PENDING_TESTS == "0" ]]
346+
then
347+
echo "No pending tests: OK"
348+
else
349+
echo "There are pending tests: $PENDING_TESTS (KO)"
350+
exit 1
351+
fi
352+

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ 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.35.0](https://github.com/crowdsecurity/php-cs-bouncer/releases/tag/v0.35.0) - 2022-12-16
9+
[_Compare with previous release_](https://github.com/crowdsecurity/php-cs-bouncer/compare/v0.34.0...v0.35.0)
10+
11+
### Changed
12+
- Set default timeout to 120 and allow negative value for unlimited timeout
13+
14+
---
15+
16+
817
## [0.34.0](https://github.com/crowdsecurity/php-cs-bouncer/releases/tag/v0.34.0) - 2022-11-24
918
[_Compare with previous release_](https://github.com/crowdsecurity/php-cs-bouncer/compare/v0.33.0...v0.34.0)
1019

docs/DEVELOPER.md

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,8 @@ For a quick start, follow the below steps.
7272

7373
#### DDEV installation
7474

75-
This project is fully compatible with DDEV 1.21.2, and it is recommended to use this specific version.
76-
For the DDEV installation, please follow the [official instructions](https://ddev.readthedocs.io/en/stable/#installation).
77-
On a Linux distribution, you can run:
78-
```
79-
sudo apt-get -qq update
80-
sudo apt-get -qq -y install libnss3-tools
81-
curl -LO https://raw.githubusercontent.com/drud/ddev/master/scripts/install_ddev.sh
82-
bash install_ddev.sh v1.21.2
83-
rm install_ddev.sh
84-
```
75+
This project is fully compatible with DDEV 1.21.4, and it is recommended to use this specific version.
76+
For the DDEV installation, please follow the [official instructions](https://ddev.readthedocs.io/en/stable/users/install/ddev-installation/).
8577

8678

8779
#### Prepare DDEV PHP environment

docs/USER_GUIDE.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,12 @@ Please note that first and foremost a CrowdSec agent must be installed on a serv
4646
- Handle IP, IP ranges and Country scoped decisions
4747
- Clear, prune and refresh the Local API cache
4848
- `Live mode` or `Stream mode`
49-
- Large PHP matrix compatibility: 7.2.x, 7.3.x, 7.4.x, 8.0.x and 8.1.x
49+
- Large PHP matrix compatibility: 7.2, 7.3, 7.4, 8.0, 8.1 and 8.2
5050
- Built-in support for the most known cache systems like Redis, Memcached, PhpFiles
5151
- Events logged using monolog
5252
- Cap remediation level (ex: for sensitives websites: ban will be capped to captcha)
5353

5454

55-
5655
## Usage
5756

5857
When a user is suspected by CrowdSec to be malevolent, a bouncer will either send him/her a captcha to resolve or
@@ -150,7 +149,8 @@ Here is the list of available settings:
150149
- `api_url`: Define the URL to your Local API server, default to `http://localhost:8080`.
151150

152151

153-
- `api_timeout`: In seconds. The timeout when calling Local API. Must be greater or equal than 1. Default to 1 sec.
152+
- `api_timeout`: In seconds. The timeout when calling Local API. Default to 120 sec. If set to a negative value,
153+
timeout will be unlimited.
154154

155155

156156
- `use_curl`: By default, this lib call the REST Local API using `file_get_contents` method (`allow_url_fopen` is required).

src/AbstractBounce.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ protected function prepareBouncerConfigs(): array
404404
'api_key' => $this->getStringSettings('api_key'),
405405
'api_url' => $this->getStringSettings('api_url'),
406406
'api_user_agent' => $this->getStringSettings('api_user_agent'),
407-
'api_timeout' => $apiTimeout > 0 ? $apiTimeout : Constants::API_TIMEOUT,
407+
'api_timeout' => $apiTimeout !== 0 ? $apiTimeout : Constants::API_TIMEOUT,
408408
'use_curl' => $this->getBoolSettings('use_curl'),
409409
// Debug
410410
'debug_mode' => $this->getBoolSettings('debug_mode'),

src/AbstractCache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,9 +396,9 @@ protected function hit(string $ip): string
396396
$remediations = $this->adapter->getItem(base64_encode($ip))->get();
397397

398398
// We apply array values first because keys are ids.
399+
/** @var array $firstRemediation */
399400
$firstRemediation = array_values($remediations)[0];
400401

401-
/** @var string */
402402
return $firstRemediation[0];
403403
}
404404

src/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ private function addConnectionNodes($rootNode)
152152
->info('Absolute path to the CA used to process TLS handshake')->defaultValue('')
153153
->end()
154154
->booleanNode('tls_verify_peer')->defaultValue(false)->end()
155-
->integerNode('api_timeout')->min(Constants::API_TIMEOUT)->defaultValue(Constants::API_TIMEOUT)->end()
155+
->integerNode('api_timeout')->defaultValue(Constants::API_TIMEOUT)->end()
156156
->booleanNode('use_curl')->defaultValue(false)->end()
157157
->end();
158158
}

src/Constants.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
class Constants
1818
{
1919
/** @var int The timeout when calling LAPI */
20-
public const API_TIMEOUT = 1;
20+
public const API_TIMEOUT = 120;
2121
/** @var string The API-KEY auth type */
2222
public const AUTH_KEY = 'api_key';
2323
/** @var string The TLS auth type */
@@ -75,7 +75,7 @@ class Constants
7575
/** @var string Path for html templates folder (e.g. ban and captcha wall) */
7676
public const TEMPLATES_DIR = __DIR__ . "/templates";
7777
/** @var string The last version of this library */
78-
public const VERSION = 'v0.34.0';
78+
public const VERSION = 'v0.35.0';
7979
/** @var string The "disabled" x-forwarded-for setting */
8080
public const X_FORWARDED_DISABLED = 'no_forward';
8181
}

src/RestClient/AbstractClient.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ abstract public function request(
6868
array $queryParams = null,
6969
array $bodyParams = null,
7070
string $method = 'GET',
71-
array $headers = null,
72-
int $timeout = null
71+
array $headers = null
7372
): ?array;
7473
}

src/RestClient/Curl.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ public function request(
2121
array $queryParams = null,
2222
array $bodyParams = null,
2323
string $method = 'GET',
24-
array $headers = null,
25-
int $timeout = null
24+
array $headers = null
2625
): ?array {
2726
$handle = curl_init();
2827
$curlOptions = $this->createOptions($endpoint, $queryParams, $bodyParams, $method, $headers ?: $this->headers);

src/RestClient/FileGetContents.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,13 @@ public function request(
2929
array $queryParams = null,
3030
array $bodyParams = null,
3131
string $method = 'GET',
32-
array $headers = null,
33-
int $timeout = null
32+
array $headers = null
3433
): ?array {
3534
if ($queryParams) {
3635
$endpoint .= '?' . http_build_query($queryParams);
3736
}
3837

39-
$config = $this->createConfig($bodyParams, $method, $headers, $timeout);
38+
$config = $this->createConfig($bodyParams, $method, $headers);
4039
$context = stream_context_create($config);
4140

4241
$this->logger->debug('', [
@@ -81,15 +80,14 @@ private function convertHeadersToString(array $headers): string
8180
private function createConfig(
8281
array $bodyParams = null,
8382
string $method = 'GET',
84-
array $headers = null,
85-
int $timeout = null
83+
array $headers = null
8684
): array {
8785
$header = $headers ? $this->convertHeadersToString($headers) : $this->headerString;
8886
$config = [
8987
'http' => [
9088
'method' => $method,
9189
'header' => $header,
92-
'timeout' => $timeout ?: $this->timeout,
90+
'timeout' => $this->timeout,
9391
'ignore_errors' => true,
9492
],
9593
];

tests/Integration/WatcherClient.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@ private function request(
115115
array $queryParams = null,
116116
array $bodyParams = null,
117117
string $method = 'GET',
118-
array $headers = null,
119-
int $timeout = null
118+
array $headers = null
120119
): ?array {
121120
$this->ensureLogin();
122121

@@ -125,8 +124,7 @@ private function request(
125124
$queryParams,
126125
$bodyParams,
127126
$method,
128-
$headers ?: $this->baseHeaders,
129-
$timeout
127+
$headers ?: $this->baseHeaders
130128
);
131129
}
132130

tests/end-to-end/settings/base.php.dist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ $crowdSecStandaloneBouncerConfig = [
3030
'trust_ip_forward_array' => ['REPLACE_PROXY_IP'],
3131
// Cache
3232
'cache_system' => Constants::CACHE_SYSTEM_PHPFS,
33-
'redis_dsn' => '',
34-
'memcached_dsn' => '',
33+
'redis_dsn' => 'redis://redis:6379',
34+
'memcached_dsn' => 'memcached://memcached:11211',
3535
'clean_ip_cache_duration' => 1,
3636
'bad_ip_cache_duration' => 1,
3737
'captcha_cache_duration'=> 86400,

0 commit comments

Comments
 (0)