Merge pull request #8176 from JakubOnderka/test_custom_warninglist

Test custom warninglist
pull/8177/head
Jakub Onderka 2022-02-26 19:38:04 +01:00 committed by GitHub
commit 1530b5e9e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 17 deletions

View File

@ -417,15 +417,14 @@ class WarninglistsController extends AppController
public function delete($id)
{
if ($this->request->is('post')) {
$id = intval($id);
$id = (int)$id;
$result = $this->Warninglist->quickDelete($id);
if ($result) {
$this->Flash->success(__('Warninglist successfully deleted.'));
$this->redirect(array('controller' => 'warninglists', 'action' => 'index'));
} else {
$this->Flash->error(__('Warninglists could not be deleted.'));
$this->redirect(array('controller' => 'warninglists', 'action' => 'index'));
$this->Flash->error(__('Warninglist could not be deleted.'));
}
$this->redirect(['controller' => 'warninglists', 'action' => 'index']);
} else {
if ($this->request->is('ajax')) {
$this->set('id', $id);

View File

@ -1,6 +1,7 @@
<?php
App::uses('AppModel', 'Model');
App::uses('CidrTool', 'Tools');
App::uses('FileAccessTool', 'Tools');
/**
* @property WarninglistType $WarninglistType
@ -277,10 +278,7 @@ class Warninglist extends AppModel
$directories = glob(APP . 'files' . DS . 'warninglists' . DS . 'lists' . DS . '*', GLOB_ONLYDIR);
$updated = array('success' => [], 'fails' => []);
foreach ($directories as $dir) {
$file = new File($dir . DS . 'list.json');
$list = $this->jsonDecode($file->read());
$file->close();
$list = FileAccessTool::readJsonFromFile($dir . DS . 'list.json');
if (!isset($list['version'])) {
$list['version'] = 1;
}
@ -801,9 +799,9 @@ class Warninglist extends AppModel
try {
$id = (int)$this->id;
if (isset($data['WarninglistEntry'])) {
$this->WarninglistEntry->deleteAll(['warninglist_id' => $id]);
$this->WarninglistEntry->deleteAll(['warninglist_id' => $id], false);
$entriesToInsert = [];
foreach ($data['WarninglistEntry'] as &$entry) {
foreach ($data['WarninglistEntry'] as $entry) {
$entriesToInsert[] = [$entry['value'], isset($entry['comment']) ? $entry['comment'] : null, $id];
}
$db->insertMulti(
@ -814,7 +812,7 @@ class Warninglist extends AppModel
}
if (isset($data['WarninglistType'])) {
$this->WarninglistType->deleteAll(['warninglist_id' => $id]);
$this->WarninglistType->deleteAll(['warninglist_id' => $id], false);
foreach ($data['WarninglistType'] as &$entry) {
$entry['warninglist_id'] = $id;
}
@ -836,19 +834,26 @@ class Warninglist extends AppModel
throw $e;
}
if ($success) {
$this->afterFullSave(!isset($data['Warninglist']['id']), $success);
}
return $success;
}
public function afterSave($created, $options = array())
/**
* @param bool $created
* @return void
*/
private function afterFullSave($created, array $data)
{
if (isset($this->data['Warninglist']['default']) && $this->data['Warninglist']['default'] == 0) {
$this->regenerateWarninglistCaches($this->data['Warninglist']['id']);
if (isset($data['Warninglist']['default']) && $data['Warninglist']['default'] == 0) {
$this->regenerateWarninglistCaches($data['Warninglist']['id']);
}
$pubToZmq = Configure::read('Plugin.ZeroMQ_enable') && Configure::read('Plugin.ZeroMQ_warninglist_notifications_enable');
if ($pubToZmq) {
if ($this->pubToZmq('warninglist')) {
$warninglist = $this->find('first', [
'conditions' => ['id' => $this->data['Warninglist']['id']],
'conditions' => ['id' => $data['Warninglist']['id']],
'contains' => ['WarninglistEntry', 'WarninglistType'],
]);
$pubSubTool = $this->getPubSubTool();

View File

@ -670,6 +670,38 @@ class TestComprehensive(unittest.TestCase):
response = self.admin_misp_connector._check_json_response(response)
self.assertEqual(403, response["errors"][0])
def test_custom_warninglist(self):
warninglist = {
"Warninglist": {
"name": "Test",
"description": "Test",
"type": "cidr",
"category": "false_positive",
"matching_attributes": ["ip-src", "ip-dst"],
"entries": "1.2.3.4",
}
}
wl = self.admin_misp_connector._prepare_request('POST', 'warninglists/add', data=warninglist)
wl = self.admin_misp_connector._check_json_response(wl)
check_response(wl)
check_response(self.admin_misp_connector.enable_warninglist(wl["Warninglist"]["id"]))
response = self.admin_misp_connector.values_in_warninglist("1.2.3.4")
self.assertEqual(wl["Warninglist"]["id"], response["1.2.3.4"][0]["id"])
warninglist["Warninglist"]["entries"] = "1.2.3.4\n2.3.4.5"
response = self.admin_misp_connector._prepare_request('POST', f'warninglists/edit/{wl["Warninglist"]["id"]}', data=warninglist)
response = self.admin_misp_connector._check_json_response(response)
check_response(response)
response = self.admin_misp_connector.values_in_warninglist("2.3.4.5")
self.assertEqual(wl["Warninglist"]["id"], response["2.3.4.5"][0]["id"])
response = self.admin_misp_connector._prepare_request('POST', f'warninglists/delete/{wl["Warninglist"]["id"]}')
response = self.admin_misp_connector._check_json_response(response)
check_response(response)
def _search(self, query: dict):
response = self.admin_misp_connector._prepare_request('POST', 'events/restSearch', data=query)
response = self.admin_misp_connector._check_response(response)