Skip to content

Commit ba3d4da

Browse files
authored
Merge pull request #17 from crowdsecurity/bug/durations
fix durations bug
2 parents 3e022ba + e4fef86 commit ba3d4da

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

docs/contribute.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,5 @@ gh pr create --fill
7171
#### New release
7272

7373
```bash
74-
gh release create vx.x.x
74+
gh release create --draft vx.x.x
7575
```

src/ApiCache.php

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Symfony\Component\Cache\PruneableInterface;
99
use Psr\Log\LoggerInterface;
1010
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
11+
use \DateTime;
1112

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

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

105106
$item->set($prioritizedRemediations);
106-
$item->expiresAfter($maxLifetime);
107+
$item->expiresAt(new DateTime('@' . $maxLifetime));
107108

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

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

149151
// Save the cache without commiting it to the cache system.
@@ -174,23 +176,23 @@ private static function parseDurationToSeconds(string $duration): int
174176
};
175177
$seconds = 0;
176178
if (isset($matches[2])) {
177-
$seconds += ((int) $matches[1]) * 3600; // hours
179+
$seconds += ((int) $matches[2]) * 3600; // hours
178180
}
179181
if (isset($matches[3])) {
180-
$seconds += ((int) $matches[2]) * 60; // minutes
182+
$seconds += ((int) $matches[3]) * 60; // minutes
181183
}
182184
if (isset($matches[4])) {
183-
$seconds += ((int) $matches[1]); // seconds
185+
$seconds += ((int) $matches[4]); // seconds
184186
}
185-
if (isset($matches[5])) { // units in milliseconds
187+
if ('m' === ($matches[5])) { // units in milliseconds
186188
$seconds *= 0.001;
187189
}
188-
if (isset($matches[1])) { // negative
190+
if ("-" === ($matches[1])) { // negative
189191
$seconds *= -1;
190192
}
191-
$seconds = round($seconds);
192193

193-
return (int)$seconds;
194+
$seconds = (int)round($seconds);
195+
return $seconds;
194196
}
195197

196198

@@ -271,8 +273,9 @@ private function removeRemediations(array $decisions): bool
271273
/**
272274
* Update the cached remediation of the specified IP from these new decisions.
273275
*/
274-
private function saveRemediationsForIp(array $decisions, string $ip): void
276+
private function saveRemediationsForIp(array $decisions, string $ip): string
275277
{
278+
$remediationResult = Constants::REMEDIATION_BYPASS;
276279
if (\count($decisions)) {
277280
foreach ($decisions as $decision) {
278281
if (!in_array($decision['type'], Constants::ORDERED_REMEDIATIONS)) {
@@ -282,13 +285,14 @@ private function saveRemediationsForIp(array $decisions, string $ip): void
282285
$decision['type'] = $highestRemediationLevel;
283286
}
284287
$remediation = $this->formatRemediationFromDecision($decision);
285-
$this->addRemediationToCacheItem($ip, $remediation[0], $remediation[1], $remediation[2]);
288+
$remediationResult = $this->addRemediationToCacheItem($ip, $remediation[0], $remediation[1], $remediation[2]);
286289
}
287290
} else {
288291
$remediation = $this->formatRemediationFromDecision(null);
289-
$this->addRemediationToCacheItem($ip, $remediation[0], $remediation[1], $remediation[2]);
292+
$remediationResult = $this->addRemediationToCacheItem($ip, $remediation[0], $remediation[1], $remediation[2]);
290293
}
291294
$this->adapter->commit();
295+
return $remediationResult;
292296
}
293297

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

367-
$this->saveRemediationsForIp($decisions, $ip);
368-
return $this->hit($ip);
371+
return $this->saveRemediationsForIp($decisions, $ip);
369372
}
370373

371374
/**

src/Configuration.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ public function getConfigTreeBuilder()
4646
->end()
4747
->end();
4848

49+
// TODO P2 add "live_mode_max_cache_duration" to avoid manually cache clear in this use case:
50+
// A ban is set for a long period, the decision is manually deleted in the meantime:
51+
// With this "live_mode_max_cache_duration" the user has not to wait for the first erroned excessive delay.
52+
4953
return $treeBuilder;
5054
}
5155
}

0 commit comments

Comments
 (0)