From 8f6d9df96e386108cc362e4704f6d41f28bee165 Mon Sep 17 00:00:00 2001 From: David Kyle Date: Tue, 25 Jul 2017 10:41:51 +0100 Subject: [PATCH] [ML] Check influencer names are valid (elastic/x-pack-elasticsearch#2073) Original commit: elastic/x-pack-elasticsearch@75869cacb3ae2c7e8242450e4dabb2a6833a6941 --- .../xpack/ml/job/config/AnalysisConfig.java | 13 ++++++++++++- .../xpack/ml/job/messages/Messages.java | 3 ++- .../xpack/ml/job/config/AnalysisConfigTests.java | 14 ++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/AnalysisConfig.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/AnalysisConfig.java index 175f3108e35..e3f6b0be735 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/AnalysisConfig.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/AnalysisConfig.java @@ -36,7 +36,6 @@ import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; import java.util.stream.Collectors; - /** * Autodetect analysis configuration options describes which fields are * analysed and the functions to use. @@ -558,6 +557,7 @@ public class AnalysisConfig implements ToXContentObject, Writeable { } verifyNoInconsistentNestedFieldNames(); + verifyInfluencerNames(); return new AnalysisConfig(bucketSpan, categorizationFieldName, categorizationFilters, latency, summaryCountFieldName, detectors, influencers, overlappingBuckets, @@ -675,6 +675,17 @@ public class AnalysisConfig implements ToXContentObject, Writeable { } } + private void verifyInfluencerNames() { + for (String influencer : influencers) { + if (influencer == null || influencer.isEmpty()) { + throw ExceptionsHelper.badRequestException( + Messages.getMessage(Messages.JOB_CONFIG_INFLUENCER_CANNOT_BE_EMPTY)); + } + + Detector.Builder.verifyFieldName(influencer); + } + } + private static void checkDetectorsHavePartitionFields(List detectors) { for (Detector detector : detectors) { if (!Strings.isNullOrEmpty(detector.getPartitionFieldName())) { diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/messages/Messages.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/messages/Messages.java index 7f5ba7ccc82..9bb68d5d4eb 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/messages/Messages.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/messages/Messages.java @@ -101,10 +101,11 @@ public final class Messages { public static final String JOB_CONFIG_FUNCTION_REQUIRES_OVERFIELD = "over_field_name must be set when the ''{0}'' function is used"; public static final String JOB_CONFIG_ID_ALREADY_TAKEN = "The job cannot be created with the Id ''{0}''. The Id is already used."; public static final String JOB_CONFIG_ID_TOO_LONG = "The job id cannot contain more than {0,number,integer} characters."; + public static final String JOB_CONFIG_INFLUENCER_CANNOT_BE_EMPTY = "Influencer names cannot be empty strings"; public static final String JOB_CONFIG_INVALID_CREATE_SETTINGS = "The job is configured with fields [{0}] that are illegal to set at job creation"; public static final String JOB_CONFIG_INVALID_FIELDNAME_CHARS = - "Invalid field name ''{0}''. Field names including over, by and partition fields cannot contain any of these characters: {1}"; + "Invalid field name ''{0}''. Field names cannot contain any of these characters: {1}"; public static final String JOB_CONFIG_INVALID_FIELDNAME = "Invalid field name ''{0}''. Field names including over, by and partition fields cannot be ''{1}''"; public static final String JOB_CONFIG_INVALID_TIMEFORMAT = "Invalid Time format string ''{0}''"; diff --git a/plugin/src/test/java/org/elasticsearch/xpack/ml/job/config/AnalysisConfigTests.java b/plugin/src/test/java/org/elasticsearch/xpack/ml/job/config/AnalysisConfigTests.java index 667629e27b9..bdb2e57a208 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/ml/job/config/AnalysisConfigTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/ml/job/config/AnalysisConfigTests.java @@ -790,6 +790,20 @@ public class AnalysisConfigTests extends AbstractSerializingTestCase