Island: Add stateful AWSService

This commit is contained in:
Mike Salvatore 2022-05-09 08:27:10 -04:00
parent e83848c8a4
commit 0f4b69a6f7
3 changed files with 70 additions and 0 deletions

View File

@ -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

View File

@ -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 [
{

View File

@ -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