Skip to content

Commit a4135a3

Browse files
fix: Skip checking projectid on cred if env var is set (#1349)
* fix: Skip checking projectid on cred if env var is set * add test for legacy project --------- Co-authored-by: Carl Lundin <108372512+clundin25@users.noreply.github.com>
1 parent 2b6a2cf commit a4135a3

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

google/auth/_default.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -660,24 +660,25 @@ def default(scopes=None, request=None, quota_project_id=None, default_scopes=Non
660660
credentials, scopes, default_scopes=default_scopes
661661
)
662662

663+
effective_project_id = explicit_project_id or project_id
664+
663665
# For external account credentials, scopes are required to determine
664666
# the project ID. Try to get the project ID again if not yet
665667
# determined.
666-
if not project_id and callable(
668+
if not effective_project_id and callable(
667669
getattr(credentials, "get_project_id", None)
668670
):
669671
if request is None:
670672
import google.auth.transport.requests
671673

672674
request = google.auth.transport.requests.Request()
673-
project_id = credentials.get_project_id(request=request)
675+
effective_project_id = credentials.get_project_id(request=request)
674676

675677
if quota_project_id and isinstance(
676678
credentials, CredentialsWithQuotaProject
677679
):
678680
credentials = credentials.with_quota_project(quota_project_id)
679681

680-
effective_project_id = explicit_project_id or project_id
681682
if not effective_project_id:
682683
_LOGGER.warning(
683684
"No project ID could be determined. Consider running "

tests/test__default.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,61 @@ def test_default_environ_external_credentials_identity_pool_impersonated(
10301030
assert project_id is mock.sentinel.project_id
10311031
assert credentials.scopes == ["https://www.google.com/calendar/feeds"]
10321032

1033+
# The credential.get_project_id should have been used in _get_external_account_credentials and default
1034+
assert get_project_id.call_count == 2
1035+
1036+
1037+
@EXTERNAL_ACCOUNT_GET_PROJECT_ID_PATCH
1038+
@mock.patch.dict(os.environ)
1039+
def test_default_environ_external_credentials_project_from_env(
1040+
get_project_id, monkeypatch, tmpdir
1041+
):
1042+
project_from_env = "project_from_env"
1043+
os.environ[environment_vars.PROJECT] = project_from_env
1044+
1045+
config_file = tmpdir.join("config.json")
1046+
config_file.write(json.dumps(IMPERSONATED_IDENTITY_POOL_DATA))
1047+
monkeypatch.setenv(environment_vars.CREDENTIALS, str(config_file))
1048+
1049+
credentials, project_id = _default.default(
1050+
scopes=["https://www.google.com/calendar/feeds"]
1051+
)
1052+
1053+
assert isinstance(credentials, identity_pool.Credentials)
1054+
assert not credentials.is_user
1055+
assert not credentials.is_workforce_pool
1056+
assert project_id == project_from_env
1057+
assert credentials.scopes == ["https://www.google.com/calendar/feeds"]
1058+
1059+
# The credential.get_project_id should have been used only in _get_external_account_credentials
1060+
assert get_project_id.call_count == 1
1061+
1062+
1063+
@EXTERNAL_ACCOUNT_GET_PROJECT_ID_PATCH
1064+
@mock.patch.dict(os.environ)
1065+
def test_default_environ_external_credentials_legacy_project_from_env(
1066+
get_project_id, monkeypatch, tmpdir
1067+
):
1068+
project_from_env = "project_from_env"
1069+
os.environ[environment_vars.LEGACY_PROJECT] = project_from_env
1070+
1071+
config_file = tmpdir.join("config.json")
1072+
config_file.write(json.dumps(IMPERSONATED_IDENTITY_POOL_DATA))
1073+
monkeypatch.setenv(environment_vars.CREDENTIALS, str(config_file))
1074+
1075+
credentials, project_id = _default.default(
1076+
scopes=["https://www.google.com/calendar/feeds"]
1077+
)
1078+
1079+
assert isinstance(credentials, identity_pool.Credentials)
1080+
assert not credentials.is_user
1081+
assert not credentials.is_workforce_pool
1082+
assert project_id == project_from_env
1083+
assert credentials.scopes == ["https://www.google.com/calendar/feeds"]
1084+
1085+
# The credential.get_project_id should have been used only in _get_external_account_credentials
1086+
assert get_project_id.call_count == 1
1087+
10331088

10341089
@EXTERNAL_ACCOUNT_GET_PROJECT_ID_PATCH
10351090
def test_default_environ_external_credentials_aws_impersonated(

0 commit comments

Comments
 (0)