8
8
import java .util .Map ;
9
9
import org .apache .logging .log4j .LogManager ;
10
10
import org .apache .logging .log4j .Logger ;
11
- import org .opensearch .common .io .stream .StreamInput ;
12
- import org .opensearch .common .io .stream .StreamOutput ;
13
- import org .opensearch .common .io .stream .Writeable ;
14
- import org .opensearch .common .xcontent .XContentParserUtils ;
15
- import org .opensearch .commons .alerting .model .CronSchedule ;
16
- import org .opensearch .commons .alerting .model .Schedule ;
17
- import org .opensearch .commons .authuser .User ;
11
+ import org .opensearch .core .common .io .stream .StreamInput ;
12
+ import org .opensearch .core .common .io .stream .StreamOutput ;
13
+ import org .opensearch .core .common .io .stream .Writeable ;
14
+ import org .opensearch .core .xcontent .XContentParserUtils ;
18
15
import org .opensearch .core .ParseField ;
19
16
import org .opensearch .core .xcontent .NamedXContentRegistry ;
20
17
import org .opensearch .core .xcontent .ToXContent ;
21
18
import org .opensearch .core .xcontent .ToXContentObject ;
22
19
import org .opensearch .core .xcontent .XContentBuilder ;
23
20
import org .opensearch .core .xcontent .XContentParser ;
21
+ import org .opensearch .commons .alerting .model .CronSchedule ;
22
+ import org .opensearch .commons .alerting .model .Schedule ;
23
+ import org .opensearch .commons .authuser .User ;
24
+
24
25
import java .io .IOException ;
25
26
import java .time .Instant ;
26
27
import java .util .ArrayList ;
27
- import java .util .Arrays ;
28
28
import java .util .Collections ;
29
29
import java .util .List ;
30
30
import java .util .Locale ;
31
31
import java .util .Objects ;
32
32
33
- import java .util .stream .Collectors ;
34
-
35
33
public class Detector implements Writeable , ToXContentObject {
36
34
37
35
private static final Logger log = LogManager .getLogger (Detector .class );
@@ -50,8 +48,11 @@ public class Detector implements Writeable, ToXContentObject {
50
48
public static final String TRIGGERS_FIELD = "triggers" ;
51
49
public static final String LAST_UPDATE_TIME_FIELD = "last_update_time" ;
52
50
public static final String ENABLED_TIME_FIELD = "enabled_time" ;
51
+ public static final String THREAT_INTEL_ENABLED_FIELD = "threat_intel_enabled" ;
53
52
public static final String ALERTING_MONITOR_ID = "monitor_id" ;
54
53
54
+ public static final String ALERTING_WORKFLOW_ID = "workflow_ids" ;
55
+
55
56
public static final String BUCKET_MONITOR_ID_RULE_ID = "bucket_monitor_id_rule_id" ;
56
57
private static final String RULE_TOPIC_INDEX = "rule_topic_index" ;
57
58
@@ -79,6 +80,8 @@ public class Detector implements Writeable, ToXContentObject {
79
80
80
81
private String name ;
81
82
83
+ private Boolean threatIntelEnabled ;
84
+
82
85
private Boolean enabled ;
83
86
84
87
private Schedule schedule ;
@@ -99,6 +102,8 @@ public class Detector implements Writeable, ToXContentObject {
99
102
100
103
private Map <String , String > ruleIdMonitorIdMap ;
101
104
105
+ private List <String > workflowIds ;
106
+
102
107
private String ruleIndex ;
103
108
104
109
private String alertsIndex ;
@@ -117,7 +122,8 @@ public Detector(String id, Long version, String name, Boolean enabled, Schedule
117
122
Instant lastUpdateTime , Instant enabledTime , String logType ,
118
123
User user , List <DetectorInput > inputs , List <DetectorTrigger > triggers , List <String > monitorIds ,
119
124
String ruleIndex , String alertsIndex , String alertsHistoryIndex , String alertsHistoryIndexPattern ,
120
- String findingsIndex , String findingsIndexPattern , Map <String , String > rulePerMonitor ) {
125
+ String findingsIndex , String findingsIndexPattern , Map <String , String > rulePerMonitor ,
126
+ List <String > workflowIds , Boolean threatIntelEnabled ) {
121
127
this .type = DETECTOR_TYPE ;
122
128
123
129
this .id = id != null ? id : NO_ID ;
@@ -139,6 +145,8 @@ public Detector(String id, Long version, String name, Boolean enabled, Schedule
139
145
this .findingsIndexPattern = findingsIndexPattern ;
140
146
this .ruleIdMonitorIdMap = rulePerMonitor ;
141
147
this .logType = logType ;
148
+ this .workflowIds = workflowIds != null ? workflowIds : null ;
149
+ this .threatIntelEnabled = threatIntelEnabled != null && threatIntelEnabled ;
142
150
143
151
if (enabled ) {
144
152
Objects .requireNonNull (enabledTime );
@@ -159,13 +167,15 @@ public Detector(StreamInput sin) throws IOException {
159
167
sin .readList (DetectorInput ::readFrom ),
160
168
sin .readList (DetectorTrigger ::readFrom ),
161
169
sin .readStringList (),
162
- sin .readString (),
163
- sin .readString (),
164
- sin .readString (),
165
- sin .readString (),
166
- sin .readString (),
167
- sin .readString (),
168
- sin .readMap (StreamInput ::readString , StreamInput ::readString )
170
+ sin .readOptionalString (),
171
+ sin .readOptionalString (),
172
+ sin .readOptionalString (),
173
+ sin .readOptionalString (),
174
+ sin .readOptionalString (),
175
+ sin .readOptionalString (),
176
+ sin .readMap (StreamInput ::readString , StreamInput ::readString ),
177
+ sin .readStringList (),
178
+ sin .readBoolean ()
169
179
);
170
180
}
171
181
@@ -197,9 +207,18 @@ public void writeTo(StreamOutput out) throws IOException {
197
207
it .writeTo (out );
198
208
}
199
209
out .writeStringCollection (monitorIds );
200
- out .writeString (ruleIndex );
201
-
210
+ out .writeOptionalString (ruleIndex );
211
+ out .writeOptionalString (alertsIndex );
212
+ out .writeOptionalString (alertsHistoryIndex );
213
+ out .writeOptionalString (alertsHistoryIndexPattern );
214
+ out .writeOptionalString (findingsIndex );
215
+ out .writeOptionalString (findingsIndexPattern );
202
216
out .writeMap (ruleIdMonitorIdMap , StreamOutput ::writeString , StreamOutput ::writeString );
217
+
218
+ if (workflowIds != null ) {
219
+ out .writeStringCollection (workflowIds );
220
+ }
221
+ out .writeBoolean (threatIntelEnabled );
203
222
}
204
223
205
224
public XContentBuilder toXContentWithUser (XContentBuilder builder , Params params ) throws IOException {
@@ -228,6 +247,7 @@ private XContentBuilder createXContentBuilder(XContentBuilder builder, ToXConten
228
247
}
229
248
}
230
249
250
+ builder .field (THREAT_INTEL_ENABLED_FIELD , threatIntelEnabled );
231
251
builder .field (ENABLED_FIELD , enabled );
232
252
233
253
if (enabledTime == null ) {
@@ -253,6 +273,14 @@ private XContentBuilder createXContentBuilder(XContentBuilder builder, ToXConten
253
273
}
254
274
255
275
builder .field (ALERTING_MONITOR_ID , monitorIds );
276
+
277
+ if (workflowIds == null ) {
278
+ builder .nullField (ALERTING_WORKFLOW_ID );
279
+ } else {
280
+ builder .field (ALERTING_WORKFLOW_ID , workflowIds );
281
+ }
282
+
283
+
256
284
builder .field (BUCKET_MONITOR_ID_RULE_ID , ruleIdMonitorIdMap );
257
285
builder .field (RULE_TOPIC_INDEX , ruleIndex );
258
286
builder .field (ALERTS_INDEX , alertsIndex );
@@ -261,7 +289,6 @@ private XContentBuilder createXContentBuilder(XContentBuilder builder, ToXConten
261
289
builder .field (FINDINGS_INDEX , findingsIndex );
262
290
builder .field (FINDINGS_INDEX_PATTERN , findingsIndexPattern );
263
291
264
-
265
292
if (params .paramAsBoolean ("with_type" , false )) {
266
293
builder .endObject ();
267
294
}
@@ -299,6 +326,7 @@ public static Detector parse(XContentParser xcp, String id, Long version) throws
299
326
List <DetectorInput > inputs = new ArrayList <>();
300
327
List <DetectorTrigger > triggers = new ArrayList <>();
301
328
List <String > monitorIds = new ArrayList <>();
329
+ List <String > workflowIds = new ArrayList <>();
302
330
Map <String , String > rulePerMonitor = new HashMap <>();
303
331
304
332
String ruleIndex = null ;
@@ -307,6 +335,7 @@ public static Detector parse(XContentParser xcp, String id, Long version) throws
307
335
String alertsHistoryIndexPattern = null ;
308
336
String findingsIndex = null ;
309
337
String findingsIndexPattern = null ;
338
+ Boolean enableThreatIntel = false ;
310
339
311
340
XContentParserUtils .ensureExpectedToken (XContentParser .Token .START_OBJECT , xcp .currentToken (), xcp );
312
341
while (xcp .nextToken () != XContentParser .Token .END_OBJECT ) {
@@ -330,6 +359,9 @@ public static Detector parse(XContentParser xcp, String id, Long version) throws
330
359
case ENABLED_FIELD :
331
360
enabled = xcp .booleanValue ();
332
361
break ;
362
+ case THREAT_INTEL_ENABLED_FIELD :
363
+ enableThreatIntel = xcp .booleanValue ();
364
+ break ;
333
365
case SCHEDULE_FIELD :
334
366
schedule = Schedule .parse (xcp );
335
367
break ;
@@ -374,6 +406,15 @@ public static Detector parse(XContentParser xcp, String id, Long version) throws
374
406
monitorIds .add (monitorId );
375
407
}
376
408
break ;
409
+ case ALERTING_WORKFLOW_ID :
410
+ XContentParserUtils .ensureExpectedToken (XContentParser .Token .START_ARRAY , xcp .currentToken (), xcp );
411
+ while (xcp .nextToken () != XContentParser .Token .END_ARRAY ) {
412
+ String workflowId = xcp .textOrNull ();
413
+ if (workflowId != null ) {
414
+ workflowIds .add (workflowId );
415
+ }
416
+ }
417
+ break ;
377
418
case BUCKET_MONITOR_ID_RULE_ID :
378
419
rulePerMonitor = xcp .mapStrings ();
379
420
break ;
@@ -429,7 +470,9 @@ public static Detector parse(XContentParser xcp, String id, Long version) throws
429
470
alertsHistoryIndexPattern ,
430
471
findingsIndex ,
431
472
findingsIndexPattern ,
432
- rulePerMonitor
473
+ rulePerMonitor ,
474
+ workflowIds ,
475
+ enableThreatIntel
433
476
);
434
477
}
435
478
@@ -566,10 +609,30 @@ public void setRuleIdMonitorIdMap(Map<String, String> ruleIdMonitorIdMap) {
566
609
this .ruleIdMonitorIdMap = ruleIdMonitorIdMap ;
567
610
}
568
611
612
+ public void setWorkflowIds (List <String > workflowIds ) {
613
+ this .workflowIds = workflowIds ;
614
+ }
615
+
616
+ public void setThreatIntelEnabled (boolean threatIntelEnabled ) {
617
+ this .threatIntelEnabled = threatIntelEnabled ;
618
+ }
619
+
620
+ public List <String > getWorkflowIds () {
621
+ return workflowIds ;
622
+ }
623
+
569
624
public String getDocLevelMonitorId () {
570
625
return ruleIdMonitorIdMap .get (DOC_LEVEL_MONITOR );
571
626
}
572
627
628
+ public boolean isWorkflowSupported () {
629
+ return workflowIds != null && !workflowIds .isEmpty ();
630
+ }
631
+
632
+ public Boolean getThreatIntelEnabled () {
633
+ return threatIntelEnabled ;
634
+ }
635
+
573
636
@ Override
574
637
public boolean equals (Object o ) {
575
638
if (this == o ) return true ;
0 commit comments