Files
domain-watchdog/src/Service/RDAPService.php

720 lines
26 KiB
PHP
Raw Normal View History

2024-07-13 23:57:07 +02:00
<?php
namespace App\Service;
2025-08-26 16:18:29 +02:00
use App\Config\DnsKey\Algorithm;
use App\Config\DnsKey\DigestType;
2024-07-13 23:57:07 +02:00
use App\Config\EventAction;
2025-08-26 16:18:29 +02:00
use App\Entity\DnsKey;
2024-07-13 23:57:07 +02:00
use App\Entity\Domain;
use App\Entity\DomainEntity;
use App\Entity\DomainEvent;
use App\Entity\DomainStatus;
2024-07-13 23:57:07 +02:00
use App\Entity\Entity;
use App\Entity\EntityEvent;
use App\Entity\Nameserver;
use App\Entity\NameserverEntity;
2024-07-18 19:13:06 +02:00
use App\Entity\RdapServer;
2024-07-19 18:59:21 +02:00
use App\Entity\Tld;
2025-10-07 15:55:17 +02:00
use App\Exception\DomainNotFoundException;
use App\Exception\MalformedDomainException;
use App\Exception\TldNotSupportedException;
use App\Exception\UnknownRdapServerException;
2024-07-13 23:57:07 +02:00
use App\Repository\DomainEntityRepository;
use App\Repository\DomainEventRepository;
use App\Repository\DomainRepository;
use App\Repository\EntityEventRepository;
use App\Repository\EntityRepository;
use App\Repository\IcannAccreditationRepository;
2024-07-13 23:57:07 +02:00
use App\Repository\NameserverEntityRepository;
use App\Repository\NameserverRepository;
2024-07-18 19:13:06 +02:00
use App\Repository\RdapServerRepository;
2024-07-19 18:59:21 +02:00
use App\Repository\TldRepository;
use Doctrine\DBAL\LockMode;
2024-07-13 23:57:07 +02:00
use Doctrine\ORM\EntityManagerInterface;
2025-10-07 15:55:17 +02:00
use Doctrine\ORM\OptimisticLockException;
2024-08-04 04:39:05 +02:00
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
2024-08-07 16:21:41 +02:00
use Symfony\Component\HttpClient\Exception\ClientException;
use Symfony\Component\HttpFoundation\Response;
2024-07-19 01:17:33 +02:00
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;
2024-07-13 23:57:07 +02:00
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
2024-07-13 23:57:07 +02:00
2025-09-06 10:35:07 +02:00
class RDAPService
2024-07-13 23:57:07 +02:00
{
public const ENTITY_HANDLE_BLACKLIST = [
'REDACTED_FOR_PRIVACY',
'ANO00-FRNIC',
'not applicable',
2025-08-11 01:18:43 +02:00
'REDACTED FOR PRIVACY-REGISTRANT',
2025-08-11 14:15:43 +02:00
'REDACTED FOR PRIVACY-TECH',
2025-08-11 01:18:43 +02:00
'REDACTED FOR PRIVACY',
2025-08-11 14:15:43 +02:00
'REDACTED-SIDN',
2025-08-11 01:18:43 +02:00
'REGISTRANT',
'REGISTRAR',
2025-08-11 14:15:43 +02:00
'ABUSE-CONTACT',
2025-08-11 01:18:43 +02:00
'None',
2025-08-11 14:15:43 +02:00
'Private',
];
2025-10-14 23:34:17 +02:00
public function __construct(
private readonly HttpClientInterface $client,
private readonly EntityRepository $entityRepository,
private readonly DomainRepository $domainRepository,
private readonly DomainEventRepository $domainEventRepository,
private readonly NameserverRepository $nameserverRepository,
private readonly NameserverEntityRepository $nameserverEntityRepository,
private readonly EntityEventRepository $entityEventRepository,
private readonly DomainEntityRepository $domainEntityRepository,
private readonly RdapServerRepository $rdapServerRepository,
private readonly TldRepository $tldRepository,
private readonly IcannAccreditationRepository $icannAccreditationRepository,
private readonly EntityManagerInterface $em,
private readonly LoggerInterface $logger,
private readonly StatService $statService,
private readonly InfluxdbService $influxService,
#[Autowire(param: 'influxdb_enabled')]
private readonly bool $influxdbEnabled,
2024-08-02 23:24:52 +02:00
) {
2024-07-13 23:57:07 +02:00
}
2024-07-14 11:20:04 +02:00
/**
2025-10-07 15:55:17 +02:00
* @throws RedirectionExceptionInterface
* @throws DomainNotFoundException
2024-07-25 16:19:57 +02:00
* @throws DecodingExceptionInterface
2025-10-07 15:55:17 +02:00
* @throws TldNotSupportedException
* @throws ClientExceptionInterface
* @throws OptimisticLockException
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws MalformedDomainException
* @throws UnknownRdapServerException
2024-07-14 11:20:04 +02:00
*/
public function registerDomains(array $domains): void
{
foreach ($domains as $fqdn) {
2024-07-18 19:13:06 +02:00
$this->registerDomain($fqdn);
}
}
/**
2025-10-07 15:55:17 +02:00
* @throws DomainNotFoundException
2024-08-07 16:21:41 +02:00
* @throws RedirectionExceptionInterface
2024-07-25 16:19:57 +02:00
* @throws DecodingExceptionInterface
2025-10-07 15:55:17 +02:00
* @throws TldNotSupportedException
2024-08-07 16:21:41 +02:00
* @throws ClientExceptionInterface
2025-10-07 15:55:17 +02:00
* @throws OptimisticLockException
* @throws TransportExceptionInterface
* @throws MalformedDomainException
* @throws ServerExceptionInterface
* @throws UnknownRdapServerException
2024-12-07 20:08:29 +01:00
* @throws \Exception
*/
public function registerDomain(string $fqdn): Domain
2024-07-13 23:57:07 +02:00
{
2025-05-21 13:14:38 +02:00
$idnDomain = RDAPService::convertToIdn($fqdn);
2024-07-19 18:59:21 +02:00
$tld = $this->getTld($idnDomain);
2024-07-18 19:13:06 +02:00
$this->logger->debug('Update request for a domain name is requested', [
2025-10-03 18:14:43 +02:00
'ldhName' => $idnDomain,
2024-08-04 04:39:05 +02:00
]);
2024-12-20 22:25:41 +01:00
$rdapServer = $this->fetchRdapServer($tld);
$domain = $this->domainRepository->findOneBy(['ldhName' => $idnDomain]);
$rdapData = $this->fetchRdapResponse($rdapServer, $idnDomain, $domain);
$this->em->beginTransaction();
2024-12-20 22:25:41 +01:00
if (null === $domain) {
$domain = $this->initNewDomain($idnDomain, $tld);
$this->em->persist($domain);
2024-12-20 22:25:41 +01:00
}
$this->em->lock($domain, LockMode::PESSIMISTIC_WRITE);
2024-12-20 22:25:41 +01:00
$this->updateDomainStatus($domain, $rdapData);
2024-12-22 23:06:52 +01:00
if (in_array('free', $domain->getStatus())) {
2025-10-07 15:55:17 +02:00
throw DomainNotFoundException::fromDomain($idnDomain);
2024-12-22 23:06:52 +01:00
}
$domain
->setRdapServer($rdapServer)
->setDelegationSigned(isset($rdapData['secureDNS']['delegationSigned']) && $rdapData['secureDNS']['delegationSigned']);
2024-12-20 22:25:41 +01:00
$this->updateDomainHandle($domain, $rdapData);
$this->updateDomainEvents($domain, $rdapData);
$this->updateDomainEntities($domain, $rdapData);
$this->updateDomainNameservers($domain, $rdapData);
2025-08-26 16:18:29 +02:00
$this->updateDomainDsData($domain, $rdapData);
2024-12-20 22:25:41 +01:00
2025-01-29 20:43:26 +01:00
$domain->setDeleted(false)->updateTimestamps();
2024-12-21 21:41:33 +01:00
2024-12-20 22:25:41 +01:00
$this->em->flush();
$this->em->commit();
2024-12-20 22:25:41 +01:00
return $domain;
}
2025-10-07 15:55:17 +02:00
/**
* @throws TldNotSupportedException
* @throws MalformedDomainException
*/
2025-05-19 09:47:21 +02:00
public function getTld(string $domain): Tld
2024-12-20 22:25:41 +01:00
{
if (!str_contains($domain, OfficialDataService::DOMAIN_DOT)) {
$tldEntity = $this->tldRepository->findOneBy(['tld' => OfficialDataService::DOMAIN_DOT]);
2025-02-09 01:12:17 +01:00
if (null == $tldEntity) {
throw TldNotSupportedException::fromTld(OfficialDataService::DOMAIN_DOT);
2025-02-09 01:12:17 +01:00
}
return $tldEntity;
2024-12-20 22:25:41 +01:00
}
2025-02-09 01:12:17 +01:00
$lastDotPosition = strrpos($domain, OfficialDataService::DOMAIN_DOT);
2025-02-09 01:12:17 +01:00
2024-12-20 22:25:41 +01:00
if (false === $lastDotPosition) {
2025-10-07 15:55:17 +02:00
throw MalformedDomainException::fromDomain($domain);
2024-12-20 22:25:41 +01:00
}
2025-09-10 00:11:05 +02:00
$tld = self::convertToIdn(substr($domain, $lastDotPosition + 1));
$tldEntity = $this->tldRepository->findOneBy(['tld' => $tld, 'deletedAt' => null]);
2025-02-09 01:12:17 +01:00
if (null === $tldEntity) {
$this->logger->debug('Domain name cannot be updated because the TLD is not supported', [
'ldhName' => $domain,
]);
throw TldNotSupportedException::fromTld($tld);
}
2024-12-20 22:25:41 +01:00
return $tldEntity;
2024-12-20 22:25:41 +01:00
}
2025-10-16 14:16:58 +02:00
/**
* @throws MalformedDomainException
*/
2025-05-21 13:14:38 +02:00
public static function convertToIdn(string $fqdn): string
2024-12-20 22:25:41 +01:00
{
2025-10-16 14:16:58 +02:00
$ascii = strtolower(idn_to_ascii($fqdn));
if (OfficialDataService::DOMAIN_DOT !== $fqdn && !preg_match('/^(xn--)?[a-z0-9-]+(\.[a-z0-9-]+)*$/', $ascii)) {
throw MalformedDomainException::fromDomain($fqdn);
}
return $ascii;
2024-12-20 22:25:41 +01:00
}
2025-10-07 15:55:17 +02:00
/**
* @throws UnknownRdapServerException
*/
2024-12-20 22:25:41 +01:00
private function fetchRdapServer(Tld $tld): RdapServer
{
$tldString = $tld->getTld();
$rdapServer = $this->rdapServerRepository->findOneBy(['tld' => $tldString], ['updatedAt' => 'DESC']);
2024-07-18 19:13:06 +02:00
2024-08-02 23:24:52 +02:00
if (null === $rdapServer) {
$this->logger->debug('Unable to determine which RDAP server to contact', [
'tld' => $tldString,
]);
2025-10-07 15:55:17 +02:00
throw UnknownRdapServerException::fromTld($tldString);
2024-08-02 23:24:52 +02:00
}
2024-07-13 23:57:07 +02:00
2024-12-20 22:25:41 +01:00
return $rdapServer;
}
2024-07-25 16:19:57 +02:00
2024-12-20 22:25:41 +01:00
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws \Exception
*/
private function fetchRdapResponse(RdapServer $rdapServer, string $idnDomain, ?Domain $domain): array
{
2024-08-04 04:39:05 +02:00
$rdapServerUrl = $rdapServer->getUrl();
$this->logger->info('An RDAP query to update this domain name will be made', [
2025-10-03 18:14:43 +02:00
'ldhName' => $idnDomain,
2024-08-04 04:39:05 +02:00
'server' => $rdapServerUrl,
]);
2024-07-14 11:20:04 +02:00
try {
2024-12-20 22:25:41 +01:00
$req = $this->client->request('GET', $rdapServerUrl.'domain/'.$idnDomain);
$this->statService->incrementStat('stats.rdap_queries.count');
2024-08-07 16:29:37 +02:00
2024-12-20 22:25:41 +01:00
return $req->toArray();
} catch (\Exception $e) {
throw $this->handleRdapException($e, $idnDomain, $domain, $req ?? null);
} finally {
if ($this->influxdbEnabled && isset($req)) {
2024-12-08 14:19:54 +01:00
$this->influxService->addRdapQueryPoint($rdapServer, $idnDomain, $req->getInfo());
}
2024-07-14 11:20:04 +02:00
}
2024-12-20 22:25:41 +01:00
}
2024-07-13 23:57:07 +02:00
2024-12-20 22:25:41 +01:00
/**
* @throws TransportExceptionInterface
* @throws \Exception
*/
private function handleRdapException(\Exception $e, string $idnDomain, ?Domain $domain, ?ResponseInterface $response): \Exception
2024-12-20 22:25:41 +01:00
{
if (
($e instanceof ClientException && Response::HTTP_NOT_FOUND === $e->getResponse()->getStatusCode())
|| ($e instanceof TransportExceptionInterface && null !== $response && !in_array('content-length', $response->getHeaders(false)) && 404 === $response->getStatusCode())
) {
2024-12-20 22:25:41 +01:00
if (null !== $domain) {
$this->logger->info('Domain name has been deleted from the WHOIS database', [
2025-10-03 18:14:43 +02:00
'ldhName' => $idnDomain,
2024-12-20 22:25:41 +01:00
]);
2024-08-04 04:39:05 +02:00
$domain->updateTimestamps();
if (!$domain->getDeleted() && $domain->getUpdatedAt() !== $domain->getCreatedAt()) {
$this->em->persist((new DomainStatus())
->setDomain($domain)
->setCreatedAt($domain->getUpdatedAt())
->setDate($domain->getUpdatedAt())
->setAddStatus([])
->setDeleteStatus($domain->getStatus()));
}
$domain->setDeleted(true);
2024-12-20 22:25:41 +01:00
$this->em->persist($domain);
$this->em->flush();
}
2025-10-07 15:55:17 +02:00
throw DomainNotFoundException::fromDomain($idnDomain);
2024-08-02 23:24:52 +02:00
}
2024-08-04 04:39:05 +02:00
$this->logger->error('Unable to perform an RDAP query for this domain name', [
'ldhName' => $idnDomain,
]);
2024-12-20 22:25:41 +01:00
return $e;
}
private function initNewDomain(string $idnDomain, Tld $tld): Domain
{
$domain = new Domain();
$this->logger->debug('Domain name was not known to this instance', [
2025-10-03 18:14:43 +02:00
'ldhName' => $idnDomain,
2024-12-20 22:25:41 +01:00
]);
2024-07-13 23:57:07 +02:00
2024-12-21 21:41:33 +01:00
return $domain->setTld($tld)->setLdhName($idnDomain)->setDeleted(false);
2024-12-20 22:25:41 +01:00
}
private function updateDomainStatus(Domain $domain, array $rdapData): void
{
if (isset($rdapData['status']) && is_array($rdapData['status'])) {
2025-10-19 14:21:52 +02:00
$status = array_map(fn ($s) => strtolower($s), array_unique($rdapData['status']));
$addedStatus = array_diff($status, $domain->getStatus());
$deletedStatus = array_diff($domain->getStatus(), $status);
$domain->setStatus($status);
if (count($addedStatus) > 0 || count($deletedStatus) > 0) {
$this->em->persist($domain);
if ($domain->getUpdatedAt() !== $domain->getCreatedAt()) {
$this->em->persist((new DomainStatus())
->setDomain($domain)
->setCreatedAt(new \DateTimeImmutable('now'))
->setDate($domain->getUpdatedAt())
->setAddStatus($addedStatus)
->setDeleteStatus($deletedStatus));
}
}
2024-08-04 14:45:27 +02:00
} else {
2025-10-03 18:14:43 +02:00
$this->logger->warning('Domain name has no WHOIS status', [
'ldhName' => $domain->getLdhName(),
2024-08-04 14:45:27 +02:00
]);
2024-08-02 23:24:52 +02:00
}
2024-12-20 22:25:41 +01:00
}
2024-08-04 14:45:27 +02:00
2024-12-20 22:25:41 +01:00
private function updateDomainHandle(Domain $domain, array $rdapData): void
{
if (isset($rdapData['handle'])) {
2024-12-20 22:25:41 +01:00
$domain->setHandle($rdapData['handle']);
2024-08-04 14:45:27 +02:00
} else {
2025-10-03 18:14:43 +02:00
$this->logger->warning('Domain name has no handle key', [
'ldhName' => $domain->getLdhName(),
2024-08-04 14:45:27 +02:00
]);
2024-08-02 23:24:52 +02:00
}
2024-12-20 22:25:41 +01:00
}
2024-12-20 22:25:41 +01:00
/**
* @throws \Exception
*/
private function updateDomainEvents(Domain $domain, array $rdapData): void
{
2025-10-21 12:52:43 +02:00
$this->domainEventRepository->setDomainEventAsDeleted($domain);
2024-09-01 21:26:07 +02:00
if (isset($rdapData['events']) && is_array($rdapData['events'])) {
2024-12-20 22:25:41 +01:00
foreach ($rdapData['events'] as $rdapEvent) {
if ($rdapEvent['eventAction'] === EventAction::LastUpdateOfRDAPDatabase->value) {
continue;
}
2024-09-22 19:49:35 +02:00
$event = $this->domainEventRepository->findOneBy([
'action' => $rdapEvent['eventAction'],
'date' => new \DateTimeImmutable($rdapEvent['eventDate']),
'domain' => $domain,
]);
2024-09-22 19:49:35 +02:00
if (null === $event) {
$event = new DomainEvent();
} else {
// at this point Doctrine doesn't know that the events are
// deleted in the database, so refresh in order to make the diff work
$this->em->refresh($event);
}
2024-12-20 22:25:41 +01:00
$domain->addEvent($event
2025-10-19 14:21:52 +02:00
->setAction(strtolower($rdapEvent['eventAction']))
->setDate(new \DateTimeImmutable($rdapEvent['eventDate']))
2024-12-20 22:25:41 +01:00
->setDeleted(false));
$this->em->persist($domain);
2024-08-02 23:24:52 +02:00
}
2024-07-13 23:57:07 +02:00
}
2024-12-20 22:25:41 +01:00
}
2024-07-13 23:57:07 +02:00
2024-12-20 22:25:41 +01:00
/**
* @throws \Exception
2024-12-20 22:25:41 +01:00
*/
private function updateDomainEntities(Domain $domain, array $rdapData): void
{
2025-10-21 12:52:43 +02:00
$this->domainEntityRepository->setDomainEntityAsDeleted($domain);
if (!isset($rdapData['entities']) || !is_array($rdapData['entities'])) {
return;
}
foreach ($rdapData['entities'] as $rdapEntity) {
$roles = $this->extractEntityRoles($rdapData['entities'], $rdapEntity);
$entity = $this->registerEntity($rdapEntity, $roles, $domain->getLdhName(), $domain->getTld());
$domainEntity = $this->domainEntityRepository->findOneBy([
'domain' => $domain,
'entity' => $entity,
]);
2024-07-23 03:13:51 +02:00
if (null === $domainEntity) {
$domainEntity = new DomainEntity();
} else {
$this->em->refresh($domainEntity);
2024-07-13 23:57:07 +02:00
}
$domain->addDomainEntity($domainEntity
->setDomain($domain)
->setEntity($entity)
->setRoles($roles)
->setDeletedAt(null));
$this->em->persist($domainEntity);
$this->em->flush();
2024-07-23 03:13:51 +02:00
}
2024-12-20 22:25:41 +01:00
}
2024-07-13 23:57:07 +02:00
/**
* @throws \Exception
*/
2024-12-20 22:25:41 +01:00
private function updateDomainNameservers(Domain $domain, array $rdapData): void
{
if (isset($rdapData['nameservers']) && is_array($rdapData['nameservers'])) {
2024-08-17 18:22:24 +02:00
$domain->getNameservers()->clear();
$this->em->persist($domain);
2024-08-17 18:22:24 +02:00
2024-12-20 22:25:41 +01:00
foreach ($rdapData['nameservers'] as $rdapNameserver) {
$nameserver = $this->fetchOrCreateNameserver($rdapNameserver, $domain);
2025-02-18 01:29:29 +01:00
$this->updateNameserverEntities($nameserver, $rdapNameserver, $domain->getTld());
2024-07-23 03:13:51 +02:00
if (!$domain->getNameservers()->contains($nameserver)) {
$domain->addNameserver($nameserver);
}
2024-07-23 03:13:51 +02:00
}
2024-08-04 14:45:27 +02:00
} else {
2025-10-03 18:14:43 +02:00
$this->logger->warning('Domain name has no nameservers', [
'ldhName' => $domain->getLdhName(),
2024-08-04 14:45:27 +02:00
]);
2024-07-13 23:57:07 +02:00
}
2024-12-20 22:25:41 +01:00
}
2024-07-13 23:57:07 +02:00
2024-12-20 22:25:41 +01:00
private function fetchOrCreateNameserver(array $rdapNameserver, Domain $domain): Nameserver
{
2025-10-19 14:21:52 +02:00
$ldhName = strtolower(rtrim($rdapNameserver['ldhName'], '.'));
2024-12-20 22:25:41 +01:00
$nameserver = $this->nameserverRepository->findOneBy([
2025-10-19 14:21:52 +02:00
'ldhName' => $ldhName,
2024-12-20 22:25:41 +01:00
]);
2024-07-13 23:57:07 +02:00
2025-10-19 14:21:52 +02:00
$existingDomainNS = $domain->getNameservers()->findFirst(fn (int $key, Nameserver $ns) => $ns->getLdhName() === $ldhName);
2024-12-20 22:25:41 +01:00
if (null !== $existingDomainNS) {
return $existingDomainNS;
} elseif (null === $nameserver) {
$nameserver = new Nameserver();
}
2025-10-19 14:21:52 +02:00
$nameserver->setLdhName($ldhName);
2024-12-20 22:25:41 +01:00
return $nameserver;
2024-07-13 23:57:07 +02:00
}
/**
* @throws \Exception
*/
2025-02-18 01:29:29 +01:00
private function updateNameserverEntities(Nameserver $nameserver, array $rdapNameserver, Tld $tld): void
2024-07-13 23:57:07 +02:00
{
if (!isset($rdapNameserver['entities']) || !is_array($rdapNameserver['entities'])) {
2024-12-20 22:25:41 +01:00
return;
2024-12-20 17:43:35 +01:00
}
2024-12-20 22:25:41 +01:00
foreach ($rdapNameserver['entities'] as $rdapEntity) {
$roles = $this->extractEntityRoles($rdapNameserver['entities'], $rdapEntity);
2025-02-18 01:29:29 +01:00
$entity = $this->registerEntity($rdapEntity, $roles, $nameserver->getLdhName(), $tld);
2024-12-20 22:25:41 +01:00
$nameserverEntity = $this->nameserverEntityRepository->findOneBy([
'nameserver' => $nameserver,
'entity' => $entity,
]);
if (null === $nameserverEntity) {
$nameserverEntity = new NameserverEntity();
}
$nameserver->addNameserverEntity($nameserverEntity
->setNameserver($nameserver)
->setEntity($entity)
2025-10-19 14:21:52 +02:00
->setStatus(array_map(fn ($s) => strtolower($s), array_unique($rdapNameserver['status'])))
2024-12-20 22:25:41 +01:00
->setRoles($roles));
$this->em->persist($nameserverEntity);
$this->em->flush();
2024-07-13 23:57:07 +02:00
}
2024-12-20 22:25:41 +01:00
}
2024-07-19 18:59:21 +02:00
2024-12-20 22:25:41 +01:00
private function extractEntityRoles(array $entities, array $targetEntity): array
{
$roles = array_map(
fn ($e) => $e['roles'],
array_filter(
$entities,
fn ($e) => isset($targetEntity['handle']) && isset($e['handle'])
? $targetEntity['handle'] === $e['handle']
: (
isset($targetEntity['vcardArray']) && isset($e['vcardArray'])
? $targetEntity['vcardArray'] === $e['vcardArray']
: $targetEntity === $e
)
2024-12-20 22:25:41 +01:00
)
);
if (count($roles) !== count($roles, COUNT_RECURSIVE)) {
$roles = array_merge(...$roles);
}
2025-10-19 14:21:52 +02:00
return array_map(fn ($x) => strtolower($x), $roles);
2024-07-13 23:57:07 +02:00
}
2024-07-14 11:20:04 +02:00
/**
2024-08-02 23:24:52 +02:00
* @throws \Exception
2024-07-14 11:20:04 +02:00
*/
2025-02-18 01:29:29 +01:00
private function registerEntity(array $rdapEntity, array $roles, string $domain, Tld $tld): Entity
2024-07-13 23:57:07 +02:00
{
/*
* If there is no number to identify the entity, one is generated from the domain name and the roles associated with this entity
*/
if (!isset($rdapEntity['handle']) || '' === $rdapEntity['handle'] || in_array($rdapEntity['handle'], self::ENTITY_HANDLE_BLACKLIST)) {
sort($roles);
$rdapEntity['handle'] = 'DW-FAKEHANDLE-'.$domain.'-'.implode(',', $roles);
2024-12-20 19:41:48 +01:00
2025-10-03 18:14:43 +02:00
$this->logger->warning('Entity has no handle key', [
'handle' => $rdapEntity['handle'],
2025-10-03 18:14:43 +02:00
'ldhName' => $domain,
]);
2024-12-20 19:41:48 +01:00
}
2024-12-20 17:43:35 +01:00
$entity = $this->entityRepository->findOneBy([
'handle' => $rdapEntity['handle'],
'tld' => $tld,
]);
2025-09-11 10:43:48 +02:00
2024-08-02 23:24:52 +02:00
if (null === $entity) {
$entity = (new Entity())->setTld($tld);
2024-08-15 19:54:36 +02:00
$this->logger->debug('Entity was not known to this instance', [
2024-08-04 14:45:27 +02:00
'handle' => $rdapEntity['handle'],
2025-10-03 18:14:43 +02:00
'ldhName' => $domain,
2024-08-04 14:45:27 +02:00
]);
2024-08-02 23:24:52 +02:00
}
2024-07-13 23:57:07 +02:00
/**
* If the RDAP server transmits the entity's IANA number, it is used as a priority to identify the entity.
*
* @see https://datatracker.ietf.org/doc/html/rfc7483#section-4.8
*/
$icannAccreditation = null;
if (isset($rdapEntity['publicIds'])) {
foreach ($rdapEntity['publicIds'] as $publicId) {
if ('IANA Registrar ID' === $publicId['type'] && isset($publicId['identifier']) && '' !== $publicId['identifier']) {
$icannAccreditation = $this->icannAccreditationRepository->findOneBy([
'id' => (int) $publicId['identifier'],
]);
}
}
}
$entity->setHandle($rdapEntity['handle'])->setIcannAccreditation($icannAccreditation);
2024-07-13 23:57:07 +02:00
2025-09-11 10:43:48 +02:00
if (isset($rdapEntity['remarks']) && is_array($rdapEntity['remarks'])) {
2024-12-28 19:26:25 +01:00
$entity->setRemarks($rdapEntity['remarks']);
}
2025-09-11 10:43:48 +02:00
if (isset($rdapEntity['vcardArray'])) {
2024-07-23 03:05:35 +02:00
if (empty($entity->getJCard())) {
2025-08-11 00:39:44 +02:00
if (!array_key_exists('elements', $rdapEntity['vcardArray'])) {
$entity->setJCard($rdapEntity['vcardArray']);
} else {
/*
* UZ registry
*/
$entity->setJCard([
'vcard',
$rdapEntity['vcardArray']['elements'],
]);
}
2024-07-23 03:05:35 +02:00
} else {
$properties = [];
2025-08-11 00:39:44 +02:00
if (!array_key_exists('elements', $rdapEntity['vcardArray'])) {
foreach ($rdapEntity['vcardArray'][1] as $prop) {
$properties[$prop[0]] = $prop;
}
} else {
/*
* UZ registry
*/
foreach ($rdapEntity['vcardArray']['elements'] as $prop) {
$properties[$prop[0]] = $prop;
}
2024-07-23 03:05:35 +02:00
}
foreach ($entity->getJCard()[1] as $prop) {
$properties[$prop[0]] = $prop;
}
2024-08-02 23:24:52 +02:00
$entity->setJCard(['vcard', array_values($properties)]);
2024-07-13 23:57:07 +02:00
}
}
2025-09-11 10:43:48 +02:00
if (isset($rdapEntity['events'])) {
/** @var EntityEvent $event */
foreach ($entity->getEvents()->getIterator() as $event) {
$event->setDeleted(true);
}
2024-09-01 21:26:07 +02:00
2025-09-11 10:43:48 +02:00
$this->em->persist($entity);
2024-09-01 21:26:07 +02:00
2025-09-11 10:43:48 +02:00
foreach ($rdapEntity['events'] as $rdapEntityEvent) {
$eventAction = $rdapEntityEvent['eventAction'];
if ($eventAction === EventAction::LastChanged->value || $eventAction === EventAction::LastUpdateOfRDAPDatabase->value) {
continue;
}
$event = $this->entityEventRepository->findOneBy([
'action' => $rdapEntityEvent['eventAction'],
'date' => new \DateTimeImmutable($rdapEntityEvent['eventDate']),
]);
2024-07-13 23:57:07 +02:00
2025-09-11 10:43:48 +02:00
if (null !== $event) {
$event->setDeleted(false);
continue;
}
$entity->addEvent(
(new EntityEvent())
->setEntity($entity)
2025-10-19 14:21:52 +02:00
->setAction(strtolower($rdapEntityEvent['eventAction']))
2025-09-11 10:43:48 +02:00
->setDate(new \DateTimeImmutable($rdapEntityEvent['eventDate']))
->setDeleted(false));
2024-08-02 23:24:52 +02:00
}
2024-07-13 23:57:07 +02:00
}
2024-08-02 23:24:52 +02:00
2024-12-20 19:41:48 +01:00
$this->em->persist($entity);
$this->em->flush();
2024-12-20 19:41:48 +01:00
2024-07-13 23:57:07 +02:00
return $entity;
}
2024-07-18 19:13:06 +02:00
2025-08-26 16:18:29 +02:00
private function updateDomainDsData(Domain $domain, array $rdapData): void
{
$domain->getDnsKey()->clear();
$this->em->persist($domain);
$this->em->flush();
if (array_key_exists('secureDNS', $rdapData) && array_key_exists('dsData', $rdapData['secureDNS']) && is_array($rdapData['secureDNS']['dsData'])) {
foreach ($rdapData['secureDNS']['dsData'] as $rdapDsData) {
$dsData = new DnsKey();
if (array_key_exists('keyTag', $rdapDsData)) {
$dsData->setKeyTag(pack('n', $rdapDsData['keyTag']));
}
if (array_key_exists('algorithm', $rdapDsData)) {
$dsData->setAlgorithm(Algorithm::from($rdapDsData['algorithm']));
}
if (array_key_exists('digest', $rdapDsData)) {
2025-09-11 13:52:53 +02:00
try {
$blob = hex2bin($rdapDsData['digest']);
} catch (\Exception) {
2025-10-03 18:14:43 +02:00
$this->logger->warning('DNSSEC digest is not a valid hexadecimal value', [
'ldhName' => $domain,
'value' => $rdapDsData['digest'],
]);
2025-09-11 13:52:53 +02:00
continue;
}
2025-08-26 16:18:29 +02:00
if (false === $blob) {
2025-10-03 18:14:43 +02:00
$this->logger->warning('DNSSEC digest is not a valid hexadecimal value', [
'ldhName' => $domain,
'value' => $rdapDsData['digest'],
]);
2025-09-11 13:52:53 +02:00
continue;
2025-08-26 16:18:29 +02:00
}
$dsData->setDigest($blob);
}
if (array_key_exists('digestType', $rdapDsData)) {
$dsData->setDigestType(DigestType::from($rdapDsData['digestType']));
}
2025-09-11 13:52:53 +02:00
$digestLengthByte = [
DigestType::SHA1->value => 20,
DigestType::SHA256->value => 32,
DigestType::GOST_R_34_11_94->value => 32,
DigestType::SHA384->value => 48,
DigestType::GOST_R_34_11_2012->value => 64,
DigestType::SM3->value => 32,
];
if (array_key_exists($dsData->getDigestType()->value, $digestLengthByte)
&& strlen($dsData->getDigest()) / 2 !== $digestLengthByte[$dsData->getDigestType()->value]) {
2025-10-03 18:14:43 +02:00
$this->logger->warning('DNSSEC digest does not have a valid length according to the digest type', [
'ldhName' => $domain,
'value' => $dsData->getDigest(),
'type' => $dsData->getDigestType()->name,
]);
2025-09-11 13:52:53 +02:00
continue;
}
2025-08-26 16:18:29 +02:00
$domain->addDnsKey($dsData);
$this->em->persist($dsData);
}
} else {
2025-10-03 18:14:43 +02:00
$this->logger->warning('Domain name has no DS record', [
'ldhName' => $domain->getLdhName(),
2025-08-26 16:18:29 +02:00
]);
}
}
2024-08-02 23:24:52 +02:00
}