Skip to content

Commit f842ff1

Browse files
sneivandtjpountz
authored andcommitted
Simple verification of the format of the language tag used in DateProcessor. (#25513)
Closes #26186
1 parent a827d54 commit f842ff1

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

core/src/main/java/org/elasticsearch/common/util/LocaleUtils.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,17 @@ private static Locale parseParts(String[] parts) {
101101
return new Locale(parts[0]);
102102
default:
103103
throw new IllegalArgumentException("Locales can have at most 3 parts but got " + parts.length + ": " + Arrays.asList(parts));
104-
}
104+
}
105105
}
106106

107+
/**
108+
* Validate a {@link Locale} object
109+
*/
110+
public static boolean isValid(Locale locale) {
111+
try {
112+
return locale.getISO3Language() != null && locale.getISO3Country() != null;
113+
} catch (MissingResourceException e) {
114+
return false;
115+
}
116+
}
107117
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.elasticsearch.ingest.common;
2121

2222
import org.elasticsearch.ExceptionsHelper;
23+
import org.elasticsearch.common.util.LocaleUtils;
2324
import org.elasticsearch.ingest.AbstractProcessor;
2425
import org.elasticsearch.ingest.ConfigurationUtils;
2526
import org.elasticsearch.ingest.IngestDocument;
@@ -125,6 +126,9 @@ public DateProcessor create(Map<String, Processor.Factory> registry, String proc
125126
} catch (IllformedLocaleException e) {
126127
throw new IllegalArgumentException("Invalid language tag specified: " + localeString);
127128
}
129+
if (!LocaleUtils.isValid(locale)) {
130+
throw new IllegalArgumentException("Invalid language tag specified: " + localeString);
131+
}
128132
}
129133
List<String> formats = ConfigurationUtils.readList(TYPE, processorTag, config, "formats");
130134
return new DateProcessor(processorTag, timezone, locale, field, formats, targetField);

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,20 @@ public void testParseLocale() throws Exception {
9595
}
9696

9797
public void testParseInvalidLocale() throws Exception {
98-
DateProcessor.Factory factory = new DateProcessor.Factory();
99-
Map<String, Object> config = new HashMap<>();
100-
String sourceField = randomAlphaOfLengthBetween(1, 10);
101-
config.put("field", sourceField);
102-
config.put("formats", Collections.singletonList("dd/MM/yyyyy"));
103-
config.put("locale", "invalid_locale");
104-
try {
105-
factory.create(null, null, config);
106-
fail("should fail with invalid locale");
107-
} catch (IllegalArgumentException e) {
108-
assertThat(e.getMessage(), equalTo("Invalid language tag specified: invalid_locale"));
98+
String[] locales = new String[] { "invalid_locale", "english" };
99+
for (String locale : locales) {
100+
DateProcessor.Factory factory = new DateProcessor.Factory();
101+
Map<String, Object> config = new HashMap<>();
102+
String sourceField = randomAlphaOfLengthBetween(1, 10);
103+
config.put("field", sourceField);
104+
config.put("formats", Collections.singletonList("dd/MM/yyyyy"));
105+
config.put("locale", locale);
106+
try {
107+
factory.create(null, null, config);
108+
fail("should fail with invalid locale");
109+
} catch(IllegalArgumentException e) {
110+
assertThat(e.getMessage(), equalTo("Invalid language tag specified: " + locale));
111+
}
109112
}
110113
}
111114

0 commit comments

Comments
 (0)