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-09-25 10:51:08 +00:00
|
|
|
from evidence.models import Annotation
|
2024-10-04 15:32:53 +00:00
|
|
|
from evidence.xapian import index
|
2024-11-18 18:37:08 +00:00
|
|
|
from dpp.api_dlt import register_device_dlt, register_passport_dlt
|
2024-11-15 11:47:08 +00:00
|
|
|
from evidence.parse_details import get_inxi_key, get_inxi
|
2024-10-25 15:36:13 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger('django')
|
2024-07-11 15:40:45 +00:00
|
|
|
|
2024-11-11 06:41:08 +00:00
|
|
|
|
2024-11-15 11:47:08 +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
|
|
|
|
2024-11-15 11:47:08 +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
|
|
|
|
2024-09-20 16:05:29 +00:00
|
|
|
|
2024-07-11 15:40:45 +00:00
|
|
|
class Build:
|
2024-07-31 11:28:46 +00:00
|
|
|
def __init__(self, evidence_json, user, check=False):
|
2024-12-03 15:37:56 +00:00
|
|
|
self.evidence = evidence_json.copy()
|
|
|
|
self.json = evidence_json.copy()
|
2024-12-05 18:23:53 +00:00
|
|
|
|
2024-12-03 15:37:56 +00:00
|
|
|
if evidence_json.get("credentialSubject"):
|
|
|
|
self.json.update(evidence_json["credentialSubject"])
|
|
|
|
if evidence_json.get("evidence"):
|
|
|
|
self.json["data"] = {}
|
|
|
|
for ev in evidence_json["evidence"]:
|
|
|
|
k = ev.get("operation")
|
|
|
|
if not k:
|
|
|
|
continue
|
|
|
|
self.json["data"][k] = ev.get("output")
|
|
|
|
|
2024-07-18 15:21:22 +00:00
|
|
|
self.uuid = self.json['uuid']
|
2024-07-11 15:40:45 +00:00
|
|
|
self.user = user
|
|
|
|
self.hid = None
|
2024-11-18 18:37:08 +00:00
|
|
|
self.chid = None
|
|
|
|
self.phid = self.get_signature(self.json)
|
2024-07-31 11:28:46 +00:00
|
|
|
self.generate_chids()
|
|
|
|
|
|
|
|
if check:
|
|
|
|
return
|
2024-07-11 15:40:45 +00:00
|
|
|
|
2024-07-15 14:23:14 +00:00
|
|
|
self.index()
|
2024-07-18 15:21:22 +00:00
|
|
|
self.create_annotations()
|
2024-11-18 18:37:08 +00:00
|
|
|
self.register_device_dlt()
|
2024-07-11 15:40:45 +00:00
|
|
|
|
2024-07-15 14:23:14 +00:00
|
|
|
def index(self):
|
2024-12-03 15:37:56 +00:00
|
|
|
snap = json.dumps(self.evidence)
|
2024-10-04 15:32:53 +00:00
|
|
|
index(self.user.institution, self.uuid, snap)
|
2024-07-15 14:23:14 +00:00
|
|
|
|
2024-07-31 11:28:46 +00:00
|
|
|
def generate_chids(self):
|
|
|
|
self.algorithms = {
|
|
|
|
'hidalgo1': self.get_hid_14(),
|
|
|
|
}
|
|
|
|
|
2024-07-15 14:23:14 +00:00
|
|
|
def get_hid_14(self):
|
2024-10-21 09:24:09 +00:00
|
|
|
if self.json.get("software") == "workbench-script":
|
2024-09-18 16:01:46 +00:00
|
|
|
hid = self.get_hid(self.json)
|
|
|
|
else:
|
|
|
|
device = self.json['device']
|
|
|
|
manufacturer = device.get("manufacturer", '')
|
|
|
|
model = device.get("model", '')
|
|
|
|
chassis = device.get("chassis", '')
|
|
|
|
serial_number = device.get("serialNumber", '')
|
|
|
|
sku = device.get("sku", '')
|
|
|
|
hid = f"{manufacturer}{model}{chassis}{serial_number}{sku}"
|
2024-10-25 15:36:13 +00:00
|
|
|
|
|
|
|
|
2024-11-18 18:37:08 +00:00
|
|
|
self.chid = hashlib.sha3_256(hid.encode()).hexdigest()
|
|
|
|
return self.chid
|
2024-07-15 14:23:14 +00:00
|
|
|
|
2024-07-18 15:21:22 +00:00
|
|
|
def create_annotations(self):
|
2024-10-25 15:36:13 +00:00
|
|
|
annotation = Annotation.objects.filter(
|
|
|
|
uuid=self.uuid,
|
|
|
|
owner=self.user.institution,
|
|
|
|
type=Annotation.Type.SYSTEM,
|
|
|
|
)
|
|
|
|
|
|
|
|
if annotation:
|
2024-10-31 13:24:16 +00:00
|
|
|
txt = "Warning: Snapshot %s already registered (annotation exists)"
|
2024-10-31 09:14:02 +00:00
|
|
|
logger.warning(txt, self.uuid)
|
2024-10-25 15:36:13 +00:00
|
|
|
return
|
2024-07-18 15:21:22 +00:00
|
|
|
|
2024-07-31 11:28:46 +00:00
|
|
|
for k, v in self.algorithms.items():
|
2024-07-18 15:21:22 +00:00
|
|
|
Annotation.objects.create(
|
|
|
|
uuid=self.uuid,
|
2024-09-18 16:01:46 +00:00
|
|
|
owner=self.user.institution,
|
2024-10-04 15:32:53 +00:00
|
|
|
user=self.user,
|
2024-07-18 15:21:22 +00:00
|
|
|
type=Annotation.Type.SYSTEM,
|
|
|
|
key=k,
|
|
|
|
value=v
|
|
|
|
)
|
2024-09-18 16:01:46 +00:00
|
|
|
|
|
|
|
def get_hid(self, snapshot):
|
2024-11-15 11:47:08 +00:00
|
|
|
try:
|
2024-12-05 18:23:53 +00:00
|
|
|
self.inxi = self.json["data"]["inxi"]
|
|
|
|
if isinstance(self.inxi, str):
|
|
|
|
self.inxi = json.loads(self.inxi)
|
2024-11-15 11:47:08 +00:00
|
|
|
except Exception:
|
|
|
|
logger.error("No inxi in snapshot %s", self.uuid)
|
|
|
|
return ""
|
2024-12-03 15:37:56 +00:00
|
|
|
|
2024-11-15 11:47:08 +00:00
|
|
|
machine = get_inxi_key(self.inxi, 'Machine')
|
|
|
|
for m in machine:
|
|
|
|
system = get_inxi(m, "System")
|
|
|
|
if system:
|
|
|
|
manufacturer = system
|
|
|
|
model = get_inxi(m, "product")
|
|
|
|
serial_number = get_inxi(m, "serial")
|
|
|
|
chassis = get_inxi(m, "Type")
|
|
|
|
else:
|
|
|
|
sku = get_inxi(m, "part-nu")
|
|
|
|
|
|
|
|
mac = get_mac(self.inxi) or ""
|
2024-09-20 16:22:27 +00:00
|
|
|
if not mac:
|
2024-10-31 09:40:53 +00:00
|
|
|
txt = "Could not retrieve MAC address in snapshot %s"
|
|
|
|
logger.warning(txt, snapshot['uuid'])
|
2024-11-15 11:47:08 +00:00
|
|
|
return f"{manufacturer}{model}{chassis}{serial_number}{sku}"
|
2024-09-18 16:01:46 +00:00
|
|
|
|
2024-09-20 16:05:29 +00:00
|
|
|
return f"{manufacturer}{model}{chassis}{serial_number}{sku}{mac}"
|
2024-11-18 18:37:08 +00:00
|
|
|
|
|
|
|
def get_signature(self, doc):
|
|
|
|
return hashlib.sha3_256(json.dumps(doc).encode()).hexdigest()
|
|
|
|
|
|
|
|
def register_device_dlt(self):
|
|
|
|
register_device_dlt(self.chid, self.phid, self.uuid, self.user)
|
|
|
|
register_passport_dlt(self.chid, self.phid, self.uuid, self.user)
|