From 7fd42db3e469a3f35872f5d417e9fa13f57172e1 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 3 Dec 2024 16:37:56 +0100 Subject: [PATCH 01/72] fix parsing --- api/views.py | 16 ++++++++++------ evidence/parse.py | 17 ++++++++++++++--- utils/save_snapshots.py | 5 ++++- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/api/views.py b/api/views.py index 0de3e5a..87177fb 100644 --- a/api/views.py +++ b/api/views.py @@ -85,17 +85,21 @@ class NewSnapshotView(ApiMixing): # except Exception: # return JsonResponse({'error': 'Invalid Snapshot'}, status=400) - if not data.get("uuid"): + ev_uuid = data.get("uuid") + if data.get("credentialSubject"): + ev_uuid = data["credentialSubject"].get("uuid") + + if not ev_uuid: txt = "error: the snapshot not have uuid" logger.error("%s", txt) return JsonResponse({'status': txt}, status=500) exist_annotation = Annotation.objects.filter( - uuid=data['uuid'] + uuid=ev_uuid ).first() if exist_annotation: - txt = "error: the snapshot {} exist".format(data['uuid']) + txt = "error: the snapshot {} exist".format(ev_uuid) logger.warning("%s", txt) return JsonResponse({'status': txt}, status=500) @@ -105,14 +109,14 @@ class NewSnapshotView(ApiMixing): except Exception as err: if settings.DEBUG: logger.exception("%s", err) - snapshot_id = data.get("uuid", "") + snapshot_id = ev_uuid txt = "It is not possible to parse snapshot: %s." logger.error(txt, snapshot_id) text = "fail: It is not possible to parse snapshot" return JsonResponse({'status': text}, status=500) annotation = Annotation.objects.filter( - uuid=data['uuid'], + uuid=ev_uuid, type=Annotation.Type.SYSTEM, # TODO this is hardcoded, it should select the user preferred algorithm key="hidalgo1", @@ -121,7 +125,7 @@ class NewSnapshotView(ApiMixing): if not annotation: - logger.error("Error: No annotation for uuid: %s", data["uuid"]) + logger.error("Error: No annotation for uuid: %s", ev_uuid) return JsonResponse({'status': 'fail'}, status=500) url_args = reverse_lazy("device:details", args=(annotation.value,)) diff --git a/evidence/parse.py b/evidence/parse.py index 3d95a84..9a8ec2a 100644 --- a/evidence/parse.py +++ b/evidence/parse.py @@ -23,7 +23,18 @@ def get_mac(inxi): class Build: def __init__(self, evidence_json, user, check=False): - self.json = evidence_json + self.evidence = evidence_json.copy() + self.json = evidence_json.copy() + 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") + self.uuid = self.json['uuid'] self.user = user self.hid = None @@ -36,7 +47,7 @@ class Build: self.create_annotations() def index(self): - snap = json.dumps(self.json) + snap = json.dumps(self.evidence) index(self.user.institution, self.uuid, snap) def generate_chids(self): @@ -87,7 +98,7 @@ class Build: except Exception: logger.error("No inxi in snapshot %s", self.uuid) return "" - + machine = get_inxi_key(self.inxi, 'Machine') for m in machine: system = get_inxi(m, "System") diff --git a/utils/save_snapshots.py b/utils/save_snapshots.py index 8c02f06..efd1fe9 100644 --- a/utils/save_snapshots.py +++ b/utils/save_snapshots.py @@ -19,7 +19,10 @@ def move_json(path_name, user, place="snapshots"): def save_in_disk(data, user, place="snapshots"): - uuid = data.get('uuid', '') + uuid = data.get("uuid") + if data.get("credentialSubject"): + uuid = data["credentialSubject"].get("uuid") + now = datetime.now() year = now.year month = now.month From 7de6d69a6c1bf9444c38b0688df4e20472784ff2 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 5 Dec 2024 19:23:53 +0100 Subject: [PATCH 02/72] fix parsing with credentials --- dashboard/views.py | 7 ++++-- evidence/models.py | 53 +++++++++++++++++++++++++-------------- evidence/parse.py | 5 +++- evidence/parse_details.py | 25 ++++++++++++------ 4 files changed, 60 insertions(+), 30 deletions(-) diff --git a/dashboard/views.py b/dashboard/views.py index 8d91732..2ec12a0 100644 --- a/dashboard/views.py +++ b/dashboard/views.py @@ -84,8 +84,11 @@ class SearchView(InventaryMixin): return devices, count def get_annotations(self, xp): - snap = xp.document.get_data() - uuid = json.loads(snap).get('uuid') + snap = json.loads(xp.document.get_data()) + if snap.get("credentialSubject"): + uuid = snap["credentialSubject"]["uuid"] + else: + uuid = snap["uuid"] return Device.get_annotation_from_uuid(uuid, self.request.user.institution) def search_hids(self, query, offset, limit): diff --git a/evidence/models.py b/evidence/models.py index 242d885..286e4d6 100644 --- a/evidence/models.py +++ b/evidence/models.py @@ -71,25 +71,37 @@ class Evidence: for xa in matches: self.doc = json.loads(xa.document.get_data()) - if not self.is_legacy(): - dmidecode_raw = self.doc["data"]["dmidecode"] - inxi_raw = self.doc["data"]["inxi"] - self.dmi = DMIParse(dmidecode_raw) - try: - self.inxi = json.loads(inxi_raw) - machine = get_inxi_key(self.inxi, 'Machine') - for m in machine: - system = get_inxi(m, "System") - if system: - self.device_manufacturer = system - self.device_model = get_inxi(m, "product") - self.device_serial_number = get_inxi(m, "serial") - self.device_chassis = get_inxi(m, "Type") - self.device_version = get_inxi(m, "v") + if self.is_legacy(): + return + if self.doc.get("credentialSubject"): + for ev in self.doc["evidence"]: + if "dmidecode" == ev.get("operation"): + dmidecode_raw = ev["output"] + if "inxi" == ev.get("operation"): + self.inxi = ev["output"] + else: + dmidecode_raw = self.doc["data"]["dmidecode"] + try: + self.inxi = json.loads(self.doc["data"]["inxi"]) except Exception: return + self.dmi = DMIParse(dmidecode_raw) + try: + machine = get_inxi_key(self.inxi, 'Machine') + for m in machine: + system = get_inxi(m, "System") + if system: + self.device_manufacturer = system + self.device_model = get_inxi(m, "product") + self.device_serial_number = get_inxi(m, "serial") + self.device_chassis = get_inxi(m, "Type") + self.device_version = get_inxi(m, "v") + + except Exception: + return + def get_time(self): if not self.doc: self.get_doc() @@ -116,7 +128,7 @@ class Evidence: if self.inxi: return self.device_manufacturer - + return self.dmi.manufacturer().strip() def get_model(self): @@ -131,13 +143,13 @@ class Evidence: if self.inxi: return self.device_model - + return self.dmi.model().strip() def get_chassis(self): if self.is_legacy(): return self.doc['device']['model'] - + if self.inxi: return self.device_chassis @@ -152,7 +164,7 @@ class Evidence: def get_serial_number(self): if self.is_legacy(): return self.doc['device']['serialNumber'] - + if self.inxi: return self.device_serial_number @@ -178,6 +190,9 @@ class Evidence: self.components = snapshot['components'] def is_legacy(self): + if self.doc.get("credentialSubject"): + return False + return self.doc.get("software") != "workbench-script" def is_web_snapshot(self): diff --git a/evidence/parse.py b/evidence/parse.py index 9a8ec2a..304d1d3 100644 --- a/evidence/parse.py +++ b/evidence/parse.py @@ -25,6 +25,7 @@ class Build: def __init__(self, evidence_json, user, check=False): self.evidence = evidence_json.copy() self.json = evidence_json.copy() + if evidence_json.get("credentialSubject"): self.json.update(evidence_json["credentialSubject"]) if evidence_json.get("evidence"): @@ -94,7 +95,9 @@ class Build: def get_hid(self, snapshot): try: - self.inxi = json.loads(self.json["data"]["inxi"]) + self.inxi = self.json["data"]["inxi"] + if isinstance(self.inxi, str): + self.inxi = json.loads(self.inxi) except Exception: logger.error("No inxi in snapshot %s", self.uuid) return "" diff --git a/evidence/parse_details.py b/evidence/parse_details.py index 35adfc5..7ce0a5b 100644 --- a/evidence/parse_details.py +++ b/evidence/parse_details.py @@ -30,9 +30,20 @@ def get_inxi(n, name): class ParseSnapshot: def __init__(self, snapshot, default="n/a"): self.default = default - self.dmidecode_raw = snapshot["data"].get("dmidecode", "{}") - self.smart_raw = snapshot["data"].get("disks", []) - self.inxi_raw = snapshot["data"].get("inxi", "") or "" + self.dmidecode_raw = snapshot.get("data", {}).get("dmidecode", "{}") + self.smart_raw = snapshot.get("data", {}).get("smartctl", []) + self.inxi_raw = snapshot.get("data", {}).get("inxi", "") or "" + for ev in snapshot.get("evidence", []): + if "dmidecode" == ev.get("operation"): + self.dmidecode_raw = ev["output"] + if "inxi" == ev.get("operation"): + self.inxi_raw = ev["output"] + if "smartctl" == ev.get("operation"): + self.smart_raw = ev["output"] + data = snapshot + if snapshot.get("credentialSubject"): + data = snapshot["credentialSubject"] + self.device = {"actions": []} self.components = [] @@ -45,11 +56,10 @@ class ParseSnapshot: self.snapshot_json = { "type": "Snapshot", "device": self.device, - "software": snapshot["software"], + "software": data["software"], "components": self.components, - "uuid": snapshot['uuid'], - "version": snapshot['version'], - "endTime": snapshot["timestamp"], + "uuid": data['uuid'], + "endTime": data["timestamp"], "elapsed": 1, } @@ -267,7 +277,6 @@ class ParseSnapshot: hd["read used"] = get_inxi(d, "read-units") hd["written used"] = get_inxi(d, "written-units") - # import pdb; pdb.set_trace() self.components.append(hd) continue From 1dad22c3d35ab1612029c23e2ca22a8dc79e3707 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 15 Nov 2024 18:56:11 +0100 Subject: [PATCH 03/72] first base for dpp --- dhub/settings.py | 1 + dpp/__init__.py | 0 dpp/admin.py | 3 ++ dpp/api_dlt.py | 96 ++++++++++++++++++++++++++++++++++ dpp/apps.py | 6 +++ dpp/migrations/0001_initial.py | 90 +++++++++++++++++++++++++++++++ dpp/migrations/__init__.py | 0 dpp/models.py | 30 +++++++++++ dpp/tests.py | 3 ++ dpp/views.py | 3 ++ 10 files changed, 232 insertions(+) create mode 100644 dpp/__init__.py create mode 100644 dpp/admin.py create mode 100644 dpp/api_dlt.py create mode 100644 dpp/apps.py create mode 100644 dpp/migrations/0001_initial.py create mode 100644 dpp/migrations/__init__.py create mode 100644 dpp/models.py create mode 100644 dpp/tests.py create mode 100644 dpp/views.py diff --git a/dhub/settings.py b/dhub/settings.py index 0dc3d84..4b5c366 100644 --- a/dhub/settings.py +++ b/dhub/settings.py @@ -89,6 +89,7 @@ INSTALLED_APPS = [ "dashboard", "admin", "api", + "dpp", ] diff --git a/dpp/__init__.py b/dpp/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/dpp/admin.py b/dpp/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/dpp/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/dpp/api_dlt.py b/dpp/api_dlt.py new file mode 100644 index 0000000..6a1317a --- /dev/null +++ b/dpp/api_dlt.py @@ -0,0 +1,96 @@ +from ereuseapi.methods import API + + +def connect_api(): + + if not session.get('token_dlt'): + return + + token_dlt = session.get('token_dlt') + api_dlt = app.config.get('API_DLT') + + return API(api_dlt, token_dlt, "ethereum") + +def register_dlt(): + api = self.connect_api() + if not api: + return + + snapshot = [x for x in self.actions if x.t == 'Snapshot'] + if not snapshot: + return + snapshot = snapshot[0] + from ereuse_devicehub.modules.dpp.models import ALGORITHM + from ereuse_devicehub.resources.enums import StatusCode + cny_a = 1 + while cny_a: + api = self.connect_api() + result = api.register_device( + self.chid, + ALGORITHM, + snapshot.phid_dpp, + app.config.get('ID_FEDERATED') + ) + try: + assert result['Status'] == StatusCode.Success.value + assert result['Data']['data']['timestamp'] + cny_a = 0 + except Exception: + if result.get("Data") != "Device already exists": + logger.error("API return: %s", result) + time.sleep(10) + else: + cny_a = 0 + + + register_proof(result) + + if app.config.get('ID_FEDERATED'): + cny = 1 + while cny: + try: + api.add_service( + self.chid, + 'DeviceHub', + app.config.get('ID_FEDERATED'), + 'Inventory service', + 'Inv', + ) + cny = 0 + except Exception: + time.sleep(10) + +def register_proof(self, result): + from ereuse_devicehub.modules.dpp.models import PROOF_ENUM, Proof + from ereuse_devicehub.resources.enums import StatusCode + + if result['Status'] == StatusCode.Success.value: + timestamp = result.get('Data', {}).get('data', {}).get('timestamp') + + if not timestamp: + return + + snapshot = [x for x in self.actions if x.t == 'Snapshot'] + if not snapshot: + return + snapshot = snapshot[0] + + d = { + "type": PROOF_ENUM['Register'], + "device": self, + "action": snapshot, + "timestamp": timestamp, + "issuer_id": g.user.id, + "documentId": snapshot.id, + "documentSignature": snapshot.phid_dpp, + "normalizeDoc": snapshot.json_hw, + } + proof = Proof(**d) + db.session.add(proof) + + if not hasattr(self, 'components'): + return + + for c in self.components: + if isinstance(c, DataStorage): + c.register_dlt() diff --git a/dpp/apps.py b/dpp/apps.py new file mode 100644 index 0000000..758fbb0 --- /dev/null +++ b/dpp/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class DppConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "dpp" diff --git a/dpp/migrations/0001_initial.py b/dpp/migrations/0001_initial.py new file mode 100644 index 0000000..0f10686 --- /dev/null +++ b/dpp/migrations/0001_initial.py @@ -0,0 +1,90 @@ +# Generated by Django 5.0.6 on 2024-11-15 18:55 + +import django.db.models.deletion +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ("user", "0001_initial"), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name="Dpp", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("timestamp", models.IntegerField()), + ("key", models.CharField(max_length=256)), + ("uuid", models.UUIDField()), + ("signature", models.CharField(max_length=256)), + ("normalizeDoc", models.TextField()), + ("type", models.CharField(max_length=256)), + ( + "owner", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + to="user.institution", + ), + ), + ( + "user", + models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.SET_NULL, + to=settings.AUTH_USER_MODEL, + ), + ), + ], + ), + migrations.CreateModel( + name="Proof", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("timestamp", models.IntegerField()), + ("uuid", models.UUIDField()), + ("signature", models.CharField(max_length=256)), + ("normalizeDoc", models.TextField()), + ("type", models.CharField(max_length=256)), + ("action", models.CharField(max_length=256)), + ( + "owner", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + to="user.institution", + ), + ), + ( + "user", + models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.SET_NULL, + to=settings.AUTH_USER_MODEL, + ), + ), + ], + ), + ] diff --git a/dpp/migrations/__init__.py b/dpp/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/dpp/models.py b/dpp/models.py new file mode 100644 index 0000000..d46f7de --- /dev/null +++ b/dpp/models.py @@ -0,0 +1,30 @@ +from django.db import models +from user.models import User, Institution +from utils.constants import STR_EXTEND_SIZE +# Create your models here. + + +class Proof(models.Model): + timestamp = models.IntegerField() + uuid = models.UUIDField() + signature = models.CharField(max_length=STR_EXTEND_SIZE) + normalizeDoc = models.TextField() + type = models.CharField(max_length=STR_EXTEND_SIZE) + action = models.CharField(max_length=STR_EXTEND_SIZE) + owner = models.ForeignKey(Institution, on_delete=models.CASCADE) + user = models.ForeignKey( + User, on_delete=models.SET_NULL, null=True, blank=True) + + +class Dpp(models.Model): + timestamp = models.IntegerField() + key = models.CharField(max_length=STR_EXTEND_SIZE) + uuid = models.UUIDField() + signature = models.CharField(max_length=STR_EXTEND_SIZE) + normalizeDoc = models.TextField() + type = models.CharField(max_length=STR_EXTEND_SIZE) + owner = models.ForeignKey(Institution, on_delete=models.CASCADE) + user = models.ForeignKey( + User, on_delete=models.SET_NULL, null=True, blank=True) + + diff --git a/dpp/tests.py b/dpp/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/dpp/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/dpp/views.py b/dpp/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/dpp/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. From f7b2687ca205efa73bacf5147cc9d2786f5a64ee Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 18 Nov 2024 19:37:08 +0100 Subject: [PATCH 04/72] register device and dpp in dlt and dpp api --- device/templates/details.html | 19 ++++ device/views.py | 3 + dhub/urls.py | 1 + dpp/api_dlt.py | 183 +++++++++++++++++++++------------ dpp/migrations/0001_initial.py | 42 +------- dpp/models.py | 19 +--- dpp/urls.py | 8 ++ dpp/views.py | 37 ++++++- evidence/models.py | 7 ++ evidence/parse.py | 17 ++- evidence/xapian.py | 12 ++- 11 files changed, 217 insertions(+), 131 deletions(-) create mode 100644 dpp/urls.py diff --git a/device/templates/details.html b/device/templates/details.html index fb362c7..921fd4b 100644 --- a/device/templates/details.html +++ b/device/templates/details.html @@ -29,6 +29,9 @@ + @@ -236,6 +239,22 @@ {% endfor %} + +
+
{% trans 'List of dpps' %}
+
+ {% for d in dpps %} +
+
+ {{ d.timestamp }} +
+

+ {{ d.signature }} +

+
+ {% endfor %} +
+
{% endblock %} diff --git a/device/views.py b/device/views.py index 319f8cf..12fb244 100644 --- a/device/views.py +++ b/device/views.py @@ -14,6 +14,7 @@ from django.views.generic.base import TemplateView from dashboard.mixins import DashboardView, Http403 from evidence.models import Annotation from lot.models import LotTag +from dpp.models import Proof from device.models import Device from device.forms import DeviceFormSet @@ -103,10 +104,12 @@ class DetailsView(DashboardView, TemplateView): context = super().get_context_data(**kwargs) self.object.initial() lot_tags = LotTag.objects.filter(owner=self.request.user.institution) + dpps = Proof.objects.filter(uuid_in=self.object.uuids) context.update({ 'object': self.object, 'snapshot': self.object.get_last_evidence(), 'lot_tags': lot_tags, + 'dpps': dpps, }) return context diff --git a/dhub/urls.py b/dhub/urls.py index fba9bd3..c50da55 100644 --- a/dhub/urls.py +++ b/dhub/urls.py @@ -27,4 +27,5 @@ urlpatterns = [ path("user/", include("user.urls")), path("lot/", include("lot.urls")), path('api/', include('api.urls')), + path('dpp/', include('dpp.urls')), ] diff --git a/dpp/api_dlt.py b/dpp/api_dlt.py index 6a1317a..6bf95c0 100644 --- a/dpp/api_dlt.py +++ b/dpp/api_dlt.py @@ -1,38 +1,106 @@ +import time +import logging + +from django.conf import settings from ereuseapi.methods import API +from dpp.models import Proof, Dpp + + +logger = logging.getLogger('django') + + +# """The code of the status response of api dlt.""" +STATUS_CODE = { + "Success": 201, + "Notwork": 400 +} + + +ALGORITHM = "sha3-256" + + +PROOF_TYPE = { + 'Register': 'Register', + 'IssueDPP': 'IssueDPP', + 'proof_of_recycling': 'proof_of_recycling', + 'Erase': 'Erase', + 'EWaste': 'EWaste', +} + def connect_api(): - if not session.get('token_dlt'): + if not settings.get('TOKEN_DLT'): return - token_dlt = session.get('token_dlt') - api_dlt = app.config.get('API_DLT') + token_dlt = settings.get('TOKEN_DLT') + api_dlt = settings.get('API_DLT') return API(api_dlt, token_dlt, "ethereum") -def register_dlt(): - api = self.connect_api() + +def register_dlt(chid, phid, proof_type=None): + api = connect_api() if not api: return - snapshot = [x for x in self.actions if x.t == 'Snapshot'] - if not snapshot: + if proof_type: + return api.generate_proof( + chid, + ALGORITHM, + phid, + proof_type, + settings.get('ID_FEDERATED') + ) + + return api.register_device( + chid, + ALGORITHM, + phid, + settings.get('ID_FEDERATED') + ) + + +def issuer_dpp_dlt(dpp): + phid = dpp.split(":")[0] + api = connect_api() + if not api: return - snapshot = snapshot[0] - from ereuse_devicehub.modules.dpp.models import ALGORITHM - from ereuse_devicehub.resources.enums import StatusCode + + return api.issue_passport( + dpp, + ALGORITHM, + phid, + settings.get('ID_FEDERATED') + ) + + + +def save_proof(signature, ev_uuid, result, proof_type, user): + if result['Status'] == STATUS_CODE.get("Success"): + timestamp = result.get('Data', {}).get('data', {}).get('timestamp') + + if not timestamp: + return + + d = { + "type": proof_type, + "timestamp": timestamp, + "issuer": user.institution.id, + "user": user, + "uuid": ev_uuid, + "signature": signature, + } + Proof.objects.create(**d) + + +def register_device_dlt(chid, phid, ev_uuid, user): cny_a = 1 while cny_a: - api = self.connect_api() - result = api.register_device( - self.chid, - ALGORITHM, - snapshot.phid_dpp, - app.config.get('ID_FEDERATED') - ) + result = register_dlt(chid, phid) try: - assert result['Status'] == StatusCode.Success.value + assert result['Status'] == STATUS_CODE.get("Success") assert result['Data']['data']['timestamp'] cny_a = 0 except Exception: @@ -42,55 +110,42 @@ def register_dlt(): else: cny_a = 0 + save_proof(phid, ev_uuid, result, PROOF_TYPE['Register'], user) - register_proof(result) - if app.config.get('ID_FEDERATED'): - cny = 1 - while cny: - try: - api.add_service( - self.chid, - 'DeviceHub', - app.config.get('ID_FEDERATED'), - 'Inventory service', - 'Inv', - ) - cny = 0 - except Exception: - time.sleep(10) + # TODO is neccesary? + # if settings.get('ID_FEDERATED'): + # cny = 1 + # while cny: + # try: + # api.add_service( + # chid, + # 'DeviceHub', + # settings.get('ID_FEDERATED'), + # 'Inventory service', + # 'Inv', + # ) + # cny = 0 + # except Exception: + # time.sleep(10) -def register_proof(self, result): - from ereuse_devicehub.modules.dpp.models import PROOF_ENUM, Proof - from ereuse_devicehub.resources.enums import StatusCode - if result['Status'] == StatusCode.Success.value: - timestamp = result.get('Data', {}).get('data', {}).get('timestamp') - - if not timestamp: - return - - snapshot = [x for x in self.actions if x.t == 'Snapshot'] - if not snapshot: - return - snapshot = snapshot[0] - - d = { - "type": PROOF_ENUM['Register'], - "device": self, - "action": snapshot, - "timestamp": timestamp, - "issuer_id": g.user.id, - "documentId": snapshot.id, - "documentSignature": snapshot.phid_dpp, - "normalizeDoc": snapshot.json_hw, - } - proof = Proof(**d) - db.session.add(proof) - - if not hasattr(self, 'components'): +def register_passport_dlt(chid, phid, ev_uuid, user): + dpp = "{chid}:{phid}".format(chid=chid, phid=phid) + if Proof.objects.filter(signature=dpp, type=PROOF_TYPE['IssueDPP']).exists(): return - for c in self.components: - if isinstance(c, DataStorage): - c.register_dlt() + cny_a = 1 + while cny_a: + try: + result = issuer_dpp_dlt(dpp) + cny_a = 0 + except Exception as err: + logger.error("ERROR API issue passport return: %s", err) + time.sleep(10) + + if result['Status'] is not STATUS_CODE.get("Success"): + logger.error("ERROR API issue passport return: %s", result) + return + + save_proof(phid, ev_uuid, result, PROOF_TYPE['IssueDPP'], user) diff --git a/dpp/migrations/0001_initial.py b/dpp/migrations/0001_initial.py index 0f10686..71e1b75 100644 --- a/dpp/migrations/0001_initial.py +++ b/dpp/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 5.0.6 on 2024-11-15 18:55 +# Generated by Django 5.0.6 on 2024-11-18 14:29 import django.db.models.deletion from django.conf import settings @@ -15,42 +15,6 @@ class Migration(migrations.Migration): ] operations = [ - migrations.CreateModel( - name="Dpp", - fields=[ - ( - "id", - models.BigAutoField( - auto_created=True, - primary_key=True, - serialize=False, - verbose_name="ID", - ), - ), - ("timestamp", models.IntegerField()), - ("key", models.CharField(max_length=256)), - ("uuid", models.UUIDField()), - ("signature", models.CharField(max_length=256)), - ("normalizeDoc", models.TextField()), - ("type", models.CharField(max_length=256)), - ( - "owner", - models.ForeignKey( - on_delete=django.db.models.deletion.CASCADE, - to="user.institution", - ), - ), - ( - "user", - models.ForeignKey( - blank=True, - null=True, - on_delete=django.db.models.deletion.SET_NULL, - to=settings.AUTH_USER_MODEL, - ), - ), - ], - ), migrations.CreateModel( name="Proof", fields=[ @@ -66,11 +30,9 @@ class Migration(migrations.Migration): ("timestamp", models.IntegerField()), ("uuid", models.UUIDField()), ("signature", models.CharField(max_length=256)), - ("normalizeDoc", models.TextField()), ("type", models.CharField(max_length=256)), - ("action", models.CharField(max_length=256)), ( - "owner", + "issuer", models.ForeignKey( on_delete=django.db.models.deletion.CASCADE, to="user.institution", diff --git a/dpp/models.py b/dpp/models.py index d46f7de..5502bcf 100644 --- a/dpp/models.py +++ b/dpp/models.py @@ -5,26 +5,11 @@ from utils.constants import STR_EXTEND_SIZE class Proof(models.Model): + ## The signature can be a phid or dpp depending of type of Proof timestamp = models.IntegerField() uuid = models.UUIDField() signature = models.CharField(max_length=STR_EXTEND_SIZE) - normalizeDoc = models.TextField() type = models.CharField(max_length=STR_EXTEND_SIZE) - action = models.CharField(max_length=STR_EXTEND_SIZE) - owner = models.ForeignKey(Institution, on_delete=models.CASCADE) + issuer = models.ForeignKey(Institution, on_delete=models.CASCADE) user = models.ForeignKey( User, on_delete=models.SET_NULL, null=True, blank=True) - - -class Dpp(models.Model): - timestamp = models.IntegerField() - key = models.CharField(max_length=STR_EXTEND_SIZE) - uuid = models.UUIDField() - signature = models.CharField(max_length=STR_EXTEND_SIZE) - normalizeDoc = models.TextField() - type = models.CharField(max_length=STR_EXTEND_SIZE) - owner = models.ForeignKey(Institution, on_delete=models.CASCADE) - user = models.ForeignKey( - User, on_delete=models.SET_NULL, null=True, blank=True) - - diff --git a/dpp/urls.py b/dpp/urls.py new file mode 100644 index 0000000..9ada14e --- /dev/null +++ b/dpp/urls.py @@ -0,0 +1,8 @@ +from django.urls import path +from dpp import views + +app_name = 'dpp' + +urlpatterns = [ + path("/", views.LotDashboardView.as_view(), name="proof"), +] diff --git a/dpp/views.py b/dpp/views.py index 91ea44a..956099d 100644 --- a/dpp/views.py +++ b/dpp/views.py @@ -1,3 +1,36 @@ -from django.shortcuts import render +from django.views.generic.edit import View +from django.http import JsonResponse -# Create your views here. +from evidence.xapian import search +from dpp.models import Proof +from dpp.api_dlt import ALGORITHM + + +class ProofView(View): + + def get(self, request, *args, **kwargs): + timestamp = kwargs.get("proof_id") + proof = Proof.objects.filter(timestamp=timestamp).first() + if not proof: + return JsonResponse({}, status=404) + + ev_uuid = 'uuid:"{}"'.format(proof.uuid) + matches = search(None, ev_uuid, limit=1) + if not matches or matches.size() < 1: + return JsonResponse({}, status=404) + + for x in matches: + snap = x.document.get_data() + + data = { + "algorithm": ALGORITHM, + "document": snap + } + + d = { + '@context': ['https://ereuse.org/proof0.json'], + 'data': data, + } + return JsonResponse(d, status=200) + + return JsonResponse({}, status=404) diff --git a/evidence/models.py b/evidence/models.py index 286e4d6..b410795 100644 --- a/evidence/models.py +++ b/evidence/models.py @@ -1,4 +1,5 @@ import json +import hashlib from dmidecode import DMIParse from django.db import models @@ -59,6 +60,12 @@ class Evidence: if a: self.owner = a.owner + def get_phid(self): + if not self.doc: + self.get_doc() + + return hashlib.sha3_256(json.dumps(self.doc)).hexdigest() + def get_doc(self): self.doc = {} if not self.owner: diff --git a/evidence/parse.py b/evidence/parse.py index 304d1d3..ccee6a2 100644 --- a/evidence/parse.py +++ b/evidence/parse.py @@ -2,11 +2,9 @@ import json import hashlib import logging -from dmidecode import DMIParse - from evidence.models import Annotation from evidence.xapian import index -from utils.constants import CHASSIS_DH +from dpp.api_dlt import register_device_dlt, register_passport_dlt from evidence.parse_details import get_inxi_key, get_inxi logger = logging.getLogger('django') @@ -39,6 +37,8 @@ class Build: self.uuid = self.json['uuid'] self.user = user self.hid = None + self.chid = None + self.phid = self.get_signature(self.json) self.generate_chids() if check: @@ -46,6 +46,7 @@ class Build: self.index() self.create_annotations() + self.register_device_dlt() def index(self): snap = json.dumps(self.evidence) @@ -69,7 +70,8 @@ class Build: hid = f"{manufacturer}{model}{chassis}{serial_number}{sku}" - return hashlib.sha3_256(hid.encode()).hexdigest() + self.chid = hashlib.sha3_256(hid.encode()).hexdigest() + return self.chid def create_annotations(self): annotation = Annotation.objects.filter( @@ -120,3 +122,10 @@ class Build: return f"{manufacturer}{model}{chassis}{serial_number}{sku}" return f"{manufacturer}{model}{chassis}{serial_number}{sku}{mac}" + + 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) diff --git a/evidence/xapian.py b/evidence/xapian.py index 98da706..2e1cd9f 100644 --- a/evidence/xapian.py +++ b/evidence/xapian.py @@ -22,10 +22,14 @@ def search(institution, qs, offset=0, limit=10): qp.set_stemming_strategy(xapian.QueryParser.STEM_SOME) qp.add_prefix("uuid", "uuid") query = qp.parse_query(qs) - institution_term = "U{}".format(institution.id) - final_query = xapian.Query( - xapian.Query.OP_AND, query, xapian.Query(institution_term) - ) + if institution: + institution_term = "U{}".format(institution.id) + final_query = xapian.Query( + xapian.Query.OP_AND, query, xapian.Query(institution_term) + ) + else: + final_query = xapian.Query(query) + enquire = xapian.Enquire(database) enquire.set_query(final_query) matches = enquire.get_mset(offset, limit) From 271ac83d71e9dff4bae89010c6fef6e927f49cee Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 19 Nov 2024 18:02:43 +0100 Subject: [PATCH 05/72] . --- device/templates/details.html | 4 ++-- device/views.py | 2 +- dhub/settings.py | 5 +++++ dpp/api_dlt.py | 24 +++++++++++++++++------- dpp/urls.py | 2 +- requirements.txt | 3 +++ 6 files changed, 29 insertions(+), 11 deletions(-) diff --git a/device/templates/details.html b/device/templates/details.html index 921fd4b..86ea5a1 100644 --- a/device/templates/details.html +++ b/device/templates/details.html @@ -30,7 +30,7 @@ {% trans 'Evidences' %} + {% if dpps %} + {% endif %} @@ -239,7 +241,8 @@ {% endfor %} - + + {% if dpps %}
{% trans 'List of dpps' %}
@@ -256,6 +259,7 @@ {% endfor %}
+ {% endif %} {% endblock %} @@ -264,12 +268,12 @@ document.addEventListener('DOMContentLoaded', function () { // Obtener el hash de la URL (ejemplo: #components) const hash = window.location.hash - + // Verificar si hay un hash en la URL if (hash) { // Buscar el botón o enlace que corresponde al hash y activarlo const tabTrigger = document.querySelector(`[data-bs-target="${hash}"]`) - + if (tabTrigger) { // Crear una instancia de tab de Bootstrap para activar el tab const tab = new bootstrap.Tab(tabTrigger) diff --git a/device/views.py b/device/views.py index c7c2b1f..3baa054 100644 --- a/device/views.py +++ b/device/views.py @@ -1,6 +1,5 @@ from django.http import JsonResponse - -from django.http import Http404 +from django.conf import settings from django.urls import reverse_lazy from django.shortcuts import get_object_or_404, Http404 from django.utils.translation import gettext_lazy as _ @@ -13,10 +12,11 @@ from django.views.generic.base import TemplateView from dashboard.mixins import DashboardView, Http403 from evidence.models import Annotation from lot.models import LotTag -from dpp.models import Proof -from dpp.api_dlt import PROOF_TYPE from device.models import Device from device.forms import DeviceFormSet +if settings.DPP: + from dpp.models import Proof + from dpp.api_dlt import PROOF_TYPE class NewDeviceView(DashboardView, FormView): @@ -104,10 +104,12 @@ class DetailsView(DashboardView, TemplateView): context = super().get_context_data(**kwargs) self.object.initial() lot_tags = LotTag.objects.filter(owner=self.request.user.institution) - dpps = Proof.objects.filter( - uuid__in=self.object.uuids, - type=PROOF_TYPE["IssueDPP"] - ) + dpps = [] + if settings.DPP: + dpps = Proof.objects.filter( + uuid__in=self.object.uuids, + type=PROOF_TYPE["IssueDPP"] + ) context.update({ 'object': self.object, 'snapshot': self.object.get_last_evidence(), diff --git a/dhub/settings.py b/dhub/settings.py index a30ab2b..2aca54e 100644 --- a/dhub/settings.py +++ b/dhub/settings.py @@ -89,10 +89,13 @@ INSTALLED_APPS = [ "dashboard", "admin", "api", - "dpp", - "did", ] +DPP = config("DPP", default=False, cast=bool) + +if DPP: + INSTALLED_APPS.extend(["dpp", "did"]) + MIDDLEWARE = [ "django.middleware.security.SecurityMiddleware", diff --git a/dhub/urls.py b/dhub/urls.py index d070a15..0b77144 100644 --- a/dhub/urls.py +++ b/dhub/urls.py @@ -14,7 +14,7 @@ Including another URLconf 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ - +from django.conf import settings from django.urls import path, include urlpatterns = [ @@ -27,6 +27,10 @@ urlpatterns = [ path("user/", include("user.urls")), path("lot/", include("lot.urls")), path('api/', include('api.urls')), - path('dpp/', include('dpp.urls')), - path('did/', include('did.urls')), ] + +if settings.DPP: + urlpatterns.extend([ + path('dpp/', include('dpp.urls')), + path('did/', include('did.urls')), + ]) diff --git a/evidence/parse.py b/evidence/parse.py index 31b12af..2f67df6 100644 --- a/evidence/parse.py +++ b/evidence/parse.py @@ -9,6 +9,11 @@ from evidence.models import Annotation from evidence.xapian import index from dpp.api_dlt import register_device_dlt, register_passport_dlt from evidence.parse_details import get_inxi_key, get_inxi +from django.conf import settings + +if settings.DPP: + from dpp.api_dlt import register_device_dlt, register_passport_dlt + logger = logging.getLogger('django') @@ -49,7 +54,8 @@ class Build: self.index() self.create_annotations() - self.register_device_dlt() + if settings.DPP: + self.register_device_dlt() def index(self): snap = json.dumps(self.evidence) @@ -152,26 +158,6 @@ class Build: value=v ) -<<<<<<< HEAD -======= - def get_chassis_dh(self): - chassis = self.get_chassis() - lower_type = chassis.lower() - for k, v in CHASSIS_DH.items(): - if lower_type in v: - return k - return self.default - - def get_sku(self): - return self.dmi.get("System")[0].get("SKU Number", "n/a").strip() - - def get_chassis(self): - return self.dmi.get("Chassis")[0].get("Type", '_virtual') # - - def get_version(self): - return self.dmi.get("System")[0].get("Verson", '_virtual') - ->>>>>>> 5949049 (new document and out device and components) def get_hid(self, snapshot): try: self.inxi = self.json["data"]["inxi"] diff --git a/evidence/views.py b/evidence/views.py index dabcd9d..c2868b2 100644 --- a/evidence/views.py +++ b/evidence/views.py @@ -137,7 +137,7 @@ class DownloadEvidenceView(DashboardView, TemplateView): evidence.get_doc() data = json.dumps(evidence.doc) response = HttpResponse(data, content_type="application/json") - response['Content-Disposition'] = 'attachment; filename={}'.format("credential.json") + response['Content-Disposition'] = 'attachment; filename={}'.format("evidence.json") return response From 07c25f4a92c02bea891b8cfb4a069cd8c1e0267a Mon Sep 17 00:00:00 2001 From: pedro Date: Wed, 11 Dec 2024 11:16:32 +0100 Subject: [PATCH 68/72] propagate DPP env var to docker --- .env.example | 1 + docker-compose.yml | 1 + 2 files changed, 2 insertions(+) diff --git a/.env.example b/.env.example index ed60e95..d8fed15 100644 --- a/.env.example +++ b/.env.example @@ -3,6 +3,7 @@ DEMO=true # note that with DEBUG=true, logs are more verbose (include tracebacks) DEBUG=true ALLOWED_HOSTS=localhost,localhost:8000,127.0.0.1, +DPP=false STATIC_ROOT=/tmp/static/ MEDIA_ROOT=/tmp/media/ diff --git a/docker-compose.yml b/docker-compose.yml index 9b82d7d..9a156c7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,6 +9,7 @@ services: - ALLOWED_HOSTS=${ALLOWED_HOSTS:-$DOMAIN} - DEMO=${DEMO:-false} - PREDEFINED_TOKEN=${PREDEFINED_TOKEN:-} + - DPP=${DPP:-false} volumes: - .:/opt/devicehub-django ports: From d429485651def082cd1023c7d8c0a344b9bffad0 Mon Sep 17 00:00:00 2001 From: pedro Date: Wed, 11 Dec 2024 11:23:35 +0100 Subject: [PATCH 69/72] docker entrypoint: adapt it to DPP env var --- docker/devicehub-django.entrypoint.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docker/devicehub-django.entrypoint.sh b/docker/devicehub-django.entrypoint.sh index de19a4d..73a0f19 100644 --- a/docker/devicehub-django.entrypoint.sh +++ b/docker/devicehub-django.entrypoint.sh @@ -34,7 +34,7 @@ gen_env_vars() { ADMIN='True' PREDEFINED_TOKEN="${PREDEFINED_TOKEN:-}" # specific dpp env vars - if [ "${DPP_MODULE}" = 'y' ]; then + if [ "${DPP}" = 'true' ]; then # fill env vars in this docker entrypoint wait_for_dpp_shared export API_DLT='http://api_connector:3010' @@ -129,11 +129,12 @@ config_phase() { # TODO: one error on add_user, and you don't add user anymore ./manage.py add_user "${INIT_ORG}" "${INIT_USER}" "${INIT_PASSWD}" "${ADMIN}" "${PREDEFINED_TOKEN}" - if [ "${DPP_MODULE}" = 'y' ]; then + if [ "${DPP}" = 'true' ]; then # 12, 13, 14 config_dpp_part1 # cleanup other spnapshots and copy dlt/dpp snapshots + # TODO make this better rm example/snapshots/* cp example/dpp-snapshots/*.json example/snapshots/ fi From 85bae67189479b5323a3a08a81c86d21f35b8a77 Mon Sep 17 00:00:00 2001 From: pedro Date: Wed, 11 Dec 2024 11:25:35 +0100 Subject: [PATCH 70/72] docker entrypoint: bugfix when DPP env var unbound --- docker/devicehub-django.entrypoint.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker/devicehub-django.entrypoint.sh b/docker/devicehub-django.entrypoint.sh index 73a0f19..d6d7bb2 100644 --- a/docker/devicehub-django.entrypoint.sh +++ b/docker/devicehub-django.entrypoint.sh @@ -34,7 +34,7 @@ gen_env_vars() { ADMIN='True' PREDEFINED_TOKEN="${PREDEFINED_TOKEN:-}" # specific dpp env vars - if [ "${DPP}" = 'true' ]; then + if [ "${DPP:-}" = 'true' ]; then # fill env vars in this docker entrypoint wait_for_dpp_shared export API_DLT='http://api_connector:3010' @@ -129,7 +129,7 @@ config_phase() { # TODO: one error on add_user, and you don't add user anymore ./manage.py add_user "${INIT_ORG}" "${INIT_USER}" "${INIT_PASSWD}" "${ADMIN}" "${PREDEFINED_TOKEN}" - if [ "${DPP}" = 'true' ]; then + if [ "${DPP:-}" = 'true' ]; then # 12, 13, 14 config_dpp_part1 From d1abb206e893372b7bdcbb12c8139da70efe83fb Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 15 Nov 2024 12:47:08 +0100 Subject: [PATCH 71/72] add inxi in parsing and show in details of devs --- evidence/models.py | 35 +++++++++++++++++------------------ evidence/parse.py | 1 - 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/evidence/models.py b/evidence/models.py index b410795..6b6069c 100644 --- a/evidence/models.py +++ b/evidence/models.py @@ -63,7 +63,7 @@ class Evidence: def get_phid(self): if not self.doc: self.get_doc() - + return hashlib.sha3_256(json.dumps(self.doc)).hexdigest() def get_doc(self): @@ -89,26 +89,23 @@ class Evidence: self.inxi = ev["output"] else: dmidecode_raw = self.doc["data"]["dmidecode"] + inxi_raw = self.doc["data"]["inxi"] + self.dmi = DMIParse(dmidecode_raw) try: - self.inxi = json.loads(self.doc["data"]["inxi"]) + self.inxi = json.loads(inxi_raw) + machine = get_inxi_key(self.inxi, 'Machine') + for m in machine: + system = get_inxi(m, "System") + if system: + self.device_manufacturer = system + self.device_model = get_inxi(m, "product") + self.device_serial_number = get_inxi(m, "serial") + self.device_chassis = get_inxi(m, "Type") + self.device_version = get_inxi(m, "v") + except Exception: return - self.dmi = DMIParse(dmidecode_raw) - try: - machine = get_inxi_key(self.inxi, 'Machine') - for m in machine: - system = get_inxi(m, "System") - if system: - self.device_manufacturer = system - self.device_model = get_inxi(m, "product") - self.device_serial_number = get_inxi(m, "serial") - self.device_chassis = get_inxi(m, "Type") - self.device_version = get_inxi(m, "v") - - except Exception: - return - def get_time(self): if not self.doc: self.get_doc() @@ -160,6 +157,9 @@ class Evidence: if self.inxi: return self.device_chassis + if self.inxi: + return self.device_chassis + chassis = self.dmi.get("Chassis")[0].get("Type", '_virtual') lower_type = chassis.lower() @@ -177,7 +177,6 @@ class Evidence: return self.dmi.serial_number().strip() - def get_version(self): if self.inxi: return self.device_version diff --git a/evidence/parse.py b/evidence/parse.py index 2f67df6..8e52a09 100644 --- a/evidence/parse.py +++ b/evidence/parse.py @@ -14,7 +14,6 @@ from django.conf import settings if settings.DPP: from dpp.api_dlt import register_device_dlt, register_passport_dlt - logger = logging.getLogger('django') From 5d190d07a37afdf82f4f7e3cb3fcd6a4cca624ff Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 12 Dec 2024 17:11:05 +0100 Subject: [PATCH 72/72] fix rebase from main --- evidence/forms.py | 12 ++++++------ evidence/models.py | 11 +++++++---- evidence/parse.py | 16 ++-------------- 3 files changed, 15 insertions(+), 24 deletions(-) diff --git a/evidence/forms.py b/evidence/forms.py index 83d9e6b..8be2887 100644 --- a/evidence/forms.py +++ b/evidence/forms.py @@ -29,17 +29,17 @@ class UploadForm(forms.Form): try: file_json = json.loads(file_data) - Build(file_json, None, check=True) + snap = Build(file_json, None, check=True) exist_annotation = Annotation.objects.filter( - uuid=file_json['uuid'] + uuid=snap.uuid ).first() - + if exist_annotation: - raise ValidationError( + raise ValidationError( _("The snapshot already exists"), code="duplicate_snapshot", ) - + #Catch any error and display it as Validation Error so the Form handles it except Exception as e: raise ValidationError( @@ -221,7 +221,7 @@ class EraseServerForm(forms.Form): if self.instance: return - + Annotation.objects.create( uuid=self.uuid, type=Annotation.Type.ERASE_SERVER, diff --git a/evidence/models.py b/evidence/models.py index 6b6069c..a10de01 100644 --- a/evidence/models.py +++ b/evidence/models.py @@ -68,8 +68,11 @@ class Evidence: def get_doc(self): self.doc = {} + self.inxi = None + if not self.owner: self.get_owner() + qry = 'uuid:"{}"'.format(self.uuid) matches = search(self.owner, qry, limit=1) if matches and matches.size() < 0: @@ -93,6 +96,10 @@ class Evidence: self.dmi = DMIParse(dmidecode_raw) try: self.inxi = json.loads(inxi_raw) + except Exception: + pass + if self.inxi: + try: machine = get_inxi_key(self.inxi, 'Machine') for m in machine: system = get_inxi(m, "System") @@ -102,7 +109,6 @@ class Evidence: self.device_serial_number = get_inxi(m, "serial") self.device_chassis = get_inxi(m, "Type") self.device_version = get_inxi(m, "v") - except Exception: return @@ -157,9 +163,6 @@ class Evidence: if self.inxi: return self.device_chassis - if self.inxi: - return self.device_chassis - chassis = self.dmi.get("Chassis")[0].get("Type", '_virtual') lower_type = chassis.lower() diff --git a/evidence/parse.py b/evidence/parse.py index 8e52a09..db2d32d 100644 --- a/evidence/parse.py +++ b/evidence/parse.py @@ -7,7 +7,6 @@ from evidence.parse_details import ParseSnapshot from evidence.models import Annotation from evidence.xapian import index -from dpp.api_dlt import register_device_dlt, register_passport_dlt from evidence.parse_details import get_inxi_key, get_inxi from django.conf import settings @@ -78,27 +77,16 @@ class Build: sku = device.get("sku", '') hid = f"{manufacturer}{model}{chassis}{serial_number}{sku}" - self.chid = hashlib.sha3_256(hid.encode()).hexdigest() 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}" + device = ParseSnapshot(self.json).device else: device = self.json['device'] - hid = self.get_id_hw_dpp(device) + hid = self.get_id_hw_dpp(device) self.chid = hashlib.sha3_256(hid.encode("utf-8")).hexdigest() return self.chid