From 7ebff00e2eea455b66423d88357e9e1d7bf75656 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 31 Oct 2024 16:30:45 +0100 Subject: [PATCH] issue 100 new url in qr for evidence instead of device --- workbench-script.py | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/workbench-script.py b/workbench-script.py index 380dbab..04667ec 100644 --- a/workbench-script.py +++ b/workbench-script.py @@ -6,6 +6,7 @@ import uuid import hashlib import argparse import configparser +import urllib.parse import urllib.request import gettext @@ -16,6 +17,7 @@ from datetime import datetime ## Legacy Functions ## + def convert_to_legacy_snapshot(snapshot): snapshot["sid"] = str(uuid.uuid4()).split("-")[0] snapshot["software"] = "workbench-script" @@ -25,9 +27,8 @@ def convert_to_legacy_snapshot(snapshot): snapshot["timestamp"] = snapshot["timestamp"].replace(" ", "T") snapshot["data"]["smart"] = snapshot["data"]["disks"] snapshot["data"].pop("disks") - snapshot.pop("code") snapshot.pop("erase") - + ## End Legacy Functions ## @@ -54,11 +55,6 @@ def exec_cmd_erase(cmd): return '' # return os.popen(cmd).read() - -def gen_code(): - uid = str(uuid.uuid4()).encode('utf-8') - return hashlib.shake_256(uid).hexdigest(3) - ## End Utility functions ## @@ -66,7 +62,6 @@ SNAPSHOT_BASE = { 'timestamp': str(datetime.now()), 'type': 'Snapshot', 'uuid': str(uuid.uuid4()), - 'code': gen_code(), 'software': "workbench-script", 'version': "0.0.1", 'data': {}, @@ -301,6 +296,15 @@ def save_snapshot_in_disk(snapshot, path): # TODO sanitize url, if url is like this, it fails # url = 'http://127.0.0.1:8000/api/snapshot/' def send_snapshot_to_devicehub(snapshot, token, url): + url_components = urllib.parse.urlparse(url) + ev_path = "evidence/{}".format(snapshot["uuid"]) + components = (url_components.schema, url_components.netloc, ev_path, '', '', '') + ev_url = urllib.parse.urlunparse(components) + # apt install qrencode + qr = "echo {} | qrencode -t ANSI".format(ev_url) + print(exec_cmd(qr)) + print(ev_url) + headers = { "Authorization": f"Bearer {token}", "Content-Type": "application/json" @@ -310,26 +314,17 @@ def send_snapshot_to_devicehub(snapshot, token, url): request = urllib.request.Request(url, data=data, headers=headers) with urllib.request.urlopen(request) as response: status_code = response.getcode() - response_text = response.read().decode('utf-8') + #response_text = response.read().decode('utf-8') if 200 <= status_code < 300: logger.info(_("Snapshot successfully sent to '%s'"), url) - - try: - response = json.loads(response_text) - if response.get('url'): - # apt install qrencode - qr = "echo {} | qrencode -t ANSI".format(response['url']) - print(exec_cmd(qr)) - print("url: {}".format(response['url'])) - if response.get("dhid"): - print("dhid: {}".format(response['dhid'])) - except Exception: - logger.error(response_text) + else: + logger.error(_("Snapshot cannot sent to '%s'"), url) except Exception as e: logger.error(_("Snapshot not remotely sent to URL '%s'. Do you have internet? Is your server up & running? Is the url token authorized?\n %s"), url, e) + def load_config(config_file="settings.ini"): """ Tries to load configuration from a config file.