Merge pull request #1615 from guardicore/1535-environment-collector-removal

1535 environment collector removal
This commit is contained in:
VakarisZ 2021-11-17 09:03:33 +02:00 committed by GitHub
commit c0f4435e5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 8 additions and 575 deletions

View File

@ -5,7 +5,7 @@
"dod": "Add a system info collector that collects the machine hostname.", "dod": "Add a system info collector that collects the machine hostname.",
"tests": [], "tests": [],
"hints": [ "hints": [
"First thing you should do is take a look at a different collector (like EnvironmentCollector) and 100% understand how it runs, how results are relayed back to the server, and how the server processes the data.", "First thing you should do is take a look at a different collector (like HostnameCollector) and 100% understand how it runs, how results are relayed back to the server, and how the server processes the data.",
"Try to run \"socket.getfqdn()\".", "Try to run \"socket.getfqdn()\".",
"Take a look at SystemInfoCollector - that's the base class you'll need to implement.", "Take a look at SystemInfoCollector - that's the base class you'll need to implement.",
"Make sure you add the new collector to the configuration in all relevant places, including making it ON by default!" "Make sure you add the new collector to the configuration in all relevant places, including making it ON by default!"
@ -25,7 +25,6 @@
" AWS_COLLECTOR = \"AwsCollector\"", " AWS_COLLECTOR = \"AwsCollector\"",
"*HOSTNAME_COLLECTOR = \"HostnameCollector\"", "*HOSTNAME_COLLECTOR = \"HostnameCollector\"",
"+# SWIMMER: Collector name goes here.", "+# SWIMMER: Collector name goes here.",
" ENVIRONMENT_COLLECTOR = \"EnvironmentCollector\"",
" PROCESS_LIST_COLLECTOR = \"ProcessListCollector\"", " PROCESS_LIST_COLLECTOR = \"ProcessListCollector\"",
" MIMIKATZ_COLLECTOR = \"MimikatzCollector\"" " MIMIKATZ_COLLECTOR = \"MimikatzCollector\""
] ]
@ -60,7 +59,6 @@
"comments": [], "comments": [],
"firstLineNumber": 4, "firstLineNumber": 4,
"lines": [ "lines": [
" ENVIRONMENT_COLLECTOR,",
"* HOSTNAME_COLLECTOR,", "* HOSTNAME_COLLECTOR,",
" MIMIKATZ_COLLECTOR,", " MIMIKATZ_COLLECTOR,",
" PROCESS_LIST_COLLECTOR,", " PROCESS_LIST_COLLECTOR,",
@ -97,7 +95,6 @@
"lines": [ "lines": [
" from common.common_consts.system_info_collectors_names import (", " from common.common_consts.system_info_collectors_names import (",
" AWS_COLLECTOR,", " AWS_COLLECTOR,",
" ENVIRONMENT_COLLECTOR,",
"* HOSTNAME_COLLECTOR,", "* HOSTNAME_COLLECTOR,",
" MIMIKATZ_COLLECTOR,", " MIMIKATZ_COLLECTOR,",
" PROCESS_LIST_COLLECTOR,", " PROCESS_LIST_COLLECTOR,",
@ -111,7 +108,6 @@
"firstLineNumber": 91, "firstLineNumber": 91,
"lines": [ "lines": [
" \"default\": [", " \"default\": [",
" ENVIRONMENT_COLLECTOR,",
" AWS_COLLECTOR,", " AWS_COLLECTOR,",
"* HOSTNAME_COLLECTOR,", "* HOSTNAME_COLLECTOR,",
" PROCESS_LIST_COLLECTOR,", " PROCESS_LIST_COLLECTOR,",
@ -149,7 +145,6 @@
" ", " ",
" from common.common_consts.system_info_collectors_names import (", " from common.common_consts.system_info_collectors_names import (",
" AWS_COLLECTOR,", " AWS_COLLECTOR,",
" ENVIRONMENT_COLLECTOR,",
"* HOSTNAME_COLLECTOR," "* HOSTNAME_COLLECTOR,"
] ]
}, },
@ -161,7 +156,6 @@
"lines": [ "lines": [
" SYSTEM_INFO_COLLECTOR_TO_TELEMETRY_PROCESSORS = {", " SYSTEM_INFO_COLLECTOR_TO_TELEMETRY_PROCESSORS = {",
" AWS_COLLECTOR: [process_aws_telemetry],", " AWS_COLLECTOR: [process_aws_telemetry],",
" ENVIRONMENT_COLLECTOR: [process_environment_telemetry],",
"* HOSTNAME_COLLECTOR: [process_hostname_telemetry],", "* HOSTNAME_COLLECTOR: [process_hostname_telemetry],",
" PROCESS_LIST_COLLECTOR: [check_antivirus_existence],", " PROCESS_LIST_COLLECTOR: [check_antivirus_existence],",
" }", " }",
@ -171,10 +165,6 @@
{ {
"type": "snippet", "type": "snippet",
"lines": [ "lines": [
" )",
" from monkey_island.cc.services.telemetry.processing.system_info_collectors.environment import (",
" process_environment_telemetry,",
" )",
"*from monkey_island.cc.services.telemetry.processing.system_info_collectors.hostname import (", "*from monkey_island.cc.services.telemetry.processing.system_info_collectors.hostname import (",
"* process_hostname_telemetry,", "* process_hostname_telemetry,",
"*)", "*)",

View File

@ -24,6 +24,7 @@ Changelog](https://keepachangelog.com/en/1.0.0/).
- Remove serialization of config. #1537 - Remove serialization of config. #1537
- Checkbox that gave the option to not try to first move the dropper file. #1537 - Checkbox that gave the option to not try to first move the dropper file. #1537
- Custom singleton mutex name config option. #1589 - Custom singleton mutex name config option. #1589
- Removed environment system info collector #1535
- Azure credential collector, because it was broken (not gathering credentials). #1535 - Azure credential collector, because it was broken (not gathering credentials). #1535
- Custom monkey directory name config option. #1537 - Custom monkey directory name config option. #1537

View File

@ -39,7 +39,7 @@ class MyNewCollector(SystemInfoCollector):
#### Implementation #### Implementation
Override the `collect` method with your own implementation. See the `EnvironmentCollector.py` System Info Collector for reference. You can log during collection as well. Override the `collect` method with your own implementation. See the `hostname_collector.py` System Info Collector for reference. You can log during collection as well.
### Modify the Monkey Island ### Modify the Monkey Island
@ -57,7 +57,7 @@ You'll need to add your Sytem Info Collector to the `monkey_island/cc/services/c
{ {
"type": "string", "type": "string",
"enum": [ "enum": [
"EnvironmentCollector" "HostnameCollector"
], ],
"title": "Which Environment this machine is on (on prem/cloud)", "title": "Which Environment this machine is on (on prem/cloud)",
"attack_techniques": [] "attack_techniques": []
@ -87,7 +87,7 @@ Also, you can add the System Info Collector to be used by default by adding it t
"$ref": "#/definitions/system_info_collectors_classes" "$ref": "#/definitions/system_info_collectors_classes"
}, },
"default": [ "default": [
"EnvironmentCollector", "HostnameCollector",
"MyNewCollector" <================================= "MyNewCollector" <=================================
], ],
"description": "Determines which system information collectors will collect information." "description": "Determines which system information collectors will collect information."

View File

@ -1,12 +0,0 @@
from typing import List
from common.cloud.aws.aws_instance import AwsInstance
from common.cloud.azure.azure_instance import AzureInstance
from common.cloud.gcp.gcp_instance import GcpInstance
from common.cloud.instance import CloudInstance
all_cloud_instances = [AwsInstance(), AzureInstance(), GcpInstance()]
def get_all_cloud_instances() -> List[CloudInstance]:
return all_cloud_instances

View File

@ -4,7 +4,6 @@ import re
import requests import requests
from common.cloud.environment_names import Environment
from common.cloud.instance import CloudInstance from common.cloud.instance import CloudInstance
AWS_INSTANCE_METADATA_LOCAL_IP_ADDRESS = "169.254.169.254" AWS_INSTANCE_METADATA_LOCAL_IP_ADDRESS = "169.254.169.254"
@ -22,9 +21,6 @@ class AwsInstance(CloudInstance):
def is_instance(self): def is_instance(self):
return self.instance_id is not None return self.instance_id is not None
def get_cloud_provider_name(self) -> Environment:
return Environment.AWS
def __init__(self): def __init__(self):
self.instance_id = None self.instance_id = None
self.region = None self.region = None

View File

@ -1,69 +0,0 @@
import logging
import requests
import simplejson
from common.cloud.environment_names import Environment
from common.cloud.instance import CloudInstance
from common.common_consts.timeouts import SHORT_REQUEST_TIMEOUT
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(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
"""
def is_instance(self):
return self._on_azure
def get_cloud_provider_name(self) -> Environment:
return Environment.AZURE
def __init__(self):
"""
Determines if on Azure and if so, gets some basic metadata on this instance.
"""
self.instance_name = None
self.instance_id = None
self.location = None
self._on_azure = False
try:
response = requests.get(
AZURE_METADATA_SERVICE_URL,
headers={"Metadata": "true"},
timeout=SHORT_REQUEST_TIMEOUT,
)
# If not on cloud, the metadata URL is non-routable and the connection will fail.
# If on AWS, should get 404 since the metadata service URL is different,
# so bool(response) will be false.
if response:
logger.debug("Trying to parse Azure metadata.")
self.try_parse_response(response)
else:
logger.warning(f"Metadata response not ok: {response.status_code}")
except requests.RequestException:
logger.debug(
"Failed to get response from Azure metadata service: This instance is not on "
"Azure."
)
def try_parse_response(self, response):
try:
response_data = response.json()
self.instance_name = response_data["compute"]["name"]
self.instance_id = response_data["compute"]["vmId"]
self.location = response_data["compute"]["location"]
self._on_azure = True
except (KeyError, simplejson.errors.JSONDecodeError) as e:
logger.exception(f"Error while parsing response from Azure metadata service: {e}")

View File

@ -2,14 +2,4 @@ from enum import Enum
class Environment(Enum): class Environment(Enum):
UNKNOWN = "Unknown"
ON_PREMISE = "On Premise"
AZURE = "Azure"
AWS = "AWS" AWS = "AWS"
GCP = "GCP"
ALIBABA = "Alibaba Cloud"
IBM = "IBM Cloud"
DigitalOcean = "Digital Ocean"
ALL_ENVIRONMENTS_NAMES = [x.value for x in Environment]

View File

@ -1,54 +0,0 @@
import logging
import requests
from common.cloud.environment_names import Environment
from common.cloud.instance import CloudInstance
from common.common_consts.timeouts import SHORT_REQUEST_TIMEOUT
logger = logging.getLogger(__name__)
GCP_METADATA_SERVICE_URL = "http://metadata.google.internal/"
class GcpInstance(CloudInstance):
"""
Used to determine if on GCP. See https://cloud.google.com/compute/docs/storing-retrieving
-metadata#runninggce
"""
def is_instance(self):
return self._on_gcp
def get_cloud_provider_name(self) -> Environment:
return Environment.GCP
def __init__(self):
self._on_gcp = False
try:
# If not on GCP, this domain shouldn't resolve.
response = requests.get(GCP_METADATA_SERVICE_URL, timeout=SHORT_REQUEST_TIMEOUT)
if response:
logger.debug("Got ok metadata response: on GCP")
self._on_gcp = True
if "Metadata-Flavor" not in response.headers:
logger.warning("Got unexpected GCP Metadata format")
else:
if not response.headers["Metadata-Flavor"] == "Google":
logger.warning(
"Got unexpected Metadata flavor: {}".format(
response.headers["Metadata-Flavor"]
)
)
else:
logger.warning(
"On GCP, but metadata response not ok: {}".format(response.status_code)
)
except requests.RequestException:
logger.debug(
"Failed to get response from GCP metadata service: This instance is not on GCP"
)
self._on_gcp = False

View File

@ -1,6 +1,3 @@
from common.cloud.environment_names import Environment
class CloudInstance(object): class CloudInstance(object):
""" """
This is an abstract class which represents a cloud instance. This is an abstract class which represents a cloud instance.
@ -10,6 +7,3 @@ class CloudInstance(object):
def is_instance(self) -> bool: def is_instance(self) -> bool:
raise NotImplementedError() raise NotImplementedError()
def get_cloud_provider_name(self) -> Environment:
raise NotImplementedError()

View File

@ -3,7 +3,3 @@ from enum import Enum
class CloudProviders(Enum): class CloudProviders(Enum):
AWS = "aws" AWS = "aws"
AZURE = "azure"
GCP = "gcp"
ALIBABA = "aliyun"
ORACLE = "oci"

View File

@ -1,5 +1,4 @@
AWS_COLLECTOR = "AwsCollector" AWS_COLLECTOR = "AwsCollector"
HOSTNAME_COLLECTOR = "HostnameCollector" HOSTNAME_COLLECTOR = "HostnameCollector"
ENVIRONMENT_COLLECTOR = "EnvironmentCollector"
PROCESS_LIST_COLLECTOR = "ProcessListCollector" PROCESS_LIST_COLLECTOR = "ProcessListCollector"
MIMIKATZ_COLLECTOR = "MimikatzCollector" MIMIKATZ_COLLECTOR = "MimikatzCollector"

View File

@ -1,24 +0,0 @@
from common.cloud.all_instances import get_all_cloud_instances
from common.cloud.environment_names import Environment
from common.common_consts.system_info_collectors_names import ENVIRONMENT_COLLECTOR
from infection_monkey.system_info.system_info_collector import SystemInfoCollector
def get_monkey_environment() -> str:
"""
Get the Monkey's running environment.
:return: One of the cloud providers if on cloud; otherwise, assumes "on premise".
"""
for instance in get_all_cloud_instances():
if instance.is_instance():
return instance.get_cloud_provider_name().value
return Environment.ON_PREMISE.value
class EnvironmentCollector(SystemInfoCollector):
def __init__(self):
super().__init__(name=ENVIRONMENT_COLLECTOR)
def collect(self) -> dict:
return {"environment": get_monkey_environment()}

View File

@ -14,7 +14,6 @@ from mongoengine import (
StringField, StringField,
) )
from common.cloud import environment_names
from monkey_island.cc.models.command_control_channel import CommandControlChannel from monkey_island.cc.models.command_control_channel import CommandControlChannel
from monkey_island.cc.models.monkey_ttl import MonkeyTtl, create_monkey_ttl_document from monkey_island.cc.models.monkey_ttl import MonkeyTtl, create_monkey_ttl_document
from monkey_island.cc.server_utils.consts import DEFAULT_MONKEY_TTL_EXPIRY_DURATION_IN_SECONDS from monkey_island.cc.server_utils.consts import DEFAULT_MONKEY_TTL_EXPIRY_DURATION_IN_SECONDS
@ -55,14 +54,8 @@ class Monkey(Document):
tunnel = ReferenceField("self") tunnel = ReferenceField("self")
command_control_channel = EmbeddedDocumentField(CommandControlChannel) command_control_channel = EmbeddedDocumentField(CommandControlChannel)
# Environment related fields # This field only exists when the monkey is running on an AWS
environment = StringField( aws_instance_id = StringField(required=False)
default=environment_names.Environment.UNKNOWN.value,
choices=environment_names.ALL_ENVIRONMENTS_NAMES,
)
aws_instance_id = StringField(
required=False
) # This field only exists when the monkey is running on an AWS
# instance. See https://github.com/guardicore/monkey/issues/426. # instance. See https://github.com/guardicore/monkey/issues/426.

View File

@ -1,6 +1,5 @@
from common.common_consts.system_info_collectors_names import ( from common.common_consts.system_info_collectors_names import (
AWS_COLLECTOR, AWS_COLLECTOR,
ENVIRONMENT_COLLECTOR,
HOSTNAME_COLLECTOR, HOSTNAME_COLLECTOR,
MIMIKATZ_COLLECTOR, MIMIKATZ_COLLECTOR,
PROCESS_LIST_COLLECTOR, PROCESS_LIST_COLLECTOR,
@ -11,14 +10,6 @@ SYSTEM_INFO_COLLECTOR_CLASSES = {
"description": "Click on a system info collector to find out what it collects.", "description": "Click on a system info collector to find out what it collects.",
"type": "string", "type": "string",
"anyOf": [ "anyOf": [
{
"type": "string",
"enum": [ENVIRONMENT_COLLECTOR],
"title": "Environment Collector",
"safe": True,
"info": "Collects information about machine's environment (on " "premise/GCP/AWS).",
"attack_techniques": ["T1082"],
},
{ {
"type": "string", "type": "string",
"enum": [MIMIKATZ_COLLECTOR], "enum": [MIMIKATZ_COLLECTOR],

View File

@ -1,6 +1,5 @@
from common.common_consts.system_info_collectors_names import ( from common.common_consts.system_info_collectors_names import (
AWS_COLLECTOR, AWS_COLLECTOR,
ENVIRONMENT_COLLECTOR,
HOSTNAME_COLLECTOR, HOSTNAME_COLLECTOR,
MIMIKATZ_COLLECTOR, MIMIKATZ_COLLECTOR,
PROCESS_LIST_COLLECTOR, PROCESS_LIST_COLLECTOR,
@ -88,7 +87,6 @@ MONKEY = {
"uniqueItems": True, "uniqueItems": True,
"items": {"$ref": "#/definitions/system_info_collector_classes"}, "items": {"$ref": "#/definitions/system_info_collector_classes"},
"default": [ "default": [
ENVIRONMENT_COLLECTOR,
AWS_COLLECTOR, AWS_COLLECTOR,
HOSTNAME_COLLECTOR, HOSTNAME_COLLECTOR,
PROCESS_LIST_COLLECTOR, PROCESS_LIST_COLLECTOR,

View File

@ -1,12 +0,0 @@
import logging
from monkey_island.cc.models.monkey import Monkey
logger = logging.getLogger(__name__)
def process_environment_telemetry(collector_results, monkey_guid):
relevant_monkey = Monkey.get_single_monkey_by_guid(monkey_guid)
relevant_monkey.environment = collector_results["environment"]
relevant_monkey.save()
logger.debug("Updated Monkey {} with env {}".format(str(relevant_monkey), collector_results))

View File

@ -3,16 +3,12 @@ import typing
from common.common_consts.system_info_collectors_names import ( from common.common_consts.system_info_collectors_names import (
AWS_COLLECTOR, AWS_COLLECTOR,
ENVIRONMENT_COLLECTOR,
HOSTNAME_COLLECTOR, HOSTNAME_COLLECTOR,
PROCESS_LIST_COLLECTOR, PROCESS_LIST_COLLECTOR,
) )
from monkey_island.cc.services.telemetry.processing.system_info_collectors.aws import ( from monkey_island.cc.services.telemetry.processing.system_info_collectors.aws import (
process_aws_telemetry, process_aws_telemetry,
) )
from monkey_island.cc.services.telemetry.processing.system_info_collectors.environment import (
process_environment_telemetry,
)
from monkey_island.cc.services.telemetry.processing.system_info_collectors.hostname import ( from monkey_island.cc.services.telemetry.processing.system_info_collectors.hostname import (
process_hostname_telemetry, process_hostname_telemetry,
) )
@ -24,7 +20,6 @@ logger = logging.getLogger(__name__)
SYSTEM_INFO_COLLECTOR_TO_TELEMETRY_PROCESSORS = { SYSTEM_INFO_COLLECTOR_TO_TELEMETRY_PROCESSORS = {
AWS_COLLECTOR: [process_aws_telemetry], AWS_COLLECTOR: [process_aws_telemetry],
ENVIRONMENT_COLLECTOR: [process_environment_telemetry],
HOSTNAME_COLLECTOR: [process_hostname_telemetry], HOSTNAME_COLLECTOR: [process_hostname_telemetry],
PROCESS_LIST_COLLECTOR: [check_antivirus_existence], PROCESS_LIST_COLLECTOR: [check_antivirus_existence],
} }

View File

@ -81,7 +81,7 @@ const getContents = (props) => {
<p className="alert alert-info"> <p className="alert alert-info">
<FontAwesomeIcon icon={faInfoCircle} style={{'marginRight': '5px'}}/> <FontAwesomeIcon icon={faInfoCircle} style={{'marginRight': '5px'}}/>
Not sure what this is? Not seeing your AWS EC2 instances? <a Not sure what this is? Not seeing your AWS EC2 instances? <a
href="https://github.com/guardicore/monkey/wiki/Monkey-Island:-Running-the-monkey-on-AWS-EC2-instances" href="https://www.guardicore.com/infectionmonkey/docs/usage/integrations/aws-run-on-ec2-machine/"
rel="noopener noreferrer" target="_blank">Read the documentation</a>! rel="noopener noreferrer" target="_blank">Read the documentation</a>!
</p> </p>
</div> </div>

View File

@ -3,7 +3,6 @@ import requests
import requests_mock import requests_mock
from common.cloud.aws.aws_instance import AWS_LATEST_METADATA_URI_PREFIX, AwsInstance from common.cloud.aws.aws_instance import AWS_LATEST_METADATA_URI_PREFIX, AwsInstance
from common.cloud.environment_names import Environment
INSTANCE_ID_RESPONSE = "i-1234567890abcdef0" INSTANCE_ID_RESPONSE = "i-1234567890abcdef0"
@ -78,10 +77,6 @@ def test_is_instance_good_data(good_data_mock_instance):
assert good_data_mock_instance.is_instance() assert good_data_mock_instance.is_instance()
def test_get_cloud_provider_name_good_data(good_data_mock_instance):
assert good_data_mock_instance.get_cloud_provider_name() == Environment.AWS
def test_get_instance_id_good_data(good_data_mock_instance): def test_get_instance_id_good_data(good_data_mock_instance):
assert good_data_mock_instance.get_instance_id() == EXPECTED_INSTANCE_ID assert good_data_mock_instance.get_instance_id() == EXPECTED_INSTANCE_ID
@ -110,10 +105,6 @@ def test_is_instance_bad_region_data(bad_region_data_mock_instance):
assert bad_region_data_mock_instance.is_instance() assert bad_region_data_mock_instance.is_instance()
def test_get_cloud_provider_name_bad_region_data(bad_region_data_mock_instance):
assert bad_region_data_mock_instance.get_cloud_provider_name() == Environment.AWS
def test_get_instance_id_bad_region_data(bad_region_data_mock_instance): def test_get_instance_id_bad_region_data(bad_region_data_mock_instance):
assert bad_region_data_mock_instance.get_instance_id() == EXPECTED_INSTANCE_ID assert bad_region_data_mock_instance.get_instance_id() == EXPECTED_INSTANCE_ID
@ -142,10 +133,6 @@ def test_is_instance_bad_account_id_data(bad_account_id_data_mock_instance):
assert bad_account_id_data_mock_instance.is_instance() assert bad_account_id_data_mock_instance.is_instance()
def test_get_cloud_provider_name_bad_account_id_data(bad_account_id_data_mock_instance):
assert bad_account_id_data_mock_instance.get_cloud_provider_name() == Environment.AWS
def test_get_instance_id_bad_account_id_data(bad_account_id_data_mock_instance): def test_get_instance_id_bad_account_id_data(bad_account_id_data_mock_instance):
assert bad_account_id_data_mock_instance.get_instance_id() == EXPECTED_INSTANCE_ID assert bad_account_id_data_mock_instance.get_instance_id() == EXPECTED_INSTANCE_ID
@ -176,11 +163,6 @@ def test_is_instance_bad_instance_id_request(bad_instance_id_request_mock_instan
assert bad_instance_id_request_mock_instance.is_instance() is False assert bad_instance_id_request_mock_instance.is_instance() is False
@pytest.mark.parametrize("instance_id_exception", [requests.RequestException, IOError])
def test_get_cloud_provider_name_bad_instance_id_request(bad_instance_id_request_mock_instance):
assert bad_instance_id_request_mock_instance.get_cloud_provider_name() == Environment.AWS
@pytest.mark.parametrize("instance_id_exception", [requests.RequestException, IOError]) @pytest.mark.parametrize("instance_id_exception", [requests.RequestException, IOError])
def test_get_instance_id_bad_instance_id_request(bad_instance_id_request_mock_instance): def test_get_instance_id_bad_instance_id_request(bad_instance_id_request_mock_instance):
assert bad_instance_id_request_mock_instance.get_instance_id() is None assert bad_instance_id_request_mock_instance.get_instance_id() is None
@ -214,11 +196,6 @@ def test_is_instance_bad_region_request(bad_region_request_mock_instance):
assert bad_region_request_mock_instance.is_instance() assert bad_region_request_mock_instance.is_instance()
@pytest.mark.parametrize("region_exception", [requests.RequestException, IOError])
def test_get_cloud_provider_name_bad_region_request(bad_region_request_mock_instance):
assert bad_region_request_mock_instance.get_cloud_provider_name() == Environment.AWS
@pytest.mark.parametrize("region_exception", [requests.RequestException, IOError]) @pytest.mark.parametrize("region_exception", [requests.RequestException, IOError])
def test_get_instance_id_bad_region_request(bad_region_request_mock_instance): def test_get_instance_id_bad_region_request(bad_region_request_mock_instance):
assert bad_region_request_mock_instance.get_instance_id() == EXPECTED_INSTANCE_ID assert bad_region_request_mock_instance.get_instance_id() == EXPECTED_INSTANCE_ID
@ -252,11 +229,6 @@ def test_is_instance_bad_account_id_request(bad_account_id_request_mock_instance
assert bad_account_id_request_mock_instance.is_instance() assert bad_account_id_request_mock_instance.is_instance()
@pytest.mark.parametrize("account_id_exception", [requests.RequestException, IOError])
def test_get_cloud_provider_name_bad_account_id_request(bad_account_id_request_mock_instance):
assert bad_account_id_request_mock_instance.get_cloud_provider_name() == Environment.AWS
@pytest.mark.parametrize("account_id_exception", [requests.RequestException, IOError]) @pytest.mark.parametrize("account_id_exception", [requests.RequestException, IOError])
def test_get_instance_id_bad_account_id_request(bad_account_id_request_mock_instance): def test_get_instance_id_bad_account_id_request(bad_account_id_request_mock_instance):
assert bad_account_id_request_mock_instance.get_instance_id() == EXPECTED_INSTANCE_ID assert bad_account_id_request_mock_instance.get_instance_id() == EXPECTED_INSTANCE_ID
@ -296,10 +268,6 @@ def test_is_instance_not_found_request(not_found_request_mock_instance):
assert not_found_request_mock_instance.is_instance() is False assert not_found_request_mock_instance.is_instance() is False
def test_get_cloud_provider_name_not_found_request(not_found_request_mock_instance):
assert not_found_request_mock_instance.get_cloud_provider_name() == Environment.AWS
def test_get_instance_id_not_found_request(not_found_request_mock_instance): def test_get_instance_id_not_found_request(not_found_request_mock_instance):
assert not_found_request_mock_instance.get_instance_id() is None assert not_found_request_mock_instance.get_instance_id() is None

View File

@ -1,223 +0,0 @@
import pytest
import requests
import requests_mock
import simplejson
from common.cloud.azure.azure_instance import AZURE_METADATA_SERVICE_URL, AzureInstance
from common.cloud.environment_names import Environment
GOOD_DATA = {
"compute": {
"azEnvironment": "AZUREPUBLICCLOUD",
"isHostCompatibilityLayerVm": "true",
"licenseType": "Windows_Client",
"location": "westus",
"name": "examplevmname",
"offer": "Windows",
"osProfile": {
"adminUsername": "admin",
"computerName": "examplevmname",
"disablePasswordAuthentication": "true",
},
"osType": "linux",
"placementGroupId": "f67c14ab-e92c-408c-ae2d-da15866ec79a",
"plan": {"name": "planName", "product": "planProduct", "publisher": "planPublisher"},
"platformFaultDomain": "36",
"platformUpdateDomain": "42",
"publicKeys": [
{"keyData": "ssh-rsa 0", "path": "/home/user/.ssh/authorized_keys0"},
{"keyData": "ssh-rsa 1", "path": "/home/user/.ssh/authorized_keys1"},
],
"publisher": "RDFE-Test-Microsoft-Windows-Server-Group",
"resourceGroupName": "macikgo-test-may-23",
"resourceId": "/subscriptions/xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx/resourceGroups/macikgo-test"
"-may-23/"
"providers/Microsoft.Compute/virtualMachines/examplevmname",
"securityProfile": {"secureBootEnabled": "true", "virtualTpmEnabled": "false"},
"sku": "Windows-Server-2012-R2-Datacenter",
"storageProfile": {
"dataDisks": [
{
"caching": "None",
"createOption": "Empty",
"diskSizeGB": "1024",
"image": {"uri": ""},
"lun": "0",
"managedDisk": {
"id": "/subscriptions/xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx/"
"resourceGroups/macikgo-test-may-23/providers/"
"Microsoft.Compute/disks/exampledatadiskname",
"storageAccountType": "Standard_LRS",
},
"name": "exampledatadiskname",
"vhd": {"uri": ""},
"writeAcceleratorEnabled": "false",
}
],
"imageReference": {
"id": "",
"offer": "UbuntuServer",
"publisher": "Canonical",
"sku": "16.04.0-LTS",
"version": "latest",
},
"osDisk": {
"caching": "ReadWrite",
"createOption": "FromImage",
"diskSizeGB": "30",
"diffDiskSettings": {"option": "Local"},
"encryptionSettings": {"enabled": "false"},
"image": {"uri": ""},
"managedDisk": {
"id": "/subscriptions/xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx/"
"resourceGroups/macikgo-test-may-23/providers/"
"Microsoft.Compute/disks/exampleosdiskname",
"storageAccountType": "Standard_LRS",
},
"name": "exampleosdiskname",
"osType": "Linux",
"vhd": {"uri": ""},
"writeAcceleratorEnabled": "false",
},
},
"subscriptionId": "xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx",
"tags": "baz:bash;foo:bar",
"version": "15.05.22",
"vmId": "02aab8a4-74ef-476e-8182-f6d2ba4166a6",
"vmScaleSetName": "crpteste9vflji9",
"vmSize": "Standard_A3",
"zone": "",
},
"network": {
"interface": [
{
"ipv4": {
"ipAddress": [{"privateIpAddress": "10.144.133.132", "publicIpAddress": ""}],
"subnet": [{"address": "10.144.133.128", "prefix": "26"}],
},
"ipv6": {"ipAddress": []},
"macAddress": "0011AAFFBB22",
}
]
},
}
BAD_DATA_NOT_JSON = (
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" '
'"http://www.w3.org/TR/xhtml1/DTD/\
xhtml1-transitional.dtd">\n<html xmlns="http://www.w3.org/1999/xhtml">\n<head>\n<meta '
'content="text/html; charset=utf-8" \
http-equiv="Content-Type" />\n<meta content="no-cache" http-equiv="Pragma" '
"/>\n<title>Waiting...</title>\n<script type=\"text/\
javascript\">\nvar pageName = '/';\ntop.location.replace(pageName);\n</script>\n</head>\n<body> "
"</body>\n</html>\n"
)
BAD_DATA_JSON = {"": ""}
def get_test_azure_instance(url, **kwargs):
with requests_mock.Mocker() as m:
m.get(url, **kwargs)
test_azure_instance_object = AzureInstance()
return test_azure_instance_object
# good request, good data
@pytest.fixture
def good_data_mock_instance():
return get_test_azure_instance(AZURE_METADATA_SERVICE_URL, text=simplejson.dumps(GOOD_DATA))
def test_is_instance_good_data(good_data_mock_instance):
assert good_data_mock_instance.is_instance()
def test_get_cloud_provider_name_good_data(good_data_mock_instance):
assert good_data_mock_instance.get_cloud_provider_name() == Environment.AZURE
def test_try_parse_response_good_data(good_data_mock_instance):
assert good_data_mock_instance.instance_name == GOOD_DATA["compute"]["name"]
assert good_data_mock_instance.instance_id == GOOD_DATA["compute"]["vmId"]
assert good_data_mock_instance.location == GOOD_DATA["compute"]["location"]
# good request, bad data (json)
@pytest.fixture
def bad_data_json_mock_instance():
return get_test_azure_instance(AZURE_METADATA_SERVICE_URL, text=simplejson.dumps(BAD_DATA_JSON))
def test_is_instance_bad_data_json(bad_data_json_mock_instance):
assert bad_data_json_mock_instance.is_instance() is False
def test_get_cloud_provider_name_bad_data_json(bad_data_json_mock_instance):
assert bad_data_json_mock_instance.get_cloud_provider_name() == Environment.AZURE
def test_instance_attributes_bad_data_json(bad_data_json_mock_instance):
assert bad_data_json_mock_instance.instance_name is None
assert bad_data_json_mock_instance.instance_id is None
assert bad_data_json_mock_instance.location is None
# good request, bad data (not json)
@pytest.fixture
def bad_data_not_json_mock_instance():
return get_test_azure_instance(AZURE_METADATA_SERVICE_URL, text=BAD_DATA_NOT_JSON)
def test_is_instance_bad_data_not_json(bad_data_not_json_mock_instance):
assert bad_data_not_json_mock_instance.is_instance() is False
def test_get_cloud_provider_name_bad_data_not_json(bad_data_not_json_mock_instance):
assert bad_data_not_json_mock_instance.get_cloud_provider_name() == Environment.AZURE
def test_instance_attributes_bad_data_not_json(bad_data_not_json_mock_instance):
assert bad_data_not_json_mock_instance.instance_name is None
assert bad_data_not_json_mock_instance.instance_id is None
assert bad_data_not_json_mock_instance.location is None
# bad request
@pytest.fixture
def bad_request_mock_instance():
return get_test_azure_instance(AZURE_METADATA_SERVICE_URL, exc=requests.RequestException)
def test_is_instance_bad_request(bad_request_mock_instance):
assert bad_request_mock_instance.is_instance() is False
def test_get_cloud_provider_name_bad_request(bad_request_mock_instance):
assert bad_request_mock_instance.get_cloud_provider_name() == Environment.AZURE
def test_instance_attributes_bad_request(bad_request_mock_instance):
assert bad_request_mock_instance.instance_name is None
assert bad_request_mock_instance.instance_id is None
assert bad_request_mock_instance.location is None
# not found request
@pytest.fixture
def not_found_request_mock_instance():
return get_test_azure_instance(AZURE_METADATA_SERVICE_URL, status_code=404)
def test_is_instance_not_found_request(not_found_request_mock_instance):
assert not_found_request_mock_instance.is_instance() is False
def test_get_cloud_provider_name_not_found_request(not_found_request_mock_instance):
assert not_found_request_mock_instance.get_cloud_provider_name() == Environment.AZURE
def test_instance_attributes_not_found_request(not_found_request_mock_instance):
assert not_found_request_mock_instance.instance_name is None
assert not_found_request_mock_instance.instance_id is None
assert not_found_request_mock_instance.location is None

View File

@ -1,55 +0,0 @@
import pytest
import requests
import requests_mock
from common.cloud.environment_names import Environment
from common.cloud.gcp.gcp_instance import GCP_METADATA_SERVICE_URL, GcpInstance
def get_test_gcp_instance(url, **kwargs):
with requests_mock.Mocker() as m:
m.get(url, **kwargs)
test_gcp_instance_object = GcpInstance()
return test_gcp_instance_object
# good request
@pytest.fixture
def good_request_mock_instance():
return get_test_gcp_instance(GCP_METADATA_SERVICE_URL)
def test_is_instance_good_request(good_request_mock_instance):
assert good_request_mock_instance.is_instance()
def test_get_cloud_provider_name_good_request(good_request_mock_instance):
assert good_request_mock_instance.get_cloud_provider_name() == Environment.GCP
# bad request
@pytest.fixture
def bad_request_mock_instance():
return get_test_gcp_instance(GCP_METADATA_SERVICE_URL, exc=requests.RequestException)
def test_is_instance_bad_request(bad_request_mock_instance):
assert bad_request_mock_instance.is_instance() is False
def test_get_cloud_provider_name_bad_request(bad_request_mock_instance):
assert bad_request_mock_instance.get_cloud_provider_name() == Environment.GCP
# not found request
@pytest.fixture
def not_found_request_mock_instance():
return get_test_gcp_instance(GCP_METADATA_SERVICE_URL, status_code=404)
def test_is_instance_not_found_request(not_found_request_mock_instance):
assert not_found_request_mock_instance.is_instance() is False
def test_get_cloud_provider_name_not_found_request(not_found_request_mock_instance):
assert not_found_request_mock_instance.get_cloud_provider_name() == Environment.GCP

View File

@ -1,28 +0,0 @@
import uuid
from monkey_island.cc.models import Monkey
from monkey_island.cc.services.telemetry.processing.system_info_collectors.system_info_telemetry_dispatcher import ( # noqa: E501
SystemInfoTelemetryDispatcher,
)
class TestEnvironmentTelemetryProcessing:
def test_process_environment_telemetry(self):
# Arrange
monkey_guid = str(uuid.uuid4())
a_monkey = Monkey(guid=monkey_guid)
a_monkey.save()
dispatcher = SystemInfoTelemetryDispatcher()
on_premise = "On Premise"
telem_json = {
"data": {
"collectors": {
"EnvironmentCollector": {"environment": on_premise},
}
},
"monkey_guid": monkey_guid,
}
dispatcher.dispatch_collector_results_to_relevant_processors(telem_json)
assert Monkey.get_single_monkey_by_guid(monkey_guid).environment == on_premise

View File

@ -199,7 +199,6 @@ LOG_DIR_NAME # unused variable (envs/monkey_zoo/blackbox/log_handlers/test_logs
delete_logs # unused function (envs/monkey_zoo/blackbox/test_blackbox.py:85) delete_logs # unused function (envs/monkey_zoo/blackbox/test_blackbox.py:85)
MongoQueryJSONEncoder # unused class (envs/monkey_zoo/blackbox/utils/json_encoder.py:6) MongoQueryJSONEncoder # unused class (envs/monkey_zoo/blackbox/utils/json_encoder.py:6)
environment # unused variable (monkey/monkey_island/cc/models/monkey.py:59) environment # unused variable (monkey/monkey_island/cc/models/monkey.py:59)
_.environment # unused attribute (monkey/monkey_island/cc/services/telemetry/processing/system_info_collectors/environment.py:10)
_.instance_name # unused attribute (monkey/common/cloud/azure/azure_instance.py:35) _.instance_name # unused attribute (monkey/common/cloud/azure/azure_instance.py:35)
_.instance_name # unused attribute (monkey/common/cloud/azure/azure_instance.py:64) _.instance_name # unused attribute (monkey/common/cloud/azure/azure_instance.py:64)
GCPHandler # unused function (envs/monkey_zoo/blackbox/test_blackbox.py:57) GCPHandler # unused function (envs/monkey_zoo/blackbox/test_blackbox.py:57)