|
22 | 22 | import org.opensearch.securityanalytics.SecurityAnalyticsPlugin;
|
23 | 23 | import org.opensearch.securityanalytics.SecurityAnalyticsRestTestCase;
|
24 | 24 | import org.opensearch.securityanalytics.config.monitors.DetectorMonitorConfig;
|
25 |
| -import org.opensearch.securityanalytics.logtype.BuiltinLogTypeLoader; |
26 | 25 | import org.opensearch.securityanalytics.model.Detector;
|
27 | 26 | import org.opensearch.securityanalytics.model.DetectorInput;
|
28 | 27 | import org.opensearch.securityanalytics.model.DetectorRule;
|
29 | 28 | import org.opensearch.securityanalytics.model.Rule;
|
30 | 29 |
|
31 | 30 | import java.io.File;
|
32 | 31 | import java.io.IOException;
|
33 |
| -import java.nio.file.Files; |
34 |
| -import java.nio.file.Path; |
35 |
| -import java.nio.file.Paths; |
36 | 32 | import java.util.Collections;
|
37 | 33 | import java.util.List;
|
38 | 34 | import java.util.Locale;
|
|
47 | 43 | import static org.opensearch.securityanalytics.TestHelpers.randomEditedRule;
|
48 | 44 | import static org.opensearch.securityanalytics.TestHelpers.randomIndex;
|
49 | 45 | import static org.opensearch.securityanalytics.TestHelpers.randomRule;
|
| 46 | +import static org.opensearch.securityanalytics.TestHelpers.randomRuleForMappingView; |
50 | 47 | import static org.opensearch.securityanalytics.TestHelpers.randomRuleWithErrors;
|
51 | 48 | import static org.opensearch.securityanalytics.TestHelpers.windowsIndexMapping;
|
52 | 49 |
|
@@ -841,4 +838,54 @@ public void testGetAllRuleCategories() throws IOException {
|
841 | 838 | assertTrue(categories.stream().anyMatch(e -> ((Map<String, Object>)e).get("key").equals("linux")));
|
842 | 839 | assertTrue(categories.stream().anyMatch(e -> ((Map<String, Object>)e).get("key").equals("waf")));
|
843 | 840 | }
|
| 841 | + |
| 842 | + @SuppressWarnings("unchecked") |
| 843 | + public void testGetMappingsViewApiForFieldAliasesWithSameName() throws IOException { |
| 844 | + String index = createTestIndex(randomIndex(), windowsIndexMapping()); |
| 845 | + // Execute GetMappingsViewAction to add alias mapping for index |
| 846 | + Request request = new Request("GET", SecurityAnalyticsPlugin.MAPPINGS_VIEW_BASE_URI); |
| 847 | + // both req params and req body are supported |
| 848 | + request.addParameter("index_name", index); |
| 849 | + request.addParameter("rule_topic", randomDetectorType()); |
| 850 | + Response response = client().performRequest(request); |
| 851 | + assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode()); |
| 852 | + Map<String, Object> respMap = responseAsMap(response); |
| 853 | + Assert.assertTrue(((List<String>) respMap.get("unmapped_index_fields")).contains("AccessList")); |
| 854 | + |
| 855 | + String rule = randomRuleForMappingView("AccessList"); |
| 856 | + |
| 857 | + Response createResponse = makeRequest(client(), "POST", SecurityAnalyticsPlugin.RULE_BASE_URI, Collections.singletonMap("category", randomDetectorType()), |
| 858 | + new StringEntity(rule), new BasicHeader("Content-Type", "application/json")); |
| 859 | + Assert.assertEquals("Create rule failed", RestStatus.CREATED, restStatus(createResponse)); |
| 860 | + Map<String, Object> responseBody = asMap(createResponse); |
| 861 | + String createdId = responseBody.get("_id").toString(); |
| 862 | + |
| 863 | + // Execute GetMappingsViewAction to add alias mapping for index |
| 864 | + request = new Request("GET", SecurityAnalyticsPlugin.MAPPINGS_VIEW_BASE_URI); |
| 865 | + // both req params and req body are supported |
| 866 | + request.addParameter("index_name", index); |
| 867 | + request.addParameter("rule_topic", randomDetectorType()); |
| 868 | + response = client().performRequest(request); |
| 869 | + assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode()); |
| 870 | + respMap = responseAsMap(response); |
| 871 | + Assert.assertTrue(((Map<String, Object>) respMap.get("properties")).containsKey("AccessList")); |
| 872 | + |
| 873 | + rule = randomRuleForMappingView("Access_List"); |
| 874 | + |
| 875 | + Response updateResponse = makeRequest(client(), "PUT", SecurityAnalyticsPlugin.RULE_BASE_URI + "/" + createdId, |
| 876 | + Map.of("category", randomDetectorType()), |
| 877 | + new StringEntity(rule), new BasicHeader("Content-Type", "application/json")); |
| 878 | + Assert.assertEquals("Update rule failed", RestStatus.OK, restStatus(updateResponse)); |
| 879 | + |
| 880 | + // Execute GetMappingsViewAction to add alias mapping for index |
| 881 | + request = new Request("GET", SecurityAnalyticsPlugin.MAPPINGS_VIEW_BASE_URI); |
| 882 | + // both req params and req body are supported |
| 883 | + request.addParameter("index_name", index); |
| 884 | + request.addParameter("rule_topic", randomDetectorType()); |
| 885 | + response = client().performRequest(request); |
| 886 | + assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode()); |
| 887 | + respMap = responseAsMap(response); |
| 888 | + Assert.assertTrue(((List<String>) respMap.get("unmapped_field_aliases")).contains("Access_List")); |
| 889 | + Assert.assertTrue(((Map<String, Object>) respMap.get("properties")).containsKey("AccessList")); |
| 890 | + } |
844 | 891 | }
|
0 commit comments