示例#1
0
    def __init__(self,
                 credsfile,
                 project,
                 topic,
                 use_instrumented_http=True,
                 ca_certs=None):
        """Process monitoring related command line flags and initialize api.

    Args:
      credsfile (str): path to the credentials json file
      project (str): the name of the Pub/Sub project to publish to.
      topic (str): the name of the Pub/Sub topic to publish to.
      use_instrumented_http (bool): whether to record monitoring metrics for
          HTTP requests made to the pubsub API.
      ca_certs (str): path to file containing root CA certificates for SSL
                      server certificate validation. If not set, a CA cert
                      file bundled with httplib2 is used.
    """
        # Do not call self._check_initialize() in the constructor. This
        # class is constructed during app initialization on AppEngine, and
        # network calls are especially flaky during that time.
        self._api = None
        self._use_instrumented_http = use_instrumented_http
        if use_instrumented_http:
            self._http = httplib2_utils.InstrumentedHttp('acq-mon-api-pubsub',
                                                         timeout=self.TIMEOUT,
                                                         ca_certs=ca_certs)
        else:
            self._http = httplib2.Http(timeout=self.TIMEOUT, ca_certs=ca_certs)
        self._credsfile = credsfile
        self._topic = 'projects/%s/topics/%s' % (project, topic)
示例#2
0
 def __init__(self, endpoint, credentials_file_path, http=None):
     self._endpoint = endpoint
     credentials = self._load_credentials(credentials_file_path)
     if http is None:
         http = httplib2_utils.RetriableHttp(
             httplib2_utils.InstrumentedHttp('acq-mon-api'))
     self._http = credentials.authorize(http)
 def create(self, scopes):
     logging.info('Delegating to service account %s',
                  self.service_account_email)
     http = httplib2_utils.InstrumentedHttp('actor-credentials')
     http = self.base.create([self.IAM_SCOPE]).authorize(http)
     return httplib2_utils.DelegateServiceAccountCredentials(
         http, self.service_account_email, scopes)
示例#4
0
 def __init__(self, endpoint, credential_factory, http=None, ca_certs=None):
   self._endpoint = endpoint
   credentials = credential_factory.create(self._SCOPES)
   if http is None:
     http = httplib2_utils.RetriableHttp(
         httplib2_utils.InstrumentedHttp('acq-mon-api', ca_certs=ca_certs))
   self._http = credentials.authorize(http)
示例#5
0
 def setUp(self):
     super(InstrumentedHttplib2Test, self).setUp()
     self.mock_time = mock.Mock()
     self.mock_time.return_value = 42
     self.http = httplib2_utils.InstrumentedHttp('test',
                                                 time_fn=self.mock_time)
     http_metrics._reset_for_testing()
     self.http._request = mock.Mock()
示例#6
0
    def __init__(self, credsfile, project, topic, use_instrumented_http=True):
        """Process monitoring related command line flags and initialize api.

    Args:
      credsfile (str): path to the credentials json file
      project (str): the name of the Pub/Sub project to publish to.
      topic (str): the name of the Pub/Sub topic to publish to.
      use_instrumented_http (bool): whether to record monitoring metrics for
          HTTP requests made to the pubsub API.
    """
        self._api = None
        self._use_instrumented_http = use_instrumented_http
        if use_instrumented_http:
            self._http = httplib2_utils.InstrumentedHttp('acq-mon-api-pubsub',
                                                         timeout=self.TIMEOUT)
        else:
            self._http = httplib2.Http(timeout=self.TIMEOUT)
        self._credsfile = credsfile
        self._topic = 'projects/%s/topics/%s' % (project, topic)
        self._check_initialize()
示例#7
0
    def __init__(self,
                 project_name,
                 credential_store='monorail.dat',
                 client_id="",
                 client_secret="",
                 service_acct=False,
                 discovery_url=MONORAIL_PROD_URL):
        '''
     Constructor
    '''
        self.project_name = project_name
        self._empty_owner_value = ''

        with open(credential_store) as data_file:
            creds_data = json.load(data_file)

        credentials = OAuth2Credentials(
            None, creds_data['client_id'], creds_data['client_secret'],
            creds_data['refresh_token'], None,
            'https://accounts.google.com/o/oauth2/token',
            'python-issue-tracker-manager/2.0')

        if credentials is None or credentials.invalid == True:
            if not client_id or not client_secret:
                raise Exception(
                    'Failed to create credentials from credential store: %s. '
                    'To authenticate and write fresh credentials to the store, '
                    'create MonorailIssueTrackerManager with valid |client_id| '
                    'and |client_secret| arguments.' % credential_store)
            api_scope = 'https://www.googleapis.com/auth/userinfo.email'
            credentials = self._authenticate(storage=None,
                                             service_acct=service_acct,
                                             client_id=client_id,
                                             client_secret=client_secret,
                                             api_scope=api_scope)

        http = httplib2_utils.InstrumentedHttp('monorail:%s' %
                                               self.project_name)
        http = credentials.authorize(http)

        self.client = build_client(discovery_url, http, 'monorail', 'v1')
示例#8
0
    def __init__(self, api_url, logger=None):
        self._api_url = api_url
        self.logger = logger or DEFAULT_LOGGER
        self.headers = {}

        # Add a Basic auth handler using credentials from netrc.
        urlp = urlparse.urlparse(self._api_url)
        self.http = httplib2_utils.InstrumentedHttp('gob:%s' % urlp.path)

        credentials = creds_service.get_luci_credentials()
        if credentials:
            self.http = credentials.authorize(self.http)
        else:
            try:
                nrc = netrc.netrc()
            except IOError:
                logging.exception('Failed to authenticate REST API client')
            else:
                if urlp.netloc in nrc.hosts:
                    self.headers['Authorization'] = 'OAuth %s' % nrc.hosts[
                        urlp.netloc][2]
                else:
                    logging.warning('No auth token found for host %s!' %
                                    urlp.netloc)
示例#9
0
 def test_instrumented_http_send(self):
   self._test_send(httplib2_utils.InstrumentedHttp('test'))
示例#10
0
 def _http_request_builder(self, _http, *args, **kwargs):
     http = httplib2_utils.InstrumentedHttp('monorail')
     http = self._credentials.authorize(http)
     http = SSLErrorLoggingHttp(http)
     return apiclient.http.HttpRequest(http, *args, **kwargs)