Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## [0.4.0](https://github.com/crowdsecurity/php-remediation-engine/releases/tag/v0.4.0) - 2022-12-30
[_Compare with previous release_](https://github.com/crowdsecurity/php-remediation-engine/compare/v0.3.1...v0.4.0)

### Changed

- Modify some log format and severity level

### Added

- Add `symfony/cache` conflicts for Redis not working versions
- Add some relevant logs

---

## [0.3.1](https://github.com/crowdsecurity/php-remediation-engine/releases/tag/v0.3.1) - 2022-12-29
[_Compare with previous release_](https://github.com/crowdsecurity/php-remediation-engine/compare/v0.3.0...v0.3.1)

Expand Down
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
"php": "^7.2.5 || ^8.0",
"ext-json": "*",
"symfony/config": "^4.4.27 || ^5.2 || ^6.0",
"symfony/cache": "^5.4.11,!=5.4.17 || ^6.0.11,!=6.0.16,!=6.0.17,!=6.1.9,!=6.2.3",
"crowdsec/capi-client": "0.7.0",
"symfony/cache": "^5.4.11|| ^6.0.11",
"crowdsec/capi-client": "0.8.0",
"crowdsec/lapi-client": "0.2.0",
"monolog/monolog": "^1.17 || ^2.1",
"mlocati/ip-lib": "^1.18",
Expand All @@ -56,6 +56,9 @@
"mikey179/vfsstream": "^1.6.11",
"ext-curl": "*"
},
"conflict": {
"symfony/cache": "6.2.3 || 6.1.9 || 6.0.17 || 5.4.17"
},
"suggest": {
"ext-curl": "*"
}
Expand Down
10 changes: 7 additions & 3 deletions src/AbstractRemediation.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ protected function getRemediationFromDecisions(array $decisions): string
$cleanDecisions = $this->cacheStorage->cleanCachedValues($decisions);

$sortedDecisions = $this->sortDecisionsByRemediationPriority($cleanDecisions);
$this->logger->debug('', [
'type' => 'REM_SORTED_DECISIONS',
'decisions' => $sortedDecisions,
]);

// Return only a remediation with the highest priority
return $sortedDecisions[0][AbstractCache::INDEX_MAIN] ?? Constants::REMEDIATION_BYPASS;
Expand Down Expand Up @@ -311,7 +315,7 @@ private function parseDurationToSeconds(string $duration): int
preg_match($re, $duration, $matches);
if (empty($matches[0])) {
$this->logger->error('', [
'type' => 'DECISION_DURATION_PARSE_ERROR',
'type' => 'REM_DECISION_DURATION_PARSE_ERROR',
'duration' => $duration,
]);

Expand Down Expand Up @@ -353,8 +357,8 @@ private function validateRawDecision(array $rawDecision): bool
return true;
}

$this->logger->warning('', [
'type' => 'RAW_DECISION_NOT_AS_EXPECTED',
$this->logger->error('', [
'type' => 'REM_RAW_DECISION_NOT_AS_EXPECTED',
'raw_decision' => json_encode($rawDecision),
]);

Expand Down
18 changes: 9 additions & 9 deletions src/CacheStorage/AbstractCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public function removeDecision(Decision $decision): array
break;
default:
$this->logger->warning('', [
'type' => 'CACHE_REMOVE_NON_IMPLEMENTED_SCOPE',
'type' => 'REM_CACHE_REMOVE_NON_IMPLEMENTED_SCOPE',
'decision' => $decision->toArray(),
]);
break;
Expand Down Expand Up @@ -252,7 +252,7 @@ public function retrieveDecisionsForIp(string $scope, string $ip): array
break;
default:
$this->logger->warning('', [
'type' => 'CACHE_RETRIEVE_FOR_IP_NON_IMPLEMENTED_SCOPE',
'type' => 'REM_CACHE_RETRIEVE_FOR_IP_NON_IMPLEMENTED_SCOPE',
'scope' => $scope,
]);
break;
Expand Down Expand Up @@ -284,7 +284,7 @@ public function storeDecision(Decision $decision): array
break;
default:
$this->logger->warning('', [
'type' => 'CACHE_STORE_NON_IMPLEMENTED_SCOPE',
'type' => 'REM_CACHE_STORE_NON_IMPLEMENTED_SCOPE',
'decision' => $decision->toArray(),
]);
}
Expand Down Expand Up @@ -468,7 +468,7 @@ private function manageRange(Decision $decision): ?RangeInterface
$range = Subnet::parseString($rangeString);
if (null === $range) {
$this->logger->error('', [
'type' => 'INVALID_RANGE',
'type' => 'REM_CACHE_INVALID_RANGE',
'decision' => $decision->toArray(),
]);

Expand All @@ -477,7 +477,7 @@ private function manageRange(Decision $decision): ?RangeInterface
$addressType = $range->getAddressType();
if (Type::T_IPv6 === $addressType) {
$this->logger->warning('', [
'type' => 'IPV6_RANGE_NOT_IMPLEMENTED',
'type' => 'REM_CACHE_IPV6_RANGE_NOT_IMPLEMENTED',
'decision' => $decision->toArray(),
]);

Expand Down Expand Up @@ -523,8 +523,8 @@ private function remove(Decision $decision, ?int $bucketInt = null): array
$result[self::DEFER] = 1;
$result[self::REMOVED] = $removed;
if (!$this->saveDeferred($item)) {
$this->logger->warning('', [
'type' => 'CACHE_STORE_DEFERRED_FAILED_FOR_REMOVE_DECISION',
$this->logger->error('', [
'type' => 'REM_CACHE_STORE_DEFERRED_FAILED_FOR_REMOVE_DECISION',
'decision' => $decision->toArray(),
'bucket_int' => $bucketInt,
]);
Expand Down Expand Up @@ -565,8 +565,8 @@ private function store(Decision $decision, ?int $bucketInt = null): array

$result = [self::DONE => 0, self::DEFER => 1, self::STORED => $currentValue];
if (!$this->saveDeferred($item)) {
$this->logger->warning('', [
'type' => 'CACHE_STORE_DEFERRED_FAILED',
$this->logger->error('', [
'type' => 'REM_CACHE_STORE_DEFERRED_FAILED',
'decision' => $decision->toArray(),
'bucket_int' => $bucketInt,
]);
Expand Down
7 changes: 6 additions & 1 deletion src/CapiRemediation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CrowdSec\RemediationEngine;

use CrowdSec\CapiClient\ClientException;
use CrowdSec\CapiClient\Watcher;
use CrowdSec\RemediationEngine\CacheStorage\AbstractCache;
use CrowdSec\RemediationEngine\CacheStorage\CacheStorageException;
Expand Down Expand Up @@ -47,6 +48,10 @@ public function getIpRemediation(string $ip): string
$cachedDecisions = $this->getAllCachedDecisions($ip, $this->getCountryForIp($ip));

if (!$cachedDecisions) {
$this->logger->debug('', [
'type' => 'CAPI_REM_NO_CACHED_DECISIONS',
'ip' => $ip,
]);
// As CAPI is always in stream_mode, we do not store this bypass
return Constants::REMEDIATION_BYPASS;
}
Expand All @@ -59,7 +64,7 @@ public function getIpRemediation(string $ip): string
*
* @throws CacheStorageException
* @throws InvalidArgumentException
* @throws CacheException
* @throws CacheException|ClientException
*/
public function refreshDecisions(): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ class Constants
/** @var string The CrowdSec Range scope for decisions */
public const SCOPE_RANGE = 'range';
/** @var string The current version of this library */
public const VERSION = 'v0.3.1';
public const VERSION = 'v0.4.0';
}
9 changes: 7 additions & 2 deletions src/LapiRemediation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace CrowdSec\RemediationEngine;

use CrowdSec\LapiClient\Bouncer;
use CrowdSec\LapiClient\ClientException;
use CrowdSec\RemediationEngine\CacheStorage\AbstractCache;
use CrowdSec\RemediationEngine\CacheStorage\CacheStorageException;
use CrowdSec\RemediationEngine\Configuration\Lapi as LapiRemediationConfig;
Expand Down Expand Up @@ -42,14 +43,18 @@ public function __construct(
* @throws CacheStorageException
* @throws InvalidArgumentException
* @throws RemediationException
* @throws CacheException
* @throws CacheException|ClientException
*/
public function getIpRemediation(string $ip): string
{
$country = $this->getCountryForIp($ip);
$cachedDecisions = $this->getAllCachedDecisions($ip, $country);

if (!$cachedDecisions) {
$this->logger->debug('', [
'type' => 'LAPI_REM_NO_CACHED_DECISIONS',
'ip' => $ip,
]);
// In stream_mode, we do not store this bypass, and we do not call LAPI directly
if ($this->getConfig('stream_mode')) {
return Constants::REMEDIATION_BYPASS;
Expand Down Expand Up @@ -89,7 +94,7 @@ public function getIpRemediation(string $ip): string
*
* @throws CacheException
* @throws CacheStorageException
* @throws InvalidArgumentException
* @throws InvalidArgumentException|ClientException
*
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ public function testPrivateOrProtectedMethods()
);
PHPUnitUtil::assertRegExp(
$this,
'/.*400.*"type":"INVALID_RANGE"/',
'/.*400.*"type":"REM_CACHE_INVALID_RANGE"/',
file_get_contents($this->root->url() . '/' . $this->prodFile),
'Prod log content should be correct'
);
Expand All @@ -354,7 +354,7 @@ public function testPrivateOrProtectedMethods()
);
PHPUnitUtil::assertRegExp(
$this,
'/.*300.*"type":"IPV6_RANGE_NOT_IMPLEMENTED"/',
'/.*300.*"type":"REM_CACHE_IPV6_RANGE_NOT_IMPLEMENTED"/',
file_get_contents($this->root->url() . '/' . $this->prodFile),
'Prod log content should be correct'
);
Expand Down Expand Up @@ -407,7 +407,7 @@ public function testRetrieveUnknownScope()
);
PHPUnitUtil::assertRegExp(
$this,
'/.*300.*"type":"CACHE_RETRIEVE_FOR_IP_NON_IMPLEMENTED_SCOPE"/',
'/.*300.*"type":"REM_CACHE_RETRIEVE_FOR_IP_NON_IMPLEMENTED_SCOPE"/',
file_get_contents($this->root->url() . '/' . $this->prodFile),
'Prod log content should be correct'
);
Expand Down
10 changes: 5 additions & 5 deletions tests/Unit/CapiRemediationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ public function testPrivateOrProtectedMethods()

PHPUnitUtil::assertRegExp(
$this,
'/.*300.*"type":"RAW_DECISION_NOT_AS_EXPECTED"/',
'/.*400.*"type":"REM_RAW_DECISION_NOT_AS_EXPECTED"/',
file_get_contents($this->root->url() . '/' . $this->prodFile),
'Prod log content should be correct'
);
Expand Down Expand Up @@ -867,7 +867,7 @@ public function testRefreshDecisions($cacheType)

PHPUnitUtil::assertRegExp(
$this,
'/.*300.*"type":"CACHE_REMOVE_NON_IMPLEMENTED_SCOPE.*CAPI-ban-do-not-know-delete-1.2.3.4"/',
'/.*300.*"type":"REM_CACHE_REMOVE_NON_IMPLEMENTED_SCOPE.*CAPI-ban-do-not-know-delete-1.2.3.4"/',
file_get_contents($this->root->url() . '/' . $this->prodFile),
'Prod log content should be correct'
);
Expand All @@ -881,7 +881,7 @@ public function testRefreshDecisions($cacheType)

PHPUnitUtil::assertRegExp(
$this,
'/.*300.*"type":"CACHE_STORE_NON_IMPLEMENTED_SCOPE.*CAPI-ban-do-not-know-store-1.2.3.4"/',
'/.*300.*"type":"REM_CACHE_STORE_NON_IMPLEMENTED_SCOPE.*CAPI-ban-do-not-know-store-1.2.3.4"/',
file_get_contents($this->root->url() . '/' . $this->prodFile),
'Prod log content should be correct'
);
Expand All @@ -895,7 +895,7 @@ public function testRefreshDecisions($cacheType)

PHPUnitUtil::assertRegExp(
$this,
'/.*300.*"type":"IPV6_RANGE_NOT_IMPLEMENTED"/',
'/.*300.*"type":"REM_CACHE_IPV6_RANGE_NOT_IMPLEMENTED"/',
file_get_contents($this->root->url() . '/' . $this->prodFile),
'Prod log content should be correct'
);
Expand Down Expand Up @@ -977,7 +977,7 @@ public function testRefreshDecisions($cacheType)
);
PHPUnitUtil::assertRegExp(
$this,
'/.*400.*"type":"DECISION_DURATION_PARSE_ERROR"/',
'/.*400.*"type":"REM_DECISION_DURATION_PARSE_ERROR"/',
file_get_contents($this->root->url() . '/' . $this->prodFile),
'Prod log content should be correct'
);
Expand Down
8 changes: 4 additions & 4 deletions tests/Unit/LapiRemediationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ public function testRefreshDecisions($cacheType)

PHPUnitUtil::assertRegExp(
$this,
'/.*300.*"type":"CACHE_REMOVE_NON_IMPLEMENTED_SCOPE.*CAPI-ban-do-not-know-delete-1.2.3.4"/',
'/.*300.*"type":"REM_CACHE_REMOVE_NON_IMPLEMENTED_SCOPE.*CAPI-ban-do-not-know-delete-1.2.3.4"/',
file_get_contents($this->root->url() . '/' . $this->prodFile),
'Prod log content should be correct'
);
Expand All @@ -884,7 +884,7 @@ public function testRefreshDecisions($cacheType)

PHPUnitUtil::assertRegExp(
$this,
'/.*300.*"type":"CACHE_STORE_NON_IMPLEMENTED_SCOPE.*CAPI-ban-do-not-know-store-1.2.3.4"/',
'/.*300.*"type":"REM_CACHE_STORE_NON_IMPLEMENTED_SCOPE.*CAPI-ban-do-not-know-store-1.2.3.4"/',
file_get_contents($this->root->url() . '/' . $this->prodFile),
'Prod log content should be correct'
);
Expand All @@ -898,7 +898,7 @@ public function testRefreshDecisions($cacheType)

PHPUnitUtil::assertRegExp(
$this,
'/.*300.*"type":"IPV6_RANGE_NOT_IMPLEMENTED"/',
'/.*300.*"type":"REM_CACHE_IPV6_RANGE_NOT_IMPLEMENTED"/',
file_get_contents($this->root->url() . '/' . $this->prodFile),
'Prod log content should be correct'
);
Expand Down Expand Up @@ -995,7 +995,7 @@ public function testRefreshDecisions($cacheType)
);
PHPUnitUtil::assertRegExp(
$this,
'/.*400.*"type":"DECISION_DURATION_PARSE_ERROR"/',
'/.*400.*"type":"REM_DECISION_DURATION_PARSE_ERROR"/',
file_get_contents($this->root->url() . '/' . $this->prodFile),
'Prod log content should be correct'
);
Expand Down