8
8
use Symfony \Component \Cache \PruneableInterface ;
9
9
use Psr \Log \LoggerInterface ;
10
10
use Symfony \Component \Cache \Adapter \FilesystemAdapter ;
11
+ use \DateTime ;
11
12
12
13
/**
13
14
* The cache mecanism to store every decisions from LAPI/CAPI. Symfony Cache component powered.
@@ -76,7 +77,7 @@ public function configure(
76
77
/**
77
78
* Add remediation to a Symfony Cache Item identified by IP
78
79
*/
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
80
81
{
81
82
$ item = $ this ->adapter ->getItem ($ ip );
82
83
@@ -103,7 +104,7 @@ private function addRemediationToCacheItem(string $ip, string $type, int $expira
103
104
$ prioritizedRemediations = Remediation::sortRemediationByPriority ($ remediations );
104
105
105
106
$ item ->set ($ prioritizedRemediations );
106
- $ item ->expiresAfter ( $ maxLifetime );
107
+ $ item ->expiresAt ( new DateTime ( ' @ ' . $ maxLifetime) );
107
108
108
109
// Save the cache without committing it to the cache system.
109
110
// Useful to improve performance when updating the cache.
@@ -113,6 +114,7 @@ private function addRemediationToCacheItem(string $ip, string $type, int $expira
113
114
"$ type for $ expiration sec, (decision $ decisionId) "
114
115
);
115
116
}
117
+ return $ prioritizedRemediations [0 ][0 ];
116
118
}
117
119
118
120
/**
@@ -143,7 +145,7 @@ private function removeDecisionFromRemediationItem(string $ip, int $decisionId):
143
145
// Build the item lifetime in cache and sort remediations by priority
144
146
$ maxLifetime = max (array_column ($ remediations , 1 ));
145
147
$ cacheContent = Remediation::sortRemediationByPriority ($ remediations );
146
- $ item ->expiresAfter ( $ maxLifetime );
148
+ $ item ->expiresAt ( new DateTime ( ' @ ' . $ maxLifetime) );
147
149
$ item ->set ($ cacheContent );
148
150
149
151
// Save the cache without commiting it to the cache system.
@@ -174,23 +176,23 @@ private static function parseDurationToSeconds(string $duration): int
174
176
};
175
177
$ seconds = 0 ;
176
178
if (isset ($ matches [2 ])) {
177
- $ seconds += ((int ) $ matches [1 ]) * 3600 ; // hours
179
+ $ seconds += ((int ) $ matches [2 ]) * 3600 ; // hours
178
180
}
179
181
if (isset ($ matches [3 ])) {
180
- $ seconds += ((int ) $ matches [2 ]) * 60 ; // minutes
182
+ $ seconds += ((int ) $ matches [3 ]) * 60 ; // minutes
181
183
}
182
184
if (isset ($ matches [4 ])) {
183
- $ seconds += ((int ) $ matches [1 ]); // seconds
185
+ $ seconds += ((int ) $ matches [4 ]); // seconds
184
186
}
185
- if (isset ($ matches [5 ])) { // units in milliseconds
187
+ if (' m ' === ($ matches [5 ])) { // units in milliseconds
186
188
$ seconds *= 0.001 ;
187
189
}
188
- if (isset ($ matches [1 ])) { // negative
190
+ if (" - " === ($ matches [1 ])) { // negative
189
191
$ seconds *= -1 ;
190
192
}
191
- $ seconds = round ($ seconds );
192
193
193
- return (int )$ seconds ;
194
+ $ seconds = (int )round ($ seconds );
195
+ return $ seconds ;
194
196
}
195
197
196
198
@@ -271,8 +273,9 @@ private function removeRemediations(array $decisions): bool
271
273
/**
272
274
* Update the cached remediation of the specified IP from these new decisions.
273
275
*/
274
- private function saveRemediationsForIp (array $ decisions , string $ ip ): void
276
+ private function saveRemediationsForIp (array $ decisions , string $ ip ): string
275
277
{
278
+ $ remediationResult = Constants::REMEDIATION_BYPASS ;
276
279
if (\count ($ decisions )) {
277
280
foreach ($ decisions as $ decision ) {
278
281
if (!in_array ($ decision ['type ' ], Constants::ORDERED_REMEDIATIONS )) {
@@ -282,13 +285,14 @@ private function saveRemediationsForIp(array $decisions, string $ip): void
282
285
$ decision ['type ' ] = $ highestRemediationLevel ;
283
286
}
284
287
$ 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 ]);
286
289
}
287
290
} else {
288
291
$ 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 ]);
290
293
}
291
294
$ this ->adapter ->commit ();
295
+ return $ remediationResult ;
292
296
}
293
297
294
298
public function clear (): bool
@@ -364,8 +368,7 @@ private function miss(string $ip): string
364
368
$ decisions = $ this ->apiClient ->getFilteredDecisions (['ip ' => $ ip ]);
365
369
}
366
370
367
- $ this ->saveRemediationsForIp ($ decisions , $ ip );
368
- return $ this ->hit ($ ip );
371
+ return $ this ->saveRemediationsForIp ($ decisions , $ ip );
369
372
}
370
373
371
374
/**
0 commit comments