From fa72894c4c2c386a3b73bcb78c46f8d14695bb70 Mon Sep 17 00:00:00 2001 From: Martin Hermann Date: Mon, 13 Nov 2023 09:59:32 +0100 Subject: [PATCH] Change: Use function to calculate timestamps The timestamp calculation has been moved to a dedicated function. The parameter passed corresponds to seconds. --- scripts/generate-random-reports.gmp.py | 45 +++++++++++--------------- 1 file changed, 18 insertions(+), 27 deletions(-) diff --git a/scripts/generate-random-reports.gmp.py b/scripts/generate-random-reports.gmp.py index a7445c28..4e0e3dfa 100755 --- a/scripts/generate-random-reports.gmp.py +++ b/scripts/generate-random-reports.gmp.py @@ -62,6 +62,15 @@ local_date_time = datetime.now() +def calculate_timestamp(delta: int = 0) -> str: + return ( + (local_date_time + timedelta(seconds=delta)) + .astimezone() + .replace(microsecond=0) + .isoformat() + ) + + def generate_ports(n_ports): protocol = ["/tcp", "/udp"] return [str(randrange(0, 65536)) + choice(protocol) for i in range(n_ports)] @@ -166,16 +175,9 @@ def generate_result_elem( e.SubElement(own, "name").text = generate_id() elem = e.Element("modification_time") - e.SubElement(result_elem, "modification_time").text = ( - local_date_time.astimezone().replace(microsecond=0).isoformat() - ) + e.SubElement(result_elem, "modification_time").text = calculate_timestamp(0) e.SubElement(result_elem, "comment").text = "" - e.SubElement(result_elem, "creation_time").text = ( - (local_date_time + timedelta(seconds=-20)) - .astimezone() - .replace(microsecond=0) - .isoformat() - ) + e.SubElement(result_elem, "creation_time").text = calculate_timestamp(-20) host_elem = e.Element("host") host_elem.text = host_ip @@ -259,18 +261,8 @@ def generate_host_elem( e.SubElement(host_elem, "ip").text = host_ip e.SubElement(host_elem, "asset", {"asset_id": host_asset}).text = "" - e.SubElement(host_elem, "start").text = ( - (local_date_time + timedelta(minutes=-15)) - .astimezone() - .replace(microsecond=0) - .isoformat() - ) - e.SubElement(host_elem, "end").text = ( - (local_date_time + timedelta(seconds=-30)) - .astimezone() - .replace(microsecond=0) - .isoformat() - ) + e.SubElement(host_elem, "start").text = calculate_timestamp(-1000) + e.SubElement(host_elem, "end").text = calculate_timestamp(-30) app = choice(list(data["apps"])) os = choice(list(data["oss"])) @@ -357,8 +349,7 @@ def generate_reports(task, n_reports, n_results, with_gauss, **kwargs): def generate_target(): - timestamp = local_date_time.astimezone().replace(microsecond=0).isoformat() - target_name = f"Random_Report_Generation_Target_{timestamp}" + target_name = f"Random_Report_Generation_Target_{calculate_timestamp(0)}" target_comment = ( f"Generated by Random Report Generation Script Version {__version__}." ) @@ -374,19 +365,19 @@ def generate_target(): def generate_data(gmp, n_tasks, task_type, **kwargs): - timestamp = local_date_time.astimezone().replace(microsecond=0).isoformat() - if task_type == "scan": scanner_id = "08b69003-5fc2-4037-a479-93b440211c73" config_id = "085569ce-73ed-11df-83c3-002264764cea" generate_target() target_id = gmp.get_targets( - filter_string=f"Random_Report_Generation_Target_{timestamp}" + filter_string=f"Random_Report_Generation_Target_{calculate_timestamp(0)}" ).xpath("//@id")[0] for i in range(n_tasks): index = f"{{0:0>{len(str(n_tasks))}}}" - task_name = f"Random_Report_Generation_{task_type.capitalize()}_Task_{timestamp}_{index.format(i + 1)}" + task_name = ( + f"Random_Report_Generation_{task_type.capitalize()}_Task_{calculate_timestamp(0)}_{index.format(i + 1)}" + ) task_comment = ( f"Generated by Random Report Generation Script Version {__version__}." )