Skip to content

Commit

Permalink
fix: add with_universe_domain to service account and external cred
Browse files Browse the repository at this point in the history
  • Loading branch information
arithmetic1728 committed Nov 7, 2023
1 parent 9a7a8ec commit a143760
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
16 changes: 16 additions & 0 deletions google/auth/external_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,22 @@ def with_token_uri(self, token_uri):
new_cred._metrics_options = self._metrics_options
return new_cred

def with_universe_domain(self, universe_domain):
"""Create a copy of these credentials with the given universe domain.
Args:
universe_domain (str): The universe domain value.
Returns:
google.auth.external_account.Credentials: A new credentials
instance.
"""
kwargs = self._constructor_args()
kwargs.update(universe_domain=universe_domain)
new_cred = self.__class__(**kwargs)
new_cred._metrics_options = self._metrics_options
return new_cred

def _initialize_impersonated_credentials(self):
"""Generates an impersonated credentials.
Expand Down
16 changes: 16 additions & 0 deletions google/oauth2/service_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,22 @@ def with_always_use_jwt_access(self, always_use_jwt_access):
cred._always_use_jwt_access = always_use_jwt_access
return cred

def with_universe_domain(self, universe_domain):
"""Create a copy of these credentials with the given universe domain.
Args:
universe_domain (str): The universe domain value.
Returns:
google.auth.service_account.Credentials: A new credentials
instance.
"""
cred = self._make_copy()
cred._universe_domain = universe_domain
if universe_domain != _DEFAULT_UNIVERSE_DOMAIN:
cred._always_use_jwt_access = True
return cred

def with_subject(self, subject):
"""Create a copy of these credentials with the specified subject.
Expand Down
11 changes: 11 additions & 0 deletions tests/oauth2/test_service_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,17 @@ def test_with_token_uri(self):
creds_with_new_token_uri = credentials.with_token_uri(new_token_uri)
assert creds_with_new_token_uri._token_uri == new_token_uri

def test_with_universe_domain(self):
credentials = self.make_credentials()

new_credentials = credentials.with_universe_domain("dummy_universe.com")
assert new_credentials.universe_domain == "dummy_universe.com"
assert new_credentials._always_use_jwt_access

new_credentials = credentials.with_universe_domain("googleapis.com")
assert new_credentials.universe_domain == "googleapis.com"
assert not new_credentials._always_use_jwt_access

def test__with_always_use_jwt_access(self):
credentials = self.make_credentials()
assert not credentials._always_use_jwt_access
Expand Down
5 changes: 5 additions & 0 deletions tests/test_external_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,11 @@ def test_universe_domain(self):
credentials = self.make_credentials()
assert credentials.universe_domain == external_account._DEFAULT_UNIVERSE_DOMAIN

def test_with_universe_domain(self):
credentials = self.make_credentials()
new_credentials = credentials.with_universe_domain("dummy_universe.com")
assert new_credentials.universe_domain == "dummy_universe.com"

def test_info_workforce_pool(self):
credentials = self.make_workforce_pool_credentials(
workforce_pool_user_project=self.WORKFORCE_POOL_USER_PROJECT
Expand Down

0 comments on commit a143760

Please sign in to comment.