refactor(security): type firewall policy events

Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
2026-08-05 23:56:28 -04:00
parent 2494cef02f
commit 3f9c2500d9
16 changed files with 377 additions and 59 deletions
@@ -0,0 +1,79 @@
<?php
declare(strict_types=1);
namespace KTXT\Unit\Event;
use KTXC\Security\Event\DeviceBlockedEvent;
use KTXC\Security\Event\FirewallSettingsUpdatedEvent;
use KTXC\Security\Event\IpAllowedEvent;
use KTXC\Security\Event\IpBlockedEvent;
use KTXC\Security\Event\SecurityEvent;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
final class FirewallPolicyEventTest extends TestCase
{
#[Test]
#[TestDox('IP and device policy events expose typed immutable state')]
public function constructsSubjectPolicyEvents(): void
{
$blocked = new IpBlockedEvent('203.0.113.10', 'Repeated abuse', 'tenant-a');
$allowed = new IpAllowedEvent('203.0.113.11', 'Trusted service', 'tenant-a');
$device = new DeviceBlockedEvent('device-a', 'Compromised device', 'tenant-a');
self::assertSame(IpBlockedEvent::class, $blocked->getName());
self::assertSame('203.0.113.10', $blocked->getIpAddress());
self::assertSame('Repeated abuse', $blocked->getReason());
self::assertSame(SecurityEvent::SEVERITY_CRITICAL, $blocked->getSeverity());
self::assertSame(IpAllowedEvent::class, $allowed->getName());
self::assertSame('203.0.113.11', $allowed->getIpAddress());
self::assertSame(SecurityEvent::SEVERITY_INFO, $allowed->getSeverity());
self::assertSame(DeviceBlockedEvent::class, $device->getName());
self::assertSame('device-a', $device->getDeviceFingerprint());
self::assertSame(SecurityEvent::SEVERITY_CRITICAL, $device->getSeverity());
}
#[Test]
#[TestDox('Firewall settings events capture the complete configuration transition')]
public function constructsSettingsEvent(): void
{
$event = new FirewallSettingsUpdatedEvent(
'Tighten controls',
['maxAuthFailures' => 5],
['maxAuthFailures' => 3],
'tenant-a',
'operator-a',
);
self::assertSame(FirewallSettingsUpdatedEvent::class, $event->getName());
self::assertSame('Tighten controls', $event->getChangeReason());
self::assertSame(['maxAuthFailures' => 5], $event->getPrevious());
self::assertSame(['maxAuthFailures' => 3], $event->getCurrent());
self::assertSame('tenant-a', $event->getTenantId());
self::assertSame('operator-a', $event->getIdentityId());
self::assertSame('manual', $event->getChangeOrigin());
self::assertSame(SecurityEvent::SEVERITY_INFO, $event->getSeverity());
}
#[Test]
#[TestDox('Firewall policy events reject missing subject and ownership context')]
public function rejectsIncompletePolicyContext(): void
{
foreach ([
static fn() => new IpBlockedEvent(''),
static fn() => new IpAllowedEvent(''),
static fn() => new DeviceBlockedEvent(''),
static fn() => new FirewallSettingsUpdatedEvent('', [], [], 'tenant-a'),
static fn() => new FirewallSettingsUpdatedEvent('Reason', [], [], ''),
] as $construction) {
try {
$construction();
self::fail('Incomplete firewall policy context was accepted.');
} catch (\InvalidArgumentException) {
$this->addToAssertionCount(1);
}
}
}
}
+1 -5
View File
@@ -19,13 +19,9 @@ final class SecurityEventTest extends TestCase
SecurityEvent::SEVERITY_WARNING,
(new SecurityEvent(SecurityEvent::AUTH_LOGOUT))->getSeverity(),
);
self::assertSame(
SecurityEvent::SEVERITY_CRITICAL,
(new SecurityEvent(SecurityEvent::DEVICE_BLOCKED))->getSeverity(),
);
self::assertSame(
SecurityEvent::SEVERITY_INFO,
(new SecurityEvent(SecurityEvent::IP_ALLOWED))->getSeverity(),
(new SecurityEvent(SecurityEvent::ACCESS_GRANTED))->getSeverity(),
);
}
+2 -1
View File
@@ -25,6 +25,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 @@ final class CoreModuleTest extends TestCase
FirewallRuleDisabledEvent::class,
FirewallRuleEnabledEvent::class,
FirewallRuleRemovedEvent::class,
SecurityEvent::FIREWALL_SETTINGS_UPDATED,
FirewallSettingsUpdatedEvent::class,
] as $event) {
$listeners = $registry->listeners($event, DeliveryMode::Deferred);
self::assertCount(1, $listeners);
@@ -16,6 +16,9 @@ use KTXC\Security\Event\FirewallRuleDisabledEvent;
use KTXC\Security\Event\FirewallRuleEnabledEvent;
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 PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\MockObject\MockObject;
@@ -301,6 +304,42 @@ class FirewallRuleManagerTest extends TestCase
self::assertSame(FirewallRuleManager::ORIGIN_MANUAL, $audit->get('origin'));
self::assertSame('admin-a', $audit->getIdentityId());
self::assertNotNull($audit->get('expiresAt'));
self::assertInstanceOf(IpBlockedEvent::class, $events[IpBlockedEvent::class]);
self::assertSame('203.0.113.10', $events[IpBlockedEvent::class]->getIpAddress());
}
#[TestDox('IP allowance and device blocking publish dedicated policy events')]
public function testSubjectPolicyEvents(): void
{
$this->store->method('findExactIpRule')->willReturn(null);
$this->store->method('depositRule')->willReturnCallback(
static function (FirewallRuleObject $rule): FirewallRuleObject {
static $sequence = 0;
return $rule->setId('policy-rule-' . ++$sequence);
}
);
$events = [];
$this->events->expects($this->exactly(4))
->method('dispatch')
->willReturnCallback(static function (\KTXF\Event\Event $event) use (&$events): void {
$events[] = $event;
});
$this->manager->allowIp(
FirewallRuleScope::tenant('tenant-a'),
'203.0.113.11',
'Trusted service',
'admin-a',
);
$this->manager->blockDevice(
FirewallRuleScope::tenant('tenant-a'),
'device-a',
'Compromised device',
'admin-a',
);
self::assertCount(1, array_filter($events, static fn($event): bool => $event instanceof IpAllowedEvent));
self::assertCount(1, array_filter($events, static fn($event): bool => $event instanceof DeviceBlockedEvent));
}
#[TestDox('Manual lifecycle changes persist state, reasons, actors, and extension history')]
@@ -16,6 +16,7 @@ use KTXC\Security\Event\AuthenticationSucceededEvent;
use KTXC\Security\Event\BruteForceDetectedEvent;
use KTXC\Security\Event\FirewallRuleCreatedEvent;
use KTXC\Security\Event\FirewallRuleDisabledEvent;
use KTXC\Security\Event\FirewallSettingsUpdatedEvent;
use KTXC\Security\Event\RateLimitExceededEvent;
use KTXC\Security\Event\SuspiciousActivityEvent;
use KTXC\Service\FirewallService;
@@ -396,11 +397,12 @@ class FirewallServiceTest extends TestCase
&& $log->getMetadata()['changeReason'] === 'Tighten controls'
))
->willReturnArgument(0);
$event = new \KTXC\Security\Event\SecurityEvent(
\KTXC\Security\Event\SecurityEvent::FIREWALL_SETTINGS_UPDATED,
['changeReason' => 'Tighten controls'],
$event = new FirewallSettingsUpdatedEvent(
changeReason: 'Tighten controls',
previous: ['maxAuthFailures' => 5],
current: ['maxAuthFailures' => 3],
tenantId: 'tenant-a',
identityId: 'operator',
actorId: 'operator',
);
$this->service->logSecurityEvent($event);
@@ -9,7 +9,7 @@ use KTXC\Models\Tenant\TenantObject;
use KTXC\Service\FirewallSettingsService;
use KTXC\Service\TenantService;
use KTXF\Event\EventDispatcherInterface;
use KTXC\Security\Event\SecurityEvent;
use KTXC\Security\Event\FirewallSettingsUpdatedEvent;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
@@ -37,8 +37,8 @@ final class FirewallSettingsServiceTest extends TestCase
$events = $this->createMock(EventDispatcherInterface::class);
$events->expects(self::once())
->method('dispatch')
->with(self::callback(static fn(SecurityEvent $event): bool =>
$event->getName() === SecurityEvent::FIREWALL_SETTINGS_UPDATED
->with(self::callback(static fn(FirewallSettingsUpdatedEvent $event): bool =>
$event->getName() === FirewallSettingsUpdatedEvent::class
&& $event->getTenantId() === 'tenant-a'
&& $event->getIdentityId() === 'admin-a'
&& $event->get('changeReason') === 'Tighten authentication controls'