@@ -25,23 +25,23 @@ class ApiCache
25
25
/** @var LoggerInterface */
26
26
private $ logger ;
27
27
28
+ /** @var ApiClient */
29
+ private $ apiClient ;
30
+
28
31
/** @var AbstractAdapter */
29
32
private $ adapter ;
30
33
31
34
/** @var bool */
32
- private $ liveMode ;
35
+ private $ liveMode = null ;
33
36
34
37
/** @var int */
35
- private $ cacheExpirationForCleanIp ;
38
+ private $ cacheExpirationForCleanIp = null ;
36
39
37
40
/** @var int */
38
- private $ cacheExpirationForBadIp ;
39
-
40
- /** @var ApiClient */
41
- private $ apiClient ;
41
+ private $ cacheExpirationForBadIp = null ;
42
42
43
43
/** @var bool */
44
- private $ warmedUp ;
44
+ private $ warmedUp = null ;
45
45
46
46
public function __construct (LoggerInterface $ logger , ApiClient $ apiClient = null , AbstractAdapter $ adapter = null )
47
47
{
@@ -69,7 +69,7 @@ public function configure(
69
69
$ cacheConfig = $ cacheConfigItem ->get ();
70
70
$ this ->warmedUp = (\is_array ($ cacheConfig ) && isset ($ cacheConfig ['warmed_up ' ])
71
71
&& true === $ cacheConfig ['warmed_up ' ]);
72
- $ this ->logger ->debug (null , [
72
+ $ this ->logger ->debug ('' , [
73
73
'type ' => 'API_CACHE_INIT ' ,
74
74
'adapter ' => \get_class ($ this ->adapter ),
75
75
'mode ' => ($ liveMode ? 'live ' : 'stream ' ),
@@ -92,7 +92,7 @@ private function addRemediationToCacheItem(string $ip, string $type, int $expira
92
92
93
93
$ index = array_search (Constants::REMEDIATION_BYPASS , array_column ($ remediations , 0 ));
94
94
if (false !== $ index ) {
95
- $ this ->logger ->debug (null , [
95
+ $ this ->logger ->debug ('' , [
96
96
'type ' => 'IP_CLEAN_TO_BAD ' ,
97
97
'ip ' => $ ip ,
98
98
'old_remediation ' => Constants::REMEDIATION_BYPASS ,
@@ -142,7 +142,7 @@ private function removeDecisionFromRemediationItem(string $ip, int $decisionId):
142
142
unset($ remediations [$ index ]);
143
143
144
144
if (!$ remediations ) {
145
- $ this ->logger ->debug (null , [
145
+ $ this ->logger ->debug ('' , [
146
146
'type ' => 'CACHE_ITEM_REMOVED ' ,
147
147
'ip ' => $ ip ,
148
148
]);
@@ -161,7 +161,7 @@ private function removeDecisionFromRemediationItem(string $ip, int $decisionId):
161
161
if (!$ this ->adapter ->saveDeferred ($ item )) {
162
162
throw new BouncerException ("cache# $ ip: Unable to save item " );
163
163
}
164
- $ this ->logger ->debug (null , [
164
+ $ this ->logger ->debug ('' , [
165
165
'type ' => 'DECISION_REMOVED ' ,
166
166
'decision ' => $ decisionId ,
167
167
'ips ' => [$ ip ],
@@ -251,7 +251,7 @@ private function removeRemediations(array $decisions): bool
251
251
foreach ($ decisions as $ decision ) {
252
252
if (\is_int ($ decision ['start_ip ' ]) && \is_int ($ decision ['end_ip ' ])) {
253
253
$ ipRange = array_map ('long2ip ' , range ($ decision ['start_ip ' ], $ decision ['end_ip ' ]));
254
- $ this ->logger ->debug (null , [
254
+ $ this ->logger ->debug ('' , [
255
255
'type ' => 'DECISION_REMOVED ' , 'decision ' => $ decision ['id ' ], 'ips ' => $ ipRange ,
256
256
]);
257
257
$ success = true ;
@@ -263,7 +263,7 @@ private function removeRemediations(array $decisions): bool
263
263
if (!$ success ) {
264
264
// The API may return stale deletion events due to API design.
265
265
// Ignoring them is therefore not a problem.
266
- $ this ->logger ->debug (null , ['type ' => 'DECISION_TO_REMOVE_NOT_FOUND_IN_CACHE ' , 'decision ' => $ decision ['id ' ]]);
266
+ $ this ->logger ->debug ('' , ['type ' => 'DECISION_TO_REMOVE_NOT_FOUND_IN_CACHE ' , 'decision ' => $ decision ['id ' ]]);
267
267
}
268
268
}
269
269
}
@@ -282,7 +282,7 @@ private function saveRemediationsForIp(array $decisions, string $ip): string
282
282
if (!\in_array ($ decision ['type ' ], Constants::ORDERED_REMEDIATIONS )) {
283
283
$ highestRemediationLevel = Constants::ORDERED_REMEDIATIONS [0 ];
284
284
// TODO P1 test the case of unknown remediation type
285
- $ this ->logger ->warning (null , ['type ' => 'UNKNOWN_REMEDIATION ' , 'remediation ' => $ decision ['type ' ]]);
285
+ $ this ->logger ->warning ('' , ['type ' => 'UNKNOWN_REMEDIATION ' , 'remediation ' => $ decision ['type ' ]]);
286
286
// TODO P2 use the fallback parameter instead.
287
287
$ decision ['type ' ] = $ highestRemediationLevel ;
288
288
}
@@ -304,7 +304,7 @@ public function clear(): bool
304
304
$ this ->warmedUp = false ;
305
305
$ this ->defferUpdateCacheConfig (['warmed_up ' => $ this ->warmedUp ]);
306
306
$ this ->adapter ->commit ();
307
- $ this ->logger ->info (null , ['type ' => 'CACHE_CLEARED ' ]);
307
+ $ this ->logger ->info ('' , ['type ' => 'CACHE_CLEARED ' ]);
308
308
309
309
return $ cleared ;
310
310
}
@@ -321,7 +321,7 @@ public function warmUp(): int
321
321
if ($ this ->warmedUp ) {
322
322
$ this ->clear ();
323
323
}
324
- $ this ->logger ->debug (null , ['type ' => 'START_CACHE_WARMUP ' ]);
324
+ $ this ->logger ->debug ('' , ['type ' => 'START_CACHE_WARMUP ' ]);
325
325
$ startup = true ;
326
326
$ decisionsDiff = $ this ->apiClient ->getStreamedDecisions ($ startup );
327
327
$ newDecisions = $ decisionsDiff ['new ' ];
@@ -341,7 +341,7 @@ public function warmUp(): int
341
341
$ this ->defferUpdateCacheConfig (['warmed_up ' => true ]);
342
342
343
343
$ this ->adapter ->commit ();
344
- $ this ->logger ->info (null , ['type ' => 'CACHE_WARMED_UP ' , 'added_decisions ' => $ nbNew ]);
344
+ $ this ->logger ->info ('' , ['type ' => 'CACHE_WARMED_UP ' , 'added_decisions ' => $ nbNew ]);
345
345
346
346
return $ nbNew ;
347
347
}
@@ -359,7 +359,7 @@ public function pullUpdates(): array
359
359
return ['deleted ' => 0 , 'new ' => $ this ->warmUp ()];
360
360
}
361
361
362
- $ this ->logger ->debug (null , ['type ' => 'START_CACHE_UPDATE ' ]);
362
+ $ this ->logger ->debug ('' , ['type ' => 'START_CACHE_UPDATE ' ]);
363
363
$ decisionsDiff = $ this ->apiClient ->getStreamedDecisions ();
364
364
$ newDecisions = $ decisionsDiff ['new ' ];
365
365
$ deletedDecisions = $ decisionsDiff ['deleted ' ];
@@ -376,7 +376,7 @@ public function pullUpdates(): array
376
376
$ nbNew = \count ($ newDecisions );
377
377
}
378
378
379
- $ this ->logger ->debug (null , ['type ' => 'CACHE_UPDATED ' , 'deleted ' => $ nbDeleted , 'new ' => $ nbNew ]);
379
+ $ this ->logger ->debug ('' , ['type ' => 'CACHE_UPDATED ' , 'deleted ' => $ nbDeleted , 'new ' => $ nbNew ]);
380
380
381
381
return ['deleted ' => $ nbDeleted , 'new ' => $ nbNew ];
382
382
}
@@ -392,7 +392,7 @@ private function miss(string $ip): string
392
392
$ decisions = [];
393
393
394
394
if ($ this ->liveMode ) {
395
- $ this ->logger ->debug (null , ['type ' => 'DIRECT_API_CALL ' , 'ip ' => $ ip ]);
395
+ $ this ->logger ->debug ('' , ['type ' => 'DIRECT_API_CALL ' , 'ip ' => $ ip ]);
396
396
$ decisions = $ this ->apiClient ->getFilteredDecisions (['ip ' => $ ip ]);
397
397
}
398
398
@@ -422,7 +422,7 @@ private function hit(string $ip): string
422
422
*/
423
423
public function get (string $ ip ): string
424
424
{
425
- $ this ->logger ->debug (null , ['type ' => 'START_IP_CHECK ' , 'ip ' => $ ip ]);
425
+ $ this ->logger ->debug ('' , ['type ' => 'START_IP_CHECK ' , 'ip ' => $ ip ]);
426
426
if (!$ this ->liveMode && !$ this ->warmedUp ) {
427
427
throw new BouncerException ('CrowdSec Bouncer configured in "stream" mode. Please warm the cache up before trying to access it. ' );
428
428
}
@@ -436,9 +436,9 @@ public function get(string $ip): string
436
436
}
437
437
438
438
if (Constants::REMEDIATION_BYPASS === $ remediation ) {
439
- $ this ->logger ->info (null , ['type ' => 'CLEAN_IP ' , 'ip ' => $ ip , 'cache ' => $ cache ]);
439
+ $ this ->logger ->info ('' , ['type ' => 'CLEAN_IP ' , 'ip ' => $ ip , 'cache ' => $ cache ]);
440
440
} else {
441
- $ this ->logger ->warning (null , [
441
+ $ this ->logger ->warning ('' , [
442
442
'type ' => 'BAD_IP ' ,
443
443
'ip ' => $ ip ,
444
444
'remediation ' => $ remediation ,
@@ -453,7 +453,7 @@ public function prune(): bool
453
453
{
454
454
if ($ this ->adapter instanceof PruneableInterface) {
455
455
$ pruned = $ this ->adapter ->prune ();
456
- $ this ->logger ->debug (null , ['type ' => 'CACHE_PRUNED ' ]);
456
+ $ this ->logger ->debug ('' , ['type ' => 'CACHE_PRUNED ' ]);
457
457
458
458
return $ pruned ;
459
459
}
0 commit comments