84eb0e2c21
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
159 lines
4.1 KiB
PHP
159 lines
4.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KTXC\Security\Event;
|
|
|
|
use KTXC\Models\Firewall\FirewallRuleObject;
|
|
use KTXF\Event\Event;
|
|
|
|
abstract class FirewallRuleEvent extends Event implements SecurityEventInterface
|
|
{
|
|
final protected function __construct(
|
|
private readonly string $ruleId,
|
|
private readonly string $ruleScope,
|
|
private readonly string $ruleType,
|
|
private readonly string $ruleAction,
|
|
private readonly string $ruleValue,
|
|
private readonly ?string $reason,
|
|
private readonly string $origin,
|
|
private readonly ?string $expiresAt,
|
|
private readonly array $details,
|
|
?string $tenantId,
|
|
?string $identityId,
|
|
) {
|
|
if ($ruleId === '') {
|
|
throw new \InvalidArgumentException('Firewall rule events require a rule ID.');
|
|
}
|
|
foreach ([
|
|
'scope' => $ruleScope,
|
|
'type' => $ruleType,
|
|
'action' => $ruleAction,
|
|
'value' => $ruleValue,
|
|
'origin' => $origin,
|
|
] as $field => $value) {
|
|
if ($value === '') {
|
|
throw new \InvalidArgumentException("Firewall rule events require a rule {$field}.");
|
|
}
|
|
}
|
|
foreach ([
|
|
'scope' => $ruleScope,
|
|
'type' => $ruleType,
|
|
'action' => $ruleAction,
|
|
'value' => $ruleValue,
|
|
'origin' => $origin,
|
|
] as $field => $value) {
|
|
if ($value === '') {
|
|
throw new \InvalidArgumentException("Firewall rule events require a rule {$field}.");
|
|
}
|
|
}
|
|
|
|
parent::__construct(
|
|
static::class,
|
|
[
|
|
'ruleId' => $ruleId,
|
|
'ruleScope' => $ruleScope,
|
|
'ruleType' => $ruleType,
|
|
'ruleAction' => $ruleAction,
|
|
'ruleValue' => $ruleValue,
|
|
'reason' => $reason,
|
|
'origin' => $origin,
|
|
'expiresAt' => $expiresAt,
|
|
...$details,
|
|
],
|
|
$tenantId,
|
|
$identityId,
|
|
);
|
|
}
|
|
|
|
public static function fromRule(
|
|
FirewallRuleObject $rule,
|
|
?string $actorId = null,
|
|
array $change = [],
|
|
): static {
|
|
$metadata = $rule->getMetadata() ?? [];
|
|
$details = [...$metadata, ...$change];
|
|
foreach ([
|
|
'ruleId',
|
|
'ruleScope',
|
|
'ruleType',
|
|
'ruleAction',
|
|
'ruleValue',
|
|
'reason',
|
|
'origin',
|
|
'expiresAt',
|
|
] as $reservedKey) {
|
|
unset($details[$reservedKey]);
|
|
}
|
|
|
|
return new static(
|
|
ruleId: (string) $rule->getId(),
|
|
ruleScope: (string) $rule->getScope(),
|
|
ruleType: (string) $rule->getType(),
|
|
ruleAction: (string) $rule->getAction(),
|
|
ruleValue: (string) $rule->getValue(),
|
|
reason: $rule->getReason(),
|
|
origin: (string) ($metadata['origin'] ?? 'manual'),
|
|
expiresAt: $rule->getExpiresAt()?->format(\DateTimeInterface::ATOM),
|
|
details: $details,
|
|
tenantId: $rule->getTenantId(),
|
|
identityId: $actorId ?? $rule->getCreatedBy(),
|
|
);
|
|
}
|
|
|
|
public function getRuleId(): string
|
|
{
|
|
return $this->ruleId;
|
|
}
|
|
|
|
public function getRuleScope(): string
|
|
{
|
|
return $this->ruleScope;
|
|
}
|
|
|
|
public function getRuleType(): string
|
|
{
|
|
return $this->ruleType;
|
|
}
|
|
|
|
public function getRuleAction(): string
|
|
{
|
|
return $this->ruleAction;
|
|
}
|
|
|
|
public function getRuleValue(): string
|
|
{
|
|
return $this->ruleValue;
|
|
}
|
|
|
|
public function getOrigin(): string
|
|
{
|
|
return $this->origin;
|
|
}
|
|
|
|
public function getExpiresAt(): ?string
|
|
{
|
|
return $this->expiresAt;
|
|
}
|
|
|
|
public function getDetails(): array
|
|
{
|
|
return $this->details;
|
|
}
|
|
|
|
public function getUserId(): ?string
|
|
{
|
|
return null;
|
|
}
|
|
|
|
public function getReason(): ?string
|
|
{
|
|
return $this->reason;
|
|
}
|
|
|
|
public function getSeverity(): SecurityEventSeverity
|
|
{
|
|
return SecurityEventSeverity::INFO;
|
|
}
|
|
}
|