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

GetIndexMapping API bugfix #293

Merged
merged 2 commits into from
Jan 12, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@

package org.opensearch.securityanalytics.mapper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -21,21 +28,12 @@
import org.opensearch.action.support.GroupedActionListener;
import org.opensearch.action.support.master.AcknowledgedResponse;
import org.opensearch.client.IndicesAdminClient;
import org.opensearch.cluster.metadata.IndexMetadata;
import org.opensearch.cluster.metadata.IndexNameExpressionResolver;
import org.opensearch.cluster.metadata.MappingMetadata;
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.collect.ImmutableOpenMap;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.rest.RestStatus;
import org.opensearch.securityanalytics.action.GetIndexMappingsResponse;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.opensearch.securityanalytics.action.GetMappingsViewResponse;
import org.opensearch.securityanalytics.model.CreateMappingResult;
import org.opensearch.securityanalytics.util.IndexUtils;
Expand Down Expand Up @@ -272,7 +270,7 @@ public void onResponse(GetMappingsResponse getMappingsResponse) {
// Extract MappingMetadata
MappingMetadata mappingMetadata = getMappingsResponse.mappings().iterator().next().value;
// List of all found applied aliases on index
List<String> appliedAliases = new ArrayList<>();
Set<String> appliedAliases = new HashSet<>();
// Get list of alias -> path pairs from index mappings
List<Pair<String, String>> indexAliasPathPairs = MapperUtils.getAllAliasPathPairs(mappingMetadata);

Expand All @@ -292,10 +290,6 @@ public void onResponse(GetMappingsResponse getMappingsResponse) {
}
}
}
// If we found all aliases we can stop searching further
if (indexAliasPathPairs.size() == appliedAliases.size()) {
break;
}
}

if (appliedAliases.size() == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ private boolean shouldSkipNode(Map<String, Object> properties) {
return false;
}

public Map<String, Object> traverseAndCopyWithFilter(List<String> nodePathsToCopy) {
public Map<String, Object> traverseAndCopyWithFilter(Set<String> nodePathsToCopy) {

Map<String, Object> outRoot = new LinkedHashMap<>(Map.of(PROPERTIES, new LinkedHashMap()));
this.addListener(new MappingsTraverserListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1442,6 +1442,7 @@ protected Map<String, Object> getIndexMappingsSAFlat(String indexName) throws IO
}



protected void createMappingsAPI(String indexName, String topicName) throws IOException {
Request request = new Request("POST", MAPPER_BASE_URI);
// both req params and req body are supported
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,38 @@ public void testGetMappingSuccess() throws IOException {
assertTrue(respMap.containsKey(testIndexPattern));
}

public void testGetMappingSuccess_1() throws IOException {
String testIndexName1 = "my_index_1";
String testIndexPattern = "my_index*";

createIndex(testIndexName1, Settings.EMPTY);

String sampleDoc = "{\n" +
" \"lvl1field\": 12345,\n" +
" \"source1.ip\": \"12345\",\n" +
" \"source1.port\": 55,\n" +
" \"some.very.long.field.name\": \"test\"\n" +
"}";

indexDoc(testIndexName1, "1", sampleDoc);
// puts mappings with timestamp alias
String createMappingsRequest = "{\"index_name\":\"my_index*\",\"rule_topic\":\"windows\",\"partial\":true,\"alias_mappings\":{\"properties\":{\"timestamp\":{\"type\":\"alias\",\"path\":\"lvl1field\"},\"winlog-computer_name\":{\"type\":\"alias\",\"path\":\"source1.port\"},\"winlog-event_data-AuthenticationPackageName\":{\"type\":\"alias\",\"path\":\"source1.ip\"},\"winlog-event_data-Company\":{\"type\":\"alias\",\"path\":\"some.very.long.field.name\"}}}}";

Request request = new Request("POST", MAPPER_BASE_URI);
// both req params and req body are supported
request.setJsonEntity(createMappingsRequest);
Response response = client().performRequest(request);
assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());

request = new Request("GET", MAPPER_BASE_URI + "?index_name=" + testIndexPattern);
response = client().performRequest(request);
assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
Map<String, Object> respMap = (Map<String, Object>) responseAsMap(response);
Map<String, Object> props = (Map<String, Object>)((Map<String, Object>) respMap.get(testIndexPattern)).get("mappings");
props = (Map<String, Object>) props.get("properties");
assertEquals(4, props.size());
}

public void testCreateMappingSuccess() throws IOException {

String testIndexName = "my_index";
Expand Down Expand Up @@ -1425,7 +1457,7 @@ public void testTraverseAndCopy() {

// Copy specific paths from mappings
Map<String, Object> filteredMappings = mappingsTraverser.traverseAndCopyWithFilter(
List.of("netflow.event_data.SourceAddress", "netflow.event.stop", "plain1", "user.first", "user.last")
Set.of("netflow.event_data.SourceAddress", "netflow.event.stop", "plain1", "user.first", "user.last")
);

// Now traverse filtered mapppings to confirm only copied paths are present
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ public void testTraverseAndCopyValidNestedMappings() {
mappingsTraverser = new MappingsTraverser(indexMappingJSON, Set.of("ip"));

// Copy mappings while excluding type=ip
Map<String, Object> filteredMappings = mappingsTraverser.traverseAndCopyWithFilter(List.of("user.first", "user.last"));
Map<String, Object> filteredMappings = mappingsTraverser.traverseAndCopyWithFilter(Set.of("user.first", "user.last"));

// Now traverse filtered mapppings to confirm type=ip is not present
List<String> paths = new ArrayList<>();
Expand Down