@@ -129,48 +129,85 @@ public void evaluateRules(List<AlertType> alerts) {
129129 return ;
130130
131131 List <UtmAlertResponseRule > rules = alertResponseRuleRepository .findAllByRuleActiveIsTrue ();
132- if (CollectionUtils .isEmpty (rules ))
133- return ;
134132
135133 // Excluding alerts tagged as false positive
136134 alerts = alerts .stream ().filter (a -> (CollectionUtils .isEmpty (a .getTags ()) || !a .getTags ().contains ("False positive" )))
137135 .collect (Collectors .toList ());
138136
137+ // Do nothing if there is no valid alerts to check
138+ if (CollectionUtils .isEmpty (alerts ))
139+ return ;
140+
139141 String alertJsonArray = new Gson ().toJson (alerts );
140142 for (UtmAlertResponseRule rule : rules ) {
141- List <FilterType > conditions = new ArrayList <>();
142143 List <String > agentNames = networkScanRepository .findAgentNamesByPlatform (rule .getAgentPlatform ());
143- if (!CollectionUtils .isEmpty (agentNames ))
144- conditions .add (new FilterType (Constants .alertDataSourceKeyword , OperatorType .IS_ONE_OF , agentNames ));
145144
146- if (StringUtils .hasText (rule .getRuleConditions ()))
147- conditions .addAll (new Gson ().fromJson (rule .getRuleConditions (), TypeToken .getParameterized (List .class , FilterType .class ).getType ()));
145+ if (CollectionUtils .isEmpty (agentNames ))
146+ continue ;
147+
148+ // Matching agents (these are the alerts made from logs coming from an agent)
149+ //------------------------------------------------------------------------------------------
150+ createResponseRuleExecution (rule ,alertJsonArray ,agentNames ,true );
151+
152+ // Then the alerts that match the filters but aren't from an agent, gets executed using the default agent if there is one
153+ //-----------------------------------------------------------------------------------------------------------------------
154+ if (StringUtils .hasText (rule .getDefaultAgent ())) {
155+ createResponseRuleExecution (rule ,alertJsonArray ,agentNames ,false );
156+ }
157+ }
158+ } catch (Exception e ) {
159+ String msg = ctx + ": " + e .getLocalizedMessage ();
160+ log .error (msg );
161+ eventService .createEvent (msg , ApplicationEventType .ERROR );
162+ }
163+ }
164+
165+ private void createResponseRuleExecution (UtmAlertResponseRule rule , String alertJsonArray , List <String > agentNames , boolean isAgent ) throws Exception {
166+ final String ctx = CLASSNAME + ".createResponseRuleExecution" ;
167+ List <FilterType > conditions = new ArrayList <>();
168+ try {
169+ // Common conditions
170+ if (StringUtils .hasText (rule .getRuleConditions ()))
171+ conditions .addAll (new Gson ().fromJson (rule .getRuleConditions (), TypeToken .getParameterized (List .class , FilterType .class ).getType ()));
148172
149- if (StringUtils .hasText (rule .getExcludedAgents ()))
150- conditions .add (new FilterType (Constants .alertDataSourceKeyword , OperatorType .IS_NOT_ONE_OF , List .of (rule .getExcludedAgents ().split ("," ))));
173+ if (StringUtils .hasText (rule .getExcludedAgents ()))
174+ conditions .add (new FilterType (Constants .alertDataSourceKeyword , OperatorType .IS_NOT_ONE_OF , List .of (rule .getExcludedAgents ().split ("," ))));
151175
152- Filter filter = buildFilters (conditions );
153- List <?> matches = UtilJson .read ("$[?]" , alertJsonArray , filter );
176+ // Specific condition for agent and non agents
177+ if (isAgent ) {
178+ conditions .add (new FilterType (Constants .alertDataSourceKeyword , OperatorType .IS_ONE_OF , agentNames ));
179+ } else {
180+ conditions .add (new FilterType (Constants .alertDataSourceKeyword , OperatorType .IS_NOT_ONE_OF , agentNames ));
181+ }
154182
155- if (CollectionUtils .isEmpty (matches ))
156- continue ;
183+ // Processing the alerts and generating the rule executions
184+ Filter filter = buildFilters (conditions );
185+ List <?> matches = UtilJson .read ("$[?]" , alertJsonArray , filter );
186+
187+ if (!CollectionUtils .isEmpty (matches )) {
157188
158189 for (Object match : matches ) {
159190 String matchAsJson = new Gson ().toJson (match );
160191
161192 UtmAlertResponseRuleExecution exe = new UtmAlertResponseRuleExecution ();
162- exe .setAgent (UtilJson .read ("$.dataSource" , matchAsJson ));
193+ // Execution agent takes the rule's default agent if the alert was generated by logs from non agent datasource
194+ if (isAgent ) {
195+ exe .setAgent (UtilJson .read ("$.dataSource" , matchAsJson ));
196+ } else {
197+ exe .setAgent (rule .getDefaultAgent ());
198+ }
199+
163200 exe .setAlertId (UtilJson .read ("$.id" , matchAsJson ));
164201 exe .setRuleId (rule .getId ());
165202 exe .setCommand (buildCommand (rule .getRuleCmd (), matchAsJson ));
166203 exe .setExecutionStatus (RuleExecutionStatus .PENDING );
167204 alertResponseRuleExecutionRepository .save (exe );
168205 }
169206 }
207+
170208 } catch (Exception e ) {
171209 String msg = ctx + ": " + e .getLocalizedMessage ();
172- log .error (msg );
173- eventService .createEvent (msg , ApplicationEventType .ERROR );
210+ throw new Exception (msg );
174211 }
175212 }
176213
0 commit comments