From 9c3ebb83a7599f863a9d864c96bf8486973441b6 Mon Sep 17 00:00:00 2001 From: Robert Muir Date: Thu, 7 Jan 2016 23:12:44 -0500 Subject: [PATCH] Don't do DNS lookups from GeoIpProcessor There is no need to involve DNS in this! --- .../ingest/processor/GeoIpProcessor.java | 9 ++------- .../ingest/processor/GeoIpProcessorTests.java | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/plugins/ingest/src/main/java/org/elasticsearch/ingest/processor/GeoIpProcessor.java b/plugins/ingest/src/main/java/org/elasticsearch/ingest/processor/GeoIpProcessor.java index 445853dccb3..8a192000714 100644 --- a/plugins/ingest/src/main/java/org/elasticsearch/ingest/processor/GeoIpProcessor.java +++ b/plugins/ingest/src/main/java/org/elasticsearch/ingest/processor/GeoIpProcessor.java @@ -30,6 +30,7 @@ import com.maxmind.geoip2.record.Location; import com.maxmind.geoip2.record.Subdivision; import org.apache.lucene.util.IOUtils; import org.elasticsearch.SpecialPermission; +import org.elasticsearch.common.network.InetAddresses; import org.elasticsearch.common.network.NetworkAddress; import org.elasticsearch.ingest.core.IngestDocument; import org.elasticsearch.ingest.core.Processor; @@ -38,7 +39,6 @@ import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.net.InetAddress; -import java.net.UnknownHostException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.PathMatcher; @@ -78,12 +78,7 @@ public final class GeoIpProcessor implements Processor { @Override public void execute(IngestDocument ingestDocument) { String ip = ingestDocument.getFieldValue(sourceField, String.class); - final InetAddress ipAddress; - try { - ipAddress = InetAddress.getByName(ip); - } catch (UnknownHostException e) { - throw new RuntimeException(e); - } + final InetAddress ipAddress = InetAddresses.forString(ip); Map geoData; switch (dbReader.getMetadata().getDatabaseType()) { diff --git a/plugins/ingest/src/test/java/org/elasticsearch/ingest/processor/GeoIpProcessorTests.java b/plugins/ingest/src/test/java/org/elasticsearch/ingest/processor/GeoIpProcessorTests.java index 818e9054749..ee09354de8f 100644 --- a/plugins/ingest/src/test/java/org/elasticsearch/ingest/processor/GeoIpProcessorTests.java +++ b/plugins/ingest/src/test/java/org/elasticsearch/ingest/processor/GeoIpProcessorTests.java @@ -30,6 +30,7 @@ import java.util.EnumSet; import java.util.HashMap; import java.util.Map; +import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; public class GeoIpProcessorTests extends ESTestCase { @@ -91,4 +92,21 @@ public class GeoIpProcessorTests extends ESTestCase { assertThat(geoData.size(), equalTo(0)); } + /** Don't silently do DNS lookups or anything trappy on bogus data */ + public void testInvalid() throws Exception { + InputStream database = GeoIpProcessor.class.getResourceAsStream("/GeoLite2-City.mmdb"); + GeoIpProcessor processor = new GeoIpProcessor("source_field", new DatabaseReader.Builder(database).build(), "target_field", EnumSet.allOf(GeoIpProcessor.Field.class)); + + Map document = new HashMap<>(); + document.put("source_field", "www.google.com"); + IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document); + try { + processor.execute(ingestDocument); + fail("did not get expected exception"); + } catch (IllegalArgumentException expected) { + assertNotNull(expected.getMessage()); + assertThat(expected.getMessage(), containsString("not an IP string literal")); + } + } + }