Skip to content

Commit 514ab4d

Browse files
authored
Merge pull request #25 from crowdsecurity/remediation-types
lint pass update doc make crowdsec mentions hidable add phpcs fix fallback remediation add pre-commit to update version in Constants.php
2 parents 0a0f585 + cc639d2 commit 514ab4d

13 files changed

+48
-61
lines changed

.githooks/pre-commit

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
3+
platform='unknown'
4+
unamestr=`uname`
5+
if [[ "$unamestr" == 'Linux' ]]; then
6+
platform='linux'
7+
elif [[ "$unamestr" == 'FreeBSD' ]]; then
8+
platform='freebsd'
9+
elif [[ "$unamestr" == 'Darwin' ]]; then
10+
platform='osx'
11+
fi
12+
13+
14+
function get_git_tag {
15+
git describe --tags `git rev-list --tags --max-count=1`
16+
}
17+
18+
git_tag=$(get_git_tag)
19+
20+
if [[ $platform == 'linux' ]]; then
21+
sed -i -E "s/v[0-9]+\.[0-9]+\.[0-9]/$git_tag/" `git rev-parse --show-toplevel`/src/Constants.php
22+
else
23+
sed -i "" -E "s/v[0-9]+\.[0-9]+\.[0-9]/$git_tag/" `git rev-parse --show-toplevel`/src/Constants.php
24+
fi
25+
git add `git rev-parse --show-toplevel`/src/Constants.php
26+
27+
echo 'Note: version number updated in src/Constants.php'

composer.json

+9-1
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,13 @@
4040
"symfony/var-dumper": "^5.2",
4141
"phpunit/phpunit": "8.5.13",
4242
"clean/phpdoc-md": "^0.19.1"
43+
},
44+
"scripts": {
45+
"post-install-cmd": [
46+
"./post-install.sh"
47+
],
48+
"post-update-cmd": [
49+
"./post-install.sh"
50+
]
4351
}
44-
}
52+
}

docs/api/RestClient.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,7 @@ Send an HTTP request using the file_get_contents and parse its JSON result if an
9090

9191

9292
`\BouncerException`
93-
> when the reponse status is not 2xx.
94-
95-
TODO P3 test the request method
93+
> when the reponse status is not 2xx.
9694
9795
<hr />
9896

docs/contribute.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ After the merge, don't forget to delete to branch.
8686
#### New release
8787

8888
```bash
89-
git describe --tags # to verify what is the current tag
89+
git checkout main && git pull
90+
git describe --tags `git rev-list --tags --max-count=1` # to verify what is the current tag
9091
gh release create --draft vx.x.x --title vx.x.x
9192
```

post-install.sh

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
git config core.hooksPath `git rev-parse --show-toplevel`/.githooks
2+
echo "Git pre-commit hook is installed. This hook fixes the src/Constants.php version for each commits."

src/ApiCache.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -280,11 +280,9 @@ private function saveRemediationsForIp(array $decisions, string $ip): string
280280
if (\count($decisions)) {
281281
foreach ($decisions as $decision) {
282282
if (!\in_array($decision['type'], Constants::ORDERED_REMEDIATIONS)) {
283-
$highestRemediationLevel = Constants::ORDERED_REMEDIATIONS[0];
284-
// TODO P1 test the case of unknown remediation type
285-
$this->logger->warning('', ['type' => 'UNKNOWN_REMEDIATION', 'remediation' => $decision['type']]);
286-
// TODO P2 use the fallback parameter instead.
287-
$decision['type'] = $highestRemediationLevel;
283+
$fallback = $this->config['fallback_remediation'];
284+
$this->logger->warning('', ['type' => 'UNKNOWN_REMEDIATION', 'unknown' => $decision['type'], 'fallback' => $fallback]);
285+
$decision['type'] = $fallback;
288286
}
289287
$remediation = $this->formatRemediationFromDecision($decision);
290288
$remediationResult = $this->addRemediationToCacheItem($ip, $remediation[0], $remediation[1], $remediation[2]);

src/Bouncer.php

-14
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,6 @@ private function capRemediationLevel(string $remediation): string
8989
return $remediation;
9090
}
9191

92-
/**
93-
* If the CrowdSec remediation is not handled by this library,
94-
* replace it with the value of the configuration "fallback_remediation".
95-
*/
96-
private function handleUnknownRemediation(string $remediation): string
97-
{
98-
if (!\in_array($remediation, Constants::ORDERED_REMEDIATIONS)) {
99-
return $this->config['fallback_remediation'];
100-
}
101-
102-
return $remediation;
103-
}
104-
10592
/**
10693
* Get the remediation for the specified IP. This method use the cache layer.
10794
* In live mode, when no remediation was found in cache,
@@ -116,7 +103,6 @@ public function getRemediationForIp(string $ip): string
116103
throw new BouncerException("IP $ip should looks like x.x.x.x, with x in 0-255. Ex: 1.2.3.4");
117104
}
118105
$remediation = $this->apiCache->get(long2ip($intIp));
119-
$remediation = $this->handleUnknownRemediation($remediation);
120106

121107
return $this->capRemediationLevel($remediation);
122108
}

src/Constants.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ class Constants
1717
/** @var string The URL of the CrowdSec Central API */
1818
public const CAPI_URL = 'https://api.crowdsec.net/v2/';
1919

20+
/** @var string The last version of this library */
21+
public const VERSION = 'v0.6.0';
22+
2023
/** @var string The user agent used to send request to LAPI or CAPI */
21-
public const BASE_USER_AGENT = 'PHP CrowdSec Bouncer/1.0.0';
24+
public const BASE_USER_AGENT = 'PHP CrowdSec Bouncer/'.self::VERSION;
2225

2326
/** @var int The timeout when calling LAPI or CAPI */
2427
public const API_TIMEOUT = 1;

src/RestClient.php

-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ private function convertHeadersToString(array $headers): string
6868
* Send an HTTP request using the file_get_contents and parse its JSON result if any.
6969
*
7070
* @throws BouncerException when the reponse status is not 2xx.
71-
*
72-
* TODO P3 test the request method
7371
*/
7472
public function request(
7573
string $endpoint,

tests/LoadPaginatedDecisionsTest.php

-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@
33
declare(strict_types=1);
44
use PHPUnit\Framework\TestCase;
55

6-
/*
7-
TODO P3 Implement decisions pagination tests
8-
cf https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
9-
*/
10-
116
final class LoadPaginatedDecisionsTest extends TestCase
127
{
138
/**

tests/LoadPaginatedLogsTest.php

-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
declare(strict_types=1);
44
use PHPUnit\Framework\TestCase;
55

6-
/*
7-
TODO P3 Implement decisions pagination tests
8-
*/
9-
106
final class LoadPaginatedLogsTest extends TestCase
117
{
128
/**

tests/Template403Test.php

-21
This file was deleted.

tests/TestHelpers.php

-4
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@ public static function createLogger(): Logger
3838
public static function cacheAdapterProvider(): array
3939
{
4040
// Init all adapters
41-
/*
42-
TODO P3 Failed on CI but some fixes may fix this bug. Just retry it could work! Else investigates.
43-
$fileSystemAdapter = new FilesystemAdapter('fs_adapter_cache', 0, self::FS_CACHE_ADAPTER_DIR);
44-
*/
4541

4642
$phpFilesAdapter = new PhpFilesAdapter('php_array_adapter_backup_cache', 0, self::PHP_FILES_CACHE_ADAPTER_DIR);
4743

0 commit comments

Comments
 (0)