chg: [localtTools:MISP] Centralized methods to issue requests

pull/59/head
mokaddem 2021-06-28 10:18:49 +02:00
parent 63928e756a
commit b90fbf3974
1 changed files with 28 additions and 37 deletions

View File

@ -101,41 +101,48 @@ class MispConnector extends CommonConnectorTools
} }
} }
private function getHeaders(array $connectionSettings): array private function genHTTPClient(Object $connection, array $options=[]): Object
{
return [
'AUTHORIZATION' => $connectionSettings['authkey'],
'Accept' => 'application/json',
'Content-type' => 'application/json'
];
}
private function getHTTPClient(Object $connection): Object
{ {
$settings = json_decode($connection->settings, true); $settings = json_decode($connection->settings, true);
$options = []; $defaultOptions = [
'headers' => [
'Authorization' => $settings['authkey'],
],
];
if (empty($options['type'])) {
$options['type'] = 'json';
}
if (!empty($settings['skip_ssl'])) { if (!empty($settings['skip_ssl'])) {
$options['ssl_verify_peer'] = false; $options['ssl_verify_peer'] = false;
$options['ssl_verify_host'] = false; $options['ssl_verify_host'] = false;
$options['ssl_verify_peer_name'] = false; $options['ssl_verify_peer_name'] = false;
$options['ssl_allow_self_signed'] = true; $options['ssl_allow_self_signed'] = true;
} }
$options = array_merge($defaultOptions, $options);
$http = new Client($options); $http = new Client($options);
return $http; return $http;
} }
public function health(Object $connection): array public function HTTPClientGET(String $relativeURL, Object $connection, array $data=[], array $options=[]): Object
{ {
$settings = json_decode($connection->settings, true); $settings = json_decode($connection->settings, true);
$http = $this->getHTTPClient($connection); $http = $this->genHTTPClient($connection, $options);
$url = sprintf('%s%s', $settings['url'], $relativeURL);
return $http->get($url, $data, $options);
}
public function HTTPClientPOST(String $relativeURL, Object $connection, $data, array $options=[]): Object
{
$settings = json_decode($connection->settings, true);
$http = $this->genHTTPClient($connection, $options);
$url = sprintf('%s%s', $settings['url'], $relativeURL);
return $http->post($url, $data, $options);
}
public function health(Object $connection): array
{
try { try {
$response = $http->post($settings['url'] . '/users/view/me.json', '{}', ['headers' => [ $response = $this->HTTPClientPOST('/users/view/me.json', $connection, '{}');
'AUTHORIZATION' => $settings['authkey'],
'Accept' => 'application/json',
],
'type' => 'json',
]);
} catch (\Exception $e) { } catch (\Exception $e) {
return [ return [
'status' => 0, 'status' => 0,
@ -165,8 +172,6 @@ class MispConnector extends CommonConnectorTools
if (empty($params['connection'])) { if (empty($params['connection'])) {
throw new NotFoundException(__('No connection object received.')); throw new NotFoundException(__('No connection object received.'));
} }
$settings = json_decode($params['connection']->settings, true);
$http = $this->getHTTPClient($params['connection']);
if (!empty($params['sort'])) { if (!empty($params['sort'])) {
$list = explode('.', $params['sort']); $list = explode('.', $params['sort']);
$params['sort'] = end($list); $params['sort'] = end($list);
@ -175,13 +180,7 @@ class MispConnector extends CommonConnectorTools
$params['limit'] = 50; $params['limit'] = 50;
} }
$url = $this->urlAppendParams($url, $params); $url = $this->urlAppendParams($url, $params);
$response = $http->get($settings['url'] . $url, false, [ $response = $this->HTTPClientGET($url, $params['connection']);
'headers' => [
'AUTHORIZATION' => $settings['authkey'],
'Accept' => 'application/json',
'Content-type' => 'application/json'
]
]);
if ($response->isOk()) { if ($response->isOk()) {
return $response; return $response;
} else { } else {
@ -197,16 +196,8 @@ class MispConnector extends CommonConnectorTools
if (empty($params['connection'])) { if (empty($params['connection'])) {
throw new NotFoundException(__('No connection object received.')); throw new NotFoundException(__('No connection object received.'));
} }
$settings = json_decode($params['connection']->settings, true);
$http = $this->getHTTPClient($params['connection']);
$url = $this->urlAppendParams($url, $params); $url = $this->urlAppendParams($url, $params);
$response = $http->post($settings['url'] . $url, json_encode($params['body']), [ $response = $this->HTTPClientPOST($url, $params['connection'], json_encode($params['body']));
'headers' => [
'AUTHORIZATION' => $settings['authkey'],
'Accept' => 'application/json',
],
'type' => 'json',
]);
if ($response->isOk()) { if ($response->isOk()) {
return $response; return $response;
} else { } else {