|
39 | 39 | import org.opensearch.securityanalytics.threatIntel.model.IocUploadSource; |
40 | 40 | import org.opensearch.securityanalytics.threatIntel.model.SATIFSourceConfig; |
41 | 41 | import org.opensearch.securityanalytics.threatIntel.model.SATIFSourceConfigDto; |
| 42 | +import org.opensearch.securityanalytics.util.SecurityAnalyticsException; |
42 | 43 |
|
43 | 44 | import java.time.Instant; |
44 | 45 | import java.util.ArrayList; |
45 | 46 | import java.util.HashSet; |
46 | 47 | import java.util.LinkedHashMap; |
47 | 48 | import java.util.LinkedHashSet; |
48 | 49 | import java.util.List; |
| 50 | +import java.util.Locale; |
49 | 51 | import java.util.Map; |
50 | 52 | import java.util.Set; |
51 | 53 | import java.util.SortedMap; |
@@ -158,7 +160,7 @@ public void createIocAndTIFSourceConfig( |
158 | 160 | )); |
159 | 161 | }, |
160 | 162 | e -> { |
161 | | - log.error("Failed to download and save IOCs for source config [{}]", indexSaTifSourceConfigResponse.getId()); |
| 163 | + log.error("Failed to download and save IOCs for threat intel source config [{}]", indexSaTifSourceConfigResponse.getId()); |
162 | 164 | saTifSourceConfigService.deleteTIFSourceConfig(indexSaTifSourceConfigResponse, ActionListener.wrap( |
163 | 165 | deleteResponse -> { |
164 | 166 | log.debug("Successfully deleted threat intel source config [{}]", indexSaTifSourceConfigResponse.getId()); |
@@ -204,15 +206,15 @@ public void downloadAndSaveIOCs(SATIFSourceConfig saTifSourceConfig, |
204 | 206 | if (saTifSourceConfig.getIocTypes().contains(stix2IOC.getType().toString())) { |
205 | 207 | validStix2IocList.add(stix2IOC); |
206 | 208 | } else { |
207 | | - log.error("{} is not a supported Ioc type for tif source config {}. Skipping IOC {}: of type {} value {}", |
| 209 | + log.error("{} is not a supported Ioc type for threat intel source config {}. Skipping IOC {}: of type {} value {}", |
208 | 210 | stix2IOC.getType().toString(), saTifSourceConfig.getId(), |
209 | 211 | stix2IOC.getId(), stix2IOC.getType().toString(), stix2IOC.getValue() |
210 | 212 | ); |
211 | 213 | } |
212 | 214 | } |
213 | 215 | if (validStix2IocList.isEmpty()) { |
214 | 216 | log.error("No supported IOCs to index"); |
215 | | - actionListener.onFailure(new OpenSearchStatusException("No compatible Iocs were uploaded for config " + saTifSourceConfig.getName(), RestStatus.BAD_REQUEST)); |
| 217 | + actionListener.onFailure(SecurityAnalyticsException.wrap(new OpenSearchStatusException("No compatible Iocs were uploaded for threat intel source config " + saTifSourceConfig.getName(), RestStatus.BAD_REQUEST))); |
216 | 218 | return; |
217 | 219 | } |
218 | 220 | stix2IOCFetchService.onlyIndexIocs(saTifSourceConfig, validStix2IocList, actionListener); |
@@ -274,14 +276,18 @@ public void updateIocAndTIFSourceConfig( |
274 | 276 | saTifSourceConfigService.getTIFSourceConfig(saTifSourceConfigDto.getId(), ActionListener.wrap( |
275 | 277 | retrievedSaTifSourceConfig -> { |
276 | 278 | if (TIFJobState.AVAILABLE.equals(retrievedSaTifSourceConfig.getState()) == false && TIFJobState.REFRESH_FAILED.equals(retrievedSaTifSourceConfig.getState()) == false) { |
277 | | - log.error("Invalid TIF job state. Expecting {} or {} but received {}", TIFJobState.AVAILABLE, TIFJobState.REFRESH_FAILED, retrievedSaTifSourceConfig.getState()); |
278 | | - listener.onFailure(new OpenSearchException("Invalid TIF job state. Expecting {} or {} but received {}", TIFJobState.AVAILABLE, TIFJobState.REFRESH_FAILED, retrievedSaTifSourceConfig.getState())); |
| 279 | + log.error("Invalid threat intel source config state. Expecting {} or {} but received {}", TIFJobState.AVAILABLE, TIFJobState.REFRESH_FAILED, retrievedSaTifSourceConfig.getState()); |
| 280 | + listener.onFailure(SecurityAnalyticsException.wrap(new OpenSearchStatusException( |
| 281 | + String.format(Locale.getDefault(), "Invalid threat intel source config state. Expecting %s or %s but received %s", TIFJobState.AVAILABLE, TIFJobState.REFRESH_FAILED, retrievedSaTifSourceConfig.getState()), |
| 282 | + RestStatus.BAD_REQUEST))); |
279 | 283 | return; |
280 | 284 | } |
281 | 285 |
|
282 | 286 | if (false == saTifSourceConfigDto.getType().equals(retrievedSaTifSourceConfig.getType())) { |
283 | | - log.error("Unable to update source config, type cannot change from {} to {}", retrievedSaTifSourceConfig.getType(), saTifSourceConfigDto.getType()); |
284 | | - listener.onFailure(new OpenSearchException("Unable to update source config, type cannot change from {} to {}", retrievedSaTifSourceConfig.getType(), saTifSourceConfigDto.getType())); |
| 287 | + log.error("Unable to update threat intel source config, type cannot change from {} to {}", retrievedSaTifSourceConfig.getType(), saTifSourceConfigDto.getType()); |
| 288 | + listener.onFailure(SecurityAnalyticsException.wrap(new OpenSearchStatusException( |
| 289 | + String.format(Locale.getDefault(), "Unable to update threat intel source config, type cannot change from %s to %s", retrievedSaTifSourceConfig.getType(), saTifSourceConfigDto.getType()), |
| 290 | + RestStatus.BAD_REQUEST))); |
285 | 291 | return; |
286 | 292 | } |
287 | 293 |
|
@@ -345,7 +351,6 @@ private void storeAndDeleteIocIndices(List<STIX2IOC> stix2IOCList, ActionListene |
345 | 351 |
|
346 | 352 | saTifSourceConfigService.getClusterState(ActionListener.wrap( |
347 | 353 | clusterStateResponse -> { |
348 | | - List<String> iocTypes = updatedSaTifSourceConfig.getIocTypes(); |
349 | 354 | IocStoreConfig iocStoreConfig = updatedSaTifSourceConfig.getIocStoreConfig(); |
350 | 355 | Set<String> activeIndices = new HashSet<>(); |
351 | 356 | Set<String> indicesToDelete = new HashSet<>(); |
@@ -388,11 +393,13 @@ private void storeAndDeleteIocIndices(List<STIX2IOC> stix2IOCList, ActionListene |
388 | 393 | ), iocIndexPatterns.toArray(new String[0])); |
389 | 394 | }, |
390 | 395 | e -> { |
391 | | - log.error("Failed to download and save IOCs for source config [{}]", updatedSaTifSourceConfig.getId()); |
| 396 | + log.error("Failed to download and save IOCs for threat intel source config [{}]", updatedSaTifSourceConfig.getId(), e); |
392 | 397 | markSourceConfigAsAction(updatedSaTifSourceConfig, TIFJobState.REFRESH_FAILED, ActionListener.wrap( |
393 | 398 | r -> { |
394 | 399 | log.info("Set threat intel source config as REFRESH_FAILED for [{}]", updatedSaTifSourceConfig.getId()); |
395 | | - listener.onFailure(new OpenSearchException("Set threat intel source config as REFRESH_FAILED for [{}]", updatedSaTifSourceConfig.getId())); |
| 400 | + listener.onFailure(SecurityAnalyticsException.wrap(new OpenSearchException( |
| 401 | + String.format(Locale.getDefault(), "Failed to download and save IOCs for threat intel source config [%s]. Set source config as REFRESH_FAILED", updatedSaTifSourceConfig.getId()), |
| 402 | + e))); |
396 | 403 | }, ex -> { |
397 | 404 | log.error("Failed to set threat intel source config as REFRESH_FAILED for [{}]", updatedSaTifSourceConfig.getId()); |
398 | 405 | listener.onFailure(ex); |
@@ -423,14 +430,18 @@ public void refreshTIFSourceConfig( |
423 | 430 | saTifSourceConfigService.getTIFSourceConfig(saTifSourceConfigId, ActionListener.wrap( |
424 | 431 | saTifSourceConfig -> { |
425 | 432 | if (saTifSourceConfig.getType() == IOC_UPLOAD) { |
426 | | - log.error("Unable to refresh source config [{}] with a source type of [{}]", saTifSourceConfig.getId(), IOC_UPLOAD); |
427 | | - listener.onFailure(new OpenSearchException("Unable to refresh source config [{}] with a source type of [{}]", saTifSourceConfig.getId(), IOC_UPLOAD)); |
| 433 | + log.error("Unable to refresh threat intel source config [{}] with a source type of [{}]", saTifSourceConfig.getId(), IOC_UPLOAD); |
| 434 | + listener.onFailure(SecurityAnalyticsException.wrap(new OpenSearchStatusException( |
| 435 | + String.format(Locale.getDefault(), "Unable to refresh threat intel source config [%s] with a source type of [%s]", saTifSourceConfig.getId(), IOC_UPLOAD), |
| 436 | + RestStatus.BAD_REQUEST))); |
428 | 437 | return; |
429 | 438 | } |
430 | 439 |
|
431 | 440 | if (TIFJobState.AVAILABLE.equals(saTifSourceConfig.getState()) == false && TIFJobState.REFRESH_FAILED.equals(saTifSourceConfig.getState()) == false) { |
432 | | - log.error("Invalid TIF job state. Expecting {} or {} but received {}", TIFJobState.AVAILABLE, TIFJobState.REFRESH_FAILED, saTifSourceConfig.getState()); |
433 | | - listener.onFailure(new OpenSearchException("Invalid TIF job state. Expecting {} or {} but received {}", TIFJobState.AVAILABLE, TIFJobState.REFRESH_FAILED, saTifSourceConfig.getState())); |
| 441 | + log.error("Invalid threat intel source config state. Expecting {} or {} but received {}", TIFJobState.AVAILABLE, TIFJobState.REFRESH_FAILED, saTifSourceConfig.getState()); |
| 442 | + listener.onFailure(SecurityAnalyticsException.wrap(new OpenSearchStatusException( |
| 443 | + String.format(Locale.getDefault(), "Invalid threat intel source config state. Expecting %s or %s but received %s", TIFJobState.AVAILABLE, TIFJobState.REFRESH_FAILED, saTifSourceConfig.getState()), |
| 444 | + RestStatus.BAD_REQUEST))); |
434 | 445 | return; |
435 | 446 | } |
436 | 447 |
|
@@ -488,14 +499,16 @@ private void downloadAndSaveIocsToRefresh(ActionListener<SATIFSourceConfigDto> l |
488 | 499 | )); |
489 | 500 | }, downloadAndSaveIocsError -> { |
490 | 501 | // Update source config as refresh failed |
491 | | - log.error("Failed to download and save IOCs for threat intel source config [{}]", updatedSourceConfig.getId()); |
| 502 | + log.error("Failed to download and save IOCs for threat intel source config [{}]", updatedSourceConfig.getId(), downloadAndSaveIocsError); |
492 | 503 | markSourceConfigAsAction(updatedSourceConfig, TIFJobState.REFRESH_FAILED, ActionListener.wrap( |
493 | 504 | r -> { |
494 | | - log.debug("Set threat intel source config as REFRESH_FAILED for [{}]", updatedSourceConfig.getId()); |
495 | | - listener.onFailure(new OpenSearchException("Set threat intel source config as REFRESH_FAILED for [{}]", updatedSourceConfig.getId())); |
496 | | - }, e -> { |
| 505 | + log.info("Set threat intel source config as REFRESH_FAILED for [{}]", updatedSourceConfig.getId()); |
| 506 | + listener.onFailure(SecurityAnalyticsException.wrap(new OpenSearchException( |
| 507 | + String.format(Locale.getDefault(), "Failed to download and save IOCs for threat intel source config [%s]. Set source config as REFRESH_FAILED", updatedSourceConfig.getId()), |
| 508 | + downloadAndSaveIocsError))); |
| 509 | + }, ex -> { |
497 | 510 | log.error("Failed to set threat intel source config as REFRESH_FAILED for [{}]", updatedSourceConfig.getId()); |
498 | | - listener.onFailure(e); |
| 511 | + listener.onFailure(ex); |
499 | 512 | } |
500 | 513 | )); |
501 | 514 | })); |
@@ -528,7 +541,7 @@ public void deleteTIFSourceConfig( |
528 | 541 | }, e -> { |
529 | 542 | log.error("Failed to get threat intel source config for [{}]", saTifSourceConfigId); |
530 | 543 | if (e instanceof IndexNotFoundException) { |
531 | | - listener.onFailure(new OpenSearchException("Threat intel source config [{}] not found", saTifSourceConfigId)); |
| 544 | + listener.onFailure(SecurityAnalyticsException.wrap(new OpenSearchStatusException(String.format(Locale.getDefault(),"Threat intel source config [%s] not found.", saTifSourceConfigId), RestStatus.NOT_FOUND))); |
532 | 545 | } else { |
533 | 546 | listener.onFailure(e); |
534 | 547 | } |
@@ -708,7 +721,7 @@ private void deleteAllIocsAndSourceConfig(String saTifSourceConfigId, ActionList |
708 | 721 | } |
709 | 722 | )); |
710 | 723 | }, e -> { |
711 | | - log.error("Failed to delete IOC indices for source config [{}]", updateSaTifSourceConfigResponse.getId()); |
| 724 | + log.error("Failed to delete IOC indices for threat intel source config [{}]", updateSaTifSourceConfigResponse.getId()); |
712 | 725 | listener.onFailure(e); |
713 | 726 | } |
714 | 727 | )); |
|
0 commit comments