Skip to content

Commit cc6fae0

Browse files
authored
Merge pull request #33 from crowdsecurity/fix-fallback
fix fallback remediation
2 parents 3f8fe1e + 3c88fed commit cc6fae0

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

src/ApiCache.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,13 @@ public function configure(
6161
string $userAgent,
6262
string $apiKey,
6363
int $cacheExpirationForCleanIp,
64-
int $cacheExpirationForBadIp
64+
int $cacheExpirationForBadIp,
65+
string $fallbackRemediation
6566
): void {
6667
$this->liveMode = $liveMode;
6768
$this->cacheExpirationForCleanIp = $cacheExpirationForCleanIp;
6869
$this->cacheExpirationForBadIp = $cacheExpirationForBadIp;
70+
$this->fallbackRemediation = $fallbackRemediation;
6971
$cacheConfigItem = $this->adapter->getItem('cacheConfig');
7072
$cacheConfig = $cacheConfigItem->get();
7173
$this->warmedUp = (\is_array($cacheConfig) && isset($cacheConfig['warmed_up'])
@@ -211,8 +213,9 @@ private function formatRemediationFromDecision(?array $decision): array
211213
$duration = time() + $this->cacheExpirationForCleanIp;
212214
if (!$this->liveMode) {
213215
// In stream mode we considere an clean IP forever... until the next resync.
214-
$duration = PHP_INT_MAX;
216+
$duration = \PHP_INT_MAX;
215217
}
218+
216219
return [Constants::REMEDIATION_BYPASS, $duration, 0];
217220
}
218221

@@ -257,8 +260,9 @@ private function saveRemediations(array $decisions): bool
257260
return $this->commit();
258261
}
259262

260-
private function removeRemediations(array $decisions): bool
263+
private function removeRemediations(array $decisions): int
261264
{
265+
$count = 0;
262266
foreach ($decisions as $decision) {
263267
if (\is_int($decision['start_ip']) && \is_int($decision['end_ip'])) {
264268
$ipRange = array_map('long2ip', range($decision['start_ip'], $decision['end_ip']));
@@ -271,15 +275,19 @@ private function removeRemediations(array $decisions): bool
271275
$success = false;
272276
}
273277
}
274-
if (!$success) {
278+
if ($success) {
279+
++$count;
280+
} else {
275281
// The API may return stale deletion events due to API design.
276282
// Ignoring them is therefore not a problem.
277283
$this->logger->debug('', ['type' => 'DECISION_TO_REMOVE_NOT_FOUND_IN_CACHE', 'decision' => $decision['id']]);
278284
}
279285
}
280286
}
281287

282-
return $this->commit();
288+
$this->commit();
289+
290+
return $count;
283291
}
284292

285293
/**
@@ -291,9 +299,8 @@ private function saveRemediationsForIp(array $decisions, string $ip): string
291299
if (\count($decisions)) {
292300
foreach ($decisions as $decision) {
293301
if (!\in_array($decision['type'], Constants::ORDERED_REMEDIATIONS)) {
294-
$fallback = $this->config['fallback_remediation'];
295-
$this->logger->warning('', ['type' => 'UNKNOWN_REMEDIATION', 'unknown' => $decision['type'], 'fallback' => $fallback]);
296-
$decision['type'] = $fallback;
302+
$this->logger->warning('', ['type' => 'UNKNOWN_REMEDIATION', 'unknown' => $decision['type'], 'fallback' => $this->fallbackRemediation]);
303+
$decision['type'] = $this->fallbackRemediation;
297304
}
298305
$remediation = $this->formatRemediationFromDecision($decision);
299306
$remediationResult = $this->addRemediationToCacheItem($ip, $remediation[0], $remediation[1], $remediation[2]);
@@ -380,8 +387,7 @@ public function pullUpdates(): array
380387

381388
$nbDeleted = 0;
382389
if ($deletedDecisions) {
383-
$this->removeRemediations($deletedDecisions);
384-
$nbDeleted = \count($deletedDecisions);
390+
$nbDeleted = $this->removeRemediations($deletedDecisions);
385391
}
386392

387393
$nbNew = 0;
@@ -485,8 +491,8 @@ public function prune(): bool
485491
private function setCustomErrorHandler(): void
486492
{
487493
if ($this->adapter instanceof MemcachedAdapter) {
488-
set_error_handler(function () {
489-
throw new BouncerException('Error when connecting to Memcached. Please fix the Memcached DSN or select another cache technology.');
494+
set_error_handler(function ($errno, $errmsg) {
495+
throw new BouncerException('Error when connecting to Memcached. Please fix the Memcached DSN or select another cache technology. Original message was: '.$errmsg);
490496
});
491497
}
492498
}

src/Bouncer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ public function configure(array $config): void
7272
$this->config['api_user_agent'],
7373
$this->config['api_key'],
7474
$this->config['cache_expiration_for_clean_ip'],
75-
$this->config['cache_expiration_for_bad_ip']
75+
$this->config['cache_expiration_for_bad_ip'],
76+
$this->config['fallback_remediation']
7677
);
7778
}
7879

0 commit comments

Comments
 (0)