From 4c1d4ac8d3ad97f7a2911f39a9ad3946e5d1e66d Mon Sep 17 00:00:00 2001 From: Sebastien Date: Fri, 27 Mar 2020 09:30:01 +0100 Subject: [PATCH 1/3] Fix scan status checking --- PatrowlEnginesUtils/PatrowlEngineTest.py | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/PatrowlEnginesUtils/PatrowlEngineTest.py b/PatrowlEnginesUtils/PatrowlEngineTest.py index 96f8019..5bab9c8 100644 --- a/PatrowlEnginesUtils/PatrowlEngineTest.py +++ b/PatrowlEnginesUtils/PatrowlEngineTest.py @@ -128,7 +128,7 @@ def custom_test(self, test_name, assets, scan_policy={}, is_valid=True, while time.time() < timeout_start + max_timeout: r = requests.get( url="{}/status/{}".format(self.base_url, TEST_SCAN_ID)) - if r.json()["status"] == "SCANNING": + if r.json()["status"] in ("SCANNING","STARTED"): time.sleep(3) else: if r.json()["status"] == "FINISHED": diff --git a/setup.py b/setup.py index f8f10d5..49f9bae 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ setup( name='PatrowlEnginesUtils', - version='0.0.12', + version='0.0.13', description='Common classes for PatrowlEngines', url='https://github.com/Patrowl/PatrowlEnginesUtils', author='Nicolas Mattiocco', From 8cc3d40a9d4292ad8d39d92378b1105d586f3601 Mon Sep 17 00:00:00 2001 From: Sebastien Date: Fri, 27 Mar 2020 13:58:57 +0100 Subject: [PATCH 2/3] Mitigate PID error --- PatrowlEnginesUtils/PatrowlEngineTest.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/PatrowlEnginesUtils/PatrowlEngineTest.py b/PatrowlEnginesUtils/PatrowlEngineTest.py index 5bab9c8..cb4c87c 100644 --- a/PatrowlEnginesUtils/PatrowlEngineTest.py +++ b/PatrowlEnginesUtils/PatrowlEngineTest.py @@ -124,12 +124,18 @@ def custom_test(self, test_name, assets, scan_policy={}, is_valid=True, # Wait until scan is finished timeout_start = time.time() + # Mitigate PID error + retries =0 has_error = False while time.time() < timeout_start + max_timeout: r = requests.get( url="{}/status/{}".format(self.base_url, TEST_SCAN_ID)) if r.json()["status"] in ("SCANNING","STARTED"): time.sleep(3) + elif (r.json()["status"] == "ERROR" and "reason" in r.json() and + r.json()["reason"] == "No PID found" and retries < 3): + retries+=1 + time.sleep(3) else: if r.json()["status"] == "FINISHED": assert True From 20fe63dcabdfdfebf8a71a5aa78bd632d201857b Mon Sep 17 00:00:00 2001 From: Sebastien Date: Fri, 27 Mar 2020 14:10:31 +0100 Subject: [PATCH 3/3] Cleaned code --- PatrowlEnginesUtils/PatrowlEngineTest.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/PatrowlEnginesUtils/PatrowlEngineTest.py b/PatrowlEnginesUtils/PatrowlEngineTest.py index cb4c87c..4665d89 100644 --- a/PatrowlEnginesUtils/PatrowlEngineTest.py +++ b/PatrowlEnginesUtils/PatrowlEngineTest.py @@ -2,9 +2,9 @@ """This file manage tests on PatrowlEngine.""" import json -import requests import time import random +import requests class PatrowlEngineTest: @@ -97,11 +97,13 @@ def test_cleanscans(self): finally: return r.json() - def custom_test(self, test_name, assets, scan_policy={}, is_valid=True, + def custom_test(self, test_name, assets, scan_policy=None, is_valid=True, max_timeout=1200): """Start a custom test.""" print("test-{}-custom: {}".format(self.engine_name, test_name)) TEST_SCAN_ID = random.randint(1000000, 1999999) + if(scan_policy == None): + scan_policy = {} post_data = { "assets": assets, "options": scan_policy, @@ -125,16 +127,16 @@ def custom_test(self, test_name, assets, scan_policy={}, is_valid=True, # Wait until scan is finished timeout_start = time.time() # Mitigate PID error - retries =0 + retries = 0 has_error = False while time.time() < timeout_start + max_timeout: r = requests.get( url="{}/status/{}".format(self.base_url, TEST_SCAN_ID)) - if r.json()["status"] in ("SCANNING","STARTED"): + if r.json()["status"] in ("SCANNING", "STARTED"): time.sleep(3) elif (r.json()["status"] == "ERROR" and "reason" in r.json() and r.json()["reason"] == "No PID found" and retries < 3): - retries+=1 + retries += 1 time.sleep(3) else: if r.json()["status"] == "FINISHED":