From e413bfedc0b8362cc778a553ab3288de82863fdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Gangloff?= Date: Sun, 4 Aug 2024 14:45:27 +0200 Subject: [PATCH] feat: add logging info --- src/Controller/ConnectorController.php | 22 +++++++++++++++++-- src/Controller/DomainRefreshController.php | 21 +++++++++++++++--- src/Controller/WatchListController.php | 8 ++++++- .../ProcessDomainTriggerHandler.php | 18 ++++++++++++++- .../ProcessWatchListTriggerHandler.php | 12 +++++++++- src/Service/RDAPService.php | 20 +++++++++++++++++ 6 files changed, 93 insertions(+), 8 deletions(-) diff --git a/src/Controller/ConnectorController.php b/src/Controller/ConnectorController.php index 6d2c3dd..b39374e 100644 --- a/src/Controller/ConnectorController.php +++ b/src/Controller/ConnectorController.php @@ -8,6 +8,7 @@ use App\Entity\User; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\EntityManagerInterface; +use Psr\Log\LoggerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Attribute\Route; @@ -16,7 +17,9 @@ class ConnectorController extends AbstractController { public function __construct( - private readonly SerializerInterface $serializer, private readonly EntityManagerInterface $em + private readonly SerializerInterface $serializer, + private readonly EntityManagerInterface $em, + private readonly LoggerInterface $logger ) { } @@ -56,13 +59,28 @@ public function createConnector(Request $request): Connector $user = $this->getUser(); $connector->setUser($user); - if (ConnectorProvider::OVH === $connector->getProvider()) { + $provider = $connector->getProvider(); + + $this->logger->info('User {username} wants to register a connector from provider {provider}.', [ + 'username' => $user->getUserIdentifier(), + 'provider' => $provider->value, + ]); + + if (ConnectorProvider::OVH === $provider) { $authData = OvhConnector::verifyAuthData($connector->getAuthData()); $connector->setAuthData($authData); + + $this->logger->info('User {username} authentication data with the OVH provider has been validated.', [ + 'username' => $user->getUserIdentifier(), + ]); } else { throw new \Exception('Unknown provider'); } + $this->logger->info('The new API connector requested by {username} has been successfully registered.', [ + 'username' => $user->getUserIdentifier(), + ]); + $this->em->persist($connector); $this->em->flush(); diff --git a/src/Controller/DomainRefreshController.php b/src/Controller/DomainRefreshController.php index 7105fa9..07542a4 100644 --- a/src/Controller/DomainRefreshController.php +++ b/src/Controller/DomainRefreshController.php @@ -7,6 +7,7 @@ use App\Message\ProcessDomainTrigger; use App\Repository\DomainRepository; use App\Service\RDAPService; +use Psr\Log\LoggerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException; use Symfony\Component\HttpKernel\KernelInterface; @@ -22,8 +23,9 @@ class DomainRefreshController extends AbstractController public function __construct(private readonly DomainRepository $domainRepository, private readonly RDAPService $RDAPService, private readonly RateLimiterFactory $authenticatedApiLimiter, - private readonly MessageBusInterface $bus) - { + private readonly MessageBusInterface $bus, + private readonly LoggerInterface $logger + ) { } /** @@ -36,6 +38,12 @@ public function __construct(private readonly DomainRepository $domainRepository, public function __invoke(string $ldhName, KernelInterface $kernel): ?Domain { $idnDomain = strtolower(idn_to_ascii($ldhName)); + $userId = $this->getUser()->getUserIdentifier(); + + $this->logger->info('User {username} wants to update the domain name {idnDomain}.', [ + 'username' => $userId, + 'idnDomain' => $idnDomain, + ]); /** @var ?Domain $domain */ $domain = $this->domainRepository->findOneBy(['ldhName' => $idnDomain]); @@ -46,12 +54,19 @@ public function __invoke(string $ldhName, KernelInterface $kernel): ?Domain && ($domain->getUpdatedAt()->diff(new \DateTimeImmutable('now'))->days < 7) && !$this->RDAPService::isToBeWatchClosely($domain, $domain->getUpdatedAt()) ) { + $this->logger->info('It is not necessary to update the information of the domain name {idnDomain} with the RDAP protocol.', [ + 'idnDomain' => $idnDomain, + ]); + return $domain; } if (false === $kernel->isDebug()) { - $limiter = $this->authenticatedApiLimiter->create($this->getUser()->getUserIdentifier()); + $limiter = $this->authenticatedApiLimiter->create($userId); if (false === $limiter->consume()->isAccepted()) { + $this->logger->warning('User {username} was rate limited by the API.', [ + 'username' => $this->getUser()->getUserIdentifier(), + ]); throw new TooManyRequestsHttpException(); } } diff --git a/src/Controller/WatchListController.php b/src/Controller/WatchListController.php index ca1211a..4bbbb1a 100644 --- a/src/Controller/WatchListController.php +++ b/src/Controller/WatchListController.php @@ -22,6 +22,7 @@ use Eluceo\iCal\Presentation\Component\Property; use Eluceo\iCal\Presentation\Component\Property\Value\TextValue; use Eluceo\iCal\Presentation\Factory\CalendarFactory; +use Psr\Log\LoggerInterface; use Sabre\VObject\EofException; use Sabre\VObject\InvalidDataException; use Sabre\VObject\ParseException; @@ -37,7 +38,8 @@ class WatchListController extends AbstractController public function __construct( private readonly SerializerInterface $serializer, private readonly EntityManagerInterface $em, - private readonly WatchListRepository $watchListRepository + private readonly WatchListRepository $watchListRepository, + private readonly LoggerInterface $logger ) { } @@ -60,6 +62,10 @@ public function createWatchList(Request $request): WatchList $user = $this->getUser(); $watchList->setUser($user); + $this->logger->info('User {username} register a Watchlist.', [ + 'username' => $user->getUserIdentifier(), + ]); + $this->em->persist($watchList); $this->em->flush(); diff --git a/src/MessageHandler/ProcessDomainTriggerHandler.php b/src/MessageHandler/ProcessDomainTriggerHandler.php index eebf90f..b4a3db6 100644 --- a/src/MessageHandler/ProcessDomainTriggerHandler.php +++ b/src/MessageHandler/ProcessDomainTriggerHandler.php @@ -14,6 +14,7 @@ use App\Message\ProcessDomainTrigger; use App\Repository\DomainRepository; use App\Repository\WatchListRepository; +use Psr\Log\LoggerInterface; use Symfony\Bridge\Twig\Mime\TemplatedEmail; use Symfony\Component\HttpKernel\KernelInterface; use Symfony\Component\Mailer\Exception\TransportExceptionInterface; @@ -29,7 +30,8 @@ public function __construct( private MailerInterface $mailer, private WatchListRepository $watchListRepository, private DomainRepository $domainRepository, - private KernelInterface $kernel + private KernelInterface $kernel, + private LoggerInterface $logger ) { } @@ -46,6 +48,12 @@ public function __invoke(ProcessDomainTrigger $message): void $connector = $watchList->getConnector(); if (null !== $connector && $domain->getDeleted()) { + $this->logger->notice('Watchlist {watchlist} is linked to connector {connector}. A purchase attempt will be made for domain name {ldhName} with provider {provider}.', [ + 'watchlist' => $message->watchListToken, + 'connector' => $connector->getId(), + 'ldhName' => $message->ldhName, + 'provider' => $connector->getProvider()->value, + ]); try { if (ConnectorProvider::OVH === $connector->getProvider()) { $ovh = new OvhConnector($connector->getAuthData()); @@ -57,6 +65,9 @@ public function __invoke(ProcessDomainTrigger $message): void throw new \Exception('Unknown provider'); } } catch (\Throwable) { + $this->logger->warning('Unable to complete purchase. An error message is sent to user {username}.', [ + 'username' => $watchList->getUser()->getUserIdentifier(), + ]); $this->sendEmailDomainOrderError($domain, $watchList->getUser()); } } @@ -68,6 +79,11 @@ public function __invoke(ProcessDomainTrigger $message): void /** @var WatchListTrigger $watchListTrigger */ foreach ($watchListTriggers->getIterator() as $watchListTrigger) { + $this->logger->info('Action {event} has been detected on the domain name {ldhName}. A notification is sent to user {username}.', [ + 'event' => $event->getAction(), + 'ldhName' => $message->ldhName, + 'username' => $watchList->getUser()->getUserIdentifier(), + ]); if (TriggerAction::SendEmail == $watchListTrigger->getAction()) { $this->sendEmailDomainUpdated($event, $watchList->getUser()); } diff --git a/src/MessageHandler/ProcessWatchListTriggerHandler.php b/src/MessageHandler/ProcessWatchListTriggerHandler.php index b0e0f32..959f727 100644 --- a/src/MessageHandler/ProcessWatchListTriggerHandler.php +++ b/src/MessageHandler/ProcessWatchListTriggerHandler.php @@ -9,6 +9,7 @@ use App\Message\ProcessWatchListTrigger; use App\Repository\WatchListRepository; use App\Service\RDAPService; +use Psr\Log\LoggerInterface; use Symfony\Bridge\Twig\Mime\TemplatedEmail; use Symfony\Component\Mailer\Exception\TransportExceptionInterface; use Symfony\Component\Mailer\MailerInterface; @@ -25,7 +26,8 @@ public function __construct( private MailerInterface $mailer, private string $mailerSenderEmail, private MessageBusInterface $bus, - private WatchListRepository $watchListRepository + private WatchListRepository $watchListRepository, + private LoggerInterface $logger ) { } @@ -38,6 +40,11 @@ public function __invoke(ProcessWatchListTrigger $message): void { /** @var WatchList $watchList */ $watchList = $this->watchListRepository->findOneBy(['token' => $message->watchListToken]); + + $this->logger->info('Domain names from Watchlist {token} will be processed.', [ + 'token' => $message->watchListToken, + ]); + /** @var Domain $domain */ foreach ($watchList->getDomains() ->filter(fn ($domain) => $domain->getUpdatedAt() @@ -54,6 +61,9 @@ public function __invoke(ProcessWatchListTrigger $message): void if (!($e instanceof HttpExceptionInterface)) { continue; } + $this->logger->notice('An update error email is sent to user {username}.', [ + 'username' => $watchList->getUser()->getUserIdentifier(), + ]); $this->sendEmailDomainUpdateError($domain, $watchList->getUser()); } diff --git a/src/Service/RDAPService.php b/src/Service/RDAPService.php index a677fbb..92e7d5e 100644 --- a/src/Service/RDAPService.php +++ b/src/Service/RDAPService.php @@ -184,9 +184,18 @@ public function registerDomain(string $fqdn): Domain if (array_key_exists('status', $res)) { $domain->setStatus($res['status']); + } else { + $this->logger->warning('The domain name {idnDomain} has no WHOIS status.', [ + 'idnDomain' => $idnDomain, + ]); } + if (array_key_exists('handle', $res)) { $domain->setHandle($res['handle']); + } else { + $this->logger->warning('The domain name {idnDomain} has no handle key.', [ + 'idnDomain' => $idnDomain, + ]); } $this->em->persist($domain); @@ -239,6 +248,9 @@ public function registerDomain(string $fqdn): Domain ) ); + /* + * Flatten the array + */ if (count($roles) !== count($roles, COUNT_RECURSIVE)) { $roles = array_merge(...$roles); } @@ -305,6 +317,10 @@ public function registerDomain(string $fqdn): Domain $domain->addNameserver($nameserver); } + } else { + $this->logger->warning('The domain name {idnDomain} has no nameservers.', [ + 'idnDomain' => $idnDomain, + ]); } $domain->updateTimestamps(); @@ -339,6 +355,10 @@ private function registerEntity(array $rdapEntity): Entity if (null === $entity) { $entity = new Entity(); + } else { + $this->logger->info('The entity {handle} was not known to this Domain Watchdog instance.', [ + 'handle' => $rdapEntity['handle'], + ]); } $entity->setHandle($rdapEntity['handle']);