From 656a391223ac810b70ba277c34a8ae991e5a4fee Mon Sep 17 00:00:00 2001 From: iglocska Date: Mon, 21 Sep 2015 14:55:40 +0200 Subject: [PATCH] Progress on the sync - pull from 2.3 -> 2.4 should work correctly now --- app/Controller/ServersController.php | 1 + app/Lib/Tools/XMLConverterTool.php | 12 +++++++++--- app/Model/Event.php | 15 ++++++++++++--- app/Model/Organisation.php | 19 +++++++++++++++---- app/Model/Server.php | 9 +++++++++ app/Model/ShadowAttribute.php | 10 +++++++++- app/Model/SharingGroup.php | 2 +- app/View/Logs/event_index.ctp | 2 +- 8 files changed, 57 insertions(+), 13 deletions(-) diff --git a/app/Controller/ServersController.php b/app/Controller/ServersController.php index 9d2374529..4716d5cf7 100755 --- a/app/Controller/ServersController.php +++ b/app/Controller/ServersController.php @@ -114,6 +114,7 @@ class ServersController extends AppController { } } if (!$fail) { + $this->request->data['Server']['org_id'] = $this->Auth->user('org_id'); if ($this->Server->save($this->request->data)) { if (isset($this->request->data['Server']['submitted_cert']) && $this->request->data['Server']['submitted_cert']['size'] != 0) { $this->__saveCert($this->request->data, $this->Server->id); diff --git a/app/Lib/Tools/XMLConverterTool.php b/app/Lib/Tools/XMLConverterTool.php index 5f6c19919..a40a1cf2c 100644 --- a/app/Lib/Tools/XMLConverterTool.php +++ b/app/Lib/Tools/XMLConverterTool.php @@ -1,9 +1,8 @@ $v) { + if (is_array($array)) foreach ($array as $k => $v) { if (is_array($v)) { if (empty($v)) $text .= '<' . $k . '/>'; else { @@ -43,7 +42,10 @@ class XMLConverterTool { $event['Event']['SharingGroup'][0] = $event['SharingGroup']; } if (isset($event['Attribute'])) $event['Event']['Attribute'] = $event['Attribute']; - if (isset($event['ShadowAttribute'])) $event['Event']['ShadowAttribute'] = $event['ShadowAttribute']; + if (isset($event['ShadowAttribute'])) { + $event['Event']['ShadowAttribute'] = $event['ShadowAttribute']; + unset($event['ShadowAttribute']); + } if (isset($event['RelatedEvent'])) if (isset($event['RelatedEvent'])) $event['Event']['RelatedEvent'] = $event['RelatedEvent']; // legacy @@ -81,6 +83,8 @@ class XMLConverterTool { $event['Event']['Attribute'][$key]['ShadowAttribute'][$skey]['value'] = str_replace($toEscape, $escapeWith, $event['Event']['Attribute'][$key]['ShadowAttribute'][$skey]['value']); $event['Event']['Attribute'][$key]['ShadowAttribute'][$skey]['comment'] = preg_replace ('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', ' ', $event['Event']['Attribute'][$key]['ShadowAttribute'][$skey]['comment']); $event['Event']['Attribute'][$key]['ShadowAttribute'][$skey]['comment'] = str_replace($toEscape, $escapeWith, $event['Event']['Attribute'][$key]['ShadowAttribute'][$skey]['comment']); + $event['Event']['Attribute'][$key]['ShadowAttribute'][$skey]['Org'] = array(0 => $event['Event']['Attribute'][$key]['ShadowAttribute'][$skey]['Org']); + $event['Event']['Attribute'][$key]['ShadowAttribute'][$skey]['EventOrg'] = array(0 => $event['Event']['Attribute'][$key]['ShadowAttribute'][$skey]['EventOrg']); } } if (isset($event['Event']['Attribute'][$key]['SharingGroup']['SharingGroupOrg'])) { @@ -107,6 +111,8 @@ class XMLConverterTool { $event['Event']['ShadowAttribute'][$key]['value'] = str_replace($toEscape, $escapeWith, $event['Event']['ShadowAttribute'][$key]['value']); $event['Event']['ShadowAttribute'][$key]['comment'] = preg_replace ('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', ' ', $event['Event']['ShadowAttribute'][$key]['comment']); $event['Event']['ShadowAttribute'][$key]['comment'] = str_replace($toEscape, $escapeWith, $event['Event']['ShadowAttribute'][$key]['comment']); + $event['Event']['ShadowAttribute'][$key]['Org'] = array(0 => $event['Event']['ShadowAttribute'][$key]['Org']); + $event['Event']['ShadowAttribute'][$key]['EventOrg'] = array(0 => $event['Event']['ShadowAttribute'][$key]['EventOrg']); } } diff --git a/app/Model/Event.php b/app/Model/Event.php index 961313624..8216cc39e 100755 --- a/app/Model/Event.php +++ b/app/Model/Event.php @@ -302,7 +302,7 @@ class Event extends AppModel { ), 'Orgc' => array( 'className' => 'Organisation', - 'foreignKey' => 'org_id' + 'foreignKey' => 'orgc_id' ), 'SharingGroup' => array( 'className' => 'SharingGroup', @@ -1270,6 +1270,8 @@ class Event extends AppModel { 'ShadowAttribute' => array( 'fields' => $fieldsShadowAtt, 'conditions' => array('deleted' => 0), + 'Org' => array('fields' => $fieldsOrg), + 'EventOrg' => array('fields' => $fieldsOrg) ), 'SharingGroup' => $fieldsSharingGroup[(($user['Role']['perm_site_admin'] || $user['Role']['perm_sync']) ? 1 : 0)], 'EventTag' => array( @@ -1688,9 +1690,14 @@ class Event extends AppModel { // When we receive an event via REST, we might end up with organisations, sharing groups, tags that we do not know // or which we need to update. All of that is controller in this method. private function __captureObjects($data, $user) { + // first we want to see how the creator organisation is encoded + // The options here are either by passing an organisation object along or simply passing a string along if (isset($data['Event']['Orgc'])) { $data['Event']['orgc_id'] = $this->Orgc->captureOrg($data['Event']['Orgc'], $user); unset ($data['Event']['Orgc']); + } else if (isset($data['Event']['orgc'])) { + $data['Event']['orgc_id'] = $this->Orgc->captureOrg($data['Event']['orgc'], $user); + unset($data['Event']['orgc']); } if (isset($data['Event']['EventTag'])) { if (isset($data['Event']['EventTag']['id'])) { @@ -1743,7 +1750,9 @@ class Event extends AppModel { } // set these fields if the event is freshly created and not pushed from another instance. // Moved out of if (!$fromXML), since we might get a restful event without the orgc/timestamp set - if (!isset ($data['Event']['orgc_id'])) $data['Event']['orgc_id'] = $data['Event']['org_id']; + if (!isset($data['Event']['orgc_id']) && !isset($data['Event']['orgc'])) { + $data['Event']['orgc_id'] = $data['Event']['org_id']; + } if ($fromXml) { // Workaround for different structure in XML/array than what CakePHP expects $data = $this->cleanupEventArrayFromXML($data); @@ -1800,7 +1809,7 @@ class Event extends AppModel { App::import('Component','Auth'); } $localEvent = $this->find('first', array('conditions' => array('Event.id' => $id), 'recursive' => -1, 'contain' => array('Attribute', 'ThreatLevel', 'ShadowAttribute'))); - if (!isset ($data['Event']['orgc_id'])) $data['Event']['orgc_id'] = $data['Event']['org_id']; + if (!isset($data['Event']['orgc_id']) && !isset($data['Event']['orgc'])) $data['Event']['orgc_id'] = $data['Event']['org_id']; if ($localEvent['Event']['timestamp'] < $data['Event']['timestamp']) { } else { diff --git a/app/Model/Organisation.php b/app/Model/Organisation.php index 8747023c4..dedb64bf1 100644 --- a/app/Model/Organisation.php +++ b/app/Model/Organisation.php @@ -85,15 +85,26 @@ class Organisation extends AppModel{ } public function captureOrg($org, $user) { + if (is_array($org)) { + $conditions = array('uuid' => $org['uuid']); + $uuid = $org['uuid']; + $name = $org['name']; + } else { + $conditions = array('name' => $org); + $uuid = String::uuid(); + $name = $org; + } + $existingOrg = $this->find('first', array( - 'recursive' => -1, - 'conditions' => array('uuid' => $org['uuid']) + 'recursive' => -1, + 'conditions' => $conditions, )); + if (empty($existingOrg)) { $this->create(); $organisation = array( - 'uuid' => $org['uuid'], - 'name' => $org['name'], + 'uuid' => $uuid, + 'name' => $name, 'local' => 0, 'created_by' => $user['id'] ); diff --git a/app/Model/Server.php b/app/Model/Server.php index f9cd3fa22..6dd01fd79 100755 --- a/app/Model/Server.php +++ b/app/Model/Server.php @@ -950,6 +950,15 @@ class Server extends AppModel { if ($oldAttribute) $proposal['old_id'] = $oldAttribute['Attribute']['id']; else $proposal['old_id'] = 0; } + // check if this is a proposal from an old MISP instance + if (!isset($proposal['org_id']) && isset($proposal['org'])) { + $proposal['Org'] = $proposal['org']; + $proposal['EventOrg'] = $proposal['event_org']; + } + $proposal['org_id'] = $this->Organisation->captureOrg($proposal['Org'], $user); + $proposal['event_org_id'] = $this->Organisation->captureOrg($proposal['EventOrg'], $user); + unset($proposal['Org']); + unset($proposal['EventOrg']); $shadowAttribute->create(); $shadowAttribute->save($proposal); } diff --git a/app/Model/ShadowAttribute.php b/app/Model/ShadowAttribute.php index 06b4465af..dbbf6585a 100644 --- a/app/Model/ShadowAttribute.php +++ b/app/Model/ShadowAttribute.php @@ -38,7 +38,15 @@ class ShadowAttribute extends AppModel { 'fields' => '', 'order' => '', 'counterCache' => true - ) + ), + 'Org' => array( + 'className' => 'Organisation', + 'foreignKey' => 'org_id' + ), + 'EventOrg' => array( + 'className' => 'Organisation', + 'foreignKey' => 'event_org_id' + ), ); /** diff --git a/app/Model/SharingGroup.php b/app/Model/SharingGroup.php index 988fd3da9..cac4e6371 100644 --- a/app/Model/SharingGroup.php +++ b/app/Model/SharingGroup.php @@ -173,7 +173,7 @@ class SharingGroup extends AppModel { 'SharingGroupServer' => array('fields' => array('id', 'server_id', 'all_orgs')), ) )); - + if (empty($sg)) return array(); // if the current server is marked as "all orgs" in the sharing group, just return true foreach ($sg['SharingGroupServer'] as $sgs) { if ($sgs['server_id'] == 0) { diff --git a/app/View/Logs/event_index.ctp b/app/View/Logs/event_index.ctp index dc5af0477..3816978df 100644 --- a/app/View/Logs/event_index.ctp +++ b/app/View/Logs/event_index.ctp @@ -28,7 +28,7 @@ $mayPublish = ($isAclPublish && $event['Event']['orgc'] == $me['org']); Paginator->sort('title');?> Paginator->sort('created');?> - +