|
@@ -29,9 +29,11 @@ import org.elasticsearch.test.ESTestCase;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
+import java.util.Arrays;
|
|
|
import java.util.Collections;
|
|
|
import java.util.EnumSet;
|
|
|
import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.function.Supplier;
|
|
|
|
|
@@ -39,13 +41,14 @@ import static org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocumen
|
|
|
import static org.hamcrest.Matchers.containsString;
|
|
|
import static org.hamcrest.Matchers.equalTo;
|
|
|
import static org.hamcrest.Matchers.is;
|
|
|
+import static org.hamcrest.Matchers.nullValue;
|
|
|
|
|
|
public class GeoIpProcessorTests extends ESTestCase {
|
|
|
|
|
|
public void testCity() throws Exception {
|
|
|
GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), "source_field",
|
|
|
loader("/GeoLite2-City.mmdb"), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false,
|
|
|
- new GeoIpCache(1000));
|
|
|
+ new GeoIpCache(1000), false);
|
|
|
|
|
|
Map<String, Object> document = new HashMap<>();
|
|
|
document.put("source_field", "8.8.8.8");
|
|
@@ -70,7 +73,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
public void testNullValueWithIgnoreMissing() throws Exception {
|
|
|
GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), "source_field",
|
|
|
loader("/GeoLite2-City.mmdb"), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), true,
|
|
|
- new GeoIpCache(1000));
|
|
|
+ new GeoIpCache(1000), false);
|
|
|
IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument(random(),
|
|
|
Collections.singletonMap("source_field", null));
|
|
|
IngestDocument ingestDocument = new IngestDocument(originalIngestDocument);
|
|
@@ -81,7 +84,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
public void testNonExistentWithIgnoreMissing() throws Exception {
|
|
|
GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), "source_field",
|
|
|
loader("/GeoLite2-City.mmdb"), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), true,
|
|
|
- new GeoIpCache(1000));
|
|
|
+ new GeoIpCache(1000), false);
|
|
|
IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.emptyMap());
|
|
|
IngestDocument ingestDocument = new IngestDocument(originalIngestDocument);
|
|
|
processor.execute(ingestDocument);
|
|
@@ -91,7 +94,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
public void testNullWithoutIgnoreMissing() throws Exception {
|
|
|
GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), "source_field",
|
|
|
loader("/GeoLite2-City.mmdb"), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false,
|
|
|
- new GeoIpCache(1000));
|
|
|
+ new GeoIpCache(1000), false);
|
|
|
IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument(random(),
|
|
|
Collections.singletonMap("source_field", null));
|
|
|
IngestDocument ingestDocument = new IngestDocument(originalIngestDocument);
|
|
@@ -102,7 +105,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
public void testNonExistentWithoutIgnoreMissing() throws Exception {
|
|
|
GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), "source_field",
|
|
|
loader("/GeoLite2-City.mmdb"), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false,
|
|
|
- new GeoIpCache(1000));
|
|
|
+ new GeoIpCache(1000), false);
|
|
|
IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.emptyMap());
|
|
|
IngestDocument ingestDocument = new IngestDocument(originalIngestDocument);
|
|
|
Exception exception = expectThrows(Exception.class, () -> processor.execute(ingestDocument));
|
|
@@ -112,7 +115,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
public void testCity_withIpV6() throws Exception {
|
|
|
GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), "source_field",
|
|
|
loader("/GeoLite2-City.mmdb"), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false,
|
|
|
- new GeoIpCache(1000));
|
|
|
+ new GeoIpCache(1000), false);
|
|
|
|
|
|
String address = "2602:306:33d3:8000::3257:9652";
|
|
|
Map<String, Object> document = new HashMap<>();
|
|
@@ -141,7 +144,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
public void testCityWithMissingLocation() throws Exception {
|
|
|
GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), "source_field",
|
|
|
loader("/GeoLite2-City.mmdb"), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false,
|
|
|
- new GeoIpCache(1000));
|
|
|
+ new GeoIpCache(1000), false);
|
|
|
|
|
|
Map<String, Object> document = new HashMap<>();
|
|
|
document.put("source_field", "80.231.5.0");
|
|
@@ -158,7 +161,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
public void testCountry() throws Exception {
|
|
|
GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), "source_field",
|
|
|
loader("/GeoLite2-Country.mmdb"), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false,
|
|
|
- new GeoIpCache(1000));
|
|
|
+ new GeoIpCache(1000), false);
|
|
|
|
|
|
Map<String, Object> document = new HashMap<>();
|
|
|
document.put("source_field", "82.170.213.79");
|
|
@@ -178,7 +181,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
public void testCountryWithMissingLocation() throws Exception {
|
|
|
GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), "source_field",
|
|
|
loader("/GeoLite2-Country.mmdb"), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false,
|
|
|
- new GeoIpCache(1000));
|
|
|
+ new GeoIpCache(1000), false);
|
|
|
|
|
|
Map<String, Object> document = new HashMap<>();
|
|
|
document.put("source_field", "80.231.5.0");
|
|
@@ -196,7 +199,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
String ip = "82.171.64.0";
|
|
|
GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), "source_field",
|
|
|
loader("/GeoLite2-ASN.mmdb"), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false,
|
|
|
- new GeoIpCache(1000));
|
|
|
+ new GeoIpCache(1000), false);
|
|
|
|
|
|
Map<String, Object> document = new HashMap<>();
|
|
|
document.put("source_field", ip);
|
|
@@ -215,7 +218,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
public void testAddressIsNotInTheDatabase() throws Exception {
|
|
|
GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), "source_field",
|
|
|
loader("/GeoLite2-City.mmdb"), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false,
|
|
|
- new GeoIpCache(1000));
|
|
|
+ new GeoIpCache(1000), false);
|
|
|
|
|
|
Map<String, Object> document = new HashMap<>();
|
|
|
document.put("source_field", "127.0.0.1");
|
|
@@ -228,7 +231,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
public void testInvalid() throws Exception {
|
|
|
GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), "source_field",
|
|
|
loader("/GeoLite2-City.mmdb"), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false,
|
|
|
- new GeoIpCache(1000));
|
|
|
+ new GeoIpCache(1000), false);
|
|
|
|
|
|
Map<String, Object> document = new HashMap<>();
|
|
|
document.put("source_field", "www.google.com");
|
|
@@ -237,6 +240,93 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
assertThat(e.getMessage(), containsString("not an IP string literal"));
|
|
|
}
|
|
|
|
|
|
+ public void testListAllValid() throws Exception {
|
|
|
+ GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), "source_field",
|
|
|
+ loader("/GeoLite2-City.mmdb"), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false,
|
|
|
+ new GeoIpCache(1000), false);
|
|
|
+
|
|
|
+ Map<String, Object> document = new HashMap<>();
|
|
|
+ document.put("source_field", Arrays.asList("8.8.8.8", "82.171.64.0"));
|
|
|
+ IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
|
|
|
+ processor.execute(ingestDocument);
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ List<Map<String, Object>> geoData = (List<Map<String, Object>>) ingestDocument.getSourceAndMetadata().get("target_field");
|
|
|
+
|
|
|
+ Map<String, Object> location = new HashMap<>();
|
|
|
+ location.put("lat", 37.751d);
|
|
|
+ location.put("lon", -97.822d);
|
|
|
+ assertThat(geoData.get(0).get("location"), equalTo(location));
|
|
|
+
|
|
|
+ assertThat(geoData.get(1).get("city_name"), equalTo("Hoensbroek"));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testListPartiallyValid() throws Exception {
|
|
|
+ GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), "source_field",
|
|
|
+ loader("/GeoLite2-City.mmdb"), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false,
|
|
|
+ new GeoIpCache(1000), false);
|
|
|
+
|
|
|
+ Map<String, Object> document = new HashMap<>();
|
|
|
+ document.put("source_field", Arrays.asList("8.8.8.8", "127.0.0.1"));
|
|
|
+ IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
|
|
|
+ processor.execute(ingestDocument);
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ List<Map<String, Object>> geoData = (List<Map<String, Object>>) ingestDocument.getSourceAndMetadata().get("target_field");
|
|
|
+
|
|
|
+ Map<String, Object> location = new HashMap<>();
|
|
|
+ location.put("lat", 37.751d);
|
|
|
+ location.put("lon", -97.822d);
|
|
|
+ assertThat(geoData.get(0).get("location"), equalTo(location));
|
|
|
+
|
|
|
+ assertThat(geoData.get(1), nullValue());
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testListNoMatches() throws Exception {
|
|
|
+ GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), "source_field",
|
|
|
+ loader("/GeoLite2-City.mmdb"), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false,
|
|
|
+ new GeoIpCache(1000), false);
|
|
|
+
|
|
|
+ Map<String, Object> document = new HashMap<>();
|
|
|
+ document.put("source_field", Arrays.asList("127.0.0.1", "127.0.0.1"));
|
|
|
+ IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
|
|
|
+ processor.execute(ingestDocument);
|
|
|
+
|
|
|
+ assertFalse(ingestDocument.hasField("target_field"));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testListFirstOnly() throws Exception {
|
|
|
+ GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), "source_field",
|
|
|
+ loader("/GeoLite2-City.mmdb"), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false,
|
|
|
+ new GeoIpCache(1000), true);
|
|
|
+
|
|
|
+ Map<String, Object> document = new HashMap<>();
|
|
|
+ document.put("source_field", Arrays.asList("8.8.8.8", "127.0.0.1"));
|
|
|
+ IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
|
|
|
+ processor.execute(ingestDocument);
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ Map<String, Object> geoData = (Map<String, Object>) ingestDocument.getSourceAndMetadata().get("target_field");
|
|
|
+
|
|
|
+ Map<String, Object> location = new HashMap<>();
|
|
|
+ location.put("lat", 37.751d);
|
|
|
+ location.put("lon", -97.822d);
|
|
|
+ assertThat(geoData.get("location"), equalTo(location));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testListFirstOnlyNoMatches() throws Exception {
|
|
|
+ GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), "source_field",
|
|
|
+ loader("/GeoLite2-City.mmdb"), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false,
|
|
|
+ new GeoIpCache(1000), true);
|
|
|
+
|
|
|
+ Map<String, Object> document = new HashMap<>();
|
|
|
+ document.put("source_field", Arrays.asList("127.0.0.1", "127.0.0.2"));
|
|
|
+ IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
|
|
|
+ processor.execute(ingestDocument);
|
|
|
+
|
|
|
+ assertThat(ingestDocument.getSourceAndMetadata().containsKey("target_field"), is(false));
|
|
|
+ }
|
|
|
+
|
|
|
private DatabaseReaderLazyLoader loader(final String path) {
|
|
|
final Supplier<InputStream> databaseInputStreamSupplier = () -> GeoIpProcessor.class.getResourceAsStream(path);
|
|
|
final CheckedSupplier<DatabaseReader, IOException> loader =
|