Esempio n. 1
0
    def __init__(self,
                 cache,
                 endpoint,
                 timeout=10,
                 try_num=3,
                 retry_backoff=2.,
                 dry_run=False,
                 _sleep_fn=time.sleep):
        """Initialize the router.

    Args:
      cache(dict): This must be config._cache. Passed as a parameter to
        avoid a circular import.
      endpoint(str or None): None means 'dry run'. What would be sent is printed
        on stdout. If endpoint starts with 'https://' data is POSTed there.
        Otherwise it is interpreted as a file where to write serialized
        LogEventLite protos.
      try_num(int): max number of http requests send to the endpoint.
      retry_backoff(float): time in seconds before retrying posting to the
         endpoint. Randomized exponential backoff is applied on subsequent
         retries.
      dry_run(boolean): if True, no http request is sent. Instead a message is
         printed.
      _sleep_fn (function): function to wait specified number of seconds. This
        argument is provided for testing purposes.
    """
        HTTP_IDENTIFIER = 'event_mon'
        _Router.__init__(self)
        self.endpoint = endpoint
        self.try_num = try_num
        self.retry_backoff = retry_backoff
        self._cache = cache
        self._http = infra_libs.InstrumentedHttp(HTTP_IDENTIFIER,
                                                 timeout=timeout)
        self._dry_run = dry_run
        self._sleep_fn = _sleep_fn

        # TODO(pgervais) pass this as parameters instead.
        if self._cache.get('service_account_creds'):
            try:
                logging.debug('Activating OAuth2 authentication.')
                self._http = infra_libs.get_authenticated_http(
                    self._cache['service_account_creds'],
                    service_accounts_creds_root=self.
                    _cache['service_accounts_creds_root'],
                    scope='https://www.googleapis.com/auth/cclog',
                    http_identifier=HTTP_IDENTIFIER,
                    timeout=timeout)
            except IOError:
                logging.error(
                    'Unable to read credentials, requests will be '
                    'unauthenticated. File: %s',
                    self._cache.get('service_account_creds'))
Esempio n. 2
0
  def __init__(self, cache, endpoint=None, timeout=10):
    # cache is defined in config.py. Passed as a parameter to avoid
    # a circular import.

    # endpoint == None means 'dry run'. No data is sent.
    self.endpoint = endpoint
    self.http = httplib2.Http(timeout=timeout)
    self.cache = cache

    if self.endpoint and self.cache['service_account_creds']:
      logging.debug('Activating OAuth2 authentication.')
      self.http = infra_libs.get_authenticated_http(
        self.cache['service_account_creds'],
        service_accounts_creds_root=self.cache['service_accounts_creds_root'],
        scope='https://www.googleapis.com/auth/cclog'
      )
Esempio n. 3
0
  def __init__(self, cache, endpoint, timeout=10, try_num=3, retry_backoff=2.,
               dry_run=False, _sleep_fn=time.sleep):
    """Initialize the router.

    Args:
      cache(dict): This must be config._cache. Passed as a parameter to
        avoid a circular import.
      endpoint(str or None): None means 'dry run'. What would be sent is printed
        on stdout. If endpoint starts with 'https://' data is POSTed there.
        Otherwise it is interpreted as a file where to write serialized
        LogEventLite protos.
      try_num(int): max number of http requests send to the endpoint.
      retry_backoff(float): time in seconds before retrying posting to the
         endpoint. Randomized exponential backoff is applied on subsequent
         retries.
      dry_run(boolean): if True, no http request is sent. Instead a message is
         printed.
      _sleep_fn (function): function to wait specified number of seconds. This
        argument is provided for testing purposes.
    """
    HTTP_IDENTIFIER = 'event_mon'
    _Router.__init__(self)
    self.endpoint = endpoint
    self.try_num = try_num
    self.retry_backoff = retry_backoff
    self._cache = cache
    self._http = infra_libs.InstrumentedHttp(HTTP_IDENTIFIER, timeout=timeout)
    self._dry_run = dry_run
    self._sleep_fn = _sleep_fn

    # TODO(pgervais) pass this as parameters instead.
    if self._cache.get('service_account_creds'):
      try:
        logging.debug('Activating OAuth2 authentication.')
        self._http = infra_libs.get_authenticated_http(
          self._cache['service_account_creds'],
          service_accounts_creds_root=
              self._cache['service_accounts_creds_root'],
          scope='https://www.googleapis.com/auth/cclog',
          http_identifier=HTTP_IDENTIFIER,
          timeout=timeout
        )
      except IOError:
        logging.error('Unable to read credentials, requests will be '
                      'unauthenticated. File: %s',
                      self._cache.get('service_account_creds'))
Esempio n. 4
0
 def test_malformed_credentials(self):
   with self.assertRaises(infra_libs.AuthError):
     infra_libs.get_authenticated_http(
       'creds_malformed.json',
       service_accounts_creds_root=DATA_DIR)
Esempio n. 5
0
 def test_valid_credentials_authenticated(self):
   http = infra_libs.get_authenticated_http(
     'valid_creds.json',
     service_accounts_creds_root=DATA_DIR,
     http_identifier='test_case')
   self.assertIsInstance(http, infra_libs.InstrumentedHttp)
Esempio n. 6
0
 def test_valid_credentials(self):
   http = infra_libs.get_authenticated_http(
     'valid_creds.json',
     service_accounts_creds_root=DATA_DIR)
   self.assertIsInstance(http, httplib2.Http)
Esempio n. 7
0
 def test_malformed_credentials(self):
   with self.assertRaises(infra_libs.AuthError):
     infra_libs.get_authenticated_http(
       'creds_malformed.json',
       service_accounts_creds_root=DATA_DIR)
Esempio n. 8
0
 def test_valid_credentials_authenticated(self):
   http = infra_libs.get_authenticated_http(
     'valid_creds.json',
     service_accounts_creds_root=DATA_DIR,
     http_identifier='test_case')
   self.assertIsInstance(http, infra_libs.InstrumentedHttp)
Esempio n. 9
0
 def test_valid_credentials(self):
   http = infra_libs.get_authenticated_http(
     'valid_creds.json',
     service_accounts_creds_root=DATA_DIR)
   self.assertIsInstance(http, httplib2.Http)