Exemple #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_project = gcp.GoogleCloudProject(project_id, zone)
    disks = gcp.GoogleComputeInstance(gcp_project, zone,
                                      instance_name).ListDisks()

    # delete the created forensics VMs
    log.info('Deleting analysis instance: {0:s}.'.format(instance_name))
    gce_instances_client = gcp_project.GceApi().instances()
    request = gce_instances_client.delete(project=gcp_project.project_id,
                                          zone=gcp_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_project.GceApi().disks()
    for disk in disks[1:]:
        log.info('Deleting disk: {0:s}.'.format(disk))
        while True:
            try:
                request = gce_disks_client.delete(
                    project=gcp_project.project_id,
                    zone=gcp_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))
import os
import unittest
import re

from googleapiclient.errors import HttpError

import mock
import six

from libcloudforensics import gcp

# For the forensics analysis
FAKE_ANALYSIS_PROJECT = gcp.GoogleCloudProject('fake-target-project',
                                               'fake-zone')
FAKE_ANALYSIS_VM = gcp.GoogleComputeInstance(FAKE_ANALYSIS_PROJECT,
                                             'fake-zone', 'fake-analysis-vm')

# Source project with the instance that needs forensicating
FAKE_SOURCE_PROJECT = gcp.GoogleCloudProject('fake-source-project',
                                             'fake-zone')
FAKE_INSTANCE = gcp.GoogleComputeInstance(FAKE_SOURCE_PROJECT, 'fake-zone',
                                          'fake-instance')
FAKE_DISK = gcp.GoogleComputeDisk(FAKE_SOURCE_PROJECT, 'fake-zone',
                                  'fake-disk')
FAKE_BOOT_DISK = gcp.GoogleComputeDisk(FAKE_SOURCE_PROJECT, 'fake-zone',
                                       'fake-boot-disk')
FAKE_SNAPSHOT = gcp.GoogleComputeSnapshot(FAKE_DISK, 'fake-snapshot')
FAKE_SNAPSHOT_LONG_NAME = gcp.GoogleComputeSnapshot(
    FAKE_DISK,
    'this-is-a-kind-of-long-fake-snapshot-name-and-is-definitely-over-63-chars'
)