From 8535118e4f5810777915fef2b22f07da495e8799 Mon Sep 17 00:00:00 2001 From: vakarisz Date: Wed, 27 Apr 2022 18:20:36 +0300 Subject: [PATCH] Island: Add locks to remote_run_aws.py Locks will avoid the situation where is_running_on_aws is called before this service finished initializing --- .../monkey_island/cc/services/remote_run_aws.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/monkey/monkey_island/cc/services/remote_run_aws.py b/monkey/monkey_island/cc/services/remote_run_aws.py index 9f94a4d0b..7b2bdbf90 100644 --- a/monkey/monkey_island/cc/services/remote_run_aws.py +++ b/monkey/monkey_island/cc/services/remote_run_aws.py @@ -1,4 +1,5 @@ import logging +from threading import Lock from common.cloud.aws.aws_instance import AwsInstance from common.cloud.aws.aws_service import AwsService @@ -7,6 +8,7 @@ from common.cmd.cmd import Cmd from common.cmd.cmd_runner import CmdRunner logger = logging.getLogger(__name__) +aws_lock = Lock() class RemoteRunAwsService: @@ -23,15 +25,8 @@ class RemoteRunAwsService: :return: None """ if RemoteRunAwsService.aws_instance is None: - RemoteRunAwsService.try_init_aws_instance() - - @staticmethod - def try_init_aws_instance(): - # noinspection PyBroadException - try: - RemoteRunAwsService.aws_instance = AwsInstance() - except Exception: - logger.error("Failed init aws instance. Exception info: ", exc_info=True) + with aws_lock: + RemoteRunAwsService.aws_instance = AwsInstance() @staticmethod def run_aws_monkeys(instances, island_ip): @@ -53,7 +48,8 @@ class RemoteRunAwsService: @staticmethod def is_running_on_aws(): - return RemoteRunAwsService.aws_instance.is_instance() + with aws_lock: + return RemoteRunAwsService.aws_instance.is_instance() @staticmethod def update_aws_region_authless():