|
@@ -27,6 +27,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
|
|
import static org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument;
|
|
|
import static org.elasticsearch.ingest.geoip.GeoIpProcessor.GEOIP_TYPE;
|
|
|
+import static org.elasticsearch.ingest.geoip.GeoIpProcessor.IP_LOCATION_TYPE;
|
|
|
import static org.elasticsearch.ingest.geoip.GeoIpTestUtils.copyDatabase;
|
|
|
import static org.hamcrest.Matchers.containsString;
|
|
|
import static org.hamcrest.Matchers.equalTo;
|
|
@@ -37,10 +38,6 @@ import static org.hamcrest.Matchers.nullValue;
|
|
|
|
|
|
public class GeoIpProcessorTests extends ESTestCase {
|
|
|
|
|
|
- private static IpDataLookup ipDataLookupAll(final Database database) {
|
|
|
- return IpDataLookupFactories.getMaxmindLookup(database).apply(database.properties());
|
|
|
- }
|
|
|
-
|
|
|
// a temporary directory that mmdb files can be copied to and read from
|
|
|
private Path tmpDir;
|
|
|
|
|
@@ -54,6 +51,66 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
IOUtils.rm(tmpDir);
|
|
|
}
|
|
|
|
|
|
+ public void testMaxmindCity() throws Exception {
|
|
|
+ String ip = "2602:306:33d3:8000::3257:9652";
|
|
|
+ GeoIpProcessor processor = new GeoIpProcessor(
|
|
|
+ GEOIP_TYPE, // n.b. this is a "geoip" processor
|
|
|
+ randomAlphaOfLength(10),
|
|
|
+ null,
|
|
|
+ "source_field",
|
|
|
+ loader("GeoLite2-City.mmdb"),
|
|
|
+ () -> true,
|
|
|
+ "target_field",
|
|
|
+ getMaxmindCityLookup(),
|
|
|
+ false,
|
|
|
+ false,
|
|
|
+ "filename"
|
|
|
+ );
|
|
|
+
|
|
|
+ Map<String, Object> document = new HashMap<>();
|
|
|
+ document.put("source_field", ip);
|
|
|
+ IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
|
|
|
+ processor.execute(ingestDocument);
|
|
|
+
|
|
|
+ assertThat(ingestDocument.getSourceAndMetadata().get("source_field"), equalTo(ip));
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ Map<String, Object> data = (Map<String, Object>) ingestDocument.getSourceAndMetadata().get("target_field");
|
|
|
+ assertThat(data, notNullValue());
|
|
|
+ assertThat(data.get("ip"), equalTo(ip));
|
|
|
+ assertThat(data.get("city_name"), equalTo("Homestead"));
|
|
|
+ // see MaxmindIpDataLookupsTests for more tests of the data lookup behavior
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testIpinfoGeolocation() throws Exception {
|
|
|
+ String ip = "13.107.39.238";
|
|
|
+ GeoIpProcessor processor = new GeoIpProcessor(
|
|
|
+ IP_LOCATION_TYPE, // n.b. this is an "ip_location" processor
|
|
|
+ randomAlphaOfLength(10),
|
|
|
+ null,
|
|
|
+ "source_field",
|
|
|
+ loader("ipinfo/ip_geolocation_sample.mmdb"),
|
|
|
+ () -> true,
|
|
|
+ "target_field",
|
|
|
+ getIpinfoGeolocationLookup(),
|
|
|
+ false,
|
|
|
+ false,
|
|
|
+ "filename"
|
|
|
+ );
|
|
|
+
|
|
|
+ Map<String, Object> document = new HashMap<>();
|
|
|
+ document.put("source_field", ip);
|
|
|
+ IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
|
|
|
+ processor.execute(ingestDocument);
|
|
|
+
|
|
|
+ assertThat(ingestDocument.getSourceAndMetadata().get("source_field"), equalTo(ip));
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ Map<String, Object> data = (Map<String, Object>) ingestDocument.getSourceAndMetadata().get("target_field");
|
|
|
+ assertThat(data, notNullValue());
|
|
|
+ assertThat(data.get("ip"), equalTo(ip));
|
|
|
+ assertThat(data.get("city_name"), equalTo("Des Moines"));
|
|
|
+ // see IpinfoIpDataLookupsTests for more tests of the data lookup behavior
|
|
|
+ }
|
|
|
+
|
|
|
public void testNullValueWithIgnoreMissing() throws Exception {
|
|
|
GeoIpProcessor processor = new GeoIpProcessor(
|
|
|
GEOIP_TYPE,
|
|
@@ -63,7 +120,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
loader("GeoLite2-City.mmdb"),
|
|
|
() -> true,
|
|
|
"target_field",
|
|
|
- ipDataLookupAll(Database.City),
|
|
|
+ getMaxmindCityLookup(),
|
|
|
true,
|
|
|
false,
|
|
|
"filename"
|
|
@@ -86,7 +143,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
loader("GeoLite2-City.mmdb"),
|
|
|
() -> true,
|
|
|
"target_field",
|
|
|
- ipDataLookupAll(Database.City),
|
|
|
+ getMaxmindCityLookup(),
|
|
|
true,
|
|
|
false,
|
|
|
"filename"
|
|
@@ -106,7 +163,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
loader("GeoLite2-City.mmdb"),
|
|
|
() -> true,
|
|
|
"target_field",
|
|
|
- ipDataLookupAll(Database.City),
|
|
|
+ getMaxmindCityLookup(),
|
|
|
false,
|
|
|
false,
|
|
|
"filename"
|
|
@@ -129,7 +186,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
loader("GeoLite2-City.mmdb"),
|
|
|
() -> true,
|
|
|
"target_field",
|
|
|
- ipDataLookupAll(Database.City),
|
|
|
+ getMaxmindCityLookup(),
|
|
|
false,
|
|
|
false,
|
|
|
"filename"
|
|
@@ -149,7 +206,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
loader("GeoLite2-City.mmdb"),
|
|
|
() -> true,
|
|
|
"target_field",
|
|
|
- ipDataLookupAll(Database.City),
|
|
|
+ getMaxmindCityLookup(),
|
|
|
false,
|
|
|
false,
|
|
|
"filename"
|
|
@@ -174,7 +231,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
loader("GeoLite2-City.mmdb"),
|
|
|
() -> true,
|
|
|
"target_field",
|
|
|
- ipDataLookupAll(Database.City),
|
|
|
+ getMaxmindCityLookup(),
|
|
|
false,
|
|
|
false,
|
|
|
"filename"
|
|
@@ -196,7 +253,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
loader("GeoLite2-City.mmdb"),
|
|
|
() -> true,
|
|
|
"target_field",
|
|
|
- ipDataLookupAll(Database.City),
|
|
|
+ getMaxmindCityLookup(),
|
|
|
false,
|
|
|
false,
|
|
|
"filename"
|
|
@@ -208,11 +265,11 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
processor.execute(ingestDocument);
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
- List<Map<String, Object>> geoData = (List<Map<String, Object>>) ingestDocument.getSourceAndMetadata().get("target_field");
|
|
|
- assertThat(geoData, notNullValue());
|
|
|
- assertThat(geoData.size(), equalTo(2));
|
|
|
- assertThat(geoData.get(0).get("location"), equalTo(Map.of("lat", 37.751d, "lon", -97.822d)));
|
|
|
- assertThat(geoData.get(1).get("city_name"), equalTo("Hoensbroek"));
|
|
|
+ List<Map<String, Object>> data = (List<Map<String, Object>>) ingestDocument.getSourceAndMetadata().get("target_field");
|
|
|
+ assertThat(data, notNullValue());
|
|
|
+ assertThat(data.size(), equalTo(2));
|
|
|
+ assertThat(data.get(0).get("location"), equalTo(Map.of("lat", 37.751d, "lon", -97.822d)));
|
|
|
+ assertThat(data.get(1).get("city_name"), equalTo("Hoensbroek"));
|
|
|
}
|
|
|
|
|
|
public void testListPartiallyValid() throws Exception {
|
|
@@ -224,7 +281,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
loader("GeoLite2-City.mmdb"),
|
|
|
() -> true,
|
|
|
"target_field",
|
|
|
- ipDataLookupAll(Database.City),
|
|
|
+ getMaxmindCityLookup(),
|
|
|
false,
|
|
|
false,
|
|
|
"filename"
|
|
@@ -236,11 +293,11 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
processor.execute(ingestDocument);
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
- List<Map<String, Object>> geoData = (List<Map<String, Object>>) ingestDocument.getSourceAndMetadata().get("target_field");
|
|
|
- assertThat(geoData, notNullValue());
|
|
|
- assertThat(geoData.size(), equalTo(2));
|
|
|
- assertThat(geoData.get(0).get("location"), equalTo(Map.of("lat", 37.751d, "lon", -97.822d)));
|
|
|
- assertThat(geoData.get(1), nullValue());
|
|
|
+ List<Map<String, Object>> data = (List<Map<String, Object>>) ingestDocument.getSourceAndMetadata().get("target_field");
|
|
|
+ assertThat(data, notNullValue());
|
|
|
+ assertThat(data.size(), equalTo(2));
|
|
|
+ assertThat(data.get(0).get("location"), equalTo(Map.of("lat", 37.751d, "lon", -97.822d)));
|
|
|
+ assertThat(data.get(1), nullValue());
|
|
|
}
|
|
|
|
|
|
public void testListNoMatches() throws Exception {
|
|
@@ -252,7 +309,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
loader("GeoLite2-City.mmdb"),
|
|
|
() -> true,
|
|
|
"target_field",
|
|
|
- ipDataLookupAll(Database.City),
|
|
|
+ getMaxmindCityLookup(),
|
|
|
false,
|
|
|
false,
|
|
|
"filename"
|
|
@@ -272,7 +329,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
GeoIpProcessor processor = new GeoIpProcessor(GEOIP_TYPE, randomAlphaOfLength(10), null, "source_field", () -> {
|
|
|
loader.preLookup();
|
|
|
return loader;
|
|
|
- }, () -> true, "target_field", ipDataLookupAll(Database.City), false, false, "filename");
|
|
|
+ }, () -> true, "target_field", getMaxmindCityLookup(), false, false, "filename");
|
|
|
|
|
|
Map<String, Object> document = new HashMap<>();
|
|
|
document.put("source_field", List.of("8.8.8.8", "82.171.64.0"));
|
|
@@ -280,11 +337,11 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
processor.execute(ingestDocument);
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
- List<Map<String, Object>> geoData = (List<Map<String, Object>>) ingestDocument.getSourceAndMetadata().get("target_field");
|
|
|
- assertThat(geoData, notNullValue());
|
|
|
- assertThat(geoData.size(), equalTo(2));
|
|
|
- assertThat(geoData.get(0).get("location"), equalTo(Map.of("lat", 37.751d, "lon", -97.822d)));
|
|
|
- assertThat(geoData.get(1).get("city_name"), equalTo("Hoensbroek"));
|
|
|
+ List<Map<String, Object>> data = (List<Map<String, Object>>) ingestDocument.getSourceAndMetadata().get("target_field");
|
|
|
+ assertThat(data, notNullValue());
|
|
|
+ assertThat(data.size(), equalTo(2));
|
|
|
+ assertThat(data.get(0).get("location"), equalTo(Map.of("lat", 37.751d, "lon", -97.822d)));
|
|
|
+ assertThat(data.get(1).get("city_name"), equalTo("Hoensbroek"));
|
|
|
|
|
|
// Check the loader's reference count and attempt to close
|
|
|
assertThat(loader.current(), equalTo(0));
|
|
@@ -301,7 +358,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
loader("GeoLite2-City.mmdb"),
|
|
|
() -> true,
|
|
|
"target_field",
|
|
|
- ipDataLookupAll(Database.City),
|
|
|
+ getMaxmindCityLookup(),
|
|
|
false,
|
|
|
true,
|
|
|
"filename"
|
|
@@ -313,9 +370,9 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
processor.execute(ingestDocument);
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
- Map<String, Object> geoData = (Map<String, Object>) ingestDocument.getSourceAndMetadata().get("target_field");
|
|
|
- assertThat(geoData, notNullValue());
|
|
|
- assertThat(geoData.get("location"), equalTo(Map.of("lat", 37.751d, "lon", -97.822d)));
|
|
|
+ Map<String, Object> data = (Map<String, Object>) ingestDocument.getSourceAndMetadata().get("target_field");
|
|
|
+ assertThat(data, notNullValue());
|
|
|
+ assertThat(data.get("location"), equalTo(Map.of("lat", 37.751d, "lon", -97.822d)));
|
|
|
}
|
|
|
|
|
|
public void testListFirstOnlyNoMatches() throws Exception {
|
|
@@ -327,7 +384,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
loader("GeoLite2-City.mmdb"),
|
|
|
() -> true,
|
|
|
"target_field",
|
|
|
- ipDataLookupAll(Database.City),
|
|
|
+ getMaxmindCityLookup(),
|
|
|
false,
|
|
|
true,
|
|
|
"filename"
|
|
@@ -350,7 +407,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
loader("GeoLite2-City.mmdb"),
|
|
|
() -> false,
|
|
|
"target_field",
|
|
|
- ipDataLookupAll(Database.City),
|
|
|
+ getMaxmindCityLookup(),
|
|
|
false,
|
|
|
true,
|
|
|
"filename"
|
|
@@ -374,7 +431,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
() -> null,
|
|
|
() -> true,
|
|
|
"target_field",
|
|
|
- ipDataLookupAll(Database.City),
|
|
|
+ getMaxmindCityLookup(),
|
|
|
false,
|
|
|
false,
|
|
|
"GeoLite2-City"
|
|
@@ -398,7 +455,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
() -> null,
|
|
|
() -> true,
|
|
|
"target_field",
|
|
|
- ipDataLookupAll(Database.City),
|
|
|
+ getMaxmindCityLookup(),
|
|
|
true,
|
|
|
false,
|
|
|
"GeoLite2-City"
|
|
@@ -412,13 +469,24 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
assertIngestDocument(originalIngestDocument, ingestDocument);
|
|
|
}
|
|
|
|
|
|
+ private static IpDataLookup getMaxmindCityLookup() {
|
|
|
+ final var database = Database.City;
|
|
|
+ return MaxmindIpDataLookups.getMaxmindLookup(database).apply(database.properties());
|
|
|
+ }
|
|
|
+
|
|
|
+ private static IpDataLookup getIpinfoGeolocationLookup() {
|
|
|
+ final var database = Database.CityV2;
|
|
|
+ return IpinfoIpDataLookups.getIpinfoLookup(database).apply(database.properties());
|
|
|
+ }
|
|
|
+
|
|
|
private CheckedSupplier<IpDatabase, IOException> loader(final String path) {
|
|
|
var loader = loader(path, null);
|
|
|
return () -> loader;
|
|
|
}
|
|
|
|
|
|
private DatabaseReaderLazyLoader loader(final String databaseName, final AtomicBoolean closed) {
|
|
|
- Path path = tmpDir.resolve(databaseName);
|
|
|
+ int last = databaseName.lastIndexOf("/");
|
|
|
+ final Path path = tmpDir.resolve(last == -1 ? databaseName : databaseName.substring(last + 1));
|
|
|
copyDatabase(databaseName, path);
|
|
|
|
|
|
final GeoIpCache cache = new GeoIpCache(1000);
|