chg: [wip] localtools

feature-scoped-element
iglocska 2021-06-10 13:45:46 +02:00
parent 0e8b2d5369
commit 7f58c34e02
No known key found for this signature in database
GPG Key ID: BEA224F1FEF113AC
5 changed files with 190 additions and 1 deletions

View File

@ -208,7 +208,39 @@ class LocalToolsController extends AppController
if ($this->ParamHandler->isRest()) {
return $this->RestResponse->viewData($tools, 'json');
}
$this->set('id', $id);
$this->set('data', $tools);
$this->set('metaGroup', 'Administration');
}
public function connectionRequest($cerebrate_id, $remote_tool_id)
{
$params = [
'cerebrate_id' => $cerebrate_id,
'remote_tool_id' => $remote_tool_id
];
if ($this->request->is(['post', 'put'])) {
$postParams = $this->ParamHandler->harvestParams(['local_tool_id']);
if (empty($postParams['local_tool_id'])) {
throw new MethodNotAllowedException(__('No local tool ID supplied.'));
}
$params['local_tool_id'] = $postParams['local_tool_id'];
$result = $this->LocalTools->encodeConnection($params);
// Send message to remote inbox
debug($result);
} else {
$this->loadModel('Broods');
$remoteCerebrate = $this->Broods->find()->where(['id' => $params['cerebrate_id']])->first();
$remoteTool = $this->LocalTools->getRemoteToolById($params);
$local_tools = $this->LocalTools->encodeConnectionChoice($params);
if (empty($local_tools)) {
throw new NotFoundException(__('No local equivalent tool found.'));
}
$this->set('data', [
'remoteCerebrate' => $remoteCerebrate,
'remoteTool' => $remoteTool,
'local_tools' => $local_tools
]);
}
}
}

View File

@ -48,6 +48,12 @@ class CommonConnectorTools
$sharing_groups->captureSharingGroup($input);
return true;
}
public function encodeConnection(array $params): array
{
$result = $this->encodeConnection($params);
return $result;
}
}
?>

View File

@ -143,6 +143,9 @@ class MispConnector extends CommonConnectorTools
if ($response->isOk()) {
return $response;
} else {
if (!empty($params['softError'])) {
return $response;
}
throw new NotFoundException(__('Could not retrieve the requested resource.'));
}
}
@ -537,11 +540,82 @@ class MispConnector extends CommonConnectorTools
if ($response->getStatusCode() == 200) {
return ['success' => 1, 'message' => __('Setting saved.')];
} else {
return ['success' => 0, 'message' => __('Could not fetch the remote sharing group.')];
return ['success' => 0, 'message' => __('Could not save the setting.')];
}
}
throw new MethodNotAllowedException(__('Invalid http request type for the given action.'));
}
public function encodeConnectionAction(array $params): array
{
if (empty($params['org_uuid'])) {
throw new MethodNotAllowedException(__('No org uuid passed, cannot encode connection.'));
}
return [];
}
private function getSetOrg(array $params): array
{
$params['softError'] = 1;
$response = $this->getData('/organisations/view/' . $params['remote_org']['uuid'], $params);
if ($response->isOk()) {
$organisation = $response->getJson()['Organisation'];
if (!$organisation['local']) {
$organisation['local'] = 1;
$response = $this->postData('/admin/organisations/edit/' . $organisation['id'], $params);
if (!$response->isOk()) {
throw new MethodNotAllowedException(__('Could not update the organisation in MISP.'));
}
}
} else {
$params['body'] = [
'uuid' => $params['remote_org']['uuid'],
'name' => $params['remote_org']['name'],
'local' => 1
];
$response = $this->postData('/admin/organisations/add', $params);
if ($response->isOk()) {
$organisation = $response->getJson()['Organisation'];
} else {
throw new MethodNotAllowedException(__('Could not create the organisation in MISP.'));
}
}
return $organisation;
}
private function createSyncUser(array $params): array
{
$params['softError'] = 1;
$username = sprintf(
'sync_%s@%s',
\Cake\Utility\Security::randomString(8),
parse_url($params['remote_cerebrate']['url'])['host']
);
$params['body'] = [
'email' => $username,
'org_id' => $params['misp_organisation']['id'],
'role_id' => empty($params['connection_settings']['role_id']) ? 5 : $params['connection_settings']['role_id'],
'disabled' => 1,
'change_pw' => 0,
'termsaccepted' => 1
];
$response = $this->postData('/admin/users/add', $params);
if (!$response->isOk()) {
throw new MethodNotAllowedException(__('Could not update the organisation in MISP.'));
}
return $response->getJson()['User'];
}
public function connectToRemoteTool(array $params): array
{
$params['connection_settings'] = json_decode($params['connection']['settings'], true);
$params['misp_organisation'] = $this->getSetOrg($params);
$params['sync_user'] = $this->createSyncUser($params);
return [
'email' => $params['sync_user']['email'],
'authkey' => $params['sync_user']['authkey'],
'url' => $params['connection_settings']['url']
];
}
}

View File

@ -166,4 +166,54 @@ class LocalToolsTable extends AppTable
}
return $children;
}
public function getRemoteToolById($params) {
$broods = \Cake\ORM\TableRegistry::getTableLocator()->get('Broods');
$tools = $broods->queryLocalTools($params['cerebrate_id']);
$remoteTool = [];
foreach ($tools as $tool) {
if ($tool['id'] === intval($params['remote_tool_id'])) {
$remoteTool = $tool;
}
}
if (empty($remoteTool)) {
throw new NotFoundException(__('Invalid remote tool specified.'));
}
return $remoteTool;
}
public function encodeConnectionChoice(array $params): array
{
$remoteTool = $this->getRemoteToolById($params);
$connections = $this->find()->where(['connector' => $remoteTool['connector']])->toArray();
$results = [];
foreach ($connections as $connection) {
$results[] = [
'id' => $connection->id,
'name' => $connection->name
];
}
return $results;
}
public function encodeConnection(array $params): array
{
$remote_tool = $this->getRemoteToolById($params);
$broods = \Cake\ORM\TableRegistry::getTableLocator()->get('Broods');
$remote_cerebrate = $broods->find()->where(['id' => $params['cerebrate_id']])->first();
$connector = $this->getConnectors($remote_tool['connector']);
$connection = $this->find()->where(['id' => $params['local_tool_id']])->first();
$remote_org = $broods->Organisations->find()->where(['id' => $remote_cerebrate['organisation_id']])->first();
if (empty($connector[$remote_tool['connector']])) {
throw new NotFoundException(__('No valid connector found for the remote tool.'));
}
$result = $connector[$remote_tool['connector']]->connectToRemoteTool([
'remote_cerebrate' => $remote_cerebrate,
'remote_org' => $remote_org,
'remote_tool' => $remote_tool,
'connector' => $connector,
'connection' => $connection
]);
return $result;
}
}

View File

@ -0,0 +1,27 @@
<?php
$dropdown = [];
foreach ($data['local_tools'] as $local_tool) {
$dropdown[$local_tool['id']] = $local_tool['name'];
}
echo $this->element('genericElements/Form/genericForm', [
'data' => [
'description' => __(
'Connect the remote tool ({0}) on remote brood ({1}) using the local tool selected below.',
h($data['remoteTool']['name']),
h($data['remoteCerebrate']['name'])
),
'model' => 'LocalTools',
'fields' => [
[
'field' => 'local_tool_id',
'options' => $dropdown,
'type' => 'dropdown'
]
],
'submit' => [
'action' => $this->request->getParam('action')
]
]
]);
?>
</div>