Skip to content

Commit

Permalink
Propagated errors
Browse files Browse the repository at this point in the history
  • Loading branch information
zbalkan committed Dec 11, 2024
1 parent 24d869a commit 7d6e4d5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
28 changes: 15 additions & 13 deletions src/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
from typing import IO, Optional
from zipfile import ZipFile

from config import PANHuntConfiguration
from exceptions import PANHuntException
import panutils
from config import PANHuntConfiguration
from job import Job


Expand All @@ -22,13 +22,13 @@ def __init__(self, path: str, payload: Optional[bytes] = None) -> None:
self.path = path
self.payload = payload

def get_children(self) -> list[Job]:
def get_children(self) -> tuple[list[Job], Optional[PANHuntException]]:
raise NotImplementedError()


class ZipArchive(Archive):

def get_children(self) -> list[Job]:
def get_children(self) -> tuple[list[Job], Optional[PANHuntException]]:
children: list[Job] = []

zip_ref: ZipFile
Expand All @@ -45,12 +45,12 @@ def get_children(self) -> list[Job]:
basename=file_info.filename, dirname=self.path, payload=payload)
children.append(job)
zip_ref.close()
return children
return children, None


class TarArchive(Archive):

def get_children(self) -> list[Job]:
def get_children(self) -> tuple[list[Job], Optional[PANHuntException]]:
children: list[Job] = []

tar_ref: TarFile
Expand All @@ -72,12 +72,12 @@ def get_children(self) -> list[Job]:
children.append(job)
tar_ref.close()

return children
return children, None


class GzipArchive(Archive):

def get_children(self) -> list[Job]:
def get_children(self) -> tuple[list[Job], Optional[PANHuntException]]:

gz_file: GzipFile

Expand Down Expand Up @@ -106,16 +106,18 @@ def get_children(self) -> list[Job]:
gz_file.close()

if not reached_eof:
raise PANHuntException('File size limit reached during decompressing. Skipping file.')
logging.warning('File size limit reached during decompressing. Skipping file.')
return [], PANHuntException(
f'File size limit reached during decompressing. Skipping file.')
else:
job = Job(
basename=compressed_filename, dirname=self.path, payload=payload)
return [job]
return [job], None


class XzArchive(Archive):

def get_children(self) -> list[Job]:
def get_children(self) -> tuple[list[Job], Optional[PANHuntException]]:

xz_file: LZMAFile

Expand All @@ -142,10 +144,10 @@ def get_children(self) -> list[Job]:
xz_file.close()

if not reached_eof:
raise PANHuntException(
'File size limit reached during decompressing. Skipping file.')
return [], PANHuntException(
f'File size limit reached during decompressing. Skipping file.')
else:
job = Job(
basename=compressed_filename, dirname=self.path, payload=payload)
xz_file.close()
return [job]
return [job], None
5 changes: 4 additions & 1 deletion src/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Optional

import enums
from exceptions import PANHuntException
import mappings
import panutils
from archive import Archive
Expand Down Expand Up @@ -94,7 +95,9 @@ def _dispatch_job(self, job: Job) -> Optional[Finding]:
# It's an archive, extract children and re-enqueue them as jobs
archive = archive_type(path=job.abspath, payload=job.payload)
try:
children: list[Job] = archive.get_children()
children, e = archive.get_children()
if e:
raise e
for child in children:
JobQueue().enqueue(child)
return None
Expand Down

0 comments on commit 7d6e4d5

Please sign in to comment.