From 4854c9cfc92f6c0c4dc01a425023d719a12507eb Mon Sep 17 00:00:00 2001 From: Shreya Date: Thu, 25 Mar 2021 19:58:50 +0530 Subject: [PATCH 1/3] Attempt to remove custom PBA file when resetting config only if filename exists in DB --- .../monkey_island/cc/resources/pba_file_upload.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/monkey/monkey_island/cc/resources/pba_file_upload.py b/monkey/monkey_island/cc/resources/pba_file_upload.py index 16d71cfeb..5f41964cb 100644 --- a/monkey/monkey_island/cc/resources/pba_file_upload.py +++ b/monkey/monkey_island/cc/resources/pba_file_upload.py @@ -69,13 +69,14 @@ class FileUpload(flask_restful.Resource): PBA_LINUX_FILENAME_PATH if file_type == "PBAlinux" else PBA_WINDOWS_FILENAME_PATH ) filename = ConfigService.get_config_value(filename_path) - file_path = Path(env_singleton.env.get_config().data_dir_abs_path).joinpath(filename) - try: - if os.path.exists(file_path): - os.remove(file_path) - ConfigService.set_config_value(filename_path, "") - except OSError as e: - LOG.error("Can't remove previously uploaded post breach files: %s" % e) + if filename: + file_path = Path(env_singleton.env.get_config().data_dir_abs_path).joinpath(filename) + try: + if os.path.exists(file_path): + os.remove(file_path) + ConfigService.set_config_value(filename_path, "") + except OSError as e: + LOG.error("Can't remove previously uploaded post breach files: %s" % e) return {} From 4f94e9de740a0ad7d58481b908d59dcd9be22a16 Mon Sep 17 00:00:00 2001 From: Shreya Date: Sat, 24 Apr 2021 12:57:56 +0530 Subject: [PATCH 2/3] Break PBA file deletion into functions: attempt to delete PBA file in another function --- .../cc/resources/pba_file_upload.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/monkey/monkey_island/cc/resources/pba_file_upload.py b/monkey/monkey_island/cc/resources/pba_file_upload.py index 5f41964cb..46169eb5e 100644 --- a/monkey/monkey_island/cc/resources/pba_file_upload.py +++ b/monkey/monkey_island/cc/resources/pba_file_upload.py @@ -18,7 +18,7 @@ from monkey_island.cc.services.post_breach_files import ( __author__ = "VakarisZ" LOG = logging.getLogger(__name__) -# Front end uses these strings to identify which files to work with (linux of windows) +# Front end uses these strings to identify which files to work with (linux or windows) LINUX_PBA_TYPE = "PBAlinux" WINDOWS_PBA_TYPE = "PBAwindows" @@ -71,12 +71,8 @@ class FileUpload(flask_restful.Resource): filename = ConfigService.get_config_value(filename_path) if filename: file_path = Path(env_singleton.env.get_config().data_dir_abs_path).joinpath(filename) - try: - if os.path.exists(file_path): - os.remove(file_path) - ConfigService.set_config_value(filename_path, "") - except OSError as e: - LOG.error("Can't remove previously uploaded post breach files: %s" % e) + FileUpload._delete_file(file_path) + ConfigService.set_config_value(filename_path, "") return {} @@ -97,3 +93,11 @@ class FileUpload(flask_restful.Resource): (PBA_LINUX_FILENAME_PATH if is_linux else PBA_WINDOWS_FILENAME_PATH), filename ) return filename + + @staticmethod + def _delete_file(file_path): + try: + if os.path.exists(file_path): + os.remove(file_path) + except OSError as e: + LOG.error("Couldn't remove previously uploaded post breach files: %s" % e) From 9b38303346683b8c0a1034eb08fd2f8a766eb9e7 Mon Sep 17 00:00:00 2001 From: Shreya Date: Mon, 26 Apr 2021 15:18:26 +0530 Subject: [PATCH 3/3] Rearrange functions' order in `monkey_island/cc/resources/pba_file_upload.py` to follow stepdown rule --- .../cc/resources/pba_file_upload.py | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/monkey/monkey_island/cc/resources/pba_file_upload.py b/monkey/monkey_island/cc/resources/pba_file_upload.py index 46169eb5e..6ae209a12 100644 --- a/monkey/monkey_island/cc/resources/pba_file_upload.py +++ b/monkey/monkey_island/cc/resources/pba_file_upload.py @@ -58,6 +58,24 @@ class FileUpload(flask_restful.Resource): response = Response(response=filename, status=200, mimetype="text/plain") return response + @staticmethod + def upload_pba_file(request_, is_linux=True): + """ + Uploads PBA file to island's file system + :param request_: Request object containing PBA file + :param is_linux: Boolean indicating if this file is for windows or for linux + :return: filename string + """ + filename = secure_filename(request_.files["filepond"].filename) + file_path = ( + Path(env_singleton.env.get_config().data_dir_abs_path).joinpath(filename).absolute() + ) + request_.files["filepond"].save(str(file_path)) + ConfigService.set_config_value( + (PBA_LINUX_FILENAME_PATH if is_linux else PBA_WINDOWS_FILENAME_PATH), filename + ) + return filename + @jwt_required def delete(self, file_type): """ @@ -76,24 +94,6 @@ class FileUpload(flask_restful.Resource): return {} - @staticmethod - def upload_pba_file(request_, is_linux=True): - """ - Uploads PBA file to island's file system - :param request_: Request object containing PBA file - :param is_linux: Boolean indicating if this file is for windows or for linux - :return: filename string - """ - filename = secure_filename(request_.files["filepond"].filename) - file_path = ( - Path(env_singleton.env.get_config().data_dir_abs_path).joinpath(filename).absolute() - ) - request_.files["filepond"].save(str(file_path)) - ConfigService.set_config_value( - (PBA_LINUX_FILENAME_PATH if is_linux else PBA_WINDOWS_FILENAME_PATH), filename - ) - return filename - @staticmethod def _delete_file(file_path): try: