Skip to content

Commit d48ce12

Browse files
authored
Convert ILM and SLM histories into hidden indices (#51456)
Modifies SLM's and ILM's history indices to be hidden indices for added protection against accidental querying and deletion, and improves IndexTemplateRegistry to handle upgrading index templates. Also modifies the REST test cleanup to delete hidden indices.
1 parent bb2e04b commit d48ce12

File tree

18 files changed

+234
-99
lines changed

18 files changed

+234
-99
lines changed

server/src/main/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexService.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ public class MetaDataCreateIndexService {
120120
* These index patterns will be converted to hidden indices, at which point they should be removed from this list.
121121
*/
122122
private static final CharacterRunAutomaton DOT_INDICES_EXCLUSIONS = new CharacterRunAutomaton(Regex.simpleMatchToAutomaton(
123-
".slm-history-*",
124123
".watch-history-*",
125124
".ml-anomalies-*",
126125
".ml-notifications-*",

server/src/test/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexServiceTests.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,6 @@ public void testValidateDotIndex() {
641641
public void testIndexNameExclusionsList() {
642642
// this test case should be removed when DOT_INDICES_EXCLUSIONS is empty
643643
List<String> excludedNames = Arrays.asList(
644-
".slm-history-" + randomAlphaOfLength(5).toLowerCase(Locale.ROOT),
645644
".watch-history-" + randomAlphaOfLength(5).toLowerCase(Locale.ROOT),
646645
".ml-anomalies-" + randomAlphaOfLength(5).toLowerCase(Locale.ROOT),
647646
".ml-notifications-" + randomAlphaOfLength(5).toLowerCase(Locale.ROOT),

test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,8 +599,11 @@ private void wipeCluster() throws Exception {
599599
}
600600

601601
protected static void wipeAllIndices() throws IOException {
602+
boolean includeHidden = minimumNodeVersion().onOrAfter(Version.V_7_7_0);
602603
try {
603-
final Response response = adminClient().performRequest(new Request("DELETE", "*"));
604+
final Request deleteReq = new Request("DELETE", "*");
605+
deleteReq.addParameter("expand_wildcards", "open,closed" + (includeHidden ? ",hidden" : ""));
606+
final Response response = adminClient().performRequest(deleteReq);
604607
try (InputStream is = response.getEntity().getContent()) {
605608
assertTrue((boolean) XContentHelper.convertToMap(XContentType.JSON.xContent(), is, true).get("acknowledged"));
606609
}
@@ -1235,7 +1238,7 @@ protected static Version minimumNodeVersion() throws IOException {
12351238
final Request request = new Request("GET", "_nodes");
12361239
request.addParameter("filter_path", "nodes.*.version");
12371240

1238-
final Response response = client().performRequest(request);
1241+
final Response response = adminClient().performRequest(request);
12391242
final Map<String, Object> nodes = ObjectPath.createFromResponse(response).evaluate("nodes");
12401243

12411244
Version minVersion = null;

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/history/SnapshotLifecycleTemplateRegistry.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
public class SnapshotLifecycleTemplateRegistry extends IndexTemplateRegistry {
3636
// history (please add a comment why you increased the version here)
3737
// version 1: initial
38-
public static final String INDEX_TEMPLATE_VERSION = "1";
38+
// version 2: converted to hidden index
39+
public static final int INDEX_TEMPLATE_VERSION = 2;
3940

4041
public static final String SLM_TEMPLATE_VERSION_VARIABLE = "xpack.slm.template.version";
4142
public static final String SLM_TEMPLATE_NAME = ".slm-history";

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/template/IndexTemplateConfig.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class IndexTemplateConfig {
1616

1717
private final String templateName;
1818
private final String fileName;
19-
private final String version;
19+
private final int version;
2020
private final String versionProperty;
2121

2222
/**
@@ -27,14 +27,17 @@ public class IndexTemplateConfig {
2727
* {@code {"myTemplateVersion": "${my.version.property}"}}
2828
* With {@code version = "42"; versionProperty = "my.version.property"} will result in {@code {"myTemplateVersion": "42"}}.
2929
*
30-
* @param templateName The name that will be used for the index template. Literal, include the version in this string if
30+
* Note that this code does not automatically insert the {@code version} index template property - include that in the JSON file
31+
* defining the template, preferably using the version variable provided to this constructor.
32+
*
33+
* @param templateName The name that will be used for the index template. Literal, include the version in this string if
3134
* it should be used.
3235
* @param fileName The filename the template should be loaded from. Literal, should include leading {@literal /} and
3336
* extension if necessary.
3437
* @param version The version of the template. Substituted for {@code versionProperty} as described above.
3538
* @param versionProperty The property that will be replaced with the {@code version} string as described above.
3639
*/
37-
public IndexTemplateConfig(String templateName, String fileName, String version, String versionProperty) {
40+
public IndexTemplateConfig(String templateName, String fileName, int version, String versionProperty) {
3841
this.templateName = templateName;
3942
this.fileName = fileName;
4043
this.version = version;
@@ -49,14 +52,20 @@ public String getTemplateName() {
4952
return templateName;
5053
}
5154

55+
public int getVersion() {
56+
return version;
57+
}
58+
5259
/**
5360
* Loads the template from disk as a UTF-8 byte array.
5461
* @return The template as a UTF-8 byte array.
5562
*/
5663
public byte[] loadBytes() {
57-
String template = TemplateUtils.loadTemplate(fileName, version,
58-
Pattern.quote("${" + versionProperty + "}"));
64+
final String versionPattern = Pattern.quote("${" + versionProperty + "}");
65+
String template = TemplateUtils.loadTemplate(fileName, Integer.toString(version), versionPattern);
5966
assert template != null && template.length() > 0;
67+
assert Pattern.compile("\"version\"\\s*:\\s*" + version).matcher(template).find()
68+
: "index template must have a version property set to the given version property";
6069
return template.getBytes(StandardCharsets.UTF_8);
6170
}
6271
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/template/IndexTemplateRegistry.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.elasticsearch.cluster.ClusterChangedEvent;
1717
import org.elasticsearch.cluster.ClusterState;
1818
import org.elasticsearch.cluster.ClusterStateListener;
19+
import org.elasticsearch.cluster.metadata.IndexTemplateMetaData;
1920
import org.elasticsearch.cluster.node.DiscoveryNode;
2021
import org.elasticsearch.cluster.service.ClusterService;
2122
import org.elasticsearch.common.settings.Settings;
@@ -31,6 +32,7 @@
3132
import org.elasticsearch.xpack.core.ilm.action.PutLifecycleAction;
3233

3334
import java.util.List;
35+
import java.util.Objects;
3436
import java.util.Optional;
3537
import java.util.concurrent.ConcurrentHashMap;
3638
import java.util.concurrent.ConcurrentMap;
@@ -130,16 +132,24 @@ public void clusterChanged(ClusterChangedEvent event) {
130132

131133
private void addTemplatesIfMissing(ClusterState state) {
132134
final List<IndexTemplateConfig> indexTemplates = getTemplateConfigs();
133-
for (IndexTemplateConfig template : indexTemplates) {
134-
final String templateName = template.getTemplateName();
135+
for (IndexTemplateConfig newTemplate : indexTemplates) {
136+
final String templateName = newTemplate.getTemplateName();
135137
final AtomicBoolean creationCheck = templateCreationsInProgress.computeIfAbsent(templateName, key -> new AtomicBoolean(false));
136138
if (creationCheck.compareAndSet(false, true)) {
137-
if (!state.metaData().getTemplates().containsKey(templateName)) {
139+
IndexTemplateMetaData currentTemplate = state.metaData().getTemplates().get(templateName);
140+
if (Objects.isNull(currentTemplate)) {
138141
logger.debug("adding index template [{}] for [{}], because it doesn't exist", templateName, getOrigin());
139-
putTemplate(template, creationCheck);
142+
putTemplate(newTemplate, creationCheck);
143+
} else if (Objects.isNull(currentTemplate.getVersion()) || newTemplate.getVersion() > currentTemplate.getVersion()) {
144+
// IndexTemplateConfig now enforces templates contain a `version` property, so if the template doesn't have one we can
145+
// safely assume it's an old version of the template.
146+
logger.info("upgrading index template [{}] for [{}] from version [{}] to version [{}]",
147+
templateName, getOrigin(), currentTemplate.getVersion(), newTemplate.getVersion());
148+
putTemplate(newTemplate, creationCheck);
140149
} else {
141150
creationCheck.set(false);
142-
logger.trace("not adding index template [{}] for [{}], because it already exists", templateName, getOrigin());
151+
logger.trace("not adding index template [{}] for [{}], because it already exists at version [{}]",
152+
templateName, getOrigin(), currentTemplate.getVersion());
143153
}
144154
} else {
145155
logger.trace("skipping the creation of index template [{}] for [{}], because its creation is in progress",

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/template/TemplateUtils.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ public static void loadTemplateIntoMap(String resource, Map<String, IndexTemplat
5959
public static String loadTemplate(String resource, String version, String versionProperty) {
6060
try {
6161
BytesReference source = load(resource);
62-
validate(source);
62+
final String filteredJson = filter(source, version, versionProperty);
63+
validate(new BytesArray(filteredJson));
6364

64-
return filter(source, version, versionProperty);
65+
return filteredJson;
6566
} catch (Exception e) {
6667
throw new IllegalArgumentException("Unable to load template [" + resource + "]", e);
6768
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/support/WatcherIndexTemplateRegistryField.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public final class WatcherIndexTemplateRegistryField {
1616
// version 9: add a user field defining which user executed the watch
1717
// version 10: add support for foreach path in actions
1818
// Note: if you change this, also inform the kibana team around the watcher-ui
19-
public static final String INDEX_TEMPLATE_VERSION = "10";
19+
public static final int INDEX_TEMPLATE_VERSION = 10;
2020
public static final String HISTORY_TEMPLATE_NAME = ".watch-history-" + INDEX_TEMPLATE_VERSION;
2121
public static final String HISTORY_TEMPLATE_NAME_NO_ILM = ".watch-history-no-ilm-" + INDEX_TEMPLATE_VERSION;
2222
public static final String TRIGGERED_TEMPLATE_NAME = ".triggered_watches";

x-pack/plugin/core/src/main/resources/ilm-history.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"index.auto_expand_replicas": "0-1",
1010
"index.lifecycle.name": "ilm-history-ilm-policy",
1111
"index.lifecycle.rollover_alias": "ilm-history-${xpack.ilm_history.template.version}",
12+
"index.hidden": true,
1213
"index.format": 1
1314
},
1415
"mappings": {
@@ -79,5 +80,6 @@
7980
}
8081
}
8182
}
82-
}
83+
},
84+
"version": ${xpack.ilm_history.template.version}
8385
}

x-pack/plugin/core/src/main/resources/slm-history.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"index.auto_expand_replicas": "0-1",
1010
"index.lifecycle.name": "slm-history-ilm-policy",
1111
"index.lifecycle.rollover_alias": ".slm-history-${xpack.slm.template.version}",
12+
"index.hidden": true,
1213
"index.format": 1
1314
},
1415
"mappings": {
@@ -55,5 +56,6 @@
5556
}
5657
}
5758
}
58-
}
59-
}
59+
},
60+
"version": ${xpack.slm.template.version}
61+
}

x-pack/plugin/core/src/main/resources/triggered-watches.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@
3636
}
3737
}
3838
}
39-
}
39+
},
40+
"version": ${xpack.watcher.template.version}
4041
}

x-pack/plugin/core/src/main/resources/watch-history-no-ilm.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,5 +611,6 @@
611611
}
612612
}
613613
}
614-
}
614+
},
615+
"version": ${xpack.watcher.template.version}
615616
}

x-pack/plugin/core/src/main/resources/watch-history.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,5 +562,6 @@
562562
}
563563
}
564564
}
565-
}
565+
},
566+
"version": ${xpack.watcher.template.version}
566567
}

x-pack/plugin/core/src/main/resources/watches.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,6 @@
5858
}
5959
}
6060
}
61-
}
61+
},
62+
"version": ${xpack.watcher.template.version}
6263
}

0 commit comments

Comments
 (0)