Skip to content

Commit

Permalink
GetIndexMapping API bugfix (opensearch-project#293)
Browse files Browse the repository at this point in the history
Signed-off-by: Petar Dzepina <[email protected]>
  • Loading branch information
petardz authored and eirsep committed Apr 3, 2023
1 parent 03d8301 commit 30ef994
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 17 deletions.
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

0 comments on commit 30ef994

Please sign in to comment.