new: [workflow_module:webhook] Added new webhook module

pull/8530/head
Sami Mokaddem 2022-06-22 13:38:19 +02:00
parent 31e3c94929
commit df8f0f9ed0
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
4 changed files with 125 additions and 2 deletions

View File

@ -2855,7 +2855,7 @@ class AppModel extends Model
*
* @return false|string
*/
protected function checkMIPSCommit()
public function checkMIPSCommit()
{
static $commit;
if ($commit === null) {

View File

@ -58,7 +58,7 @@ class WorkflowBaseModule
$nodeParam[$name] = $value;
}
foreach ($this->saved_filters as $filter) {
$filter['value'] = $nodeParam[$filter['text']];
$filter['value'] = $nodeParam[$filter['text']] ?? $filter['value'];
$indexedFilters[$filter['text']] = $filter['value'];
}
return $indexedFilters;
@ -122,6 +122,24 @@ class WorkflowBaseModule
return $extracted;
}
protected function extractDataForFilters(array $node, WorkflowRoamingData $roamingData)
{
$rData = $roamingData->getData();
if (empty($this->support_filters)) {
return $rData;
}
$filters = $this->getFilters($node);
if (in_array(null, array_values($filters))) {
return $rData;
}
$extracted = $this->extractData($rData, $filters['selector']);
if ($extracted === false) {
return $rData;
}
$matchingItems = $this->getItemsMatchingCondition($extracted, $filters['value'], $filters['operator'], $filters['path']);
return $matchingItems;
}
protected function evaluateCondition($item, $operator, $value): bool
{
if ($operator == 'in') {

View File

@ -0,0 +1,105 @@
<?php
include_once APP . 'Model/WorkflowModules/WorkflowBaseModule.php';
App::uses('SyncTool', 'Tools');
App::uses('JsonTool', 'Tools');
class Module_webhook extends WorkflowBaseModule
{
public $id = 'webhook';
public $name = 'Webhook';
public $description = 'Lorem ipsum dolor, sit amet consectetur adipisicing elit.';
public $icon_path = 'webhook.png';
public $inputs = 1;
public $outputs = 1;
public $support_filters = false;
public $params = [];
private $timeout = false;
private $WorkflowPart;
public function __construct()
{
parent::__construct();
$this->params = [
[
'type' => 'input',
'label' => 'Payload URL',
'placeholder' => 'https://example.com/test',
],
[
'type' => 'select',
'label' => 'Content type',
'default' => 'form',
'options' => [
'json' => 'application/json',
'form' => 'application/x-www-form-urlencoded',
],
],
[
'type' => 'input',
'label' => 'Match Condition',
'default' => '',
'placeholder' => 'Attribute.{n}.AttributeTag.{n}.Tag.name',
],
];
}
public function exec(array $node, WorkflowRoamingData $roamingData, array &$errors = []): bool
{
parent::exec($node, $roamingData, $errors);
$params = $this->getParamsWithValues($node);
if (empty($params['Payload URL']['value'])) {
$errors[] = __('URL not provided.');
return false;
}
$rData = $roamingData->getData();
$path = $params['Match Condition']['value'];
$extracted = !empty($params['Match Condition']['value']) ? $this->extractData($rData, $path) : $rData;
try {
$response = $this->doRequest($params['Payload URL']['value'], $params['Content type']['value'], $extracted);
if ($response->isOk()) {
return true;
}
if ($response->code === 403 || $response->code === 401) {
$errors[] = __('Authentication failed.');
return false;
}
$errors[] = __('Something went wrong with the request or the remote side is having issues. Body returned: %s', $response->body);
return false;
} catch (SocketException $e) {
$errors[] = __('Something went wrong while sending the request. Error returned: %s', $e->getMessage());
return false;
} catch (Exception $e) {
$errors[] = __('Something went wrong. Error returned: %s', $e->getMessage());
return false;
}
$errors[] = __('Something went wrong with the request or the remote side is having issues.');
return false;
}
private function doRequest($url, $contentType, array $data)
{
$this->WorkflowPart = ClassRegistry::init('Event'); // We just need a small model to use AppModel functions
$version = implode('.', $this->WorkflowPart->checkMISPVersion());
$commit = $this->WorkflowPart->checkMIPSCommit();
$request = [
'header' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'User-Agent' => 'MISP ' . $version . (empty($commit) ? '' : ' - #' . $commit),
]
];
$syncTool = new SyncTool();
$HttpSocket = $syncTool->setupHttpSocket(null, $this->timeout);
if ($contentType == 'form') {
$request['header']['Content-Type'] = 'application/x-www-form-urlencoded';
$response = $HttpSocket->post($url, $data, $request);
} else {
$response = $HttpSocket->post($url, JsonTool::encode($data), $request);
}
return $response;
}
}

BIN
app/webroot/img/webhook.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB