| OLD | NEW |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import collections | 5 import collections |
| 6 import copy | 6 import copy |
| 7 import json | 7 import json |
| 8 import logging | 8 import logging |
| 9 import os | 9 import os |
| 10 import re | 10 import re |
| 11 import socket | 11 import socket |
| 12 import sys | 12 import sys |
| 13 import time | 13 import time |
| 14 | 14 |
| 15 import httplib2 | 15 import httplib2 |
| 16 import oauth2client.client | 16 import oauth2client.client |
| 17 | 17 |
| 18 from infra_libs.ts_mon.common import http_metrics | 18 from infra_libs.ts_mon.common import http_metrics |
| 19 | 19 |
| 20 DEFAULT_SCOPES = ['email'] | 20 DEFAULT_SCOPES = ['email'] |
| 21 | 21 |
| 22 # default timeout for http requests, in seconds |
| 23 DEFAULT_TIMEOUT = 30 |
| 24 |
| 22 # This is part of the API. | 25 # This is part of the API. |
| 23 if sys.platform.startswith('win'): # pragma: no cover | 26 if sys.platform.startswith('win'): # pragma: no cover |
| 24 SERVICE_ACCOUNTS_CREDS_ROOT = 'C:\\creds\\service_accounts' | 27 SERVICE_ACCOUNTS_CREDS_ROOT = 'C:\\creds\\service_accounts' |
| 25 else: | 28 else: |
| 26 SERVICE_ACCOUNTS_CREDS_ROOT = '/creds/service_accounts' | 29 SERVICE_ACCOUNTS_CREDS_ROOT = '/creds/service_accounts' |
| 27 | 30 |
| 28 | 31 |
| 29 class AuthError(Exception): | 32 class AuthError(Exception): |
| 30 pass | 33 pass |
| 31 | 34 |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 service_accounts_creds_root=service_accounts_creds_root) | 122 service_accounts_creds_root=service_accounts_creds_root) |
| 120 | 123 |
| 121 return oauth2client.client.SignedJwtAssertionCredentials( | 124 return oauth2client.client.SignedJwtAssertionCredentials( |
| 122 key['client_email'], key['private_key'], scope) | 125 key['client_email'], key['private_key'], scope) |
| 123 | 126 |
| 124 | 127 |
| 125 def get_authenticated_http(credentials_filename, | 128 def get_authenticated_http(credentials_filename, |
| 126 scope=None, | 129 scope=None, |
| 127 service_accounts_creds_root=None, | 130 service_accounts_creds_root=None, |
| 128 http_identifier=None, | 131 http_identifier=None, |
| 129 timeout=30): | 132 timeout=DEFAULT_TIMEOUT): |
| 130 """Creates an httplib2.Http wrapped with a service account authenticator. | 133 """Creates an httplib2.Http wrapped with a service account authenticator. |
| 131 | 134 |
| 132 Args: | 135 Args: |
| 133 credentials_filename (str): relative path to the file containing | 136 credentials_filename (str): relative path to the file containing |
| 134 credentials in json format. Path is relative to the default | 137 credentials in json format. Path is relative to the default |
| 135 location where credentials are stored (platform-dependent). | 138 location where credentials are stored (platform-dependent). |
| 136 | 139 |
| 137 Keyword Args: | 140 Keyword Args: |
| 138 scope (str|list of str): scope(s) of the credentials being | 141 scope (str|list of str): scope(s) of the credentials being |
| 139 requested. Defaults to https://www.googleapis.com/auth/userinfo.email. | 142 requested. Defaults to https://www.googleapis.com/auth/userinfo.email. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 155 if http_identifier: | 158 if http_identifier: |
| 156 http = InstrumentedHttp(http_identifier, timeout=timeout) | 159 http = InstrumentedHttp(http_identifier, timeout=timeout) |
| 157 else: | 160 else: |
| 158 http = httplib2.Http(timeout=timeout) | 161 http = httplib2.Http(timeout=timeout) |
| 159 return creds.authorize(http) | 162 return creds.authorize(http) |
| 160 | 163 |
| 161 | 164 |
| 162 class InstrumentedHttp(httplib2.Http): | 165 class InstrumentedHttp(httplib2.Http): |
| 163 """A httplib2.Http object that reports ts_mon metrics about its requests.""" | 166 """A httplib2.Http object that reports ts_mon metrics about its requests.""" |
| 164 | 167 |
| 165 def __init__(self, name, time_fn=time.time, **kwargs): | 168 def __init__(self, name, time_fn=time.time, timeout=DEFAULT_TIMEOUT, |
| 169 **kwargs): |
| 166 """ | 170 """ |
| 167 Args: | 171 Args: |
| 168 name: An identifier for the HTTP requests made by this object. | 172 name: An identifier for the HTTP requests made by this object. |
| 169 time_fn: Function returning the current time in seconds. Use for testing | 173 time_fn: Function returning the current time in seconds. Use for testing |
| 170 purposes only. | 174 purposes only. |
| 171 """ | 175 """ |
| 172 | 176 |
| 173 super(InstrumentedHttp, self).__init__(**kwargs) | 177 super(InstrumentedHttp, self).__init__(timeout=timeout, **kwargs) |
| 174 self.fields = {'name': name, 'client': 'httplib2'} | 178 self.fields = {'name': name, 'client': 'httplib2'} |
| 175 self.time_fn = time_fn | 179 self.time_fn = time_fn |
| 176 | 180 |
| 177 def _update_metrics(self, status, start_time): | 181 def _update_metrics(self, status, start_time): |
| 178 status_fields = {'status': status} | 182 status_fields = {'status': status} |
| 179 status_fields.update(self.fields) | 183 status_fields.update(self.fields) |
| 180 http_metrics.response_status.increment(fields=status_fields) | 184 http_metrics.response_status.increment(fields=status_fields) |
| 181 | 185 |
| 182 duration_msec = (self.time_fn() - start_time) * 1000 | 186 duration_msec = (self.time_fn() - start_time) * 1000 |
| 183 http_metrics.durations.add(duration_msec, fields=self.fields) | 187 http_metrics.durations.add(duration_msec, fields=self.fields) |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 self.requests_made.append(self.HttpCall(uri, method, body, headers)) | 257 self.requests_made.append(self.HttpCall(uri, method, body, headers)) |
| 254 headers = None | 258 headers = None |
| 255 body = None | 259 body = None |
| 256 for candidate in self._uris: | 260 for candidate in self._uris: |
| 257 if candidate[0].match(uri): | 261 if candidate[0].match(uri): |
| 258 _, headers, body = candidate | 262 _, headers, body = candidate |
| 259 break | 263 break |
| 260 if not headers: | 264 if not headers: |
| 261 raise AssertionError("Unexpected request to %s" % uri) | 265 raise AssertionError("Unexpected request to %s" % uri) |
| 262 return httplib2.Response(headers), body | 266 return httplib2.Response(headers), body |
| OLD | NEW |