From 7e61deb959866a12a2c39e81a5b8483105fd9115 Mon Sep 17 00:00:00 2001 From: Lucas Cherifi Date: Sun, 20 Dec 2020 16:42:34 +0100 Subject: [PATCH 1/3] remove todos --- docs/api/RestClient.md | 4 +--- docs/contribute.md | 1 + src/ApiCache.php | 2 -- src/RestClient.php | 2 -- tests/LoadPaginatedDecisionsTest.php | 5 ----- tests/LoadPaginatedLogsTest.php | 4 ---- tests/Template403Test.php | 21 --------------------- tests/TestHelpers.php | 4 ---- 8 files changed, 2 insertions(+), 41 deletions(-) delete mode 100644 tests/Template403Test.php diff --git a/docs/api/RestClient.md b/docs/api/RestClient.md index 4eab4ae..d8d8e56 100644 --- a/docs/api/RestClient.md +++ b/docs/api/RestClient.md @@ -90,9 +90,7 @@ Send an HTTP request using the file_get_contents and parse its JSON result if an `\BouncerException` -> when the reponse status is not 2xx. - -TODO P3 test the request method +> when the reponse status is not 2xx.
diff --git a/docs/contribute.md b/docs/contribute.md index 1bdad31..017d047 100644 --- a/docs/contribute.md +++ b/docs/contribute.md @@ -86,6 +86,7 @@ After the merge, don't forget to delete to branch. #### New release ```bash +git checkout main && git pull git describe --tags # to verify what is the current tag gh release create --draft vx.x.x --title vx.x.x ``` diff --git a/src/ApiCache.php b/src/ApiCache.php index b23c6ca..2e77f9e 100644 --- a/src/ApiCache.php +++ b/src/ApiCache.php @@ -281,9 +281,7 @@ private function saveRemediationsForIp(array $decisions, string $ip): string foreach ($decisions as $decision) { if (!\in_array($decision['type'], Constants::ORDERED_REMEDIATIONS)) { $highestRemediationLevel = Constants::ORDERED_REMEDIATIONS[0]; - // TODO P1 test the case of unknown remediation type $this->logger->warning('', ['type' => 'UNKNOWN_REMEDIATION', 'remediation' => $decision['type']]); - // TODO P2 use the fallback parameter instead. $decision['type'] = $highestRemediationLevel; } $remediation = $this->formatRemediationFromDecision($decision); diff --git a/src/RestClient.php b/src/RestClient.php index e12fcb3..f35b93d 100644 --- a/src/RestClient.php +++ b/src/RestClient.php @@ -68,8 +68,6 @@ private function convertHeadersToString(array $headers): string * Send an HTTP request using the file_get_contents and parse its JSON result if any. * * @throws BouncerException when the reponse status is not 2xx. - * - * TODO P3 test the request method */ public function request( string $endpoint, diff --git a/tests/LoadPaginatedDecisionsTest.php b/tests/LoadPaginatedDecisionsTest.php index bc7e252..925e754 100644 --- a/tests/LoadPaginatedDecisionsTest.php +++ b/tests/LoadPaginatedDecisionsTest.php @@ -3,11 +3,6 @@ declare(strict_types=1); use PHPUnit\Framework\TestCase; -/* -TODO P3 Implement decisions pagination tests -cf https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice -*/ - final class LoadPaginatedDecisionsTest extends TestCase { /** diff --git a/tests/LoadPaginatedLogsTest.php b/tests/LoadPaginatedLogsTest.php index 6e63b61..66054a8 100644 --- a/tests/LoadPaginatedLogsTest.php +++ b/tests/LoadPaginatedLogsTest.php @@ -3,10 +3,6 @@ declare(strict_types=1); use PHPUnit\Framework\TestCase; -/* -TODO P3 Implement decisions pagination tests -*/ - final class LoadPaginatedLogsTest extends TestCase { /** diff --git a/tests/Template403Test.php b/tests/Template403Test.php deleted file mode 100644 index 668e9c9..0000000 --- a/tests/Template403Test.php +++ /dev/null @@ -1,21 +0,0 @@ -configure($config); - //$this->assertIsString($bouncer->getAccessForbiddenHtmlTemplate()); - } -} diff --git a/tests/TestHelpers.php b/tests/TestHelpers.php index c83bf34..1cd82ee 100644 --- a/tests/TestHelpers.php +++ b/tests/TestHelpers.php @@ -38,10 +38,6 @@ public static function createLogger(): Logger public static function cacheAdapterProvider(): array { // Init all adapters - /* - TODO P3 Failed on CI but some fixes may fix this bug. Just retry it could work! Else investigates. - $fileSystemAdapter = new FilesystemAdapter('fs_adapter_cache', 0, self::FS_CACHE_ADAPTER_DIR); - */ $phpFilesAdapter = new PhpFilesAdapter('php_array_adapter_backup_cache', 0, self::PHP_FILES_CACHE_ADAPTER_DIR); From 2e5e2ae23477997b82df521238de6fe342289876 Mon Sep 17 00:00:00 2001 From: Lucas Cherifi Date: Mon, 21 Dec 2020 11:48:02 +0100 Subject: [PATCH 2/3] fix fallback remediation --- src/ApiCache.php | 6 +++--- src/Bouncer.php | 14 -------------- 2 files changed, 3 insertions(+), 17 deletions(-) diff --git a/src/ApiCache.php b/src/ApiCache.php index 2e77f9e..679db49 100644 --- a/src/ApiCache.php +++ b/src/ApiCache.php @@ -280,9 +280,9 @@ private function saveRemediationsForIp(array $decisions, string $ip): string if (\count($decisions)) { foreach ($decisions as $decision) { if (!\in_array($decision['type'], Constants::ORDERED_REMEDIATIONS)) { - $highestRemediationLevel = Constants::ORDERED_REMEDIATIONS[0]; - $this->logger->warning('', ['type' => 'UNKNOWN_REMEDIATION', 'remediation' => $decision['type']]); - $decision['type'] = $highestRemediationLevel; + $fallback = $this->config['fallback_remediation']; + $this->logger->warning('', ['type' => 'UNKNOWN_REMEDIATION', 'unknown' => $decision['type'], 'fallback' => $fallback]); + $decision['type'] = $fallback; } $remediation = $this->formatRemediationFromDecision($decision); $remediationResult = $this->addRemediationToCacheItem($ip, $remediation[0], $remediation[1], $remediation[2]); diff --git a/src/Bouncer.php b/src/Bouncer.php index 9c2f397..9dac7d7 100644 --- a/src/Bouncer.php +++ b/src/Bouncer.php @@ -89,19 +89,6 @@ private function capRemediationLevel(string $remediation): string return $remediation; } - /** - * If the CrowdSec remediation is not handled by this library, - * replace it with the value of the configuration "fallback_remediation". - */ - private function handleUnknownRemediation(string $remediation): string - { - if (!\in_array($remediation, Constants::ORDERED_REMEDIATIONS)) { - return $this->config['fallback_remediation']; - } - - return $remediation; - } - /** * Get the remediation for the specified IP. This method use the cache layer. * In live mode, when no remediation was found in cache, @@ -116,7 +103,6 @@ public function getRemediationForIp(string $ip): string throw new BouncerException("IP $ip should looks like x.x.x.x, with x in 0-255. Ex: 1.2.3.4"); } $remediation = $this->apiCache->get(long2ip($intIp)); - $remediation = $this->handleUnknownRemediation($remediation); return $this->capRemediationLevel($remediation); } From cc639d2f5a0e9776499ba4186ee9c5156e428083 Mon Sep 17 00:00:00 2001 From: Lucas Cherifi Date: Mon, 21 Dec 2020 15:09:14 +0100 Subject: [PATCH 3/3] add pre-commit to update version in Constants.php --- .githooks/pre-commit | 27 +++++++++++++++++++++++++++ composer.json | 10 +++++++++- docs/contribute.md | 2 +- post-install.sh | 2 ++ src/Constants.php | 5 ++++- 5 files changed, 43 insertions(+), 3 deletions(-) create mode 100755 .githooks/pre-commit create mode 100755 post-install.sh diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 0000000..f5b5dd5 --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,27 @@ +#!/bin/bash + +platform='unknown' +unamestr=`uname` +if [[ "$unamestr" == 'Linux' ]]; then + platform='linux' +elif [[ "$unamestr" == 'FreeBSD' ]]; then + platform='freebsd' +elif [[ "$unamestr" == 'Darwin' ]]; then + platform='osx' +fi + + +function get_git_tag { + git describe --tags `git rev-list --tags --max-count=1` +} + +git_tag=$(get_git_tag) + +if [[ $platform == 'linux' ]]; then + sed -i -E "s/v[0-9]+\.[0-9]+\.[0-9]/$git_tag/" `git rev-parse --show-toplevel`/src/Constants.php +else + sed -i "" -E "s/v[0-9]+\.[0-9]+\.[0-9]/$git_tag/" `git rev-parse --show-toplevel`/src/Constants.php +fi +git add `git rev-parse --show-toplevel`/src/Constants.php + +echo 'Note: version number updated in src/Constants.php' \ No newline at end of file diff --git a/composer.json b/composer.json index 75efa09..f9f6886 100644 --- a/composer.json +++ b/composer.json @@ -40,5 +40,13 @@ "symfony/var-dumper": "^5.2", "phpunit/phpunit": "8.5.13", "clean/phpdoc-md": "^0.19.1" + }, + "scripts": { + "post-install-cmd": [ + "./post-install.sh" + ], + "post-update-cmd": [ + "./post-install.sh" + ] } -} +} \ No newline at end of file diff --git a/docs/contribute.md b/docs/contribute.md index 017d047..1484974 100644 --- a/docs/contribute.md +++ b/docs/contribute.md @@ -87,6 +87,6 @@ After the merge, don't forget to delete to branch. ```bash git checkout main && git pull -git describe --tags # to verify what is the current tag +git describe --tags `git rev-list --tags --max-count=1` # to verify what is the current tag gh release create --draft vx.x.x --title vx.x.x ``` diff --git a/post-install.sh b/post-install.sh new file mode 100755 index 0000000..d11d8ba --- /dev/null +++ b/post-install.sh @@ -0,0 +1,2 @@ +git config core.hooksPath `git rev-parse --show-toplevel`/.githooks +echo "Git pre-commit hook is installed. This hook fixes the src/Constants.php version for each commits." \ No newline at end of file diff --git a/src/Constants.php b/src/Constants.php index a4ac875..feec3c3 100644 --- a/src/Constants.php +++ b/src/Constants.php @@ -17,8 +17,11 @@ class Constants /** @var string The URL of the CrowdSec Central API */ public const CAPI_URL = 'https://api.crowdsec.net/v2/'; + /** @var string The last version of this library */ + public const VERSION = 'v0.6.0'; + /** @var string The user agent used to send request to LAPI or CAPI */ - public const BASE_USER_AGENT = 'PHP CrowdSec Bouncer/1.0.0'; + public const BASE_USER_AGENT = 'PHP CrowdSec Bouncer/'.self::VERSION; /** @var int The timeout when calling LAPI or CAPI */ public const API_TIMEOUT = 1;