From d4e42cbe126df436c428a0acf56dcfc58e014545 Mon Sep 17 00:00:00 2001 From: itay Date: Sun, 14 Apr 2019 14:17:18 +0300 Subject: [PATCH] all monkey_island references are now absolute --- monkey/monkey_island/cc/app.py | 3 ++- monkey/monkey_island/cc/consts.py | 5 +++++ monkey/monkey_island/cc/encryptor.py | 4 +++- monkey/monkey_island/cc/environment/environment.py | 4 +++- monkey/monkey_island/cc/main.py | 13 +++++++++---- monkey/monkey_island/cc/resources/local_run.py | 6 ++++-- .../monkey_island/cc/resources/monkey_download.py | 6 ++++-- 7 files changed, 30 insertions(+), 11 deletions(-) create mode 100644 monkey/monkey_island/cc/consts.py diff --git a/monkey/monkey_island/cc/app.py b/monkey/monkey_island/cc/app.py index a05236d17..22b65c8b2 100644 --- a/monkey/monkey_island/cc/app.py +++ b/monkey/monkey_island/cc/app.py @@ -28,6 +28,7 @@ from monkey_island.cc.resources.root import Root from monkey_island.cc.resources.telemetry import Telemetry from monkey_island.cc.resources.telemetry_feed import TelemetryFeed from monkey_island.cc.services.config import ConfigService +from monkey_island.cc.consts import MONKEY_ISLAND_ABS_PATH __author__ = 'Barak' @@ -39,7 +40,7 @@ def serve_static_file(static_path): if static_path.startswith('api/'): raise NotFound() try: - return send_from_directory(os.path.join(os.getcwd(), 'monkey_island/cc/ui/dist'), static_path) + return send_from_directory(os.path.join(MONKEY_ISLAND_ABS_PATH, 'cc/ui/dist'), static_path) except NotFound: # Because react uses various urls for same index page, this is probably the user's intention. if static_path == HOME_FILE: diff --git a/monkey/monkey_island/cc/consts.py b/monkey/monkey_island/cc/consts.py new file mode 100644 index 000000000..deb1db449 --- /dev/null +++ b/monkey/monkey_island/cc/consts.py @@ -0,0 +1,5 @@ +import os + +__author__ = 'itay.mizeretz' + +MONKEY_ISLAND_ABS_PATH = os.path.join(os.getcwd(), 'monkey_island') diff --git a/monkey/monkey_island/cc/encryptor.py b/monkey/monkey_island/cc/encryptor.py index 84e6fba9d..98473b60a 100644 --- a/monkey/monkey_island/cc/encryptor.py +++ b/monkey/monkey_island/cc/encryptor.py @@ -4,12 +4,14 @@ import os from Crypto import Random from Crypto.Cipher import AES +from monkey_island.cc.consts import MONKEY_ISLAND_ABS_PATH + __author__ = "itay.mizeretz" class Encryptor: _BLOCK_SIZE = 32 - _DB_PASSWORD_FILENAME = "monkey_island/cc/mongo_key.bin" + _DB_PASSWORD_FILENAME = os.path.join(MONKEY_ISLAND_ABS_PATH, 'cc/mongo_key.bin') def __init__(self): self._load_key() diff --git a/monkey/monkey_island/cc/environment/environment.py b/monkey/monkey_island/cc/environment/environment.py index 5707eb40a..3cd6bb587 100644 --- a/monkey/monkey_island/cc/environment/environment.py +++ b/monkey/monkey_island/cc/environment/environment.py @@ -1,9 +1,11 @@ import json import logging +import os from monkey_island.cc.environment import standard from monkey_island.cc.environment import aws from monkey_island.cc.environment import password +from monkey_island.cc.consts import MONKEY_ISLAND_ABS_PATH __author__ = 'itay.mizeretz' @@ -21,7 +23,7 @@ ENV_DICT = { def load_server_configuration_from_file(): - with open('monkey_island/cc/server_config.json', 'r') as f: + with open(os.path.join(MONKEY_ISLAND_ABS_PATH, 'cc/server_config.json'), 'r') as f: config_content = f.read() return json.loads(config_content) diff --git a/monkey/monkey_island/cc/main.py b/monkey/monkey_island/cc/main.py index 9205c91ed..412c3c399 100644 --- a/monkey/monkey_island/cc/main.py +++ b/monkey/monkey_island/cc/main.py @@ -12,8 +12,9 @@ if BASE_PATH not in sys.path: sys.path.insert(0, BASE_PATH) from monkey_island.cc.island_logger import json_setup_logging +from monkey_island.cc.consts import MONKEY_ISLAND_ABS_PATH # This is here in order to catch EVERYTHING, some functions are being called on imports the log init needs to be on top. -json_setup_logging(default_path=os.path.join(BASE_PATH, 'cc', 'island_logger_default_config.json'), +json_setup_logging(default_path=os.path.join(MONKEY_ISLAND_ABS_PATH, 'cc', 'island_logger_default_config.json'), default_level=logging.DEBUG) logger = logging.getLogger(__name__) @@ -37,12 +38,16 @@ def main(): populate_exporter_list() app = init_app(mongo_url) + + crt_path = os.path.join(MONKEY_ISLAND_ABS_PATH, 'cc', 'server.crt') + key_path = os.path.join(MONKEY_ISLAND_ABS_PATH, 'cc', 'server.key') + if env.is_debug(): - app.run(host='0.0.0.0', debug=True, ssl_context=('monkey_island/cc/server.crt', 'monkey_island/cc/server.key')) + app.run(host='0.0.0.0', debug=True, ssl_context=(crt_path, key_path)) else: http_server = HTTPServer(WSGIContainer(app), - ssl_options={'certfile': os.environ.get('SERVER_CRT', 'monkey_island/cc/server.crt'), - 'keyfile': os.environ.get('SERVER_KEY', 'monkey_island/cc/server.key')}) + ssl_options={'certfile': os.environ.get('SERVER_CRT', crt_path), + 'keyfile': os.environ.get('SERVER_KEY', key_path)}) http_server.listen(env.get_island_port()) logger.info( 'Monkey Island Server is running on https://{}:{}'.format(local_ip_addresses()[0], env.get_island_port())) diff --git a/monkey/monkey_island/cc/resources/local_run.py b/monkey/monkey_island/cc/resources/local_run.py index 7f636ec2a..d402a440c 100644 --- a/monkey/monkey_island/cc/resources/local_run.py +++ b/monkey/monkey_island/cc/resources/local_run.py @@ -10,12 +10,14 @@ from monkey_island.cc.environment.environment import env from monkey_island.cc.resources.monkey_download import get_monkey_executable from monkey_island.cc.services.node import NodeService from monkey_island.cc.utils import local_ip_addresses +from monkey_island.cc.consts import MONKEY_ISLAND_ABS_PATH __author__ = 'Barak' import logging logger = logging.getLogger(__name__) + def run_local_monkey(): import platform import subprocess @@ -26,8 +28,8 @@ def run_local_monkey(): if not result: return False, "OS Type not found" - monkey_path = os.path.join(os.getcwd(), 'monkey_island', 'cc', 'binaries', result['filename']) - target_path = os.path.join(os.getcwd(), 'monkey_island', result['filename']) + monkey_path = os.path.join(MONKEY_ISLAND_ABS_PATH, 'cc', 'binaries', result['filename']) + target_path = os.path.join(MONKEY_ISLAND_ABS_PATH, result['filename']) # copy the executable to temp path (don't run the monkey from its current location as it may delete itself) try: diff --git a/monkey/monkey_island/cc/resources/monkey_download.py b/monkey/monkey_island/cc/resources/monkey_download.py index 305d8c6e9..78a092a26 100644 --- a/monkey/monkey_island/cc/resources/monkey_download.py +++ b/monkey/monkey_island/cc/resources/monkey_download.py @@ -5,6 +5,8 @@ import os import flask_restful from flask import request, send_from_directory +from monkey_island.cc.consts import MONKEY_ISLAND_ABS_PATH + __author__ = 'Barak' logger = logging.getLogger(__name__) @@ -70,7 +72,7 @@ class MonkeyDownload(flask_restful.Resource): # Used by monkey. can't secure. def get(self, path): - return send_from_directory('binaries', path) + return send_from_directory(os.path.join(MONKEY_ISLAND_ABS_PATH, 'cc', 'binaries'), path) # Used by monkey. can't secure. def post(self): @@ -81,7 +83,7 @@ class MonkeyDownload(flask_restful.Resource): if result: # change resulting from new base path - real_path = os.path.join("monkey_island", "cc", 'binaries', result['filename']) + real_path = os.path.join(MONKEY_ISLAND_ABS_PATH, "cc", 'binaries', result['filename']) if os.path.isfile(real_path): result['size'] = os.path.getsize(real_path) return result