def __init__(self, oauth2session, client_type, client_config, redirect_uri=None, code_verifier=None, autogenerate_code_verifier=False): """Initializes a google_auth_flow.InstalledAppFlow. Args: oauth2session (requests_oauthlib.OAuth2Session): The OAuth 2.0 session from ``requests-oauthlib``. client_type (str): The client type, either ``web`` or ``installed``. client_config (Mapping[str, Any]): The client configuration in the Google `client secrets`_ format. redirect_uri (str): The OAuth 2.0 redirect URI if known at flow creation time. Otherwise, it will need to be set using :attr:`redirect_uri`. code_verifier (str): random string of 43-128 chars used to verify the key exchange.using PKCE. autogenerate_code_verifier (bool): If true, auto-generate a code_verifier. .. _client secrets: https://developers.google.com/api-client-library/python/guide /aaa_client_secrets """ session = requests.GetSession(session=oauth2session) super(InstalledAppFlow, self).__init__(session, client_type, client_config, redirect_uri, code_verifier, autogenerate_code_verifier)
def __init__(self, oauth2session, client_type, client_config, redirect_uri=None, code_verifier=None, autogenerate_code_verifier=False, require_local_server=False): session = requests.GetSession(session=oauth2session) super(InstalledAppFlow, self).__init__( session, client_type, client_config, redirect_uri=redirect_uri, code_verifier=code_verifier, autogenerate_code_verifier=autogenerate_code_verifier) self.original_client_config = client_config if require_local_server: self.host = _LOCALHOST self.app = _RedirectWSGIApp() self.server = CreateLocalServer(self.app, self.host, _PORT_SEARCH_START, _PORT_SEARCH_END) self.redirect_uri = 'http://{}:{}/'.format(self.host, self.server.server_port) else: self.redirect_uri = self._OOB_REDIRECT_URI
def testUserAgent_SpacesInVersion(self): # This is similar to what the versions look like for some internal builds config.CLOUD_SDK_VERSION = 'Mon Sep 12 08:35:01 2016' request_mock = self.StartObjectPatch(requests.Session, 'request') uuid_mock = self.StartObjectPatch(uuid, 'uuid4') uuid_mock.return_value = uuid.UUID('12345678123456781234567812345678') is_interactive_mock = self.StartObjectPatch(console_io, 'IsInteractive') is_interactive_mock.return_value = False python_version = '2.7.6' self.StartPatch( 'platform.python_version').return_value = python_version http_client = core_requests.GetSession() url = 'http://foo.com' request_mock.return_value = self.default_response http_client.request('GET', url) expect_user_agent = self.UserAgent('Mon_Sep_12_08:35:01_2016', 'None', uuid_mock.return_value.hex, python_version, False) request_mock.assert_called_once_with( 'GET', url, headers={b'user-agent': expect_user_agent}, timeout=mock.ANY) request_mock.reset_mock()
def _WebRequest(self, method, url, headers=None): """Internal method to make requests against web URLs. Args: method: request method, e.g. GET url: request URL headers: dictionary of request headers Returns: Response body as a string Raises: Error: If the response has a status code >= 400. """ if base.UseRequests(): r = requests.GetSession().request(method, url, headers=headers) content = r.content status = r.status_code else: r, content = http.Http().request(url, method, headers=headers) status = r.status if status >= 400: raise exceptions.Error('status: {}, reason: {}'.format( status, r.reason)) return content
def GetApitoolsTransport(timeout='unset', response_encoding=None, ca_certs=None): """Get an unauthenticated transport client for use with apitools. Args: timeout: double, The request timeout in seconds. This is the socket level timeout. If timeout is None, timeout is infinite. If default argument 'unset' is given, a sensible default is selected. response_encoding: str, the encoding to use to decode the response. ca_certs: str, absolute filename of a ca_certs file that overrides the default Returns: 1. A httplib2.Http-like object backed by httplib2 or requests. """ if base.UseRequests(): session = requests.GetSession(timeout=timeout, response_encoding=response_encoding, ca_certs=ca_certs) return requests.GetApitoolsRequests(session) return http.Http(timeout=timeout, response_encoding=response_encoding, ca_certs=ca_certs)
def _DownscopeCredentials(token, access_boundary_json): """Downscope the given credentials to the given access boundary. Args: token: The credentials to downscope. access_boundary_json: The JSON-formatted access boundary. Returns: A downscopded credential with the given access-boundary. """ payload = { 'grant_type': 'urn:ietf:params:oauth:grant-type:token-exchange', 'requested_token_type': 'urn:ietf:params:oauth:token-type:access_token', 'subject_token_type': 'urn:ietf:params:oauth:token-type:access_token', 'subject_token': token, 'options': access_boundary_json } cab_token_url = 'https://sts.googleapis.com/v1/token' headers = {'Content-Type': 'application/x-www-form-urlencoded'} downscope_response = requests.GetSession().post(cab_token_url, headers=headers, data=payload) if downscope_response.status_code != 200: raise ValueError('Error downscoping credentials') cab_token = json.loads(downscope_response.content) return cab_token.get('access_token', None)
def testDefaultTimeout(self): timeout_mock = self.StartObjectPatch(transport, 'GetDefaultTimeout') timeout_mock.return_value = 0.001 self.socket_connect_mock.side_effect = socket.timeout http_client = core_requests.GetSession() with self.assertRaises(requests.ConnectTimeout): http_client.request('GET', 'http://localhost/')
def _CheckURLRequests(self, url): try: core_requests.GetSession().request('GET', url) except requests.exceptions.RequestException as err: msg = 'Cannot reach {0} with requests ({1})'.format( url, type(err).__name__) return check_base.Failure(message=msg, exception=err)
def testResponseEncoding(self): request_mock = self.StartObjectPatch(requests.Session, 'request') request_mock.return_value = MakeRequestsResponse( httplib.OK, {}, b'\xe1\x95\x95( \xe1\x90\x9b )\xe1\x95\x97') http_client = core_requests.GetSession(response_encoding='utf-8') response = http_client.request('GET', 'http://foo.com', data='Request Body') self.assertEqual('ᕕ( ᐛ )ᕗ', response.text)
def GetSession(timeout='unset', response_encoding=None, ca_certs=None, enable_resource_quota=True, force_resource_quota=False, allow_account_impersonation=True, session=None, streaming_response_body=False): """Get requests.Session object for working with the Google API. 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. response_encoding: str, the encoding to use to decode the response. ca_certs: str, absolute filename of a ca_certs file that overrides the default enable_resource_quota: bool, By default, we are going to tell APIs to use the quota of the project being operated on. For some APIs we want to use gcloud's quota, so you can explicitly disable that behavior by passing False here. force_resource_quota: bool, If true resource project quota will be used by this client regardless of the settings in gcloud. This should be used for newer APIs that cannot work with legacy project quota. allow_account_impersonation: bool, True to allow use of impersonated service account credentials for calls made with this client. If False, the active user credentials will always be used. session: requests.Session instance. Otherwise, a new requests.Session will be initialized. streaming_response_body: bool, True indicates that the response body will be a streaming body. Returns: 1. A regular requests.Session object if no credentials are available; 2. Or an authorized requests.Session object authorized by google-auth credentials. Raises: c_store.Error: If an error loading the credentials occurs. """ session = requests.GetSession( timeout=timeout, response_encoding=response_encoding, ca_certs=ca_certs, session=session, streaming_response_body=streaming_response_body) request_wrapper = RequestWrapper() session = request_wrapper.WrapQuota(session, enable_resource_quota, force_resource_quota, allow_account_impersonation, True) session = request_wrapper.WrapCredentials(session, allow_account_impersonation) return session
def LogSurveyAnswers(survey_instance): """Sends survey response to clearcut table.""" http_client = requests.GetSession() headers = {'user-agent': metrics.GetUserAgent()} body = json.dumps(_ClearcutRequest(survey_instance), sort_keys=True) response = http_client.request( 'POST', _CLEARCUT_ENDPOINT, data=body, headers=headers) if response.status_code != httplib.OK: raise SurveyNotRecordedError( 'We cannot record your feedback at this time, please try again later.') _UpdateSurveyCache() log.err.Print('Your response is submitted.')
def testRPCDurationReporting(self): request_mock = self.StartObjectPatch(requests.Session, 'request') request_mock.return_value = self.default_response time_mock = self.StartPatch('time.time', autospec=True) time_mock.side_effect = [1.0, 4.0] duration_mock = self.StartObjectPatch(metrics, 'RPCDuration') http_client = core_requests.GetSession() http_client.request('GET', 'http://foo.com', data='Request Body') duration_mock.assert_called_once_with(3.0)
def ValidateSignedUrl(signed_url): """Validates the Signed URL by returning the response code for HEAD request. Args: signed_url: The Signed URL which should be validated. Returns: Returns the response code for the HEAD request to the specified Signed URL. """ http_client = requests.GetSession() http_response = http_client.request('HEAD', signed_url) return http_response.status_code
def FetchFeatureFlagsConfig(): """Downloads the feature flag config file.""" # pylint: disable=g-import-not-at-top import requests from googlecloudsdk.core import requests as core_requests try: yaml_request = core_requests.GetSession() response = yaml_request.get(_FEATURE_FLAG_YAML_URL) response.raise_for_status() return response.text except requests.exceptions.RequestException as e: logging.debug('Unable to fetch feature flags config from [%s]: %s', _FEATURE_FLAG_YAML_URL, e) return None
def Run(args, release_track): """Call a v2 Google Cloud Function.""" v2_client = v2_api_util.GetClientInstance(release_track=release_track) v2_messages = v2_client.MESSAGES_MODULE function_ref = args.CONCEPTS.name.Parse() if args.data: try: json.loads(args.data) except ValueError as e: raise exceptions.InvalidArgumentException( '--data', 'Is not a valid JSON: ' + six.text_type(e)) request_data = args.data headers = _DEFAULT_HEADERS elif args.cloud_event: try: request_data_json = json.loads(args.cloud_event) except ValueError as e: raise exceptions.InvalidArgumentException( '--cloud-event', 'Is not a valid JSON: ' + six.text_type(e)) request_data, headers = _StructuredToBinaryData(request_data_json) else: # If neither --data nor --cloud-event flag are specified request_data = None headers = _DEFAULT_HEADERS # cloudfunctions_v2alpha_messages.Function function = v2_client.projects_locations_functions.Get( v2_messages.CloudfunctionsProjectsLocationsFunctionsGetRequest( name=function_ref.RelativeName())) cloud_run_uri = function.serviceConfig.uri token = GenerateIdToken() headers['Authorization'] = 'Bearer {}'.format(token) requests_session = core_requests.GetSession() response = requests_session.post( cloud_run_uri, # None | str, if None an empty body is sent in POST request. data=request_data, headers=headers) response.raise_for_status() return response.content
def GetApitoolsTransport(timeout='unset', response_encoding=None, ca_certs=None, client_certificate=None, client_key=None, client_cert_domain=None): """Get an unauthenticated transport client for use with apitools. Args: timeout: double, The request timeout in seconds. This is the socket level timeout. If timeout is None, timeout is infinite. If default argument 'unset' is given, a sensible default is selected. response_encoding: str, the encoding to use to decode the response. ca_certs: str, absolute filename of a ca_certs file that overrides the default client_certificate: str, absolute filename of a client_certificate file client_key: str, absolute filename of a client_key file client_cert_domain: str, domain we are connecting to (used only by httplib2) Returns: 1. A httplib2.Http-like object backed by httplib2 or requests. """ if base.UseRequests(): # pylint: disable=g-import-not-at-top from googlecloudsdk.core import requests session = requests.GetSession( timeout=timeout, ca_certs=ca_certs, client_certificate=client_certificate, client_key=client_key) return requests.GetApitoolsRequests( session, response_encoding=response_encoding) else: from googlecloudsdk.core import http # pylint: disable=g-import-not-at-top http_client = http.Http( timeout=timeout, response_encoding=response_encoding, ca_certs=ca_certs) # httplib2 always applies the first client certificate # in the chain for authentication http_client.certificates.credentials.insert( 0, (client_cert_domain, client_key, client_certificate, '')) return http_client
def Run(url, extra_headers): request_mock.return_value = MakeRequestsResponse( httplib.OK, { 'header1': 'value1', 'header2': 'value2' }, b'response content') properties.VALUES.core.log_http.Set(True) http_client = core_requests.GetSession() http_client.request('GET', url, data='request content', headers=extra_headers) request_mock.assert_called_once_with( 'GET', url, headers=mock.ANY, data='request content', timeout=mock.ANY, ) request_mock.reset_mock()
def UploadArchive(upload_url, zip_file): """Uploads the specified zip file with a PUT request to the provided URL. Args: upload_url: A string of the URL to send the PUT request to. Required to be a signed URL from GCS. zip_file: A string of the file path to the zip file to upload. Returns: A requests.Response object. """ sess = requests.GetSession() # Required headers for the Apigee generated signed URL. headers = { 'content-type': 'application/zip', 'x-goog-content-length-range': '0,1073741824' } with files.BinaryFileReader(zip_file) as data: response = sess.put(upload_url, data=data, headers=headers) return response
def testWithContextAwareConfig(self): properties.VALUES.context_aware.use_client_certificate.Set(True) context_mock = mock.Mock() self.StartObjectPatch(core_requests, 'CreateSSLContext', return_value=context_mock) context_aware_config = self.StartObjectPatch(context_aware, 'Config') context_aware_config.return_value = mock.Mock( client_cert_path='mock_path', client_cert_password='******') http_adapter_mock = self.StartObjectPatch(core_requests.HTTPAdapter, 'send') http_adapter_mock.return_value = MakeRequestsResponse( httplib.OK, {}, b'response content') http_client = core_requests.GetSession() http_client.request('GET', 'https://www.foo.com', data='Request Body') context_mock.load_cert_chain.assert_called_once_with( 'mock_path', **{ 'keyfile': 'mock_path', 'password': '******', })
def testUserAgent_AllSpellings(self, ua_header, ua_value): request_mock = self.StartObjectPatch(requests.Session, 'request') uuid_mock = self.StartObjectPatch(uuid, 'uuid4') uuid_mock.return_value = uuid.UUID('12345678123456781234567812345678') is_interactive_mock = self.StartObjectPatch(console_io, 'IsInteractive') is_interactive_mock.return_value = False python_version = '2.7.6' self.StartPatch( 'platform.python_version').return_value = python_version headers = {} http_client = core_requests.GetSession() url = 'http://foo.com' request_mock.return_value = self.default_response headers[ua_header] = ua_value http_client.request('GET', url, headers=headers, uncopyable=_UncopyableObject()) expect_user_agent = ( b'%s %s %s' % (config.CLOUDSDK_USER_AGENT.encode('utf-8'), ua_value, self.UserAgent('10.0.0', 'None', uuid_mock.return_value.hex, python_version, False, include_cloudsdk_prefix=False))) request_mock.assert_called_once_with( 'GET', url, headers={b'user-agent': expect_user_agent}, uncopyable=mock.ANY, timeout=mock.ANY) request_mock.reset_mock()
def _ExecuteRequestAndRaiseExceptions(url, headers, timeout): """Executes an HTTP request using requests. Args: url: str, the url to download. headers: obj, the headers to include in the request. timeout: int, the timeout length for the request. Returns: A response object from the request. Raises: requests.exceptions.HTTPError in the case of a client or server error. """ requests_session = core_requests.GetSession() if url.startswith('file://'): requests_session.mount('file://', local_file_adapter.LocalFileAdapter()) response = requests_session.get(url, headers=headers, timeout=timeout, stream=True) response.raise_for_status() return response
def testRequestReason(self): properties.VALUES.core.request_reason.Set('my request justification') request_mock = self.StartObjectPatch(requests.Session, 'request') request_mock.return_value = MakeRequestsResponse( httplib.OK, { 'header1': 'value1', 'header2': 'value2' }, b'response content') http_client = core_requests.GetSession() http_client.request('GET', 'http://www.example.com/foo', data='request content', headers={}) expected_headers = { b'user-agent': mock.ANY, b'X-Goog-Request-Reason': b'my request justification' } request_mock.assert_called_once_with('GET', 'http://www.example.com/foo', data='request content', headers=expected_headers, timeout=mock.ANY)
def GetServices(): response = requests.GetSession().get(_SERVICE_CATALOG_URL) catalog = json.loads(response.text) return catalog['services']
def testUserAgent(self): request_mock = self.StartObjectPatch(requests.Session, 'request') uuid_mock = self.StartObjectPatch(uuid, 'uuid4') uuid_mock.return_value = uuid.UUID('12345678123456781234567812345678') is_interactive_mock = self.StartObjectPatch(console_io, 'IsInteractive') is_interactive_mock.return_value = False python_version = '2.7.6' self.StartPatch( 'platform.python_version').return_value = python_version headers = {} http_client = core_requests.GetSession() url = 'http://foo.com' request_mock.return_value = self.default_response http_client.request('GET', url, headers=headers, uncopyable=_UncopyableObject()) expect_user_agent = self.UserAgent('10.0.0', 'None', uuid_mock.return_value.hex, python_version, False) request_mock.assert_called_once_with( 'GET', url, headers={b'user-agent': expect_user_agent}, uncopyable=mock.ANY, timeout=mock.ANY) request_mock.reset_mock() # Make sure our wrapping did not actually affect args we pass into request. # If it does, we could accidentally be modifying global state. self.assertEqual(headers, {}) cmd_path = 'a.b.c.d' properties.VALUES.metrics.command_name.Set(cmd_path) http_client = core_requests.GetSession() request_mock.return_value = self.default_response http_client.request('GET', url) expect_user_agent = self.UserAgent('10.0.0', cmd_path, uuid_mock.return_value.hex, python_version, False) request_mock.assert_called_once_with( 'GET', url, headers={b'user-agent': expect_user_agent}, timeout=mock.ANY) request_mock.reset_mock() cmd_path = 'a.b.e.d' properties.VALUES.metrics.command_name.Set(cmd_path) http_client = core_requests.GetSession() expect_headers = {b'user-agent': b'hello'} request_mock.return_value = self.default_response http_client.request('GET', url, headers=expect_headers) expect_user_agent = (b'%s hello %s' % (config.CLOUDSDK_USER_AGENT.encode('utf-8'), self.UserAgent('10.0.0', cmd_path, uuid_mock.return_value.hex, python_version, False, include_cloudsdk_prefix=False))) request_mock.assert_called_once_with( 'GET', url, headers={b'user-agent': expect_user_agent}, timeout=mock.ANY) request_mock.reset_mock() cmd_path = 'a.b.e.d' properties.VALUES.metrics.command_name.Set(cmd_path) http_client = core_requests.GetSession() expect_headers = {b'user-agent': b'google-cloud-sdk'} request_mock.return_value = self.default_response http_client.request('GET', url, headers=expect_headers) expect_user_agent = self.UserAgent('10.0.0', cmd_path, uuid_mock.return_value.hex, python_version, False) request_mock.assert_called_once_with( 'GET', url, headers={b'user-agent': expect_user_agent}, timeout=mock.ANY)
def testTraces(self): request_mock = self.StartObjectPatch(requests.Session, 'request') uuid_mock = self.StartObjectPatch(uuid, 'uuid4') uuid_mock.return_value = uuid.UUID('12345678123456781234567812345678') is_interactive_mock = self.StartObjectPatch(console_io, 'IsInteractive') is_interactive_mock.return_value = False python_version = '2.7.6' self.StartPatch( 'platform.python_version').return_value = python_version expect_user_agent = self.UserAgent('10.0.0', 'None', uuid_mock.return_value.hex, python_version, False) http_client = core_requests.GetSession() url = 'http://foo.com' request_mock.return_value = self.default_response http_client.request('GET', url) request_mock.assert_called_once_with( 'GET', url, headers={b'user-agent': expect_user_agent}, timeout=mock.ANY) request_mock.reset_mock() trace_token = 'hello' properties.VALUES.core.trace_token.Set(trace_token) http_client = core_requests.GetSession() request_mock.return_value = self.default_response http_client.request('GET', url) expect_url = '{0}?trace=token%3A{1}'.format(url, trace_token) request_mock.assert_called_once_with( 'GET', expect_url, headers={b'user-agent': expect_user_agent}, timeout=mock.ANY) request_mock.reset_mock() properties.VALUES.core.trace_token.Set(None) trace_email = 'hello' properties.VALUES.core.trace_email.Set(trace_email) http_client = core_requests.GetSession() request_mock.return_value = self.default_response http_client.request('GET', url) expect_url = '{0}?trace=email%3A{1}'.format(url, trace_email) request_mock.assert_called_once_with( 'GET', expect_url, headers={b'user-agent': expect_user_agent}, timeout=mock.ANY) request_mock.reset_mock() properties.VALUES.core.trace_email.Set(None) properties.VALUES.core.trace_log.Set(True) http_client = core_requests.GetSession() request_mock.return_value = self.default_response http_client.request('GET', url) expect_url = '{0}?trace=log'.format(url) request_mock.assert_called_once_with( 'GET', expect_url, headers={b'user-agent': expect_user_agent}, timeout=mock.ANY) properties.VALUES.core.trace_log.Set(None)
def testRequestResponseDump(self): request_mock = self.StartObjectPatch(requests.Session, 'request') response_headers = {b'header1': b'value1', b'header2': b'value2'} request_mock.return_value = MakeRequestsResponse( httplib.OK, response_headers, 'response content') uuid_mock = self.StartObjectPatch(uuid, 'uuid4') uuid_mock.return_value = uuid.UUID('12345678123456781234567812345678') is_interactive_mock = self.StartObjectPatch(console_io, 'IsInteractive') is_interactive_mock.return_value = False python_version = '2.7.6' self.StartPatch( 'platform.python_version').return_value = python_version expect_user_agent = self.UserAgent('10.0.0', 'None', uuid_mock.return_value.hex, python_version, False) user_agent_header = {b'user-agent': expect_user_agent} log_mock = self.StartObjectPatch(log.status, 'Print') properties.VALUES.core.log_http.Set(True) http_client = core_requests.GetSession() url = 'http://foo.com' time_mock = self.StartPatch('time.time', autospec=True) # Time is called twice by RPC duration reporting and twice by logging time_mock.side_effect = [1.0, 1.1, 3.0, 3.1] http_client.request('GET', url, data='Request Body') request_mock.assert_called_once_with( 'GET', url, headers=user_agent_header, data='Request Body', timeout=mock.ANY, ) request_mock.reset_mock() expected_output = """\ ======================= ==== request start ==== uri: http://foo.com method: GET == headers start == {0} == headers end == == body start == Request Body == body end == ==== request end ==== ---- response start ---- {1} -- headers start -- {2} -- headers end -- -- body start -- response content -- body end -- total round trip time (request+response): 2.000 secs ---- response end ---- ----------------------""".format( self._FormatHeaderOutput(user_agent_header), self._FormatHeaderOutput({'status': httplib.OK}), self._FormatHeaderOutput(response_headers)) calls = [] for line in expected_output.split('\n'): if line == 'response content': line = b'response content' calls.append(mock.call(line)) log_mock.assert_has_calls(calls)
def CheckURLRequests(url): try: core_requests.GetSession(timeout=_NETWORK_TIMEOUT).request('GET', url) except requests.exceptions.RequestException as err: msg = 'requests cannot reach {0}:\n{1}\n'.format(url, err) return check_base.Failure(message=msg, exception=err)