Skip to content

Commit a1c9def

Browse files
authored
[Rollup] Disallow index patterns that match the rollup index (#30491)
We should not allow the user to configure index patterns that also match the index which stores the rollup index. For example, it is quite natural for a user to specify `metricbeat-*` as the index pattern, and then store the rollups in `metricbeat-rolled`. This will start throwing errors as soon as the rollup index is created because the indexer will try to search it. Note: this does not prevent the user from matching against existing rollup indices. That should be prevented by the field-level validation during job creation.
1 parent 05ee0f8 commit a1c9def

File tree

5 files changed

+49
-6
lines changed

5 files changed

+49
-6
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/RollupJobConfig.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.elasticsearch.common.io.stream.StreamInput;
1414
import org.elasticsearch.common.io.stream.StreamOutput;
1515
import org.elasticsearch.common.io.stream.Writeable;
16+
import org.elasticsearch.common.regex.Regex;
1617
import org.elasticsearch.common.unit.TimeValue;
1718
import org.elasticsearch.common.xcontent.ObjectParser;
1819
import org.elasticsearch.common.xcontent.ToXContent;
@@ -173,7 +174,7 @@ public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params par
173174
builder.endObject();
174175
return builder;
175176
}
176-
177+
177178
@Override
178179
public void writeTo(StreamOutput out) throws IOException {
179180
out.writeString(id);
@@ -336,6 +337,17 @@ public RollupJobConfig build() {
336337
if (indexPattern == null || indexPattern.isEmpty()) {
337338
throw new IllegalArgumentException("An index pattern is mandatory.");
338339
}
340+
if (Regex.isMatchAllPattern(indexPattern)) {
341+
throw new IllegalArgumentException("Index pattern must not match all indices (as it would match it's own rollup index");
342+
}
343+
if (Regex.isSimpleMatchPattern(indexPattern)) {
344+
if (Regex.simpleMatch(indexPattern, rollupIndex)) {
345+
throw new IllegalArgumentException("Index pattern would match rollup index name which is not allowed.");
346+
}
347+
}
348+
if (indexPattern.equals(rollupIndex)) {
349+
throw new IllegalArgumentException("Rollup index may not be the same as the index pattern.");
350+
}
339351
if (rollupIndex == null || rollupIndex.isEmpty()) {
340352
throw new IllegalArgumentException("A rollup index name is mandatory.");
341353
}

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/ConfigTestHelpers.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ public static RollupJobConfig.Builder getRollupJob(String jobId) {
2727
builder.setId(jobId);
2828
builder.setCron(getCronString());
2929
builder.setTimeout(new TimeValue(ESTestCase.randomIntBetween(1,100)));
30-
builder.setIndexPattern(ESTestCase.randomAlphaOfLengthBetween(1,10));
31-
builder.setRollupIndex(ESTestCase.randomAlphaOfLengthBetween(1,10));
30+
String indexPattern = ESTestCase.randomAlphaOfLengthBetween(1,10);
31+
builder.setIndexPattern(indexPattern);
32+
builder.setRollupIndex("rollup_" + indexPattern); // to ensure the index pattern != rollup index
3233
builder.setGroupConfig(ConfigTestHelpers.getGroupConfig().build());
3334
builder.setPageSize(ESTestCase.randomIntBetween(1,10));
3435
if (ESTestCase.randomBoolean()) {

x-pack/plugin/rollup/src/main/java/org/elasticsearch/xpack/rollup/action/TransportPutRollupJobAction.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ protected void masterOperation(PutRollupJobAction.Request request, ClusterState
9595
XPackPlugin.checkReadyForXPackCustomMetadata(clusterState);
9696

9797
FieldCapabilitiesRequest fieldCapsRequest = new FieldCapabilitiesRequest()
98-
.indices(request.getConfig().getIndexPattern())
99-
.fields(request.getConfig().getAllFields().toArray(new String[0]));
98+
.indices(request.getConfig().getIndexPattern())
99+
.fields(request.getConfig().getAllFields().toArray(new String[0]));
100100

101101
client.fieldCaps(fieldCapsRequest, new ActionListener<FieldCapabilitiesResponse>() {
102102
@Override

x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/config/ConfigTests.java

+31
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,37 @@ public void testEmptyIndexPattern() {
122122
assertThat(e.getMessage(), equalTo("An index pattern is mandatory."));
123123
}
124124

125+
public void testMatchAllIndexPattern() {
126+
RollupJobConfig.Builder job = ConfigTestHelpers.getRollupJob("foo");
127+
job.setIndexPattern("*");
128+
Exception e = expectThrows(IllegalArgumentException.class, job::build);
129+
assertThat(e.getMessage(), equalTo("Index pattern must not match all indices (as it would match it's own rollup index"));
130+
}
131+
132+
public void testMatchOwnRollupPatternPrefix() {
133+
RollupJobConfig.Builder job = ConfigTestHelpers.getRollupJob("foo");
134+
job.setIndexPattern("foo-*");
135+
job.setRollupIndex("foo-rollup");
136+
Exception e = expectThrows(IllegalArgumentException.class, job::build);
137+
assertThat(e.getMessage(), equalTo("Index pattern would match rollup index name which is not allowed."));
138+
}
139+
140+
public void testMatchOwnRollupPatternSuffix() {
141+
RollupJobConfig.Builder job = ConfigTestHelpers.getRollupJob("foo");
142+
job.setIndexPattern("*-rollup");
143+
job.setRollupIndex("foo-rollup");
144+
Exception e = expectThrows(IllegalArgumentException.class, job::build);
145+
assertThat(e.getMessage(), equalTo("Index pattern would match rollup index name which is not allowed."));
146+
}
147+
148+
public void testIndexPatternIdenticalToRollup() {
149+
RollupJobConfig.Builder job = ConfigTestHelpers.getRollupJob("foo");
150+
job.setIndexPattern("foo");
151+
job.setRollupIndex("foo");
152+
Exception e = expectThrows(IllegalArgumentException.class, job::build);
153+
assertThat(e.getMessage(), equalTo("Rollup index may not be the same as the index pattern."));
154+
}
155+
125156
public void testEmptyRollupIndex() {
126157
RollupJobConfig.Builder job = ConfigTestHelpers.getRollupJob("foo");
127158
job.setRollupIndex("");

x-pack/plugin/src/test/resources/rest-api-spec/test/rollup/put_job.yml

-1
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,3 @@ setup:
188188
]
189189
}
190190
191-

0 commit comments

Comments
 (0)