From a5f07e907773bd2e3a443d65702d63e1f9214309 Mon Sep 17 00:00:00 2001 From: Jacob Boddey Date: Thu, 25 Jul 2024 16:49:25 +0100 Subject: [PATCH 1/5] Allow draft profiles to expire --- framework/python/src/common/session.py | 12 +++++------- testing/unit/services/output/services.log | 6 ------ 2 files changed, 5 insertions(+), 13 deletions(-) delete mode 100644 testing/unit/services/output/services.log diff --git a/framework/python/src/common/session.py b/framework/python/src/common/session.py index feee6e8a0..d57e5afef 100644 --- a/framework/python/src/common/session.py +++ b/framework/python/src/common/session.py @@ -528,16 +528,14 @@ def update_profile(self, profile_json): return risk_profile def check_profile_status(self, profile): + """Check if a profile has expired""" - if profile.status == 'Valid': + created_date = profile.created.timestamp() - # Check expiry - created_date = profile.created.timestamp() + today = datetime.datetime.now().timestamp() - today = datetime.datetime.now().timestamp() - - if created_date < (today - SECONDS_IN_YEAR): - profile.status = 'Expired' + if created_date < (today - SECONDS_IN_YEAR): + profile.status = 'Expired' return profile.status diff --git a/testing/unit/services/output/services.log b/testing/unit/services/output/services.log deleted file mode 100644 index 7df3f745b..000000000 --- a/testing/unit/services/output/services.log +++ /dev/null @@ -1,6 +0,0 @@ -Jun 17 09:23:01 test_services INFO Module report generated at: testing/unit/services/output/services_report.html -Jun 17 09:23:01 test_services INFO Module report generated at: testing/unit/services/output/services_report.html -Jun 17 09:23:01 test_services INFO Module report generated at: testing/unit/services/output/services_report.html -Jun 17 09:32:48 test_services INFO Module report generated at: testing/unit/services/output/services_report.html -Jun 17 09:32:48 test_services INFO Module report generated at: testing/unit/services/output/services_report.html -Jun 17 09:32:48 test_services INFO Module report generated at: testing/unit/services/output/services_report.html From 22e70e9437dd64c1dcd6b7f9017622fa839172ab Mon Sep 17 00:00:00 2001 From: Jacob Boddey Date: Thu, 25 Jul 2024 21:19:31 +0100 Subject: [PATCH 2/5] Move status method into risk profile class --- framework/python/src/common/risk_profile.py | 12 +++++++++ framework/python/src/common/session.py | 27 ++++++++++----------- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/framework/python/src/common/risk_profile.py b/framework/python/src/common/risk_profile.py index 6afb229ac..26c4e2426 100644 --- a/framework/python/src/common/risk_profile.py +++ b/framework/python/src/common/risk_profile.py @@ -24,6 +24,7 @@ PROFILES_PATH = 'local/risk_profiles' LOGGER = logger.get_logger('risk_profile') RESOURCES_DIR = 'resources/report' +SECONDS_IN_YEAR = 31536000 # Locate parent directory current_dir = os.path.dirname(os.path.realpath(__file__)) @@ -95,6 +96,17 @@ def get_file_path(self): return os.path.join(PROFILES_PATH, self.name + '.json') + def check_status(self): + """Checks if the profile has expired + and updates the status to Expired if required.""" + + created_date = self.created.timestamp() + + today = datetime.now().timestamp() + + if created_date < (today - SECONDS_IN_YEAR): + self.status = 'Expired' + def _validate(self, profile_json, profile_format): if self._valid(profile_json, profile_format): if self._expired(): diff --git a/framework/python/src/common/session.py b/framework/python/src/common/session.py index d57e5afef..23dee6c1f 100644 --- a/framework/python/src/common/session.py +++ b/framework/python/src/common/session.py @@ -37,7 +37,6 @@ MAX_DEVICE_REPORTS_KEY = 'max_device_reports' CERTS_PATH = 'local/root_certs' CONFIG_FILE_PATH = 'local/system.json' -SECONDS_IN_YEAR = 31536000 PROFILE_FORMAT_PATH = 'resources/risk_assessment.json' PROFILES_DIR = 'local/risk_profiles' @@ -402,17 +401,29 @@ def _load_profiles(self): try: for risk_profile_file in os.listdir( os.path.join(self._root_dir, PROFILES_DIR)): + LOGGER.debug(f'Discovered profile {risk_profile_file}') + # Open the risk profile file with open(os.path.join(self._root_dir, PROFILES_DIR, risk_profile_file), encoding='utf-8') as f: + + # Parse risk profile json json_data = json.load(f) + + # Instantiate a new risk profile risk_profile = RiskProfile() + + # Pass JSON to populate risk profile risk_profile.load( profile_json=json_data, profile_format=self._profile_format ) - risk_profile.status = self.check_profile_status(risk_profile) + + # Trigger checking of profile status (expiry) + risk_profile.check_status() + + # Add risk profile to session self._profiles.append(risk_profile) except Exception as e: @@ -527,18 +538,6 @@ def update_profile(self, profile_json): return risk_profile - def check_profile_status(self, profile): - """Check if a profile has expired""" - - created_date = profile.created.timestamp() - - today = datetime.datetime.now().timestamp() - - if created_date < (today - SECONDS_IN_YEAR): - profile.status = 'Expired' - - return profile.status - def delete_profile(self, profile): try: From 6232788b021d8ebc99505f2ee4e0634b6dabc1cd Mon Sep 17 00:00:00 2001 From: Jacob Boddey Date: Mon, 29 Jul 2024 19:35:42 +0100 Subject: [PATCH 3/5] Use existing method --- framework/python/src/common/risk_profile.py | 16 +++++----------- framework/python/src/common/session.py | 3 --- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/framework/python/src/common/risk_profile.py b/framework/python/src/common/risk_profile.py index 26c4e2426..83ecb5ac3 100644 --- a/framework/python/src/common/risk_profile.py +++ b/framework/python/src/common/risk_profile.py @@ -73,6 +73,11 @@ def load(self, profile_json, profile_format): self._validate(profile_json, profile_format) self.update_risk(profile_format) + # Check if the profile has expired + expired = self._expired() + if expired: + self.status = 'Expired' + return self def update(self, profile_json, profile_format): @@ -96,17 +101,6 @@ def get_file_path(self): return os.path.join(PROFILES_PATH, self.name + '.json') - def check_status(self): - """Checks if the profile has expired - and updates the status to Expired if required.""" - - created_date = self.created.timestamp() - - today = datetime.now().timestamp() - - if created_date < (today - SECONDS_IN_YEAR): - self.status = 'Expired' - def _validate(self, profile_json, profile_format): if self._valid(profile_json, profile_format): if self._expired(): diff --git a/framework/python/src/common/session.py b/framework/python/src/common/session.py index 23dee6c1f..795506083 100644 --- a/framework/python/src/common/session.py +++ b/framework/python/src/common/session.py @@ -420,9 +420,6 @@ def _load_profiles(self): profile_format=self._profile_format ) - # Trigger checking of profile status (expiry) - risk_profile.check_status() - # Add risk profile to session self._profiles.append(risk_profile) From 542cd931e5b6b30edec3c868e8bb1e45731f8140 Mon Sep 17 00:00:00 2001 From: Jacob Boddey Date: Tue, 30 Jul 2024 10:21:52 +0100 Subject: [PATCH 4/5] Check for expiry in validate method --- framework/python/src/common/risk_profile.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/framework/python/src/common/risk_profile.py b/framework/python/src/common/risk_profile.py index 83ecb5ac3..1b22e0aa4 100644 --- a/framework/python/src/common/risk_profile.py +++ b/framework/python/src/common/risk_profile.py @@ -73,11 +73,6 @@ def load(self, profile_json, profile_format): self._validate(profile_json, profile_format) self.update_risk(profile_format) - # Check if the profile has expired - expired = self._expired() - if expired: - self.status = 'Expired' - return self def update(self, profile_json, profile_format): @@ -102,11 +97,11 @@ def get_file_path(self): self.name + '.json') def _validate(self, profile_json, profile_format): - if self._valid(profile_json, profile_format): - if self._expired(): - self.status = 'Expired' + if self._expired(): + self.status = 'Expired' + elif self._valid(profile_json, profile_format): # User only wants to save a draft - elif 'status' in profile_json and profile_json['status'] == 'Draft': + if 'status' in profile_json and profile_json['status'] == 'Draft': self.status = 'Draft' else: self.status = 'Valid' From f9393a316331cf7490766005d1b82f8274ac21a3 Mon Sep 17 00:00:00 2001 From: Jacob Boddey Date: Wed, 31 Jul 2024 09:48:56 +0100 Subject: [PATCH 5/5] Remove unused variable --- framework/python/src/common/risk_profile.py | 1 - 1 file changed, 1 deletion(-) diff --git a/framework/python/src/common/risk_profile.py b/framework/python/src/common/risk_profile.py index 1b22e0aa4..760f73d72 100644 --- a/framework/python/src/common/risk_profile.py +++ b/framework/python/src/common/risk_profile.py @@ -24,7 +24,6 @@ PROFILES_PATH = 'local/risk_profiles' LOGGER = logger.get_logger('risk_profile') RESOURCES_DIR = 'resources/report' -SECONDS_IN_YEAR = 31536000 # Locate parent directory current_dir = os.path.dirname(os.path.realpath(__file__))