Skip to content

Commit

Permalink
Ensure index templates are not applied to system indices (#16418)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
pyek-bot authored Oct 29, 2024
1 parent 08dc3bb commit 1e7c122
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Fixed
- 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))
- Revert changes to upload remote state manifest using minimum codec version([#16403](https://github.com/opensearch-project/OpenSearch/pull/16403))
- Ensure index templates are not applied to system indices ([#16418](https://github.com/opensearch-project/OpenSearch/pull/16418))

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,76 @@ public void testSystemIndexAccessBlockedByDefault() throws Exception {
}
}

public void testSystemIndexCreatedWithoutAnyTemplates() throws Exception {
// create template
{
Request templateRequest = new Request("POST", "_component_template/error_mapping_test_template");
String jsonBody = "{\n" +
" \"template\": {\n" +
" \"mappings\": {\n" +
" \"properties\": {\n" +
" \"error\" : {\n" +
" \"type\": \"nested\",\n" +
" \"properties\": {\n" +
" \"message\": {\n" +
" \"type\": \"text\"\n" +
" },\n" +
" \"status\": {\n" +
" \"type\": \"integer\"\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
"}";

templateRequest.setJsonEntity(jsonBody);
Response resp = getRestClient().performRequest(templateRequest);
assertThat(resp.getStatusLine().getStatusCode(), equalTo(200));
}


// apply template to indices
{
Request applyTemplateRequest = new Request("POST", "_index_template/match_all_test_template");
String jsonBody = "{\n" +
" \"index_patterns\": [\n" +
" \"*system-idx*\"\n" +
" ],\n" +
" \"template\": {\n" +
" \"settings\": {}\n" +
" },\n" +
" \"priority\": 10,\n" +
" \"composed_of\": [\n" +
" \"error_mapping_test_template\"\n" +
" ],\n" +
" \"version\": 1\n" +
"}";

applyTemplateRequest.setJsonEntity(jsonBody);
Response resp = getRestClient().performRequest(applyTemplateRequest);
assertThat(resp.getStatusLine().getStatusCode(), equalTo(200));
}

// create system index - success
{
Request indexRequest = new Request("PUT", "/" + SystemIndexTestPlugin.SYSTEM_INDEX_NAME);
String jsonBody = "{\n" +
" \"mappings\": {\n" +
" \"properties\": {\n" +
" \"error\": {\n" +
" \"type\": \"text\"\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
indexRequest.setJsonEntity(jsonBody);
Response resp = getRestClient().performRequest(indexRequest);
assertThat(resp.getStatusLine().getStatusCode(), equalTo(200));
}
}

private void assertDeprecationWarningOnAccess(String queryPattern, String warningIndexName) throws IOException {
String expectedWarning = "this request accesses system indices: [" + warningIndexName + "], but in a " +
"future major version, direct access to system indices will be prevented by default";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,15 +434,21 @@ public ClusterState applyCreateIndexRequest(
// in which case templates don't apply, so create the index from the source metadata
return applyCreateIndexRequestWithExistingMetadata(currentState, request, silent, sourceMetadata, metadataTransformer);
} else {
// The backing index may have a different name or prefix than the data stream name.
final String name = request.dataStreamName() != null ? request.dataStreamName() : request.index();

// Do not apply any templates to system indices
if (systemIndices.isSystemIndex(name)) {
return applyCreateIndexRequestWithNoTemplates(currentState, request, silent, metadataTransformer);
}

// Hidden indices apply templates slightly differently (ignoring wildcard '*'
// templates), so we need to check to see if the request is creating a hidden index
// prior to resolving which templates it matches
final Boolean isHiddenFromRequest = IndexMetadata.INDEX_HIDDEN_SETTING.exists(request.settings())
? IndexMetadata.INDEX_HIDDEN_SETTING.get(request.settings())
: null;

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

private ClusterState applyCreateIndexRequestWithNoTemplates(
final ClusterState currentState,
final CreateIndexClusterStateUpdateRequest request,
final boolean silent,
final BiConsumer<Metadata.Builder, IndexMetadata> metadataTransformer
) throws Exception {
// Using applyCreateIndexRequestWithV1Templates with empty list instead of applyCreateIndexRequestWithV2Template
// with null template as applyCreateIndexRequestWithV2Template has assertions when template is null
return applyCreateIndexRequestWithV1Templates(currentState, request, silent, Collections.emptyList(), metadataTransformer);
}

private ClusterState applyCreateIndexRequestWithV1Templates(
final ClusterState currentState,
final CreateIndexClusterStateUpdateRequest request,
Expand Down

0 comments on commit 1e7c122

Please sign in to comment.