From b9d2614271689d3752cbf0f57b3e2f4f07f37a9c Mon Sep 17 00:00:00 2001 From: Shay Nehmad Date: Wed, 8 Jan 2020 11:09:52 +0200 Subject: [PATCH] CR: Moved AWS classes to own package, create generic CloudInstance class --- monkey/common/cloud/aws/__init__.py | 0 monkey/common/cloud/{ => aws}/aws_instance.py | 6 ++++-- monkey/common/cloud/{ => aws}/aws_service.py | 2 +- monkey/common/cloud/{ => aws}/aws_service_test.py | 0 monkey/common/cloud/azure/azure_instance.py | 8 +++++--- monkey/common/cloud/environment_names.py | 7 ++++++- monkey/common/cloud/instance.py | 3 +++ monkey/common/cmd/aws/aws_cmd_runner.py | 2 +- monkey/infection_monkey/system_info/aws_collector.py | 4 ++-- .../system_info/collectors/environment_collector.py | 6 +++--- monkey/monkey_island/cc/environment/aws.py | 2 +- monkey/monkey_island/cc/resources/remote_run.py | 2 +- monkey/monkey_island/cc/services/remote_run_aws.py | 6 +++--- .../monkey_island/cc/services/reporting/aws_exporter.py | 2 +- 14 files changed, 31 insertions(+), 19 deletions(-) create mode 100644 monkey/common/cloud/aws/__init__.py rename monkey/common/cloud/{ => aws}/aws_instance.py (96%) rename monkey/common/cloud/{ => aws}/aws_service.py (98%) rename monkey/common/cloud/{ => aws}/aws_service_test.py (100%) create mode 100644 monkey/common/cloud/instance.py diff --git a/monkey/common/cloud/aws/__init__.py b/monkey/common/cloud/aws/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/monkey/common/cloud/aws_instance.py b/monkey/common/cloud/aws/aws_instance.py similarity index 96% rename from monkey/common/cloud/aws_instance.py rename to monkey/common/cloud/aws/aws_instance.py index 4339fbcf4..301881894 100644 --- a/monkey/common/cloud/aws_instance.py +++ b/monkey/common/cloud/aws/aws_instance.py @@ -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 diff --git a/monkey/common/cloud/aws_service.py b/monkey/common/cloud/aws/aws_service.py similarity index 98% rename from monkey/common/cloud/aws_service.py rename to monkey/common/cloud/aws/aws_service.py index 6ef385542..a42c2e1dd 100644 --- a/monkey/common/cloud/aws_service.py +++ b/monkey/common/cloud/aws/aws_service.py @@ -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'] diff --git a/monkey/common/cloud/aws_service_test.py b/monkey/common/cloud/aws/aws_service_test.py similarity index 100% rename from monkey/common/cloud/aws_service_test.py rename to monkey/common/cloud/aws/aws_service_test.py diff --git a/monkey/common/cloud/azure/azure_instance.py b/monkey/common/cloud/azure/azure_instance.py index a58e0e126..5222c7620 100644 --- a/monkey/common/cloud/azure/azure_instance.py +++ b/monkey/common/cloud/azure/azure_instance.py @@ -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 diff --git a/monkey/common/cloud/environment_names.py b/monkey/common/cloud/environment_names.py index 1745eed62..0c8655753 100644 --- a/monkey/common/cloud/environment_names.py +++ b/monkey/common/cloud/environment_names.py @@ -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] diff --git a/monkey/common/cloud/instance.py b/monkey/common/cloud/instance.py new file mode 100644 index 000000000..52dd56b02 --- /dev/null +++ b/monkey/common/cloud/instance.py @@ -0,0 +1,3 @@ +class CloudInstance(object): + def is_instance(self) -> bool: + raise NotImplementedError() diff --git a/monkey/common/cmd/aws/aws_cmd_runner.py b/monkey/common/cmd/aws/aws_cmd_runner.py index 459a42129..1ab680c4d 100644 --- a/monkey/common/cmd/aws/aws_cmd_runner.py +++ b/monkey/common/cmd/aws/aws_cmd_runner.py @@ -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 diff --git a/monkey/infection_monkey/system_info/aws_collector.py b/monkey/infection_monkey/system_info/aws_collector.py index df90e5913..f39662d13 100644 --- a/monkey/infection_monkey/system_info/aws_collector.py +++ b/monkey/infection_monkey/system_info/aws_collector.py @@ -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 = \ { diff --git a/monkey/infection_monkey/system_info/collectors/environment_collector.py b/monkey/infection_monkey/system_info/collectors/environment_collector.py index 523989f61..208bbfa42 100644 --- a/monkey/infection_monkey/system_info/collectors/environment_collector.py +++ b/monkey/infection_monkey/system_info/collectors/environment_collector.py @@ -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: diff --git a/monkey/monkey_island/cc/environment/aws.py b/monkey/monkey_island/cc/environment/aws.py index 18db5c376..5608bddcd 100644 --- a/monkey/monkey_island/cc/environment/aws.py +++ b/monkey/monkey_island/cc/environment/aws.py @@ -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' diff --git a/monkey/monkey_island/cc/resources/remote_run.py b/monkey/monkey_island/cc/resources/remote_run.py index c41699add..98d3694bf 100644 --- a/monkey/monkey_island/cc/resources/remote_run.py +++ b/monkey/monkey_island/cc/resources/remote_run.py @@ -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. " diff --git a/monkey/monkey_island/cc/services/remote_run_aws.py b/monkey/monkey_island/cc/services/remote_run_aws.py index 9627bf74c..0ba6fa4ef 100644 --- a/monkey/monkey_island/cc/services/remote_run_aws.py +++ b/monkey/monkey_island/cc/services/remote_run_aws.py @@ -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(): diff --git a/monkey/monkey_island/cc/services/reporting/aws_exporter.py b/monkey/monkey_island/cc/services/reporting/aws_exporter.py index 1df12e2eb..86486b9ba 100644 --- a/monkey/monkey_island/cc/services/reporting/aws_exporter.py +++ b/monkey/monkey_island/cc/services/reporting/aws_exporter.py @@ -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