devicehub-django/evidence/parse.py

55 lines
1.4 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-31 11:28:46 +00:00
def __init__(self, evidence_json, user, check=False):
2024-07-26 15:59:34 +00:00
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-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-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
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):
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):
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,
owner=self.user,
type=Annotation.Type.SYSTEM,
key=k,
value=v
)