From bc98a44c58cd1751c5df87838becb0a92d5a079f Mon Sep 17 00:00:00 2001 From: iglocska Date: Wed, 10 Apr 2019 08:29:28 +0200 Subject: [PATCH] new: [CLI] Update all JSON structures in one shot --- app/Console/Command/AdminShell.php | 360 +++++++++++++++-------------- app/Model/ObjectTemplate.php | 13 +- 2 files changed, 199 insertions(+), 174 deletions(-) diff --git a/app/Console/Command/AdminShell.php b/app/Console/Command/AdminShell.php index 23be71703..83eb23bc7 100644 --- a/app/Console/Command/AdminShell.php +++ b/app/Console/Command/AdminShell.php @@ -2,37 +2,37 @@ App::uses('AppShell', 'Console/Command'); class AdminShell extends AppShell { - public $uses = array('Event', 'Post', 'Attribute', 'Job', 'User', 'Task', 'Whitelist', 'Server', 'Organisation', 'AdminSetting', 'Galaxy', 'Taxonomy', 'Warninglist', 'Noticelist', 'ObjectTemplate', 'Bruteforce', 'Role'); + public $uses = array('Event', 'Post', 'Attribute', 'Job', 'User', 'Task', 'Whitelist', 'Server', 'Organisation', 'AdminSetting', 'Galaxy', 'Taxonomy', 'Warninglist', 'Noticelist', 'ObjectTemplate', 'Bruteforce', 'Role'); - public function jobGenerateCorrelation() { - $jobId = $this->args[0]; - $this->loadModel('Job'); - $this->Job->id = $jobId; - $this->loadModel('Attribute'); - $this->Attribute->generateCorrelation($jobId, 0); - $this->Job->saveField('progress', 100); - $this->Job->saveField('message', 'Job done.'); - $this->Job->saveField('status', 4); - } + public function jobGenerateCorrelation() { + $jobId = $this->args[0]; + $this->loadModel('Job'); + $this->Job->id = $jobId; + $this->loadModel('Attribute'); + $this->Attribute->generateCorrelation($jobId, 0); + $this->Job->saveField('progress', 100); + $this->Job->saveField('message', 'Job done.'); + $this->Job->saveField('status', 4); + } - public function jobPurgeCorrelation() { - $jobId = $this->args[0]; - $this->loadModel('Job'); - $this->Job->id = $jobId; - $this->loadModel('Attribute'); - $this->Attribute->purgeCorrelations(); - $this->Job->saveField('progress', 100); - $this->Job->saveField('message', 'Job done.'); - $this->Job->saveField('status', 4); - } + public function jobPurgeCorrelation() { + $jobId = $this->args[0]; + $this->loadModel('Job'); + $this->Job->id = $jobId; + $this->loadModel('Attribute'); + $this->Attribute->purgeCorrelations(); + $this->Job->saveField('progress', 100); + $this->Job->saveField('message', 'Job done.'); + $this->Job->saveField('status', 4); + } - public function jobGenerateShadowAttributeCorrelation() { - $jobId = $this->args[0]; - $this->loadModel('Job'); - $this->Job->id = $jobId; - $this->loadModel('ShadowAttribute'); - $this->ShadowAttribute->generateCorrelation($jobId); - } + public function jobGenerateShadowAttributeCorrelation() { + $jobId = $this->args[0]; + $this->loadModel('Job'); + $this->Job->id = $jobId; + $this->loadModel('ShadowAttribute'); + $this->ShadowAttribute->generateCorrelation($jobId); + } public function updateMISP() { $status = array('branch' => '2.4'); @@ -97,103 +97,123 @@ class AdminShell extends AppShell ); } - public function updateGalaxies() { - // The following is 7.x upwards only - //$value = $this->args[0] ?? $this->args[0] ?? 0; - $value = empty($this->args[0]) ? null : $this->args[0]; - if ($value === 'false') $value = 0; - if ($value === 'true') $value = 1; - if ($value === 'force') $value = 1; - $force = $value; - $result = $this->Galaxy->update($force); - if ($result) { - echo 'Galaxies updated'; - } else { - echo 'Could not update Galaxies'; - } - } + public function updateJSON() { + $toUpdate = array('Galaxy', 'Noticelist', 'Warninglist', 'Taxonomy', 'ObjectTemplate'); + echo 'Updating all JSON structures.' . PHP_EOL; + foreach ($toUpdate as $target) { + $result = $this->$target->update(); + if ($result !== false) { + echo sprintf( + __('%s updated.') . PHP_EOL, + Inflector::pluralize(Inflector::humanize($target)) + ); + } else { + echo sprintf( + __('Could not update %s.') . PHP_EOL, + Inflector::pluralize(Inflector::humanize($target)) + ); + } + } + echo 'All JSON structures updated. Thank you and have a very safe and productive day.'; + } - # FIXME: Make Taxonomy->update() return a status string on API if successful - public function updateTaxonomies() { - $result = $this->Taxonomy->update(); - if ($result) { - echo 'Taxonomies updated'; - } else { - echo 'Could not update Taxonomies'; - } - } + public function updateGalaxies() { + // The following is 7.x upwards only + //$value = $this->args[0] ?? $this->args[0] ?? 0; + $value = empty($this->args[0]) ? null : $this->args[0]; + if ($value === 'false') $value = 0; + if ($value === 'true') $value = 1; + if ($value === 'force') $value = 1; + $force = $value; + $result = $this->Galaxy->update($force); + if ($result) { + echo 'Galaxies updated'; + } else { + echo 'Could not update Galaxies'; + } + } - public function updateWarningLists() { - $result = $this->Galaxy->update(); - if ($result) { - echo 'Warning lists updated'; - } else { - echo 'Could not update warning lists'; - } - } + # FIXME: Make Taxonomy->update() return a status string on API if successful + public function updateTaxonomies() { + $result = $this->Taxonomy->update(); + if ($result) { + echo 'Taxonomies updated'; + } else { + echo 'Could not update Taxonomies'; + } + } - public function updateNoticeLists() { - $result = $this->Noticelist->update(); - if ($result) { - echo 'Notice lists updated'; - } else { - echo 'Could not update notice lists'; - } - } + public function updateWarningLists() { + $result = $this->Galaxy->update(); + if ($result) { + echo 'Warning lists updated'; + } else { + echo 'Could not update warning lists'; + } + } - # FIXME: Debug and make it work, fails to pass userId/orgId properly - public function updateObjectTemplates() { - if (empty($this->args[0])) { - echo 'Usage: ' . APP . '/cake ' . 'Admin updateNoticeLists [user_id]'; - } else { - $userId = $this->args[0]; - $user = $this->User->find('first', array( - 'recursive' => -1, - 'conditions' => array( - 'User.id' => $userId, - ), - 'fields' => array('User.id', 'User.org_id') - )); - if (empty($user)) { - echo 'User not found'; - } else { - $result = $this->ObjectTemplate->update($user, false,false); - if ($result) { - echo 'Object templates updated'; - } else { - echo 'Could not update object templates'; - } - } - } - } + public function updateNoticeLists() { + $result = $this->Noticelist->update(); + if ($result) { + echo 'Notice lists updated'; + } else { + echo 'Could not update notice lists'; + } + } - public function jobUpgrade24() { - $jobId = $this->args[0]; - $user_id = $this->args[1]; - $this->loadModel('Job'); - $this->Job->id = $jobId; - $this->loadModel('Server'); - $this->Server->upgrade2324($user_id, $jobId); - $this->Job->saveField('progress', 100); - $this->Job->saveField('message', 'Job done.'); - $this->Job->saveField('status', 4); - } + # FIXME: Debug and make it work, fails to pass userId/orgId properly + public function updateObjectTemplates() { + if (empty($this->args[0])) { + echo 'Usage: ' . APP . '/cake ' . 'Admin updateNoticeLists [user_id]'; + } else { + $userId = $this->args[0]; + $user = $this->User->find('first', array( + 'recursive' => -1, + 'conditions' => array( + 'User.id' => $userId, + ), + 'fields' => array('User.id', 'User.org_id') + )); + if (empty($user)) { + echo 'User not found'; + } else { + $result = $this->ObjectTemplate->update($user, false,false); + if ($result) { + echo 'Object templates updated'; + } else { + echo 'Could not update object templates'; + } + } + } + } - public function prune_update_logs() { - $jobId = $this->args[0]; - $user_id = $this->args[1]; - $user = $this->User->getAuthUser($user_id); - $this->loadModel('Job'); - $this->Job->id = $jobId; - $this->loadModel('Log'); - $this->Log->pruneUpdateLogs($jobId, $user); - $this->Job->saveField('progress', 100); - $this->Job->saveField('message', 'Job done.'); - $this->Job->saveField('status', 4); - } + public function jobUpgrade24() { + $jobId = $this->args[0]; + $user_id = $this->args[1]; + $this->loadModel('Job'); + $this->Job->id = $jobId; + $this->loadModel('Server'); + $this->Server->upgrade2324($user_id, $jobId); + $this->Job->saveField('progress', 100); + $this->Job->saveField('message', 'Job done.'); + $this->Job->saveField('status', 4); + } - public function getWorkers() { - $result = $this->Server->workerDiagnostics($workerIssueCount); + public function prune_update_logs() { + $jobId = $this->args[0]; + $user_id = $this->args[1]; + $user = $this->User->getAuthUser($user_id); + $this->loadModel('Job'); + $this->Job->id = $jobId; + $this->loadModel('Log'); + $this->Log->pruneUpdateLogs($jobId, $user); + $this->Job->saveField('progress', 100); + $this->Job->saveField('message', 'Job done.'); + $this->Job->saveField('status', 4); + } + + public function getWorkers() { + $result = $this->Server->workerDiagnostics($workerIssueCount); $query = 'all'; if (!empty($this->args[0])) { $query = $this->args[0]; @@ -213,34 +233,34 @@ class AdminShell extends AppShell } } } - echo json_encode($result, JSON_PRETTY_PRINT) . PHP_EOL; + echo json_encode($result, JSON_PRETTY_PRINT) . PHP_EOL; } - public function getSetting() { - $param = empty($this->args[0]) ? 'all' : $this->args[0]; - $settings = $this->Server->serverSettingsRead(); - $result = $settings; - if (!empty($param)) { - $result = 'No valid setting found for ' . $param; - foreach ($settings as $setting) { - if ($setting['setting'] == $param) { - $result = $setting; - break; - } - } - } - echo json_encode($result, JSON_PRETTY_PRINT) . PHP_EOL; + public function getSetting() { + $param = empty($this->args[0]) ? 'all' : $this->args[0]; + $settings = $this->Server->serverSettingsRead(); + $result = $settings; + if (!empty($param)) { + $result = 'No valid setting found for ' . $param; + foreach ($settings as $setting) { + if ($setting['setting'] == $param) { + $result = $setting; + break; + } + } + } + echo json_encode($result, JSON_PRETTY_PRINT) . PHP_EOL; } - public function setSetting() { - $setting_name = !isset($this->args[0]) ? null : $this->args[0]; - $value = !isset($this->args[1]) ? null : $this->args[1]; - if ($value === 'false') $value = 0; - if ($value === 'true') $value = 1; + public function setSetting() { + $setting_name = !isset($this->args[0]) ? null : $this->args[0]; + $value = !isset($this->args[1]) ? null : $this->args[1]; + if ($value === 'false') $value = 0; + if ($value === 'true') $value = 1; $cli_user = array('id' => 0, 'email' => 'SYSTEM', 'Organisation' => array('name' => 'SYSTEM')); - if (empty($setting_name) || $value === null) { - echo 'Invalid parameters. Usage: ' . APP . 'Console/cake Admin setSetting [setting_name] [setting_value]'; - } else { + if (empty($setting_name) || $value === null) { + echo 'Invalid parameters. Usage: ' . APP . 'Console/cake Admin setSetting [setting_name] [setting_value]'; + } else { $setting = $this->Server->getSettingData($setting_name); if (empty($setting)) { echo 'Invalid setting. Please make sure that the setting that you are attempting to change exists.'; @@ -251,25 +271,25 @@ class AdminShell extends AppShell } else { echo $result; } - } + } echo PHP_EOL; - } + } - public function setDatabaseVersion() { - if (empty($this->args[0])) echo 'Invalid parameters. Usage: ' . APP . 'Console/cake Admin setDatabaseVersion [db_version]' . PHP_EOL; - else { - $db_version = $this->AdminSetting->find('first', array( - 'conditions' => array('setting' => 'db_version') - )); - if (!empty($db_version)) { - $db_version['value'] = trim($this->args[0]); - $this->AdminSetting->save($db_version); - echo 'Database version set. MISP will replay all of the upgrade scripts since the selected version on the next user login.' . PHP_EOL; - } else { - echo 'Something went wrong. Could not find the existing db version.' . PHP_EOL; - } - } - } + public function setDatabaseVersion() { + if (empty($this->args[0])) echo 'Invalid parameters. Usage: ' . APP . 'Console/cake Admin setDatabaseVersion [db_version]' . PHP_EOL; + else { + $db_version = $this->AdminSetting->find('first', array( + 'conditions' => array('setting' => 'db_version') + )); + if (!empty($db_version)) { + $db_version['value'] = trim($this->args[0]); + $this->AdminSetting->save($db_version); + echo 'Database version set. MISP will replay all of the upgrade scripts since the selected version on the next user login.' . PHP_EOL; + } else { + echo 'Something went wrong. Could not find the existing db version.' . PHP_EOL; + } + } + } public function updateDatabase() { echo 'Executing all updates to bring the database up to date with the current version.' . PHP_EOL; @@ -294,20 +314,20 @@ class AdminShell extends AppShell } } - public function clearBruteforce() - { - $conditions = array('Bruteforce.username !=' => ''); - if (!empty($this->args[0])) { + public function clearBruteforce() + { + $conditions = array('Bruteforce.username !=' => ''); + if (!empty($this->args[0])) { $conditions = array('Bruteforce.username' => $this->args[0]); } - $result = $this->Bruteforce->deleteAll($conditions, false, false); - $target = empty($this->args[0]) ? 'all users' : $this->args[0]; - if ($result) { - echo 'Brutefoce entries for ' . $target . ' deleted.' . PHP_EOL; - } else { - echo 'Something went wrong, could not delete bruteforce entries for ' . $target . '.' . PHP_EOL; - } - } + $result = $this->Bruteforce->deleteAll($conditions, false, false); + $target = empty($this->args[0]) ? 'all users' : $this->args[0]; + if ($result) { + echo 'Brutefoce entries for ' . $target . ' deleted.' . PHP_EOL; + } else { + echo 'Something went wrong, could not delete bruteforce entries for ' . $target . '.' . PHP_EOL; + } + } public function setDefaultRole() { diff --git a/app/Model/ObjectTemplate.php b/app/Model/ObjectTemplate.php index 4312dc055..589e7d1d5 100644 --- a/app/Model/ObjectTemplate.php +++ b/app/Model/ObjectTemplate.php @@ -47,7 +47,7 @@ class ObjectTemplate extends AppModel return true; } - public function update($user, $type = false, $force = false) + public function update($user = false, $type = false, $force = false) { $objectsDir = APP . 'files/misp-objects/objects'; $directories = glob($objectsDir . '/*', GLOB_ONLYDIR); @@ -94,7 +94,7 @@ class ObjectTemplate extends AppModel return $updated; } - private function __updateObjectTemplate($template, $current, $user) + private function __updateObjectTemplate($template, $current, $user = false) { $success = false; $template['requirements'] = array(); @@ -104,8 +104,13 @@ class ObjectTemplate extends AppModel $template['requirements'][$field] = $template[$field]; } } - $template['user_id'] = $user['id']; - $template['org_id'] = $user['org_id']; + if (!empty($user)) { + $template['user_id'] = $user['id']; + $template['org_id'] = $user['org_id']; + } else { + $template['user_id'] = 0; + $template['org_id'] = 0; + } $template['fixed'] = 1; $this->create(); $result = $this->save($template);