|
| 1 | +import logging |
| 2 | + |
| 3 | +from lib.cuckoo.common.config import Config |
| 4 | +from typing import List |
| 5 | + |
| 6 | +cfg = Config() |
| 7 | +HAVE_GCP = False |
| 8 | +if cfg.cuckoo.machinery == "gcp": |
| 9 | + try: |
| 10 | + from google.cloud import compute_v1 |
| 11 | + from google.oauth2 import service_account |
| 12 | + from google.auth import compute_engine |
| 13 | + |
| 14 | + HAVE_GCP = True |
| 15 | + except ImportError: |
| 16 | + pass |
| 17 | + |
| 18 | +from lib.cuckoo.common.abstracts import Machinery |
| 19 | +from lib.cuckoo.common.exceptions import CuckooMachineError, CuckooDependencyError |
| 20 | + |
| 21 | +log = logging.getLogger(__name__) |
| 22 | + |
| 23 | + |
| 24 | +class GCP(Machinery): |
| 25 | + |
| 26 | + module_name = "gcp" |
| 27 | + |
| 28 | + # VM states |
| 29 | + RUNNING = "RUNNING" |
| 30 | + PAUSED = "SUSPENDED" |
| 31 | + POWEROFF = "TERMINATED" |
| 32 | + PENDING = "PENDING" |
| 33 | + ABORTED = "ABORTED" |
| 34 | + ERROR = "ERROR" |
| 35 | + |
| 36 | + def _initialize_check(self): |
| 37 | + """Runs all checks when a machine manager is initialized. |
| 38 | + @raise CuckooDependencyError: if google-cloud-compute is not installed |
| 39 | + @raise CuckooMachineError: if configuration is invalid |
| 40 | + """ |
| 41 | + if not HAVE_GCP: |
| 42 | + raise CuckooDependencyError("Missed google-cloud-compute dependencies: poetry add google-cloud-compute") |
| 43 | + |
| 44 | + # Read Configuration |
| 45 | + self.project = self.options.gcp.project |
| 46 | + self.zone = self.options.gcp.zone |
| 47 | + self.json_key_path = getattr(self.options.gcp, "service_account_path", None) |
| 48 | + self.running_in_gcp = getattr(self.options.gcp, "running_in_gcp", False) |
| 49 | + |
| 50 | + log.info("Connecting to GCP Project: %s, Zone: %s", self.project, self.zone) |
| 51 | + |
| 52 | + # Initialize Clients |
| 53 | + if self.json_key_path: |
| 54 | + creds = service_account.Credentials.from_service_account_file(self.json_key_path) |
| 55 | + self.instances_client = compute_v1.InstancesClient(credentials=creds) |
| 56 | + elif self.running_in_gcp: |
| 57 | + log.info("Using Compute Engine credentials") |
| 58 | + creds = compute_engine.Credentials() |
| 59 | + self.instances_client = compute_v1.InstancesClient(credentials=creds) |
| 60 | + else: |
| 61 | + log.info("No Service Account JSON provided; using Application Default Credentials") |
| 62 | + self.instances_client = compute_v1.InstancesClient() |
| 63 | + |
| 64 | + super()._initialize_check() |
| 65 | + |
| 66 | + def _list(self) -> List[str]: |
| 67 | + """Lists virtual machines configured. |
| 68 | + """ |
| 69 | + try: |
| 70 | + request = compute_v1.ListInstancesRequest( |
| 71 | + project=self.project, |
| 72 | + zone=self.zone, |
| 73 | + ) |
| 74 | + instances = self.instances_client.list(request=request) |
| 75 | + return [instance.name for instance in instances] |
| 76 | + except Exception as e: |
| 77 | + raise CuckooMachineError(f"Failed to list instances in project '{self.project}' and zone '{self.zone}': {e}") from e |
| 78 | + |
| 79 | + def _status(self, label) -> str: |
| 80 | + """ |
| 81 | + Get current status of a VM |
| 82 | + @param label: virtual machine label |
| 83 | + @return: status string |
| 84 | + """ |
| 85 | + try: |
| 86 | + request = compute_v1.GetInstanceRequest( |
| 87 | + project=self.project, |
| 88 | + zone=self.zone, |
| 89 | + instance=label |
| 90 | + ) |
| 91 | + instance = self.instances_client.get(request=request) |
| 92 | + except Exception as e: |
| 93 | + raise CuckooMachineError(f"Error getting status for machine '{label}': {e}") from e |
| 94 | + |
| 95 | + # Reference: https://docs.cloud.google.com/compute/docs/instances/instance-lifecycle |
| 96 | + if instance.status in {"PENDING", "PROVISIONING", "STAGING", "REPAIRING"}: |
| 97 | + return self.PENDING |
| 98 | + elif instance.status == "RUNNING": |
| 99 | + return self.RUNNING |
| 100 | + elif instance.status in {"SUSPENDED", "SUSPENDING"}: |
| 101 | + return self.PAUSED |
| 102 | + elif instance.status == "TERMINATED": |
| 103 | + return self.POWEROFF |
| 104 | + elif instance.status in {"STOPPING", "PENDING_STOP"}: |
| 105 | + return self.ABORTED |
| 106 | + else: |
| 107 | + return self.ERROR |
| 108 | + |
| 109 | + def start(self, label): |
| 110 | + """ |
| 111 | + Start a virtual machine. |
| 112 | + @param label: virtual machine label. |
| 113 | + @raise CuckooMachineError: if unable to start. |
| 114 | + """ |
| 115 | + log.debug("Starting VM %s", label) |
| 116 | + try: |
| 117 | + if self._status(label) in (self.RUNNING, self.PENDING): |
| 118 | + log.warning("Trying to start a machine that is already running or pending: %s", label) |
| 119 | + return |
| 120 | + |
| 121 | + request = compute_v1.StartInstanceRequest( |
| 122 | + project=self.project, |
| 123 | + zone=self.zone, |
| 124 | + instance=label |
| 125 | + ) |
| 126 | + self.instances_client.start(request=request) |
| 127 | + except Exception as e: |
| 128 | + raise CuckooMachineError(f"Unable to start machine '{label}': {e}") from e |
| 129 | + |
| 130 | + def stop(self, label): |
| 131 | + """ |
| 132 | + Stop a virtual machine. |
| 133 | + @param label: virtual machine label. |
| 134 | + @raise CuckooMachineError: if unable to stop. |
| 135 | + """ |
| 136 | + log.debug("Stopping VM %s", label) |
| 137 | + try: |
| 138 | + if self._status(label) == self.POWEROFF: |
| 139 | + log.warning("Trying to stop a machine that is already stopped: %s", label) |
| 140 | + return |
| 141 | + |
| 142 | + request = compute_v1.StopInstanceRequest( |
| 143 | + project=self.project, |
| 144 | + zone=self.zone, |
| 145 | + instance=label |
| 146 | + ) |
| 147 | + self.instances_client.stop(request=request) |
| 148 | + except Exception as e: |
| 149 | + raise CuckooMachineError(f"Unable to stop machine '{label}': {e}") from e |
0 commit comments