island: Pass data_dir to LocalRun instead of using global singleton

This commit is contained in:
Mike Salvatore 2021-05-11 06:18:43 -04:00
parent 0b21dac261
commit f048cf313c
3 changed files with 20 additions and 11 deletions

View File

@ -117,14 +117,19 @@ def init_app_url_rules(app):
app.add_url_rule("/<path:static_path>", "serve_static_file", serve_static_file)
def init_api_resources(api):
def init_api_resources(api, data_dir):
api.add_resource(Root, "/api")
api.add_resource(Registration, "/api/registration")
api.add_resource(Authenticate, "/api/auth")
api.add_resource(Environment, "/api/environment")
api.add_resource(Monkey, "/api/monkey", "/api/monkey/", "/api/monkey/<string:guid>")
api.add_resource(Bootloader, "/api/bootloader/<string:os>")
api.add_resource(LocalRun, "/api/local-monkey", "/api/local-monkey/")
api.add_resource(
LocalRun,
"/api/local-monkey",
"/api/local-monkey/",
resource_class_kwargs={"data_dir": data_dir},
)
api.add_resource(ClientRun, "/api/client-monkey", "/api/client-monkey/")
api.add_resource(
Telemetry, "/api/telemetry", "/api/telemetry/", "/api/telemetry/<string:monkey_guid>"
@ -173,7 +178,7 @@ def init_api_resources(api):
api.add_resource(TelemetryBlackboxEndpoint, "/api/test/telemetry")
def init_app(mongo_url):
def init_app(mongo_url, data_dir):
app = Flask(__name__)
api = flask_restful.Api(app)
@ -182,6 +187,6 @@ def init_app(mongo_url):
init_app_config(app, mongo_url)
init_app_services(app)
init_app_url_rules(app)
init_api_resources(api)
init_api_resources(api, data_dir)
return app

View File

@ -35,8 +35,10 @@ MINIMUM_MONGO_DB_VERSION_REQUIRED = "4.2.0"
def main(should_setup_only=False, server_config_filename=DEFAULT_SERVER_CONFIG_PATH):
logger.info("Starting bootloader server")
data_dir = env_singleton.env.get_config().data_dir_abs_path
env_singleton.initialize_from_file(server_config_filename)
initialize_encryptor(env_singleton.env.get_config().data_dir_abs_path)
initialize_encryptor(data_dir)
mongo_url = os.environ.get("MONGO_URL", env_singleton.env.get_mongo_url())
bootloader_server_thread = Thread(
@ -44,17 +46,17 @@ def main(should_setup_only=False, server_config_filename=DEFAULT_SERVER_CONFIG_P
)
bootloader_server_thread.start()
start_island_server(should_setup_only)
start_island_server(should_setup_only, data_dir)
bootloader_server_thread.join()
def start_island_server(should_setup_only):
def start_island_server(should_setup_only, data_dir):
mongo_url = os.environ.get("MONGO_URL", env_singleton.env.get_mongo_url())
wait_for_mongo_db_server(mongo_url)
assert_mongo_db_version(mongo_url)
populate_exporter_list()
app = init_app(mongo_url)
app = init_app(mongo_url, data_dir)
crt_path = str(Path(MONKEY_ISLAND_ABS_PATH, "cc", "server.crt"))
key_path = str(Path(MONKEY_ISLAND_ABS_PATH, "cc", "server.key"))

View File

@ -20,7 +20,7 @@ __author__ = "Barak"
logger = logging.getLogger(__name__)
def run_local_monkey():
def run_local_monkey(dest_dir):
import platform
import stat
import subprocess
@ -31,7 +31,6 @@ def run_local_monkey():
return False, "OS Type not found"
src_path = os.path.join(MONKEY_ISLAND_ABS_PATH, "cc", "binaries", result["filename"])
dest_dir = env_singleton.env.get_config().data_dir_abs_path
dest_path = os.path.join(dest_dir, result["filename"])
# copy the executable to temp path (don't run the monkey from its current location as it may
@ -60,6 +59,9 @@ def run_local_monkey():
class LocalRun(flask_restful.Resource):
def __init__(self, data_dir):
self._data_dir = data_dir
@jwt_required
def get(self):
NodeService.update_dead_monkeys()
@ -75,7 +77,7 @@ class LocalRun(flask_restful.Resource):
def post(self):
body = json.loads(request.data)
if body.get("action") == "run":
local_run = run_local_monkey()
local_run = run_local_monkey(self._data_dir)
return jsonify(is_running=local_run[0], error_text=local_run[1])
# default action