Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 2.x] Ensure index templates are not applied to system indices #16518

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,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 @@ -435,15 +435,21 @@
// 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);

Check warning on line 443 in server/src/main/java/org/opensearch/cluster/metadata/MetadataCreateIndexService.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L443 was not covered by tests
}

// 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 @@ -677,6 +683,17 @@
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);

Check warning on line 694 in server/src/main/java/org/opensearch/cluster/metadata/MetadataCreateIndexService.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L694 was not covered by tests
}

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