[ML] Check influencer names are valid (elastic/x-pack-elasticsearch#2073)

Original commit: elastic/x-pack-elasticsearch@75869cacb3
This commit is contained in:
David Kyle 2017-07-25 10:41:51 +01:00 committed by GitHub
parent 9ab6d3cbc3
commit 8f6d9df96e
3 changed files with 28 additions and 2 deletions

View File

@ -36,7 +36,6 @@ import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException; import java.util.regex.PatternSyntaxException;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
* Autodetect analysis configuration options describes which fields are * Autodetect analysis configuration options describes which fields are
* analysed and the functions to use. * analysed and the functions to use.
@ -558,6 +557,7 @@ public class AnalysisConfig implements ToXContentObject, Writeable {
} }
verifyNoInconsistentNestedFieldNames(); verifyNoInconsistentNestedFieldNames();
verifyInfluencerNames();
return new AnalysisConfig(bucketSpan, categorizationFieldName, categorizationFilters, return new AnalysisConfig(bucketSpan, categorizationFieldName, categorizationFilters,
latency, summaryCountFieldName, detectors, influencers, overlappingBuckets, 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<Detector> detectors) { private static void checkDetectorsHavePartitionFields(List<Detector> detectors) {
for (Detector detector : detectors) { for (Detector detector : detectors) {
if (!Strings.isNullOrEmpty(detector.getPartitionFieldName())) { if (!Strings.isNullOrEmpty(detector.getPartitionFieldName())) {

View File

@ -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_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_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_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 = public static final String JOB_CONFIG_INVALID_CREATE_SETTINGS =
"The job is configured with fields [{0}] that are illegal to set at job creation"; "The job is configured with fields [{0}] that are illegal to set at job creation";
public static final String JOB_CONFIG_INVALID_FIELDNAME_CHARS = 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 = public static final String JOB_CONFIG_INVALID_FIELDNAME =
"Invalid field name ''{0}''. Field names including over, by and partition fields cannot be ''{1}''"; "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}''"; public static final String JOB_CONFIG_INVALID_TIMEFORMAT = "Invalid Time format string ''{0}''";

View File

@ -790,6 +790,20 @@ public class AnalysisConfigTests extends AbstractSerializingTestCase<AnalysisCon
assertEquals(Messages.getMessage(Messages.JOB_CONFIG_PER_PARTITION_NORMALIZATION_CANNOT_USE_INFLUENCERS), e.getMessage()); assertEquals(Messages.getMessage(Messages.JOB_CONFIG_PER_PARTITION_NORMALIZATION_CANNOT_USE_INFLUENCERS), e.getMessage());
} }
public void testVerifyInfluencerNames() {
AnalysisConfig.Builder config = createValidConfig();
Detector.Builder builder = new Detector.Builder(config.build().getDetectors().get(0));
config.build().getDetectors().set(0, builder.build());
config.setInfluencers(Arrays.asList("inf1", ""));
ElasticsearchException e = ESTestCase.expectThrows(ElasticsearchException.class, config::build);
assertEquals("Influencer names cannot be empty strings", e.getMessage());
config.setInfluencers(Arrays.asList("invalid\\backslash"));
e = ESTestCase.expectThrows(ElasticsearchException.class, config::build);
assertEquals("Invalid field name 'invalid\\backslash'. Field names cannot contain any of these characters: \",\\", e.getMessage());
}
public void testVerify_GivenCategorizationFiltersContainInvalidRegex() { public void testVerify_GivenCategorizationFiltersContainInvalidRegex() {
AnalysisConfig.Builder config = createValidCategorizationConfig(); AnalysisConfig.Builder config = createValidCategorizationConfig();
config.setCategorizationFilters(Arrays.asList("foo", "(")); config.setCategorizationFilters(Arrays.asList("foo", "("));