Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audit Log IP addresses #93

Open
wants to merge 8 commits into
base: v1.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,18 @@ public function getConfigTreeBuilder(): TreeBuilder
->end()
->children()
->booleanNode('blame_impersonator')
->defaultFalse()
->defaultFalse()
->end()
->end()
->children()
->booleanNode('log_user_ip')
->defaultFalse()
->end()
->booleanNode('log_user_agent')
->defaultFalse()
->end()
->end()

;
// @formatter:on

Expand Down
7 changes: 7 additions & 0 deletions src/DependencyInjection/DataDogAuditExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,12 @@ public function load(array $configs, ContainerBuilder $container): void
if (isset($config['blame_impersonator'])) {
$auditSubscriber->addMethodCall('setBlameImpersonator', array($config['blame_impersonator']));
}

if (isset($config['log_user_ip'])) {
$auditSubscriber->addMethodCall('setLogIp', array($config['log_user_ip']));
}
if (isset($config['log_user_agent'])) {
$auditSubscriber->addMethodCall('setLogUserAgent', array($config['log_user_agent']));
}
}
}
14 changes: 14 additions & 0 deletions src/Entity/AuditLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ class AuditLog

private string $action;

private ?string $ip;

private ?string $userAgent;

private string $tbl;

private Association $source;
Expand Down Expand Up @@ -59,4 +63,14 @@ public function getLoggedAt(): \DateTimeInterface
{
return $this->loggedAt;
}

public function getIp(): ?string
{
return $this->ip;
}

public function getUserAgent(): ?string
{
return $this->userAgent;
}
}
30 changes: 29 additions & 1 deletion src/EventSubscriber/AuditSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Doctrine\ORM\Event\OnFlushEventArgs;
use Doctrine\ORM\Events;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Role\SwitchUserRole;
Expand Down Expand Up @@ -52,9 +53,19 @@ class AuditSubscriber implements EventSubscriber

protected TokenStorageInterface $securityTokenStorage;

public function __construct(TokenStorageInterface $securityTokenStorage)
private RequestStack $requestStack;

private bool $logIp = false;

private bool $logUserAgent = false;

public function __construct(
TokenStorageInterface $securityTokenStorage,
RequestStack $requestStack
)
{
$this->securityTokenStorage = $securityTokenStorage;
$this->requestStack = $requestStack;
}

public function setLabeler(?callable $labeler = null): self
Expand Down Expand Up @@ -345,6 +356,13 @@ protected function audit(EntityManager $em, array $data)
// audit association explicitly sets that.
$data[$field] = $meta->idGenerator->generate($em, null);
}
// Log the ip address.
$mainRequest = $this->requestStack->getMasterRequest();
// use this instead when support for symfony <5.3 dropped
acornforth marked this conversation as resolved.
Show resolved Hide resolved
// $mainRequest = $this->requestStack->getMainRequest();

$data['ip'] = $this->logIp && $mainRequest ? $mainRequest->getClientIp() : null;
$data['userAgent'] = $this->logUserAgent && $mainRequest ? substr($mainRequest->headers->get('User-Agent'), 0, 255) : null;
acornforth marked this conversation as resolved.
Show resolved Hide resolved

$meta = $em->getClassMetadata(AuditLog::class);
$data['loggedAt'] = new \DateTime();
Expand Down Expand Up @@ -534,4 +552,14 @@ public function setBlameUser(UserInterface $user)
{
$this->blameUser = $user;
}

public function setLogIp(bool $logIp): void
{
$this->logIp = $logIp;
}

public function setLogUserAgent(bool $logUserAgent): void
{
$this->logUserAgent = $logUserAgent;
}
}
2 changes: 2 additions & 0 deletions src/Resources/config/doctrine/AuditLog.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
<id name="id" type="bigint" column="id">
<generator strategy="AUTO" />
</id>
<field name="ip" nullable="true" type="string" length="32"/>
acornforth marked this conversation as resolved.
Show resolved Hide resolved
<field name="userAgent" nullable="true" type="string" length="255"/>
acornforth marked this conversation as resolved.
Show resolved Hide resolved
<field name="action" length="12" />
<field name="tbl" length="128" />
<one-to-one field="source" target-entity="Association">
Expand Down
2 changes: 2 additions & 0 deletions src/Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DataDog\AuditBundle\EventSubscriber\AuditSubscriber;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

return static function (ContainerConfigurator $container) {
Expand All @@ -12,6 +13,7 @@
$services
->set('datadog.event_subscriber.audit', AuditSubscriber::class)->private()
->arg(0, new Reference(TokenStorageInterface::class))
->arg(1, new Reference(RequestStack::class))
->tag('doctrine.event_subscriber')
;
// @formatter:on
Expand Down