Island: Raise RetrievalError from IAgentBinaryRepository

This commit is contained in:
Mike Salvatore 2022-06-21 09:18:42 -04:00
parent 22b22c5f0a
commit 47df257545
6 changed files with 14 additions and 19 deletions

View File

@ -1,6 +1,6 @@
from .errors import RetrievalError from .errors import RetrievalError
from .file_storage import FileNotFoundError, IFileRepository, LocalStorageFileRepository from .file_storage import FileNotFoundError, IFileRepository, LocalStorageFileRepository
from .i_agent_binary_repository import IAgentBinaryRepository, AgentRetrievalError from .i_agent_binary_repository import IAgentBinaryRepository
from .agent_binary_repository import AgentBinaryRepository from .agent_binary_repository import AgentBinaryRepository
from .i_agent_configuration_repository import IAgentConfigurationRepository from .i_agent_configuration_repository import IAgentConfigurationRepository
from .file_agent_configuration_repository import FileAgentConfigurationRepository from .file_agent_configuration_repository import FileAgentConfigurationRepository

View File

@ -1,8 +1,6 @@
from typing import BinaryIO from typing import BinaryIO
from monkey_island.cc import repository from . import IAgentBinaryRepository, IFileRepository, RetrievalError
from . import AgentRetrievalError, IAgentBinaryRepository, IFileRepository
LINUX_AGENT_FILE_NAME = "monkey-linux-64" LINUX_AGENT_FILE_NAME = "monkey-linux-64"
WINDOWS_AGENT_FILE_NAME = "monkey-windows-64.exe" WINDOWS_AGENT_FILE_NAME = "monkey-windows-64.exe"
@ -22,9 +20,8 @@ class AgentBinaryRepository(IAgentBinaryRepository):
try: try:
agent_binary = self._file_repository.open_file(filename) agent_binary = self._file_repository.open_file(filename)
return agent_binary return agent_binary
# TODO: Reevaluate this except Exception as err:
except repository.FileNotFoundError as err: raise RetrievalError(
raise AgentRetrievalError(
f"An error occurred while retrieving the {filename}" f"An error occurred while retrieving the {filename}"
f" agent binary from {self._file_repository}: {err}" f" agent binary from {self._file_repository}: {err}"
) )

View File

@ -2,10 +2,6 @@ import abc
from typing import BinaryIO from typing import BinaryIO
class AgentRetrievalError(IOError):
pass
class IAgentBinaryRepository(metaclass=abc.ABCMeta): class IAgentBinaryRepository(metaclass=abc.ABCMeta):
""" """
A repository that retrieves the agent binaries A repository that retrieves the agent binaries
@ -17,6 +13,7 @@ class IAgentBinaryRepository(metaclass=abc.ABCMeta):
Retrieve linux agent binary Retrieve linux agent binary
:return: A file-like object that represents the linux agent binary :return: A file-like object that represents the linux agent binary
:raises RetrievalError: If the agent binary could not be retrieved
""" """
@abc.abstractmethod @abc.abstractmethod
@ -25,4 +22,5 @@ class IAgentBinaryRepository(metaclass=abc.ABCMeta):
Retrieve windows agent binary Retrieve windows agent binary
:return: A file-like object that represents the windows agent binary :return: A file-like object that represents the windows agent binary
:raises RetrievalError: If the agent binary could not be retrieved
""" """

View File

@ -2,7 +2,7 @@ import logging
from flask import make_response, send_file from flask import make_response, send_file
from monkey_island.cc.repository import AgentRetrievalError, IAgentBinaryRepository from monkey_island.cc.repository import IAgentBinaryRepository, RetrievalError
from monkey_island.cc.resources.AbstractResource import AbstractResource from monkey_island.cc.resources.AbstractResource import AbstractResource
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -31,10 +31,10 @@ class AgentBinaries(AbstractResource):
file = agent_binaries[os]() file = agent_binaries[os]()
return send_file(file, mimetype="application/octet-stream") return send_file(file, mimetype="application/octet-stream")
except AgentRetrievalError as err:
logger.error(err)
return make_response({"error": str(err)}, 500)
except KeyError as err: except KeyError as err:
error_msg = f'No Agents are available for unsupported operating system "{os}": {err}' error_msg = f'No Agents are available for unsupported operating system "{os}": {err}'
logger.error(error_msg) logger.error(error_msg)
return make_response({"error": error_msg}, 404) return make_response({"error": error_msg}, 404)
except RetrievalError as err:
logger.error(err)
return make_response({"error": str(err)}, 500)

View File

@ -6,12 +6,12 @@ from common.aws import AWSInstance
from common.utils.file_utils import get_binary_io_sha256_hash from common.utils.file_utils import get_binary_io_sha256_hash
from monkey_island.cc.repository import ( from monkey_island.cc.repository import (
AgentBinaryRepository, AgentBinaryRepository,
AgentRetrievalError,
FileAgentConfigurationRepository, FileAgentConfigurationRepository,
IAgentBinaryRepository, IAgentBinaryRepository,
IAgentConfigurationRepository, IAgentConfigurationRepository,
IFileRepository, IFileRepository,
LocalStorageFileRepository, LocalStorageFileRepository,
RetrievalError,
) )
from monkey_island.cc.server_utils.consts import MONKEY_ISLAND_ABS_PATH from monkey_island.cc.server_utils.consts import MONKEY_ISLAND_ABS_PATH
from monkey_island.cc.services import AWSService from monkey_island.cc.services import AWSService
@ -76,7 +76,7 @@ def _log_agent_binary_hashes(agent_binary_repository: IAgentBinaryRepository):
agent_binary = get_agent_binary() agent_binary = get_agent_binary()
binary_sha256_hash = get_binary_io_sha256_hash(agent_binary) binary_sha256_hash = get_binary_io_sha256_hash(agent_binary)
agent_hashes[os] = binary_sha256_hash agent_hashes[os] = binary_sha256_hash
except AgentRetrievalError as err: except RetrievalError as err:
logger.error(f"No agent available for {os}: {err}") logger.error(f"No agent available for {os}: {err}")
for os, binary_sha256_hash in agent_hashes.items(): for os, binary_sha256_hash in agent_hashes.items():

View File

@ -5,7 +5,7 @@ import subprocess
from pathlib import Path from pathlib import Path
from shutil import copyfileobj from shutil import copyfileobj
from monkey_island.cc.repository import AgentRetrievalError, IAgentBinaryRepository from monkey_island.cc.repository import IAgentBinaryRepository, RetrievalError
from monkey_island.cc.server_utils.consts import ISLAND_PORT from monkey_island.cc.server_utils.consts import ISLAND_PORT
from monkey_island.cc.services.utils.network_utils import local_ip_addresses from monkey_island.cc.services.utils.network_utils import local_ip_addresses
@ -29,7 +29,7 @@ class LocalMonkeyRunService:
} }
agent_binary = agents[platform.system().lower()]() agent_binary = agents[platform.system().lower()]()
except AgentRetrievalError as err: except RetrievalError as err:
logger.error( logger.error(
f"No Agent can be retrieved for the specified operating system" f"No Agent can be retrieved for the specified operating system"
f'"{operating_system}"' f'"{operating_system}"'