devicehub-django/evidence/parse.py

61 lines
1.6 KiB
Python
Raw Normal View History

2024-07-11 15:40:45 +00:00
import os
import json
import shutil
import hashlib
2024-07-01 10:17:23 +00:00
2024-07-11 15:40:45 +00:00
from datetime import datetime
2024-07-26 15:59:34 +00:00
from evidence.xapian import search, index
from evidence.models import Evidence, Annotation
2024-07-18 15:21:22 +00:00
from utils.constants import ALGOS
2024-07-11 15:40:45 +00:00
class Build:
2024-07-26 15:59:34 +00:00
def __init__(self, evidence_json, user):
self.json = evidence_json
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-07-15 14:23:14 +00:00
self.index()
2024-07-18 15:21:22 +00:00
self.create_annotations()
2024-07-11 15:40:45 +00:00
2024-07-15 14:23:14 +00:00
def index(self):
snap = json.dumps(self.json)
2024-07-18 15:21:22 +00:00
index(self.uuid, snap)
2024-07-15 14:23:14 +00:00
def get_hid_14(self):
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}"
return hashlib.sha3_256(hid.encode()).hexdigest()
2024-07-18 15:21:22 +00:00
def create_annotations(self):
algorithms = {
'hidalgo1': self.get_hid_14(),
}
2024-07-23 13:37:40 +00:00
# TODO is neccesary?
2024-07-18 15:21:22 +00:00
annotation = Annotation.objects.filter(
owner=self.user,
type=Annotation.Type.SYSTEM,
key='hidalgo1',
value = algorithms['hidalgo1']
).first()
for k, v in algorithms.items():
2024-07-23 13:37:40 +00:00
if annotation and k == annotation.key:
continue
2024-07-18 15:21:22 +00:00
Annotation.objects.create(
uuid=self.uuid,
owner=self.user,
type=Annotation.Type.SYSTEM,
key=k,
value=v
)
2024-07-11 15:40:45 +00:00