Skip to content

Commit

Permalink
Make the interface String-centric
Browse files Browse the repository at this point in the history
  • Loading branch information
joegallo committed Sep 17, 2024
1 parent d658aca commit 53e21f0
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.elasticsearch.client.internal.Client;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.network.InetAddresses;
import org.elasticsearch.common.util.concurrent.AtomicArray;
import org.elasticsearch.core.IOUtils;
import org.elasticsearch.index.VersionType;
Expand Down Expand Up @@ -206,10 +205,10 @@ private static DatabaseNodeService createRegistry(Path geoIpConfigDir, Path geoI
private static void lazyLoadReaders(DatabaseNodeService databaseNodeService) throws IOException {
if (databaseNodeService.get("GeoLite2-City.mmdb") != null) {
databaseNodeService.get("GeoLite2-City.mmdb").getDatabaseType();
databaseNodeService.get("GeoLite2-City.mmdb").getCity(InetAddresses.forString("2.125.160.216"));
databaseNodeService.get("GeoLite2-City.mmdb").getCity("2.125.160.216");
}
databaseNodeService.get("GeoLite2-City-Test.mmdb").getDatabaseType();
databaseNodeService.get("GeoLite2-City-Test.mmdb").getCity(InetAddresses.forString("2.125.160.216"));
databaseNodeService.get("GeoLite2-City-Test.mmdb").getCity("2.125.160.216");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.lucene.util.SetOnce;
import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.common.CheckedBiFunction;
import org.elasticsearch.common.CheckedSupplier;
import org.elasticsearch.common.network.InetAddresses;
import org.elasticsearch.common.network.NetworkAddress;
import org.elasticsearch.core.Booleans;
import org.elasticsearch.core.IOUtils;
Expand Down Expand Up @@ -96,19 +98,19 @@ public final String getDatabaseType() throws IOException {

@Nullable
@Override
public CityResponse getCity(InetAddress ipAddress) {
public CityResponse getCity(String ipAddress) {
return getResponse(ipAddress, (reader, ip) -> lookup(reader, ip, CityResponse.class, CityResponse::new));
}

@Nullable
@Override
public CountryResponse getCountry(InetAddress ipAddress) {
public CountryResponse getCountry(String ipAddress) {
return getResponse(ipAddress, (reader, ip) -> lookup(reader, ip, CountryResponse.class, CountryResponse::new));
}

@Nullable
@Override
public AsnResponse getAsn(InetAddress ipAddress) {
public AsnResponse getAsn(String ipAddress) {
return getResponse(
ipAddress,
(reader, ip) -> lookup(
Expand All @@ -122,7 +124,7 @@ public AsnResponse getAsn(InetAddress ipAddress) {

@Nullable
@Override
public AnonymousIpResponse getAnonymousIp(InetAddress ipAddress) {
public AnonymousIpResponse getAnonymousIp(String ipAddress) {
return getResponse(
ipAddress,
(reader, ip) -> lookup(
Expand All @@ -136,7 +138,7 @@ public AnonymousIpResponse getAnonymousIp(InetAddress ipAddress) {

@Nullable
@Override
public ConnectionTypeResponse getConnectionType(InetAddress ipAddress) {
public ConnectionTypeResponse getConnectionType(String ipAddress) {
return getResponse(
ipAddress,
(reader, ip) -> lookup(
Expand All @@ -150,7 +152,7 @@ public ConnectionTypeResponse getConnectionType(InetAddress ipAddress) {

@Nullable
@Override
public DomainResponse getDomain(InetAddress ipAddress) {
public DomainResponse getDomain(String ipAddress) {
return getResponse(
ipAddress,
(reader, ip) -> lookup(
Expand All @@ -164,13 +166,13 @@ public DomainResponse getDomain(InetAddress ipAddress) {

@Nullable
@Override
public EnterpriseResponse getEnterprise(InetAddress ipAddress) {
public EnterpriseResponse getEnterprise(String ipAddress) {
return getResponse(ipAddress, (reader, ip) -> lookup(reader, ip, EnterpriseResponse.class, EnterpriseResponse::new));
}

@Nullable
@Override
public IspResponse getIsp(InetAddress ipAddress) {
public IspResponse getIsp(String ipAddress) {
return getResponse(
ipAddress,
(reader, ip) -> lookup(
Expand Down Expand Up @@ -199,14 +201,14 @@ int current() {

@Nullable
private <T extends AbstractResponse> T getResponse(
InetAddress ipAddress,
CheckedBiFunction<Reader, InetAddress, Optional<T>, Exception> responseProvider
String ipAddress,
CheckedBiFunction<Reader, String, Optional<T>, Exception> responseProvider
) {
return cache.putIfAbsent(ipAddress, databasePath.toString(), ip -> {
try {
return responseProvider.apply(get(), ipAddress).orElse(null);
} catch (Exception e) {
throw new RuntimeException(e);
throw ExceptionsHelper.convertToRuntime(e);
}
});
}
Expand Down Expand Up @@ -267,14 +269,15 @@ private interface ResponseBuilder<RESPONSE> {
RESPONSE build(RESPONSE response, String responseIp, Network network, List<String> locales);
}

private <RESPONSE> Optional<RESPONSE> lookup(Reader reader, InetAddress ip, Class<RESPONSE> clazz, ResponseBuilder<RESPONSE> builder)
private <RESPONSE> Optional<RESPONSE> lookup(Reader reader, String ip, Class<RESPONSE> clazz, ResponseBuilder<RESPONSE> builder)
throws IOException {
DatabaseRecord<RESPONSE> record = reader.getRecord(ip, clazz);
InetAddress inetAddress = InetAddresses.forString(ip);
DatabaseRecord<RESPONSE> record = reader.getRecord(inetAddress, clazz);
RESPONSE result = record.getData();
if (result == null) {
return Optional.empty();
} else {
return Optional.of(builder.build(result, NetworkAddress.format(ip), record.getNetwork(), List.of("en")));
return Optional.of(builder.build(result, NetworkAddress.format(inetAddress), record.getNetwork(), List.of("en")));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.ingest.geoip.stats.CacheStats;

import java.net.InetAddress;
import java.nio.file.Path;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Function;
Expand Down Expand Up @@ -62,11 +61,7 @@ public String toString() {
}

@SuppressWarnings("unchecked")
<T extends AbstractResponse> T putIfAbsent(
InetAddress ip,
String databasePath,
Function<InetAddress, AbstractResponse> retrieveFunction
) {
<T extends AbstractResponse> T putIfAbsent(String ip, String databasePath, Function<String, AbstractResponse> retrieveFunction) {
// can't use cache.computeIfAbsent due to the elevated permissions for the jackson (run via the cache loader)
CacheKey cacheKey = new CacheKey(ip, databasePath);
long cacheStart = relativeNanoTimeProvider.getAsLong();
Expand Down Expand Up @@ -98,7 +93,7 @@ <T extends AbstractResponse> T putIfAbsent(
}

// only useful for testing
AbstractResponse get(InetAddress ip, String databasePath) {
AbstractResponse get(String ip, String databasePath) {
CacheKey cacheKey = new CacheKey(ip, databasePath);
return cache.get(cacheKey);
}
Expand Down Expand Up @@ -141,5 +136,5 @@ public CacheStats getCacheStats() {
* path is needed to be included in the cache key. For example, if we only used the IP address as the key the City and ASN the same
* IP may be in both with different values and we need to cache both.
*/
private record CacheKey(InetAddress ip, String databasePath) {}
private record CacheKey(String ip, String databasePath) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@
import org.elasticsearch.common.CheckedSupplier;
import org.elasticsearch.common.logging.DeprecationCategory;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.network.InetAddresses;
import org.elasticsearch.core.Assertions;
import org.elasticsearch.ingest.AbstractProcessor;
import org.elasticsearch.ingest.IngestDocument;
import org.elasticsearch.ingest.Processor;
import org.elasticsearch.ingest.geoip.Database.Property;

import java.io.IOException;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -165,15 +163,14 @@ public IngestDocument execute(IngestDocument ingestDocument) throws IOException
return ingestDocument;
}

private Map<String, Object> getGeoData(IpDatabase ipDatabase, String ip) throws IOException {
private Map<String, Object> getGeoData(IpDatabase ipDatabase, String ipAddress) throws IOException {
final String databaseType = ipDatabase.getDatabaseType();
final Database database;
try {
database = Database.getDatabase(databaseType, databaseFile);
} catch (IllegalArgumentException e) {
throw new ElasticsearchParseException(e.getMessage(), e);
}
final InetAddress ipAddress = InetAddresses.forString(ip);
return switch (database) {
case City -> retrieveCityGeoData(ipDatabase, ipAddress);
case Country -> retrieveCountryGeoData(ipDatabase, ipAddress);
Expand Down Expand Up @@ -207,7 +204,7 @@ Set<Property> getProperties() {
return properties;
}

private Map<String, Object> retrieveCityGeoData(IpDatabase ipDatabase, InetAddress ipAddress) {
private Map<String, Object> retrieveCityGeoData(IpDatabase ipDatabase, String ipAddress) {
CityResponse response = ipDatabase.getCity(ipAddress);
if (response == null) {
return Map.of();
Expand Down Expand Up @@ -289,7 +286,7 @@ private Map<String, Object> retrieveCityGeoData(IpDatabase ipDatabase, InetAddre
return geoData;
}

private Map<String, Object> retrieveCountryGeoData(IpDatabase ipDatabase, InetAddress ipAddress) {
private Map<String, Object> retrieveCountryGeoData(IpDatabase ipDatabase, String ipAddress) {
CountryResponse response = ipDatabase.getCountry(ipAddress);
if (response == null) {
return Map.of();
Expand Down Expand Up @@ -330,7 +327,7 @@ private Map<String, Object> retrieveCountryGeoData(IpDatabase ipDatabase, InetAd
return geoData;
}

private Map<String, Object> retrieveAsnGeoData(IpDatabase ipDatabase, InetAddress ipAddress) {
private Map<String, Object> retrieveAsnGeoData(IpDatabase ipDatabase, String ipAddress) {
AsnResponse response = ipDatabase.getAsn(ipAddress);
if (response == null) {
return Map.of();
Expand Down Expand Up @@ -363,7 +360,7 @@ private Map<String, Object> retrieveAsnGeoData(IpDatabase ipDatabase, InetAddres
return geoData;
}

private Map<String, Object> retrieveAnonymousIpGeoData(IpDatabase ipDatabase, InetAddress ipAddress) {
private Map<String, Object> retrieveAnonymousIpGeoData(IpDatabase ipDatabase, String ipAddress) {
AnonymousIpResponse response = ipDatabase.getAnonymousIp(ipAddress);
if (response == null) {
return Map.of();
Expand Down Expand Up @@ -403,7 +400,7 @@ private Map<String, Object> retrieveAnonymousIpGeoData(IpDatabase ipDatabase, In
return geoData;
}

private Map<String, Object> retrieveConnectionTypeGeoData(IpDatabase ipDatabase, InetAddress ipAddress) {
private Map<String, Object> retrieveConnectionTypeGeoData(IpDatabase ipDatabase, String ipAddress) {
ConnectionTypeResponse response = ipDatabase.getConnectionType(ipAddress);
if (response == null) {
return Map.of();
Expand All @@ -425,7 +422,7 @@ private Map<String, Object> retrieveConnectionTypeGeoData(IpDatabase ipDatabase,
return geoData;
}

private Map<String, Object> retrieveDomainGeoData(IpDatabase ipDatabase, InetAddress ipAddress) {
private Map<String, Object> retrieveDomainGeoData(IpDatabase ipDatabase, String ipAddress) {
DomainResponse response = ipDatabase.getDomain(ipAddress);
if (response == null) {
return Map.of();
Expand All @@ -447,7 +444,7 @@ private Map<String, Object> retrieveDomainGeoData(IpDatabase ipDatabase, InetAdd
return geoData;
}

private Map<String, Object> retrieveEnterpriseGeoData(IpDatabase ipDatabase, InetAddress ipAddress) {
private Map<String, Object> retrieveEnterpriseGeoData(IpDatabase ipDatabase, String ipAddress) {
EnterpriseResponse response = ipDatabase.getEnterprise(ipAddress);
if (response == null) {
return Map.of();
Expand Down Expand Up @@ -620,7 +617,7 @@ private Map<String, Object> retrieveEnterpriseGeoData(IpDatabase ipDatabase, Ine
return geoData;
}

private Map<String, Object> retrieveIspGeoData(IpDatabase ipDatabase, InetAddress ipAddress) {
private Map<String, Object> retrieveIspGeoData(IpDatabase ipDatabase, String ipAddress) {
IspResponse response = ipDatabase.getIsp(ipAddress);
if (response == null) {
return Map.of();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.elasticsearch.core.Nullable;

import java.io.IOException;
import java.net.InetAddress;

/**
* Provides a uniform interface for interacting with various ip databases.
Expand All @@ -40,15 +39,15 @@ public interface IpDatabase {
* @throws UnsupportedOperationException may be thrown if the implementation does not support retrieving city data
*/
@Nullable
CityResponse getCity(InetAddress ipAddress);
CityResponse getCity(String ipAddress);

/**
* @param ipAddress the IP address to look up
* @return a response containing the country data for the given address if it exists, or <code>null</code> if it could not be found
* @throws UnsupportedOperationException may be thrown if the implementation does not support retrieving country data
*/
@Nullable
CountryResponse getCountry(InetAddress ipAddress);
CountryResponse getCountry(String ipAddress);

/**
* @param ipAddress the IP address to look up
Expand All @@ -57,22 +56,22 @@ public interface IpDatabase {
* @throws UnsupportedOperationException may be thrown if the implementation does not support retrieving ASN data
*/
@Nullable
AsnResponse getAsn(InetAddress ipAddress);
AsnResponse getAsn(String ipAddress);

@Nullable
AnonymousIpResponse getAnonymousIp(InetAddress ipAddress);
AnonymousIpResponse getAnonymousIp(String ipAddress);

@Nullable
ConnectionTypeResponse getConnectionType(InetAddress ipAddress);
ConnectionTypeResponse getConnectionType(String ipAddress);

@Nullable
DomainResponse getDomain(InetAddress ipAddress);
DomainResponse getDomain(String ipAddress);

@Nullable
EnterpriseResponse getEnterprise(InetAddress ipAddress);
EnterpriseResponse getEnterprise(String ipAddress);

@Nullable
IspResponse getIsp(InetAddress ipAddress);
IspResponse getIsp(String ipAddress);

/**
* Releases the current database object. Called after processing a single document. Databases should be closed or returned to a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import com.maxmind.geoip2.model.CityResponse;

import org.elasticsearch.common.network.InetAddresses;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.test.ESTestCase;
Expand Down Expand Up @@ -127,7 +126,7 @@ public void testDatabasesUpdateExistingConfDatabase() throws Exception {

DatabaseReaderLazyLoader loader = configDatabases.getDatabase("GeoLite2-City.mmdb");
assertThat(loader.getDatabaseType(), equalTo("GeoLite2-City"));
CityResponse cityResponse = loader.getCity(InetAddresses.forString("89.160.20.128"));
CityResponse cityResponse = loader.getCity("89.160.20.128");
assertThat(cityResponse.getCity().getName(), equalTo("Tumba"));
assertThat(cache.count(), equalTo(1));
}
Expand All @@ -139,7 +138,7 @@ public void testDatabasesUpdateExistingConfDatabase() throws Exception {

DatabaseReaderLazyLoader loader = configDatabases.getDatabase("GeoLite2-City.mmdb");
assertThat(loader.getDatabaseType(), equalTo("GeoLite2-City"));
CityResponse cityResponse = loader.getCity(InetAddresses.forString("89.160.20.128"));
CityResponse cityResponse = loader.getCity("89.160.20.128");
assertThat(cityResponse.getCity().getName(), equalTo("Linköping"));
assertThat(cache.count(), equalTo(1));
});
Expand Down
Loading

0 comments on commit 53e21f0

Please sign in to comment.