Przeglądaj źródła

Slightly better geoip databaseType validation (#106889)

Joe Gallo 1 rok temu
rodzic
commit
1c35baa603

+ 5 - 0
docs/changelog/106889.yaml

@@ -0,0 +1,5 @@
+pr: 106889
+summary: Slightly better geoip `databaseType` validation
+area: Ingest Node
+type: bug
+issues: []

+ 17 - 10
modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/GeoIpProcessor.java

@@ -175,10 +175,7 @@ public final class GeoIpProcessor extends AbstractProcessor {
         } else if (databaseType.endsWith(ASN_DB_SUFFIX)) {
             geoData = retrieveAsnGeoData(geoIpDatabase, ipAddress);
         } else {
-            throw new ElasticsearchParseException(
-                "Unsupported database type [" + geoIpDatabase.getDatabaseType() + "]",
-                new IllegalStateException()
-            );
+            throw new ElasticsearchParseException("Unsupported database type [" + databaseType + "]", new IllegalStateException());
         }
         return geoData;
     }
@@ -440,12 +437,24 @@ public final class GeoIpProcessor extends AbstractProcessor {
                 // pipeline.
                 return new DatabaseUnavailableProcessor(processorTag, description, databaseFile);
             }
+
             final String databaseType;
             try {
                 databaseType = geoIpDatabase.getDatabaseType();
             } finally {
                 geoIpDatabase.release();
             }
+            if (databaseType == null
+                || (databaseType.endsWith(CITY_DB_SUFFIX)
+                    || databaseType.endsWith(COUNTRY_DB_SUFFIX)
+                    || databaseType.endsWith(ASN_DB_SUFFIX)) == false) {
+                throw newConfigurationException(
+                    TYPE,
+                    processorTag,
+                    "database_file",
+                    "Unsupported database type [" + databaseType + "] for file [" + databaseFile + "]"
+                );
+            }
 
             final Set<Property> properties;
             if (propertyNames != null) {
@@ -466,12 +475,8 @@ public final class GeoIpProcessor extends AbstractProcessor {
                 } else if (databaseType.endsWith(ASN_DB_SUFFIX)) {
                     properties = DEFAULT_ASN_PROPERTIES;
                 } else {
-                    throw newConfigurationException(
-                        TYPE,
-                        processorTag,
-                        "database_file",
-                        "Unsupported database type [" + databaseType + "]"
-                    );
+                    assert false : "unsupported database type [" + databaseType + "]";
+                    properties = Set.of();
                 }
             }
             return new GeoIpProcessor(
@@ -545,6 +550,8 @@ public final class GeoIpProcessor extends AbstractProcessor {
                 validProperties = ALL_COUNTRY_PROPERTIES;
             } else if (databaseType.endsWith(ASN_DB_SUFFIX)) {
                 validProperties = ALL_ASN_PROPERTIES;
+            } else {
+                assert false : "unsupported database type [" + databaseType + "]";
             }
 
             try {

+ 36 - 0
modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/GeoIpProcessorFactoryTests.java

@@ -50,6 +50,7 @@ import static org.hamcrest.Matchers.hasEntry;
 import static org.hamcrest.Matchers.instanceOf;
 import static org.hamcrest.Matchers.nullValue;
 import static org.hamcrest.Matchers.sameInstance;
+import static org.mockito.ArgumentMatchers.anyString;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
@@ -287,6 +288,41 @@ public class GeoIpProcessorFactoryTests extends ESTestCase {
         assertThat(e.getMessage(), equalTo("[properties] property isn't a list, but of type [java.lang.String]"));
     }
 
+    public void testBuildUnsupportedDatabase() throws Exception {
+        // mock up some unsupported database (it has a databaseType that we don't recognize)
+        GeoIpDatabase database = mock(GeoIpDatabase.class);
+        when(database.getDatabaseType()).thenReturn("some-unsupported-database");
+        GeoIpDatabaseProvider provider = mock(GeoIpDatabaseProvider.class);
+        when(provider.getDatabase(anyString())).thenReturn(database);
+
+        GeoIpProcessor.Factory factory = new GeoIpProcessor.Factory(provider);
+
+        Map<String, Object> config1 = new HashMap<>();
+        config1.put("field", "_field");
+        config1.put("properties", List.of("ip"));
+        Exception e = expectThrows(ElasticsearchParseException.class, () -> factory.create(null, null, null, config1));
+        assertThat(
+            e.getMessage(),
+            equalTo("[database_file] Unsupported database type [some-unsupported-database] for file [GeoLite2-City.mmdb]")
+        );
+    }
+
+    public void testBuildNullDatabase() throws Exception {
+        // mock up a provider that returns a null databaseType
+        GeoIpDatabase database = mock(GeoIpDatabase.class);
+        when(database.getDatabaseType()).thenReturn(null);
+        GeoIpDatabaseProvider provider = mock(GeoIpDatabaseProvider.class);
+        when(provider.getDatabase(anyString())).thenReturn(database);
+
+        GeoIpProcessor.Factory factory = new GeoIpProcessor.Factory(provider);
+
+        Map<String, Object> config1 = new HashMap<>();
+        config1.put("field", "_field");
+        config1.put("properties", List.of("ip"));
+        Exception e = expectThrows(ElasticsearchParseException.class, () -> factory.create(null, null, null, config1));
+        assertThat(e.getMessage(), equalTo("[database_file] Unsupported database type [null] for file [GeoLite2-City.mmdb]"));
+    }
+
     @SuppressWarnings("HiddenField")
     public void testLazyLoading() throws Exception {
         final Path configDir = createTempDir();