32
32
import org .opensearch .securityanalytics .transport .TransportCorrelateFindingAction ;
33
33
import org .opensearch .securityanalytics .util .CorrelationIndices ;
34
34
35
+ import java .util .Arrays ;
35
36
import java .util .List ;
36
37
import java .util .Locale ;
37
38
import java .util .Map ;
@@ -94,6 +95,7 @@ public void insertCorrelatedFindings(String detectorType, Finding finding, Strin
94
95
request .indices (CorrelationIndices .CORRELATION_HISTORY_INDEX_PATTERN_REGEXP );
95
96
request .source (searchSourceBuilder );
96
97
request .preference (Preference .PRIMARY_FIRST .type ());
98
+ request .setCancelAfterTimeInterval (TimeValue .timeValueSeconds (30L ));
97
99
98
100
mSearchRequest .add (request );
99
101
}
@@ -195,6 +197,12 @@ public void insertCorrelatedFindings(String detectorType, Finding finding, Strin
195
197
}
196
198
197
199
public void insertOrphanFindings (String detectorType , Finding finding , float timestampFeature , Map <String , CustomLogType > logTypes ) {
200
+ if (logTypes .get (detectorType ) == null ) {
201
+ log .debug ("Missing detector type {} in the log types index for finding id {}. Keys in the index: {}" ,
202
+ detectorType , finding .getId (), Arrays .toString (logTypes .keySet ().toArray ()));
203
+ onFailure (new OpenSearchStatusException ("insertOrphanFindings null log types for detector type: " + detectorType , RestStatus .INTERNAL_SERVER_ERROR ));
204
+ }
205
+
198
206
SearchRequest searchRequest = getSearchMetadataIndexRequest (detectorType , finding , logTypes );
199
207
Map <String , Object > tags = logTypes .get (detectorType ).getTags ();
200
208
String correlationId = tags .get ("correlation_id" ).toString ();
@@ -251,7 +259,8 @@ public void insertOrphanFindings(String detectorType, Finding finding, float tim
251
259
onFailure (ex );
252
260
}
253
261
} else {
254
- onFailure (new OpenSearchStatusException (indexResponse .toString (), RestStatus .INTERNAL_SERVER_ERROR ));
262
+ onFailure (new OpenSearchStatusException ("Indexing failed with response {} " ,
263
+ indexResponse .status (), indexResponse .toString ()));
255
264
}
256
265
}, this ::onFailure ));
257
266
} else {
@@ -297,7 +306,8 @@ public void insertOrphanFindings(String detectorType, Finding finding, float tim
297
306
onFailure (ex );
298
307
}
299
308
} else {
300
- onFailure (new OpenSearchStatusException (indexResponse .toString (), RestStatus .INTERNAL_SERVER_ERROR ));
309
+ onFailure (new OpenSearchStatusException ("Indexing failed with response {} " ,
310
+ indexResponse .status (), indexResponse .toString ()));
301
311
}
302
312
}, this ::onFailure ));
303
313
} else {
@@ -323,6 +333,7 @@ public void insertOrphanFindings(String detectorType, Finding finding, float tim
323
333
request .indices (CorrelationIndices .CORRELATION_HISTORY_INDEX_PATTERN_REGEXP );
324
334
request .source (searchSourceBuilder );
325
335
request .preference (Preference .PRIMARY_FIRST .type ());
336
+ request .setCancelAfterTimeInterval (TimeValue .timeValueSeconds (30L ));
326
337
327
338
client .search (request , ActionListener .wrap (searchResponse -> {
328
339
if (searchResponse .isTimedOut ()) {
@@ -407,6 +418,9 @@ public void insertOrphanFindings(String detectorType, Finding finding, float tim
407
418
} catch (Exception ex ) {
408
419
onFailure (ex );
409
420
}
421
+ } else {
422
+ onFailure (new OpenSearchStatusException ("Indexing failed with response {} " ,
423
+ indexResponse .status (), indexResponse .toString ()));
410
424
}
411
425
}, this ::onFailure ));
412
426
} catch (Exception ex ) {
@@ -432,7 +446,7 @@ private void indexCorrelatedFindings(XContentBuilder builder) {
432
446
if (response .status ().equals (RestStatus .CREATED )) {
433
447
correlateFindingAction .onOperation ();
434
448
} else {
435
- onFailure (new OpenSearchStatusException (response . toString (), RestStatus . INTERNAL_SERVER_ERROR ));
449
+ onFailure (new OpenSearchStatusException ("Indexing failed with response {} " , response . status (), response . toString () ));
436
450
}
437
451
}, this ::onFailure ));
438
452
}
@@ -454,6 +468,7 @@ private SearchRequest getSearchMetadataIndexRequest(String detectorType, Finding
454
468
searchRequest .indices (CorrelationIndices .CORRELATION_METADATA_INDEX );
455
469
searchRequest .source (searchSourceBuilder );
456
470
searchRequest .preference (Preference .PRIMARY_FIRST .type ());
471
+ searchRequest .setCancelAfterTimeInterval (TimeValue .timeValueSeconds (30L ));
457
472
return searchRequest ;
458
473
}
459
474
0 commit comments