forked from p15670423/monkey
Island, UT: Fix file_utils paths
This commit is contained in:
parent
8d65fa36f2
commit
86c6a55097
|
@ -3,6 +3,7 @@ import os
|
|||
import platform
|
||||
import stat
|
||||
from contextlib import contextmanager
|
||||
from pathlib import Path
|
||||
from typing import Generator
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
@ -20,32 +21,32 @@ if is_windows_os():
|
|||
import monkey_island.cc.server_utils.windows_permissions as windows_permissions
|
||||
|
||||
|
||||
def create_secure_directory(path: str):
|
||||
if not os.path.isdir(path):
|
||||
def create_secure_directory(path: Path):
|
||||
if not path.is_dir():
|
||||
if is_windows_os():
|
||||
_create_secure_directory_windows(path)
|
||||
else:
|
||||
_create_secure_directory_linux(path)
|
||||
|
||||
|
||||
def _create_secure_directory_linux(path: str):
|
||||
def _create_secure_directory_linux(path: Path):
|
||||
try:
|
||||
# Don't split directory creation and permission setting
|
||||
# because it will temporarily create an accessible directory which anyone can use.
|
||||
os.mkdir(path, mode=stat.S_IRWXU)
|
||||
path.mkdir(mode=stat.S_IRWXU)
|
||||
|
||||
except Exception as ex:
|
||||
logger.error(f'Could not create a directory at "{path}": {str(ex)}')
|
||||
raise ex
|
||||
|
||||
|
||||
def _create_secure_directory_windows(path: str):
|
||||
def _create_secure_directory_windows(path: Path):
|
||||
try:
|
||||
security_attributes = win32security.SECURITY_ATTRIBUTES()
|
||||
security_attributes.SECURITY_DESCRIPTOR = (
|
||||
windows_permissions.get_security_descriptor_for_owner_only_perms()
|
||||
)
|
||||
win32file.CreateDirectory(path, security_attributes)
|
||||
win32file.CreateDirectory(str(path), security_attributes)
|
||||
|
||||
except Exception as ex:
|
||||
logger.error(f'Could not create a directory at "{path}": {str(ex)}')
|
||||
|
|
|
@ -20,7 +20,7 @@ def setup_data_dir(data_dir_path: Path):
|
|||
if _is_data_dir_old(data_dir_path):
|
||||
logger.info("Version in data directory does not match the Island's version.")
|
||||
_handle_old_data_directory(data_dir_path)
|
||||
create_secure_directory(str(data_dir_path))
|
||||
create_secure_directory(data_dir_path)
|
||||
write_version(data_dir_path)
|
||||
logger.info(f"Data directory set up in {data_dir_path}.")
|
||||
|
||||
|
|
|
@ -31,12 +31,12 @@ def start_mongodb(data_dir: Path) -> MongoDbProcess:
|
|||
return mongo_db_process
|
||||
|
||||
|
||||
def _create_db_dir(db_dir_parent_path) -> str:
|
||||
db_dir = os.path.join(db_dir_parent_path, DB_DIR_NAME)
|
||||
def _create_db_dir(db_dir_parent_path: Path) -> str:
|
||||
db_dir = db_dir_parent_path / DB_DIR_NAME
|
||||
logger.info(f"Database content directory: {db_dir}.")
|
||||
|
||||
create_secure_directory(db_dir)
|
||||
return db_dir
|
||||
return str(db_dir)
|
||||
|
||||
|
||||
def register_mongo_shutdown_callback(mongo_db_process: MongoDbProcess):
|
||||
|
|
|
@ -12,22 +12,22 @@ from monkey_island.cc.server_utils.file_utils import (
|
|||
|
||||
|
||||
@pytest.fixture
|
||||
def test_path_nested(tmpdir):
|
||||
path = os.path.join(tmpdir, "test1", "test2", "test3")
|
||||
def test_path_nested(tmp_path):
|
||||
path = tmp_path / "test1" / "test2" / "test3"
|
||||
return path
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def test_path(tmpdir):
|
||||
def test_path(tmp_path):
|
||||
test_path = "test1"
|
||||
path = os.path.join(tmpdir, test_path)
|
||||
path = tmp_path / test_path
|
||||
|
||||
return path
|
||||
|
||||
|
||||
def test_create_secure_directory__already_exists(test_path):
|
||||
os.mkdir(test_path)
|
||||
assert os.path.isdir(test_path)
|
||||
test_path.mkdir()
|
||||
assert test_path.is_dir()
|
||||
create_secure_directory(test_path)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue