feat(firewall): add scoped log administration reads
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -66,6 +66,14 @@ class FirewallStore
|
||||
['tenantId' => 1, 'eventType' => 1, 'timestamp' => -1],
|
||||
['name' => 'logs_event_type']
|
||||
),
|
||||
$logs->createIndex(
|
||||
['tenantId' => 1, 'ruleId' => 1, 'timestamp' => -1],
|
||||
['name' => 'logs_rule']
|
||||
),
|
||||
$logs->createIndex(
|
||||
['timestamp' => -1],
|
||||
['name' => 'logs_global_timeline']
|
||||
),
|
||||
$claims->createIndex(
|
||||
['expiresAt' => 1],
|
||||
['name' => 'claims_expiry', 'expireAfterSeconds' => 0]
|
||||
@@ -357,6 +365,62 @@ class FirewallStore
|
||||
// Log Operations
|
||||
// ========================================
|
||||
|
||||
public function queryTenantLogs(
|
||||
string $tenantId,
|
||||
array $filters,
|
||||
int $limit,
|
||||
int $offset
|
||||
): array {
|
||||
return $this->queryLogs(['tenantId' => $tenantId], $filters, $limit, $offset);
|
||||
}
|
||||
|
||||
public function querySystemLogs(
|
||||
?string $tenantId,
|
||||
array $filters,
|
||||
int $limit,
|
||||
int $offset
|
||||
): array {
|
||||
return $this->queryLogs($tenantId === null ? [] : ['tenantId' => $tenantId], $filters, $limit, $offset);
|
||||
}
|
||||
|
||||
/** @return array{items: FirewallLogObject[], total: int, limit: int, offset: int} */
|
||||
private function queryLogs(array $scopeFilter, array $filters, int $limit, int $offset): array
|
||||
{
|
||||
$filter = $scopeFilter;
|
||||
foreach (['ipAddress', 'eventType', 'result', 'ruleId', 'ruleScope'] as $field) {
|
||||
if (($filters[$field] ?? null) !== null) {
|
||||
$filter[$field] = $filters[$field];
|
||||
}
|
||||
}
|
||||
$timestamp = [];
|
||||
if (($filters['from'] ?? null) instanceof \DateTimeInterface) {
|
||||
$timestamp['$gte'] = self::bsonDate($filters['from']);
|
||||
}
|
||||
if (($filters['to'] ?? null) instanceof \DateTimeInterface) {
|
||||
$timestamp['$lte'] = self::bsonDate($filters['to']);
|
||||
}
|
||||
if ($timestamp !== []) {
|
||||
$filter['timestamp'] = $timestamp;
|
||||
}
|
||||
|
||||
$collection = $this->dataStore->selectCollection(self::LOGS_COLLECTION);
|
||||
$items = [];
|
||||
foreach ($collection->find($filter, [
|
||||
'sort' => ['timestamp' => -1, '_id' => -1],
|
||||
'limit' => $limit,
|
||||
'skip' => $offset,
|
||||
]) as $entry) {
|
||||
$items[] = (new FirewallLogObject())->jsonDeserialize((array)$entry);
|
||||
}
|
||||
|
||||
return [
|
||||
'items' => $items,
|
||||
'total' => $collection->countDocuments($filter),
|
||||
'limit' => $limit,
|
||||
'offset' => $offset,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a firewall event
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user