Merge pull request #2160 from guardicore/2109-add-version-object-to-di

2109 add version object to di
This commit is contained in:
Mike Salvatore 2022-08-03 14:35:45 -04:00 committed by GitHub
commit c57e865192
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 9 deletions

View File

@ -9,7 +9,7 @@ PATCH = "0"
build_file_path = Path(__file__).parent.joinpath("BUILD") build_file_path = Path(__file__).parent.joinpath("BUILD")
with open(build_file_path, "r") as build_file: with open(build_file_path, "r") as build_file:
BUILD = build_file.read() BUILD = build_file.read().strip()
def get_version(build=BUILD): def get_version(build=BUILD):

View File

@ -1,8 +1,7 @@
import logging import logging
from common.version import get_version
from monkey_island.cc.resources.AbstractResource import AbstractResource from monkey_island.cc.resources.AbstractResource import AbstractResource
from monkey_island.cc.services.version_update import VersionUpdateService from monkey_island.cc.version import Version
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -10,14 +9,14 @@ logger = logging.getLogger(__name__)
class Version(AbstractResource): class Version(AbstractResource):
urls = ["/api/island/version"] urls = ["/api/island/version"]
def __init__(self): def __init__(self, version: Version):
super(Version, self).__init__() self._version = version
# We don't secure this since it doesn't give out any private info and we want UI to know version # We don't secure this since it doesn't give out any private info and we want UI to know version
# even when not authenticated # even when not authenticated
def get(self): def get(self):
return { return {
"current_version": get_version(), "version_number": self._version.version_number,
"newer_version": VersionUpdateService.get_newer_version(), "latest_version": self._version.latest_version,
"download_link": VersionUpdateService.get_download_link(), "download_link": self._version.download_url,
} }

View File

@ -1,3 +1,4 @@
import json
import logging import logging
from pathlib import Path from pathlib import Path
@ -12,6 +13,8 @@ from common.agent_configuration import (
from common.aws import AWSInstance from common.aws import AWSInstance
from common.common_consts.telem_categories import TelemCategoryEnum from common.common_consts.telem_categories import TelemCategoryEnum
from common.utils.file_utils import get_binary_io_sha256_hash from common.utils.file_utils import get_binary_io_sha256_hash
from common.version import get_version
from monkey_island.cc.deployment import Deployment
from monkey_island.cc.repository import ( from monkey_island.cc.repository import (
AgentBinaryRepository, AgentBinaryRepository,
FileAgentConfigurationRepository, FileAgentConfigurationRepository,
@ -43,6 +46,7 @@ from monkey_island.cc.services.telemetry.processing.processing import (
TELEMETRY_CATEGORY_TO_PROCESSING_FUNC, TELEMETRY_CATEGORY_TO_PROCESSING_FUNC,
) )
from monkey_island.cc.setup.mongo.mongo_setup import MONGO_URL from monkey_island.cc.setup.mongo.mongo_setup import MONGO_URL
from monkey_island.cc.version import Version
from . import AuthenticationService from . import AuthenticationService
from .reporting.report import ReportService from .reporting.report import ReportService
@ -50,6 +54,7 @@ from .reporting.report import ReportService
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
AGENT_BINARIES_PATH = Path(MONKEY_ISLAND_ABS_PATH) / "cc" / "binaries" AGENT_BINARIES_PATH = Path(MONKEY_ISLAND_ABS_PATH) / "cc" / "binaries"
DEPLOYMENT_FILE_PATH = Path(MONKEY_ISLAND_ABS_PATH) / "cc" / "deployment.json"
REPOSITORY_KEY_FILE_NAME = "repository_key.bin" REPOSITORY_KEY_FILE_NAME = "repository_key.bin"
@ -57,12 +62,13 @@ def initialize_services(data_dir: Path) -> DIContainer:
container = DIContainer() container = DIContainer()
_register_conventions(container, data_dir) _register_conventions(container, data_dir)
container.register_instance(Deployment, _get_depyloyment_from_file(DEPLOYMENT_FILE_PATH))
container.register_instance(AWSInstance, AWSInstance()) container.register_instance(AWSInstance, AWSInstance())
container.register_instance(MongoClient, MongoClient(MONGO_URL, serverSelectionTimeoutMS=100)) container.register_instance(MongoClient, MongoClient(MONGO_URL, serverSelectionTimeoutMS=100))
container.register_instance( container.register_instance(
ILockableEncryptor, RepositoryEncryptor(data_dir / REPOSITORY_KEY_FILE_NAME) ILockableEncryptor, RepositoryEncryptor(data_dir / REPOSITORY_KEY_FILE_NAME)
) )
container.register_instance(Version, container.resolve(Version))
_register_repositories(container, data_dir) _register_repositories(container, data_dir)
_register_services(container) _register_services(container)
@ -89,6 +95,7 @@ def _register_conventions(container: DIContainer, data_dir: Path):
DEFAULT_RANSOMWARE_AGENT_CONFIGURATION, DEFAULT_RANSOMWARE_AGENT_CONFIGURATION,
) )
container.register_convention(Path, "island_log_file_path", get_log_file_path(data_dir)) container.register_convention(Path, "island_log_file_path", get_log_file_path(data_dir))
container.register_convention(str, "version_number", get_version())
def _register_repositories(container: DIContainer, data_dir: Path): def _register_repositories(container: DIContainer, data_dir: Path):
@ -146,6 +153,22 @@ def _log_agent_binary_hashes(agent_binary_repository: IAgentBinaryRepository):
logger.info(f"{os} agent: SHA-256 hash: {binary_sha256_hash}") logger.info(f"{os} agent: SHA-256 hash: {binary_sha256_hash}")
# TODO: The deployment should probably be passed into initialize_services(), but we can rework that
# when we refactor this file.
def _get_depyloyment_from_file(file_path: Path) -> Deployment:
try:
with open(file_path, "r") as deployment_info_file:
deployment_info = json.load(deployment_info_file)
return Deployment[deployment_info["deployment"].upper()]
except KeyError as err:
raise Exception(
f"The deployment file ({file_path}) did not contain the expected data: "
f"missing key {err}"
)
except Exception as err:
raise Exception(f"Failed to fetch the deployment from {file_path}: {err}")
def _register_services(container: DIContainer): def _register_services(container: DIContainer):
container.register_instance(AWSService, container.resolve(AWSService)) container.register_instance(AWSService, container.resolve(AWSService))
container.register_instance(LocalMonkeyRunService, container.resolve(LocalMonkeyRunService)) container.register_instance(LocalMonkeyRunService, container.resolve(LocalMonkeyRunService))