Skip to content

Commit 333cb76

Browse files
fix: use gcloud creds flow (#705)
1 parent 3f2f3ea commit 333cb76

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

google/auth/_default.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,25 @@ def _get_gcloud_sdk_credentials():
198198
def _get_explicit_environ_credentials():
199199
"""Gets credentials from the GOOGLE_APPLICATION_CREDENTIALS environment
200200
variable."""
201+
from google.auth import _cloud_sdk
202+
203+
cloud_sdk_adc_path = _cloud_sdk.get_application_default_credentials_path()
201204
explicit_file = os.environ.get(environment_vars.CREDENTIALS)
202205

203206
_LOGGER.debug(
204207
"Checking %s for explicit credentials as part of auth process...", explicit_file
205208
)
206209

210+
if explicit_file is not None and explicit_file == cloud_sdk_adc_path:
211+
# Cloud sdk flow calls gcloud to fetch project id, so if the explicit
212+
# file path is cloud sdk credentials path, then we should fall back
213+
# to cloud sdk flow, otherwise project id cannot be obtained.
214+
_LOGGER.debug(
215+
"Explicit credentials path %s is the same as Cloud SDK credentials path, fall back to Cloud SDK credentials flow...",
216+
explicit_file,
217+
)
218+
return _get_gcloud_sdk_credentials()
219+
207220
if explicit_file is not None:
208221
credentials, project_id = load_credentials_from_file(
209222
os.environ[environment_vars.CREDENTIALS]

google/auth/_default_async.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,17 @@ def _get_gcloud_sdk_credentials():
127127
def _get_explicit_environ_credentials():
128128
"""Gets credentials from the GOOGLE_APPLICATION_CREDENTIALS environment
129129
variable."""
130+
from google.auth import _cloud_sdk
131+
132+
cloud_sdk_adc_path = _cloud_sdk.get_application_default_credentials_path()
130133
explicit_file = os.environ.get(environment_vars.CREDENTIALS)
131134

135+
if explicit_file is not None and explicit_file == cloud_sdk_adc_path:
136+
# Cloud sdk flow calls gcloud to fetch project id, so if the explicit
137+
# file path is cloud sdk credentials path, then we should fall back
138+
# to cloud sdk flow, otherwise project id cannot be obtained.
139+
return _get_gcloud_sdk_credentials()
140+
132141
if explicit_file is not None:
133142
credentials, project_id = load_credentials_from_file(
134143
os.environ[environment_vars.CREDENTIALS]

tests/test__default.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,24 @@ def test__get_explicit_environ_credentials_no_project_id(load, monkeypatch):
350350
assert project_id is None
351351

352352

353+
@mock.patch(
354+
"google.auth._cloud_sdk.get_application_default_credentials_path", autospec=True
355+
)
356+
@mock.patch("google.auth._default._get_gcloud_sdk_credentials", autospec=True)
357+
def test__get_explicit_environ_credentials_fallback_to_gcloud(
358+
get_gcloud_creds, get_adc_path, monkeypatch
359+
):
360+
# Set explicit credentials path to cloud sdk credentials path.
361+
get_adc_path.return_value = "filename"
362+
monkeypatch.setenv(environment_vars.CREDENTIALS, "filename")
363+
364+
_default._get_explicit_environ_credentials()
365+
366+
# Check we fall back to cloud sdk flow since explicit credentials path is
367+
# cloud sdk credentials path
368+
get_gcloud_creds.assert_called_once()
369+
370+
353371
@LOAD_FILE_PATCH
354372
@mock.patch(
355373
"google.auth._cloud_sdk.get_application_default_credentials_path", autospec=True

tests_async/test__default_async.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,24 @@ def test__get_explicit_environ_credentials_no_project_id(load, monkeypatch):
187187
assert project_id is None
188188

189189

190+
@mock.patch(
191+
"google.auth._cloud_sdk.get_application_default_credentials_path", autospec=True
192+
)
193+
@mock.patch("google.auth._default_async._get_gcloud_sdk_credentials", autospec=True)
194+
def test__get_explicit_environ_credentials_fallback_to_gcloud(
195+
get_gcloud_creds, get_adc_path, monkeypatch
196+
):
197+
# Set explicit credentials path to cloud sdk credentials path.
198+
get_adc_path.return_value = "filename"
199+
monkeypatch.setenv(environment_vars.CREDENTIALS, "filename")
200+
201+
_default._get_explicit_environ_credentials()
202+
203+
# Check we fall back to cloud sdk flow since explicit credentials path is
204+
# cloud sdk credentials path
205+
get_gcloud_creds.assert_called_once()
206+
207+
190208
@LOAD_FILE_PATCH
191209
@mock.patch(
192210
"google.auth._cloud_sdk.get_application_default_credentials_path", autospec=True

0 commit comments

Comments
 (0)