From 782f6dac51c3f916862128fa17a26743a56677ed Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 16 Dec 2024 18:12:02 +0100 Subject: [PATCH] refactor inxi --- evidence/forms.py | 12 ++++++------ evidence/models.py | 38 ++++++++++++++++++++------------------ evidence/parse.py | 43 +++++-------------------------------------- 3 files changed, 31 insertions(+), 62 deletions(-) diff --git a/evidence/forms.py b/evidence/forms.py index 83d9e6b..8be2887 100644 --- a/evidence/forms.py +++ b/evidence/forms.py @@ -29,17 +29,17 @@ class UploadForm(forms.Form): try: file_json = json.loads(file_data) - Build(file_json, None, check=True) + snap = Build(file_json, None, check=True) exist_annotation = Annotation.objects.filter( - uuid=file_json['uuid'] + uuid=snap.uuid ).first() - + if exist_annotation: - raise ValidationError( + raise ValidationError( _("The snapshot already exists"), code="duplicate_snapshot", ) - + #Catch any error and display it as Validation Error so the Form handles it except Exception as e: raise ValidationError( @@ -221,7 +221,7 @@ class EraseServerForm(forms.Form): if self.instance: return - + Annotation.objects.create( uuid=self.uuid, type=Annotation.Type.ERASE_SERVER, diff --git a/evidence/models.py b/evidence/models.py index b410795..a10de01 100644 --- a/evidence/models.py +++ b/evidence/models.py @@ -63,13 +63,16 @@ class Evidence: def get_phid(self): if not self.doc: self.get_doc() - + return hashlib.sha3_256(json.dumps(self.doc)).hexdigest() def get_doc(self): self.doc = {} + self.inxi = None + if not self.owner: self.get_owner() + qry = 'uuid:"{}"'.format(self.uuid) matches = search(self.owner, qry, limit=1) if matches and matches.size() < 0: @@ -89,26 +92,26 @@ class Evidence: self.inxi = ev["output"] else: dmidecode_raw = self.doc["data"]["dmidecode"] + inxi_raw = self.doc["data"]["inxi"] + self.dmi = DMIParse(dmidecode_raw) try: - self.inxi = json.loads(self.doc["data"]["inxi"]) + self.inxi = json.loads(inxi_raw) + except Exception: + pass + if self.inxi: + try: + machine = get_inxi_key(self.inxi, 'Machine') + for m in machine: + system = get_inxi(m, "System") + if system: + self.device_manufacturer = system + self.device_model = get_inxi(m, "product") + self.device_serial_number = get_inxi(m, "serial") + self.device_chassis = get_inxi(m, "Type") + self.device_version = get_inxi(m, "v") except Exception: return - self.dmi = DMIParse(dmidecode_raw) - try: - machine = get_inxi_key(self.inxi, 'Machine') - for m in machine: - system = get_inxi(m, "System") - if system: - self.device_manufacturer = system - self.device_model = get_inxi(m, "product") - self.device_serial_number = get_inxi(m, "serial") - self.device_chassis = get_inxi(m, "Type") - self.device_version = get_inxi(m, "v") - - except Exception: - return - def get_time(self): if not self.doc: self.get_doc() @@ -177,7 +180,6 @@ class Evidence: return self.dmi.serial_number().strip() - def get_version(self): if self.inxi: return self.device_version diff --git a/evidence/parse.py b/evidence/parse.py index 790f809..db2d32d 100644 --- a/evidence/parse.py +++ b/evidence/parse.py @@ -3,21 +3,16 @@ import hashlib import logging from dmidecode import DMIParse -from json_repair import repair_json -from django.conf import settings -from evidence.parse_details import get_lshw_child, ParseSnapshot +from evidence.parse_details import ParseSnapshot from evidence.models import Annotation from evidence.xapian import index -from utils.constants import CHASSIS_DH +from evidence.parse_details import get_inxi_key, get_inxi +from django.conf import settings if settings.DPP: from dpp.api_dlt import register_device_dlt, register_passport_dlt - - -from evidence.parse_details import get_inxi_key, get_inxi - logger = logging.getLogger('django') @@ -82,27 +77,16 @@ class Build: sku = device.get("sku", '') hid = f"{manufacturer}{model}{chassis}{serial_number}{sku}" - self.chid = hashlib.sha3_256(hid.encode()).hexdigest() return self.chid def get_chid_dpp(self): if self.json.get("software") == "workbench-script": - dmidecode_raw = self.json["data"]["dmidecode"] - dmi = DMIParse(dmidecode_raw) - - manufacturer = dmi.manufacturer().strip() - model = dmi.model().strip() - chassis = self.get_chassis_dh() - serial_number = dmi.serial_number() - sku = self.get_sku() - typ = chassis - version = self.get_version() - hid = f"{manufacturer}{model}{chassis}{serial_number}{sku}{typ}{version}" + device = ParseSnapshot(self.json).device else: device = self.json['device'] - hid = self.get_id_hw_dpp(device) + hid = self.get_id_hw_dpp(device) self.chid = hashlib.sha3_256(hid.encode("utf-8")).hexdigest() return self.chid @@ -161,23 +145,6 @@ class Build: value=v ) - def get_chassis_dh(self): - chassis = self.get_chassis() - lower_type = chassis.lower() - for k, v in CHASSIS_DH.items(): - if lower_type in v: - return k - return self.default - - def get_sku(self): - return self.dmi.get("System")[0].get("SKU Number", "n/a").strip() - - def get_chassis(self): - return self.dmi.get("Chassis")[0].get("Type", '_virtual') # - - def get_version(self): - return self.dmi.get("System")[0].get("Verson", '_virtual') - def get_hid(self, snapshot): try: self.inxi = self.json["data"]["inxi"]