From 4966e6252ec88b70362da01cd8b6875bfc216a83 Mon Sep 17 00:00:00 2001 From: mokaddem Date: Sat, 12 Jun 2021 12:13:15 +0200 Subject: [PATCH] new: [requestProcessor:localTool] Added generic localTool processor files --- .../LocalToolRequestProcessor.php | 227 ++++++++++++++++++ .../templates/LocalTool/GenericRequest.php | 80 ++++++ 2 files changed, 307 insertions(+) create mode 100644 libraries/default/RequestProcessors/LocalToolRequestProcessor.php create mode 100644 libraries/default/RequestProcessors/templates/LocalTool/GenericRequest.php diff --git a/libraries/default/RequestProcessors/LocalToolRequestProcessor.php b/libraries/default/RequestProcessors/LocalToolRequestProcessor.php new file mode 100644 index 0000000..ce17083 --- /dev/null +++ b/libraries/default/RequestProcessors/LocalToolRequestProcessor.php @@ -0,0 +1,227 @@ +Broods = TableRegistry::getTableLocator()->get('Broods'); + } + + public function create($requestData) + { + return parent::create($requestData); + } + + protected function assignProcessingTemplate($toolName) + { + $processingTemplatePath = sprintf('%s/%s/%s.php', $this->scope, $toolName, $this->action); + $file = new File($this->processingTemplatesDirectory . DS . $processingTemplatePath); + if ($file->exists()) { + $this->processingTemplate = str_replace('.php', '', $processingTemplatePath); + } + $file->close(); + } + + protected function validateToolName($requestData) + { + if (empty($requestData['data']['toolName'])) { + throw new Exception('Error while validating request data. Tool name is missing.'); + } + } + + protected function getIssuerBrood($request) + { + $brood = $this->Broods->find() + ->where(['url' => $request['origin']]) + ->first(); + return $brood; + } + + protected function addBaseValidatorRules($validator) + { + return $validator + ->requirePresence('toolName') + ->notEmpty('toolName', 'A url must be provided') + ->requirePresence('url') + ->notEmpty('url', 'A url must be provided'); + // ->add('url', 'validFormat', [ + // 'rule' => 'url', + // 'message' => 'URL must be valid' + // ]); + } +} + +class IncomingConnectionRequestProcessor extends LocalToolRequestProcessor implements GenericProcessorActionI { + public $action = 'IncomingConnectionRequest'; + protected $description; + + public function __construct() { + parent::__construct(); + $this->description = __('Handle Phase I of inter-connection when another cerebrate instance performs the request.'); + } + + protected function addValidatorRules($validator) + { + return $this->addBaseValidatorRules($validator); + } + + public function create($requestData) { + $this->validateToolName($requestData); + $this->validateRequestData($requestData); + $requestData['title'] = __('Request for {0} Inter-connection', $requestData['data']['toolName']); + return parent::create($requestData); + } + + public function getViewVariables($request) + { + $request->brood = $this->getIssuerBrood($request); + return [ + 'request' => $request, + 'progressStep' => 0, + ]; + } + + public function process($id, $requestData) + { + $connectionSuccessfull = false; + $interConnectionResult = []; + if ($connectionSuccessfull) { + $this->discard($id, $requestData); + } + return $this->genActionResult( + $interConnectionResult, + $connectionSuccessfull, + $connectionSuccessfull ? __('Interconnection for `{0}`\'s {1} created',$requestData['origin'], '[toolname]') : __('Could not inter-connect `{0}`\'s {1}', $requestData['origin'], '[toolname]'), + [] + ); + } + + public function discard($id, $requestData) + { + // /!\ TODO: send decline message to remote cerebrate + return parent::discard($id, $requestData); + } +} + +class AcceptedRequestProcessor extends LocalToolRequestProcessor implements GenericProcessorActionI { + public $action = 'AcceptedRequest'; + protected $description; + + public function __construct() { + parent::__construct(); + $this->description = __('Handle Phase II of inter-connection when initial request has been accepted by the remote cerebrate.'); + // $this->Broods = TableRegistry::getTableLocator()->get('Broods'); + } + + protected function addValidatorRules($validator) + { + return $this->addBaseValidatorRules($validator); + } + + public function create($requestData) { + $this->validateToolName($requestData); + $this->validateRequestData($requestData); + $requestData['title'] = __('Inter-connection for {0} has been accepted', $requestData['data']['toolName']); + return parent::create($requestData); + } + + public function getViewVariables($request) + { + $request->brood = $this->getIssuerBrood($request); + return [ + 'request' => $request, + 'progressStep' => 1, + ]; + } + + public function process($id, $requestData) + { + $connectionSuccessfull = false; + $interConnectionResult = []; + if ($connectionSuccessfull) { + $this->discard($id, $requestData); + } + return $this->genActionResult( + $interConnectionResult, + $connectionSuccessfull, + $connectionSuccessfull ? __('Interconnection for `{0}`\'s {1} finalized', $requestData['origin'], '[toolname]') : __('Could not inter-connect `{0}`\'s {1}', $requestData['origin'], '[toolname]'), + [] + ); + } + + public function discard($id, $requestData) + { + return parent::discard($id, $requestData); + } +} + +class DeclinedRequestProcessor extends LocalToolRequestProcessor implements GenericProcessorActionI { + public $action = 'DeclinedRequest'; + protected $description; + + public function __construct() { + parent::__construct(); + $this->description = __('Handle Phase II of MISP inter-connection when initial request has been declined by the remote cerebrate.'); + } + + protected function addValidatorRules($validator) + { + return $this->addBaseValidatorRules($validator); + } + + public function create($requestData) { + $this->validateToolName($requestData); + $this->validateRequestData($requestData); + $requestData['title'] = __('Declined inter-connection for {0}', $requestData['data']['toolName']); + return parent::create($requestData); + } + + public function getViewVariables($request) + { + $request->brood = $this->getIssuerBrood($request); + return [ + 'request' => $request, + 'progressStep' => 1, + 'progressVariant' => 'danger', + 'steps' => [ + 1 => ['icon' => 'times', 'text' => __('Request Declined'), 'confirmButton' => __('Clean-up')], + 2 => ['icon' => 'trash', 'text' => __('Clean-up')], + ] + ]; + } + + public function process($id, $requestData) + { + $connectionSuccessfull = false; + $interConnectionResult = []; + if ($connectionSuccessfull) { + $this->discard($id, $requestData); + } + return $this->genActionResult( + $interConnectionResult, + $connectionSuccessfull, + $connectionSuccessfull ? __('Interconnection for `{0}`\'s {1} finalized', $requestData['origin'], '[toolname]') : __('Could not inter-connect `{0}`\'s {1}', $requestData['origin'], '[toolname]'), + [] + ); + } + public function discard($id, $requestData) + { + return parent::discard($id, $requestData); + } +} diff --git a/libraries/default/RequestProcessors/templates/LocalTool/GenericRequest.php b/libraries/default/RequestProcessors/templates/LocalTool/GenericRequest.php new file mode 100644 index 0000000..2c60181 --- /dev/null +++ b/libraries/default/RequestProcessors/templates/LocalTool/GenericRequest.php @@ -0,0 +1,80 @@ + __('Request Sent'), + 'icon' => 'paper-plane', + 'title' => __(''), + 'confirmButton' => __('Accept Request') + ], + [ + 'text' => __('Request Accepted'), + 'icon' => 'check-square', + 'title' => __(''), + 'confirmButton' => __('Finalize Connection') + ], + [ + 'text' => __('Connection done'), + 'icon' => 'exchange-alt', + 'title' => __(''), + ] +]; + +$progressVariant = !empty($progressVariant) ? $progressVariant : 'info'; +$finalSteps = array_replace($defaultSteps, $steps ?? []); +$currentStep = $finalSteps[$progressStep]; +$progress = $this->Bootstrap->progressTimeline([ + 'variant' => $progressVariant, + 'selected' => !empty($progressStep) ? $progressStep : 0, + 'steps' => $finalSteps, +]); + +$table = $this->Bootstrap->table(['small' => true, 'bordered' => false, 'striped' => false, 'hover' => false], [ + 'fields' => [ + ['key' => 'data.toolName', 'label' => __('Tool Name')], + ['key' => 'created', 'label' => __('Date'), 'formatter' => function($value, $row) { + return $value->i18nFormat('yyyy-MM-dd HH:mm:ss'); + }], + ['key' => 'origin', 'label' => __('Origin')], + ['key' => 'brood', 'label' => __('Brood'), 'formatter' => function($brood, $row) { + return sprintf('%s', $this->Url->build(['controller' => 'broods', 'action' => 'view', $brood['id']]), h($brood['name'])); + }] + ], + 'items' => [$request->toArray()], +]); +$form = $this->element('genericElements/Form/genericForm', [ + 'entity' => null, + 'ajax' => false, + 'raw' => true, + 'data' => [ + 'model' => 'Inbox', + 'fields' => [], + 'submit' => [ + 'action' => $this->request->getParam('action') + ] + ] +]); + +$requestData = $this->Bootstrap->collapse( + [ + 'title' => __('Inter-connection data'), + 'open' => true, + ], + sprintf('
%s
', json_encode($request['data'], JSON_PRETTY_PRINT)) +); + +$bodyHtml = sprintf('
%s
%s
%s', + $table, + $requestData, + $form +); + +echo $this->Bootstrap->modal([ + 'title' => __('Interconnection Request for {0}', h($request->data['toolName'])), + 'size' => 'lg', + 'type' => 'confirm', + 'bodyHtml' => sprintf('
%s
%s
', + $progress, + $bodyHtml + ), + 'confirmText' => $currentStep['confirmButton'] ?? __('Submit'), +]);