예제 #1
0
def EffectiveProxyInfo():
    """Returns ProxyInfo effective in gcloud and if it is from gloud properties.

  Returns:
    A tuple of two elements in which the first element is an httplib2.ProxyInfo
      object and the second is a bool that is True if the proxy info came from
      previously set Cloud SDK proxy properties.

  Raises:
    properties.InvalidValueError: If the properties did not include a valid set.
      "Valid" means all three of these attributes are present: proxy type, host,
      and port.
  """
    proxy_info = http_proxy.GetHttpProxyInfo()  # raises InvalidValueError
    if not proxy_info:
        return None, False

    # googlecloudsdk.core.http_proxy.GetHttpProxyInfo() will return a function
    # if there are no valid proxy settings in gcloud properties. Otherwise, it
    # will return an instantiated httplib2.ProxyInfo object.
    from_gcloud_properties = True
    if not isinstance(proxy_info, httplib2.ProxyInfo):
        from_gcloud_properties = False
        # All Google Cloud SDK network calls use https.
        proxy_info = proxy_info('https')

    return proxy_info, from_gcloud_properties
예제 #2
0
 def build(host, **kwargs):
     proxy_info = http_proxy.GetHttpProxyInfo()
     if callable(proxy_info):
         proxy_info = proxy_info('https')
     return httplib2.HTTPSConnectionWithTimeout(host,
                                                proxy_info=proxy_info,
                                                **kwargs)
예제 #3
0
def Http(timeout='unset'):
    """Get an httplib2.Http client that is properly configured for use by gcloud.

  This method does not add credentials to the client.  For an Http client that
  has been authenticated, use core.credentials.http.Http().

  Args:
    timeout: double, The timeout in seconds to pass to httplib2.  This is the
        socket level timeout.  If timeout is None, timeout is infinite.  If
        default argument 'unset' is given, a sensible default is selected.

  Returns:
    An httplib2.Http client object configured with all the required settings
    for gcloud.
  """
    # Compared with setting the default timeout in the function signature (i.e.
    # timeout=300), this lets you test with short default timeouts by mocking
    # GetDefaultTimeout.
    effective_timeout = timeout if timeout != 'unset' else GetDefaultTimeout()
    no_validate = properties.VALUES.auth.disable_ssl_validation.GetBool()
    ca_certs = properties.VALUES.core.custom_ca_certs_file.Get()
    http_client = httplib2.Http(timeout=effective_timeout,
                                proxy_info=http_proxy.GetHttpProxyInfo(),
                                ca_certs=ca_certs,
                                disable_ssl_certificate_validation=no_validate)

    # Wrap the request method to put in our own user-agent, and trace reporting.
    gcloud_ua = MakeUserAgentString(
        properties.VALUES.metrics.command_name.Get())
    http_client = _Wrap(http_client, properties.VALUES.core.trace_token.Get(),
                        properties.VALUES.core.trace_email.Get(),
                        properties.VALUES.core.trace_log.GetBool(), gcloud_ua,
                        properties.VALUES.core.log_http.GetBool())

    return http_client
예제 #4
0
def _CheckGcloudProxyInfo():
    """Gets ProxyInfo effective in gcloud and whether it is from gloud properties.

  Returns:
    tuple of (httplib2.ProxyInfo, bool): First entry is proxy information, and
      second is whether that proxy information came from previously set Cloud
      SDK proxy properties.

  Raises:
    properties.InvalidValueError: If the properties did not include a valid set.
      "Valid" means all three of these attributes are present: proxy type, host,
      and port.
  """
    proxy_info = http_proxy.GetHttpProxyInfo()  # raises InvalidValueError
    if not proxy_info:
        return None, False

    # googlecloudsdk.core.http_proxy.GetHttpProxyInfo() will return a function
    # if there are no valid proxy settings in gcloud properties. Otherwise, it
    # will return an instantiated httplib2.ProxyInfo object.
    from_gcloud_properties = True
    if callable(proxy_info):
        from_gcloud_properties = False
        # All Google Cloud SDK network calls use https.
        proxy_info = proxy_info('https')

    return proxy_info, from_gcloud_properties
예제 #5
0
 def testProxyHttpProxyInfo(self):
   properties.VALUES.proxy.proxy_type.Set('socks4')
   properties.VALUES.proxy.address.Set('123.123.123.123')
   properties.VALUES.proxy.port.Set('4321')
   pi = http_proxy.GetHttpProxyInfo()
   self.assertEqual((socks.PROXY_TYPE_SOCKS4, '123.123.123.123', 4321, True,
                     None, None, None), pi.astuple())
   self.assertTrue(pi.isgood())
예제 #6
0
 def build(host, **kwargs):
   proxy_info = http_proxy.GetHttpProxyInfo()
   if callable(proxy_info):
     proxy_info = proxy_info('http')
   return httplib2.HTTPConnectionWithTimeout(
       host.encode('idna').decode(),
       proxy_info=proxy_info,
       **kwargs)
예제 #7
0
 def testProxyHttpProxyInfoNoType(self):
   properties.VALUES.proxy.proxy_type.Set(None)
   properties.VALUES.proxy.address.Set('123.123.123.123')
   properties.VALUES.proxy.port.Set('4321')
   with self.assertRaisesRegex(
       properties.InvalidValueError,
       'Please set all or none of the following properties: '
       'proxy/type, proxy/address and proxy/port'):
     unused_pi = http_proxy.GetHttpProxyInfo()
예제 #8
0
 def build(host, **kwargs):
     proxy_info = http_proxy.GetHttpProxyInfo()
     if callable(proxy_info):
         proxy_info = proxy_info('https')
     ca_certs = properties.VALUES.core.custom_ca_certs_file.Get()
     return httplib2.HTTPSConnectionWithTimeout(host.encode('idna'),
                                                proxy_info=proxy_info,
                                                ca_certs=ca_certs,
                                                **kwargs)
예제 #9
0
 def _GetTunnelTargetInfo(self):
   proxy_info = http_proxy.GetHttpProxyInfo()
   if callable(proxy_info):
     proxy_info = proxy_info(method='https')
   return utils.IapTunnelTargetInfo(project=self._project,
                                    zone=self._zone,
                                    instance=self._instance,
                                    interface=self._interface,
                                    port=self._port,
                                    url_override=self._iap_tunnel_url_override,
                                    proxy_info=proxy_info)
예제 #10
0
파일: http.py 프로젝트: barber223/AudioApp
def _CreateRawHttpClient(timeout='unset'):
    # Compared with setting the default timeout in the function signature (i.e.
    # timeout=300), this lets you test with short default timeouts by mocking
    # GetDefaultTimeout.
    effective_timeout = timeout if timeout != 'unset' else GetDefaultTimeout()
    no_validate = properties.VALUES.auth.disable_ssl_validation.GetBool(
    ) or False
    ca_certs = properties.VALUES.core.custom_ca_certs_file.Get()
    return httplib2.Http(timeout=effective_timeout,
                         proxy_info=http_proxy.GetHttpProxyInfo(),
                         ca_certs=ca_certs,
                         disable_ssl_certificate_validation=no_validate)
def ReportMetrics(metrics_file_path):
  """Sends the specified anonymous usage event to the given analytics endpoint.

  Args:
      metrics_file_path: str, File with pickled metrics (list of tuples).
  """
  with files.BinaryFileReader(metrics_file_path) as metrics_file:
    metrics = pickle.load(metrics_file)
  os.remove(metrics_file_path)

  http = httplib2.Http(timeout=TIMEOUT_IN_SEC,
                       proxy_info=http_proxy.GetHttpProxyInfo())

  for metric in metrics:
    http.request(metric[0], method=metric[1], body=metric[2], headers=metric[3])
예제 #12
0
 def _GetTunnelTargetInfo(self):
     proxy_info = http_proxy.GetHttpProxyInfo()
     if callable(proxy_info):
         proxy_info = proxy_info(method='https')
     return utils.IapTunnelTargetInfo(
         project=self._project,
         zone=self._zone,
         instance=self._instance,
         interface=self._interface,
         port=self._port,
         url_override=self._iap_tunnel_url_override,
         proxy_info=proxy_info,
         region=self._region,
         network=self._network,
         host=self._host,
         dest_group=self._dest_group)
예제 #13
0
def _CreateRawHttpClient(timeout='unset', ca_certs=None):
  """Create an HTTP client matching the appropriate gcloud properties."""
  # Compared with setting the default timeout in the function signature (i.e.
  # timeout=300), this lets you test with short default timeouts by mocking
  # GetDefaultTimeout.
  effective_timeout = timeout if timeout != 'unset' else GetDefaultTimeout()
  no_validate = properties.VALUES.auth.disable_ssl_validation.GetBool() or False
  ca_certs_property = properties.VALUES.core.custom_ca_certs_file.Get()
  # Believe an explicitly-set ca_certs property over anything we added.
  if ca_certs_property:
    ca_certs = ca_certs_property
  if no_validate:
    ca_certs = None
  return HttpClient(timeout=effective_timeout,
                    proxy_info=http_proxy.GetHttpProxyInfo(),
                    ca_certs=ca_certs,
                    disable_ssl_certificate_validation=no_validate)
예제 #14
0
 def testChangeExistingProxyFromEnvVar(self):
   self.StartEnvPatch(
       {'http_proxy': 'https://*****:*****@badproxy:8080',
        'https_proxy': 'https://*****:*****@badproxy:8081'})
   pi = http_proxy.GetHttpProxyInfo()
   self.assertTrue(callable(pi))
   self.assertEqual(
       (socks.PROXY_TYPE_HTTP, 'badproxy', 8080, True, 'baduser',
        'badpassword', None),
       pi('http').astuple())
   self.assertEqual(
       (socks.PROXY_TYPE_HTTP, 'badproxy', 8081, True, 'baduser',
        'badpassword', None),
       pi('https').astuple())
   self.AssertNoGloudProxyProperties()
   self.setAnswers('Y', '1', 'golden', '80', 'Y', 'username', 'password')
   self.assertTrue(http_proxy_setup.ChangeGcloudProxySettings())
   self.AssertErrContains(self._ADD_NEW_PROXY_PROMPT)
   self.AssertErrNotContains(self._CHANGE_EXISTING_MENU)
   self.AssertErrContains('Cloud SDK proxy properties set.')
   self.AssertGoldenProxyProperties(with_auth=True)
예제 #15
0
 def testProxyHttpProxyInfoEnvVar(self):
   self.StartEnvPatch({
       'HTTP_PROXY': 'http://*****:*****@awesome_proxy:8080',
       'HTTPS_PROXY': 'http://*****:*****@secure_proxy:8081',
       'NO_PROXY': 'someaddress.com,otheraddress.com'
   })
   pi = http_proxy.GetHttpProxyInfo()
   self.assertTrue(callable(pi))
   self.assertEqual((socks.PROXY_TYPE_HTTP, 'awesome_proxy', 8080, True,
                     'user', 'pass', None),
                    pi('http').astuple())
   self.assertEqual((socks.PROXY_TYPE_HTTP, 'secure_proxy', 8081, True,
                     'suser', 'spass', None),
                    pi('https').astuple())
   self.assertTrue(pi('http').isgood())
   self.assertTrue(pi('https').isgood())
   self.assertTrue(pi('http').bypass_host('someaddress.com'))
   self.assertTrue(pi('https').bypass_host('someaddress.com'))
   self.assertTrue(pi('http').bypass_host('otheraddress.com'))
   self.assertTrue(pi('https').bypass_host('otheraddress.com'))
   self.assertFalse(pi('http').bypass_host('google.com'))
   self.assertFalse(pi('https').bypass_host('google.com'))
예제 #16
0
 def testProxyHttpProxyInfoNoProperties(self):
   pi = http_proxy.GetHttpProxyInfo()
   self.assertTrue(callable(pi))
   self.assertEqual(None, pi('http'))