Merge pull request #1054 from shreyamalviya/bugfix_pba_file_delete

Fix error when removing uploaded post breach file on config reset
This commit is contained in:
Mike Salvatore 2021-04-26 09:06:49 -04:00 committed by GitHub
commit c6fd04e2b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 27 additions and 22 deletions

View File

@ -18,7 +18,7 @@ from monkey_island.cc.services.post_breach_files import (
__author__ = "VakarisZ" __author__ = "VakarisZ"
LOG = logging.getLogger(__name__) 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" LINUX_PBA_TYPE = "PBAlinux"
WINDOWS_PBA_TYPE = "PBAwindows" WINDOWS_PBA_TYPE = "PBAwindows"
@ -58,27 +58,6 @@ class FileUpload(flask_restful.Resource):
response = Response(response=filename, status=200, mimetype="text/plain") response = Response(response=filename, status=200, mimetype="text/plain")
return response return response
@jwt_required
def delete(self, file_type):
"""
Deletes file that has been deleted on the front end
:param file_type: Type indicates which file was deleted, linux of windows
:return: Empty response
"""
filename_path = (
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)
return {}
@staticmethod @staticmethod
def upload_pba_file(request_, is_linux=True): def upload_pba_file(request_, is_linux=True):
""" """
@ -96,3 +75,29 @@ class FileUpload(flask_restful.Resource):
(PBA_LINUX_FILENAME_PATH if is_linux else PBA_WINDOWS_FILENAME_PATH), filename (PBA_LINUX_FILENAME_PATH if is_linux else PBA_WINDOWS_FILENAME_PATH), filename
) )
return filename return filename
@jwt_required
def delete(self, file_type):
"""
Deletes file that has been deleted on the front end
:param file_type: Type indicates which file was deleted, linux of windows
:return: Empty response
"""
filename_path = (
PBA_LINUX_FILENAME_PATH if file_type == "PBAlinux" else PBA_WINDOWS_FILENAME_PATH
)
filename = ConfigService.get_config_value(filename_path)
if filename:
file_path = Path(env_singleton.env.get_config().data_dir_abs_path).joinpath(filename)
FileUpload._delete_file(file_path)
ConfigService.set_config_value(filename_path, "")
return {}
@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)