From 53a126482f3a6491529128ed1eb016428d74ad5b Mon Sep 17 00:00:00 2001 From: Shreya Date: Mon, 7 Jun 2021 13:14:31 +0530 Subject: [PATCH] Extract file checking activities --- .../cc/services/utils/file_handling.py | 22 +++++++++++++++++++ .../cc/services/utils/file_permissions.py | 8 ------- .../cc/setup/certificate/certificate_setup.py | 17 +++++--------- 3 files changed, 28 insertions(+), 19 deletions(-) create mode 100644 monkey/monkey_island/cc/services/utils/file_handling.py delete mode 100644 monkey/monkey_island/cc/services/utils/file_permissions.py diff --git a/monkey/monkey_island/cc/services/utils/file_handling.py b/monkey/monkey_island/cc/services/utils/file_handling.py new file mode 100644 index 000000000..0715e6efe --- /dev/null +++ b/monkey/monkey_island/cc/services/utils/file_handling.py @@ -0,0 +1,22 @@ +import os + +from common.utils.exceptions import InsecurePermissionsError + + +def ensure_file_existence(file: str) -> None: + if not os.path.exists(file): + raise FileNotFoundError(f"File not found at {file}. Exiting.") + + +def ensure_file_permissions(file: str) -> None: + if not file_has_sufficient_permissions(path=file, required_permissions="0o400"): + raise InsecurePermissionsError( + f"{file} has insecure permissions. Required permissions: r--------. Exiting." + ) + + +def file_has_sufficient_permissions(path: str, required_permissions: str) -> bool: + file_mode = os.stat(path).st_mode + file_permissions = oct(file_mode & 0o777) + + return file_permissions == required_permissions diff --git a/monkey/monkey_island/cc/services/utils/file_permissions.py b/monkey/monkey_island/cc/services/utils/file_permissions.py deleted file mode 100644 index 05587ad09..000000000 --- a/monkey/monkey_island/cc/services/utils/file_permissions.py +++ /dev/null @@ -1,8 +0,0 @@ -import os - - -def has_sufficient_permissions(path: str, required_permissions: str) -> bool: - file_mode = os.stat(path).st_mode - file_permissions = oct(file_mode & 0o777) - - return file_permissions == required_permissions diff --git a/monkey/monkey_island/cc/setup/certificate/certificate_setup.py b/monkey/monkey_island/cc/setup/certificate/certificate_setup.py index 8363b675d..1eaab4f04 100644 --- a/monkey/monkey_island/cc/setup/certificate/certificate_setup.py +++ b/monkey/monkey_island/cc/setup/certificate/certificate_setup.py @@ -1,8 +1,9 @@ import logging -import os -from common.utils.exceptions import InsecurePermissionsError -from monkey_island.cc.services.utils.file_permissions import has_sufficient_permissions +from monkey_island.cc.services.utils.file_handling import ( + ensure_file_existence, + ensure_file_permissions, +) from monkey_island.cc.setup.island_config_options import IslandConfigOptions logger = logging.getLogger(__name__) @@ -12,15 +13,9 @@ def setup_certificate(config_options: IslandConfigOptions) -> (str, str): crt_path = config_options.crt_path key_path = config_options.key_path - # check paths for file in [crt_path, key_path]: - if not os.path.exists(file): - raise FileNotFoundError(f"File not found at {file}. Exiting.") - - if not has_sufficient_permissions(path=file, required_permissions="0o400"): - raise InsecurePermissionsError( - f"{file} has insecure permissions. Required permissions: r--------. Exiting." - ) + ensure_file_existence(file) + ensure_file_permissions(file) logger.info(f"Using certificate path: {crt_path}, and key path: {key_path}.")