Common: Rename AwsInstance -> AWSInstance

This commit is contained in:
Mike Salvatore 2022-05-05 11:29:08 -04:00
parent d5f93d21b7
commit 64f9bcacb5
4 changed files with 20 additions and 20 deletions

View File

@ -16,19 +16,19 @@ AWS_TIMEOUT = 2
@dataclass
class AwsInstanceInfo:
class AWSInstanceInfo:
instance_id: Optional[str] = None
region: Optional[str] = None
account_id: Optional[str] = None
class AwsInstance:
class AWSInstance:
"""
Class which gives useful information about the current instance you're on.
"""
def __init__(self):
self._is_instance, self._instance_info = AwsInstance._fetch_instance_info()
self._is_instance, self._instance_info = AWSInstance._fetch_instance_info()
@property
def is_instance(self) -> bool:
@ -47,29 +47,29 @@ class AwsInstance:
return self._instance_info.account_id
@staticmethod
def _fetch_instance_info() -> Tuple[bool, AwsInstanceInfo]:
def _fetch_instance_info() -> Tuple[bool, AWSInstanceInfo]:
try:
response = requests.get(
AWS_LATEST_METADATA_URI_PREFIX + "meta-data/instance-id",
timeout=AWS_TIMEOUT,
)
if not response:
return False, AwsInstanceInfo()
return False, AWSInstanceInfo()
info = AwsInstanceInfo()
info = AWSInstanceInfo()
info.instance_id = response.text if response else False
info.region = AwsInstance._parse_region(
info.region = AWSInstance._parse_region(
requests.get(
AWS_LATEST_METADATA_URI_PREFIX + "meta-data/placement/availability-zone",
timeout=AWS_TIMEOUT,
).text
)
except (requests.RequestException, IOError) as e:
logger.debug("Failed init of AwsInstance while getting metadata: {}".format(e))
return False, AwsInstanceInfo()
logger.debug("Failed init of AWSInstance while getting metadata: {}".format(e))
return False, AWSInstanceInfo()
try:
info.account_id = AwsInstance._extract_account_id(
info.account_id = AWSInstance._extract_account_id(
requests.get(
AWS_LATEST_METADATA_URI_PREFIX + "dynamic/instance-identity/document",
timeout=AWS_TIMEOUT,
@ -77,9 +77,9 @@ class AwsInstance:
)
except (requests.RequestException, json.decoder.JSONDecodeError, IOError) as e:
logger.debug(
"Failed init of AwsInstance while getting dynamic instance data: {}".format(e)
"Failed init of AWSInstance while getting dynamic instance data: {}".format(e)
)
return False, AwsInstanceInfo()
return False, AWSInstanceInfo()
return True, info

View File

@ -1,6 +1,6 @@
import logging
from common.aws.aws_instance import AwsInstance
from common.aws.aws_instance import AWSInstance
from infection_monkey.telemetry.aws_instance_telem import AWSInstanceTelemetry
from infection_monkey.telemetry.messengers.legacy_telemetry_messenger_adapter import (
LegacyTelemetryMessengerAdapter,
@ -13,7 +13,7 @@ logger = logging.getLogger(__name__)
def _report_aws_environment(telemetry_messenger: LegacyTelemetryMessengerAdapter):
logger.info("Collecting AWS info")
aws_instance = AwsInstance()
aws_instance = AWSInstance()
if aws_instance.is_instance:
logger.info("Machine is an AWS instance")

View File

@ -6,7 +6,7 @@ from typing import Callable, Optional
import boto3
import botocore
from common.aws.aws_instance import AwsInstance
from common.aws.aws_instance import AWSInstance
INSTANCE_INFORMATION_LIST_KEY = "InstanceInformationList"
INSTANCE_ID_KEY = "InstanceId"
@ -29,14 +29,14 @@ def filter_instance_data_from_aws_response(response):
]
aws_instance: Optional[AwsInstance] = None
aws_instance: Optional[AWSInstance] = None
AWS_INFO_FETCH_TIMEOUT = 10.0 # Seconds
init_done = Event()
def initialize():
global aws_instance
aws_instance = AwsInstance()
aws_instance = AWSInstance()
init_done.set()

View File

@ -4,7 +4,7 @@ import pytest
import requests
import requests_mock
from common.aws.aws_instance import AWS_LATEST_METADATA_URI_PREFIX, AwsInstance
from common.aws.aws_instance import AWS_LATEST_METADATA_URI_PREFIX, AWSInstance
INSTANCE_ID_RESPONSE = "i-1234567890abcdef0"
@ -59,7 +59,7 @@ def get_test_aws_instance(
url, exc=exception["account_id"]
)
test_aws_instance_object = AwsInstance()
test_aws_instance_object = AWSInstance()
return test_aws_instance_object
@ -204,7 +204,7 @@ def not_found_request_mock_instance():
url = f"{AWS_LATEST_METADATA_URI_PREFIX}dynamic/instance-identity/document"
m.get(url)
not_found_aws_instance_object = AwsInstance()
not_found_aws_instance_object = AWSInstance()
yield not_found_aws_instance_object
del not_found_aws_instance_object