Compare commits

..

13 commits

Author SHA1 Message Date
pedro b7d7b9041d Merge pull request 'Parsing error handling and MAC retrieval on new snapshots' (#27) from pr_25 into main
Reviewed-on: #27
2024-11-12 13:57:04 +00:00
pedro 65788b36af up_snapshots: simplify and clarify errors
specially, do not halt in case of errors, just print them in logs
2024-11-12 14:48:35 +01:00
pedro a748b900ca recover old up_snapshot functionality
- We don't need the if settings.DEBUG anymore, we have a logger
configuration that when DEBUG is there, a trace is always generated
- restore old error message that is shorter and clearer than the new one
2024-11-12 14:48:35 +01:00
Thomas Nahuel Rusiecki ac48a77a2f added success messages 2024-11-12 14:48:35 +01:00
Thomas Nahuel Rusiecki ac70a4ae25 catching pandas exception 2024-11-12 14:48:35 +01:00
Thomas Nahuel Rusiecki 8dade92f7e changed form validation onto field 2024-11-12 14:48:35 +01:00
Thomas Nahuel Rusiecki b4c18535fb grammar fix and todo added 2024-11-12 14:48:35 +01:00
Thomas Nahuel Rusiecki 90f057288d upload form now displays error messages once 2024-11-12 14:48:35 +01:00
Thomas Nahuel Rusiecki 80083caf99 better error messages 2024-11-12 14:48:35 +01:00
Thomas Nahuel Rusiecki 3763124440 minor fix in mac parsing 2024-11-12 14:48:35 +01:00
Thomas Nahuel Rusiecki cd0a8217c8 fixed mac not recovered properly 2024-11-12 14:48:35 +01:00
Thomas Nahuel Rusiecki 83dd044325 better error handling and logging 2024-11-12 14:48:35 +01:00
Thomas Nahuel Rusiecki ccb0be1b4c check for values on websnapshot 2024-11-12 14:48:35 +01:00
6 changed files with 58 additions and 47 deletions

View file

@ -58,7 +58,7 @@
<div class="col-lg-9 col-md-8">{{ object.type }}</div>
</div>
{% if object.is_websnapshot %}
{% if object.is_websnapshot and object.last_user_evidence %}
{% for k, v in object.last_user_evidence %}
<div class="row mb-3">
<div class="col-lg-3 col-md-4 label">{{ k }}</div>

View file

@ -15,7 +15,7 @@ from utils.save_snapshots import move_json, save_in_disk
class UploadForm(forms.Form):
evidence_file = MultipleFileField(label=_("File"))
def clean(self):
def clean_evidence_file(self):
self.evidences = []
data = self.cleaned_data.get('evidence_file')
if not data:
@ -33,13 +33,20 @@ class UploadForm(forms.Form):
exist_annotation = Annotation.objects.filter(
uuid=file_json['uuid']
).first()
if exist_annotation:
raise ValidationError("error: {} exist".format(file_name))
except Exception:
raise ValidationError("error in: {}".format(file_name))
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(
_("Error on '%(file_name)s': %(error)s"),
code="error",
params={"file_name": file_name, "error": getattr(e, 'message', str(e))},
)
self.evidences.append((file_name, file_json))
return True
@ -123,7 +130,15 @@ class ImportForm(forms.Form):
data = self.cleaned_data["file_import"]
self.file_name = data.name
df = pd.read_excel(data)
try:
df = pd.read_excel(data)
except Exception as e:
raise ValidationError(
_("Error on '%(file_name)s': Invalid File"),
params={"file_name": self.file_name}
)
df.fillna('', inplace=True)
data_pd = df.to_dict(orient='index')

View file

@ -47,17 +47,22 @@ class Command(BaseCommand):
self.open(filepath)
def open(self, filepath):
with open(filepath, 'r') as file:
content = json.loads(file.read())
path_name = save_in_disk(content, self.user.institution.name)
self.snapshots.append((content, path_name))
try:
with open(filepath, 'r') as file:
content = json.loads(file.read())
path_name = save_in_disk(content, self.user.institution.name)
self.snapshots.append((content, path_name))
except Exception as e:
logger.error("Could not open file %s: %s", filepath, e)
def parsing(self):
for s, p in self.snapshots:
try:
self.devices.append(Build(s, self.user))
move_json(p, self.user.institution.name)
except Exception as err:
except Exception as e:
snapshot_id = s.get("uuid", "")
txt = "Could not parse snapshot: %s"
logger.error(txt, snapshot_id)
txt = "Could not parse snapshot %s: %s"
logger.error(txt, snapshot_id, e)

View file

@ -4,6 +4,7 @@ import logging
from dmidecode import DMIParse
from json_repair import repair_json
from evidence.parse_details import get_lshw_child
from evidence.models import Annotation
from evidence.xapian import index
@ -12,16 +13,7 @@ from utils.constants import CHASSIS_DH
logger = logging.getLogger('django')
def get_network_cards(child, nets):
if child['id'] == 'network' and "PCI:" in child.get("businfo"):
nets.append(child)
if child.get('children'):
[get_network_cards(x, nets) for x in child['children']]
def get_mac(lshw):
nets = []
try:
if type(lshw) is dict:
hw = lshw
@ -30,18 +22,16 @@ def get_mac(lshw):
except json.decoder.JSONDecodeError:
hw = json.loads(repair_json(lshw))
try:
get_network_cards(hw, nets)
except Exception as ss:
logger.warning("%s", ss)
return
nets = []
get_lshw_child(hw, nets, 'network')
nets_sorted = sorted(nets, key=lambda x: x['businfo'])
# This funcion get the network card integrated in motherboard
# integrate = [x for x in nets if "pci@0000:00:" in x.get('businfo', '')]
if nets_sorted:
return nets_sorted[0]['serial']
mac = nets_sorted[0]['serial']
logger.debug("The snapshot has the following MAC: %s" , mac)
return mac
class Build:

View file

@ -8,23 +8,21 @@
</div>
</div>
<!-- override invalid-feedback class -->
<style>
.invalid-feedback {
color: #670000;
font-size: 1rem;
}
</style>
{% load django_bootstrap5 %}
<form role="form" method="post" enctype="multipart/form-data">
{% csrf_token %}
{% if form.errors %}
<div class="alert alert-danger alert-icon alert-icon-border alert-dismissible" role="alert">
<div class="icon"><span class="mdi mdi-close-circle-o"></span></div>
<div class="message">
{% for field, error in form.errors.items %}
{{ error }}<br />
{% endfor %}
<button class="btn-close" type="button" data-dismiss="alert" aria-label="Close"></button>
</div>
</div>
{% endif %}
{% bootstrap_form form %}
<div class="form-actions-no-box">
<a class="btn btn-grey" href="{% url 'dashboard:unassigned_devices' %}">{% translate "Cancel" %}</a>
{% bootstrap_form form alert_error_type="none" error_css_class="alert alert-danger alert-icon alert-icon-border" %}
<div class="form-actions-no-box">
<a class="btn btn-grey" href="{% url 'dashboard:unassigned_devices' %}">{% translate "Cancel" %}</a>
<input class="btn btn-green-admin" type="submit" name="submit" value="{% translate 'Save' %}" />
</div>

View file

@ -1,5 +1,6 @@
import json
from django.contrib import messages
from urllib.parse import urlparse
from django.http import HttpResponse
from django.utils.translation import gettext_lazy as _
@ -47,6 +48,7 @@ class UploadView(DashboardView, FormView):
def form_valid(self, form):
form.save(self.request.user)
messages.success(self.request, _("Evidence uploaded successfully."))
response = super().form_valid(form)
return response
@ -70,6 +72,7 @@ class ImportView(DashboardView, FormView):
def form_valid(self, form):
form.save()
messages.success(self.request, _("Evidence imported successfully."))
response = super().form_valid(form)
return response