new document and out device and components

This commit is contained in:
Cayo Puigdefabregas 2024-11-28 12:21:26 +01:00
parent 74f0d5a507
commit 594904905b
3 changed files with 85 additions and 6 deletions

View File

@ -4,6 +4,7 @@ import logging
from django.http import JsonResponse, Http404 from django.http import JsonResponse, Http404
from django.views.generic.base import TemplateView from django.views.generic.base import TemplateView
from device.models import Device from device.models import Device
from evidence.parse import Build
from dpp.api_dlt import ALGORITHM from dpp.api_dlt import ALGORITHM
from dpp.models import Proof from dpp.models import Proof
from dpp.api_dlt import PROOF_TYPE from dpp.api_dlt import PROOF_TYPE
@ -93,6 +94,8 @@ class PublicDeviceWebView(TemplateView):
'algorithm': ALGORITHM, 'algorithm': ALGORITHM,
'components': components, 'components': components,
'manufacturer DPP': '', 'manufacturer DPP': '',
'device': {},
'components': [],
} }
result = { result = {
'@context': ['https://ereuse.org/dpp0.json'], '@context': ['https://ereuse.org/dpp0.json'],
@ -100,7 +103,11 @@ class PublicDeviceWebView(TemplateView):
} }
if len(self.pk.split(":")) > 1: if len(self.pk.split(":")) > 1:
data['document'] = json.dumps(self.object.last_evidence.doc) dev = Build(self.object.last_evidence.doc, None, check=True)
doc = dev.get_phid()
data['document'] = json.dumps(doc)
data['device'] = dev.device
data['components'] = dev.components
self.object.get_evidences() self.object.get_evidences()
last_dpp = Proof.objects.filter( last_dpp = Proof.objects.filter(
@ -122,13 +129,18 @@ class PublicDeviceWebView(TemplateView):
self.object.initial() self.object.initial()
for d in self.object.evidences: for d in self.object.evidences:
d.get_doc() d.get_doc()
ev = json.dumps(d.doc) dev = Build(d.doc, None, check=True)
doc = dev.get_phid()
ev = json.dumps(doc)
rr = { rr = {
'dpp': d.key, 'dpp': d.key,
'document': ev, 'document': ev,
'algorithm': ALGORITHM, 'algorithm': ALGORITHM,
'manufacturer DPP': '', 'manufacturer DPP': '',
'device': dev.device,
'components': dev.components
} }
dpps.append(rr) dpps.append(rr)
return { return {
'@context': ['https://ereuse.org/dpp0.json'], '@context': ['https://ereuse.org/dpp0.json'],

View File

@ -4,7 +4,7 @@ import logging
from dmidecode import DMIParse from dmidecode import DMIParse
from json_repair import repair_json from json_repair import repair_json
from evidence.parse_details import get_lshw_child from evidence.parse_details import get_lshw_child, ParseSnapshot
from evidence.models import Annotation from evidence.models import Annotation
from evidence.xapian import index from evidence.xapian import index
@ -60,6 +60,7 @@ class Build:
def generate_chids(self): def generate_chids(self):
self.algorithms = { self.algorithms = {
'hidalgo1': self.get_hid_14(), 'hidalgo1': self.get_hid_14(),
'legacy_dpp': self.get_chid_dpp(),
} }
def get_hid_14(self): def get_hid_14(self):
@ -78,6 +79,56 @@ class Build:
self.chid = hashlib.sha3_256(hid.encode()).hexdigest() self.chid = hashlib.sha3_256(hid.encode()).hexdigest()
return self.chid 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}"
else:
device = self.json['device']
hid = self.get_id_hw_dpp(device)
self.chid = hashlib.sha3_256(hid.encode("utf-8")).hexdigest()
return self.chid
def get_id_hw_dpp(self, d):
manufacturer = d.get("manufacturer", '')
model = d.get("model", '')
chassis = d.get("chassis", '')
serial_number = d.get("serialNumber", '')
sku = d.get("sku", '')
typ = d.get("type", '')
version = d.get("version", '')
return f"{manufacturer}{model}{chassis}{serial_number}{sku}{typ}{version}"
def get_phid(self):
if self.json.get("software") == "workbench-script":
data = ParseSnapshot(self.json)
self.device = data.device
self.components = data.components
else:
self.device = self.json.get("device")
self.components = self.json.get("components" [])
device = self.get_id_hw_dpp(self.device)
components = self.json.get(self.components)
components = sorted(components, key=lambda x: x.get("type"))
doc = [("computer", device)]
for c in components:
doc.append((c.get("type"), self.get_id_hw_dpp(c)))
return doc
def create_annotations(self): def create_annotations(self):
annotation = Annotation.objects.filter( annotation = Annotation.objects.filter(
uuid=self.uuid, uuid=self.uuid,
@ -114,6 +165,9 @@ class Build:
def get_chassis(self): def get_chassis(self):
return self.dmi.get("Chassis")[0].get("Type", '_virtual') 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): def get_hid(self, snapshot):
dmidecode_raw = snapshot["data"]["dmidecode"] dmidecode_raw = snapshot["data"]["dmidecode"]
self.dmi = DMIParse(dmidecode_raw) self.dmi = DMIParse(dmidecode_raw)
@ -140,5 +194,7 @@ class Build:
return hashlib.sha3_256(json.dumps(doc).encode()).hexdigest() return hashlib.sha3_256(json.dumps(doc).encode()).hexdigest()
def register_device_dlt(self): def register_device_dlt(self):
register_device_dlt(self.chid, self.phid, self.uuid, self.user) chid = self.algorithms.get('legacy_dpp')
register_passport_dlt(self.chid, self.phid, self.uuid, self.user) phid = self.get_signature(self.get_phid(self))
register_device_dlt(chid, phid, self.uuid, self.user)
register_passport_dlt(chid, phid, self.uuid, self.user)

View File

@ -17,8 +17,19 @@ HID_ALGO1 = [
"sku" "sku"
] ]
LEGACY_DPP = [
"manufacturer",
"model",
"chassis",
"serialNumber",
"sku",
"type",
"version"
]
ALGOS = { ALGOS = {
"hidalgo1": HID_ALGO1, "hidalgo1": HID_ALGO1,
"legacy_dpp": LEGACY_DPP
} }