From 0f4b69a6f7a1b7479bd4bf99577ea0b6e9a7bb78 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Mon, 9 May 2022 08:27:10 -0400 Subject: [PATCH] Island: Add stateful AWSService --- monkey/monkey_island/cc/services/__init__.py | 2 + .../monkey_island/cc/services/aws_service.py | 12 ++++ .../cc/services/test_aws_service.py | 56 +++++++++++++++++++ 3 files changed, 70 insertions(+) diff --git a/monkey/monkey_island/cc/services/__init__.py b/monkey/monkey_island/cc/services/__init__.py index 43aa39382..7d96ee5c4 100644 --- a/monkey/monkey_island/cc/services/__init__.py +++ b/monkey/monkey_island/cc/services/__init__.py @@ -3,3 +3,5 @@ from .directory_file_storage_service import DirectoryFileStorageService from .authentication.authentication_service import AuthenticationService from .authentication.json_file_user_datastore import JsonFileUserDatastore + +from .aws_service import AWSService diff --git a/monkey/monkey_island/cc/services/aws_service.py b/monkey/monkey_island/cc/services/aws_service.py index 12432b8b7..fe7f51487 100644 --- a/monkey/monkey_island/cc/services/aws_service.py +++ b/monkey/monkey_island/cc/services/aws_service.py @@ -15,6 +15,18 @@ IP_ADDRESS_KEY = "IPAddress" logger = logging.getLogger(__name__) +class AWSService: + def __init__(self, aws_instance: AWSInstance): + self._aws_instance = aws_instance + + def island_is_running_on_aws(self) -> bool: + return self._aws_instance.is_instance + + @property + def island_aws_instance(self) -> AWSInstance: + return self._aws_instance + + def filter_instance_data_from_aws_response(response): return [ { diff --git a/monkey/tests/unit_tests/monkey_island/cc/services/test_aws_service.py b/monkey/tests/unit_tests/monkey_island/cc/services/test_aws_service.py index 0d8a71f36..bd85595f5 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/services/test_aws_service.py +++ b/monkey/tests/unit_tests/monkey_island/cc/services/test_aws_service.py @@ -1,6 +1,12 @@ import json +import threading +from typing import Optional from unittest import TestCase +import pytest + +from common.aws import AWSInstance +from monkey_island.cc.services import AWSService from monkey_island.cc.services.aws_service import filter_instance_data_from_aws_response @@ -54,3 +60,53 @@ class TestAwsService(TestCase): filter_instance_data_from_aws_response(json.loads(json_response_full)), [{"instance_id": "string", "ip_address": "string", "name": "string", "os": "string"}], ) + + +class StubAWSInstance(AWSInstance): + def __init__( + self, + instance_id: Optional[str] = None, + region: Optional[str] = None, + account_id: Optional[str] = None, + ): + self._instance_id = instance_id + self._region = region + self._account_id = account_id + + self._initialization_complete = threading.Event() + self._initialization_complete.set() + + +def test_aws_is_on_aws__true(): + aws_instance = StubAWSInstance("1") + aws_service = AWSService(aws_instance) + assert aws_service.island_is_running_on_aws() is True + + +def test_aws_is_on_aws__False(): + aws_instance = StubAWSInstance() + aws_service = AWSService(aws_instance) + assert aws_service.island_is_running_on_aws() is False + + +INSTANCE_ID = "1" +REGION = "2" +ACCOUNT_ID = "3" + + +@pytest.fixture +def aws_service(): + aws_instance = StubAWSInstance(INSTANCE_ID, REGION, ACCOUNT_ID) + return AWSService(aws_instance) + + +def test_instance_id(aws_service): + assert aws_service.island_aws_instance.instance_id == INSTANCE_ID + + +def test_region(aws_service): + assert aws_service.island_aws_instance.region == REGION + + +def test_account_id(aws_service): + assert aws_service.island_aws_instance.account_id == ACCOUNT_ID