fix: [stix 1/2 restSearch] Deleting temporary files in case of an error in the python side

pull/5230/head
chrisr3d 2019-09-25 18:28:37 +02:00
parent cebe7de575
commit 78dc7e7512
No known key found for this signature in database
GPG Key ID: 6BBED1B63A6D639F
1 changed files with 18 additions and 5 deletions

View File

@ -34,7 +34,7 @@ class StixExport
$this->__empty_file = false; $this->__empty_file = false;
} else { } else {
if ($attributes_count > $this->__attributes_limit) { if ($attributes_count > $this->__attributes_limit) {
$randomFileName = $this->generateRandomFileName(); $randomFileName = $this->__generateRandomFileName();
$tmpFile = new File($this->__tmp_dir . $randomFileName, true, 0644); $tmpFile = new File($this->__tmp_dir . $randomFileName, true, 0644);
$tmpFile->write($event); $tmpFile->write($event);
$tmpFile->close(); $tmpFile->close();
@ -55,7 +55,7 @@ class StixExport
{ {
$this->__return_type = $options['returnFormat']; $this->__return_type = $options['returnFormat'];
$framing_cmd = $this->initiate_framing_params(); $framing_cmd = $this->initiate_framing_params();
$randomFileName = $this->generateRandomFileName(); $randomFileName = $this->__generateRandomFileName();
$this->__tmp_dir = $this->__scripts_dir . 'tmp/'; $this->__tmp_dir = $this->__scripts_dir . 'tmp/';
$this->__framing = json_decode(shell_exec($framing_cmd), true); $this->__framing = json_decode(shell_exec($framing_cmd), true);
$this->__stix_file = new File($this->__tmp_dir . $randomFileName . '.' . $this->__return_type); $this->__stix_file = new File($this->__tmp_dir . $randomFileName . '.' . $this->__return_type);
@ -75,10 +75,12 @@ class StixExport
$this->__tmp_file->close(); $this->__tmp_file->close();
array_push($this->__filenames, $this->__current_filename); array_push($this->__filenames, $this->__current_filename);
} }
foreach ($this->__filenames as $filename) { foreach ($this->__filenames as $f => $filename) {
$result = $this->__parse_misp_events($filename); $result = $this->__parse_misp_events($filename);
$decoded = json_decode($result, true); $decoded = json_decode($result, true);
if (!isset($decoded['success']) || !$decoded['success']) { if (!isset($decoded['success']) || !$decoded['success']) {
$this->__delete_temporary_files($f);
// TODO: return an exception from the python script instead of empty string
return ''; return '';
} }
$file = new File($this->__tmp_dir . $filename . '.out'); $file = new File($this->__tmp_dir . $filename . '.out');
@ -104,14 +106,25 @@ class StixExport
private function __initialize_misp_file() private function __initialize_misp_file()
{ {
$this->__current_filename = $this->generateRandomFileName(); $this->__current_filename = $this->__generateRandomFileName();
$this->__tmp_file = new File($this->__tmp_dir . $this->__current_filename, true, 0644); $this->__tmp_file = new File($this->__tmp_dir . $this->__current_filename, true, 0644);
$this->__tmp_file->write('{"response": ['); $this->__tmp_file->write('{"response": [');
$this->__empty_file = true; $this->__empty_file = true;
} }
public function generateRandomFileName() private function __generateRandomFileName()
{ {
return (new RandomTool())->random_str(false, 12); return (new RandomTool())->random_str(false, 12);
} }
private function __delete_temporary_files($index)
{
foreach ($this->__filenames as $f => $filename) {
if ($index >= $f) {
unlink($this->__tmp_dir . $filename);
}
}
$this->__stix_file->close();
$this->__stix_file->delete();
}
} }