chg: [workflow] Small improvements and refactored behavior of if blocks

pull/8530/head
Sami Mokaddem 2022-06-09 14:08:43 +02:00
parent 03c2f074ef
commit 5a7da21f04
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
4 changed files with 63 additions and 23 deletions

View File

@ -117,12 +117,10 @@ class GraphWalker
return $triggers;
}
private function _getPathType($node_id, $path_type, $output_id)
private function _getPathType($node_id, $path_type)
{
$node = $this->graph[$node_id];
if (!empty($this->triggersByNodeID[$node_id])) {
return $output_id == 'output_1' ? 'blocking' : 'non-blocking';
} elseif ($node['data']['module_type'] == 'logic' && $node['data']['id'] == 'parallel-task') {
if ($node['data']['module_type'] == 'logic' && $node['data']['id'] == 'parallel-task') {
return 'non-blocking';
}
return $path_type;
@ -169,7 +167,7 @@ class GraphWalker
}
$allowedOutputs = $this->_evaluateOutputs($node, $roamingData);
foreach ($allowedOutputs as $output_id => $outputs) {
$path_type = $this->_getPathType($node_id, $path_type, $output_id);
$path_type = $this->_getPathType($node_id, $path_type);
if (is_null($this->for_path) || $path_type == $this->for_path) {
foreach ($outputs as $connections) {
foreach ($connections as $connection_id => $connection) {

View File

@ -5984,7 +5984,7 @@ class Event extends AppModel
}
$this->Module = ClassRegistry::init('Module');
$enabledModules = $this->Module->getEnabledModules($params['user']);
if (empty($enabledModules)) {
if (empty($enabledModules) || is_string($enabledModules)) {
return true;
}
$options = array();

View File

@ -310,7 +310,7 @@ class Workflow extends AppModel
throw new WorkflowNotFoundException(__('Could not get workflow for trigger `%s`', $trigger_id));
}
$walkResult = [];
$blockingPathExecutionSuccess = $this->walkGraph($workflow, $trigger_id, 'all', $data, $blockingErrors, $walkResult);
$blockingPathExecutionSuccess = $this->walkGraph($workflow, $trigger_id, null, $data, $blockingErrors, $walkResult);
return $blockingPathExecutionSuccess;
}
@ -327,15 +327,51 @@ class Workflow extends AppModel
private function walkGraph(array $workflow, $trigger_id, $for_path=null, array $data=[], array &$errors=[], array &$walkResult=[]): bool
{
$walkResult = [
'Blocked paths' => [],
'Executed nodes' => [],
'Nodes that stopped execution' => [],
'blocking_nodes' => [],
'executed_nodes' => [],
'blocked_paths' => [],
];
// $workflowUser = $this->User->getAuthUser($workflow['Workflow']['user_id'], true);
$roamingData = $this->workflowGraphTool->getRoamingData($workflowUser, $data);
$this->Organisation = ClassRegistry::init('Organisation');
$hostOrg = $this->Organisation->find('first', [
'recursive' => -1,
'conditions' => [
'id' => Configure::read('MISP.host_org_id')
],
]);
if (!empty($hostOrg)) {
$userForWorkflow = [
'email' => 'SYSTEM',
'id' => 0,
'org_id' => $hostOrg['Org']['id'],
'Role' => ['perm_site_admin' => 1],
'Organisation' => $hostOrg['Org']
];
} else {
$this->User = ClassRegistry::init('User');
$userForWorkflow = $this->User->find('first', [
'recursive' => -1,
'conditions' => [
'Role.perm_site_admin' => 1,
'User.disabled' => 0
],
'contain' => [
'Organisation' => ['fields' => ['name']],
'Role' => ['fields' => ['*']],
],
'fields' => ['User.org_id', 'User.id', 'User.email'],
]);
$userForWorkflow['Server'] = [];
$userForWorkflow = $this->User->rearrangeToAuthForm($userForWorkflow);
}
if (empty($userForWorkflow)) {
$errors[] = __('Could not find a valid user to run the workflow. Please set setting `MISP.host_org_id` or make sure a valid site_admin user exists.');
return false;
}
$roamingData = $this->workflowGraphTool->getRoamingData($userForWorkflow, $data);
$graphData = !empty($workflow['Workflow']) ? $workflow['Workflow']['data'] : $workflow['data'];
$startNode = $this->workflowGraphTool->getNodeIdForTrigger($graphData, $trigger_id);
if ($startNode == -1) {
$errors[] = __('Invalid start node `%s`', $startNode);
return false;
}
$graphWalker = $this->workflowGraphTool->getWalkerIterator($graphData, $this, $startNode, $for_path, $roamingData);
@ -344,15 +380,15 @@ class Workflow extends AppModel
$node = $graphNode['node'];
foreach ($preventExecutionForPaths as $path_to_block) {
if ($path_to_block == array_slice($graphNode['path_list'], 0, count($path_to_block))) {
$walkResult['Blocked paths'][] = $graphNode['path_list'];
$walkResult['blocked_paths'][] = $graphNode['path_list'];
continue 2;
}
}
$nodeError = [];
$success = $this->__executeNode($node, $roamingData, $nodeError);
$walkResult['Executed nodes'][] = $node;
$walkResult['executed_nodes'][] = $node;
if (empty($success)) {
$walkResult['Nodes that stopped execution'][] = $node;
$walkResult['blocking_nodes'][] = $node;
if ($graphNode['path_type'] == 'blocking') {
if (!empty($nodeError)) {
$errors[] = __(

View File

@ -42,7 +42,6 @@ class Module_enrich_event extends WorkflowBaseModule
$errors[] = __('No enrichmnent module selected');
return false;
}
$rData = $roamingData->getData();
$event_id = $rData['Event']['id'];
$options = [
@ -50,16 +49,23 @@ class Module_enrich_event extends WorkflowBaseModule
'event_id' => $event_id,
'modules' => [$params['Modules']['value']]
];
if (!empty($rData['__conditionData']['Attribute.uuid'])) {
$options['attribute_uuids'] = $rData['__conditionData']['Attribute.uuid'];
}
// if (!empty($rData['__conditionData']['Attribute.uuid'])) {
// $options['attribute_uuids'] = $rData['__conditionData']['Attribute.uuid'];
// }
$this->Event = ClassRegistry::init('Event');
$result = $this->Event->enrichment($options);
$this->push_zmq([
'Enriching event' => $event_id,
'Attribute added' => $result
]);
if ($result === true) {
$this->push_zmq([
'Warning' => __('Error while trying to reach enrichment service or no module available'),
'Attribute added' => 0
]);
} else {
$this->push_zmq([
'Enriching event' => $event_id,
'Attribute added' => $result
]);
}
return true;
}
}