add legacy_hid

This commit is contained in:
Cayo Puigdefabregas 2024-12-19 12:27:47 +01:00
parent c85e5bddb9
commit 13bac41f9f

View file

@ -3,11 +3,11 @@ import hashlib
import logging import logging
from dmidecode import DMIParse from dmidecode import DMIParse
from evidence.parse_details import ParseSnapshot
from evidence.models import Annotation from evidence.models import Annotation
from evidence.xapian import index from evidence.xapian import index
from evidence.parse_details import get_inxi_key, get_inxi from evidence.parse_details import ParseSnapshot, get_inxi_key, get_inxi, get_lshw_child
from utils.constants import CHASSIS_DH
from django.conf import settings from django.conf import settings
if settings.DPP: if settings.DPP:
@ -25,6 +25,26 @@ def get_mac(inxi):
return get_inxi(iface, 'mac') return get_inxi(iface, 'mac')
def get_legacy_mac(lshw):
try:
if type(lshw) is dict:
hw = lshw
else:
hw = json.loads(lshw)
except json.decoder.JSONDecodeError:
return ""
nets = []
get_lshw_child(hw, nets, 'network')
nets_sorted = sorted(nets, key=lambda x: x['businfo'])
if nets_sorted:
mac = nets_sorted[0]['serial']
logger.debug("The snapshot has the following MAC: %s" , mac)
return mac
class Build: class Build:
def __init__(self, evidence_json, user, check=False): def __init__(self, evidence_json, user, check=False):
self.evidence = evidence_json.copy() self.evidence = evidence_json.copy()
@ -67,7 +87,7 @@ class Build:
def get_hid_14(self): def get_hid_14(self):
if self.json.get("software") == "workbench-script": if self.json.get("software") == "workbench-script":
hid = self.get_hid(self.json) hid = self.get_hid()
else: else:
device = self.json['device'] device = self.json['device']
manufacturer = device.get("manufacturer", '') manufacturer = device.get("manufacturer", '')
@ -145,7 +165,10 @@ class Build:
value=v value=v
) )
def get_hid(self, snapshot): def get_hid(self):
if not self.json.get("data", {}).get("inxi"):
return self.get_legacy_hid()
try: try:
self.inxi = self.json["data"]["inxi"] self.inxi = self.json["data"]["inxi"]
if isinstance(self.inxi, str): if isinstance(self.inxi, str):
@ -168,11 +191,50 @@ class Build:
mac = get_mac(self.inxi) or "" mac = get_mac(self.inxi) or ""
if not mac: if not mac:
txt = "Could not retrieve MAC address in snapshot %s" txt = "Could not retrieve MAC address in snapshot %s"
logger.warning(txt, snapshot['uuid']) logger.warning(txt, self.json.get('uuid', "n/a"))
return f"{manufacturer}{model}{chassis}{serial_number}{sku}" return f"{manufacturer}{model}{chassis}{serial_number}{sku}"
return f"{manufacturer}{model}{chassis}{serial_number}{sku}{mac}" return f"{manufacturer}{model}{chassis}{serial_number}{sku}{mac}"
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_legacy_hid(self):
dmidecode_raw = self.json.get("data", {}).get("dmidecode")
self.dmi = DMIParse(dmidecode_raw)
manufacturer = self.dmi.manufacturer().strip()
model = self.dmi.model().strip()
chassis = self.get_chassis_dh()
serial_number = self.dmi.serial_number()
sku = self.get_sku()
if not self.json.get("data", {}).get('lshw'):
return f"{manufacturer}{model}{chassis}{serial_number}{sku}"
lshw = self.json.get("data", {}).get("lshw")
# mac = get_mac2(hwinfo_raw) or ""
mac = get_legacy_mac(lshw) or ""
if not mac:
txt = "Could not retrieve MAC address in snapshot %s"
logger.warning(txt, self.json.get('uuid', "n/a"))
return f"{manufacturer}{model}{chassis}{serial_number}{sku}{mac}"
def get_signature(self, doc): def get_signature(self, doc):
return hashlib.sha3_256(json.dumps(doc).encode()).hexdigest() return hashlib.sha3_256(json.dumps(doc).encode()).hexdigest()