Skip to content

Commit aab56fb

Browse files
Merge pull request #101 from julienloizelet/feat/code-standard-and-api-cache-fix
Feat/code standard and api cache fix
2 parents e8aea7e + 33c1f4c commit aab56fb

File tree

6 files changed

+44
-16
lines changed

6 files changed

+44
-16
lines changed

CHANGELOG.md

+10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ 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+
9+
## [0.27.0] - 2022-07-29
10+
11+
### Changed
12+
- *Breaking change*: Modify `getBouncerInstance` and `init` signatures
13+
14+
### Fixed
15+
- Fix wrongly formatted range scoped decision retrieving
16+
- Fix cache updated decisions count
17+
818
## [0.26.0] - 2022-07-28
919

1020
### Changed

src/ApiCache.php

+28-5
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ private function getCacheKey(string $scope, string $value): string
468468
private function saveRemediations(array $decisions): array
469469
{
470470
$errors = [];
471+
$count = 0;
471472
foreach ($decisions as $decision) {
472473
$remediation = $this->formatRemediationFromDecision($decision);
473474
$type = $remediation[0];
@@ -492,9 +493,16 @@ private function saveRemediations(array $decisions): array
492493
}
493494
$cacheKey = $this->getCacheKey($decision['scope'], $address->toString());
494495
$this->addRemediationToCacheItem($cacheKey, $type, $exp, $id);
496+
++$count;
495497
} elseif (Constants::SCOPE_RANGE === $decision['scope']) {
496498
$range = Subnet::parseString($decision['value']);
497-
499+
if(null === $range) {
500+
$this->logger->warning('', [
501+
'type' => 'INVALID_RANGE_TO_ADD_FROM_REMEDIATION',
502+
'decision' => $decision,
503+
]);
504+
continue;
505+
}
498506
$addressType = $range->getAddressType();
499507
$isIpv6 = (Type::T_IPv6 === $addressType);
500508
if ($isIpv6 || ($range->getNetworkPrefix() < 24)) {
@@ -527,13 +535,15 @@ private function saveRemediations(array $decisions): array
527535
throw new BouncerException($message);
528536
}
529537
} while (0 !== strcmp($address->getComparableString(), $comparableEndAddress));
538+
++$count;
530539
} elseif (Constants::SCOPE_COUNTRY === $decision['scope']) {
531540
$cacheKey = $this->getCacheKey($decision['scope'], $decision['value']);
532541
$this->addRemediationToCacheItem($cacheKey, $type, $exp, $id);
542+
++$count;
533543
}
534544
}
535545

536-
return ['success' => $this->commit(), 'errors' => $errors];
546+
return ['success' => $this->commit(), 'errors' => $errors, 'count' => $count];
537547
}
538548

539549
/**
@@ -570,7 +580,13 @@ private function removeRemediations(array $decisions): array
570580
}
571581
} elseif (Constants::SCOPE_RANGE === $decision['scope']) {
572582
$range = Subnet::parseString($decision['value']);
573-
583+
if(null === $range) {
584+
$this->logger->warning('', [
585+
'type' => 'INVALID_RANGE_TO_REMOVE_FROM_REMEDIATION',
586+
'decision' => $decision,
587+
]);
588+
continue;
589+
}
574590
$addressType = $range->getAddressType();
575591
$isIpv6 = (Type::T_IPv6 === $addressType);
576592
if ($isIpv6 || ($range->getNetworkPrefix() < 24)) {
@@ -751,8 +767,15 @@ public function pullUpdates(): array
751767
$nbNew = 0;
752768
if ($newDecisions) {
753769
$saveResult = $this->saveRemediations($newDecisions);
754-
$addErrors = $saveResult['errors'];
755-
$nbNew = \count($newDecisions);
770+
if(!empty($saveResult['success'])){
771+
$addErrors = $saveResult['errors']??0;
772+
$nbNew = $saveResult['count']??0;
773+
}
774+
else{
775+
$this->logger->warning('', [
776+
'type' => 'CACHE_UPDATED_FAILED',
777+
'message' => 'Something went wrong while committing to cache adapter']);
778+
}
756779
}
757780

758781
$this->logger->debug('', ['type' => 'CACHE_UPDATED', 'deleted' => $nbDeleted, 'new' => $nbNew]);

src/Constants.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Constants
2020
public const DEFAULT_LAPI_URL = 'http://localhost:8080';
2121

2222
/** @var string The last version of this library */
23-
public const VERSION = 'v0.26.0';
23+
public const VERSION = 'v0.27.0';
2424

2525
/** @var string The user agent used to send request to LAPI */
2626
public const BASE_USER_AGENT = 'PHP CrowdSec Bouncer/' . self::VERSION;

src/IBounce.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ interface IBounce
1717
/**
1818
* Init the bouncer.
1919
*/
20-
public function init(array $configs, array $forcedConfigs = []): Bouncer;
20+
public function init(array $configs): Bouncer;
2121

2222
/**
2323
* Init the logger.
@@ -27,7 +27,7 @@ public function initLogger(array $configs): void;
2727
/**
2828
* Get the bouncer instance.
2929
*/
30-
public function getBouncerInstance(array $settings, bool $forceReload = false): Bouncer;
30+
public function getBouncerInstance(array $settings): Bouncer;
3131

3232
/**
3333
* If there is any technical problem while bouncing, don't block the user. Bypass bouncing and log the error.

src/RestClient/Curl.php

-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public function request(
5252

5353
/**
5454
* Retrieve Curl options.
55-
*
5655
*/
5756
private function createOptions(
5857
string $endpoint,

src/StandaloneBounce.php

+3-7
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class StandaloneBounce extends AbstractBounce
2626
* @throws ErrorException
2727
* @throws \Psr\Cache\InvalidArgumentException|BouncerException
2828
*/
29-
public function init(array $configs, array $forcedConfigs = []): Bouncer
29+
public function init(array $configs): Bouncer
3030
{
3131
// Convert array of string to array of array with comparable IPs
3232
if (\is_array(($configs['trust_ip_forward_array']))) {
@@ -45,7 +45,7 @@ public function init(array $configs, array $forcedConfigs = []): Bouncer
4545
}
4646
$configs['trust_ip_forward_array'] = $finalForwardConfigs;
4747
}
48-
$this->settings = array_merge($configs, $forcedConfigs);
48+
$this->settings = $configs;
4949

5050
return $this->getBouncerInstance($this->settings);
5151
}
@@ -57,12 +57,8 @@ public function init(array $configs, array $forcedConfigs = []): Bouncer
5757
* @throws ErrorException
5858
* @throws \Psr\Cache\InvalidArgumentException|BouncerException
5959
*/
60-
public function getBouncerInstance(array $settings, bool $forceReload = false): Bouncer
60+
public function getBouncerInstance(array $settings): Bouncer
6161
{
62-
// Singleton for this function (if no reload forcing)
63-
if ($this->bouncer && !$forceReload) {
64-
return $this->bouncer;
65-
}
6662
$this->settings = array_merge($this->settings, $settings);
6763
$apiUserAgent = 'Standalone CrowdSec PHP Bouncer/' . Constants::VERSION;
6864

0 commit comments

Comments
 (0)