Skip to content

Commit

Permalink
feat: add more ITs
Browse files Browse the repository at this point in the history
  • Loading branch information
MiguelAHM committed Dec 4, 2024
1 parent 4cbd0be commit 32ca759
Show file tree
Hide file tree
Showing 3 changed files with 501 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package net.ripe.db.whois.api.rdap;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import net.ripe.db.whois.api.rdap.domain.Ip;
import net.ripe.db.whois.api.rdap.domain.RelationType;
import net.ripe.db.whois.common.dao.RpslObjectDao;
import net.ripe.db.whois.common.domain.CIString;
Expand All @@ -21,10 +19,12 @@
import net.ripe.db.whois.common.rpsl.attrs.Domain;
import net.ripe.db.whois.common.rpsl.attrs.Inet6numStatus;
import net.ripe.db.whois.common.rpsl.attrs.InetnumStatus;
import net.ripe.db.whois.common.search.ManagedAttributeSearch;
import org.eclipse.jetty.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Nullable;
import java.util.List;
import java.util.Objects;
import java.util.Set;
Expand All @@ -33,17 +33,17 @@
@Service
public class RdapRelationService {

private final RdapQueryHandler rdapQueryHandler;
private static final Logger LOGGER = LoggerFactory.getLogger(RdapRelationService.class);

private final Ipv4Tree ip4Tree;
private final Ipv6Tree ip6Tree;
private final Ipv4DomainTree ipv4DomainTree;
private final Ipv6DomainTree ipv6DomainTree;
private final RpslObjectDao rpslObjectDao;

public RdapRelationService(final RdapQueryHandler rdapQueryHandler, final Ipv4Tree ip4Tree, final Ipv6Tree ip6Tree,
public RdapRelationService(final Ipv4Tree ip4Tree, final Ipv6Tree ip6Tree,
final Ipv4DomainTree ipv4DomainTree, final Ipv6DomainTree ipv6DomainTree,
final RpslObjectDao rpslObjectDao) {
this.rdapQueryHandler = rdapQueryHandler;
this.ip4Tree = ip4Tree;
this.ip6Tree = ip6Tree;
this.ipv4DomainTree = ipv4DomainTree;
Expand All @@ -54,17 +54,17 @@ public RdapRelationService(final RdapQueryHandler rdapQueryHandler, final Ipv4Tr
public List<String> getDomainRelationPkeys(final String pkey, final RelationType relationType){
final Domain domain = Domain.parse(pkey);
final IpInterval reverseIp = domain.getReverseIp();
final List<IpEntry> ipEntries = getIpEntries(getIpDomainTree(reverseIp), relationType, reverseIp);

return getRelationPkeys(relationType, getIpDomainTree(reverseIp), reverseIp);
return ipEntries
.stream()
.map(ipEntry -> rpslObjectDao.getById(ipEntry.getObjectId()).getKey().toString())
.toList();
}

public List<String> getInetnumRelationPkeys(final String pkey, final RelationType relationType){
final IpInterval ip = IpInterval.parse(pkey);
return getRelationPkeys(relationType, getIpTree(ip), ip);
}

private List<String> getRelationPkeys(RelationType relationType, IpTree ipTree, IpInterval ip) {
final List<IpEntry> ipEntries = getIpEntries(ipTree, relationType, ip);
final List<IpEntry> ipEntries = getIpEntries(getIpTree(ip), relationType, ip);
return ipEntries.stream().map(ipEntry -> ipEntry.getKey().toString()).toList();
}

Expand Down Expand Up @@ -127,39 +127,51 @@ private static boolean childrenCoverParentRange(final IpInterval firstResource,
}

private static List<IpEntry> findSiblingsAndExact(final IpTree ipTree, final IpInterval parentResource) {
final IpEntry parent = (IpEntry)ipTree.findFirstLessSpecific(parentResource).getFirst();
return ipTree.findFirstMoreSpecific(IpInterval.parse(parent.getKey().toString()));
final List<IpEntry> parentList = ipTree.findFirstLessSpecific(parentResource);
if (parentList.isEmpty()){
return ipTree.findExact(parentResource);
}
return ipTree.findFirstMoreSpecific(IpInterval.parse(parentList.getFirst().getKey().toString()));
}

private IpEntry searchFirstLessSpecificCoMntner(final IpTree ipTree, final IpInterval reverseIp){
final IpEntry firstLessSpecific = (IpEntry) ipTree.findFirstLessSpecific(reverseIp).getFirst();
final RpslObject rpslObject = rpslObjectDao.getById(firstLessSpecific.getObjectId());

if (!isOutOfRegionOrRoot(rpslObject)) {
return firstLessSpecific;
final List<IpEntry> parentList = ipTree.findFirstLessSpecific(reverseIp);
if (parentList.isEmpty() || isOutOfRegionOrRoot(parentList.getFirst())){
throw new RdapException("404 Not Found", "No up level object has been found for " + reverseIp.toString(), HttpStatus.NOT_FOUND_404);
}

throw new RdapException("404 Not Found", "No up level object has been found for " + reverseIp.toString(), HttpStatus.NOT_FOUND_404);
return parentList.getFirst();
}


private List<IpEntry> searchCoMntnerTopLevel(final IpTree ipTree, final IpInterval reverseIp) {
for (final Object parentEntry : ipTree.findAllLessSpecific(reverseIp)) {
final IpEntry ipEntry = (IpEntry) parentEntry;
final RpslObject rpslObject = rpslObjectDao.getById(ipEntry.getObjectId());
if (!isOutOfRegionOrRoot(rpslObject)){
if (!isOutOfRegionOrRoot(ipEntry)){
return List.of(ipEntry);
}
}
throw new RdapException("404 Not Found", "No top level object has been found for " + reverseIp.toString(), HttpStatus.NOT_FOUND_404);
}

private static boolean isOutOfRegionOrRoot(final RpslObject rpslObject) {
private boolean isOutOfRegionOrRoot(final IpEntry firstLessSpecific) {
final RpslObject rpslObject = getResourceByKey(firstLessSpecific.getKey().toString());
if (rpslObject == null) {
return true;
}
final CIString status = rpslObject.getValueForAttribute(AttributeType.STATUS);
return (rpslObject.getType().equals(ObjectType.INETNUM) && InetnumStatus.getStatusFor(status).isOutOfRegionOrRoot())
|| (rpslObject.getType().equals(ObjectType.INET6NUM) && Inet6numStatus.getStatusFor(status).isOutOfRegionOrRoot());
}

@Nullable
private RpslObject getResourceByKey(final String key){
final RpslObject rpslObject = rpslObjectDao.getByKeyOrNull(ObjectType.INET6NUM, key);
if (rpslObject == null){
LOGGER.error("INET(6)NUM {} does not exist in RIPE Database ", key);
return rpslObjectDao.getByKeyOrNull(ObjectType.INETNUM, key);
}
return rpslObject;
}

private IpTree getIpTree(final IpInterval reverseIp) {
if (reverseIp instanceof Ipv4Resource) {
return ip4Tree;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -519,14 +519,14 @@ private List<RpslObject> handleRelationQuery(final HttpServletRequest request, f
final RelationType relationType, final String key) {
final List<RpslObject> rpslObjects;
switch (requestType) {
case AUTNUM -> throw new RdapException("400 Bad Request", "Relation queries not allowed for autnum", HttpStatus.BAD_REQUEST_400);
case DOMAIN -> {
case AUTNUMS -> throw new RdapException("400 Bad Request", "Relation queries not allowed for autnum", HttpStatus.BAD_REQUEST_400);
case DOMAINS -> {
rdapRequestValidator.validateDomain(key);
final List<String> relatedPkeys = rdapRelationService.getDomainRelationPkeys(key, relationType);

rpslObjects = relatedPkeys
.stream()
.flatMap(relatedPkey -> rdapQueryHandler.handleQueryStream(getQueryObject(ImmutableSet.of(DOMAIN), key), request))
.flatMap(relatedPkey -> rdapQueryHandler.handleQueryStream(getQueryObject(ImmutableSet.of(DOMAIN), relatedPkey), request))
.toList();
}
case IPS -> {
Expand Down
Loading

0 comments on commit 32ca759

Please sign in to comment.