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

Ensure index templates are not applied to system indices #16418

Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- [Workload Management] Fixing Create/Update QueryGroup TransportActions to execute from non-cluster manager nodes ([16422](https://github.com/opensearch-project/OpenSearch/pull/16422))
- Fix flaky test in `testApproximateRangeWithSizeOverDefault` by adjusting totalHits assertion logic ([#16434](https://github.com/opensearch-project/OpenSearch/pull/16434#pullrequestreview-2386999409))
- 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 @@
// 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)) {
reta marked this conversation as resolved.
Show resolved Hide resolved
return applyCreateIndexRequestWithNoTemplates(currentState, request, silent, metadataTransformer);

Check warning on line 442 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#L442

Added line #L442 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 @@ -676,6 +682,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 693 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#L693

Added line #L693 was not covered by tests
cwperks marked this conversation as resolved.
Show resolved Hide resolved
}

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