Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error when removing uploaded post breach file on config reset #1054

Merged
merged 3 commits into from
Apr 26, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions monkey/monkey_island/cc/resources/pba_file_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
__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"

Expand Down Expand Up @@ -69,13 +69,10 @@ def delete(self, file_type):
PBA_LINUX_FILENAME_PATH if file_type == "PBAlinux" else PBA_WINDOWS_FILENAME_PATH
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding the value of adding tests, does this function behave in a sane way if I pass an invalid value for file_type?

)
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)
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, "")
except OSError as e:
LOG.error("Can't remove previously uploaded post breach files: %s" % e)

return {}

Expand All @@ -96,3 +93,11 @@ def upload_pba_file(request_, is_linux=True):
(PBA_LINUX_FILENAME_PATH if is_linux else PBA_WINDOWS_FILENAME_PATH), filename
)
return filename

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend putting this method immediately after the one that calls it so as to follow the Stepdown rule

image

@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)