Skip to content

Commit 41599ae

Browse files
busunkim96tseaver
andauthored
refactor: split 'with_quota_project' into separate base class (#561)
Co-authored-by: Tres Seaver <tseaver@palladion.com>
1 parent 6269643 commit 41599ae

File tree

9 files changed

+34
-33
lines changed

9 files changed

+34
-33
lines changed

google/auth/app_engine.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ def get_project_id():
7777
return app_identity.get_application_id()
7878

7979

80-
class Credentials(credentials.Scoped, credentials.Signing, credentials.Credentials):
80+
class Credentials(
81+
credentials.Scoped, credentials.Signing, credentials.CredentialsWithQuotaProject
82+
):
8183
"""App Engine standard environment credentials.
8284
8385
These credentials use the App Engine App Identity API to obtain access
@@ -145,7 +147,7 @@ def with_scopes(self, scopes):
145147
quota_project_id=self.quota_project_id,
146148
)
147149

148-
@_helpers.copy_docstring(credentials.Credentials)
150+
@_helpers.copy_docstring(credentials.CredentialsWithQuotaProject)
149151
def with_quota_project(self, quota_project_id):
150152
return self.__class__(
151153
scopes=self._scopes,

google/auth/compute_engine/credentials.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from google.oauth2 import _client
3333

3434

35-
class Credentials(credentials.ReadOnlyScoped, credentials.Credentials):
35+
class Credentials(credentials.ReadOnlyScoped, credentials.CredentialsWithQuotaProject):
3636
"""Compute Engine Credentials.
3737
3838
These credentials use the Google Compute Engine metadata server to obtain
@@ -118,7 +118,7 @@ def requires_scopes(self):
118118
"""False: Compute Engine credentials can not be scoped."""
119119
return False
120120

121-
@_helpers.copy_docstring(credentials.Credentials)
121+
@_helpers.copy_docstring(credentials.CredentialsWithQuotaProject)
122122
def with_quota_project(self, quota_project_id):
123123
return self.__class__(
124124
service_account_email=self._service_account_email,
@@ -130,7 +130,7 @@ def with_quota_project(self, quota_project_id):
130130
_DEFAULT_TOKEN_URI = "https://www.googleapis.com/oauth2/v4/token"
131131

132132

133-
class IDTokenCredentials(credentials.Credentials, credentials.Signing):
133+
class IDTokenCredentials(credentials.CredentialsWithQuotaProject, credentials.Signing):
134134
"""Open ID Connect ID Token-based service account credentials.
135135
136136
These credentials relies on the default service account of a GCE instance.
@@ -254,7 +254,7 @@ def with_target_audience(self, target_audience):
254254
quota_project_id=self._quota_project_id,
255255
)
256256

257-
@_helpers.copy_docstring(credentials.Credentials)
257+
@_helpers.copy_docstring(credentials.CredentialsWithQuotaProject)
258258
def with_quota_project(self, quota_project_id):
259259

260260
# since the signer is already instantiated,

google/auth/credentials.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ def before_request(self, request, method, url, headers):
133133
self.refresh(request)
134134
self.apply(headers)
135135

136+
137+
class CredentialsWithQuotaProject(Credentials):
138+
"""Abstract base for credentials supporting ``with_quota_project`` factory"""
139+
136140
def with_quota_project(self, quota_project_id):
137141
"""Returns a copy of these credentials with a modified quota project
138142
@@ -143,7 +147,7 @@ def with_quota_project(self, quota_project_id):
143147
Returns:
144148
google.oauth2.credentials.Credentials: A new credentials instance.
145149
"""
146-
raise NotImplementedError("This class does not support quota project.")
150+
raise NotImplementedError("This credential does not support quota project.")
147151

148152

149153
class AnonymousCredentials(Credentials):
@@ -182,9 +186,6 @@ def apply(self, headers, token=None):
182186
def before_request(self, request, method, url, headers):
183187
"""Anonymous credentials do nothing to the request."""
184188

185-
def with_quota_project(self, quota_project_id):
186-
raise ValueError("Anonymous credentials don't support quota project.")
187-
188189

189190
@six.add_metaclass(abc.ABCMeta)
190191
class ReadOnlyScoped(object):

google/auth/impersonated_credentials.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def _make_iam_token_request(request, principal, headers, body):
115115
six.raise_from(new_exc, caught_exc)
116116

117117

118-
class Credentials(credentials.Credentials, credentials.Signing):
118+
class Credentials(credentials.CredentialsWithQuotaProject, credentials.Signing):
119119
"""This module defines impersonated credentials which are essentially
120120
impersonated identities.
121121
@@ -293,7 +293,7 @@ def service_account_email(self):
293293
def signer(self):
294294
return self
295295

296-
@_helpers.copy_docstring(credentials.Credentials)
296+
@_helpers.copy_docstring(credentials.CredentialsWithQuotaProject)
297297
def with_quota_project(self, quota_project_id):
298298
return self.__class__(
299299
self._source_credentials,
@@ -305,7 +305,7 @@ def with_quota_project(self, quota_project_id):
305305
)
306306

307307

308-
class IDTokenCredentials(credentials.Credentials):
308+
class IDTokenCredentials(credentials.CredentialsWithQuotaProject):
309309
"""Open ID Connect ID Token-based service account credentials.
310310
311311
"""
@@ -359,7 +359,7 @@ def with_include_email(self, include_email):
359359
quota_project_id=self._quota_project_id,
360360
)
361361

362-
@_helpers.copy_docstring(credentials.Credentials)
362+
@_helpers.copy_docstring(credentials.CredentialsWithQuotaProject)
363363
def with_quota_project(self, quota_project_id):
364364
return self.__class__(
365365
target_credentials=self._target_credentials,

google/auth/jwt.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,9 @@ def decode(token, certs=None, verify=True, audience=None):
288288
return payload
289289

290290

291-
class Credentials(google.auth.credentials.Signing, google.auth.credentials.Credentials):
291+
class Credentials(
292+
google.auth.credentials.Signing, google.auth.credentials.CredentialsWithQuotaProject
293+
):
292294
"""Credentials that use a JWT as the bearer token.
293295
294296
These credentials require an "audience" claim. This claim identifies the
@@ -493,7 +495,7 @@ def with_claims(
493495
quota_project_id=self._quota_project_id,
494496
)
495497

496-
@_helpers.copy_docstring(google.auth.credentials.Credentials)
498+
@_helpers.copy_docstring(google.auth.credentials.CredentialsWithQuotaProject)
497499
def with_quota_project(self, quota_project_id):
498500
return self.__class__(
499501
self._signer,
@@ -554,7 +556,7 @@ def signer(self):
554556

555557

556558
class OnDemandCredentials(
557-
google.auth.credentials.Signing, google.auth.credentials.Credentials
559+
google.auth.credentials.Signing, google.auth.credentials.CredentialsWithQuotaProject
558560
):
559561
"""On-demand JWT credentials.
560562
@@ -721,7 +723,7 @@ def with_claims(self, issuer=None, subject=None, additional_claims=None):
721723
quota_project_id=self._quota_project_id,
722724
)
723725

724-
@_helpers.copy_docstring(google.auth.credentials.Credentials)
726+
@_helpers.copy_docstring(google.auth.credentials.CredentialsWithQuotaProject)
725727
def with_quota_project(self, quota_project_id):
726728

727729
return self.__class__(

google/oauth2/credentials.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
_GOOGLE_OAUTH2_TOKEN_ENDPOINT = "https://oauth2.googleapis.com/token"
4848

4949

50-
class Credentials(credentials.ReadOnlyScoped, credentials.Credentials):
50+
class Credentials(credentials.ReadOnlyScoped, credentials.CredentialsWithQuotaProject):
5151
"""Credentials using OAuth 2.0 access and refresh tokens.
5252
5353
The credentials are considered immutable. If you want to modify the
@@ -161,7 +161,7 @@ def requires_scopes(self):
161161
the initial token is requested and can not be changed."""
162162
return False
163163

164-
@_helpers.copy_docstring(credentials.Credentials)
164+
@_helpers.copy_docstring(credentials.CredentialsWithQuotaProject)
165165
def with_quota_project(self, quota_project_id):
166166

167167
return self.__class__(
@@ -305,7 +305,7 @@ def to_json(self, strip=None):
305305
return json.dumps(prep)
306306

307307

308-
class UserAccessTokenCredentials(credentials.Credentials):
308+
class UserAccessTokenCredentials(credentials.CredentialsWithQuotaProject):
309309
"""Access token credentials for user account.
310310
311311
Obtain the access token for a given user account or the current active
@@ -336,7 +336,7 @@ def with_account(self, account):
336336
"""
337337
return self.__class__(account=account, quota_project_id=self._quota_project_id)
338338

339-
@_helpers.copy_docstring(credentials.Credentials)
339+
@_helpers.copy_docstring(credentials.CredentialsWithQuotaProject)
340340
def with_quota_project(self, quota_project_id):
341341
return self.__class__(account=self._account, quota_project_id=quota_project_id)
342342

google/oauth2/service_account.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@
8282
_DEFAULT_TOKEN_LIFETIME_SECS = 3600 # 1 hour in seconds
8383

8484

85-
class Credentials(credentials.Signing, credentials.Scoped, credentials.Credentials):
85+
class Credentials(
86+
credentials.Signing, credentials.Scoped, credentials.CredentialsWithQuotaProject
87+
):
8688
"""Service account credentials
8789
8890
Usually, you'll create these credentials with one of the helper
@@ -306,7 +308,7 @@ def with_claims(self, additional_claims):
306308
additional_claims=new_additional_claims,
307309
)
308310

309-
@_helpers.copy_docstring(credentials.Credentials)
311+
@_helpers.copy_docstring(credentials.CredentialsWithQuotaProject)
310312
def with_quota_project(self, quota_project_id):
311313

312314
return self.__class__(
@@ -375,7 +377,7 @@ def signer_email(self):
375377
return self._service_account_email
376378

377379

378-
class IDTokenCredentials(credentials.Signing, credentials.Credentials):
380+
class IDTokenCredentials(credentials.Signing, credentials.CredentialsWithQuotaProject):
379381
"""Open ID Connect ID Token-based service account credentials.
380382
381383
These credentials are largely similar to :class:`.Credentials`, but instead
@@ -533,7 +535,7 @@ def with_target_audience(self, target_audience):
533535
quota_project_id=self.quota_project_id,
534536
)
535537

536-
@_helpers.copy_docstring(credentials.Credentials)
538+
@_helpers.copy_docstring(credentials.CredentialsWithQuotaProject)
537539
def with_quota_project(self, quota_project_id):
538540
return self.__class__(
539541
self._signer,

tests/test__default.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
with open(SERVICE_ACCOUNT_FILE) as fh:
5050
SERVICE_ACCOUNT_FILE_DATA = json.load(fh)
5151

52-
MOCK_CREDENTIALS = mock.Mock(spec=credentials.Credentials)
52+
MOCK_CREDENTIALS = mock.Mock(spec=credentials.CredentialsWithQuotaProject)
5353
MOCK_CREDENTIALS.with_quota_project.return_value = MOCK_CREDENTIALS
5454

5555
LOAD_FILE_PATCH = mock.patch(

tests/test_credentials.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,6 @@ def test_anonymous_credentials_before_request():
115115
assert headers == {}
116116

117117

118-
def test_anonymous_credentials_with_quota_project():
119-
with pytest.raises(ValueError):
120-
anon = credentials.AnonymousCredentials()
121-
anon.with_quota_project("project-foo")
122-
123-
124118
class ReadOnlyScopedCredentialsImpl(credentials.ReadOnlyScoped, CredentialsImpl):
125119
@property
126120
def requires_scopes(self):

0 commit comments

Comments
 (0)