Skip to content

Commit 6760806

Browse files
authored
Fix bug when deleting detector with 0 rules. (#1648)
Signed-off-by: Thomas Hurney <hurneyt@amazon.com>
1 parent 8764ab1 commit 6760806

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/main/java/org/opensearch/securityanalytics/transport/TransportDeleteDetectorAction.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,13 @@ private void onGetResponse(Detector detector) {
197197
deleteWorkflow(detector, onDeleteWorkflowStep);
198198
onDeleteWorkflowStep.whenComplete(acknowledgedResponse -> {
199199
List<String> monitorIds = detector.getMonitorIds();
200+
201+
// A detector with 0 rules will have 0 monitors to delete. Skipping monitor deletion steps.
202+
if (monitorIds.isEmpty()) {
203+
deleteDetectorFromConfig(detector.getId(), request.getRefreshPolicy());
204+
return;
205+
}
206+
200207
ActionListener<DeleteMonitorResponse> deletesListener = new GroupedActionListener<>(new ActionListener<>() {
201208
@Override
202209
public void onResponse(Collection<DeleteMonitorResponse> responses) {

src/test/java/org/opensearch/securityanalytics/resthandler/DetectorRestApiIT.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1877,4 +1877,32 @@ public void testCreatingDetectorWithDynamicQueryIndexEnabledAndThenDisabled() th
18771877
Assert.assertEquals("Create detector failed", RestStatus.CREATED, restStatus(createResponse));
18781878
Assert.assertTrue(doesIndexExist(DetectorMonitorConfig.getRuleIndex(randomDetectorType()) + "-000001"));
18791879
}
1880+
1881+
public void testDeletingDetectorEditedToHaveZeroRules() throws IOException {
1882+
String index = createTestIndex(randomIndex(), windowsIndexMapping());
1883+
1884+
Request createMappingRequest = new Request("POST", SecurityAnalyticsPlugin.MAPPER_BASE_URI);
1885+
createMappingRequest.setJsonEntity(
1886+
"{ \"index_name\":\"" + index + "\"," +
1887+
" \"rule_topic\":\"" + randomDetectorType() + "\", " +
1888+
" \"partial\":true" +
1889+
"}"
1890+
);
1891+
Response response = client().performRequest(createMappingRequest);
1892+
assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
1893+
1894+
// Create a test detector that has at least 1 rule.
1895+
Detector detector = randomDetectorWithTriggers(getRandomPrePackagedRules(), List.of(new DetectorTrigger(null, "test-trigger", "1", List.of(randomDetectorType()), List.of(), List.of(), List.of(), List.of(), List.of())));
1896+
String detectorId = createDetector(detector);
1897+
1898+
// Update the detector to have 0 rules. This should update the detector's 'monitor_id' attribute to be an empty array.
1899+
DetectorInput input = new DetectorInput("detector with no rules", List.of(index), List.of(), List.of());
1900+
Detector updatedDetector = randomDetectorWithInputs(List.of(input));
1901+
1902+
Response updateResponse = makeRequest(client(), "PUT", SecurityAnalyticsPlugin.DETECTOR_BASE_URI + "/" + detectorId, Collections.emptyMap(), toHttpEntity(updatedDetector));
1903+
Assert.assertEquals("Update detector failed", RestStatus.OK, restStatus(updateResponse));
1904+
1905+
Response deleteResponse = makeRequest(client(), "DELETE", SecurityAnalyticsPlugin.DETECTOR_BASE_URI + "/" + detectorId, Collections.emptyMap(), null);
1906+
Assert.assertEquals("Delete detector failed", RestStatus.OK, restStatus(deleteResponse));
1907+
}
18801908
}

0 commit comments

Comments
 (0)