From 9a29e597818cd0758c6360aedb87424decdc5244 Mon Sep 17 00:00:00 2001 From: Sergi Delgado Segura Date: Wed, 1 Apr 2020 21:30:12 +0200 Subject: [PATCH] Adds additional checks to DBManagers (Appointments and Users) --- teos/appointments_dbm.py | 112 ++++++++++++++++++++++++++++++++------- teos/users_dbm.py | 38 ++++++++++--- 2 files changed, 126 insertions(+), 24 deletions(-) diff --git a/teos/appointments_dbm.py b/teos/appointments_dbm.py index 52cb946..ba1a9f8 100644 --- a/teos/appointments_dbm.py +++ b/teos/appointments_dbm.py @@ -103,9 +103,8 @@ class AppointmentsDBM(DBManager): Returns ``None`` otherwise. """ - data = self.load_entry(key, prefix=WATCHER_PREFIX) - try: + data = self.load_entry(key, prefix=WATCHER_PREFIX) data = json.loads(data) except (TypeError, json.decoder.JSONDecodeError): data = None @@ -122,9 +121,8 @@ class AppointmentsDBM(DBManager): Returns ``None`` otherwise. """ - data = self.load_entry(key, prefix=RESPONDER_PREFIX) - try: + data = self.load_entry(key, prefix=RESPONDER_PREFIX) data = json.loads(data) except (TypeError, json.decoder.JSONDecodeError): data = None @@ -171,10 +169,23 @@ class AppointmentsDBM(DBManager): Args: uuid (:obj:`str`): the identifier of the appointment to be stored. appointment (:obj: `dict`): an appointment encoded as dictionary. + + Returns: + :obj:`bool`: True if the appointment was stored in the db. False otherwise. """ - self.create_entry(uuid, json.dumps(appointment), prefix=WATCHER_PREFIX) - logger.info("Adding appointment to Watchers's db", uuid=uuid) + try: + self.create_entry(uuid, json.dumps(appointment), prefix=WATCHER_PREFIX) + logger.info("Adding appointment to Watchers's db", uuid=uuid) + return True + + except json.JSONDecodeError: + logger.info("Could't add appointment to db. Wrong appointment format.", uuid=uuid, appoinent=appointment) + return False + + except TypeError: + logger.info("Could't add appointment to db.", uuid=uuid, appoinent=appointment) + return False def store_responder_tracker(self, uuid, tracker): """ @@ -183,10 +194,23 @@ class AppointmentsDBM(DBManager): Args: uuid (:obj:`str`): the identifier of the appointment to be stored. tracker (:obj: `dict`): a tracker encoded as dictionary. + + Returns: + :obj:`bool`: True if the tracker was stored in the db. False otherwise. """ - self.create_entry(uuid, json.dumps(tracker), prefix=RESPONDER_PREFIX) - logger.info("Adding appointment to Responder's db", uuid=uuid) + try: + self.create_entry(uuid, json.dumps(tracker), prefix=RESPONDER_PREFIX) + logger.info("Adding tracker to Responder's db", uuid=uuid) + return True + + except json.JSONDecodeError: + logger.info("Could't add tracker to db. Wrong tracker format.", uuid=uuid, tracker=tracker) + return False + + except TypeError: + logger.info("Could't add tracker to db.", uuid=uuid, tracker=tracker) + return False def load_locator_map(self, locator): """ @@ -265,10 +289,19 @@ class AppointmentsDBM(DBManager): Args: locator (:obj:`str`): a 16-byte hex-encoded string identifying the map to delete. + + Returns: + :obj:`bool`: True if the locator map was deleted from the database or it was non-existent, False otherwise. """ - self.delete_entry(locator, prefix=LOCATOR_MAP_PREFIX) - logger.info("Deleting locator map from db", uuid=locator) + try: + self.delete_entry(locator, prefix=LOCATOR_MAP_PREFIX) + logger.info("Deleting locator map from db", locator=locator) + return True + + except TypeError: + logger.info("Couldn't delete locator map from db, locator has wrong type", locator=locator) + return False def delete_watcher_appointment(self, uuid): """ @@ -276,10 +309,19 @@ class AppointmentsDBM(DBManager): Args: uuid (:obj:`str`): a 16-byte hex-encoded string identifying the appointment to be deleted. + + Returns: + :obj:`bool`: True if the appointment was deleted from the database or it was non-existent, False otherwise. """ - self.delete_entry(uuid, prefix=WATCHER_PREFIX) - logger.info("Deleting appointment from Watcher's db", uuid=uuid) + try: + self.delete_entry(uuid, prefix=WATCHER_PREFIX) + logger.info("Deleting appointment from Watcher's db", uuid=uuid) + return True + + except TypeError: + logger.info("Couldn't delete appointment from db, uuid has wrong type", uuid=uuid) + return False def batch_delete_watcher_appointments(self, uuids): """ @@ -300,10 +342,19 @@ class AppointmentsDBM(DBManager): Args: uuid (:obj:`str`): a 16-byte hex-encoded string identifying the tracker to be deleted. + + Returns: + :obj:`bool`: True if the tracker was deleted from the database or it was non-existent, False otherwise. """ - self.delete_entry(uuid, prefix=RESPONDER_PREFIX) - logger.info("Deleting appointment from Responder's db", uuid=uuid) + try: + self.delete_entry(uuid, prefix=RESPONDER_PREFIX) + logger.info("Deleting tracker from Responder's db", uuid=uuid) + return True + + except TypeError: + logger.info("Couldn't delete tracker from db, uuid has wrong type", uuid=uuid) + return False def batch_delete_responder_trackers(self, uuids): """ @@ -346,9 +397,17 @@ class AppointmentsDBM(DBManager): Args: block_hash (:obj:`str`): the block hash to be stored (32-byte hex-encoded) + + Returns: + :obj:`bool`: True if the block hash was stored in the db. False otherwise. """ - self.create_entry(WATCHER_LAST_BLOCK_KEY, block_hash) + try: + self.create_entry(WATCHER_LAST_BLOCK_KEY, block_hash) + return True + + except (TypeError, json.JSONDecodeError): + return False def store_last_block_hash_responder(self, block_hash): """ @@ -356,9 +415,17 @@ class AppointmentsDBM(DBManager): Args: block_hash (:obj:`str`): the block hash to be stored (32-byte hex-encoded) + + Returns: + :obj:`bool`: True if the block hash was stored in the db. False otherwise. """ - self.create_entry(RESPONDER_LAST_BLOCK_KEY, block_hash) + try: + self.create_entry(RESPONDER_LAST_BLOCK_KEY, block_hash) + return True + + except (TypeError, json.JSONDecodeError): + return False def create_triggered_appointment_flag(self, uuid): """ @@ -403,10 +470,19 @@ class AppointmentsDBM(DBManager): Args: uuid (:obj:`str`): the identifier of the flag to be removed. + + Returns: + :obj:`bool`: True if the flag was deleted from the database or it was non-existent, False otherwise. """ - self.delete_entry(uuid, prefix=TRIGGERED_APPOINTMENTS_PREFIX) - logger.info("Removing triggered flag from appointment appointment", uuid=uuid) + try: + self.delete_entry(uuid, prefix=TRIGGERED_APPOINTMENTS_PREFIX) + logger.info("Removing triggered flag from appointment appointment", uuid=uuid) + return True + + except TypeError: + logger.info("Couldn't delete triggered flag from db, uuid has wrong type", uuid=uuid) + return False def batch_delete_triggered_appointment_flag(self, uuids): """ diff --git a/teos/users_dbm.py b/teos/users_dbm.py index d0caac2..d9809be 100644 --- a/teos/users_dbm.py +++ b/teos/users_dbm.py @@ -5,6 +5,7 @@ from teos import LOG_PREFIX from teos.db_manager import DBManager from common.logger import Logger +from common.tools import check_compressed_pk_format logger = Logger(actor="UsersDBM", log_name_prefix=LOG_PREFIX) @@ -43,10 +44,27 @@ class UsersDBM(DBManager): Args: user_pk (:obj:`str`): a 33-byte hex-encoded string identifying the user. user_data (:obj:`dict`): the user associated data, as a dictionary. + + Returns: + :obj:`bool`: True if the user was stored in the database, false otherwise. """ - self.create_entry(user_pk, json.dumps(user_data)) - logger.info("Adding user to Gatekeeper's db", uuid=user_pk) + if check_compressed_pk_format(user_pk): + try: + self.create_entry(user_pk, json.dumps(user_data)) + logger.info("Adding user to Gatekeeper's db", user_pk=user_pk) + return True + + except json.JSONDecodeError: + logger.info("Could't add user to db. Wrong user data format.", user_pk=user_pk, user_data=user_data) + return False + + except TypeError: + logger.info("Could't add user to db.", user_pk=user_pk, user_data=user_data) + return False + else: + logger.info("Could't add user to db. Wrong pk format.", user_pk=user_pk, user_data=user_data) + return False def load_user(self, user_pk): """ @@ -60,9 +78,8 @@ class UsersDBM(DBManager): Returns ``None`` otherwise. """ - data = self.load_entry(user_pk) - try: + data = self.load_entry(user_pk) data = json.loads(data) except (TypeError, json.decoder.JSONDecodeError): data = None @@ -75,10 +92,19 @@ class UsersDBM(DBManager): Args: user_pk (:obj:`str`): a 33-byte hex-encoded string identifying the user. + + Returns: + :obj:`bool`: True if the user was deleted from the database or it was non-existent, False otherwise. """ - self.delete_entry(user_pk) - logger.info("Deleting user from Gatekeeper's db", uuid=user_pk) + try: + self.delete_entry(user_pk) + logger.info("Deleting user from Gatekeeper's db", uuid=user_pk) + return True + + except TypeError: + logger.info("Cant delete user from db, user key has wrong type", uuid=user_pk) + return False def load_all_users(self): """