def test_request_proxy(self): cfg = Configuration("http://my_service.com") cfg.proxies.add("http://my_service.com", 'http://localhost:57979') creds = Authentication() def hook(adptr, request, *args, **kwargs): self.assertEqual(kwargs.get('proxies'), {"http://my_service.com":'http://localhost:57979'}) kwargs['result']._content_consumed = True kwargs['result'].status_code = 200 return kwargs['result'] client = ServiceClient(creds, cfg) client.add_hook('request', hook, precall=False, overwrite=True) url = client.format_url("/get_endpoint") request = client.get(url, {'check':True}) response = client.send(request) os.environ['HTTPS_PROXY'] = "http://localhost:1987" def hook2(adptr, request, *args, **kwargs): self.assertEqual(kwargs.get('proxies')['https'], "http://localhost:1987") kwargs['result']._content_consumed = True kwargs['result'].status_code = 200 return kwargs['result'] cfg = Configuration("http://my_service.com") client = ServiceClient(creds, cfg) client.add_hook('request', hook2, precall=False, overwrite=True) url = client.format_url("/get_endpoint") request = client.get(url, {'check':True}) response = client.send(request) del os.environ['HTTPS_PROXY']
def select(endpoint, cert=None, key=None, pem=None, ca=None, aad=False, no_verify=False): #pylint: disable-msg=too-many-locals """ Connects to a Service Fabric cluster endpoint. If connecting to secure cluster specify an absolute path to a cert (.crt) and key file (.key) or a single file with both (.pem). Do not specify both. Optionally, if connecting to a secure cluster, specify also an absolute path to a CA bundle file or directory of trusted CA certs. :param str endpoint: Cluster endpoint URL, including port and HTTP or HTTPS prefix :param str cert: Absolute path to a client certificate file :param str key: Absolute path to client certificate key file :param str pem: Absolute path to client certificate, as a .pem file :param str ca: Absolute path to CA certs directory to treat as valid or CA bundle file :param bool aad: Use Azure Active Directory for authentication :param bool no_verify: Disable verification for certificates when using HTTPS, note: this is an insecure option and should not be used for production environments """ from sfctl.config import (set_ca_cert, set_auth, set_aad_cache, set_cluster_endpoint, set_no_verify) from msrest import ServiceClient, Configuration from sfctl.auth import ClientCertAuthentication, AdalAuthentication select_arg_verify(endpoint, cert, key, pem, ca, aad, no_verify) if aad: new_token, new_cache = get_aad_token(endpoint, no_verify) set_aad_cache(new_token, new_cache) rest_client = ServiceClient(AdalAuthentication(no_verify), Configuration(endpoint)) # Make sure basic GET request succeeds rest_client.send(rest_client.get('/')).raise_for_status() else: client_cert = None if pem: client_cert = pem elif cert: client_cert = (cert, key) rest_client = ServiceClient( ClientCertAuthentication(client_cert, ca, no_verify), Configuration(endpoint)) # Make sure basic GET request succeeds rest_client.send(rest_client.get('/')).raise_for_status() set_cluster_endpoint(endpoint) set_no_verify(no_verify) set_ca_cert(ca) set_auth(pem, cert, key, aad)
def _get_rest_client( endpoint, cert=None, key=None, pem=None, ca=None, # pylint: disable=invalid-name, too-many-arguments aad=False, no_verify=False): """ Get the rest client to send a http request with secured connections :param endpoint: See select command in this file :param cert: See select command in this file :param key: See select command in this file :param pem: See select command in this file :param ca: See select command in this file :param aad: See select command in this file :param no_verify: See select command in this file :return: ServiceClient from msrest """ if aad: new_token, new_cache = get_aad_token(endpoint, no_verify) set_aad_cache(new_token, new_cache) return ServiceClient(AdalAuthentication(no_verify), Configuration(endpoint)) # If the code reaches here, it is not AAD return ServiceClient(_get_client_cert_auth(pem, cert, key, ca, no_verify), Configuration(endpoint))
def test_request_proxy(self): # Note that this test requires requests >= 2.8.0 to accept host on proxy cfg = Configuration("http://my_service.com") cfg.proxies.add("http://my_service.com", 'http://localhost:57979') cfg.credentials = Authentication() httpretty.register_uri(httpretty.GET, "http://localhost:57979/get_endpoint?check=True", body='"Mocked body"', content_type="application/json", status=200) client = ServiceClient(None, cfg) url = client.format_url("/get_endpoint") request = client.get(url, {'check':True}) response = client.send(request) assert response.json() == "Mocked body" with mock.patch.dict('os.environ', {'HTTP_PROXY': "http://localhost:1987"}): httpretty.register_uri(httpretty.GET, "http://localhost:1987/get_endpoint?check=True", body='"Mocked body"', content_type="application/json", status=200) cfg = Configuration("http://my_service.com") client = ServiceClient(None, cfg) url = client.format_url("/get_endpoint") request = client.get(url, {'check':True}) response = client.send(request) assert response.json() == "Mocked body"
def __init__(self, credentials, subscription_id, base_url=None): self.config = LogAnalyticsManagementClientConfiguration( credentials, subscription_id, base_url) self._client = ServiceClient(self.config.credentials, self.config) client_models = { k: v for k, v in models.__dict__.items() if isinstance(v, type) } self._serialize = Serializer(client_models) self._serialize.basic_types[unicode] = "unicode" self._serialize.serialize_type["dict"] = self._patched_serialize_dict self._deserialize = Deserializer(client_models) self._deserialize.basic_types[unicode] = "unicode" self._deserialize.deserialize_type[ "dict"] = self._patched_deserialize_dict self._deserialize.deserialize_type[ "list"] = self._patched_deserialize_iter self.alert_services = AlertServicesOperations(self._client, self.config, self._serialize, self._deserialize)
def setUp(self): cfg = Configuration("https://my_service.com") cfg.retry_policy.backoff_factor = 0 creds = Authentication() self.client = ServiceClient(creds, cfg) self.request = self.client.get("/get_endpoint", {'check': True}) return super(TestRuntimeRetry, self).setUp()
def setUp(self): cfg = Configuration("https://my_service.com") cfg.retry_policy.backoff_factor=0 cfg.redirect_policy.max_redirects=2 creds = Authentication() self.client = ServiceClient(creds, cfg) return super(TestRedirect, self).setUp()
def test_credential_headers(self): httpretty.register_uri(httpretty.GET, "https://my_service.com/get_endpoint", body='[{"title": "Test Data"}]', content_type="application/json") token = { 'access_token': 'eswfld123kjhn1v5423', 'refresh_token': 'asdfkljh23490sdf', 'token_type': 'Bearer', 'expires_in': '3600', } creds = OAuthTokenAuthentication("client_id", token) cfg = Configuration("https://my_service.com") client = ServiceClient(creds, cfg) def hook(aptr, req, *args, **kwargs): self.assertTrue('Authorization' in req.headers) self.assertEqual(req.headers['Authorization'], 'Bearer eswfld123kjhn1v5423') client.add_hook('request', hook) url = client.format_url("/get_endpoint") request = client.get(url, {'check': True}) response = client.send(request) check = httpretty.last_request() self.assertEqual(response.json(), [{"title": "Test Data"}]) token['expires_in'] = '-30' creds = OAuthTokenAuthentication("client_id", token) client = ServiceClient(creds, cfg) url = client.format_url("/get_endpoint") request = client.get(url, {'check': True}) with self.assertRaises(TokenExpiredError): response = client.send(request)
def test_credential_headers(self): httpretty.register_uri(httpretty.GET, "https://my_service.com/get_endpoint", body='[{"title": "Test Data"}]', content_type="application/json") token = { 'access_token': 'eswfld123kjhn1v5423', 'refresh_token': 'asdfkljh23490sdf', 'token_type': 'Bearer', 'expires_in': '3600', } cfg = Configuration("https://my_service.com") cfg.credentials = OAuthTokenAuthentication("client_id", token) client = ServiceClient(None, cfg) url = client.format_url("/get_endpoint") request = client.get(url, {'check':True}) response = client.send(request) assert 'Authorization' in response.request.headers assert response.request.headers['Authorization'] == 'Bearer eswfld123kjhn1v5423' httpretty.has_request() assert response.json() == [{"title": "Test Data"}] # Expiration test token['expires_in'] = '-30' cfg.credentials = OAuthTokenAuthentication("client_id", token) client = ServiceClient(None, cfg) url = client.format_url("/get_endpoint") request = client.get(url, {'check':True}) with pytest.raises(TokenExpiredError): response = client.send(request)
def test_client_add_hook(self): client = ServiceClient(self.creds, self.cfg) def hook(): pass client.add_hook("request", hook) self.assertTrue(hook in client._adapter._client_hooks['request'].precalls) client.add_hook("request", hook, precall=False) self.assertTrue(hook in client._adapter._client_hooks['request'].postcalls) client.remove_hook("request", hook) self.assertFalse(hook in client._adapter._client_hooks['request'].precalls) self.assertFalse(hook in client._adapter._client_hooks['request'].postcalls)
def test_request_builder(self): client = ServiceClient(self.creds, self.cfg) req = client.get('http://127.0.0.1/') assert req.method == 'GET' assert req.url == 'http://127.0.0.1/' assert req.headers == {'Accept': 'application/json'} assert req.data is None assert req.files is None req = client.put("http://127.0.0.1/", content={'creation': True}) assert req.method == 'PUT' assert req.url == "http://127.0.0.1/" assert req.headers == {'Content-Length': '18', 'Accept': 'application/json'} assert req.data == '{"creation": true}' assert req.files is None
def test_session_callback(self): client = ServiceClient(self.creds, self.cfg) local_session = requests.Session() def callback(session, global_config, local_config, **kwargs): self.assertIs(session, local_session) self.assertIs(global_config, self.cfg) self.assertTrue(local_config["test"]) return {'used_callback': True} self.cfg.session_configuration_callback = callback output_kwargs = client._configure_session(local_session, **{"test": True}) self.assertTrue(output_kwargs['used_callback'])
def test_request_fail(self, mock_requests): mock_requests.return_value.request.return_value = mock.Mock(text="text") cfg = Configuration("https://my_service.com") cfg.credentials = Authentication() client = ServiceClient(None, cfg) url = client.format_url("/get_endpoint") request = client.get(url, {'check':True}) response = client.send(request) assert response.text == "text" mock_requests.return_value.request.side_effect = requests.RequestException with self.assertRaises(ClientRequestError): client.send(request)
def test_request_fail(self, mock_requests): mock_requests.return_value.request.return_value = "test" cfg = Configuration("https://my_service.com") creds = Authentication() client = ServiceClient(creds, cfg) request = client.get("/get_endpoint", {'check': True}) response = client.send(request) check = httpretty.last_request() self.assertEqual(response, "test") mock_requests.return_value.request.side_effect = requests.RequestException with self.assertRaises(ClientRequestError): client.send(request)
def test_request_fail(self, mock_requests): mock_requests.return_value.request.return_value = mock.Mock(_content_consumed=True) cfg = Configuration("https://my_service.com") creds = Authentication() client = ServiceClient(creds, cfg) url = client.format_url("/get_endpoint") request = client.get(url, {'check':True}) response = client.send(request) check = httpretty.last_request() self.assertTrue(response._content_consumed) mock_requests.return_value.request.side_effect = requests.RequestException with self.assertRaises(ClientRequestError): client.send(request)
def _credential_setup(self): auth_options = dict( auth_source=self.get_option('auth_source'), profile=self.get_option('profile'), subscription_id=self.get_option('subscription_id'), client_id=self.get_option('client_id'), secret=self.get_option('secret'), tenant=self.get_option('tenant'), ad_user=self.get_option('ad_user'), password=self.get_option('password'), cloud_environment=self.get_option('cloud_environment'), cert_validation_mode=self.get_option('cert_validation_mode'), api_profile=self.get_option('api_profile'), adfs_authority_url=self.get_option('adfs_authority_url') ) self.azure_auth = AzureRMAuth(**auth_options) self._clientconfig = AzureRMRestConfiguration(self.azure_auth.azure_credentials, self.azure_auth.subscription_id, self.azure_auth._cloud_environment.endpoints.resource_manager) self._client = ServiceClient(self._clientconfig.credentials, self._clientconfig)
def test_request_builder(self): client = ServiceClient(self.creds, self.cfg) req = client.get('http://example.org') assert req.method == 'GET' assert req.url == 'http://example.org' assert req.params == {} assert req.headers == {'Accept': 'application/json'} assert req.data == [] assert req.files == [] req = client.put('http://example.org', content={'creation': True}) assert req.method == 'PUT' assert req.url == 'http://example.org' assert req.params == {} assert req.headers == { 'Content-Length': '18', 'Accept': 'application/json' } assert req.data == '{"creation": true}' assert req.files == []
def test_client_request(self): client = ServiceClient(self.creds, self.cfg) obj = client.get() self.assertEqual(obj.method, 'GET') self.assertIsNone(obj.url) self.assertEqual(obj.params, {}) obj = client.get("/service", {'param': "testing"}) self.assertEqual(obj.method, 'GET') self.assertEqual(obj.url, "https://my_endpoint.com/service?param=testing") self.assertEqual(obj.params, {}) obj = client.get("service 2") self.assertEqual(obj.method, 'GET') self.assertEqual(obj.url, "https://my_endpoint.com/service 2") self.cfg.base_url = "https://my_endpoint.com/" obj = client.get("//service3") self.assertEqual(obj.method, 'GET') self.assertEqual(obj.url, "https://my_endpoint.com/service3") obj = client.put() self.assertEqual(obj.method, 'PUT') obj = client.post() self.assertEqual(obj.method, 'POST') obj = client.head() self.assertEqual(obj.method, 'HEAD') obj = client.merge() self.assertEqual(obj.method, 'MERGE') obj = client.patch() self.assertEqual(obj.method, 'PATCH') obj = client.delete() self.assertEqual(obj.method, 'DELETE')
def test_keep_alive(self): cfg = Configuration("http://127.0.0.1/") cfg.keep_alive = True class Creds(Authentication): def __init__(self): self.first_session = None self.called = 0 def signed_session(self, session=None): self.called += 1 assert session is not None if self.first_session: assert self.first_session is session else: self.first_session = session creds = Creds() client = ServiceClient(creds, cfg) req = client.get() try: client.send( req ) # Will fail, I don't care, that's not the point of the test except Exception: pass try: client.send( req ) # Will fail, I don't care, that's not the point of the test except Exception: pass assert creds.called == 2 assert client._http_driver.session # Still alive # Manually close the client in "keep_alive" mode client.close()
def test_context_manager(self): cfg = Configuration("http://127.0.0.1/") class Creds(Authentication): def __init__(self): self.first_session = None self.called = 0 def signed_session(self, session=None): self.called += 1 assert session is not None if self.first_session: assert self.first_session is session else: self.first_session = session creds = Creds() with ServiceClient(creds, cfg) as client: assert cfg.keep_alive req = client.get() try: client.send( req ) # Will fail, I don't care, that's not the point of the test except Exception: pass try: client.send( req ) # Will fail, I don't care, that's not the point of the test except Exception: pass assert client._http_driver.session # Still alive assert not cfg.keep_alive assert creds.called == 2
def test_client_request(self): cfg = Configuration("http://127.0.0.1/") client = ServiceClient(self.creds, cfg) obj = client.get('/') self.assertEqual(obj.method, 'GET') self.assertEqual(obj.url, "http://127.0.0.1/") obj = client.get("/service", {'param':"testing"}) self.assertEqual(obj.method, 'GET') self.assertEqual(obj.url, "http://127.0.0.1/service?param=testing") obj = client.get("service 2") self.assertEqual(obj.method, 'GET') self.assertEqual(obj.url, "http://127.0.0.1/service 2") cfg.base_url = "https://127.0.0.1/" obj = client.get("//service3") self.assertEqual(obj.method, 'GET') self.assertEqual(obj.url, "https://127.0.0.1/service3") obj = client.put('/') self.assertEqual(obj.method, 'PUT') obj = client.post('/') self.assertEqual(obj.method, 'POST') obj = client.head('/') self.assertEqual(obj.method, 'HEAD') obj = client.merge('/') self.assertEqual(obj.method, 'MERGE') obj = client.patch('/') self.assertEqual(obj.method, 'PATCH') obj = client.delete('/') self.assertEqual(obj.method, 'DELETE')
def test_client_header(self): client = ServiceClient(self.creds, self.cfg) client.add_header("test", "value") self.assertEqual(client._headers.get('test'), 'value')
def test_client_send(self): current_ua = self.cfg.user_agent client = ServiceClient(self.creds, self.cfg) client.config.keep_alive = True req_response = requests.Response() req_response._content = br'{"real": true}' # Has to be valid bytes JSON req_response._content_consumed = True req_response.status_code = 200 def side_effect(*args, **kwargs): return req_response session = mock.create_autospec(requests.Session) session.request.side_effect = side_effect session.adapters = { "http://": HTTPAdapter(), "https://": HTTPAdapter(), } # Be sure the mock does not trick me assert not hasattr(session.resolve_redirects, 'is_msrest_patched') client.config.pipeline._sender.driver.session = session client.config.credentials.signed_session.return_value = session client.config.credentials.refresh_session.return_value = session request = ClientRequest('GET', '/') client.send(request, stream=False) session.request.call_count = 0 session.request.assert_called_with( 'GET', '/', allow_redirects=True, cert=None, headers={ 'User-Agent': current_ua, 'Test': 'true' # From global config }, stream=False, timeout=100, verify=True ) assert session.resolve_redirects.is_msrest_patched client.send(request, headers={'id':'1234'}, content={'Test':'Data'}, stream=False) session.request.assert_called_with( 'GET', '/', data='{"Test": "Data"}', allow_redirects=True, cert=None, headers={ 'User-Agent': current_ua, 'Content-Length': '16', 'id':'1234', 'Test': 'true' # From global config }, stream=False, timeout=100, verify=True ) self.assertEqual(session.request.call_count, 1) session.request.call_count = 0 assert session.resolve_redirects.is_msrest_patched session.request.side_effect = requests.RequestException("test") with self.assertRaises(ClientRequestError): client.send(request, headers={'id':'1234'}, content={'Test':'Data'}, test='value', stream=False) session.request.assert_called_with( 'GET', '/', data='{"Test": "Data"}', allow_redirects=True, cert=None, headers={ 'User-Agent': current_ua, 'Content-Length': '16', 'id':'1234', 'Test': 'true' # From global config }, stream=False, timeout=100, verify=True ) self.assertEqual(session.request.call_count, 1) session.request.call_count = 0 assert session.resolve_redirects.is_msrest_patched session.request.side_effect = oauth2.rfc6749.errors.InvalidGrantError("test") with self.assertRaises(TokenExpiredError): client.send(request, headers={'id':'1234'}, content={'Test':'Data'}, test='value') self.assertEqual(session.request.call_count, 2) session.request.call_count = 0 session.request.side_effect = ValueError("test") with self.assertRaises(ValueError): client.send(request, headers={'id':'1234'}, content={'Test':'Data'}, test='value')
def select( endpoint, cert=None, key=None, pem=None, ca=None, #pylint: disable=invalid-name, too-many-arguments aad=False, no_verify=False): #pylint: disable-msg=too-many-locals """ Connects to a Service Fabric cluster endpoint. If connecting to secure cluster, specify an absolute path to a cert (.crt) and key file (.key) or a single file with both (.pem). Do not specify both. Optionally, if connecting to a secure cluster, also specify an absolute path to a CA bundle file or directory of trusted CA certs. If using a directory of CA certs, `c_rehash <directory>` provided by OpenSSL must be run first to compute the certificate hashes and create the appropriate symbolics links. :param str endpoint: Cluster endpoint URL, including port and HTTP or HTTPS prefix :param str cert: Absolute path to a client certificate file :param str key: Absolute path to client certificate key file :param str pem: Absolute path to client certificate, as a .pem file :param str ca: Absolute path to CA certs directory to treat as valid or CA bundle file :param bool aad: Use Azure Active Directory for authentication :param bool no_verify: Disable verification for certificates when using HTTPS, note: this is an insecure option and should not be used for production environments """ # Regarding c_rehash: # The c_rehash is needed when specifying a CA certs directory # because requests.Sessions which is used underneath requires # the c_rehash operation to be performed. # See http://docs.python-requests.org/en/master/user/advanced/ from sfctl.config import (set_ca_cert, set_auth, set_aad_cache, set_cluster_endpoint, set_no_verify) from msrest import ServiceClient, Configuration from sfctl.auth import ClientCertAuthentication, AdalAuthentication select_arg_verify(endpoint, cert, key, pem, ca, aad, no_verify) if aad: new_token, new_cache = get_aad_token(endpoint, no_verify) set_aad_cache(new_token, new_cache) rest_client = ServiceClient(AdalAuthentication(no_verify), Configuration(endpoint)) # Make sure basic GET request succeeds rest_client.send(rest_client.get('/')).raise_for_status() else: client_cert = None if pem: client_cert = pem elif cert: client_cert = (cert, key) rest_client = ServiceClient( ClientCertAuthentication(client_cert, ca, no_verify), Configuration(endpoint)) # Make sure basic GET request succeeds rest_client.send(rest_client.get('/')).raise_for_status() set_cluster_endpoint(endpoint) set_no_verify(no_verify) set_ca_cert(ca) set_auth(pem, cert, key, aad)
def test_client_send(self): class MockHTTPDriver(object): def configure_session(self, **config): pass def send(self, request, **config): pass client = ServiceClient(self.creds, self.cfg) client.config.keep_alive = True session = mock.create_autospec(requests.Session) client._http_driver.session = session session.adapters = { "http://": HTTPAdapter(), "https://": HTTPAdapter(), } client.creds.signed_session.return_value = session client.creds.refresh_session.return_value = session # Be sure the mock does not trick me assert not hasattr(session.resolve_redirects, 'is_mrest_patched') request = ClientRequest('GET') client.send(request, stream=False) session.request.call_count = 0 session.request.assert_called_with( 'GET', None, allow_redirects=True, cert=None, headers={ 'User-Agent': self.cfg.user_agent, 'Test': 'true' # From global config }, stream=False, timeout=100, verify=True) assert session.resolve_redirects.is_mrest_patched client.send(request, headers={'id': '1234'}, content={'Test': 'Data'}, stream=False) session.request.assert_called_with( 'GET', None, data='{"Test": "Data"}', allow_redirects=True, cert=None, headers={ 'User-Agent': self.cfg.user_agent, 'Content-Length': '16', 'id': '1234', 'Test': 'true' # From global config }, stream=False, timeout=100, verify=True) self.assertEqual(session.request.call_count, 1) session.request.call_count = 0 assert session.resolve_redirects.is_mrest_patched session.request.side_effect = requests.RequestException("test") with self.assertRaises(ClientRequestError): client.send(request, headers={'id': '1234'}, content={'Test': 'Data'}, test='value', stream=False) session.request.assert_called_with( 'GET', None, data='{"Test": "Data"}', allow_redirects=True, cert=None, headers={ 'User-Agent': self.cfg.user_agent, 'Content-Length': '16', 'id': '1234', 'Test': 'true' # From global config }, stream=False, timeout=100, verify=True) self.assertEqual(session.request.call_count, 1) session.request.call_count = 0 assert session.resolve_redirects.is_mrest_patched session.request.side_effect = oauth2.rfc6749.errors.InvalidGrantError( "test") with self.assertRaises(TokenExpiredError): client.send(request, headers={'id': '1234'}, content={'Test': 'Data'}, test='value') self.assertEqual(session.request.call_count, 2) session.request.call_count = 0 session.request.side_effect = ValueError("test") with self.assertRaises(ValueError): client.send(request, headers={'id': '1234'}, content={'Test': 'Data'}, test='value')