|
@@ -9,6 +9,7 @@
|
|
|
package org.elasticsearch.ingest.geoip;
|
|
|
|
|
|
import com.maxmind.geoip2.DatabaseReader;
|
|
|
+
|
|
|
import org.elasticsearch.common.CheckedSupplier;
|
|
|
import org.elasticsearch.core.PathUtils;
|
|
|
import org.elasticsearch.ingest.IngestDocument;
|
|
@@ -28,6 +29,7 @@ import java.util.function.Supplier;
|
|
|
import static org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument;
|
|
|
import static org.hamcrest.Matchers.containsString;
|
|
|
import static org.hamcrest.Matchers.equalTo;
|
|
|
+import static org.hamcrest.Matchers.hasEntry;
|
|
|
import static org.hamcrest.Matchers.is;
|
|
|
import static org.hamcrest.Matchers.nullValue;
|
|
|
|
|
@@ -35,7 +37,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
|
|
|
public void testCity() throws Exception {
|
|
|
GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field",
|
|
|
- loader("/GeoLite2-City.mmdb"), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false);
|
|
|
+ loader("/GeoLite2-City.mmdb"), () -> true, "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false);
|
|
|
|
|
|
Map<String, Object> document = new HashMap<>();
|
|
|
document.put("source_field", "8.8.8.8");
|
|
@@ -59,7 +61,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
|
|
|
public void testNullValueWithIgnoreMissing() throws Exception {
|
|
|
GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field",
|
|
|
- loader("/GeoLite2-City.mmdb"), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), true, false);
|
|
|
+ loader("/GeoLite2-City.mmdb"), () -> true, "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), true, false);
|
|
|
IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument(random(),
|
|
|
Collections.singletonMap("source_field", null));
|
|
|
IngestDocument ingestDocument = new IngestDocument(originalIngestDocument);
|
|
@@ -69,7 +71,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
|
|
|
public void testNonExistentWithIgnoreMissing() throws Exception {
|
|
|
GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field",
|
|
|
- loader("/GeoLite2-City.mmdb"), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), true, false);
|
|
|
+ loader("/GeoLite2-City.mmdb"), () -> true, "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), true, false);
|
|
|
IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.emptyMap());
|
|
|
IngestDocument ingestDocument = new IngestDocument(originalIngestDocument);
|
|
|
processor.execute(ingestDocument);
|
|
@@ -78,7 +80,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
|
|
|
public void testNullWithoutIgnoreMissing() throws Exception {
|
|
|
GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field",
|
|
|
- loader("/GeoLite2-City.mmdb"), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false);
|
|
|
+ loader("/GeoLite2-City.mmdb"), () -> true, "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false);
|
|
|
IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument(random(),
|
|
|
Collections.singletonMap("source_field", null));
|
|
|
IngestDocument ingestDocument = new IngestDocument(originalIngestDocument);
|
|
@@ -88,7 +90,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
|
|
|
public void testNonExistentWithoutIgnoreMissing() throws Exception {
|
|
|
GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field",
|
|
|
- loader("/GeoLite2-City.mmdb"), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false);
|
|
|
+ loader("/GeoLite2-City.mmdb"), () -> true, "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false);
|
|
|
IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.emptyMap());
|
|
|
IngestDocument ingestDocument = new IngestDocument(originalIngestDocument);
|
|
|
Exception exception = expectThrows(Exception.class, () -> processor.execute(ingestDocument));
|
|
@@ -97,7 +99,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
|
|
|
public void testCity_withIpV6() throws Exception {
|
|
|
GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field",
|
|
|
- loader("/GeoLite2-City.mmdb"), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false);
|
|
|
+ loader("/GeoLite2-City.mmdb"), () -> true, "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false);
|
|
|
|
|
|
String address = "2602:306:33d3:8000::3257:9652";
|
|
|
Map<String, Object> document = new HashMap<>();
|
|
@@ -125,7 +127,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
|
|
|
public void testCityWithMissingLocation() throws Exception {
|
|
|
GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field",
|
|
|
- loader("/GeoLite2-City.mmdb"), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false);
|
|
|
+ loader("/GeoLite2-City.mmdb"), () -> true, "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false);
|
|
|
|
|
|
Map<String, Object> document = new HashMap<>();
|
|
|
document.put("source_field", "80.231.5.0");
|
|
@@ -141,7 +143,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
|
|
|
public void testCountry() throws Exception {
|
|
|
GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field",
|
|
|
- loader("/GeoLite2-Country.mmdb"), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false);
|
|
|
+ loader("/GeoLite2-Country.mmdb"), () -> true, "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false);
|
|
|
|
|
|
Map<String, Object> document = new HashMap<>();
|
|
|
document.put("source_field", "82.170.213.79");
|
|
@@ -160,7 +162,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
|
|
|
public void testCountryWithMissingLocation() throws Exception {
|
|
|
GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field",
|
|
|
- loader("/GeoLite2-Country.mmdb"), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false);
|
|
|
+ loader("/GeoLite2-Country.mmdb"), () -> true, "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false);
|
|
|
|
|
|
Map<String, Object> document = new HashMap<>();
|
|
|
document.put("source_field", "80.231.5.0");
|
|
@@ -177,7 +179,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
public void testAsn() throws Exception {
|
|
|
String ip = "82.171.64.0";
|
|
|
GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field",
|
|
|
- loader("/GeoLite2-ASN.mmdb"), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false);
|
|
|
+ loader("/GeoLite2-ASN.mmdb"), () -> true, "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false);
|
|
|
|
|
|
Map<String, Object> document = new HashMap<>();
|
|
|
document.put("source_field", ip);
|
|
@@ -196,7 +198,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
|
|
|
public void testAddressIsNotInTheDatabase() throws Exception {
|
|
|
GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field",
|
|
|
- loader("/GeoLite2-City.mmdb"), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false);
|
|
|
+ loader("/GeoLite2-City.mmdb"), () -> true, "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false);
|
|
|
|
|
|
Map<String, Object> document = new HashMap<>();
|
|
|
document.put("source_field", "127.0.0.1");
|
|
@@ -205,10 +207,12 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
assertThat(ingestDocument.getSourceAndMetadata().containsKey("target_field"), is(false));
|
|
|
}
|
|
|
|
|
|
- /** Don't silently do DNS lookups or anything trappy on bogus data */
|
|
|
+ /**
|
|
|
+ * Don't silently do DNS lookups or anything trappy on bogus data
|
|
|
+ */
|
|
|
public void testInvalid() throws Exception {
|
|
|
GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field",
|
|
|
- loader("/GeoLite2-City.mmdb"), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false);
|
|
|
+ loader("/GeoLite2-City.mmdb"), () -> true, "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false);
|
|
|
|
|
|
Map<String, Object> document = new HashMap<>();
|
|
|
document.put("source_field", "www.google.com");
|
|
@@ -219,7 +223,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
|
|
|
public void testListAllValid() throws Exception {
|
|
|
GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field",
|
|
|
- loader("/GeoLite2-City.mmdb"), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false);
|
|
|
+ loader("/GeoLite2-City.mmdb"), () -> true, "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false);
|
|
|
|
|
|
Map<String, Object> document = new HashMap<>();
|
|
|
document.put("source_field", Arrays.asList("8.8.8.8", "82.171.64.0"));
|
|
@@ -239,7 +243,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
|
|
|
public void testListPartiallyValid() throws Exception {
|
|
|
GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field",
|
|
|
- loader("/GeoLite2-City.mmdb"), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false);
|
|
|
+ loader("/GeoLite2-City.mmdb"), () -> true, "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false);
|
|
|
|
|
|
Map<String, Object> document = new HashMap<>();
|
|
|
document.put("source_field", Arrays.asList("8.8.8.8", "127.0.0.1"));
|
|
@@ -259,7 +263,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
|
|
|
public void testListNoMatches() throws Exception {
|
|
|
GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field",
|
|
|
- loader("/GeoLite2-City.mmdb"), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false);
|
|
|
+ loader("/GeoLite2-City.mmdb"), () -> true, "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false);
|
|
|
|
|
|
Map<String, Object> document = new HashMap<>();
|
|
|
document.put("source_field", Arrays.asList("127.0.0.1", "127.0.0.1"));
|
|
@@ -271,7 +275,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
|
|
|
public void testListFirstOnly() throws Exception {
|
|
|
GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field",
|
|
|
- loader("/GeoLite2-City.mmdb"), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, true);
|
|
|
+ loader("/GeoLite2-City.mmdb"), () -> true, "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, true);
|
|
|
|
|
|
Map<String, Object> document = new HashMap<>();
|
|
|
document.put("source_field", Arrays.asList("8.8.8.8", "127.0.0.1"));
|
|
@@ -289,7 +293,19 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
|
|
|
public void testListFirstOnlyNoMatches() throws Exception {
|
|
|
GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field",
|
|
|
- loader("/GeoLite2-City.mmdb"), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, true);
|
|
|
+ loader("/GeoLite2-City.mmdb"), () -> true, "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, 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));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testInvalidDatabase() throws Exception {
|
|
|
+ GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field",
|
|
|
+ loader("/GeoLite2-City.mmdb"), () -> false, "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, true);
|
|
|
|
|
|
Map<String, Object> document = new HashMap<>();
|
|
|
document.put("source_field", Arrays.asList("127.0.0.1", "127.0.0.2"));
|
|
@@ -297,6 +313,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
processor.execute(ingestDocument);
|
|
|
|
|
|
assertThat(ingestDocument.getSourceAndMetadata().containsKey("target_field"), is(false));
|
|
|
+ assertThat(ingestDocument.getSourceAndMetadata(), hasEntry("tags", List.of("_geoip_expired_database")));
|
|
|
}
|
|
|
|
|
|
private CheckedSupplier<DatabaseReaderLazyLoader, IOException> loader(final String path) {
|