Merge pull request #8653 from JakubOnderka/workflow-fixes

fix: [workflow] Basic cleanup
pull/8682/head
Jakub Onderka 2022-10-18 21:20:40 +02:00 committed by GitHub
commit aae65c42c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 49 additions and 39 deletions

View File

@ -53,6 +53,7 @@ class AuditLogsController extends AppController
'News', 'News',
'Warninglist', 'Warninglist',
'Workflow', 'Workflow',
'WorkflowBlueprint',
]; ];
public $paginate = [ public $paginate = [

View File

@ -1,6 +1,9 @@
<?php <?php
App::uses('AppController', 'Controller'); App::uses('AppController', 'Controller');
/**
* @property WorkflowBlueprint $WorkflowBlueprint
*/
class WorkflowBlueprintsController extends AppController class WorkflowBlueprintsController extends AppController
{ {
public $components = array( public $components = array(
@ -14,10 +17,9 @@ class WorkflowBlueprintsController extends AppController
$message = __('Default workflow blueprints updated'); $message = __('Default workflow blueprints updated');
if ($this->_isRest()) { if ($this->_isRest()) {
return $this->RestResponse->saveSuccessResponse('WorkflowBlueprint', 'update', false, $this->response->type(), $message); return $this->RestResponse->saveSuccessResponse('WorkflowBlueprint', 'update', false, $this->response->type(), $message);
} else {
$this->Flash->success($message);
$this->redirect(array('controller' => 'workflowBlueprints', 'action' => 'index'));
} }
$this->Flash->success($message);
$this->redirect(array('controller' => 'workflowBlueprints', 'action' => 'index'));
} }
public function index() public function index()
@ -69,9 +71,7 @@ class WorkflowBlueprintsController extends AppController
public function delete($id) public function delete($id)
{ {
$params = [ $this->CRUD->delete($id);
];
$this->CRUD->delete($id, $params);
if ($this->IndexFilter->isRest()) { if ($this->IndexFilter->isRest()) {
return $this->restResponsePayload; return $this->restResponsePayload;
} }
@ -82,16 +82,15 @@ class WorkflowBlueprintsController extends AppController
{ {
$filters = $this->IndexFilter->harvestParameters(['format']); $filters = $this->IndexFilter->harvestParameters(['format']);
if (!empty($filters['format'])) { if (!empty($filters['format'])) {
if ($filters['format'] == 'dot') { if ($filters['format'] === 'dot') {
$dot = $this->WorkflowBlueprint->getDotNotation($id); $dot = $this->WorkflowBlueprint->getDotNotation($id);
return $this->RestResponse->viewData($dot, $this->response->type()); return $this->RestResponse->viewData($dot, $this->response->type());
} else if ($filters['format'] == 'mermaid') { } else if ($filters['format'] === 'mermaid') {
$mermaid = $this->WorkflowBlueprint->getMermaid($id); $mermaid = $this->WorkflowBlueprint->getMermaid($id);
return $this->RestResponse->viewData($mermaid, $this->response->type()); return $this->RestResponse->viewData($mermaid, $this->response->type());
} }
} }
$this->CRUD->view($id, [ $this->CRUD->view($id);
]);
if ($this->IndexFilter->isRest()) { if ($this->IndexFilter->isRest()) {
return $this->restResponsePayload; return $this->restResponsePayload;
} }
@ -102,11 +101,8 @@ class WorkflowBlueprintsController extends AppController
public function import() public function import()
{ {
if ($this->request->is('post') || $this->request->is('put')) { if ($this->request->is('post') || $this->request->is('put')) {
$workflowBlueprintData = JsonTool::decode($this->request->data['WorkflowBlueprint']['data']); $workflowBlueprintData = $this->_jsonDecode($this->request->data['WorkflowBlueprint']['json']);
if ($workflowBlueprintData === null) { $this->request->data = $workflowBlueprintData;
throw new MethodNotAllowedException(__('Error while decoding JSON'));
}
$this->request->data['WorkflowBlueprint']['data'] = JsonTool::encode($workflowBlueprintData);
$this->add(); $this->add();
} }
} }
@ -118,7 +114,11 @@ class WorkflowBlueprintsController extends AppController
'id' => $id, 'id' => $id,
] ]
]); ]);
$content = JsonTool::encode($workflowBlueprint, JSON_PRETTY_PRINT); if (empty($workflowBlueprint)) {
throw new NotFoundException(__('Invalid workflow blueprint'));
}
$content = JsonTool::encode($workflowBlueprint, true);
$this->response->body($content); $this->response->body($content);
$this->response->type('json'); $this->response->type('json');
$this->response->download(sprintf('blueprint_%s_%s.json', str_replace(' ', '-', strtolower($workflowBlueprint['WorkflowBlueprint']['name'])), time())); $this->response->download(sprintf('blueprint_%s_%s.json', str_replace(' ', '-', strtolower($workflowBlueprint['WorkflowBlueprint']['name'])), time()));

View File

@ -3735,7 +3735,7 @@ class AppModel extends Model
* @param array $logging If the execution failure should be logged * @param array $logging If the execution failure should be logged
* @return boolean If the execution for the blocking path was a success * @return boolean If the execution for the blocking path was a success
*/ */
public function executeTrigger($trigger_id, array $data=[], array &$blockingErrors=[], array $logging=[]): bool protected function executeTrigger($trigger_id, array $data=[], array &$blockingErrors=[], array $logging=[]): bool
{ {
if ($this->isTriggerCallable($trigger_id)) { if ($this->isTriggerCallable($trigger_id)) {
$success = $this->Workflow->executeWorkflowForTriggerRouter($trigger_id, $data, $blockingErrors, $logging); $success = $this->Workflow->executeWorkflowForTriggerRouter($trigger_id, $data, $blockingErrors, $logging);
@ -3749,8 +3749,17 @@ class AppModel extends Model
return true; return true;
} }
public function isTriggerCallable($trigger_id): bool protected function isTriggerCallable($trigger_id): bool
{ {
static $workflowEnabled;
if ($workflowEnabled === null) {
$workflowEnabled = (bool)Configure::read('Plugin.Workflow_enable');
}
if (!$workflowEnabled) {
return false;
}
if ($this->Workflow === null) { if ($this->Workflow === null) {
$this->Workflow = ClassRegistry::init('Workflow'); $this->Workflow = ClassRegistry::init('Workflow');
} }

View File

@ -515,14 +515,16 @@ class Attribute extends AppModel
$kafkaPubTool = $this->getKafkaPubTool(); $kafkaPubTool = $this->getKafkaPubTool();
$kafkaPubTool->publishJson($kafkaTopic, $attributeForPublish, $action); $kafkaPubTool->publishJson($kafkaTopic, $attributeForPublish, $action);
} }
$workflowErrors = []; if ($isTriggerCallable) {
$logging = [ $workflowErrors = [];
$logging = [
'model' => 'Attribute', 'model' => 'Attribute',
'action' => $action, 'action' => $action,
'id' => $attributeForPublish['Attribute']['id'], 'id' => $attributeForPublish['Attribute']['id'],
]; ];
$triggerData = $attributeForPublish; $triggerData = $attributeForPublish;
$this->executeTrigger('attribute-after-save', $triggerData, $workflowErrors, $logging); $this->executeTrigger('attribute-after-save', $triggerData, $workflowErrors, $logging);
}
} }
} }
if ($created && isset($attribute['event_id']) && empty($attribute['skip_auto_increment'])) { if ($created && isset($attribute['event_id']) && empty($attribute['skip_auto_increment'])) {

View File

@ -1,7 +1,6 @@
<?php <?php
App::uses('AppModel', 'Model'); App::uses('AppModel', 'Model');
App::uses('Folder', 'Utility'); App::uses('Folder', 'Utility');
App::uses('File', 'Utility');
class WorkflowBlueprint extends AppModel class WorkflowBlueprint extends AppModel
{ {
@ -18,10 +17,9 @@ class WorkflowBlueprint extends AppModel
], ],
]; ];
public $belongsTo = [
];
public $validate = [ public $validate = [
'name' => 'stringNotEmpty',
'value' => [ 'value' => [
'stringNotEmpty' => [ 'stringNotEmpty' => [
'rule' => ['stringNotEmpty'] 'rule' => ['stringNotEmpty']
@ -92,11 +90,11 @@ class WorkflowBlueprint extends AppModel
return $blueprint; return $blueprint;
} }
/** /**
* __readBlueprintsFromRepo Reads blueprints from the misp-workflow-blueprints repository * __readBlueprintsFromRepo Reads blueprints from the misp-workflow-blueprints repository
* *
* @return array * @return array
* @throws Exception
*/ */
private function __readBlueprintsFromRepo(): array private function __readBlueprintsFromRepo(): array
{ {
@ -114,6 +112,7 @@ class WorkflowBlueprint extends AppModel
* *
* @param boolean $force * @param boolean $force
* @return void * @return void
* @throws Exception
*/ */
public function update($force=false) public function update($force=false)
{ {
@ -133,7 +132,6 @@ class WorkflowBlueprint extends AppModel
if ($force || $blueprint_from_repo['timestamp'] > $existing_blueprint['timestamp']) { if ($force || $blueprint_from_repo['timestamp'] > $existing_blueprint['timestamp']) {
$blueprint_from_repo['id'] = $existing_blueprint['id']; $blueprint_from_repo['id'] = $existing_blueprint['id'];
$this->save($blueprint_from_repo); $this->save($blueprint_from_repo);
continue;
} }
} else { } else {
$this->create(); $this->create();

View File

@ -1664,13 +1664,13 @@ $divider = $this->element('/genericElements/SideMenu/side_menu_divider');
if ($isSiteAdmin) { if ($isSiteAdmin) {
echo $this->element('/genericElements/SideMenu/side_menu_link', array( echo $this->element('/genericElements/SideMenu/side_menu_link', array(
'element_id' => 'edit', 'element_id' => 'edit',
'url' => '/workflows/edit/' . h($id), 'url' => '/workflowBlueprints/edit/' . h($id),
'text' => __('Edit Workflow Blueprint') 'text' => __('Edit Workflow Blueprint')
)); ));
} }
echo $this->element('/genericElements/SideMenu/side_menu_link', array( echo $this->element('/genericElements/SideMenu/side_menu_link', array(
'url' => '/admin/audit_logs/index/model:WorkflowBlueprints/model_id:' . h($id), 'url' => '/admin/audit_logs/index/model:WorkflowBlueprint/model_id:' . h($id),
'text' => __('View worflow blueprint history'), 'text' => __('View workflow blueprint history'),
'requirement' => Configure::read('MISP.log_new_audit') && $canAccess('auditLogs', 'admin_index'), 'requirement' => Configure::read('MISP.log_new_audit') && $canAccess('auditLogs', 'admin_index'),
)); ));
} }

View File

@ -5,7 +5,7 @@ echo $this->element('genericElements/Form/genericForm', [
'enctype' => 'multipart/form-data', 'enctype' => 'multipart/form-data',
], ],
'data' => [ 'data' => [
'model' => 'Workflow', 'model' => 'WorkflowBlueprint',
'title' => __('Import Workflow Blueprint'), 'title' => __('Import Workflow Blueprint'),
'description' => __('Paste a JSON of a Workflow blueprint to import it or provide a JSON file below.'), 'description' => __('Paste a JSON of a Workflow blueprint to import it or provide a JSON file below.'),
'fields' => [ 'fields' => [

View File

@ -32,10 +32,10 @@
'name' => __('Timestamp'), 'name' => __('Timestamp'),
'sort' => 'WorkflowBlueprint.timestamp', 'sort' => 'WorkflowBlueprint.timestamp',
'data_path' => 'WorkflowBlueprint.timestamp', 'data_path' => 'WorkflowBlueprint.timestamp',
'element' => 'datetime',
], ],
]; ];
echo $this->element('genericElements/IndexTable/scaffold', [ echo $this->element('genericElements/IndexTable/scaffold', [
'scaffold_data' => [ 'scaffold_data' => [
'data' => [ 'data' => [
@ -73,11 +73,13 @@
'url_params_data_paths' => ['WorkflowBlueprint.id'], 'url_params_data_paths' => ['WorkflowBlueprint.id'],
'icon' => 'eye', 'icon' => 'eye',
'dbclickAction' => true, 'dbclickAction' => true,
'title' => __('View'),
], ],
[ [
'url' => $baseurl . '/workflowBlueprints/edit', 'url' => $baseurl . '/workflowBlueprints/edit',
'url_params_data_paths' => ['WorkflowBlueprint.id'], 'url_params_data_paths' => ['WorkflowBlueprint.id'],
'icon' => 'edit', 'icon' => 'edit',
'title' => __('Edit'),
], ],
[ [
'url' => $baseurl . '/workflowBlueprints/export', 'url' => $baseurl . '/workflowBlueprints/export',
@ -86,13 +88,11 @@
'icon' => 'download', 'icon' => 'download',
], ],
[ [
'class' => 'modal-open',
'onclick' => sprintf( 'url' => $baseurl . '/workflowBlueprints/delete/',
'openGenericModal(\'%s/workflowBlueprints/delete/[onclick_params_data_path]\');', 'url_params_data_paths' => 'WorkflowBlueprint.id',
$baseurl 'icon' => 'trash',
), 'title' => __('Delete'),
'onclick_params_data_path' => 'WorkflowBlueprint.id',
'icon' => 'trash'
] ]
] ]
] ]