From c4d7bf7030b8d71f89311433a7c1b8258d25f2b9 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Fri, 19 Nov 2021 08:23:56 -0500 Subject: [PATCH] Island: replace get_island_port() with constant --- monkey/monkey_island/cc/environment/__init__.py | 4 ---- monkey/monkey_island/cc/server_setup.py | 11 ++++------- .../cc/server_utils/bootloader_server.py | 4 ++-- monkey/monkey_island/cc/server_utils/consts.py | 2 ++ monkey/monkey_island/cc/services/config.py | 6 +++--- monkey/monkey_island/cc/services/run_local_monkey.py | 5 ++--- .../unit_tests/monkey_island/cc/services/conftest.py | 2 -- .../monkey_island/cc/services/test_config.py | 7 ++----- 8 files changed, 15 insertions(+), 26 deletions(-) diff --git a/monkey/monkey_island/cc/environment/__init__.py b/monkey/monkey_island/cc/environment/__init__.py index 45462af7d..f38ed54d4 100644 --- a/monkey/monkey_island/cc/environment/__init__.py +++ b/monkey/monkey_island/cc/environment/__init__.py @@ -12,7 +12,6 @@ logger = logging.getLogger(__name__) class Environment(object, metaclass=ABCMeta): - _ISLAND_PORT = 5000 _DEBUG_SERVER = False _AUTH_EXPIRATION_TIME = timedelta(minutes=30) @@ -33,9 +32,6 @@ class Environment(object, metaclass=ABCMeta): def get_config(self) -> EnvironmentConfig: return self._config - def get_island_port(self): - return self._ISLAND_PORT - def is_debug(self): return self._DEBUG_SERVER diff --git a/monkey/monkey_island/cc/server_setup.py b/monkey/monkey_island/cc/server_setup.py index c6dc9c0b9..62afdd0e2 100644 --- a/monkey/monkey_island/cc/server_setup.py +++ b/monkey/monkey_island/cc/server_setup.py @@ -10,6 +10,8 @@ from typing import Tuple import gevent.hub from gevent.pywsgi import WSGIServer +from monkey_island.cc.server_utils.consts import ISLAND_PORT + # Add the monkey_island directory to the path, to make sure imports that don't start with # "monkey_island." work. MONKEY_ISLAND_DIR_BASE_PATH = str(Path(__file__).parent.parent) @@ -152,7 +154,7 @@ def _start_island_server(should_setup_only, config_options: IslandConfigOptions) ) else: http_server = WSGIServer( - ("0.0.0.0", env_singleton.env.get_island_port()), + ("0.0.0.0", ISLAND_PORT), app, certfile=config_options.crt_path, keyfile=config_options.key_path, @@ -178,12 +180,7 @@ def _log_init_info(): logger.info(f"version: {get_version()}") logger.info( "Listening on the following URLs: {}".format( - ", ".join( - [ - "https://{}:{}".format(x, env_singleton.env.get_island_port()) - for x in local_ip_addresses() - ] - ) + ", ".join(["https://{}:{}".format(x, ISLAND_PORT) for x in local_ip_addresses()]) ) ) MonkeyDownload.log_executable_hashes() diff --git a/monkey/monkey_island/cc/server_utils/bootloader_server.py b/monkey/monkey_island/cc/server_utils/bootloader_server.py index bfdd42cf2..fa00fbd24 100644 --- a/monkey/monkey_island/cc/server_utils/bootloader_server.py +++ b/monkey/monkey_island/cc/server_utils/bootloader_server.py @@ -7,7 +7,7 @@ import requests import urllib3 from common.common_consts.timeouts import SHORT_REQUEST_TIMEOUT -from monkey_island.cc.environment import Environment +from monkey_island.cc.server_utils.consts import ISLAND_PORT # Disable "unverified certificate" warnings when sending requests to island urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) # noqa: DUO131 @@ -49,4 +49,4 @@ class BootloaderHTTPRequestHandler(BaseHTTPRequestHandler): @staticmethod def get_bootloader_resource_url(server_ip): - return "https://" + server_ip + ":" + str(Environment._ISLAND_PORT) + "/api/bootloader/" + return "https://" + server_ip + ":" + str(ISLAND_PORT) + "/api/bootloader/" diff --git a/monkey/monkey_island/cc/server_utils/consts.py b/monkey/monkey_island/cc/server_utils/consts.py index 30749cb3e..ab9261140 100644 --- a/monkey/monkey_island/cc/server_utils/consts.py +++ b/monkey/monkey_island/cc/server_utils/consts.py @@ -51,3 +51,5 @@ DEFAULT_CERTIFICATE_PATHS = { } GEVENT_EXCEPTION_LOG = "gevent_exceptions.log" + +ISLAND_PORT = 5000 diff --git a/monkey/monkey_island/cc/services/config.py b/monkey/monkey_island/cc/services/config.py index 6ddcd896f..0214a957e 100644 --- a/monkey/monkey_island/cc/services/config.py +++ b/monkey/monkey_island/cc/services/config.py @@ -5,7 +5,6 @@ import logging from jsonschema import Draft4Validator, validators -import monkey_island.cc.environment.environment_singleton as env_singleton from common.config_value_paths import ( AWS_KEYS_PATH, EXPORT_MONKEY_TELEMS_PATH, @@ -19,6 +18,7 @@ from common.config_value_paths import ( USER_LIST_PATH, ) from monkey_island.cc.database import mongo +from monkey_island.cc.server_utils.consts import ISLAND_PORT from monkey_island.cc.server_utils.encryption import get_datastore_encryptor from monkey_island.cc.services.config_manipulator import update_config_per_mode from monkey_island.cc.services.config_schema.config_schema import SCHEMA @@ -264,11 +264,11 @@ class ConfigService: def set_server_ips_in_config(config): ips = local_ip_addresses() config["internal"]["island_server"]["command_servers"] = [ - "%s:%d" % (ip, env_singleton.env.get_island_port()) for ip in ips + "%s:%d" % (ip, ISLAND_PORT) for ip in ips ] config["internal"]["island_server"]["current_server"] = "%s:%d" % ( ips[0], - env_singleton.env.get_island_port(), + ISLAND_PORT, ) @staticmethod diff --git a/monkey/monkey_island/cc/services/run_local_monkey.py b/monkey/monkey_island/cc/services/run_local_monkey.py index e7e18045a..ce6c98c61 100644 --- a/monkey/monkey_island/cc/services/run_local_monkey.py +++ b/monkey/monkey_island/cc/services/run_local_monkey.py @@ -5,9 +5,8 @@ import stat import subprocess from shutil import copyfile -import monkey_island.cc.environment.environment_singleton as env_singleton from monkey_island.cc.resources.monkey_download import get_monkey_executable -from monkey_island.cc.server_utils.consts import MONKEY_ISLAND_ABS_PATH +from monkey_island.cc.server_utils.consts import ISLAND_PORT, MONKEY_ISLAND_ABS_PATH from monkey_island.cc.services.utils.network_utils import local_ip_addresses logger = logging.getLogger(__name__) @@ -45,7 +44,7 @@ class LocalMonkeyRunService: # run the monkey try: ip = local_ip_addresses()[0] - port = env_singleton.env.get_island_port() + port = ISLAND_PORT args = [dest_path, "m0nk3y", "-s", f"{ip}:{port}"] subprocess.Popen(args, cwd=LocalMonkeyRunService.DATA_DIR) diff --git a/monkey/tests/unit_tests/monkey_island/cc/services/conftest.py b/monkey/tests/unit_tests/monkey_island/cc/services/conftest.py index bd0744f17..213dbaefc 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/services/conftest.py +++ b/monkey/tests/unit_tests/monkey_island/cc/services/conftest.py @@ -1,6 +1,5 @@ import pytest -from monkey_island.cc.environment import Environment from monkey_island.cc.services.config import ConfigService @@ -17,7 +16,6 @@ def PORT(): @pytest.fixture def config(monkeypatch, IPS, PORT): monkeypatch.setattr("monkey_island.cc.services.config.local_ip_addresses", lambda: IPS) - monkeypatch.setattr(Environment, "_ISLAND_PORT", PORT) config = ConfigService.get_default_config(True) return config diff --git a/monkey/tests/unit_tests/monkey_island/cc/services/test_config.py b/monkey/tests/unit_tests/monkey_island/cc/services/test_config.py index 75b3152e5..751ca98ed 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/services/test_config.py +++ b/monkey/tests/unit_tests/monkey_island/cc/services/test_config.py @@ -11,11 +11,8 @@ class MockClass: @pytest.fixture(scope="function", autouse=True) -def mock_port_in_env_singleton(monkeypatch, PORT): - mock_singleton = MockClass() - mock_singleton.env = MockClass() - mock_singleton.env.get_island_port = lambda: PORT - monkeypatch.setattr("monkey_island.cc.services.config.env_singleton", mock_singleton) +def mock_port(monkeypatch, PORT): + monkeypatch.setattr("monkey_island.cc.services.config.ISLAND_PORT", PORT) @pytest.mark.usefixtures("uses_encryptor")