Search in sources :

Example 11 with GeoIpCache

use of org.opensearch.ingest.geoip.IngestGeoIpPlugin.GeoIpCache in project OpenSearch by opensearch-project.

the class GeoIpProcessorTests method testAsn.

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, new GeoIpCache(1000), false);
    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> geoData = (Map<String, Object>) ingestDocument.getSourceAndMetadata().get("target_field");
    assertThat(geoData.size(), equalTo(4));
    assertThat(geoData.get("ip"), equalTo(ip));
    assertThat(geoData.get("asn"), equalTo(1136));
    assertThat(geoData.get("organization_name"), equalTo("KPN B.V."));
    assertThat(geoData.get("network"), equalTo("82.168.0.0/14"));
}
Also used : HashMap(java.util.HashMap) IngestDocumentMatcher.assertIngestDocument(org.opensearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.opensearch.ingest.IngestDocument) Matchers.containsString(org.hamcrest.Matchers.containsString) HashMap(java.util.HashMap) Map(java.util.Map) GeoIpCache(org.opensearch.ingest.geoip.IngestGeoIpPlugin.GeoIpCache)

Example 12 with GeoIpCache

use of org.opensearch.ingest.geoip.IngestGeoIpPlugin.GeoIpCache in project OpenSearch by opensearch-project.

the class GeoIpProcessorTests method testInvalid.

/**
 * 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, new GeoIpCache(1000), false);
    Map<String, Object> document = new HashMap<>();
    document.put("source_field", "www.google.com");
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
    Exception e = expectThrows(IllegalArgumentException.class, () -> processor.execute(ingestDocument));
    assertThat(e.getMessage(), containsString("not an IP string literal"));
}
Also used : HashMap(java.util.HashMap) IngestDocumentMatcher.assertIngestDocument(org.opensearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.opensearch.ingest.IngestDocument) Matchers.containsString(org.hamcrest.Matchers.containsString) GeoIpCache(org.opensearch.ingest.geoip.IngestGeoIpPlugin.GeoIpCache) IOException(java.io.IOException)

Example 13 with GeoIpCache

use of org.opensearch.ingest.geoip.IngestGeoIpPlugin.GeoIpCache in project OpenSearch by opensearch-project.

the class GeoIpProcessorTests method testCity.

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, new GeoIpCache(1000), false);
    Map<String, Object> document = new HashMap<>();
    document.put("source_field", "8.8.8.8");
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
    processor.execute(ingestDocument);
    assertThat(ingestDocument.getSourceAndMetadata().get("source_field"), equalTo("8.8.8.8"));
    @SuppressWarnings("unchecked") Map<String, Object> geoData = (Map<String, Object>) ingestDocument.getSourceAndMetadata().get("target_field");
    assertThat(geoData.size(), equalTo(6));
    assertThat(geoData.get("ip"), equalTo("8.8.8.8"));
    assertThat(geoData.get("country_iso_code"), equalTo("US"));
    assertThat(geoData.get("country_name"), equalTo("United States"));
    assertThat(geoData.get("continent_name"), equalTo("North America"));
    assertThat(geoData.get("timezone"), equalTo("America/Chicago"));
    Map<String, Object> location = new HashMap<>();
    location.put("lat", 37.751d);
    location.put("lon", -97.822d);
    assertThat(geoData.get("location"), equalTo(location));
}
Also used : HashMap(java.util.HashMap) IngestDocumentMatcher.assertIngestDocument(org.opensearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.opensearch.ingest.IngestDocument) Matchers.containsString(org.hamcrest.Matchers.containsString) HashMap(java.util.HashMap) Map(java.util.Map) GeoIpCache(org.opensearch.ingest.geoip.IngestGeoIpPlugin.GeoIpCache)

Example 14 with GeoIpCache

use of org.opensearch.ingest.geoip.IngestGeoIpPlugin.GeoIpCache in project OpenSearch by opensearch-project.

the class GeoIpProcessorTests method testNullWithoutIgnoreMissing.

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, new GeoIpCache(1000), false);
    IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.singletonMap("source_field", null));
    IngestDocument ingestDocument = new IngestDocument(originalIngestDocument);
    Exception exception = expectThrows(Exception.class, () -> processor.execute(ingestDocument));
    assertThat(exception.getMessage(), equalTo("field [source_field] is null, cannot extract geoip information."));
}
Also used : IngestDocumentMatcher.assertIngestDocument(org.opensearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.opensearch.ingest.IngestDocument) GeoIpCache(org.opensearch.ingest.geoip.IngestGeoIpPlugin.GeoIpCache) IOException(java.io.IOException)

Example 15 with GeoIpCache

use of org.opensearch.ingest.geoip.IngestGeoIpPlugin.GeoIpCache in project OpenSearch by opensearch-project.

the class GeoIpProcessorTests method testCity_withIpV6.

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, new GeoIpCache(1000), false);
    String address = "2602:306:33d3:8000::3257:9652";
    Map<String, Object> document = new HashMap<>();
    document.put("source_field", address);
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
    processor.execute(ingestDocument);
    assertThat(ingestDocument.getSourceAndMetadata().get("source_field"), equalTo(address));
    @SuppressWarnings("unchecked") Map<String, Object> geoData = (Map<String, Object>) ingestDocument.getSourceAndMetadata().get("target_field");
    assertThat(geoData.size(), equalTo(9));
    assertThat(geoData.get("ip"), equalTo(address));
    assertThat(geoData.get("country_iso_code"), equalTo("US"));
    assertThat(geoData.get("country_name"), equalTo("United States"));
    assertThat(geoData.get("continent_name"), equalTo("North America"));
    assertThat(geoData.get("region_iso_code"), equalTo("US-FL"));
    assertThat(geoData.get("region_name"), equalTo("Florida"));
    assertThat(geoData.get("city_name"), equalTo("Homestead"));
    assertThat(geoData.get("timezone"), equalTo("America/New_York"));
    Map<String, Object> location = new HashMap<>();
    location.put("lat", 25.4573d);
    location.put("lon", -80.4572d);
    assertThat(geoData.get("location"), equalTo(location));
}
Also used : HashMap(java.util.HashMap) IngestDocumentMatcher.assertIngestDocument(org.opensearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.opensearch.ingest.IngestDocument) Matchers.containsString(org.hamcrest.Matchers.containsString) HashMap(java.util.HashMap) Map(java.util.Map) GeoIpCache(org.opensearch.ingest.geoip.IngestGeoIpPlugin.GeoIpCache)

Aggregations

GeoIpCache (org.opensearch.ingest.geoip.IngestGeoIpPlugin.GeoIpCache)33 HashMap (java.util.HashMap)26 Matchers.containsString (org.hamcrest.Matchers.containsString)26 IngestDocument (org.opensearch.ingest.IngestDocument)19 IngestDocumentMatcher.assertIngestDocument (org.opensearch.ingest.IngestDocumentMatcher.assertIngestDocument)17 Matchers.hasToString (org.hamcrest.Matchers.hasToString)13 Map (java.util.Map)9 IOException (java.io.IOException)7 OpenSearchParseException (org.opensearch.OpenSearchParseException)4 AbstractResponse (com.maxmind.geoip2.model.AbstractResponse)2 Path (java.nio.file.Path)2 List (java.util.List)2 Mockito.mock (org.mockito.Mockito.mock)2 InetAddresses (org.opensearch.common.network.InetAddresses)2 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)2 ArrayList (java.util.ArrayList)1