From a47ca7978c951cc0f65fcd40114915c0a091031b Mon Sep 17 00:00:00 2001 From: thomas rusiecki Date: Thu, 7 Nov 2024 12:57:00 -0300 Subject: [PATCH 1/5] better error handling issue #21 --- evidence/forms.py | 8 ++++++-- evidence/management/commands/up_snapshots.py | 16 ++++++++++++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/evidence/forms.py b/evidence/forms.py index dab7a60..05c9b82 100644 --- a/evidence/forms.py +++ b/evidence/forms.py @@ -37,8 +37,12 @@ class UploadForm(forms.Form): if exist_annotation: raise ValidationError("error: {} exist".format(file_name)) - except Exception: - raise ValidationError("error in: {}".format(file_name)) + except json.JSONDecodeError: + raise ValidationError("Error in parsing JSON: '{}'. Check for file integrity.".format(file_name)) + except ValidationError as e: + raise e + except Exception as e: + raise ValidationError("Oops! Something went wrong in '{}': {}".format(file_name, str(e))) self.evidences.append((file_name, file_json)) diff --git a/evidence/management/commands/up_snapshots.py b/evidence/management/commands/up_snapshots.py index 3cb12fa..bea6ae1 100644 --- a/evidence/management/commands/up_snapshots.py +++ b/evidence/management/commands/up_snapshots.py @@ -41,10 +41,18 @@ class Command(BaseCommand): self.open(filepath) def open(self, filepath): - with open(filepath, 'r') as file: - content = json.loads(file.read()) - self.snapshots.append(content) - + try: + with open(filepath, 'r') as file: + content = json.loads(file.read()) + path_name = save_in_disk(content, self.user.institution.name) + + self.snapshots.append((content, path_name)) + except json.JSONDecodeError as e: + raise e + #or we cath'em all + except Exception: + raise Exception(f"Oops! Something went wrong there") + def parsing(self): for s in self.snapshots: self.devices.append(Build(s, self.user)) From 434c518eec98a80a65068cf23a60748c235c935b Mon Sep 17 00:00:00 2001 From: thomas rusiecki Date: Thu, 7 Nov 2024 12:59:05 -0300 Subject: [PATCH 2/5] check that websnapshot has values --- device/templates/details.html | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/device/templates/details.html b/device/templates/details.html index d0e9c02..593d549 100644 --- a/device/templates/details.html +++ b/device/templates/details.html @@ -55,8 +55,7 @@
{% trans "Type" %}
{{ object.type }}
- - {% if object.is_websnapshot %} + {% if object.is_websnapshot and object.last_user_evidence %} {% for k, v in object.last_user_evidence %}
{{ k }}
From 21b1f071515bad37c014138a599e1a8bbe54e6af Mon Sep 17 00:00:00 2001 From: thomas rusiecki Date: Thu, 7 Nov 2024 13:09:22 -0300 Subject: [PATCH 3/5] fixed mac retrieval --- evidence/parse.py | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/evidence/parse.py b/evidence/parse.py index 944dbd3..8848265 100644 --- a/evidence/parse.py +++ b/evidence/parse.py @@ -5,23 +5,15 @@ import logging from dmidecode import DMIParse from json_repair import repair_json +from evidence.parse_details import get_lshw_child from evidence.models import Annotation from evidence.xapian import index from utils.constants import CHASSIS_DH -logger = logging.getLogger('django') - - -def get_network_cards(child, nets): - if child['id'] == 'network' and "PCI:" in child.get("businfo"): - nets.append(child) - if child.get('children'): - [get_network_cards(x, nets) for x in child['children']] - +logger = logging.getLogger(__name__) def get_mac(lshw): - nets = [] try: if type(lshw) is dict: hw = lshw @@ -30,19 +22,17 @@ def get_mac(lshw): except json.decoder.JSONDecodeError: hw = json.loads(repair_json(lshw)) - try: - get_network_cards(hw, nets) - except Exception as ss: - print("WARNING!! {}".format(ss)) - return + networks = [] + get_lshw_child(hw, networks, 'network') - nets_sorted = sorted(nets, key=lambda x: x['businfo']) + nets_sorted = sorted(networks, key=lambda x: x['businfo']) # This funcion get the network card integrated in motherboard # integrate = [x for x in nets if "pci@0000:00:" in x.get('businfo', '')] if nets_sorted: - return nets_sorted[0]['serial'] - + mac = nets_sorted[0]['serial'] + logger.debug("The snapshot has the following MAC: %s" , mac) + return mac class Build: def __init__(self, evidence_json, user, check=False): From cdf4771b7dbce9783d4f370b8a97085d4886ac62 Mon Sep 17 00:00:00 2001 From: thomas rusiecki Date: Thu, 7 Nov 2024 13:13:32 -0300 Subject: [PATCH 4/5] exception message minor change --- evidence/forms.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/evidence/forms.py b/evidence/forms.py index 05c9b82..005a2c0 100644 --- a/evidence/forms.py +++ b/evidence/forms.py @@ -35,7 +35,7 @@ class UploadForm(forms.Form): ).first() if exist_annotation: - raise ValidationError("error: {} exist".format(file_name)) + raise ValidationError("Error: {} already exists".format(file_name)) except json.JSONDecodeError: raise ValidationError("Error in parsing JSON: '{}'. Check for file integrity.".format(file_name)) From ae991be92472ae3e8d0fa6d131813ab70d3d8fc4 Mon Sep 17 00:00:00 2001 From: Thomas Rusiecki Date: Thu, 7 Nov 2024 16:21:37 -0300 Subject: [PATCH 5/5] added missing code; got deleted, probably amnesia --- evidence/management/commands/up_snapshots.py | 25 ++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/evidence/management/commands/up_snapshots.py b/evidence/management/commands/up_snapshots.py index bea6ae1..f2dbb22 100644 --- a/evidence/management/commands/up_snapshots.py +++ b/evidence/management/commands/up_snapshots.py @@ -1,12 +1,18 @@ - import os import json +import logging from django.core.management.base import BaseCommand from django.contrib.auth import get_user_model +from django.conf import settings + +from utils.save_snapshots import move_json, save_in_disk from evidence.parse import Build +logger = logging.getLogger('django') + + User = get_user_model() @@ -31,9 +37,11 @@ class Command(BaseCommand): elif os.path.isdir(path): self.read_directory(path) + else: + raise ValueError(f"The path {path} is neither a file nor a directory") self.parsing() - + def read_directory(self, directory): for filename in os.listdir(directory): filepath = os.path.join(directory, filename) @@ -53,6 +61,15 @@ class Command(BaseCommand): except Exception: raise Exception(f"Oops! Something went wrong there") + def parsing(self): - for s in self.snapshots: - self.devices.append(Build(s, self.user)) + for s, p in self.snapshots: + try: + self.devices.append(Build(s, self.user)) + move_json(p, self.user.institution.name) + except Exception as err: + if settings.DEBUG: + logger.exception("%s", err) + snapshot_id = s.get("uuid", "") + txt = "It is not possible to parse snapshot: %s" + logger.error(txt, snapshot_id)