From 4024a49a4a20417fd2493a9530debc7be171faa2 Mon Sep 17 00:00:00 2001 From: qweqweasdasd <qweqweasdasd@infn.it> Date: Tue, 6 Dec 2022 11:04:00 +0100 Subject: [PATCH] Added multiple retries during connection creation --- files/script.py | 382 ++++++++++++++++++++++++------------------------ 1 file changed, 191 insertions(+), 191 deletions(-) diff --git a/files/script.py b/files/script.py index 737c9fb..7ef1f74 100644 --- a/files/script.py +++ b/files/script.py @@ -10,49 +10,65 @@ from sys import argv, exit import socket import os -def get_version(): +def get_version_old(): with Gmp(connection, transform=transform) as gmp: gmp.authenticate(auth_name, auth_passwd) pretty_print(gmp.get_version()) +def create_connection(): + connection_retries = 5 + retry = connection_retries + while(retry > 0): + try: + gmp = Gmp(connection, transform=transform) + gmp.authenticate(auth_name, auth_passwd) + return gmp + except: + print("WARNING: Connection error with the gmp endpoint. Remaining {retry} retries") + retry -= 1 + sleep(0.5) + raise Exception("Impossible connect to the gmp endpoint after 5 retries") + +def get_version(): + gmp = create_connection() + res = gmp.get_version() + pretty_print(res) + ########## PORT LIST ################################## def create_port_list(port_list_name, ports): - with Gmp(connection, transform=transform) as gmp: - gmp.authenticate(auth_name, auth_passwd) - res = gmp.create_port_list(port_list_name, ','.join(ports)) - status = res.xpath('@status')[0] - status_text = res.xpath('@status_text')[0] - if status == "201": - id = res.xpath('@id')[0] - return {'name': port_list_name, 'id': id} - else: - msg = f"ERROR during Port list creation. Status code: {status}, msg: {status_text}" - raise Exception(msg) + gmp = create_connection() + res = gmp.create_port_list(port_list_name, ','.join(ports)) + status = res.xpath('@status')[0] + status_text = res.xpath('@status_text')[0] + if status == "201": + id = res.xpath('@id')[0] + return {'name': port_list_name, 'id': id} + else: + msg = f"ERROR during Port list creation. Status code: {status}, msg: {status_text}" + raise Exception(msg) def get_port_lists(filter_str="rows=-1"): l_o = [] - with Gmp(connection, transform=transform) as gmp: - gmp.authenticate(auth_name, auth_passwd) - res = gmp.get_port_lists(filter_string=filter_str) - for pl in res.xpath('port_list'): - o = dict() - o['name'] = pl.xpath('name/text()')[0] - o['id'] = pl.xpath('@id')[0] - o['in_use'] = pl.xpath('in_use/text()')[0] - l_o.append(o) + gmp = create_connection() + res = gmp.get_port_lists(filter_string=filter_str) + for pl in res.xpath('port_list'): + o = dict() + o['name'] = pl.xpath('name/text()')[0] + o['id'] = pl.xpath('@id')[0] + o['in_use'] = pl.xpath('in_use/text()')[0] + l_o.append(o) return l_o def delete_port_list(port_list): - with Gmp(connection, transform=transform) as gmp: - gmp.authenticate(auth_name, auth_passwd) - res = gmp.delete_port_list(port_list['id']) - status = res.xpath('@status')[0] - status_text = res.xpath('@status_text')[0] - if status == "200": - print(f"Port_list with id: {port_list['id']} and name: {port_list['name']} DELETED") - else: - print(f"ERROR {status}: {status_text}") + gmp = create_connection() + res = gmp.delete_portlist(port_list['id']) + status = res.xpath('@status')[0] + status_text = res.xpath('@status_text')[0] + if status == "200": + print(f"Port_list with id: {port_list['id']} and name: {port_list['name']} DELETED") + else: + print(f"ERROR {status}: {status_text}") def get_or_create_port_list(port_list_name, ports): res = get_port_lists(port_list_name) @@ -69,48 +85,45 @@ def get_or_create_port_list(port_list_name, ports): def create_target(name,ip,port_list,ovs_ssh_credential): o = dict() - with Gmp(connection, transform=transform) as gmp: - gmp.authenticate(auth_name, auth_passwd) - res = gmp.create_target( - name=name, - comment = "", - hosts=[ip], - port_list_id = port_list['id'], - ssh_credential_id = ovs_ssh_credential['id'], - alive_test=AliveTest('Consider Alive')) - status = res.xpath('@status')[0] - status_text = res.xpath('@status_text')[0] - if status == "201": - id = res.xpath('@id')[0] - return {'name': name, 'id': id} - else: - msg = f"ERROR during Target creation. Status code: {status}, msg: {status_text}" - raise Exception(msg) + gmp = create_connection() + res = gmp.create_target( + name=name, + comment = "", + hosts=[ip], + port_list_id = port_list['id'], + ssh_credential_id = ovs_ssh_credential['id'], + alive_test=AliveTest('Consider Alive')) + status = res.xpath('@status')[0] + status_text = res.xpath('@status_text')[0] + if status == "201": + id = res.xpath('@id')[0] + return {'name': name, 'id': id} + else: + msg = f"ERROR during Target creation. Status code: {status}, msg: {status_text}" + raise Exception(msg) def get_targets(filter_str): res = [] - with Gmp(connection, transform=transform) as gmp: - gmp.authenticate(auth_name, auth_passwd) - targets = gmp.get_targets(filter_string=filter_str) - for target in targets.xpath('target'): - o = dict() - o['name'] = target.xpath('name/text()')[0] - o['hosts'] = target.xpath('hosts/text()')[0] - o['id'] = target.xpath('@id')[0] - o['in_use'] = target.xpath('in_use/text()')[0] - res.append(o) + gmp = create_connection() + targets = gmp.get_targets(filter_string=filter_str) + for target in targets.xpath('target'): + o = dict() + o['name'] = target.xpath('name/text()')[0] + o['hosts'] = target.xpath('hosts/text()')[0] + o['id'] = target.xpath('@id')[0] + o['in_use'] = target.xpath('in_use/text()')[0] + res.append(o) return res def delete_target(target): - with Gmp(connection, transform=transform) as gmp: - gmp.authenticate(auth_name, auth_passwd) - res = gmp.delete_target(target['id']) - status = res.xpath('@status')[0] - status_text = res.xpath('@status_text')[0] - if status == "200": - print(f"Port_list with id: {target['id']} and name: {target['name']} DELETED") - else: - print(f"ERROR {status}: {status_text}") + gmp = create_connection() + res = gmp.delete_target(target['id']) + status = res.xpath('@status')[0] + status_text = res.xpath('@status_text')[0] + if status == "200": + print(f"Port_list with id: {target['id']} and name: {target['name']} DELETED") + else: + print(f"ERROR {status}: {status_text}") def get_or_create_target(target_name,ip,port_list,ovs_ssh_credential): res = get_targets(target_name) @@ -139,40 +152,38 @@ def search_and_delete_all_targets(target_name): def create_task(name, config, target, scanner): o = dict() - with Gmp(connection, transform=transform) as gmp: - gmp.authenticate(auth_name, auth_passwd) - res = gmp.create_task( - name=name, - config_id=config['id'], - target_id=target['id'], - scanner_id=scanner['id']) - status = res.xpath('@status')[0] - status_text = res.xpath('@status_text')[0] - if status == "201": - id = res.xpath('@id')[0] - return {'name': name, 'id': id} - else: - msg = f"ERROR during Task creation. Status code: {status}, msg: {status_text}" - raise Exception(msg) + gmp = create_connection() + res = gmp.create_task( + name=name, + config_id=config['id'], + target_id=target['id'], + scanner_id=scanner['id']) + status = res.xpath('@status')[0] + status_text = res.xpath('@status_text')[0] + if status == "201": + id = res.xpath('@id')[0] + return {'name': name, 'id': id} + else: + msg = f"ERROR during Task creation. Status code: {status}, msg: {status_text}" + raise Exception(msg) def get_tasks(filter_str): res = [] - with Gmp(connection, transform=transform) as gmp: - gmp.authenticate(auth_name, auth_passwd) - tasks = gmp.get_tasks(filter_string=filter_str) - for task in tasks.xpath('task'): - o = dict() - o['name'] = task.xpath('name/text()')[0] - o['id'] = task.xpath('@id')[0] - o['progress'] = task.xpath('progress/text()')[0] - o['in_use'] = task.xpath('in_use/text()')[0] - o['status'] = task.xpath('status/text()')[0] - o['target_id'] = task.xpath('target/@id')[0] - try: - o['report_id'] = task.xpath('last_report/report/@id')[0] - except: - pass - res.append(o) + gmp = create_connection() + tasks = gmp.get_tasks(filter_string=filter_str) + for task in tasks.xpath('task'): + o = dict() + o['name'] = task.xpath('name/text()')[0] + o['id'] = task.xpath('@id')[0] + o['progress'] = task.xpath('progress/text()')[0] + o['in_use'] = task.xpath('in_use/text()')[0] + o['status'] = task.xpath('status/text()')[0] + o['target_id'] = task.xpath('target/@id')[0] + try: + o['report_id'] = task.xpath('last_report/report/@id')[0] + except: + pass + res.append(o) return res def get_or_create_task(task_name, config, target, scanner): @@ -188,22 +199,21 @@ def get_or_create_task(task_name, config, target, scanner): def get_all_tasks(): res = [] - with Gmp(connection, transform=transform) as gmp: - gmp.authenticate(auth_name, auth_passwd) - tasks = gmp.get_tasks(filter_string="rows=-1") - for task in tasks.xpath('task'): - o = dict() - o['name'] = task.xpath('name/text()')[0] - o['id'] = task.xpath('@id')[0] - o['progress'] = task.xpath('progress/text()')[0] - o['in_use'] = task.xpath('in_use/text()')[0] - o['status'] = task.xpath('status/text()')[0] - o['target_id'] = task.xpath('target/@id')[0] - try: - o['report_id'] = task.xpath('last_report/report/@id')[0] - except: - pass - res.append(o) + gmp = create_connection() + tasks = gmp.get_tasks(filter_string="rows=-1") + for task in tasks.xpath('task'): + o = dict() + o['name'] = task.xpath('name/text()')[0] + o['id'] = task.xpath('@id')[0] + o['progress'] = task.xpath('progress/text()')[0] + o['in_use'] = task.xpath('in_use/text()')[0] + o['status'] = task.xpath('status/text()')[0] + o['target_id'] = task.xpath('target/@id')[0] + try: + o['report_id'] = task.xpath('last_report/report/@id')[0] + except: + pass + res.append(o) return res def search_and_delete_all_tasks(filter_str): @@ -212,27 +222,25 @@ def search_and_delete_all_tasks(filter_str): delete_task(task) def start_task(task): - with Gmp(connection, transform=transform) as gmp: - gmp.authenticate(auth_name, auth_passwd) - res = gmp.start_task(task['id']) - task['report_id'] = res.xpath('report_id/text()')[0] + gmp = create_connection() + res = gmp.start_task(task['id']) + task['report_id'] = res.xpath('report_id/text()')[0] return task def stop_task(task): - with Gmp(connection, transform=transform) as gmp: - gmp.authenticate(auth_name, auth_passwd) - pretty_print(gmp.stop_task(task['id'])) + gmp = create_connection() + res = gmp.stop_task(task['id']) + pretty_print(res) def delete_task(task): - with Gmp(connection, transform=transform) as gmp: - gmp.authenticate(auth_name, auth_passwd) - res = gmp.delete_task(task['id']) - status = res.xpath('@status')[0] - status_text = res.xpath('@status_text')[0] - if status == "200": - print(f"Target with id: {task['id']} and name: {task['name']} DELETED") - else: - print(f"ERROR {status}: {status_text}") + gmp = create_connection() + res = gmp.delete_task(task['id']) + status = res.xpath('@status')[0] + status_text = res.xpath('@status_text')[0] + if status == "200": + print(f"Target with id: {task['id']} and name: {task['name']} DELETED") + else: + print(f"ERROR {status}: {status_text}") ############## REPORTS #####################################3 @@ -245,19 +253,17 @@ class report_formats: xml = "a994b278-1f62-11e1-96ac-406186ea4fc5" def get_report_formats(): - with Gmp(connection, transform=transform) as gmp: - gmp.authenticate(auth_name, auth_passwd) - res = gmp.get_report_formats() - for f in res.xpath('report_format'): - name = f.xpath('name/text()')[0] - id = f.xpath('@id')[0] - print(id,name) + gmp = create_connection() + res = gmp.get_report_formats() + for f in res.xpath('report_format'): + name = f.xpath('name/text()')[0] + id = f.xpath('@id')[0] + print(id,name) def get_report_format(id): - with Gmp(connection, transform=transform) as gmp: - gmp.authenticate(auth_name, auth_passwd) - res = gmp.get_report_formats() - pretty_print(res) + gmp = create_connection() + res = gmp.get_report_formats() + pretty_print(res) def get_progress(task): task_info = get_tasks(task['id'])[0] @@ -279,64 +285,58 @@ def wait_for_task_completition(task, timeout=3600): sleep(60) def save_report(task,report_format_id, report_filename ): - with Gmp(connection, transform=transform) as gmp: - gmp.authenticate(auth_name, auth_passwd) - res = gmp.get_report(task['report_id'], - report_format_id=report_format_id, - ignore_pagination=True, - details="1") - code = str(res.xpath('report/text()')[0]) - with open(report_filename, "wb") as fh: - fh.write(base64.b64decode(code)) + gmp = create_connection() + res = gmp.get_report(task['report_id'], + report_format_id=report_format_id, + ignore_pagination=True, + details="1") + code = str(res.xpath('report/text()')[0]) + with open(report_filename, "wb") as fh: + fh.write(base64.b64decode(code)) def save_severity_report(task, severity_filename): dict_severity = {"Log": 0, "Low": 1, "Medium": 2, "High": 3} - with Gmp(connection, transform=transform) as gmp: - gmp.authenticate(auth_name, auth_passwd) - res = gmp.get_report(task['report_id'], - report_format_id=report_formats.anonymous_xml, - ignore_pagination=True, - details="1") - severities = res.xpath('report/report/ports/port/threat/text()') - old_num_severity = 0 - severity = "Log" - for sev in severities: - if dict_severity[sev] > old_num_severity: - old_num_severity = dict_severity[sev] - severity = sev - with open(severity_filename, "w") as f: - f.write(severity) + gmp = create_connection() + res = gmp.get_report(task['report_id'], + report_format_id=report_formats.anonymous_xml, + ignore_pagination=True, + details="1") + severities = res.xpath('report/report/ports/port/threat/text()') + old_num_severity = 0 + severity = "Log" + for sev in severities: + if dict_severity[sev] > old_num_severity: + old_num_severity = dict_severity[sev] + severity = sev + with open(severity_filename, "w") as f: + f.write(severity) def get_severity(task): dict_severity = {"Log": 0, "Low": 1, "Medium": 2, "High": 3} - with Gmp(connection, transform=transform) as gmp: - gmp.authenticate(auth_name, auth_passwd) - res = gmp.get_report(task['report_id'], - report_format_id=report_formats.anonymous_xml, - ignore_pagination=True, - details="1") - severities = res.xpath('report/report/ports/port/threat/text()') - old_num_severity = 0 - severity = "Log" - for sev in severities: - if dict_severity[sev] > old_num_severity: - old_num_severity = dict_severity[sev] - severity = sev - return severity + gmp = create_connection() + res = gmp.get_report(task['report_id'], + report_format_id=report_formats.anonymous_xml, + ignore_pagination=True, + details="1") + severities = res.xpath('report/report/ports/port/threat/text()') + old_num_severity = 0 + severity = "Log" + for sev in severities: + if dict_severity[sev] > old_num_severity: + old_num_severity = dict_severity[sev] + severity = sev + return severity def get_reports(filter_str="rows=-1"): lo = [] - with Gmp(connection, transform=transform) as gmp: - gmp.authenticate(auth_name, auth_passwd) - reports = gmp.get_reports(filter_string = filter_str) - for report in reports.xpath('report'): - o = dict() - o['task_name'] = report.xpath('task/name/text()')[0] - o['id'] = report.xpath('@id')[0] - lo.append(o) - return lo - - + gmp = create_connection() + reports = gmp.get_reports(filter_string = filter_str) + for report in reports.xpath('report'): + o = dict() + o['task_name'] = report.xpath('task/name/text()')[0] + o['id'] = report.xpath('@id')[0] + lo.append(o) + return lo def get_numeric_severity(severity): if severity == "Log": @@ -392,9 +392,9 @@ def import_dep_info(file_path, endpoints_to_scan): else: host = url if prefix == "https": - port = 443 + port = '443' elif prefix == 'http': - port = 80 + port = '80' else: raise Exception(f"Impossible to parse the endpoint port. Endpoint: {endpoint}") print(f"Endpoint: {host}:{port}") -- GitLab