refactor(security): type firewall rule lifecycle events
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -5,6 +5,12 @@ declare(strict_types=1);
|
||||
namespace KTXC\Service;
|
||||
|
||||
use KTXC\Models\Firewall\FirewallRuleObject;
|
||||
use KTXC\Security\Event\FirewallRuleCreatedEvent;
|
||||
use KTXC\Security\Event\FirewallRuleDisabledEvent;
|
||||
use KTXC\Security\Event\FirewallRuleEnabledEvent;
|
||||
use KTXC\Security\Event\FirewallRuleEvent;
|
||||
use KTXC\Security\Event\FirewallRuleExtendedEvent;
|
||||
use KTXC\Security\Event\FirewallRuleRemovedEvent;
|
||||
use KTXC\Stores\FirewallStore;
|
||||
use KTXF\Event\EventDispatcherInterface;
|
||||
use KTXC\Security\Event\SecurityEvent;
|
||||
@@ -282,7 +288,7 @@ final class FirewallRuleManager
|
||||
$this->store->depositRule($rule);
|
||||
$this->cache->invalidate();
|
||||
$this->publishLifecycleEvent(
|
||||
SecurityEvent::FIREWALL_RULE_DISABLED,
|
||||
FirewallRuleDisabledEvent::class,
|
||||
$rule,
|
||||
$actorId,
|
||||
['changeReason' => $reason, 'changeOrigin' => self::ORIGIN_MANUAL]
|
||||
@@ -321,7 +327,7 @@ final class FirewallRuleManager
|
||||
$this->store->depositRule($rule);
|
||||
$this->cache->invalidate();
|
||||
$this->publishLifecycleEvent(
|
||||
SecurityEvent::FIREWALL_RULE_ENABLED,
|
||||
FirewallRuleEnabledEvent::class,
|
||||
$rule,
|
||||
$actorId,
|
||||
['changeReason' => $reason, 'changeOrigin' => self::ORIGIN_MANUAL]
|
||||
@@ -365,7 +371,7 @@ final class FirewallRuleManager
|
||||
$this->store->depositRule($rule);
|
||||
$this->cache->invalidate();
|
||||
$this->publishLifecycleEvent(
|
||||
SecurityEvent::FIREWALL_RULE_EXTENDED,
|
||||
FirewallRuleExtendedEvent::class,
|
||||
$rule,
|
||||
$actorId,
|
||||
[
|
||||
@@ -392,7 +398,7 @@ final class FirewallRuleManager
|
||||
$this->store->destroyRule($rule);
|
||||
$this->cache->invalidate();
|
||||
$this->publishLifecycleEvent(
|
||||
SecurityEvent::FIREWALL_RULE_REMOVED,
|
||||
FirewallRuleRemovedEvent::class,
|
||||
$rule,
|
||||
$actorId,
|
||||
['changeReason' => $reason, 'changeOrigin' => self::ORIGIN_MANUAL]
|
||||
@@ -447,9 +453,10 @@ final class FirewallRuleManager
|
||||
}
|
||||
$rule->setMetadata($metadata);
|
||||
|
||||
$this->store->depositRule($rule);
|
||||
$rule = $this->store->depositRule($rule)
|
||||
?? throw new \RuntimeException('Failed to persist firewall rule.');
|
||||
$this->cache->invalidate();
|
||||
$this->publishLifecycleEvent(SecurityEvent::FIREWALL_RULE_CREATED, $rule);
|
||||
$this->publishLifecycleEvent(FirewallRuleCreatedEvent::class, $rule);
|
||||
|
||||
return $rule;
|
||||
}
|
||||
@@ -486,7 +493,7 @@ final class FirewallRuleManager
|
||||
|
||||
$this->store->depositRule($rule);
|
||||
$this->cache->invalidate();
|
||||
$this->publishLifecycleEvent(SecurityEvent::FIREWALL_RULE_EXTENDED, $rule);
|
||||
$this->publishLifecycleEvent(FirewallRuleExtendedEvent::class, $rule);
|
||||
|
||||
return $rule;
|
||||
}
|
||||
@@ -514,30 +521,17 @@ final class FirewallRuleManager
|
||||
$this->events->dispatch($event);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param class-string<FirewallRuleEvent> $eventClass
|
||||
*/
|
||||
private function publishLifecycleEvent(
|
||||
string $name,
|
||||
string $eventClass,
|
||||
FirewallRuleObject $rule,
|
||||
?string $actorId = null,
|
||||
array $change = []
|
||||
): void
|
||||
{
|
||||
$event = new SecurityEvent(
|
||||
$name,
|
||||
[
|
||||
'ruleId' => $rule->getId(),
|
||||
'ruleScope' => $rule->getScope(),
|
||||
'ruleType' => $rule->getType(),
|
||||
'ruleAction' => $rule->getAction(),
|
||||
'ruleValue' => $rule->getValue(),
|
||||
'reason' => $rule->getReason(),
|
||||
'origin' => $rule->getMetadata()['origin'] ?? self::ORIGIN_MANUAL,
|
||||
'expiresAt' => $rule->getExpiresAt()?->format(\DateTimeInterface::ATOM),
|
||||
...($rule->getMetadata() ?? []),
|
||||
...$change,
|
||||
],
|
||||
tenantId: $rule->getTenantId(),
|
||||
identityId: $actorId ?? $rule->getCreatedBy(),
|
||||
);
|
||||
$event = $eventClass::fromRule($rule, $actorId, $change);
|
||||
$this->events->dispatch($event);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,11 @@ use KTXC\Security\Event\AccessDeniedEvent;
|
||||
use KTXC\Security\Event\AuthenticationFailedEvent;
|
||||
use KTXC\Security\Event\AuthenticationSucceededEvent;
|
||||
use KTXC\Security\Event\BruteForceDetectedEvent;
|
||||
use KTXC\Security\Event\FirewallRuleCreatedEvent;
|
||||
use KTXC\Security\Event\FirewallRuleDisabledEvent;
|
||||
use KTXC\Security\Event\FirewallRuleEnabledEvent;
|
||||
use KTXC\Security\Event\FirewallRuleExtendedEvent;
|
||||
use KTXC\Security\Event\FirewallRuleRemovedEvent;
|
||||
use KTXC\Security\Event\RateLimitExceededEvent;
|
||||
use KTXC\Security\Event\SuspiciousActivityEvent;
|
||||
use KTXC\Security\Event\SecurityEvent;
|
||||
@@ -289,11 +294,11 @@ class FirewallService
|
||||
RateLimitExceededEvent::class => FirewallLogObject::EVENT_RATE_LIMIT,
|
||||
AccessDeniedEvent::class => FirewallLogObject::EVENT_RULE_MATCH,
|
||||
SuspiciousActivityEvent::class => FirewallLogObject::EVENT_SUSPICIOUS,
|
||||
SecurityEvent::FIREWALL_RULE_CREATED => FirewallLogObject::EVENT_RULE_CREATED,
|
||||
SecurityEvent::FIREWALL_RULE_EXTENDED => FirewallLogObject::EVENT_RULE_EXTENDED,
|
||||
SecurityEvent::FIREWALL_RULE_ENABLED => FirewallLogObject::EVENT_RULE_ENABLED,
|
||||
SecurityEvent::FIREWALL_RULE_DISABLED => FirewallLogObject::EVENT_RULE_DISABLED,
|
||||
SecurityEvent::FIREWALL_RULE_REMOVED => FirewallLogObject::EVENT_RULE_REMOVED,
|
||||
FirewallRuleCreatedEvent::class => FirewallLogObject::EVENT_RULE_CREATED,
|
||||
FirewallRuleExtendedEvent::class => FirewallLogObject::EVENT_RULE_EXTENDED,
|
||||
FirewallRuleEnabledEvent::class => FirewallLogObject::EVENT_RULE_ENABLED,
|
||||
FirewallRuleDisabledEvent::class => FirewallLogObject::EVENT_RULE_DISABLED,
|
||||
FirewallRuleRemovedEvent::class => FirewallLogObject::EVENT_RULE_REMOVED,
|
||||
SecurityEvent::FIREWALL_SETTINGS_UPDATED => FirewallLogObject::EVENT_SETTINGS_UPDATED,
|
||||
default => FirewallLogObject::EVENT_ACCESS_CHECK,
|
||||
};
|
||||
@@ -307,11 +312,11 @@ class FirewallService
|
||||
return match ($event->getName()) {
|
||||
AuthenticationSucceededEvent::class,
|
||||
SecurityEvent::ACCESS_GRANTED => FirewallLogObject::RESULT_ALLOWED,
|
||||
SecurityEvent::FIREWALL_RULE_CREATED,
|
||||
SecurityEvent::FIREWALL_RULE_EXTENDED,
|
||||
SecurityEvent::FIREWALL_RULE_ENABLED,
|
||||
SecurityEvent::FIREWALL_RULE_DISABLED,
|
||||
SecurityEvent::FIREWALL_RULE_REMOVED,
|
||||
FirewallRuleCreatedEvent::class,
|
||||
FirewallRuleExtendedEvent::class,
|
||||
FirewallRuleEnabledEvent::class,
|
||||
FirewallRuleDisabledEvent::class,
|
||||
FirewallRuleRemovedEvent::class,
|
||||
SecurityEvent::FIREWALL_SETTINGS_UPDATED => FirewallLogObject::RESULT_RECORDED,
|
||||
default => FirewallLogObject::RESULT_BLOCKED,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user