| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent dea6a1b commit 614a3d0
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -27,11 +27,10 @@ | |||
| 27 | 27 | ||
| 28 | 28 | from google.auth import environment_vars | |
| 29 | 29 | from google.auth import exceptions | |
| 30 | - import google.auth.transport._http_client | ||
| 31 | 30 | ||
| 32 | 31 | if TYPE_CHECKING: # pragma: NO COVER | |
| 33 | - from google.auth.credentials import Credentials # noqa: F401 | ||
| 34 | - from google.auth.transport import Request # noqa: F401 | ||
| 32 | + import google.auth.credentials.Credentials # type: ignore | ||
| 33 | + import google.auth.transport.Request # type: ignore | ||
| 35 | 34 | ||
| 36 | 35 | _LOGGER = logging.getLogger(__name__) | |
| 37 | 36 | ||
@@ -390,22 +389,19 @@ def _get_gae_credentials(): | |||
| 390 | 389 | ||
| 391 | 390 | def _get_gce_credentials(request=None, quota_project_id=None): | |
| 392 | 391 | """Gets credentials and project ID from the GCE Metadata Service.""" | |
| 393 | - # Ping requires a transport, but we want application default credentials | ||
| 394 | - # to require no arguments. So, we'll use the _http_client transport which | ||
| 395 | - # uses http.client. This is only acceptable because the metadata server | ||
| 396 | - # doesn't do SSL and never requires proxies. | ||
| 397 | - | ||
| 398 | 392 | # While this library is normally bundled with compute_engine, there are | |
| 399 | 393 | # some cases where it's not available, so we tolerate ImportError. | |
| 394 | + # Compute Engine requires optional `requests` dependency. | ||
| 400 | 395 | try: | |
| 401 | 396 | from google.auth import compute_engine | |
| 402 | 397 | from google.auth.compute_engine import _metadata | |
| 398 | + import google.auth.transport.requests | ||
| 403 | 399 | except ImportError: | |
| 404 | 400 | _LOGGER.warning("Import of Compute Engine auth library failed.") | |
| 405 | 401 | return None, None | |
| 406 | 402 | ||
| 407 | 403 | if request is None: | |
| 408 | - request = google.auth.transport._http_client.Request() | ||
| 404 | + request = google.auth.transport.requests.Request() | ||
| 409 | 405 | ||
| 410 | 406 | if _metadata.is_on_gce(request=request): | |
| 411 | 407 | # Get the project ID. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -94,7 +94,7 @@ def __call__( | |||
| 94 | 94 | if parts.scheme != "http": | |
| 95 | 95 | raise exceptions.TransportError( | |
| 96 | 96 | "http.client transport only supports the http scheme, {}" | |
| 97 | - "was specified".format(parts.scheme) | ||
| 97 | + " was specified".format(parts.scheme) | ||
| 98 | 98 | ) | |
| 99 | 99 | ||
| 100 | 100 | connection = http_client.HTTPConnection(parts.netloc, timeout=timeout) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -890,6 +890,18 @@ def test__get_gce_credentials_explicit_request(ping): | |||
| 890 | 890 | ping.assert_called_with(request=mock.sentinel.request) | |
| 891 | 891 | ||
| 892 | 892 | ||
| 893 | + @mock.patch( | ||
| 894 | + "google.auth.compute_engine._metadata.is_on_gce", return_value=False, autospec=True | ||
| 895 | + ) | ||
| 896 | + @mock.patch("google.auth.transport.requests.Request", autospec=True) | ||
| 897 | + def test__get_gce_credentials_default_request(mock_request_cls, ping): | ||
| 898 | + credentials, project_id = _default._get_gce_credentials() | ||
| 899 | + mock_request_cls.assert_called_once() | ||
| 900 | + ping.assert_called_with(request=mock_request_cls.return_value) | ||
| 901 | + assert credentials is None | ||
| 902 | + assert project_id is None | ||
| 903 | + | ||
| 904 | + | ||
| 893 | 905 | @mock.patch( | |
| 894 | 906 | "google.auth._default._get_explicit_environ_credentials", | |
| 895 | 907 | return_value=(MOCK_CREDENTIALS, mock.sentinel.project_id), | |
@@ -1006,6 +1018,35 @@ def test_default_fail(unused_gce, unused_gae, unused_sdk, unused_explicit): | |||
| 1006 | 1018 | assert excinfo.match(_default._CLOUD_SDK_MISSING_CREDENTIALS) | |
| 1007 | 1019 | ||
| 1008 | 1020 | ||
| 1021 | + @mock.patch( | ||
| 1022 | + "google.auth._default._get_explicit_environ_credentials", | ||
| 1023 | + return_value=(None, None), | ||
| 1024 | + autospec=True, | ||
| 1025 | + ) | ||
| 1026 | + @mock.patch( | ||
| 1027 | + "google.auth._default._get_gcloud_sdk_credentials", | ||
| 1028 | + return_value=(None, None), | ||
| 1029 | + autospec=True, | ||
| 1030 | + ) | ||
| 1031 | + @mock.patch( | ||
| 1032 | + "google.auth._default._get_gae_credentials", | ||
| 1033 | + return_value=(None, None), | ||
| 1034 | + autospec=True, | ||
| 1035 | + ) | ||
| 1036 | + @mock.patch( | ||
| 1037 | + "google.auth.compute_engine._metadata.is_on_gce", return_value=False, autospec=True | ||
| 1038 | + ) | ||
| 1039 | + @mock.patch("google.auth.transport.requests.Request", autospec=True) | ||
| 1040 | + def test_default_gce_triggers_request_creation( | ||
| 1041 | + mock_request_cls, is_on_gce, unused_gae, unused_sdk, unused_explicit | ||
| 1042 | + ): | ||
| 1043 | + with pytest.raises(exceptions.DefaultCredentialsError): | ||
| 1044 | + _default.default() | ||
| 1045 | + | ||
| 1046 | + mock_request_cls.assert_called_once() | ||
| 1047 | + is_on_gce.assert_called_once_with(request=mock_request_cls.return_value) | ||
| 1048 | + | ||
| 1049 | + | ||
| 1009 | 1050 | @mock.patch( | |
| 1010 | 1051 | "google.auth._default._get_explicit_environ_credentials", | |
| 1011 | 1052 | return_value=(MOCK_CREDENTIALS, mock.sentinel.project_id), | |
| Back | FazBrowse Home | New Git URL |
0 commit comments