refactor(security): type firewall policy events
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -20,6 +20,7 @@ use KTXC\Security\Event\FirewallRuleDisabledEvent;
|
||||
use KTXC\Security\Event\FirewallRuleEnabledEvent;
|
||||
use KTXC\Security\Event\FirewallRuleExtendedEvent;
|
||||
use KTXC\Security\Event\FirewallRuleRemovedEvent;
|
||||
use KTXC\Security\Event\FirewallSettingsUpdatedEvent;
|
||||
use KTXC\Security\Event\RateLimitExceededEvent;
|
||||
use KTXC\Security\Event\SuspiciousActivityEvent;
|
||||
use KTXC\Security\Event\SecurityEvent;
|
||||
@@ -63,7 +64,7 @@ class Module extends ModuleInstanceAbstract implements ModuleConsoleInterface, M
|
||||
FirewallRuleEnabledEvent::class,
|
||||
FirewallRuleDisabledEvent::class,
|
||||
FirewallRuleRemovedEvent::class,
|
||||
SecurityEvent::FIREWALL_SETTINGS_UPDATED,
|
||||
FirewallSettingsUpdatedEvent::class,
|
||||
] as $event) {
|
||||
$this->events->listen(
|
||||
'core',
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KTXC\Security\Event;
|
||||
|
||||
use KTXF\Event\Event;
|
||||
|
||||
final class DeviceBlockedEvent extends Event implements SecurityRequestEventInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly string $deviceFingerprint,
|
||||
private readonly ?string $reason = null,
|
||||
?string $tenantId = null,
|
||||
) {
|
||||
if ($deviceFingerprint === '') {
|
||||
throw new \InvalidArgumentException('Device-block events require a fingerprint.');
|
||||
}
|
||||
|
||||
parent::__construct(
|
||||
self::class,
|
||||
['device' => $deviceFingerprint, 'reason' => $reason],
|
||||
$tenantId,
|
||||
);
|
||||
}
|
||||
|
||||
public function getIpAddress(): ?string
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getDeviceFingerprint(): string
|
||||
{
|
||||
return $this->deviceFingerprint;
|
||||
}
|
||||
|
||||
public function getUserAgent(): ?string
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getRequestPath(): ?string
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getRequestMethod(): ?string
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getUserId(): ?string
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getReason(): ?string
|
||||
{
|
||||
return $this->reason;
|
||||
}
|
||||
|
||||
public function getSeverity(): int
|
||||
{
|
||||
return SecurityEvent::SEVERITY_CRITICAL;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KTXC\Security\Event;
|
||||
|
||||
use KTXF\Event\Event;
|
||||
|
||||
abstract class FirewallIpEvent extends Event implements SecurityRequestEventInterface
|
||||
{
|
||||
protected const SEVERITY = SecurityEvent::SEVERITY_INFO;
|
||||
|
||||
final public function __construct(
|
||||
private readonly string $ipAddress,
|
||||
private readonly ?string $reason = null,
|
||||
?string $tenantId = null,
|
||||
) {
|
||||
if ($ipAddress === '') {
|
||||
throw new \InvalidArgumentException('Firewall IP events require an IP address.');
|
||||
}
|
||||
|
||||
parent::__construct(
|
||||
static::class,
|
||||
['ip' => $ipAddress, 'reason' => $reason],
|
||||
$tenantId,
|
||||
);
|
||||
}
|
||||
|
||||
public function getIpAddress(): string
|
||||
{
|
||||
return $this->ipAddress;
|
||||
}
|
||||
|
||||
public function getDeviceFingerprint(): ?string
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getUserAgent(): ?string
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getRequestPath(): ?string
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getRequestMethod(): ?string
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getUserId(): ?string
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getReason(): ?string
|
||||
{
|
||||
return $this->reason;
|
||||
}
|
||||
|
||||
public function getSeverity(): int
|
||||
{
|
||||
return static::SEVERITY;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KTXC\Security\Event;
|
||||
|
||||
use KTXF\Event\Event;
|
||||
|
||||
final class FirewallSettingsUpdatedEvent extends Event implements SecurityEventInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly string $changeReason,
|
||||
private readonly array $previous,
|
||||
private readonly array $current,
|
||||
string $tenantId,
|
||||
?string $actorId = null,
|
||||
private readonly string $changeOrigin = 'manual',
|
||||
) {
|
||||
if ($changeReason === '') {
|
||||
throw new \InvalidArgumentException('Firewall settings updates require a change reason.');
|
||||
}
|
||||
if ($tenantId === '') {
|
||||
throw new \InvalidArgumentException('Firewall settings updates require a tenant ID.');
|
||||
}
|
||||
if ($changeOrigin === '') {
|
||||
throw new \InvalidArgumentException('Firewall settings updates require a change origin.');
|
||||
}
|
||||
|
||||
parent::__construct(
|
||||
self::class,
|
||||
[
|
||||
'changeReason' => $changeReason,
|
||||
'changeOrigin' => $changeOrigin,
|
||||
'previous' => $previous,
|
||||
'current' => $current,
|
||||
],
|
||||
$tenantId,
|
||||
$actorId,
|
||||
);
|
||||
}
|
||||
|
||||
public function getChangeReason(): string
|
||||
{
|
||||
return $this->changeReason;
|
||||
}
|
||||
|
||||
public function getPrevious(): array
|
||||
{
|
||||
return $this->previous;
|
||||
}
|
||||
|
||||
public function getCurrent(): array
|
||||
{
|
||||
return $this->current;
|
||||
}
|
||||
|
||||
public function getChangeOrigin(): string
|
||||
{
|
||||
return $this->changeOrigin;
|
||||
}
|
||||
|
||||
public function getUserId(): ?string
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getReason(): string
|
||||
{
|
||||
return $this->changeReason;
|
||||
}
|
||||
|
||||
public function getSeverity(): int
|
||||
{
|
||||
return SecurityEvent::SEVERITY_INFO;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KTXC\Security\Event;
|
||||
|
||||
final class IpAllowedEvent extends FirewallIpEvent
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KTXC\Security\Event;
|
||||
|
||||
final class IpBlockedEvent extends FirewallIpEvent
|
||||
{
|
||||
protected const SEVERITY = SecurityEvent::SEVERITY_CRITICAL;
|
||||
}
|
||||
@@ -18,10 +18,6 @@ final class SecurityEvent extends Event implements SecurityRequestEventInterface
|
||||
|
||||
public const ACCESS_GRANTED = 'security.access.granted';
|
||||
|
||||
public const IP_BLOCKED = 'security.ip.blocked';
|
||||
public const IP_ALLOWED = 'security.ip.allowed';
|
||||
public const DEVICE_BLOCKED = 'security.device.blocked';
|
||||
public const FIREWALL_SETTINGS_UPDATED = 'security.firewall.settings.updated';
|
||||
|
||||
// Severity levels
|
||||
public const SEVERITY_DEBUG = 0;
|
||||
@@ -96,9 +92,6 @@ final class SecurityEvent extends Event implements SecurityRequestEventInterface
|
||||
self::AUTH_LOGOUT,
|
||||
self::TOKEN_REVOKED => self::SEVERITY_WARNING,
|
||||
|
||||
self::IP_BLOCKED,
|
||||
self::DEVICE_BLOCKED => self::SEVERITY_CRITICAL,
|
||||
|
||||
default => self::SEVERITY_INFO,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -11,9 +11,11 @@ use KTXC\Security\Event\FirewallRuleEnabledEvent;
|
||||
use KTXC\Security\Event\FirewallRuleEvent;
|
||||
use KTXC\Security\Event\FirewallRuleExtendedEvent;
|
||||
use KTXC\Security\Event\FirewallRuleRemovedEvent;
|
||||
use KTXC\Security\Event\DeviceBlockedEvent;
|
||||
use KTXC\Security\Event\IpAllowedEvent;
|
||||
use KTXC\Security\Event\IpBlockedEvent;
|
||||
use KTXC\Stores\FirewallStore;
|
||||
use KTXF\Event\EventDispatcherInterface;
|
||||
use KTXC\Security\Event\SecurityEvent;
|
||||
use KTXF\IpUtils;
|
||||
|
||||
final class FirewallRuleManager
|
||||
@@ -192,7 +194,7 @@ final class FirewallRuleManager
|
||||
$origin,
|
||||
$metadata
|
||||
);
|
||||
$this->publishIpEvent(SecurityEvent::IP_BLOCKED, $scope, $ipAddress, $reason);
|
||||
$this->events->dispatch(new IpBlockedEvent($ipAddress, $reason, $scope->tenantId));
|
||||
|
||||
return $rule;
|
||||
}
|
||||
@@ -215,7 +217,7 @@ final class FirewallRuleManager
|
||||
null,
|
||||
$origin
|
||||
);
|
||||
$this->publishIpEvent(SecurityEvent::IP_ALLOWED, $scope, $ipAddress, $reason);
|
||||
$this->events->dispatch(new IpAllowedEvent($ipAddress, $reason, $scope->tenantId));
|
||||
|
||||
return $rule;
|
||||
}
|
||||
@@ -260,13 +262,7 @@ final class FirewallRuleManager
|
||||
$origin
|
||||
);
|
||||
|
||||
$event = new SecurityEvent(
|
||||
SecurityEvent::DEVICE_BLOCKED,
|
||||
['device' => $fingerprint, 'reason' => $reason],
|
||||
tenantId: $scope->tenantId,
|
||||
deviceFingerprint: $fingerprint,
|
||||
reason: $reason,
|
||||
);
|
||||
$event = new DeviceBlockedEvent($fingerprint, $reason, $scope->tenantId);
|
||||
$this->events->dispatch($event);
|
||||
|
||||
return $rule;
|
||||
@@ -505,22 +501,6 @@ final class FirewallRuleManager
|
||||
return $rule && $scope->owns($rule) ? $rule : null;
|
||||
}
|
||||
|
||||
private function publishIpEvent(
|
||||
string $name,
|
||||
FirewallRuleScope $scope,
|
||||
string $ipAddress,
|
||||
?string $reason
|
||||
): void {
|
||||
$event = new SecurityEvent(
|
||||
$name,
|
||||
['ip' => $ipAddress, 'reason' => $reason],
|
||||
tenantId: $scope->tenantId,
|
||||
ipAddress: $ipAddress,
|
||||
reason: $reason,
|
||||
);
|
||||
$this->events->dispatch($event);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param class-string<FirewallRuleEvent> $eventClass
|
||||
*/
|
||||
|
||||
@@ -19,6 +19,7 @@ use KTXC\Security\Event\FirewallRuleDisabledEvent;
|
||||
use KTXC\Security\Event\FirewallRuleEnabledEvent;
|
||||
use KTXC\Security\Event\FirewallRuleExtendedEvent;
|
||||
use KTXC\Security\Event\FirewallRuleRemovedEvent;
|
||||
use KTXC\Security\Event\FirewallSettingsUpdatedEvent;
|
||||
use KTXC\Security\Event\RateLimitExceededEvent;
|
||||
use KTXC\Security\Event\SuspiciousActivityEvent;
|
||||
use KTXC\Security\Event\SecurityEvent;
|
||||
@@ -299,7 +300,7 @@ class FirewallService
|
||||
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,
|
||||
FirewallSettingsUpdatedEvent::class => FirewallLogObject::EVENT_SETTINGS_UPDATED,
|
||||
default => FirewallLogObject::EVENT_ACCESS_CHECK,
|
||||
};
|
||||
}
|
||||
@@ -317,7 +318,7 @@ class FirewallService
|
||||
FirewallRuleEnabledEvent::class,
|
||||
FirewallRuleDisabledEvent::class,
|
||||
FirewallRuleRemovedEvent::class,
|
||||
SecurityEvent::FIREWALL_SETTINGS_UPDATED => FirewallLogObject::RESULT_RECORDED,
|
||||
FirewallSettingsUpdatedEvent::class => FirewallLogObject::RESULT_RECORDED,
|
||||
default => FirewallLogObject::RESULT_BLOCKED,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ namespace KTXC\Service;
|
||||
|
||||
use KTXC\Models\Tenant\TenantConfiguration;
|
||||
use KTXF\Event\EventDispatcherInterface;
|
||||
use KTXC\Security\Event\SecurityEvent;
|
||||
use KTXC\Security\Event\FirewallSettingsUpdatedEvent;
|
||||
|
||||
final class FirewallSettingsService
|
||||
{
|
||||
@@ -52,16 +52,13 @@ final class FirewallSettingsService
|
||||
$tenant->setConfiguration($configuration);
|
||||
$this->tenants->deposit($tenant);
|
||||
|
||||
$event = new SecurityEvent(
|
||||
SecurityEvent::FIREWALL_SETTINGS_UPDATED,
|
||||
[
|
||||
'changeReason' => $reason,
|
||||
'changeOrigin' => FirewallRuleManager::ORIGIN_MANUAL,
|
||||
'previous' => $previous,
|
||||
'current' => $current,
|
||||
],
|
||||
$event = new FirewallSettingsUpdatedEvent(
|
||||
changeReason: $reason,
|
||||
previous: $previous,
|
||||
current: $current,
|
||||
tenantId: $tenantId,
|
||||
identityId: $actorId,
|
||||
actorId: $actorId,
|
||||
changeOrigin: FirewallRuleManager::ORIGIN_MANUAL,
|
||||
);
|
||||
$this->events->dispatch($event);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user