Exemplo n.º 1
0
    def _ProbeObjectAccessWithClient(self, key, client_email, gcs_path, logger,
                                     region):
        """Performs a head request against a signed url to check for read access."""

        # Choose a reasonable time in the future; if the user's system clock is
        # 60 or more seconds behind the server's this will generate an error.
        signed_url = _GenSignedUrl(key, client_email, 'HEAD',
                                   timedelta(seconds=60), gcs_path, logger,
                                   region)

        try:
            h = GetNewHttp()
            req = Request(signed_url, 'HEAD')
            response = MakeRequest(h, req)

            if response.status_code not in [200, 403, 404]:
                raise HttpError.FromResponse(response)

            return response.status_code
        except HttpError as http_error:
            if http_error.has_attr('response'):
                error_response = http_error.response
                error_string = (
                    'Unexpected HTTP response code %s while querying '
                    'object readability. Is your system clock accurate?' %
                    error_response.status_code)
                if error_response.content:
                    error_string += ' Content: %s' % error_response.content
            else:
                error_string = (
                    'Expected an HTTP response code of '
                    '200 while querying object readability, but received '
                    'an error: %s' % http_error)
            raise CommandException(error_string)
def ReportMetrics(metrics_file_path, log_level, log_file_path=None):
    """Sends the specified anonymous usage event to the given analytics endpoint.

  Args:
      metrics_file_path: str, File with pickled metrics (list of tuples).
      log_level: int, The logging level of gsutil's root logger.
      log_file_path: str, The file that this module should write its logs to.
        This parameter is intended for use by tests that need to evaluate the
        contents of the file at this path.

  """
    logger = logging.getLogger()
    if log_file_path is not None:
        # Use a separate logger so that we don't add another handler to the default
        # module-level logger. This is intended to prevent multiple calls from tests
        # running in parallel from writing output to the same file.
        new_name = '%s.%s' % (logger.name, ''.join(
            random.choice(string.ascii_lowercase) for _ in range(8)))
        logger = logging.getLogger(new_name)

    log_file_path = log_file_path or LOG_FILE_PATH
    log_file_parent_dir = os.path.dirname(log_file_path)
    CreateDirIfNeeded(log_file_parent_dir)
    handler = logging.FileHandler(log_file_path, mode='w')
    logger.addHandler(handler)
    logger.setLevel(log_level)

    with open(metrics_file_path, 'rb') as metrics_file:
        metrics = pickle.load(metrics_file)
    os.remove(metrics_file_path)

    ConfigureCertsFile()
    http = GetNewHttp()

    for metric in metrics:
        try:
            headers = {'User-Agent': metric.user_agent}
            response = http.request(metric.endpoint,
                                    method=metric.method,
                                    body=metric.body,
                                    headers=headers)
            logger.debug(metric)
            logger.debug('RESPONSE: %s', response[0]['status'])
        except Exception as e:  # pylint: disable=broad-except
            logger.debug(e)
    def __init__(self, logger=None, credentials=None, debug=0):
        """Performs necessary setup for interacting with Google Cloud IAM
    Credentials.

    Args:
      logger: logging.logger for outputting log messages.
      credentials: Credentials to be used for interacting with Cloud IAM
      debug: Debug level for the API implementation (0..3).
    """
        super(IamcredentailsApi, self).__init__()

        self.logger = logger
        self.credentials = credentials

        self.certs_file = GetCertsFile()
        self.http = GetNewHttp()
        self.http_base = 'https://'
        self.host_base = config.get('Credentials', 'gs_iamcredentails_host',
                                    'iamcredentials.googleapis.com')
        gs_iamcred_port = config.get('Credentials', 'gs_iamcredentails_port',
                                     None)
        self.host_port = (':' + gs_iamcred_port) if gs_iamcred_port else ''
        self.url_base = (self.http_base + self.host_base + self.host_port)

        log_request = (debug >= 3)
        log_response = (debug >= 3)

        self.api_client = apitools_client.IamcredentialsV1(
            url=self.url_base,
            http=self.http,
            log_request=log_request,
            log_response=log_response,
            credentials=self.credentials)

        self.num_retries = GetNumRetries()
        self.api_client.num_retries = self.num_retries

        self.max_retry_wait = GetMaxRetryDelay()
        self.api_client.max_retry_wait = self.max_retry_wait

        if isinstance(self.credentials, NoOpCredentials):
            # This API key is not secret and is used to identify gsutil during
            # anonymous requests.
            self.api_client.AddGlobalParam(
                'key', u'AIzaSyDnacJHrKma0048b13sh8cgxNUwulubmJM')