test_urllib3.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. # Copyright 2016 Google LLC
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os
  15. import sys
  16. import mock
  17. import OpenSSL
  18. import pytest
  19. from six.moves import http_client
  20. import urllib3
  21. from google.auth import environment_vars
  22. from google.auth import exceptions
  23. import google.auth.credentials
  24. import google.auth.transport._mtls_helper
  25. import google.auth.transport.urllib3
  26. from google.oauth2 import service_account
  27. from tests.transport import compliance
  28. class TestRequestResponse(compliance.RequestResponseTests):
  29. def make_request(self):
  30. http = urllib3.PoolManager()
  31. return google.auth.transport.urllib3.Request(http)
  32. def test_timeout(self):
  33. http = mock.create_autospec(urllib3.PoolManager)
  34. request = google.auth.transport.urllib3.Request(http)
  35. request(url="http://example.com", method="GET", timeout=5)
  36. assert http.request.call_args[1]["timeout"] == 5
  37. def test__make_default_http_with_certifi():
  38. http = google.auth.transport.urllib3._make_default_http()
  39. assert "cert_reqs" in http.connection_pool_kw
  40. @mock.patch.object(google.auth.transport.urllib3, "certifi", new=None)
  41. def test__make_default_http_without_certifi():
  42. http = google.auth.transport.urllib3._make_default_http()
  43. assert "cert_reqs" not in http.connection_pool_kw
  44. class CredentialsStub(google.auth.credentials.Credentials):
  45. def __init__(self, token="token"):
  46. super(CredentialsStub, self).__init__()
  47. self.token = token
  48. def apply(self, headers, token=None):
  49. headers["authorization"] = self.token
  50. def before_request(self, request, method, url, headers):
  51. self.apply(headers)
  52. def refresh(self, request):
  53. self.token += "1"
  54. def with_quota_project(self, quota_project_id):
  55. raise NotImplementedError()
  56. class HttpStub(object):
  57. def __init__(self, responses, headers=None):
  58. self.responses = responses
  59. self.requests = []
  60. self.headers = headers or {}
  61. def urlopen(self, method, url, body=None, headers=None, **kwargs):
  62. self.requests.append((method, url, body, headers, kwargs))
  63. return self.responses.pop(0)
  64. class ResponseStub(object):
  65. def __init__(self, status=http_client.OK, data=None):
  66. self.status = status
  67. self.data = data
  68. class TestMakeMutualTlsHttp(object):
  69. def test_success(self):
  70. http = google.auth.transport.urllib3._make_mutual_tls_http(
  71. pytest.public_cert_bytes, pytest.private_key_bytes
  72. )
  73. assert isinstance(http, urllib3.PoolManager)
  74. def test_crypto_error(self):
  75. with pytest.raises(OpenSSL.crypto.Error):
  76. google.auth.transport.urllib3._make_mutual_tls_http(
  77. b"invalid cert", b"invalid key"
  78. )
  79. @mock.patch.dict("sys.modules", {"OpenSSL.crypto": None})
  80. def test_import_error(self):
  81. with pytest.raises(ImportError):
  82. google.auth.transport.urllib3._make_mutual_tls_http(
  83. pytest.public_cert_bytes, pytest.private_key_bytes
  84. )
  85. class TestAuthorizedHttp(object):
  86. TEST_URL = "http://example.com"
  87. def test_authed_http_defaults(self):
  88. authed_http = google.auth.transport.urllib3.AuthorizedHttp(
  89. mock.sentinel.credentials
  90. )
  91. assert authed_http.credentials == mock.sentinel.credentials
  92. assert isinstance(authed_http.http, urllib3.PoolManager)
  93. def test_urlopen_no_refresh(self):
  94. credentials = mock.Mock(wraps=CredentialsStub())
  95. response = ResponseStub()
  96. http = HttpStub([response])
  97. authed_http = google.auth.transport.urllib3.AuthorizedHttp(
  98. credentials, http=http
  99. )
  100. result = authed_http.urlopen("GET", self.TEST_URL)
  101. assert result == response
  102. assert credentials.before_request.called
  103. assert not credentials.refresh.called
  104. assert http.requests == [
  105. ("GET", self.TEST_URL, None, {"authorization": "token"}, {})
  106. ]
  107. def test_urlopen_refresh(self):
  108. credentials = mock.Mock(wraps=CredentialsStub())
  109. final_response = ResponseStub(status=http_client.OK)
  110. # First request will 401, second request will succeed.
  111. http = HttpStub([ResponseStub(status=http_client.UNAUTHORIZED), final_response])
  112. authed_http = google.auth.transport.urllib3.AuthorizedHttp(
  113. credentials, http=http
  114. )
  115. authed_http = authed_http.urlopen("GET", "http://example.com")
  116. assert authed_http == final_response
  117. assert credentials.before_request.call_count == 2
  118. assert credentials.refresh.called
  119. assert http.requests == [
  120. ("GET", self.TEST_URL, None, {"authorization": "token"}, {}),
  121. ("GET", self.TEST_URL, None, {"authorization": "token1"}, {}),
  122. ]
  123. def test_urlopen_no_default_host(self):
  124. credentials = mock.create_autospec(service_account.Credentials)
  125. authed_http = google.auth.transport.urllib3.AuthorizedHttp(credentials)
  126. authed_http.credentials._create_self_signed_jwt.assert_not_called()
  127. def test_urlopen_with_default_host(self):
  128. default_host = "pubsub.googleapis.com"
  129. credentials = mock.create_autospec(service_account.Credentials)
  130. authed_http = google.auth.transport.urllib3.AuthorizedHttp(
  131. credentials, default_host=default_host
  132. )
  133. authed_http.credentials._create_self_signed_jwt.assert_called_once_with(
  134. "https://{}/".format(default_host)
  135. )
  136. def test_proxies(self):
  137. http = mock.create_autospec(urllib3.PoolManager)
  138. authed_http = google.auth.transport.urllib3.AuthorizedHttp(None, http=http)
  139. with authed_http:
  140. pass
  141. assert http.__enter__.called
  142. assert http.__exit__.called
  143. authed_http.headers = mock.sentinel.headers
  144. assert authed_http.headers == http.headers
  145. @mock.patch("google.auth.transport.urllib3._make_mutual_tls_http", autospec=True)
  146. def test_configure_mtls_channel_with_callback(self, mock_make_mutual_tls_http):
  147. callback = mock.Mock()
  148. callback.return_value = (pytest.public_cert_bytes, pytest.private_key_bytes)
  149. authed_http = google.auth.transport.urllib3.AuthorizedHttp(
  150. credentials=mock.Mock(), http=mock.Mock()
  151. )
  152. with pytest.warns(UserWarning):
  153. with mock.patch.dict(
  154. os.environ, {environment_vars.GOOGLE_API_USE_CLIENT_CERTIFICATE: "true"}
  155. ):
  156. is_mtls = authed_http.configure_mtls_channel(callback)
  157. assert is_mtls
  158. mock_make_mutual_tls_http.assert_called_once_with(
  159. cert=pytest.public_cert_bytes, key=pytest.private_key_bytes
  160. )
  161. @mock.patch("google.auth.transport.urllib3._make_mutual_tls_http", autospec=True)
  162. @mock.patch(
  163. "google.auth.transport._mtls_helper.get_client_cert_and_key", autospec=True
  164. )
  165. def test_configure_mtls_channel_with_metadata(
  166. self, mock_get_client_cert_and_key, mock_make_mutual_tls_http
  167. ):
  168. authed_http = google.auth.transport.urllib3.AuthorizedHttp(
  169. credentials=mock.Mock()
  170. )
  171. mock_get_client_cert_and_key.return_value = (
  172. True,
  173. pytest.public_cert_bytes,
  174. pytest.private_key_bytes,
  175. )
  176. with mock.patch.dict(
  177. os.environ, {environment_vars.GOOGLE_API_USE_CLIENT_CERTIFICATE: "true"}
  178. ):
  179. is_mtls = authed_http.configure_mtls_channel()
  180. assert is_mtls
  181. mock_get_client_cert_and_key.assert_called_once()
  182. mock_make_mutual_tls_http.assert_called_once_with(
  183. cert=pytest.public_cert_bytes, key=pytest.private_key_bytes
  184. )
  185. @mock.patch("google.auth.transport.urllib3._make_mutual_tls_http", autospec=True)
  186. @mock.patch(
  187. "google.auth.transport._mtls_helper.get_client_cert_and_key", autospec=True
  188. )
  189. def test_configure_mtls_channel_non_mtls(
  190. self, mock_get_client_cert_and_key, mock_make_mutual_tls_http
  191. ):
  192. authed_http = google.auth.transport.urllib3.AuthorizedHttp(
  193. credentials=mock.Mock()
  194. )
  195. mock_get_client_cert_and_key.return_value = (False, None, None)
  196. with mock.patch.dict(
  197. os.environ, {environment_vars.GOOGLE_API_USE_CLIENT_CERTIFICATE: "true"}
  198. ):
  199. is_mtls = authed_http.configure_mtls_channel()
  200. assert not is_mtls
  201. mock_get_client_cert_and_key.assert_called_once()
  202. mock_make_mutual_tls_http.assert_not_called()
  203. @mock.patch(
  204. "google.auth.transport._mtls_helper.get_client_cert_and_key", autospec=True
  205. )
  206. def test_configure_mtls_channel_exceptions(self, mock_get_client_cert_and_key):
  207. authed_http = google.auth.transport.urllib3.AuthorizedHttp(
  208. credentials=mock.Mock()
  209. )
  210. mock_get_client_cert_and_key.side_effect = exceptions.ClientCertError()
  211. with pytest.raises(exceptions.MutualTLSChannelError):
  212. with mock.patch.dict(
  213. os.environ, {environment_vars.GOOGLE_API_USE_CLIENT_CERTIFICATE: "true"}
  214. ):
  215. authed_http.configure_mtls_channel()
  216. mock_get_client_cert_and_key.return_value = (False, None, None)
  217. with mock.patch.dict("sys.modules"):
  218. sys.modules["OpenSSL"] = None
  219. with pytest.raises(exceptions.MutualTLSChannelError):
  220. with mock.patch.dict(
  221. os.environ,
  222. {environment_vars.GOOGLE_API_USE_CLIENT_CERTIFICATE: "true"},
  223. ):
  224. authed_http.configure_mtls_channel()
  225. @mock.patch(
  226. "google.auth.transport._mtls_helper.get_client_cert_and_key", autospec=True
  227. )
  228. def test_configure_mtls_channel_without_client_cert_env(
  229. self, get_client_cert_and_key
  230. ):
  231. callback = mock.Mock()
  232. authed_http = google.auth.transport.urllib3.AuthorizedHttp(
  233. credentials=mock.Mock(), http=mock.Mock()
  234. )
  235. # Test the callback is not called if GOOGLE_API_USE_CLIENT_CERTIFICATE is not set.
  236. is_mtls = authed_http.configure_mtls_channel(callback)
  237. assert not is_mtls
  238. callback.assert_not_called()
  239. # Test ADC client cert is not used if GOOGLE_API_USE_CLIENT_CERTIFICATE is not set.
  240. is_mtls = authed_http.configure_mtls_channel(callback)
  241. assert not is_mtls
  242. get_client_cert_and_key.assert_not_called()