|
@@ -9,27 +9,27 @@
|
|
|
|
|
|
package org.elasticsearch.ingest.geoip;
|
|
|
|
|
|
-import com.maxmind.geoip2.DatabaseReader;
|
|
|
-
|
|
|
import org.elasticsearch.common.CheckedSupplier;
|
|
|
import org.elasticsearch.common.util.set.Sets;
|
|
|
-import org.elasticsearch.core.PathUtils;
|
|
|
+import org.elasticsearch.core.IOUtils;
|
|
|
import org.elasticsearch.ingest.IngestDocument;
|
|
|
import org.elasticsearch.ingest.RandomDocumentPicks;
|
|
|
import org.elasticsearch.ingest.geoip.Database.Property;
|
|
|
import org.elasticsearch.test.ESTestCase;
|
|
|
+import org.junit.After;
|
|
|
+import org.junit.Before;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
+import java.nio.file.Path;
|
|
|
import java.util.Collections;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Set;
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
-import java.util.function.Supplier;
|
|
|
|
|
|
import static org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument;
|
|
|
+import static org.elasticsearch.ingest.geoip.GeoIpTestUtils.copyDatabase;
|
|
|
import static org.hamcrest.Matchers.containsString;
|
|
|
import static org.hamcrest.Matchers.empty;
|
|
|
import static org.hamcrest.Matchers.equalTo;
|
|
@@ -41,6 +41,19 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
|
|
|
private static final Set<Property> ALL_PROPERTIES = Set.of(Property.values());
|
|
|
|
|
|
+ // a temporary directory that mmdb files can be copied to and read from
|
|
|
+ Path tmpDir;
|
|
|
+
|
|
|
+ @Before
|
|
|
+ public void setup() {
|
|
|
+ tmpDir = createTempDir();
|
|
|
+ }
|
|
|
+
|
|
|
+ @After
|
|
|
+ public void cleanup() throws IOException {
|
|
|
+ IOUtils.rm(tmpDir);
|
|
|
+ }
|
|
|
+
|
|
|
public void testDatabasePropertyInvariants() {
|
|
|
// the city database is like a specialization of the country database
|
|
|
assertThat(Sets.difference(Database.Country.properties(), Database.City.properties()), is(empty()));
|
|
@@ -64,7 +77,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
randomAlphaOfLength(10),
|
|
|
null,
|
|
|
"source_field",
|
|
|
- loader("/GeoLite2-City.mmdb"),
|
|
|
+ loader("GeoLite2-City.mmdb"),
|
|
|
() -> true,
|
|
|
"target_field",
|
|
|
ALL_PROPERTIES,
|
|
@@ -99,7 +112,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
randomAlphaOfLength(10),
|
|
|
null,
|
|
|
"source_field",
|
|
|
- loader("/GeoLite2-City.mmdb"),
|
|
|
+ loader("GeoLite2-City.mmdb"),
|
|
|
() -> true,
|
|
|
"target_field",
|
|
|
ALL_PROPERTIES,
|
|
@@ -121,7 +134,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
randomAlphaOfLength(10),
|
|
|
null,
|
|
|
"source_field",
|
|
|
- loader("/GeoLite2-City.mmdb"),
|
|
|
+ loader("GeoLite2-City.mmdb"),
|
|
|
() -> true,
|
|
|
"target_field",
|
|
|
ALL_PROPERTIES,
|
|
@@ -140,7 +153,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
randomAlphaOfLength(10),
|
|
|
null,
|
|
|
"source_field",
|
|
|
- loader("/GeoLite2-City.mmdb"),
|
|
|
+ loader("GeoLite2-City.mmdb"),
|
|
|
() -> true,
|
|
|
"target_field",
|
|
|
ALL_PROPERTIES,
|
|
@@ -162,7 +175,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
randomAlphaOfLength(10),
|
|
|
null,
|
|
|
"source_field",
|
|
|
- loader("/GeoLite2-City.mmdb"),
|
|
|
+ loader("GeoLite2-City.mmdb"),
|
|
|
() -> true,
|
|
|
"target_field",
|
|
|
ALL_PROPERTIES,
|
|
@@ -181,7 +194,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
randomAlphaOfLength(10),
|
|
|
null,
|
|
|
"source_field",
|
|
|
- loader("/GeoLite2-City.mmdb"),
|
|
|
+ loader("GeoLite2-City.mmdb"),
|
|
|
() -> true,
|
|
|
"target_field",
|
|
|
ALL_PROPERTIES,
|
|
@@ -220,7 +233,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
randomAlphaOfLength(10),
|
|
|
null,
|
|
|
"source_field",
|
|
|
- loader("/GeoLite2-City.mmdb"),
|
|
|
+ loader("GeoLite2-City.mmdb"),
|
|
|
() -> true,
|
|
|
"target_field",
|
|
|
ALL_PROPERTIES,
|
|
@@ -246,7 +259,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
randomAlphaOfLength(10),
|
|
|
null,
|
|
|
"source_field",
|
|
|
- loader("/GeoLite2-Country.mmdb"),
|
|
|
+ loader("GeoLite2-Country.mmdb"),
|
|
|
() -> true,
|
|
|
"target_field",
|
|
|
ALL_PROPERTIES,
|
|
@@ -276,7 +289,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
randomAlphaOfLength(10),
|
|
|
null,
|
|
|
"source_field",
|
|
|
- loader("/GeoLite2-Country.mmdb"),
|
|
|
+ loader("GeoLite2-Country.mmdb"),
|
|
|
() -> true,
|
|
|
"target_field",
|
|
|
ALL_PROPERTIES,
|
|
@@ -303,7 +316,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
randomAlphaOfLength(10),
|
|
|
null,
|
|
|
"source_field",
|
|
|
- loader("/GeoLite2-ASN.mmdb"),
|
|
|
+ loader("GeoLite2-ASN.mmdb"),
|
|
|
() -> true,
|
|
|
"target_field",
|
|
|
ALL_PROPERTIES,
|
|
@@ -333,7 +346,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
randomAlphaOfLength(10),
|
|
|
null,
|
|
|
"source_field",
|
|
|
- loader("/GeoIP2-Anonymous-IP-Test.mmdb"),
|
|
|
+ loader("GeoIP2-Anonymous-IP-Test.mmdb"),
|
|
|
() -> true,
|
|
|
"target_field",
|
|
|
ALL_PROPERTIES,
|
|
@@ -366,7 +379,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
randomAlphaOfLength(10),
|
|
|
null,
|
|
|
"source_field",
|
|
|
- loader("/GeoIP2-Connection-Type-Test.mmdb"),
|
|
|
+ loader("GeoIP2-Connection-Type-Test.mmdb"),
|
|
|
() -> true,
|
|
|
"target_field",
|
|
|
ALL_PROPERTIES,
|
|
@@ -394,7 +407,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
randomAlphaOfLength(10),
|
|
|
null,
|
|
|
"source_field",
|
|
|
- loader("/GeoIP2-Domain-Test.mmdb"),
|
|
|
+ loader("GeoIP2-Domain-Test.mmdb"),
|
|
|
() -> true,
|
|
|
"target_field",
|
|
|
ALL_PROPERTIES,
|
|
@@ -422,7 +435,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
randomAlphaOfLength(10),
|
|
|
null,
|
|
|
"source_field",
|
|
|
- loader("/GeoIP2-Enterprise-Test.mmdb"),
|
|
|
+ loader("GeoIP2-Enterprise-Test.mmdb"),
|
|
|
() -> true,
|
|
|
"target_field",
|
|
|
ALL_PROPERTIES,
|
|
@@ -475,7 +488,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
randomAlphaOfLength(10),
|
|
|
null,
|
|
|
"source_field",
|
|
|
- loader("/GeoIP2-ISP-Test.mmdb"),
|
|
|
+ loader("GeoIP2-ISP-Test.mmdb"),
|
|
|
() -> true,
|
|
|
"target_field",
|
|
|
ALL_PROPERTIES,
|
|
@@ -508,7 +521,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
randomAlphaOfLength(10),
|
|
|
null,
|
|
|
"source_field",
|
|
|
- loader("/GeoLite2-City.mmdb"),
|
|
|
+ loader("GeoLite2-City.mmdb"),
|
|
|
() -> true,
|
|
|
"target_field",
|
|
|
ALL_PROPERTIES,
|
|
@@ -532,7 +545,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
randomAlphaOfLength(10),
|
|
|
null,
|
|
|
"source_field",
|
|
|
- loader("/GeoLite2-City.mmdb"),
|
|
|
+ loader("GeoLite2-City.mmdb"),
|
|
|
() -> true,
|
|
|
"target_field",
|
|
|
ALL_PROPERTIES,
|
|
@@ -553,7 +566,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
randomAlphaOfLength(10),
|
|
|
null,
|
|
|
"source_field",
|
|
|
- loader("/GeoLite2-City.mmdb"),
|
|
|
+ loader("GeoLite2-City.mmdb"),
|
|
|
() -> true,
|
|
|
"target_field",
|
|
|
ALL_PROPERTIES,
|
|
@@ -583,7 +596,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
randomAlphaOfLength(10),
|
|
|
null,
|
|
|
"source_field",
|
|
|
- loader("/GeoLite2-City.mmdb"),
|
|
|
+ loader("GeoLite2-City.mmdb"),
|
|
|
() -> true,
|
|
|
"target_field",
|
|
|
ALL_PROPERTIES,
|
|
@@ -613,7 +626,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
randomAlphaOfLength(10),
|
|
|
null,
|
|
|
"source_field",
|
|
|
- loader("/GeoLite2-City.mmdb"),
|
|
|
+ loader("GeoLite2-City.mmdb"),
|
|
|
() -> true,
|
|
|
"target_field",
|
|
|
ALL_PROPERTIES,
|
|
@@ -632,7 +645,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
|
|
|
public void testListDatabaseReferenceCounting() throws Exception {
|
|
|
AtomicBoolean closeCheck = new AtomicBoolean(false);
|
|
|
- var loader = loader("/GeoLite2-City.mmdb", closeCheck);
|
|
|
+ var loader = loader("GeoLite2-City.mmdb", closeCheck);
|
|
|
GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field", () -> {
|
|
|
loader.preLookup();
|
|
|
return loader;
|
|
@@ -664,7 +677,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
randomAlphaOfLength(10),
|
|
|
null,
|
|
|
"source_field",
|
|
|
- loader("/GeoLite2-City.mmdb"),
|
|
|
+ loader("GeoLite2-City.mmdb"),
|
|
|
() -> true,
|
|
|
"target_field",
|
|
|
ALL_PROPERTIES,
|
|
@@ -692,7 +705,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
randomAlphaOfLength(10),
|
|
|
null,
|
|
|
"source_field",
|
|
|
- loader("/GeoLite2-City.mmdb"),
|
|
|
+ loader("GeoLite2-City.mmdb"),
|
|
|
() -> true,
|
|
|
"target_field",
|
|
|
ALL_PROPERTIES,
|
|
@@ -714,7 +727,7 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
randomAlphaOfLength(10),
|
|
|
null,
|
|
|
"source_field",
|
|
|
- loader("/GeoLite2-City.mmdb"),
|
|
|
+ loader("GeoLite2-City.mmdb"),
|
|
|
() -> false,
|
|
|
"target_field",
|
|
|
ALL_PROPERTIES,
|
|
@@ -782,32 +795,12 @@ public class GeoIpProcessorTests extends ESTestCase {
|
|
|
return () -> loader;
|
|
|
}
|
|
|
|
|
|
- private DatabaseReaderLazyLoader loader(final String path, final AtomicBoolean closed) {
|
|
|
- final Supplier<InputStream> databaseInputStreamSupplier = () -> GeoIpProcessor.class.getResourceAsStream(path);
|
|
|
- final CheckedSupplier<DatabaseReader, IOException> loader = () -> new DatabaseReader.Builder(databaseInputStreamSupplier.get())
|
|
|
- .build();
|
|
|
- final GeoIpCache cache = new GeoIpCache(1000);
|
|
|
- return new DatabaseReaderLazyLoader(cache, PathUtils.get(path), null, loader) {
|
|
|
-
|
|
|
- @Override
|
|
|
- long databaseFileSize() throws IOException {
|
|
|
- try (InputStream is = databaseInputStreamSupplier.get()) {
|
|
|
- long bytesRead = 0;
|
|
|
- do {
|
|
|
- final byte[] bytes = new byte[1 << 10];
|
|
|
- final int read = is.read(bytes);
|
|
|
- if (read == -1) break;
|
|
|
- bytesRead += read;
|
|
|
- } while (true);
|
|
|
- return bytesRead;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- InputStream databaseInputStream() {
|
|
|
- return databaseInputStreamSupplier.get();
|
|
|
- }
|
|
|
+ private DatabaseReaderLazyLoader loader(final String databaseName, final AtomicBoolean closed) {
|
|
|
+ Path path = tmpDir.resolve(databaseName);
|
|
|
+ copyDatabase(databaseName, path);
|
|
|
|
|
|
+ final GeoIpCache cache = new GeoIpCache(1000);
|
|
|
+ return new DatabaseReaderLazyLoader(cache, path, null) {
|
|
|
@Override
|
|
|
protected void doClose() throws IOException {
|
|
|
if (closed != null) {
|