Skip to content

Commit

Permalink
Merge pull request #17 from crowdsecurity/bug/durations
Browse files Browse the repository at this point in the history
fix durations bug
  • Loading branch information
mobula9 authored Dec 12, 2020
2 parents 3e022ba + e4fef86 commit ba3d4da
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
2 changes: 1 addition & 1 deletion docs/contribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,5 @@ gh pr create --fill
#### New release

```bash
gh release create vx.x.x
gh release create --draft vx.x.x
```
33 changes: 18 additions & 15 deletions src/ApiCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Symfony\Component\Cache\PruneableInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use \DateTime;

/**
* The cache mecanism to store every decisions from LAPI/CAPI. Symfony Cache component powered.
Expand Down Expand Up @@ -76,7 +77,7 @@ public function configure(
/**
* Add remediation to a Symfony Cache Item identified by IP
*/
private function addRemediationToCacheItem(string $ip, string $type, int $expiration, int $decisionId): void
private function addRemediationToCacheItem(string $ip, string $type, int $expiration, int $decisionId): string
{
$item = $this->adapter->getItem($ip);

Expand All @@ -103,7 +104,7 @@ private function addRemediationToCacheItem(string $ip, string $type, int $expira
$prioritizedRemediations = Remediation::sortRemediationByPriority($remediations);

$item->set($prioritizedRemediations);
$item->expiresAfter($maxLifetime);
$item->expiresAt(new DateTime('@' . $maxLifetime));

// Save the cache without committing it to the cache system.
// Useful to improve performance when updating the cache.
Expand All @@ -113,6 +114,7 @@ private function addRemediationToCacheItem(string $ip, string $type, int $expira
"$type for $expiration sec, (decision $decisionId)"
);
}
return $prioritizedRemediations[0][0];
}

/**
Expand Down Expand Up @@ -143,7 +145,7 @@ private function removeDecisionFromRemediationItem(string $ip, int $decisionId):
// Build the item lifetime in cache and sort remediations by priority
$maxLifetime = max(array_column($remediations, 1));
$cacheContent = Remediation::sortRemediationByPriority($remediations);
$item->expiresAfter($maxLifetime);
$item->expiresAt(new DateTime('@' . $maxLifetime));
$item->set($cacheContent);

// Save the cache without commiting it to the cache system.
Expand Down Expand Up @@ -174,23 +176,23 @@ private static function parseDurationToSeconds(string $duration): int
};
$seconds = 0;
if (isset($matches[2])) {
$seconds += ((int) $matches[1]) * 3600; // hours
$seconds += ((int) $matches[2]) * 3600; // hours
}
if (isset($matches[3])) {
$seconds += ((int) $matches[2]) * 60; // minutes
$seconds += ((int) $matches[3]) * 60; // minutes
}
if (isset($matches[4])) {
$seconds += ((int) $matches[1]); // seconds
$seconds += ((int) $matches[4]); // seconds
}
if (isset($matches[5])) { // units in milliseconds
if ('m' === ($matches[5])) { // units in milliseconds
$seconds *= 0.001;
}
if (isset($matches[1])) { // negative
if ("-" === ($matches[1])) { // negative
$seconds *= -1;
}
$seconds = round($seconds);

return (int)$seconds;
$seconds = (int)round($seconds);
return $seconds;
}


Expand Down Expand Up @@ -271,8 +273,9 @@ private function removeRemediations(array $decisions): bool
/**
* Update the cached remediation of the specified IP from these new decisions.
*/
private function saveRemediationsForIp(array $decisions, string $ip): void
private function saveRemediationsForIp(array $decisions, string $ip): string
{
$remediationResult = Constants::REMEDIATION_BYPASS;
if (\count($decisions)) {
foreach ($decisions as $decision) {
if (!in_array($decision['type'], Constants::ORDERED_REMEDIATIONS)) {
Expand All @@ -282,13 +285,14 @@ private function saveRemediationsForIp(array $decisions, string $ip): void
$decision['type'] = $highestRemediationLevel;
}
$remediation = $this->formatRemediationFromDecision($decision);
$this->addRemediationToCacheItem($ip, $remediation[0], $remediation[1], $remediation[2]);
$remediationResult = $this->addRemediationToCacheItem($ip, $remediation[0], $remediation[1], $remediation[2]);
}
} else {
$remediation = $this->formatRemediationFromDecision(null);
$this->addRemediationToCacheItem($ip, $remediation[0], $remediation[1], $remediation[2]);
$remediationResult = $this->addRemediationToCacheItem($ip, $remediation[0], $remediation[1], $remediation[2]);
}
$this->adapter->commit();
return $remediationResult;
}

public function clear(): bool
Expand Down Expand Up @@ -364,8 +368,7 @@ private function miss(string $ip): string
$decisions = $this->apiClient->getFilteredDecisions(['ip' => $ip]);
}

$this->saveRemediationsForIp($decisions, $ip);
return $this->hit($ip);
return $this->saveRemediationsForIp($decisions, $ip);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public function getConfigTreeBuilder()
->end()
->end();

// TODO P2 add "live_mode_max_cache_duration" to avoid manually cache clear in this use case:
// A ban is set for a long period, the decision is manually deleted in the meantime:
// With this "live_mode_max_cache_duration" the user has not to wait for the first erroned excessive delay.

return $treeBuilder;
}
}

0 comments on commit ba3d4da

Please sign in to comment.