diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedConfig.java index 44151f992f75e..b1ed7f61ef576 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedConfig.java @@ -9,6 +9,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.ElasticsearchException; +import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.TransportVersion; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.support.IndicesOptions; @@ -21,6 +22,7 @@ import org.elasticsearch.core.TimeValue; import org.elasticsearch.core.Tuple; import org.elasticsearch.index.query.QueryBuilder; +import org.elasticsearch.rest.RestStatus; import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.AggregatorFactories; import org.elasticsearch.search.aggregations.bucket.composite.CompositeAggregationBuilder; @@ -1050,6 +1052,10 @@ public DatafeedConfig build() { if (indicesOptions == null) { indicesOptions = IndicesOptions.STRICT_EXPAND_OPEN_HIDDEN_FORBID_CLOSED; } + if (indicesOptions.resolveCrossProjectIndexExpression()) { + throw new ElasticsearchStatusException("Cross-project search is not enabled for Datafeeds", RestStatus.FORBIDDEN); + } + return new DatafeedConfig( id, jobId, diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedConfigBuilderTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedConfigBuilderTests.java index ac3565e3d3be6..800406adc2868 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedConfigBuilderTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedConfigBuilderTests.java @@ -6,12 +6,14 @@ */ package org.elasticsearch.xpack.core.ml.datafeed; +import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.rest.RestStatus; import org.elasticsearch.search.SearchModule; import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.AggregationBuilders; @@ -30,6 +32,7 @@ import static org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfigTests.randomStringList; import static org.elasticsearch.xpack.core.ml.utils.QueryProviderTests.createTestQueryProvider; +import static org.hamcrest.Matchers.equalTo; public class DatafeedConfigBuilderTests extends AbstractWireSerializingTestCase { @@ -147,4 +150,17 @@ protected Writeable.Reader instanceReader() { return DatafeedConfig.Builder::new; } + public void testResolveCrossProjectIsDisabled() { + var datafeedBuilder = createRandomizedDatafeedConfigBuilder("jobId", "datafeed-id", 3600000); + datafeedBuilder = datafeedBuilder.setIndicesOptions( + IndicesOptions.builder(datafeedBuilder.getIndicesOptions()) + .crossProjectModeOptions(new IndicesOptions.CrossProjectModeOptions(true)) + .build() + ); + + var actualException = assertThrows(ElasticsearchStatusException.class, datafeedBuilder::build); + assertThat(actualException.getMessage(), equalTo("Cross-project search is not enabled for Datafeeds")); + assertThat(actualException.status(), equalTo(RestStatus.FORBIDDEN)); + } + }