Skip to content

Commit ab2be5d

Browse files
authored
fix: don't add empty quota project (#560)
1 parent 9058f1f commit ab2be5d

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

google/auth/_default.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,13 @@ def load_credentials_from_file(filename, scopes=None, quota_project_id=None):
116116
try:
117117
credentials = credentials.Credentials.from_authorized_user_info(
118118
info, scopes=scopes
119-
).with_quota_project(quota_project_id)
119+
)
120120
except ValueError as caught_exc:
121121
msg = "Failed to load authorized user credentials from {}".format(filename)
122122
new_exc = exceptions.DefaultCredentialsError(msg, caught_exc)
123123
six.raise_from(new_exc, caught_exc)
124+
if quota_project_id:
125+
credentials = credentials.with_quota_project(quota_project_id)
124126
if not credentials.quota_project_id:
125127
_warn_about_problematic_credentials(credentials)
126128
return credentials, None
@@ -131,11 +133,13 @@ def load_credentials_from_file(filename, scopes=None, quota_project_id=None):
131133
try:
132134
credentials = service_account.Credentials.from_service_account_info(
133135
info, scopes=scopes
134-
).with_quota_project(quota_project_id)
136+
)
135137
except ValueError as caught_exc:
136138
msg = "Failed to load service account credentials from {}".format(filename)
137139
new_exc = exceptions.DefaultCredentialsError(msg, caught_exc)
138140
six.raise_from(new_exc, caught_exc)
141+
if quota_project_id:
142+
credentials = credentials.with_quota_project(quota_project_id)
139143
return credentials, info.get("project_id")
140144

141145
else:
@@ -317,9 +321,10 @@ def default(scopes=None, request=None, quota_project_id=None):
317321
for checker in checkers:
318322
credentials, project_id = checker()
319323
if credentials is not None:
320-
credentials = with_scopes_if_required(
321-
credentials, scopes
322-
).with_quota_project(quota_project_id)
324+
credentials = with_scopes_if_required(credentials, scopes)
325+
if quota_project_id:
326+
credentials = credentials.with_quota_project(quota_project_id)
327+
323328
effective_project_id = explicit_project_id or project_id
324329
if not effective_project_id:
325330
_LOGGER.warning(

tests/test__default.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,15 @@ def test_load_credentials_from_file_service_account_with_scopes():
165165
assert credentials.scopes == ["https://www.google.com/calendar/feeds"]
166166

167167

168+
def test_load_credentials_from_file_service_account_with_quota_project():
169+
credentials, project_id = _default.load_credentials_from_file(
170+
SERVICE_ACCOUNT_FILE, quota_project_id="project-foo"
171+
)
172+
assert isinstance(credentials, service_account.Credentials)
173+
assert project_id == SERVICE_ACCOUNT_FILE_DATA["project_id"]
174+
assert credentials.quota_project_id == "project-foo"
175+
176+
168177
def test_load_credentials_from_file_service_account_bad_format(tmpdir):
169178
filename = tmpdir.join("serivce_account_bad.json")
170179
filename.write(json.dumps({"type": "service_account"}))
@@ -465,6 +474,18 @@ def test_default_scoped(with_scopes, unused_get):
465474
with_scopes.assert_called_once_with(MOCK_CREDENTIALS, scopes)
466475

467476

477+
@mock.patch(
478+
"google.auth._default._get_explicit_environ_credentials",
479+
return_value=(MOCK_CREDENTIALS, mock.sentinel.project_id),
480+
autospec=True,
481+
)
482+
def test_default_quota_project(with_quota_project):
483+
credentials, project_id = _default.default(quota_project_id="project-foo")
484+
485+
MOCK_CREDENTIALS.with_quota_project.assert_called_once_with("project-foo")
486+
assert project_id == mock.sentinel.project_id
487+
488+
468489
@mock.patch(
469490
"google.auth._default._get_explicit_environ_credentials",
470491
return_value=(MOCK_CREDENTIALS, mock.sentinel.project_id),

0 commit comments

Comments
 (0)