chg: [workflowBlueprint] Added support of misp-workflow-blueprints repository

pull/8530/head
Sami Mokaddem 2022-07-19 11:50:42 +02:00
parent 2be9d07502
commit 7877145960
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
3 changed files with 71 additions and 17 deletions

View File

@ -7,6 +7,19 @@ class WorkflowBlueprintsController extends AppController
'RequestHandler'
);
public function update($force = false)
{
$this->request->allowMethod(['post', 'put']);
$this->WorkflowBlueprint->update($force);
$message = __('Default workflow blueprints updated');
if ($this->_isRest()) {
return $this->RestResponse->saveSuccessResponse('WorkflowBlueprint', 'update', false, $this->response->type(), $message);
} else {
$this->Flash->success($message);
$this->redirect(array('controller' => 'workflowBlueprints', 'action' => 'index'));
}
}
public function index()
{
$params = [

View File

@ -1,8 +1,11 @@
<?php
App::uses('AppModel', 'Model');
App::uses('Folder', 'Utility');
App::uses('File', 'Utility');
class WorkflowBlueprint extends AppModel
{
const REPOSITORY_PATH = APP . 'files' . DS . 'misp-workflow-blueprints' . DS . 'blueprints';
public $recursive = -1;
public $actsAs = [
@ -79,30 +82,58 @@ class WorkflowBlueprint extends AppModel
}
public function logExecutionError($workflow, $message)
/**
* __readBlueprintsFromRepo Reads blueprints from the misp-workflow-blueprints repository
*
* @return array
*/
private function __readBlueprintsFromRepo(): array
{
$this->Log = ClassRegistry::init('Log');
$this->Log->createLogEntry('SYSTEM', 'execute_workflow', 'Workflow', $workflow['Workflow']['id'], $message);
$dir = new Folder(self::REPOSITORY_PATH);
$files = $dir->find('.*\.json');
$blueprints = [];
foreach ($files as $file) {
$file = new File($dir->pwd() . DS . $file);
$blueprints[] = JsonTool::decode($file->read());
$file->close();
}
return $blueprints;
}
private function __logError($id, $message)
/**
* update Update the blueprint using the default repository
*
* @param boolean $force
* @return void
*/
public function update($force=false)
{
$this->Log = ClassRegistry::init('Log');
$this->Log->createLogEntry('SYSTEM', 'load_module', 'Workflow', $id, $message);
return false;
}
private function __saveAndReturnErrors($data, $saveOptions = [], $errors = [])
{
$saveSuccess = $this->save($data, $saveOptions);
if (!$saveSuccess) {
foreach ($this->validationErrors as $validationError) {
$errors[] = $validationError[0];
$blueprints_from_repo = $this->__readBlueprintsFromRepo();
if (empty($blueprints_from_repo)) {
throw new NotFoundException(__('Default blueprints could not be loaded or `%s` folder is empty', self::REPOSITORY_PATH));
}
$existing_blueprints = $this->find('all', array(
'recursive' => -1
));
$existing_blueprints_by_uuid = Hash::combine($existing_blueprints, '{n}.WorkflowBlueprint.uuid', '{n}.WorkflowBlueprint');
foreach ($blueprints_from_repo as $blueprint_from_repo) {
$blueprint_from_repo = $blueprint_from_repo['WorkflowBlueprint'];
if (!empty($existing_blueprints_by_uuid[$blueprint_from_repo['uuid']])) {
$existing_blueprint = $existing_blueprints_by_uuid[$blueprint_from_repo['uuid']];
if ($force || $blueprint_from_repo['timestamp'] > $existing_blueprint['timestamp']) {
$blueprint_from_repo['id'] = $existing_blueprint['id'];
$blueprint_from_repo['default'] = true;
$this->save($blueprint_from_repo);
continue;
}
} else {
$this->create();
$blueprint_from_repo['default'] = true;
$this->save($blueprint_from_repo);
}
}
return $errors;
}
public function getMermaid($workflowBlueprintData)
{
App::uses('MermaidFlowchartTool', 'Tools');

View File

@ -1622,6 +1622,16 @@ $divider = $this->element('/genericElements/SideMenu/side_menu_divider');
'url' => $baseurl . '/workflowBlueprints/import',
'text' => __('Import Workflow Blueprints')
));
if ($isSiteAdmin && ($menuItem === 'view' || $menuItem === 'index')) {
echo $this->element('/genericElements/SideMenu/side_menu_post_link', array(
'url' => $baseurl . '/workflowBlueprints/update',
'text' => __('Update Default Blueprints')
));
echo $this->element('/genericElements/SideMenu/side_menu_post_link', array(
'url' => $baseurl . '/workflowBlueprints/update/true',
'text' => __('Force Update Default Blueprints')
));
}
if ($menuItem === 'view' || $menuItem === 'edit') {
echo $this->element('/genericElements/SideMenu/side_menu_link', array(
'element_id' => 'view',