CR: Moved AWS classes to own package, create generic CloudInstance class

This commit is contained in:
Shay Nehmad 2020-01-08 11:09:52 +02:00
parent fdb54f6b8d
commit b9d2614271
14 changed files with 31 additions and 19 deletions

View File

View File

@ -6,6 +6,8 @@ import logging
__author__ = 'itay.mizeretz'
from common.cloud.instance import CloudInstance
AWS_INSTANCE_METADATA_LOCAL_IP_ADDRESS = "169.254.169.254"
AWS_LATEST_METADATA_URI_PREFIX = 'http://{0}/latest/'.format(AWS_INSTANCE_METADATA_LOCAL_IP_ADDRESS)
ACCOUNT_ID_KEY = "accountId"
@ -13,7 +15,7 @@ ACCOUNT_ID_KEY = "accountId"
logger = logging.getLogger(__name__)
class AwsInstance(object):
class AwsInstance(CloudInstance):
"""
Class which gives useful information about the current instance you're on.
"""
@ -57,7 +59,7 @@ class AwsInstance(object):
def get_region(self):
return self.region
def is_aws_instance(self):
def is_instance(self):
return self.instance_id is not None
@staticmethod

View File

@ -4,7 +4,7 @@ import boto3
import botocore
from botocore.exceptions import ClientError
from common.cloud.aws_instance import AwsInstance
from common.cloud.aws.aws_instance import AwsInstance
__author__ = ['itay.mizeretz', 'shay.nehmad']

View File

@ -1,13 +1,15 @@
import logging
import requests
LATEST_AZURE_METADATA_API_VERSION = "2019-06-04"
from common.cloud.instance import CloudInstance
LATEST_AZURE_METADATA_API_VERSION = "2019-04-30"
AZURE_METADATA_SERVICE_URL = "http://169.254.169.254/metadata/instance?api-version=%s" % LATEST_AZURE_METADATA_API_VERSION
logger = logging.getLogger(__name__)
class AzureInstance(object):
class AzureInstance(CloudInstance):
"""
Access to useful information about the current machine if it's an Azure VM.
Based on Azure metadata service: https://docs.microsoft.com/en-us/azure/virtual-machines/windows/instance-metadata-service
@ -46,5 +48,5 @@ class AzureInstance(object):
except KeyError:
logger.exception("Error while parsing response from Azure metadata service.")
def is_azure_instance(self):
def is_instance(self):
return self.on_azure

View File

@ -1,7 +1,12 @@
# When adding a new environment to this file, make sure to add it to ALL_ENV_NAMES as well!
UNKNOWN = "Unknown"
ON_PREMISE = "On Premise"
AZURE = "Azure"
AWS = "AWS"
GCP = "GCP"
ALIBABA = "Alibaba Cloud"
IBM = "IBM Cloud"
DigitalOcean = "Digital Ocean"
ALL_ENV_NAMES = [UNKNOWN, ON_PREMISE, AZURE, AWS, GCP]
ALL_ENV_NAMES = [UNKNOWN, ON_PREMISE, AZURE, AWS, GCP, ALIBABA, IBM, DigitalOcean]

View File

@ -0,0 +1,3 @@
class CloudInstance(object):
def is_instance(self) -> bool:
raise NotImplementedError()

View File

@ -1,6 +1,6 @@
import logging
from common.cloud.aws_service import AwsService
from common.cloud.aws.aws_service import AwsService
from common.cmd.aws.aws_cmd_result import AwsCmdResult
from common.cmd.cmd_runner import CmdRunner
from common.cmd.cmd_status import CmdStatus

View File

@ -1,6 +1,6 @@
import logging
from common.cloud.aws_instance import AwsInstance
from common.cloud.aws.aws_instance import AwsInstance
__author__ = 'itay.mizeretz'
@ -17,7 +17,7 @@ class AwsCollector(object):
LOG.info("Collecting AWS info")
aws = AwsInstance()
info = {}
if aws.is_aws_instance():
if aws.is_instance():
LOG.info("Machine is an AWS instance")
info = \
{

View File

@ -1,4 +1,4 @@
from common.cloud.aws_instance import AwsInstance
from common.cloud.aws.aws_instance import AwsInstance
from common.cloud.azure.azure_instance import AzureInstance
from common.cloud.environment_names import ON_PREMISE, AZURE, AWS
from infection_monkey.system_info.system_info_collector import SystemInfoCollector
@ -6,9 +6,9 @@ from infection_monkey.system_info.system_info_collector import SystemInfoCollect
def get_monkey_environment():
# Check if on any cloud env. Default is on prem.
if AwsInstance().is_aws_instance():
if AwsInstance().is_instance():
env = AWS
elif AzureInstance().is_azure_instance():
elif AzureInstance().is_instance():
env = AZURE
# TODO: elif GcpInstance().is_gcp_instance():
else:

View File

@ -1,6 +1,6 @@
import monkey_island.cc.auth
from monkey_island.cc.environment import Environment
from common.cloud.aws_instance import AwsInstance
from common.cloud.aws.aws_instance import AwsInstance
__author__ = 'itay.mizeretz'

View File

@ -6,7 +6,7 @@ import flask_restful
from monkey_island.cc.auth import jwt_required
from monkey_island.cc.services.remote_run_aws import RemoteRunAwsService
from common.cloud.aws_service import AwsService
from common.cloud.aws.aws_service import AwsService
CLIENT_ERROR_FORMAT = "ClientError, error message: '{}'. Probably, the IAM role that has been associated with the " \
"instance doesn't permit SSM calls. "

View File

@ -1,7 +1,7 @@
import logging
from common.cloud.aws_instance import AwsInstance
from common.cloud.aws_service import AwsService
from common.cloud.aws.aws_instance import AwsInstance
from common.cloud.aws.aws_service import AwsService
from common.cmd.aws.aws_cmd_runner import AwsCmdRunner
from common.cmd.cmd import Cmd
from common.cmd.cmd_runner import CmdRunner
@ -54,7 +54,7 @@ class RemoteRunAwsService:
@staticmethod
def is_running_on_aws():
return RemoteRunAwsService.aws_instance.is_aws_instance()
return RemoteRunAwsService.aws_instance.is_instance()
@staticmethod
def update_aws_region_authless():

View File

@ -5,7 +5,7 @@ from datetime import datetime
import boto3
from botocore.exceptions import UnknownServiceError
from common.cloud.aws_instance import AwsInstance
from common.cloud.aws.aws_instance import AwsInstance
from monkey_island.cc.environment.environment import load_server_configuration_from_file
from monkey_island.cc.services.reporting.exporter import Exporter