Skip to content

Commit 39003ec

Browse files
pyek-botakolarkunnu
authored andcommitted
Ensure index templates are not applied to system indices (opensearch-project#16418)
* fix: ensure system indices are processed without templates Signed-off-by: Pavan Yekbote <[email protected]> * refactor: overloaded method for creating without templates Signed-off-by: Pavan Yekbote <[email protected]> * test: adding test to check call for notemplates on system index Signed-off-by: Pavan Yekbote <[email protected]> * refactor: cchange modifier to package private and add entry in changelog Signed-off-by: Pavan Yekbote <[email protected]> * test: adding IT test Signed-off-by: Pavan Yekbote <[email protected]> * refactor: remove UT and add private modifiers Signed-off-by: Pavan Yekbote <[email protected]> * refactor: spotless changes Signed-off-by: Pavan Yekbote <[email protected]> --------- Signed-off-by: Pavan Yekbote <[email protected]>
1 parent 66b7243 commit 39003ec

File tree

3 files changed

+90
-2
lines changed

3 files changed

+90
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
2222
### Fixed
2323
- Fix get index settings API doesn't show `number_of_routing_shards` setting when it was explicitly set ([#16294](https://github.com/opensearch-project/OpenSearch/pull/16294))
2424
- Revert changes to upload remote state manifest using minimum codec version([#16403](https://github.com/opensearch-project/OpenSearch/pull/16403))
25+
- Ensure index templates are not applied to system indices ([#16418](https://github.com/opensearch-project/OpenSearch/pull/16418))
2526

2627
### Security
2728

qa/smoke-test-http/src/test/java/org/opensearch/http/SystemIndexRestIT.java

+70
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,76 @@ public void testSystemIndexAccessBlockedByDefault() throws Exception {
123123
}
124124
}
125125

126+
public void testSystemIndexCreatedWithoutAnyTemplates() throws Exception {
127+
// create template
128+
{
129+
Request templateRequest = new Request("POST", "_component_template/error_mapping_test_template");
130+
String jsonBody = "{\n" +
131+
" \"template\": {\n" +
132+
" \"mappings\": {\n" +
133+
" \"properties\": {\n" +
134+
" \"error\" : {\n" +
135+
" \"type\": \"nested\",\n" +
136+
" \"properties\": {\n" +
137+
" \"message\": {\n" +
138+
" \"type\": \"text\"\n" +
139+
" },\n" +
140+
" \"status\": {\n" +
141+
" \"type\": \"integer\"\n" +
142+
" }\n" +
143+
" }\n" +
144+
" }\n" +
145+
" }\n" +
146+
" }\n" +
147+
" }\n" +
148+
"}";
149+
150+
templateRequest.setJsonEntity(jsonBody);
151+
Response resp = getRestClient().performRequest(templateRequest);
152+
assertThat(resp.getStatusLine().getStatusCode(), equalTo(200));
153+
}
154+
155+
156+
// apply template to indices
157+
{
158+
Request applyTemplateRequest = new Request("POST", "_index_template/match_all_test_template");
159+
String jsonBody = "{\n" +
160+
" \"index_patterns\": [\n" +
161+
" \"*system-idx*\"\n" +
162+
" ],\n" +
163+
" \"template\": {\n" +
164+
" \"settings\": {}\n" +
165+
" },\n" +
166+
" \"priority\": 10,\n" +
167+
" \"composed_of\": [\n" +
168+
" \"error_mapping_test_template\"\n" +
169+
" ],\n" +
170+
" \"version\": 1\n" +
171+
"}";
172+
173+
applyTemplateRequest.setJsonEntity(jsonBody);
174+
Response resp = getRestClient().performRequest(applyTemplateRequest);
175+
assertThat(resp.getStatusLine().getStatusCode(), equalTo(200));
176+
}
177+
178+
// create system index - success
179+
{
180+
Request indexRequest = new Request("PUT", "/" + SystemIndexTestPlugin.SYSTEM_INDEX_NAME);
181+
String jsonBody = "{\n" +
182+
" \"mappings\": {\n" +
183+
" \"properties\": {\n" +
184+
" \"error\": {\n" +
185+
" \"type\": \"text\"\n" +
186+
" }\n" +
187+
" }\n" +
188+
" }\n" +
189+
"}";
190+
indexRequest.setJsonEntity(jsonBody);
191+
Response resp = getRestClient().performRequest(indexRequest);
192+
assertThat(resp.getStatusLine().getStatusCode(), equalTo(200));
193+
}
194+
}
195+
126196
private void assertDeprecationWarningOnAccess(String queryPattern, String warningIndexName) throws IOException {
127197
String expectedWarning = "this request accesses system indices: [" + warningIndexName + "], but in a " +
128198
"future major version, direct access to system indices will be prevented by default";

server/src/main/java/org/opensearch/cluster/metadata/MetadataCreateIndexService.java

+19-2
Original file line numberDiff line numberDiff line change
@@ -434,15 +434,21 @@ public ClusterState applyCreateIndexRequest(
434434
// in which case templates don't apply, so create the index from the source metadata
435435
return applyCreateIndexRequestWithExistingMetadata(currentState, request, silent, sourceMetadata, metadataTransformer);
436436
} else {
437+
// The backing index may have a different name or prefix than the data stream name.
438+
final String name = request.dataStreamName() != null ? request.dataStreamName() : request.index();
439+
440+
// Do not apply any templates to system indices
441+
if (systemIndices.isSystemIndex(name)) {
442+
return applyCreateIndexRequestWithNoTemplates(currentState, request, silent, metadataTransformer);
443+
}
444+
437445
// Hidden indices apply templates slightly differently (ignoring wildcard '*'
438446
// templates), so we need to check to see if the request is creating a hidden index
439447
// prior to resolving which templates it matches
440448
final Boolean isHiddenFromRequest = IndexMetadata.INDEX_HIDDEN_SETTING.exists(request.settings())
441449
? IndexMetadata.INDEX_HIDDEN_SETTING.get(request.settings())
442450
: null;
443451

444-
// The backing index may have a different name or prefix than the data stream name.
445-
final String name = request.dataStreamName() != null ? request.dataStreamName() : request.index();
446452
// Check to see if a v2 template matched
447453
final String v2Template = MetadataIndexTemplateService.findV2Template(
448454
currentState.metadata(),
@@ -676,6 +682,17 @@ public void addRemoteStoreCustomMetadata(IndexMetadata.Builder tmpImdBuilder, bo
676682
tmpImdBuilder.putCustom(IndexMetadata.REMOTE_STORE_CUSTOM_KEY, remoteCustomData);
677683
}
678684

685+
private ClusterState applyCreateIndexRequestWithNoTemplates(
686+
final ClusterState currentState,
687+
final CreateIndexClusterStateUpdateRequest request,
688+
final boolean silent,
689+
final BiConsumer<Metadata.Builder, IndexMetadata> metadataTransformer
690+
) throws Exception {
691+
// Using applyCreateIndexRequestWithV1Templates with empty list instead of applyCreateIndexRequestWithV2Template
692+
// with null template as applyCreateIndexRequestWithV2Template has assertions when template is null
693+
return applyCreateIndexRequestWithV1Templates(currentState, request, silent, Collections.emptyList(), metadataTransformer);
694+
}
695+
679696
private ClusterState applyCreateIndexRequestWithV1Templates(
680697
final ClusterState currentState,
681698
final CreateIndexClusterStateUpdateRequest request,

0 commit comments

Comments
 (0)