From 3cd8f3fdd50d510165032dfb9fb4b1a5b4a224b5 Mon Sep 17 00:00:00 2001 From: Petar Dzepina Date: Thu, 12 Jan 2023 01:06:13 +0100 Subject: [PATCH] GetIndexMapping API bugfix (#293) Signed-off-by: Petar Dzepina (cherry picked from commit 4c160e258d632f13ac0373596b847800b3eb1a75) --- .../mapper/MapperService.java | 22 +++++------- .../mapper/MappingsTraverser.java | 2 +- .../SecurityAnalyticsRestTestCase.java | 1 + .../mapper/MapperRestApiIT.java | 34 ++++++++++++++++++- .../mapper/MappingsTraverserTests.java | 2 +- 5 files changed, 44 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/opensearch/securityanalytics/mapper/MapperService.java b/src/main/java/org/opensearch/securityanalytics/mapper/MapperService.java index 553905565..0f55daecd 100644 --- a/src/main/java/org/opensearch/securityanalytics/mapper/MapperService.java +++ b/src/main/java/org/opensearch/securityanalytics/mapper/MapperService.java @@ -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; @@ -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; @@ -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 appliedAliases = new ArrayList<>(); + Set appliedAliases = new HashSet<>(); // Get list of alias -> path pairs from index mappings List> indexAliasPathPairs = MapperUtils.getAllAliasPathPairs(mappingMetadata); @@ -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) { diff --git a/src/main/java/org/opensearch/securityanalytics/mapper/MappingsTraverser.java b/src/main/java/org/opensearch/securityanalytics/mapper/MappingsTraverser.java index 88d183480..16335894b 100644 --- a/src/main/java/org/opensearch/securityanalytics/mapper/MappingsTraverser.java +++ b/src/main/java/org/opensearch/securityanalytics/mapper/MappingsTraverser.java @@ -214,7 +214,7 @@ private boolean shouldSkipNode(Map properties) { return false; } - public Map traverseAndCopyWithFilter(List nodePathsToCopy) { + public Map traverseAndCopyWithFilter(Set nodePathsToCopy) { Map outRoot = new LinkedHashMap<>(Map.of(PROPERTIES, new LinkedHashMap())); this.addListener(new MappingsTraverserListener() { diff --git a/src/test/java/org/opensearch/securityanalytics/SecurityAnalyticsRestTestCase.java b/src/test/java/org/opensearch/securityanalytics/SecurityAnalyticsRestTestCase.java index c096fa7c0..a4f2df778 100644 --- a/src/test/java/org/opensearch/securityanalytics/SecurityAnalyticsRestTestCase.java +++ b/src/test/java/org/opensearch/securityanalytics/SecurityAnalyticsRestTestCase.java @@ -1442,6 +1442,7 @@ protected Map 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 diff --git a/src/test/java/org/opensearch/securityanalytics/mapper/MapperRestApiIT.java b/src/test/java/org/opensearch/securityanalytics/mapper/MapperRestApiIT.java index c518a11a2..9e7337a92 100644 --- a/src/test/java/org/opensearch/securityanalytics/mapper/MapperRestApiIT.java +++ b/src/test/java/org/opensearch/securityanalytics/mapper/MapperRestApiIT.java @@ -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 respMap = (Map) responseAsMap(response); + Map props = (Map)((Map) respMap.get(testIndexPattern)).get("mappings"); + props = (Map) props.get("properties"); + assertEquals(4, props.size()); + } + public void testCreateMappingSuccess() throws IOException { String testIndexName = "my_index"; @@ -1425,7 +1457,7 @@ public void testTraverseAndCopy() { // Copy specific paths from mappings Map 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 diff --git a/src/test/java/org/opensearch/securityanalytics/mapper/MappingsTraverserTests.java b/src/test/java/org/opensearch/securityanalytics/mapper/MappingsTraverserTests.java index 19ebb9e12..1c71df93c 100644 --- a/src/test/java/org/opensearch/securityanalytics/mapper/MappingsTraverserTests.java +++ b/src/test/java/org/opensearch/securityanalytics/mapper/MappingsTraverserTests.java @@ -369,7 +369,7 @@ public void testTraverseAndCopyValidNestedMappings() { mappingsTraverser = new MappingsTraverser(indexMappingJSON, Set.of("ip")); // Copy mappings while excluding type=ip - Map filteredMappings = mappingsTraverser.traverseAndCopyWithFilter(List.of("user.first", "user.last")); + Map filteredMappings = mappingsTraverser.traverseAndCopyWithFilter(Set.of("user.first", "user.last")); // Now traverse filtered mapppings to confirm type=ip is not present List paths = new ArrayList<>();