forked from p15670423/monkey
Common: Rename AwsInstance -> AWSInstance
This commit is contained in:
parent
d5f93d21b7
commit
64f9bcacb5
|
@ -16,19 +16,19 @@ AWS_TIMEOUT = 2
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class AwsInstanceInfo:
|
class AWSInstanceInfo:
|
||||||
instance_id: Optional[str] = None
|
instance_id: Optional[str] = None
|
||||||
region: Optional[str] = None
|
region: Optional[str] = None
|
||||||
account_id: Optional[str] = None
|
account_id: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
class AwsInstance:
|
class AWSInstance:
|
||||||
"""
|
"""
|
||||||
Class which gives useful information about the current instance you're on.
|
Class which gives useful information about the current instance you're on.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._is_instance, self._instance_info = AwsInstance._fetch_instance_info()
|
self._is_instance, self._instance_info = AWSInstance._fetch_instance_info()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_instance(self) -> bool:
|
def is_instance(self) -> bool:
|
||||||
|
@ -47,29 +47,29 @@ class AwsInstance:
|
||||||
return self._instance_info.account_id
|
return self._instance_info.account_id
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _fetch_instance_info() -> Tuple[bool, AwsInstanceInfo]:
|
def _fetch_instance_info() -> Tuple[bool, AWSInstanceInfo]:
|
||||||
try:
|
try:
|
||||||
response = requests.get(
|
response = requests.get(
|
||||||
AWS_LATEST_METADATA_URI_PREFIX + "meta-data/instance-id",
|
AWS_LATEST_METADATA_URI_PREFIX + "meta-data/instance-id",
|
||||||
timeout=AWS_TIMEOUT,
|
timeout=AWS_TIMEOUT,
|
||||||
)
|
)
|
||||||
if not response:
|
if not response:
|
||||||
return False, AwsInstanceInfo()
|
return False, AWSInstanceInfo()
|
||||||
|
|
||||||
info = AwsInstanceInfo()
|
info = AWSInstanceInfo()
|
||||||
info.instance_id = response.text if response else False
|
info.instance_id = response.text if response else False
|
||||||
info.region = AwsInstance._parse_region(
|
info.region = AWSInstance._parse_region(
|
||||||
requests.get(
|
requests.get(
|
||||||
AWS_LATEST_METADATA_URI_PREFIX + "meta-data/placement/availability-zone",
|
AWS_LATEST_METADATA_URI_PREFIX + "meta-data/placement/availability-zone",
|
||||||
timeout=AWS_TIMEOUT,
|
timeout=AWS_TIMEOUT,
|
||||||
).text
|
).text
|
||||||
)
|
)
|
||||||
except (requests.RequestException, IOError) as e:
|
except (requests.RequestException, IOError) as e:
|
||||||
logger.debug("Failed init of AwsInstance while getting metadata: {}".format(e))
|
logger.debug("Failed init of AWSInstance while getting metadata: {}".format(e))
|
||||||
return False, AwsInstanceInfo()
|
return False, AWSInstanceInfo()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
info.account_id = AwsInstance._extract_account_id(
|
info.account_id = AWSInstance._extract_account_id(
|
||||||
requests.get(
|
requests.get(
|
||||||
AWS_LATEST_METADATA_URI_PREFIX + "dynamic/instance-identity/document",
|
AWS_LATEST_METADATA_URI_PREFIX + "dynamic/instance-identity/document",
|
||||||
timeout=AWS_TIMEOUT,
|
timeout=AWS_TIMEOUT,
|
||||||
|
@ -77,9 +77,9 @@ class AwsInstance:
|
||||||
)
|
)
|
||||||
except (requests.RequestException, json.decoder.JSONDecodeError, IOError) as e:
|
except (requests.RequestException, json.decoder.JSONDecodeError, IOError) as e:
|
||||||
logger.debug(
|
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
|
return True, info
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import logging
|
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.aws_instance_telem import AWSInstanceTelemetry
|
||||||
from infection_monkey.telemetry.messengers.legacy_telemetry_messenger_adapter import (
|
from infection_monkey.telemetry.messengers.legacy_telemetry_messenger_adapter import (
|
||||||
LegacyTelemetryMessengerAdapter,
|
LegacyTelemetryMessengerAdapter,
|
||||||
|
@ -13,7 +13,7 @@ logger = logging.getLogger(__name__)
|
||||||
def _report_aws_environment(telemetry_messenger: LegacyTelemetryMessengerAdapter):
|
def _report_aws_environment(telemetry_messenger: LegacyTelemetryMessengerAdapter):
|
||||||
logger.info("Collecting AWS info")
|
logger.info("Collecting AWS info")
|
||||||
|
|
||||||
aws_instance = AwsInstance()
|
aws_instance = AWSInstance()
|
||||||
|
|
||||||
if aws_instance.is_instance:
|
if aws_instance.is_instance:
|
||||||
logger.info("Machine is an AWS instance")
|
logger.info("Machine is an AWS instance")
|
||||||
|
|
|
@ -6,7 +6,7 @@ from typing import Callable, Optional
|
||||||
import boto3
|
import boto3
|
||||||
import botocore
|
import botocore
|
||||||
|
|
||||||
from common.aws.aws_instance import AwsInstance
|
from common.aws.aws_instance import AWSInstance
|
||||||
|
|
||||||
INSTANCE_INFORMATION_LIST_KEY = "InstanceInformationList"
|
INSTANCE_INFORMATION_LIST_KEY = "InstanceInformationList"
|
||||||
INSTANCE_ID_KEY = "InstanceId"
|
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
|
AWS_INFO_FETCH_TIMEOUT = 10.0 # Seconds
|
||||||
init_done = Event()
|
init_done = Event()
|
||||||
|
|
||||||
|
|
||||||
def initialize():
|
def initialize():
|
||||||
global aws_instance
|
global aws_instance
|
||||||
aws_instance = AwsInstance()
|
aws_instance = AWSInstance()
|
||||||
init_done.set()
|
init_done.set()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ import pytest
|
||||||
import requests
|
import requests
|
||||||
import requests_mock
|
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"
|
INSTANCE_ID_RESPONSE = "i-1234567890abcdef0"
|
||||||
|
|
||||||
|
@ -59,7 +59,7 @@ def get_test_aws_instance(
|
||||||
url, exc=exception["account_id"]
|
url, exc=exception["account_id"]
|
||||||
)
|
)
|
||||||
|
|
||||||
test_aws_instance_object = AwsInstance()
|
test_aws_instance_object = AWSInstance()
|
||||||
return test_aws_instance_object
|
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"
|
url = f"{AWS_LATEST_METADATA_URI_PREFIX}dynamic/instance-identity/document"
|
||||||
m.get(url)
|
m.get(url)
|
||||||
|
|
||||||
not_found_aws_instance_object = AwsInstance()
|
not_found_aws_instance_object = AWSInstance()
|
||||||
yield not_found_aws_instance_object
|
yield not_found_aws_instance_object
|
||||||
del not_found_aws_instance_object
|
del not_found_aws_instance_object
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue