diff --git a/monkey/common/cloud/all_instances.py b/monkey/common/cloud/all_instances.py index 986bf1a80..6387730f6 100644 --- a/monkey/common/cloud/all_instances.py +++ b/monkey/common/cloud/all_instances.py @@ -2,9 +2,10 @@ 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()] +all_cloud_instances = [AwsInstance(), AzureInstance(), GcpInstance()] def get_all_cloud_instances() -> List[CloudInstance]: diff --git a/monkey/common/cloud/gcp/__init__.py b/monkey/common/cloud/gcp/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/monkey/common/cloud/gcp/gcp_instance.py b/monkey/common/cloud/gcp/gcp_instance.py new file mode 100644 index 000000000..5f04aabd0 --- /dev/null +++ b/monkey/common/cloud/gcp/gcp_instance.py @@ -0,0 +1,41 @@ +import logging +import requests + +from common.cloud.environment_names import GCP +from common.cloud.instance import CloudInstance + +logger = logging.getLogger(__name__) +GCP_METADATA_SERVICE_URL = "http://metadata.google.internal/" + + +class GcpInstance(CloudInstance): + def is_instance(self): + return self.on_gcp + + def get_cloud_provider_name(self) -> str: + return GCP + + def __init__(self): + """ + Determines if on GCP. + """ + self.on_gcp = False + + try: + # If not on GCP, this domain shouldn't resolve. + response = requests.get(GCP_METADATA_SERVICE_URL) + + if response: + logger.debug("Got response, so probably on GCP. Trying to parse.") + 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