Browse Source

Simple verification of the format of the language tag used in DateProcessor. (#25513)

Closes #26186
Stuart Neivandt 8 years ago
parent
commit
f842ff1ae1

+ 11 - 1
core/src/main/java/org/elasticsearch/common/util/LocaleUtils.java

@@ -101,7 +101,17 @@ public class LocaleUtils {
             return new Locale(parts[0]);
         default:
             throw new IllegalArgumentException("Locales can have at most 3 parts but got " + parts.length + ": " + Arrays.asList(parts));
-    }
+        }
     }
 
+    /**
+     * Validate a {@link Locale} object
+     */
+    public static boolean isValid(Locale locale) {
+        try {
+            return locale.getISO3Language() != null && locale.getISO3Country() != null;
+        } catch (MissingResourceException e) {
+            return false;
+        }
+    }
 }

+ 4 - 0
modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateProcessor.java

@@ -20,6 +20,7 @@
 package org.elasticsearch.ingest.common;
 
 import org.elasticsearch.ExceptionsHelper;
+import org.elasticsearch.common.util.LocaleUtils;
 import org.elasticsearch.ingest.AbstractProcessor;
 import org.elasticsearch.ingest.ConfigurationUtils;
 import org.elasticsearch.ingest.IngestDocument;
@@ -125,6 +126,9 @@ public final class DateProcessor extends AbstractProcessor {
                 } catch (IllformedLocaleException e) {
                     throw new IllegalArgumentException("Invalid language tag specified: " + localeString);
                 }
+                if (!LocaleUtils.isValid(locale)) {
+                    throw new IllegalArgumentException("Invalid language tag specified: " + localeString);
+                }
             }
             List<String> formats = ConfigurationUtils.readList(TYPE, processorTag, config, "formats");
             return new DateProcessor(processorTag, timezone, locale, field, formats, targetField);

+ 14 - 11
modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DateProcessorFactoryTests.java

@@ -95,17 +95,20 @@ public class DateProcessorFactoryTests extends ESTestCase {
     }
 
     public void testParseInvalidLocale() throws Exception {
-        DateProcessor.Factory factory = new DateProcessor.Factory();
-        Map<String, Object> config = new HashMap<>();
-        String sourceField = randomAlphaOfLengthBetween(1, 10);
-        config.put("field", sourceField);
-        config.put("formats", Collections.singletonList("dd/MM/yyyyy"));
-        config.put("locale", "invalid_locale");
-        try {
-            factory.create(null, null, config);
-            fail("should fail with invalid locale");
-        } catch (IllegalArgumentException e) {
-            assertThat(e.getMessage(), equalTo("Invalid language tag specified: invalid_locale"));
+        String[] locales = new String[] { "invalid_locale", "english" };
+        for (String locale : locales) {
+            DateProcessor.Factory factory = new DateProcessor.Factory();
+            Map<String, Object> config = new HashMap<>();
+            String sourceField = randomAlphaOfLengthBetween(1, 10);
+            config.put("field", sourceField);
+            config.put("formats", Collections.singletonList("dd/MM/yyyyy"));
+            config.put("locale", locale);
+            try {
+                factory.create(null, null, config);
+                fail("should fail with invalid locale");
+            } catch(IllegalArgumentException e) {
+                assertThat(e.getMessage(), equalTo("Invalid language tag specified: " + locale));
+            }
         }
     }