Пример #1
0
def CleanUp(project_id, zone, instance_name):
    """Clean up GCP project.

  Remove the instance [instance_name] in the GCP project [project_id] and its
  disks that were created as part of the end to end test.

  Attributes:
    project_id (str): the project id of the GCP project.
    zone (str): the zone for the project.
    instance_name (str): the name of the analysis VM to remove.
  """

    gcp_client = common.GoogleCloudComputeClient(project_id=project_id)
    project = gcp_project.GoogleCloudProject(project_id, zone)
    disks = compute.GoogleComputeInstance(project.project_id, zone,
                                          instance_name).ListDisks()

    # delete the created forensics VMs
    log.info('Deleting analysis instance: {0:s}.'.format(instance_name))
    gce_instances_client = gcp_client.GceApi().instances()
    request = gce_instances_client.delete(project=project.project_id,
                                          zone=project.default_zone,
                                          instance=instance_name)
    try:
        request.execute()
    except HttpError:
        # GceOperation triggers a while(True) loop that checks on the
        # operation ID. Sometimes it loops one more time right when the
        # operation has finished and thus the associated ID doesn't exists
        # anymore, throwing an HttpError. We can ignore this.
        pass
    log.info('Instance {0:s} successfully deleted.'.format(instance_name))

    # delete the copied disks
    # we ignore the disk that was created for the analysis VM (disks[0]) as
    # it is deleted in the previous operation
    gce_disks_client = gcp_client.GceApi().disks()
    for disk in list(disks.keys())[1:]:
        log.info('Deleting disk: {0:s}.'.format(disk))
        while True:
            try:
                request = gce_disks_client.delete(project=project.project_id,
                                                  zone=project.default_zone,
                                                  disk=disk)
                request.execute()
                break
            except HttpError as exception:
                # GceApi() will throw a 400 error until the analysis VM deletion is
                # correctly propagated. When the disk is finally deleted, it will
                # throw a 404 not found if it looped again after deletion.
                if exception.resp.status == 404:
                    break
                if exception.resp.status != 400:
                    log.warning(
                        'Could not delete the disk {0:s}: {1:s}'.format(
                            disk, str(exception)))
                # Throttle the requests to one every 10 seconds
                time.sleep(10)

        log.info('Disk {0:s} successfully deleted.'.format(disk))
Пример #2
0
import unittest

import mock
from libcloudforensics.providers.gcp.internal import project as gcp_project
from libcloudforensics.providers.gcp.internal import compute

from dftimewolf import config
from dftimewolf.lib import state
from dftimewolf.lib.containers import containers
from dftimewolf.lib.collectors import gcloud

FAKE_PROJECT = gcp_project.GoogleCloudProject('test-target-project-name',
                                              'fake_zone')
FAKE_ANALYSIS_VM = compute.GoogleComputeInstance(FAKE_PROJECT.project_id,
                                                 'fake_zone',
                                                 'fake-analysis-vm')
FAKE_INSTANCE = compute.GoogleComputeInstance(FAKE_PROJECT.project_id,
                                              'fake_zone', 'fake-instance')
FAKE_DISK = compute.GoogleComputeDisk(FAKE_PROJECT.project_id, 'fake_zone',
                                      'disk1')
FAKE_BOOT_DISK = compute.GoogleComputeDisk(FAKE_PROJECT.project_id,
                                           'fake_zone', 'bootdisk')
FAKE_SNAPSHOT = compute.GoogleComputeSnapshot(FAKE_DISK, 'fake_snapshot')
FAKE_DISK_COPY = compute.GoogleComputeDisk(FAKE_PROJECT.project_id,
                                           'fake_zone', 'disk1-copy')


class GoogleCloudCollectorTest(unittest.TestCase):
    """Tests for the GCloud collector."""
    def testInitialization(self):