fix: register unknown domain when create watchlist. Fixes #69

This commit is contained in:
Maël Gangloff 2025-03-01 18:52:19 +01:00
parent 9c3cda1853
commit c4ef77b374
No known key found for this signature in database
GPG Key ID: 11FDC81C24A7F629
6 changed files with 68 additions and 28 deletions

View File

@ -60,14 +60,6 @@ export default function OvhCloudConnectorForm({form}: {
<Input autoComplete='off'/> <Input autoComplete='off'/>
</Form.Item> </Form.Item>
<Form.Item
label={t`Application key`}
name={['authData', 'appKey']}
rules={[{required: true, message: t`Required`}]}
>
<Input autoComplete='off'/>
</Form.Item>
<Form.Item <Form.Item
label={t`OVH Endpoint`} label={t`OVH Endpoint`}
name={['authData', 'apiEndpoint']} name={['authData', 'apiEndpoint']}

View File

@ -11,17 +11,13 @@ use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Serializer\Exception\ExceptionInterface; use Symfony\Component\Serializer\Exception\ExceptionInterface;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class ConnectorController extends AbstractController class ConnectorController extends AbstractController
{ {
public function __construct( public function __construct(
private readonly SerializerInterface $serializer,
private readonly EntityManagerInterface $em, private readonly EntityManagerInterface $em,
private readonly LoggerInterface $logger, private readonly LoggerInterface $logger,
#[Autowire(service: 'service_container')] #[Autowire(service: 'service_container')]
@ -59,9 +55,8 @@ class ConnectorController extends AbstractController
], ],
methods: ['POST'] methods: ['POST']
)] )]
public function createConnector(Request $request, HttpClientInterface $client): Connector public function createConnector(Connector $connector): Connector
{ {
$connector = $this->serializer->deserialize($request->getContent(), Connector::class, 'json', ['groups' => 'connector:create']);
/** @var User $user */ /** @var User $user */
$user = $this->getUser(); $user = $this->getUser();
$connector->setUser($user); $connector->setUser($user);

View File

@ -26,7 +26,8 @@ class DomainRefreshController extends AbstractController
private readonly RDAPService $RDAPService, private readonly RDAPService $RDAPService,
private readonly RateLimiterFactory $rdapRequestsLimiter, private readonly RateLimiterFactory $rdapRequestsLimiter,
private readonly MessageBusInterface $bus, private readonly MessageBusInterface $bus,
private readonly LoggerInterface $logger, private readonly KernelInterface $kernel, private readonly LoggerInterface $logger,
private readonly KernelInterface $kernel,
) { ) {
} }
@ -38,7 +39,7 @@ class DomainRefreshController extends AbstractController
* @throws HttpExceptionInterface * @throws HttpExceptionInterface
* @throws \Throwable * @throws \Throwable
*/ */
public function __invoke(string $ldhName, KernelInterface $kernel, Request $request): Domain public function __invoke(string $ldhName, Request $request): Domain
{ {
$idnDomain = strtolower(idn_to_ascii($ldhName)); $idnDomain = strtolower(idn_to_ascii($ldhName));
$userId = $this->getUser()->getUserIdentifier(); $userId = $this->getUser()->getUserIdentifier();
@ -64,7 +65,7 @@ class DomainRefreshController extends AbstractController
return $domain; return $domain;
} }
if (false === $kernel->isDebug() && true === $this->getParameter('limited_features')) { if (false === $this->kernel->isDebug() && true === $this->getParameter('limited_features')) {
$limiter = $this->rdapRequestsLimiter->create($userId); $limiter = $this->rdapRequestsLimiter->create($userId);
$limit = $limiter->consume(); $limit = $limiter->consume();

View File

@ -9,9 +9,11 @@ use App\Entity\DomainEvent;
use App\Entity\User; use App\Entity\User;
use App\Entity\WatchList; use App\Entity\WatchList;
use App\Notifier\TestChatNotification; use App\Notifier\TestChatNotification;
use App\Repository\DomainRepository;
use App\Repository\WatchListRepository; use App\Repository\WatchListRepository;
use App\Service\ChatNotificationService; use App\Service\ChatNotificationService;
use App\Service\Connector\AbstractProvider; use App\Service\Connector\AbstractProvider;
use App\Service\RDAPService;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\LockMode; use Doctrine\DBAL\LockMode;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
@ -40,25 +42,45 @@ use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\RateLimiter\RateLimiterFactory;
use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Serializer\Encoder\DecoderInterface;
use Symfony\Component\Serializer\Exception\ExceptionInterface; use Symfony\Component\Serializer\Exception\ExceptionInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\SerializerInterface; use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
class WatchListController extends AbstractController class WatchListController extends AbstractController
{ {
public function __construct( public function __construct(
private readonly SerializerInterface $serializer, private readonly SerializerInterface&DecoderInterface&DenormalizerInterface $serializer,
private readonly EntityManagerInterface $em, private readonly EntityManagerInterface $em,
private readonly WatchListRepository $watchListRepository, private readonly WatchListRepository $watchListRepository,
private readonly LoggerInterface $logger, private readonly LoggerInterface $logger,
private readonly ChatNotificationService $chatNotificationService, private readonly ChatNotificationService $chatNotificationService,
private readonly DomainRepository $domainRepository,
private readonly RDAPService $RDAPService,
private readonly RateLimiterFactory $rdapRequestsLimiter,
private readonly KernelInterface $kernel,
#[Autowire(service: 'service_container')] #[Autowire(service: 'service_container')]
private ContainerInterface $locator, private readonly ContainerInterface $locator,
) { ) {
} }
/** /**
* @throws \Exception * @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws \JsonException
*/ */
#[Route( #[Route(
path: '/api/watchlists', path: '/api/watchlists',
@ -71,8 +93,37 @@ class WatchListController extends AbstractController
)] )]
public function createWatchList(Request $request): WatchList public function createWatchList(Request $request): WatchList
{ {
/** @var WatchList $watchList */
$watchList = $this->serializer->deserialize($request->getContent(), WatchList::class, 'json', ['groups' => 'watchlist:create']); $watchList = $this->serializer->deserialize($request->getContent(), WatchList::class, 'json', ['groups' => 'watchlist:create']);
$data = json_decode($request->getContent(), true, 512, JSON_THROW_ON_ERROR);
if (!is_array($data) || !isset($data['domains']) || !is_array($data['domains'])) {
throw new BadRequestHttpException('Invalid payload: missing or invalid "domains" field.');
}
$domains = array_map(fn (string $d) => str_replace('/api/domains/', '', $d), $data['domains']);
foreach ($domains as $ldhName) {
/** @var ?Domain $domain */
$domain = $this->domainRepository->findOneBy(['ldhName' => $ldhName]);
if (null === $domain) {
$domain = $this->RDAPService->registerDomain($ldhName);
if (false === $this->kernel->isDebug() && true === $this->getParameter('limited_features')) {
$limiter = $this->rdapRequestsLimiter->create($this->getUser()->getUserIdentifier());
$limit = $limiter->consume();
if (!$limit->isAccepted()) {
throw new TooManyRequestsHttpException($limit->getRetryAfter()->getTimestamp() - time());
}
}
}
$watchList->addDomain($domain);
}
/** @var User $user */ /** @var User $user */
$user = $this->getUser(); $user = $this->getUser();
$watchList->setUser($user); $watchList->setUser($user);

View File

@ -104,7 +104,7 @@ class WatchList
#[ORM\JoinTable(name: 'watch_lists_domains', #[ORM\JoinTable(name: 'watch_lists_domains',
joinColumns: [new ORM\JoinColumn(name: 'watch_list_token', referencedColumnName: 'token', onDelete: 'CASCADE')], joinColumns: [new ORM\JoinColumn(name: 'watch_list_token', referencedColumnName: 'token', onDelete: 'CASCADE')],
inverseJoinColumns: [new ORM\JoinColumn(name: 'domain_ldh_name', referencedColumnName: 'ldh_name', onDelete: 'CASCADE')])] inverseJoinColumns: [new ORM\JoinColumn(name: 'domain_ldh_name', referencedColumnName: 'ldh_name', onDelete: 'CASCADE')])]
#[Groups(['watchlist:list', 'watchlist:item', 'watchlist:create'])] #[Groups(['watchlist:list', 'watchlist:item'])]
private Collection $domains; private Collection $domains;
/** /**

View File

@ -150,7 +150,7 @@ readonly class RDAPService
$this->updateDomainStatus($domain, $rdapData); $this->updateDomainStatus($domain, $rdapData);
if (in_array('free', $domain->getStatus())) { if (in_array('free', $domain->getStatus())) {
throw new NotFoundHttpException('The domain name is not present in the WHOIS database.'); throw new NotFoundHttpException("The domain name $idnDomain is not present in the WHOIS database.");
} }
$domain->setRdapServer($rdapServer)->setDelegationSigned(isset($rdapData['secureDNS']['delegationSigned']) && true === $rdapData['secureDNS']['delegationSigned']); $domain->setRdapServer($rdapServer)->setDelegationSigned(isset($rdapData['secureDNS']['delegationSigned']) && true === $rdapData['secureDNS']['delegationSigned']);
@ -170,13 +170,13 @@ readonly class RDAPService
return $domain; return $domain;
} }
private function getTld($domain): Tld private function getTld(string $domain): Tld
{ {
if (!str_contains($domain, '.')) { if (!str_contains($domain, '.')) {
$tldEntity = $this->tldRepository->findOneBy(['tld' => '']); $tldEntity = $this->tldRepository->findOneBy(['tld' => '']);
if (null == $tldEntity) { if (null == $tldEntity) {
throw new NotFoundHttpException('The requested TLD is not yet supported, please try again with another one'); throw new NotFoundHttpException("The requested TLD $domain is not yet supported, please try again with another one");
} }
return $tldEntity; return $tldEntity;
@ -192,7 +192,7 @@ readonly class RDAPService
$tldEntity = $this->tldRepository->findOneBy(['tld' => $tld]); $tldEntity = $this->tldRepository->findOneBy(['tld' => $tld]);
if (null === $tldEntity) { if (null === $tldEntity) {
throw new NotFoundHttpException('The requested TLD is not yet supported, please try again with another one'); throw new NotFoundHttpException("The requested TLD $tld is not yet supported, please try again with another one");
} }
return $tldEntity; return $tldEntity;
@ -205,10 +205,11 @@ readonly class RDAPService
private function fetchRdapServer(Tld $tld): RdapServer private function fetchRdapServer(Tld $tld): RdapServer
{ {
$rdapServer = $this->rdapServerRepository->findOneBy(['tld' => $tld], ['updatedAt' => 'DESC']); $tldString = $tld->getTld();
$rdapServer = $this->rdapServerRepository->findOneBy(['tld' => $tldString], ['updatedAt' => 'DESC']);
if (null === $rdapServer) { if (null === $rdapServer) {
throw new NotFoundHttpException('Unable to determine which RDAP server to contact'); throw new NotFoundHttpException("TLD $tldString : Unable to determine which RDAP server to contact");
} }
return $rdapServer; return $rdapServer;
@ -262,7 +263,7 @@ readonly class RDAPService
$this->em->flush(); $this->em->flush();
} }
throw new NotFoundHttpException('The domain name is not present in the WHOIS database.'); throw new NotFoundHttpException("The domain name $idnDomain is not present in the WHOIS database.");
} }
return $e; return $e;