From f421ec7812036162a2fff04661c1b724bfb03979 Mon Sep 17 00:00:00 2001 From: Dimitris Athanasiou Date: Thu, 13 Jun 2019 19:52:39 +0300 Subject: [PATCH 1/4] [FEATURE][ML] Stregthen source dest validations for DF analytics This adds the following validations: - dest index name is valid - source index exists - dest index is not included in source index - dest index is matching a single index at most --- .../ml/dataframe/DataFrameAnalyticsDest.java | 13 ++ .../DataFrameAnalyticsDestTests.java | 19 ++ .../ml/qa/ml-with-security/build.gradle | 6 + .../TransportPutDataFrameAnalyticsAction.java | 12 +- ...ransportStartDataFrameAnalyticsAction.java | 6 +- .../ml/dataframe/SourceDestValidator.java | 65 +++++++ .../dataframe/SourceDestValidatorTests.java | 176 ++++++++++++++++++ .../test/ml/data_frame_analytics_crud.yml | 153 ++++++++++++++- .../test/ml/start_data_frame_analytics.yml | 10 +- 9 files changed, 451 insertions(+), 9 deletions(-) create mode 100644 x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/SourceDestValidator.java create mode 100644 x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/SourceDestValidatorTests.java diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsDest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsDest.java index 98f1bdeb19189..3bc435336f062 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsDest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsDest.java @@ -13,11 +13,15 @@ import org.elasticsearch.common.xcontent.ConstructingObjectParser; import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.indices.InvalidIndexNameException; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; import java.io.IOException; +import java.util.Locale; import java.util.Objects; +import static org.elasticsearch.cluster.metadata.MetaDataCreateIndexService.validateIndexOrAliasName; + public class DataFrameAnalyticsDest implements Writeable, ToXContentObject { public static final ParseField INDEX = new ParseField("index"); @@ -90,4 +94,13 @@ public String getIndex() { public String getResultsField() { return resultsField; } + + public void validate() { + if (index != null) { + validateIndexOrAliasName(index, InvalidIndexNameException::new); + if (index.toLowerCase(Locale.ROOT).equals(index) == false) { + throw new InvalidIndexNameException(index, "dest.index must be lowercase"); + } + } + } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsDestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsDestTests.java index 7332687723805..bf8ce4c8a99b0 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsDestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsDestTests.java @@ -7,10 +7,14 @@ import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.indices.InvalidIndexNameException; +import org.elasticsearch.rest.RestStatus; import org.elasticsearch.test.AbstractSerializingTestCase; import java.io.IOException; +import static org.hamcrest.Matchers.equalTo; + public class DataFrameAnalyticsDestTests extends AbstractSerializingTestCase { @Override @@ -33,4 +37,19 @@ public static DataFrameAnalyticsDest createRandom() { protected Writeable.Reader instanceReader() { return DataFrameAnalyticsDest::new; } + + public void testValidate_GivenIndexWithFunkyChars() { + expectThrows(InvalidIndexNameException.class, () -> new DataFrameAnalyticsDest("