diff --git a/src/main/java/org/opensearch/securityanalytics/transport/TransportIndexDetectorAction.java b/src/main/java/org/opensearch/securityanalytics/transport/TransportIndexDetectorAction.java index 4fec6c262..e184f0706 100644 --- a/src/main/java/org/opensearch/securityanalytics/transport/TransportIndexDetectorAction.java +++ b/src/main/java/org/opensearch/securityanalytics/transport/TransportIndexDetectorAction.java @@ -775,7 +775,8 @@ public void onResponse(SearchResponse searchResponse) { ) ) ); - return; + } else { + continueCreatingDetector(detector); } @@ -790,7 +791,10 @@ public void onFailure(Exception e) { } }); } + } + + void continueCreatingDetector(Detector detector){ String ruleTopic = detector.getDetectorType(); request.getDetector().setAlertsIndex(DetectorMonitorConfig.getAlertsIndex(ruleTopic)); diff --git a/src/test/java/org/opensearch/securityanalytics/SecurityAnalyticsRestTestCase.java b/src/test/java/org/opensearch/securityanalytics/SecurityAnalyticsRestTestCase.java index 1592036c4..743a07925 100644 --- a/src/test/java/org/opensearch/securityanalytics/SecurityAnalyticsRestTestCase.java +++ b/src/test/java/org/opensearch/securityanalytics/SecurityAnalyticsRestTestCase.java @@ -1197,16 +1197,8 @@ boolean preserveODFEIndicesAfterTest() { return false; } - protected boolean keepDetectorConfigIndex() { - return false; - } - @After protected void wipeAllODFEIndices() throws IOException { - wipeAllODFEIndices(keepDetectorConfigIndex()); - } - - protected void wipeAllODFEIndices(boolean keepDetectorConfigIndex) throws IOException { if (preserveODFEIndicesAfterTest()) return; Response response = client().performRequest(new Request("GET", "/_cat/indices?format=json&expand_wildcards=all")); @@ -1224,15 +1216,12 @@ protected void wipeAllODFEIndices(boolean keepDetectorConfigIndex) throws IOExc String indexName = jsonObject.get("index").toString(); // .opendistro_security isn't allowed to delete from cluster if (!".opendistro_security".equals(indexName)) { - String detectorConfigIndex = keepDetectorConfigIndex?".opensearch-sap-detectors-config":""; - if (!detectorConfigIndex.equals(indexName)) { Request request = new Request("DELETE", String.format(Locale.getDefault(), "/%s", indexName)); // TODO: remove PERMISSIVE option after moving system index access to REST API call RequestOptions.Builder options = RequestOptions.DEFAULT.toBuilder(); options.setWarningsHandler(WarningsHandler.PERMISSIVE); request.setOptions(options.build()); adminClient().performRequest(request); - } } } } @@ -1522,32 +1511,4 @@ protected void createSampleDatastream(String datastreamName, String mappings) th createDatastreamAPI(datastreamName); } - - -// private String dummyIndex=""; -// -// @Before -// void createDummyDetector() throws IOException { -// if ("".equals(dummyIndex)) { -// dummyIndex = createTestIndex(randomIndexDns(), dnsIndexMapping()); -// //indexDoc(index, "1", randomDoc()); -// // Execute CreateMappingsAction to add alias mapping for index -// Request createMappingRequest = new Request("POST", SecurityAnalyticsPlugin.MAPPER_BASE_URI); -// // both req params and req body are supported -// createMappingRequest.setJsonEntity( -// "{ \"index_name\":\"" + dummyIndex + "\"," + -// " \"rule_topic\":\"" + randomDetectorTypeDns() + "\", " + -// " \"partial\":true" + -// "}" -// ); -// -// Response response = client().performRequest(createMappingRequest); -// assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode()); -// -// -// Detector detector = randomDetectorDns(List.of(new String("8ae51330-899c-4641-8125-e39f2e07da72"))); -// -// Response createResponse = makeRequest(client(), "POST", SecurityAnalyticsPlugin.DETECTOR_BASE_URI, Collections.emptyMap(), toHttpEntity(detector)); -// } -// } } \ No newline at end of file diff --git a/src/test/java/org/opensearch/securityanalytics/TestHelpers.java b/src/test/java/org/opensearch/securityanalytics/TestHelpers.java index 007e39a10..eaf3df694 100644 --- a/src/test/java/org/opensearch/securityanalytics/TestHelpers.java +++ b/src/test/java/org/opensearch/securityanalytics/TestHelpers.java @@ -1258,41 +1258,4 @@ public static NamedXContentRegistry xContentRegistry() { public static XContentBuilder builder() throws IOException { return XContentBuilder.builder(XContentType.JSON.xContent()); } - - public static String dnsIndexMapping() { - return "\"properties\": {\n" + - " \"parent_domain\": {\n" + - " \"type\": \"text\"\n" + - " },\n" + - " \"type\": {\n" + - " \"type\": \"text\"\n" + - " },\n" + - " \"record_type\": {\n" + - " \"type\": \"text\"\n" + - " },\n" + - " \"query\": {\n" + - " \"type\": \"text\",\n" + - " \"fields\": {\n" + - " \"keyword\": {\n" + - " \"type\": \"keyword\",\n" + - " \"ignore_above\": 256\n" + - " }\n" + - " }\n" + - " }\n" + - " }"; - } - - public static String randomDetectorTypeDns() { - return "dns"; - } - - public static String randomIndexDns() { - return "dns"; - } - - public static Detector randomDetectorDns(List rules) { - DetectorInput input = new DetectorInput("dns detector for security analytics", List.of("dns"), Collections.emptyList(), - rules.stream().map(DetectorRule::new).collect(Collectors.toList())); - return randomDetector(null, Detector.DetectorType.valueOf(randomDetectorTypeDns().toUpperCase(Locale.ROOT)), null, List.of(input), List.of(), null, null, null, null); - } } \ No newline at end of file diff --git a/src/test/java/org/opensearch/securityanalytics/resthandler/DetectorRestApiIT.java b/src/test/java/org/opensearch/securityanalytics/resthandler/DetectorRestApiIT.java index 76e52c0ce..1eee89c7e 100644 --- a/src/test/java/org/opensearch/securityanalytics/resthandler/DetectorRestApiIT.java +++ b/src/test/java/org/opensearch/securityanalytics/resthandler/DetectorRestApiIT.java @@ -45,40 +45,6 @@ public class DetectorRestApiIT extends SecurityAnalyticsRestTestCase { - private String dummyIndex=""; - - @Before - void createDummyDetector() throws IOException { - if ("".equals(dummyIndex)) { - dummyIndex = createTestIndex(randomIndexDns(), dnsIndexMapping()); - //indexDoc(index, "1", randomDoc()); - // Execute CreateMappingsAction to add alias mapping for index - Request createMappingRequest = new Request("POST", SecurityAnalyticsPlugin.MAPPER_BASE_URI); - // both req params and req body are supported - createMappingRequest.setJsonEntity( - "{ \"index_name\":\"" + dummyIndex + "\"," + - " \"rule_topic\":\"" + randomDetectorTypeDns() + "\", " + - " \"partial\":true" + - "}" - ); - - Response response = client().performRequest(createMappingRequest); - assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode()); - - - Detector detector = randomDetectorDns(List.of(new String("8ae51330-899c-4641-8125-e39f2e07da72"))); - - Response createResponse = makeRequest(client(), "POST", SecurityAnalyticsPlugin.DETECTOR_BASE_URI, Collections.emptyMap(), toHttpEntity(detector)); - - } - } - - @Override - protected boolean keepDetectorConfigIndex() { - return true; - } - - @SuppressWarnings("unchecked") public void testCreatingADetector() throws IOException { String index = createTestIndex(randomIndex(), windowsIndexMapping());