devicehub-django/evidence/parse.py

96 lines
3.2 KiB
Python
Raw Permalink Normal View History

2024-07-11 15:40:45 +00:00
import json
import hashlib
2024-10-25 15:36:13 +00:00
import logging
2024-07-01 10:17:23 +00:00
2024-12-20 16:04:09 +00:00
from evidence import legacy_parse
2025-01-11 19:44:21 +00:00
from evidence import old_parse
from evidence import normal_parse
2024-12-16 17:12:02 +00:00
from evidence.parse_details import ParseSnapshot
2024-10-21 16:39:31 +00:00
from evidence.models import SystemProperty
from evidence.xapian import index
2025-01-11 19:44:21 +00:00
from evidence.normal_parse_details import get_inxi_key, get_inxi
2024-12-16 17:12:02 +00:00
from django.conf import settings
2024-12-05 09:54:41 +00:00
if settings.DPP:
from dpp.api_dlt import register_device_dlt, register_passport_dlt
2024-10-25 15:36:13 +00:00
logger = logging.getLogger('django')
2024-11-11 06:41:08 +00:00
2024-11-09 19:52:26 +00:00
def get_mac(inxi):
nets = get_inxi_key(inxi, "Network")
networks = [(nets[i], nets[i + 1]) for i in range(0, len(nets) - 1, 2)]
2024-11-09 19:52:26 +00:00
for n, iface in networks:
if get_inxi(n, "port"):
return get_inxi(iface, 'mac')
2024-07-11 15:40:45 +00:00
class Build:
2025-01-11 19:44:21 +00:00
def __init__(self, evidence_json, user, check=False):
"""
This Build do the save in xapian as document, in Annotations and do
register in dlt if is configured for that.
We have 4 cases for parser diferents snapshots than come from workbench.
1) worbench 11 is old_parse.
2) legacy is the worbench-script when create a snapshot for devicehub-teal
3) some snapshots come as a credential. In this case is parsed as normal_parse
4) normal snapshot from worbench-script is the most basic and is parsed as normal_parse
"""
self.evidence = evidence_json.copy()
2025-01-13 16:56:48 +00:00
self.uuid = self.evidence.get('uuid')
2025-01-11 19:44:21 +00:00
self.user = user
if evidence_json.get("credentialSubject"):
self.build = normal_parse.Build(evidence_json)
2025-01-13 16:56:48 +00:00
self.uuid = evidence_json.get("credentialSubject", {}).get("uuid")
2025-01-11 19:44:21 +00:00
elif evidence_json.get("software") != "workbench-script":
self.build = old_parse.Build(evidence_json)
elif evidence_json.get("data",{}).get("lshw"):
self.build = legacy_parse.Build(evidence_json)
else:
self.build = normal_parse.Build(evidence_json)
if check:
return
self.index()
self.create_annotations()
if settings.DPP:
self.register_device_dlt()
def index(self):
snap = json.dumps(self.evidence)
index(self.user.institution, self.uuid, snap)
def create_annotations(self):
2025-01-23 12:05:09 +00:00
prop = SystemProperty.objects.filter(
2025-01-11 19:44:21 +00:00
uuid=self.uuid,
owner=self.user.institution,
)
2025-01-23 12:05:09 +00:00
if prop:
txt = "Warning: Snapshot %s already registered (property exists)"
2025-01-11 19:44:21 +00:00
logger.warning(txt, self.uuid)
return
for k, v in self.build.algorithms.items():
2025-01-23 12:05:09 +00:00
SystemProperty.objects.create(
2025-01-11 19:44:21 +00:00
uuid=self.uuid,
owner=self.user.institution,
user=self.user,
key=k,
value=self.sign(v)
)
def sign(self, doc):
return hashlib.sha3_256(doc.encode()).hexdigest()
def register_device_dlt(self):
legacy_dpp = self.build.algorithms.get('legacy_dpp')
chid = self.sign(legacy_dpp)
phid = self.sign(json.dumps(self.build.get_doc()))
register_device_dlt(chid, phid, self.uuid, self.user)
register_passport_dlt(chid, phid, self.uuid, self.user)