|
26 | 26 |
|
27 | 27 | import static java.util.Collections.emptyList;
|
28 | 28 | import static org.opensearch.securityanalytics.TestHelpers.randomDetectorType;
|
| 29 | +import static org.opensearch.securityanalytics.TestHelpers.randomDetectorWithInputs; |
29 | 30 | import static org.opensearch.securityanalytics.TestHelpers.randomDetectorWithInputsAndThreatIntel;
|
30 | 31 | import static org.opensearch.securityanalytics.TestHelpers.randomDetectorWithInputsAndThreatIntelAndTriggers;
|
31 | 32 | import static org.opensearch.securityanalytics.TestHelpers.randomDoc;
|
32 | 33 | import static org.opensearch.securityanalytics.TestHelpers.randomDocWithIpIoc;
|
| 34 | +import static org.opensearch.securityanalytics.TestHelpers.randomDocWithNullField; |
33 | 35 | import static org.opensearch.securityanalytics.TestHelpers.randomIndex;
|
| 36 | +import static org.opensearch.securityanalytics.TestHelpers.randomNullRule; |
34 | 37 | import static org.opensearch.securityanalytics.TestHelpers.randomRule;
|
35 | 38 | import static org.opensearch.securityanalytics.TestHelpers.windowsIndexMapping;
|
36 | 39 | import static org.opensearch.securityanalytics.settings.SecurityAnalyticsSettings.ENABLE_WORKFLOW_USAGE;
|
@@ -150,6 +153,94 @@ public void testCreateDetectorWithThreatIntelEnabled_updateDetectorWithThreatInt
|
150 | 153 | assertEquals(1, noOfSigmaRuleMatches);
|
151 | 154 | }
|
152 | 155 |
|
| 156 | + public void testCreateDetectorForSigmaRuleWithNullCondition() throws IOException { |
| 157 | + |
| 158 | + updateClusterSetting(ENABLE_WORKFLOW_USAGE.getKey(), "true"); |
| 159 | + String index = createTestIndex(randomIndex(), windowsIndexMapping()); |
| 160 | + |
| 161 | + // Execute CreateMappingsAction to add alias mapping for index |
| 162 | + Request createMappingRequest = new Request("POST", SecurityAnalyticsPlugin.MAPPER_BASE_URI); |
| 163 | + // both req params and req body are supported |
| 164 | + createMappingRequest.setJsonEntity( |
| 165 | + "{ \"index_name\":\"" + index + "\"," + |
| 166 | + " \"rule_topic\":\"" + randomDetectorType() + "\", " + |
| 167 | + " \"partial\":true" + |
| 168 | + "}" |
| 169 | + ); |
| 170 | + |
| 171 | + Response createMappingResponse = client().performRequest(createMappingRequest); |
| 172 | + |
| 173 | + assertEquals(HttpStatus.SC_OK, createMappingResponse.getStatusLine().getStatusCode()); |
| 174 | + |
| 175 | + String testOpCode = "Test"; |
| 176 | + |
| 177 | + String randomDocRuleId = createRule(randomNullRule()); |
| 178 | + List<DetectorRule> detectorRules = List.of(new DetectorRule(randomDocRuleId)); |
| 179 | + DetectorInput input = new DetectorInput("windows detector for security analytics", List.of("windows"), detectorRules, |
| 180 | + emptyList()); |
| 181 | + DetectorTrigger trigger = new DetectorTrigger("all", "all", "high", List.of(randomDetectorType()), emptyList(), emptyList(), List.of(), emptyList(), List.of(DetectorTrigger.RULES_DETECTION_TYPE, DetectorTrigger.THREAT_INTEL_DETECTION_TYPE)); |
| 182 | + Detector detector = randomDetectorWithInputs(List.of(input)); |
| 183 | + Response createResponse = makeRequest(client(), "POST", SecurityAnalyticsPlugin.DETECTOR_BASE_URI, Collections.emptyMap(), toHttpEntity(detector)); |
| 184 | + |
| 185 | + String request = "{\n" + |
| 186 | + " \"query\" : {\n" + |
| 187 | + " \"match_all\":{\n" + |
| 188 | + " }\n" + |
| 189 | + " }\n" + |
| 190 | + "}"; |
| 191 | + |
| 192 | + assertEquals("Create detector failed", RestStatus.CREATED, restStatus(createResponse)); |
| 193 | + Map<String, Object> responseBody = asMap(createResponse); |
| 194 | + |
| 195 | + String detectorId = responseBody.get("_id").toString(); |
| 196 | + request = "{\n" + |
| 197 | + " \"query\" : {\n" + |
| 198 | + " \"match\":{\n" + |
| 199 | + " \"_id\": \"" + detectorId + "\"\n" + |
| 200 | + " }\n" + |
| 201 | + " }\n" + |
| 202 | + "}"; |
| 203 | + List<SearchHit> hits = executeSearch(Detector.DETECTORS_INDEX, request); |
| 204 | + SearchHit hit = hits.get(0); |
| 205 | + Map<String, Object> detectorMap = (HashMap<String, Object>) (hit.getSourceAsMap().get("detector")); |
| 206 | + List inputArr = (List) detectorMap.get("inputs"); |
| 207 | + |
| 208 | + |
| 209 | + List<String> monitorIds = ((List<String>) (detectorMap).get("monitor_id")); |
| 210 | + assertEquals(1, monitorIds.size()); |
| 211 | + |
| 212 | + Response getMonitorResponse = getAlertingMonitor(client(), monitorIds.get(0)); |
| 213 | + Map<String, Object> alertingMonitor = asMap(getMonitorResponse); |
| 214 | + assertNotNull(alertingMonitor); |
| 215 | + String workflowId = ((List<String>) detectorMap.get("workflow_ids")).get(0); |
| 216 | + |
| 217 | + indexDoc(index, "1", randomDocWithNullField()); |
| 218 | + indexDoc(index, "2", randomDoc()); |
| 219 | + |
| 220 | + Response executeResponse = executeAlertingWorkflow(workflowId, Collections.emptyMap()); |
| 221 | + |
| 222 | + List<Map<String, Object>> monitorRunResults = (List<Map<String, Object>>) entityAsMap(executeResponse).get("monitor_run_results"); |
| 223 | + assertEquals(1, monitorRunResults.size()); |
| 224 | + |
| 225 | + Map<String, Object> docLevelQueryResults = ((List<Map<String, Object>>) ((Map<String, Object>) monitorRunResults.get(0).get("input_results")).get("results")).get(0); |
| 226 | + int noOfSigmaRuleMatches = docLevelQueryResults.size(); |
| 227 | + assertEquals(1, noOfSigmaRuleMatches); |
| 228 | + String queryId = docLevelQueryResults.keySet().stream().findAny().get(); |
| 229 | + ArrayList<String> docs = (ArrayList<String>) docLevelQueryResults.get(queryId); |
| 230 | + assertEquals(docs.size(), 1); |
| 231 | + |
| 232 | + indexDoc(index, "3", randomDoc()); |
| 233 | + Response executeResponse1 = executeAlertingWorkflow(workflowId, Collections.emptyMap()); |
| 234 | + |
| 235 | + List<Map<String, Object>> monitorRunResults1 = (List<Map<String, Object>>) entityAsMap(executeResponse1).get("monitor_run_results"); |
| 236 | + assertEquals(1, monitorRunResults1.size()); |
| 237 | + |
| 238 | + Map<String, Object> docLevelQueryResults1 = ((List<Map<String, Object>>) ((Map<String, Object>) monitorRunResults1.get(0).get("input_results")).get("results")).get(0); |
| 239 | + int noOfSigmaRuleMatches1 = docLevelQueryResults1.size(); |
| 240 | + assertEquals(0, noOfSigmaRuleMatches1); |
| 241 | + |
| 242 | + } |
| 243 | + |
153 | 244 | public void testCreateDetectorWithThreatIntelDisabled_updateDetectorWithThreatIntelEnabled() throws IOException {
|
154 | 245 |
|
155 | 246 | updateClusterSetting(ENABLE_WORKFLOW_USAGE.getKey(), "true");
|
@@ -594,8 +685,8 @@ public void testCreateDetectorWithThreatIntelDisabled_triggerWithThreatIntelDete
|
594 | 685 | verifyWorkflow(detectorMap, monitorIds, 1);
|
595 | 686 |
|
596 | 687 | int i = 1;
|
597 |
| - while (i<4) { |
598 |
| - indexDoc(index, i + "", randomDocWithIpIoc(5, 3, i+"")); |
| 688 | + while (i < 4) { |
| 689 | + indexDoc(index, i + "", randomDocWithIpIoc(5, 3, i + "")); |
599 | 690 | i++;
|
600 | 691 | }
|
601 | 692 | String workflowId = ((List<String>) detectorMap.get("workflow_ids")).get(0);
|
@@ -686,8 +777,8 @@ public void testCreateDetectorWithThreatIntelDisabled_triggerWithRulesDetectionT
|
686 | 777 | verifyWorkflow(detectorMap, monitorIds, 1);
|
687 | 778 |
|
688 | 779 | int i = 1;
|
689 |
| - while (i<4) { |
690 |
| - indexDoc(index, i + "", randomDocWithIpIoc(5, 3, i+"")); |
| 780 | + while (i < 4) { |
| 781 | + indexDoc(index, i + "", randomDocWithIpIoc(5, 3, i + "")); |
691 | 782 | i++;
|
692 | 783 | }
|
693 | 784 | String workflowId = ((List<String>) detectorMap.get("workflow_ids")).get(0);
|
|
0 commit comments