From f185ee5ae26d8679a8e521ba9c9d7648f6e17988 Mon Sep 17 00:00:00 2001 From: ohmayr Date: Tue, 27 Feb 2024 03:38:23 +0000 Subject: [PATCH 01/18] add common logic for supporting universe domain --- google/api_core/universe_helpers.py | 63 +++++++++++++++++++++++++++++ tests/unit/test_universe_helpers.py | 39 ++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 google/api_core/universe_helpers.py create mode 100644 tests/unit/test_universe_helpers.py diff --git a/google/api_core/universe_helpers.py b/google/api_core/universe_helpers.py new file mode 100644 index 00000000..d1bba156 --- /dev/null +++ b/google/api_core/universe_helpers.py @@ -0,0 +1,63 @@ + + +from google.auth.exceptions import MutualTLSChannelError +from google.auth.credentials import Credentials as ga_credentials +from oauth2client.client import Credentials as oauth2_credentials +from typing import Optional, Union + +_DEFAULT_UNIVERSE = "googleapis.com" + +_mTLS_Universe_Error = MutualTLSChannelError(f"mTLS is not supported in any universe other than {_DEFAULT_UNIVERSE}.") +_Empty_Universe_Error = ValueError("Universe Domain cannot be an empty string.") + +class _UniverseMismatchError(ValueError): + def __init__(self, client_universe, credentials_universe): + message = ( + f"The configured universe domain ({client_universe}) does not match the universe domain " + f"found in the credentials ({credentials_universe}). " + "If you haven't configured the universe domain explicitly, " + f"`{_DEFAULT_UNIVERSE}` is the default." + ) + super().__init__(message) + +def _get_universe_domain(client_universe_domain: Optional[str], universe_domain_env: Optional[str]) -> str: # temp disable Optional + """Return the universe domain used by the client. + + Args: + client_universe_domain (Optional[str]): The universe domain configured via the client options. + universe_domain_env (Optional[str]): The universe domain configured via the "GOOGLE_CLOUD_UNIVERSE_DOMAIN" environment variable. + + Returns: + str: The universe domain to be used by the client. + + Raises: + ValueError: If the universe domain is an empty string. + """ + universe_domain = _DEFAULT_UNIVERSE + if client_universe_domain is not None: + universe_domain = client_universe_domain + elif universe_domain_env is not None: + universe_domain = universe_domain_env + if len(universe_domain.strip()) == 0: + raise _Empty_Universe_Error + return universe_domain + +def _compare_universes(client_universe: str, + credentials: Union[ga_credentials, oauth2_credentials]) -> bool: + """Returns True iff the universe domains used by the client and credentials match. + + Args: + client_universe (str): The universe domain configured via the client options. + credentials Union[google.auth.credentials.Credentials, oauth2client.client.Credentials]: The credentials being used in the client. + + Returns: + bool: True iff client_universe matches the universe in credentials. + + Raises: + ValueError: when client_universe does not match the universe in credentials. + """ + credentials_universe = getattr(credentials, "universe_domain", _DEFAULT_UNIVERSE) + + if client_universe != credentials_universe: + raise _UniverseMismatchError(client_universe, credentials_universe) + return True diff --git a/tests/unit/test_universe_helpers.py b/tests/unit/test_universe_helpers.py new file mode 100644 index 00000000..7c1807f1 --- /dev/null +++ b/tests/unit/test_universe_helpers.py @@ -0,0 +1,39 @@ +import pytest +from google.api_core import universe_helpers +from google.auth import credentials +from oauth2client import client + +def test__get_universe_domain(): + client_universe_domain = "foo.com" + universe_domain_env = "bar.com" + + assert universe_helpers._get_universe_domain(client_universe_domain, universe_domain_env) == client_universe_domain + assert universe_helpers._get_universe_domain(None, universe_domain_env) == universe_domain_env + assert universe_helpers._get_universe_domain(None, None) == universe_helpers._DEFAULT_UNIVERSE + + with pytest.raises(ValueError) as excinfo: + universe_helpers._get_universe_domain("", None) + assert str(excinfo.value) == str(universe_helpers._Empty_Universe_Error) + + +def test__compare_universes(): + ga_credentials = credentials.AnonymousCredentials() + oauth2_credentials = client.GoogleCredentials(None, None, None, None, None, None, None, None) + mismatch_err_msg = ( + "The configured universe domain (foo.com) does not match the universe domain " + "found in the credentials (googleapis.com). " + "If you haven't configured the universe domain explicitly, " + "`googleapis.com` is the default." + ) + + assert universe_helpers._compare_universes(universe_helpers._DEFAULT_UNIVERSE, ga_credentials) == True + assert universe_helpers._compare_universes(universe_helpers._DEFAULT_UNIVERSE, oauth2_credentials) == True + + with pytest.raises(ValueError) as excinfo: + universe_helpers._compare_universes("foo.com", ga_credentials) + assert str(excinfo.value) == mismatch_err_msg + + with pytest.raises(ValueError) as excinfo: + universe_helpers._compare_universes("foo.com", oauth2_credentials) + assert str(excinfo.value) == mismatch_err_msg + \ No newline at end of file From 7f8515877f4f2550fc0c2e73bbb75da96cf9dcec Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Tue, 27 Feb 2024 03:40:43 +0000 Subject: [PATCH 02/18] =?UTF-8?q?=F0=9F=A6=89=20Updates=20from=20OwlBot=20?= =?UTF-8?q?post-processor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --- google/api_core/universe_helpers.py | 18 ++++++++---- tests/unit/test_universe_helpers.py | 45 ++++++++++++++++++++++------- 2 files changed, 46 insertions(+), 17 deletions(-) diff --git a/google/api_core/universe_helpers.py b/google/api_core/universe_helpers.py index d1bba156..fc237257 100644 --- a/google/api_core/universe_helpers.py +++ b/google/api_core/universe_helpers.py @@ -1,5 +1,3 @@ - - from google.auth.exceptions import MutualTLSChannelError from google.auth.credentials import Credentials as ga_credentials from oauth2client.client import Credentials as oauth2_credentials @@ -7,9 +5,12 @@ _DEFAULT_UNIVERSE = "googleapis.com" -_mTLS_Universe_Error = MutualTLSChannelError(f"mTLS is not supported in any universe other than {_DEFAULT_UNIVERSE}.") +_mTLS_Universe_Error = MutualTLSChannelError( + f"mTLS is not supported in any universe other than {_DEFAULT_UNIVERSE}." +) _Empty_Universe_Error = ValueError("Universe Domain cannot be an empty string.") + class _UniverseMismatchError(ValueError): def __init__(self, client_universe, credentials_universe): message = ( @@ -20,7 +21,10 @@ def __init__(self, client_universe, credentials_universe): ) super().__init__(message) -def _get_universe_domain(client_universe_domain: Optional[str], universe_domain_env: Optional[str]) -> str: # temp disable Optional + +def _get_universe_domain( + client_universe_domain: Optional[str], universe_domain_env: Optional[str] +) -> str: # temp disable Optional """Return the universe domain used by the client. Args: @@ -42,8 +46,10 @@ def _get_universe_domain(client_universe_domain: Optional[str], universe_domain_ raise _Empty_Universe_Error return universe_domain -def _compare_universes(client_universe: str, - credentials: Union[ga_credentials, oauth2_credentials]) -> bool: + +def _compare_universes( + client_universe: str, credentials: Union[ga_credentials, oauth2_credentials] +) -> bool: """Returns True iff the universe domains used by the client and credentials match. Args: diff --git a/tests/unit/test_universe_helpers.py b/tests/unit/test_universe_helpers.py index 7c1807f1..72a82c4f 100644 --- a/tests/unit/test_universe_helpers.py +++ b/tests/unit/test_universe_helpers.py @@ -3,13 +3,25 @@ from google.auth import credentials from oauth2client import client + def test__get_universe_domain(): client_universe_domain = "foo.com" universe_domain_env = "bar.com" - assert universe_helpers._get_universe_domain(client_universe_domain, universe_domain_env) == client_universe_domain - assert universe_helpers._get_universe_domain(None, universe_domain_env) == universe_domain_env - assert universe_helpers._get_universe_domain(None, None) == universe_helpers._DEFAULT_UNIVERSE + assert ( + universe_helpers._get_universe_domain( + client_universe_domain, universe_domain_env + ) + == client_universe_domain + ) + assert ( + universe_helpers._get_universe_domain(None, universe_domain_env) + == universe_domain_env + ) + assert ( + universe_helpers._get_universe_domain(None, None) + == universe_helpers._DEFAULT_UNIVERSE + ) with pytest.raises(ValueError) as excinfo: universe_helpers._get_universe_domain("", None) @@ -18,16 +30,28 @@ def test__get_universe_domain(): def test__compare_universes(): ga_credentials = credentials.AnonymousCredentials() - oauth2_credentials = client.GoogleCredentials(None, None, None, None, None, None, None, None) + oauth2_credentials = client.GoogleCredentials( + None, None, None, None, None, None, None, None + ) mismatch_err_msg = ( - "The configured universe domain (foo.com) does not match the universe domain " - "found in the credentials (googleapis.com). " - "If you haven't configured the universe domain explicitly, " - "`googleapis.com` is the default." + "The configured universe domain (foo.com) does not match the universe domain " + "found in the credentials (googleapis.com). " + "If you haven't configured the universe domain explicitly, " + "`googleapis.com` is the default." ) - assert universe_helpers._compare_universes(universe_helpers._DEFAULT_UNIVERSE, ga_credentials) == True - assert universe_helpers._compare_universes(universe_helpers._DEFAULT_UNIVERSE, oauth2_credentials) == True + assert ( + universe_helpers._compare_universes( + universe_helpers._DEFAULT_UNIVERSE, ga_credentials + ) + == True + ) + assert ( + universe_helpers._compare_universes( + universe_helpers._DEFAULT_UNIVERSE, oauth2_credentials + ) + == True + ) with pytest.raises(ValueError) as excinfo: universe_helpers._compare_universes("foo.com", ga_credentials) @@ -36,4 +60,3 @@ def test__compare_universes(): with pytest.raises(ValueError) as excinfo: universe_helpers._compare_universes("foo.com", oauth2_credentials) assert str(excinfo.value) == mismatch_err_msg - \ No newline at end of file From 7660a2ac5018c6b0c0b790699c48c0666b794fd5 Mon Sep 17 00:00:00 2001 From: ohmayr Date: Tue, 27 Feb 2024 16:32:25 +0000 Subject: [PATCH 03/18] fix lint issues --- google/api_core/universe_helpers.py | 18 ++++++++---- tests/unit/test_universe_helpers.py | 45 ++++++++++++++++++++++------- 2 files changed, 46 insertions(+), 17 deletions(-) diff --git a/google/api_core/universe_helpers.py b/google/api_core/universe_helpers.py index d1bba156..fc237257 100644 --- a/google/api_core/universe_helpers.py +++ b/google/api_core/universe_helpers.py @@ -1,5 +1,3 @@ - - from google.auth.exceptions import MutualTLSChannelError from google.auth.credentials import Credentials as ga_credentials from oauth2client.client import Credentials as oauth2_credentials @@ -7,9 +5,12 @@ _DEFAULT_UNIVERSE = "googleapis.com" -_mTLS_Universe_Error = MutualTLSChannelError(f"mTLS is not supported in any universe other than {_DEFAULT_UNIVERSE}.") +_mTLS_Universe_Error = MutualTLSChannelError( + f"mTLS is not supported in any universe other than {_DEFAULT_UNIVERSE}." +) _Empty_Universe_Error = ValueError("Universe Domain cannot be an empty string.") + class _UniverseMismatchError(ValueError): def __init__(self, client_universe, credentials_universe): message = ( @@ -20,7 +21,10 @@ def __init__(self, client_universe, credentials_universe): ) super().__init__(message) -def _get_universe_domain(client_universe_domain: Optional[str], universe_domain_env: Optional[str]) -> str: # temp disable Optional + +def _get_universe_domain( + client_universe_domain: Optional[str], universe_domain_env: Optional[str] +) -> str: # temp disable Optional """Return the universe domain used by the client. Args: @@ -42,8 +46,10 @@ def _get_universe_domain(client_universe_domain: Optional[str], universe_domain_ raise _Empty_Universe_Error return universe_domain -def _compare_universes(client_universe: str, - credentials: Union[ga_credentials, oauth2_credentials]) -> bool: + +def _compare_universes( + client_universe: str, credentials: Union[ga_credentials, oauth2_credentials] +) -> bool: """Returns True iff the universe domains used by the client and credentials match. Args: diff --git a/tests/unit/test_universe_helpers.py b/tests/unit/test_universe_helpers.py index 7c1807f1..72a82c4f 100644 --- a/tests/unit/test_universe_helpers.py +++ b/tests/unit/test_universe_helpers.py @@ -3,13 +3,25 @@ from google.auth import credentials from oauth2client import client + def test__get_universe_domain(): client_universe_domain = "foo.com" universe_domain_env = "bar.com" - assert universe_helpers._get_universe_domain(client_universe_domain, universe_domain_env) == client_universe_domain - assert universe_helpers._get_universe_domain(None, universe_domain_env) == universe_domain_env - assert universe_helpers._get_universe_domain(None, None) == universe_helpers._DEFAULT_UNIVERSE + assert ( + universe_helpers._get_universe_domain( + client_universe_domain, universe_domain_env + ) + == client_universe_domain + ) + assert ( + universe_helpers._get_universe_domain(None, universe_domain_env) + == universe_domain_env + ) + assert ( + universe_helpers._get_universe_domain(None, None) + == universe_helpers._DEFAULT_UNIVERSE + ) with pytest.raises(ValueError) as excinfo: universe_helpers._get_universe_domain("", None) @@ -18,16 +30,28 @@ def test__get_universe_domain(): def test__compare_universes(): ga_credentials = credentials.AnonymousCredentials() - oauth2_credentials = client.GoogleCredentials(None, None, None, None, None, None, None, None) + oauth2_credentials = client.GoogleCredentials( + None, None, None, None, None, None, None, None + ) mismatch_err_msg = ( - "The configured universe domain (foo.com) does not match the universe domain " - "found in the credentials (googleapis.com). " - "If you haven't configured the universe domain explicitly, " - "`googleapis.com` is the default." + "The configured universe domain (foo.com) does not match the universe domain " + "found in the credentials (googleapis.com). " + "If you haven't configured the universe domain explicitly, " + "`googleapis.com` is the default." ) - assert universe_helpers._compare_universes(universe_helpers._DEFAULT_UNIVERSE, ga_credentials) == True - assert universe_helpers._compare_universes(universe_helpers._DEFAULT_UNIVERSE, oauth2_credentials) == True + assert ( + universe_helpers._compare_universes( + universe_helpers._DEFAULT_UNIVERSE, ga_credentials + ) + == True + ) + assert ( + universe_helpers._compare_universes( + universe_helpers._DEFAULT_UNIVERSE, oauth2_credentials + ) + == True + ) with pytest.raises(ValueError) as excinfo: universe_helpers._compare_universes("foo.com", ga_credentials) @@ -36,4 +60,3 @@ def test__compare_universes(): with pytest.raises(ValueError) as excinfo: universe_helpers._compare_universes("foo.com", oauth2_credentials) assert str(excinfo.value) == mismatch_err_msg - \ No newline at end of file From cd18267237fe4308d6273d98c38da65634abc781 Mon Sep 17 00:00:00 2001 From: ohmayr Date: Tue, 27 Feb 2024 16:42:40 +0000 Subject: [PATCH 04/18] add dependency to oauth2client --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index 47a3c203..21423436 100644 --- a/setup.py +++ b/setup.py @@ -33,6 +33,7 @@ "protobuf>=3.19.5,<5.0.0.dev0,!=3.20.0,!=3.20.1,!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5", "google-auth >= 2.14.1, < 3.0.dev0", "requests >= 2.18.0, < 3.0.0.dev0", + "oauth2client", ] extras = { "grpc": [ From 16aa621771af105090e689acdf603359d45d52b6 Mon Sep 17 00:00:00 2001 From: ohmayr Date: Tue, 27 Feb 2024 16:51:14 +0000 Subject: [PATCH 05/18] update test cases --- tests/unit/test_universe_helpers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/test_universe_helpers.py b/tests/unit/test_universe_helpers.py index 72a82c4f..011bcc04 100644 --- a/tests/unit/test_universe_helpers.py +++ b/tests/unit/test_universe_helpers.py @@ -44,13 +44,13 @@ def test__compare_universes(): universe_helpers._compare_universes( universe_helpers._DEFAULT_UNIVERSE, ga_credentials ) - == True + is True ) assert ( universe_helpers._compare_universes( universe_helpers._DEFAULT_UNIVERSE, oauth2_credentials ) - == True + is True ) with pytest.raises(ValueError) as excinfo: From 3bd6c5dd87ff1e48465b292fa807e73143bbf185 Mon Sep 17 00:00:00 2001 From: ohmayr Date: Tue, 27 Feb 2024 18:22:07 +0000 Subject: [PATCH 06/18] remove dependency to oauth2client --- google/api_core/universe_helpers.py | 8 +++----- setup.py | 1 - tests/unit/test_universe_helpers.py | 14 -------------- 3 files changed, 3 insertions(+), 20 deletions(-) diff --git a/google/api_core/universe_helpers.py b/google/api_core/universe_helpers.py index fc237257..9e32a36d 100644 --- a/google/api_core/universe_helpers.py +++ b/google/api_core/universe_helpers.py @@ -1,7 +1,5 @@ from google.auth.exceptions import MutualTLSChannelError -from google.auth.credentials import Credentials as ga_credentials -from oauth2client.client import Credentials as oauth2_credentials -from typing import Optional, Union +from typing import Any, Optional _DEFAULT_UNIVERSE = "googleapis.com" @@ -48,13 +46,13 @@ def _get_universe_domain( def _compare_universes( - client_universe: str, credentials: Union[ga_credentials, oauth2_credentials] + client_universe: str, credentials: Any ) -> bool: """Returns True iff the universe domains used by the client and credentials match. Args: client_universe (str): The universe domain configured via the client options. - credentials Union[google.auth.credentials.Credentials, oauth2client.client.Credentials]: The credentials being used in the client. + credentials Any: The credentials being used in the client. Returns: bool: True iff client_universe matches the universe in credentials. diff --git a/setup.py b/setup.py index 21423436..47a3c203 100644 --- a/setup.py +++ b/setup.py @@ -33,7 +33,6 @@ "protobuf>=3.19.5,<5.0.0.dev0,!=3.20.0,!=3.20.1,!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5", "google-auth >= 2.14.1, < 3.0.dev0", "requests >= 2.18.0, < 3.0.0.dev0", - "oauth2client", ] extras = { "grpc": [ diff --git a/tests/unit/test_universe_helpers.py b/tests/unit/test_universe_helpers.py index 011bcc04..ee9213d5 100644 --- a/tests/unit/test_universe_helpers.py +++ b/tests/unit/test_universe_helpers.py @@ -1,7 +1,6 @@ import pytest from google.api_core import universe_helpers from google.auth import credentials -from oauth2client import client def test__get_universe_domain(): @@ -30,9 +29,6 @@ def test__get_universe_domain(): def test__compare_universes(): ga_credentials = credentials.AnonymousCredentials() - oauth2_credentials = client.GoogleCredentials( - None, None, None, None, None, None, None, None - ) mismatch_err_msg = ( "The configured universe domain (foo.com) does not match the universe domain " "found in the credentials (googleapis.com). " @@ -46,17 +42,7 @@ def test__compare_universes(): ) is True ) - assert ( - universe_helpers._compare_universes( - universe_helpers._DEFAULT_UNIVERSE, oauth2_credentials - ) - is True - ) with pytest.raises(ValueError) as excinfo: universe_helpers._compare_universes("foo.com", ga_credentials) assert str(excinfo.value) == mismatch_err_msg - - with pytest.raises(ValueError) as excinfo: - universe_helpers._compare_universes("foo.com", oauth2_credentials) - assert str(excinfo.value) == mismatch_err_msg From 0b58823688e64fee415e7b97b575e6841bc8d50b Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Tue, 27 Feb 2024 18:23:53 +0000 Subject: [PATCH 07/18] =?UTF-8?q?=F0=9F=A6=89=20Updates=20from=20OwlBot=20?= =?UTF-8?q?post-processor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --- google/api_core/universe_helpers.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/google/api_core/universe_helpers.py b/google/api_core/universe_helpers.py index 9e32a36d..f4605423 100644 --- a/google/api_core/universe_helpers.py +++ b/google/api_core/universe_helpers.py @@ -45,9 +45,7 @@ def _get_universe_domain( return universe_domain -def _compare_universes( - client_universe: str, credentials: Any -) -> bool: +def _compare_universes(client_universe: str, credentials: Any) -> bool: """Returns True iff the universe domains used by the client and credentials match. Args: From 9172a2c70c7f4921b1d5b6b96ca3c3174514225f Mon Sep 17 00:00:00 2001 From: ohmayr Date: Tue, 27 Feb 2024 18:24:37 +0000 Subject: [PATCH 08/18] fix lint issues --- google/api_core/universe_helpers.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/google/api_core/universe_helpers.py b/google/api_core/universe_helpers.py index 9e32a36d..f4605423 100644 --- a/google/api_core/universe_helpers.py +++ b/google/api_core/universe_helpers.py @@ -45,9 +45,7 @@ def _get_universe_domain( return universe_domain -def _compare_universes( - client_universe: str, credentials: Any -) -> bool: +def _compare_universes(client_universe: str, credentials: Any) -> bool: """Returns True iff the universe domains used by the client and credentials match. Args: From e9dffa8a7cf2a2502a09b8be74dbeaef53c0b098 Mon Sep 17 00:00:00 2001 From: ohmayr Date: Tue, 27 Feb 2024 21:40:24 +0000 Subject: [PATCH 09/18] updates to universe helpers --- google/api_core/universe_helpers.py | 21 ++++++++++++++++++--- tests/unit/test_universe_helpers.py | 16 +++++++++++++++- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/google/api_core/universe_helpers.py b/google/api_core/universe_helpers.py index f4605423..3231272f 100644 --- a/google/api_core/universe_helpers.py +++ b/google/api_core/universe_helpers.py @@ -1,3 +1,19 @@ +# Copyright 2023 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Helpers for universe domain.""" + from google.auth.exceptions import MutualTLSChannelError from typing import Any, Optional @@ -6,7 +22,6 @@ _mTLS_Universe_Error = MutualTLSChannelError( f"mTLS is not supported in any universe other than {_DEFAULT_UNIVERSE}." ) -_Empty_Universe_Error = ValueError("Universe Domain cannot be an empty string.") class _UniverseMismatchError(ValueError): @@ -22,7 +37,7 @@ def __init__(self, client_universe, credentials_universe): def _get_universe_domain( client_universe_domain: Optional[str], universe_domain_env: Optional[str] -) -> str: # temp disable Optional +) -> str: """Return the universe domain used by the client. Args: @@ -41,7 +56,7 @@ def _get_universe_domain( elif universe_domain_env is not None: universe_domain = universe_domain_env if len(universe_domain.strip()) == 0: - raise _Empty_Universe_Error + raise ValueError("Universe Domain cannot be an empty string.") return universe_domain diff --git a/tests/unit/test_universe_helpers.py b/tests/unit/test_universe_helpers.py index ee9213d5..aab50c8e 100644 --- a/tests/unit/test_universe_helpers.py +++ b/tests/unit/test_universe_helpers.py @@ -1,3 +1,17 @@ +# Copyright 2023 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import pytest from google.api_core import universe_helpers from google.auth import credentials @@ -24,7 +38,7 @@ def test__get_universe_domain(): with pytest.raises(ValueError) as excinfo: universe_helpers._get_universe_domain("", None) - assert str(excinfo.value) == str(universe_helpers._Empty_Universe_Error) + assert str(excinfo.value) == "Universe Domain cannot be an empty string." def test__compare_universes(): From cbc679ee34e374233119b12461e6e2fc137ab94a Mon Sep 17 00:00:00 2001 From: ohmayr Date: Tue, 12 Mar 2024 17:45:48 +0000 Subject: [PATCH 10/18] update module name and make methods public --- .../{universe_helpers.py => universe.py} | 22 ++++++++--------- ...t_universe_helpers.py => test_universe.py} | 24 +++++++++---------- 2 files changed, 23 insertions(+), 23 deletions(-) rename google/api_core/{universe_helpers.py => universe.py} (82%) rename tests/unit/{test_universe_helpers.py => test_universe.py} (71%) diff --git a/google/api_core/universe_helpers.py b/google/api_core/universe.py similarity index 82% rename from google/api_core/universe_helpers.py rename to google/api_core/universe.py index 3231272f..a3998932 100644 --- a/google/api_core/universe_helpers.py +++ b/google/api_core/universe.py @@ -1,4 +1,4 @@ -# Copyright 2023 Google LLC +# Copyright 2024 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -17,25 +17,25 @@ from google.auth.exceptions import MutualTLSChannelError from typing import Any, Optional -_DEFAULT_UNIVERSE = "googleapis.com" +DEFAULT_UNIVERSE = "googleapis.com" -_mTLS_Universe_Error = MutualTLSChannelError( - f"mTLS is not supported in any universe other than {_DEFAULT_UNIVERSE}." +mTLS_Universe_Error = MutualTLSChannelError( + f"mTLS is not supported in any universe other than {DEFAULT_UNIVERSE}." ) -class _UniverseMismatchError(ValueError): +class UniverseMismatchError(ValueError): def __init__(self, client_universe, credentials_universe): message = ( f"The configured universe domain ({client_universe}) does not match the universe domain " f"found in the credentials ({credentials_universe}). " "If you haven't configured the universe domain explicitly, " - f"`{_DEFAULT_UNIVERSE}` is the default." + f"`{DEFAULT_UNIVERSE}` is the default." ) super().__init__(message) -def _get_universe_domain( +def determine_domain( client_universe_domain: Optional[str], universe_domain_env: Optional[str] ) -> str: """Return the universe domain used by the client. @@ -50,7 +50,7 @@ def _get_universe_domain( Raises: ValueError: If the universe domain is an empty string. """ - universe_domain = _DEFAULT_UNIVERSE + universe_domain = DEFAULT_UNIVERSE if client_universe_domain is not None: universe_domain = client_universe_domain elif universe_domain_env is not None: @@ -60,7 +60,7 @@ def _get_universe_domain( return universe_domain -def _compare_universes(client_universe: str, credentials: Any) -> bool: +def compare_domains(client_universe: str, credentials: Any) -> bool: """Returns True iff the universe domains used by the client and credentials match. Args: @@ -73,8 +73,8 @@ def _compare_universes(client_universe: str, credentials: Any) -> bool: Raises: ValueError: when client_universe does not match the universe in credentials. """ - credentials_universe = getattr(credentials, "universe_domain", _DEFAULT_UNIVERSE) + credentials_universe = getattr(credentials, "universe_domain", DEFAULT_UNIVERSE) if client_universe != credentials_universe: - raise _UniverseMismatchError(client_universe, credentials_universe) + raise UniverseMismatchError(client_universe, credentials_universe) return True diff --git a/tests/unit/test_universe_helpers.py b/tests/unit/test_universe.py similarity index 71% rename from tests/unit/test_universe_helpers.py rename to tests/unit/test_universe.py index aab50c8e..a381c19b 100644 --- a/tests/unit/test_universe_helpers.py +++ b/tests/unit/test_universe.py @@ -1,4 +1,4 @@ -# Copyright 2023 Google LLC +# Copyright 2024 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,35 +13,35 @@ # limitations under the License. import pytest -from google.api_core import universe_helpers +from google.api_core import universe from google.auth import credentials -def test__get_universe_domain(): +def test_determine_domain(): client_universe_domain = "foo.com" universe_domain_env = "bar.com" assert ( - universe_helpers._get_universe_domain( + universe.determine_domain( client_universe_domain, universe_domain_env ) == client_universe_domain ) assert ( - universe_helpers._get_universe_domain(None, universe_domain_env) + universe.determine_domain(None, universe_domain_env) == universe_domain_env ) assert ( - universe_helpers._get_universe_domain(None, None) - == universe_helpers._DEFAULT_UNIVERSE + universe.determine_domain(None, None) + == universe.DEFAULT_UNIVERSE ) with pytest.raises(ValueError) as excinfo: - universe_helpers._get_universe_domain("", None) + universe.determine_domain("", None) assert str(excinfo.value) == "Universe Domain cannot be an empty string." -def test__compare_universes(): +def test_compare_domains(): ga_credentials = credentials.AnonymousCredentials() mismatch_err_msg = ( "The configured universe domain (foo.com) does not match the universe domain " @@ -51,12 +51,12 @@ def test__compare_universes(): ) assert ( - universe_helpers._compare_universes( - universe_helpers._DEFAULT_UNIVERSE, ga_credentials + universe.compare_domains( + universe.DEFAULT_UNIVERSE, ga_credentials ) is True ) with pytest.raises(ValueError) as excinfo: - universe_helpers._compare_universes("foo.com", ga_credentials) + universe.compare_domains("foo.com", ga_credentials) assert str(excinfo.value) == mismatch_err_msg From 7edf13dcd52f48dc9f677adfbd6117571e0a021b Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Tue, 12 Mar 2024 17:49:40 +0000 Subject: [PATCH 11/18] =?UTF-8?q?=F0=9F=A6=89=20Updates=20from=20OwlBot=20?= =?UTF-8?q?post-processor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --- tests/unit/test_universe.py | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/tests/unit/test_universe.py b/tests/unit/test_universe.py index a381c19b..51587411 100644 --- a/tests/unit/test_universe.py +++ b/tests/unit/test_universe.py @@ -22,19 +22,11 @@ def test_determine_domain(): universe_domain_env = "bar.com" assert ( - universe.determine_domain( - client_universe_domain, universe_domain_env - ) + universe.determine_domain(client_universe_domain, universe_domain_env) == client_universe_domain ) - assert ( - universe.determine_domain(None, universe_domain_env) - == universe_domain_env - ) - assert ( - universe.determine_domain(None, None) - == universe.DEFAULT_UNIVERSE - ) + assert universe.determine_domain(None, universe_domain_env) == universe_domain_env + assert universe.determine_domain(None, None) == universe.DEFAULT_UNIVERSE with pytest.raises(ValueError) as excinfo: universe.determine_domain("", None) @@ -50,12 +42,7 @@ def test_compare_domains(): "`googleapis.com` is the default." ) - assert ( - universe.compare_domains( - universe.DEFAULT_UNIVERSE, ga_credentials - ) - is True - ) + assert universe.compare_domains(universe.DEFAULT_UNIVERSE, ga_credentials) is True with pytest.raises(ValueError) as excinfo: universe.compare_domains("foo.com", ga_credentials) From 812eb3de33c3a57981b53d2c51ae26a43f92df45 Mon Sep 17 00:00:00 2001 From: ohmayr Date: Tue, 12 Mar 2024 17:55:54 +0000 Subject: [PATCH 12/18] update lint issues --- google/api_core/universe.py | 7 ++++--- tests/unit/test_universe.py | 21 ++++----------------- 2 files changed, 8 insertions(+), 20 deletions(-) diff --git a/google/api_core/universe.py b/google/api_core/universe.py index a3998932..77a91ec4 100644 --- a/google/api_core/universe.py +++ b/google/api_core/universe.py @@ -24,7 +24,7 @@ ) -class UniverseMismatchError(ValueError): +class _UniverseMismatchError(ValueError): def __init__(self, client_universe, credentials_universe): message = ( f"The configured universe domain ({client_universe}) does not match the universe domain " @@ -42,7 +42,8 @@ def determine_domain( Args: client_universe_domain (Optional[str]): The universe domain configured via the client options. - universe_domain_env (Optional[str]): The universe domain configured via the "GOOGLE_CLOUD_UNIVERSE_DOMAIN" environment variable. + universe_domain_env (Optional[str]): The universe domain configured via the + "GOOGLE_CLOUD_UNIVERSE_DOMAIN" environment variable. Returns: str: The universe domain to be used by the client. @@ -76,5 +77,5 @@ def compare_domains(client_universe: str, credentials: Any) -> bool: credentials_universe = getattr(credentials, "universe_domain", DEFAULT_UNIVERSE) if client_universe != credentials_universe: - raise UniverseMismatchError(client_universe, credentials_universe) + raise _UniverseMismatchError(client_universe, credentials_universe) return True diff --git a/tests/unit/test_universe.py b/tests/unit/test_universe.py index a381c19b..51587411 100644 --- a/tests/unit/test_universe.py +++ b/tests/unit/test_universe.py @@ -22,19 +22,11 @@ def test_determine_domain(): universe_domain_env = "bar.com" assert ( - universe.determine_domain( - client_universe_domain, universe_domain_env - ) + universe.determine_domain(client_universe_domain, universe_domain_env) == client_universe_domain ) - assert ( - universe.determine_domain(None, universe_domain_env) - == universe_domain_env - ) - assert ( - universe.determine_domain(None, None) - == universe.DEFAULT_UNIVERSE - ) + assert universe.determine_domain(None, universe_domain_env) == universe_domain_env + assert universe.determine_domain(None, None) == universe.DEFAULT_UNIVERSE with pytest.raises(ValueError) as excinfo: universe.determine_domain("", None) @@ -50,12 +42,7 @@ def test_compare_domains(): "`googleapis.com` is the default." ) - assert ( - universe.compare_domains( - universe.DEFAULT_UNIVERSE, ga_credentials - ) - is True - ) + assert universe.compare_domains(universe.DEFAULT_UNIVERSE, ga_credentials) is True with pytest.raises(ValueError) as excinfo: universe.compare_domains("foo.com", ga_credentials) From 8c1dd55559420eefc258db6315cf01bd79564bff Mon Sep 17 00:00:00 2001 From: ohmayr Date: Tue, 12 Mar 2024 22:53:17 +0000 Subject: [PATCH 13/18] address PR comments --- google/api_core/universe.py | 10 ++++++-- tests/unit/test_universe.py | 48 +++++++++++++++++++++---------------- 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/google/api_core/universe.py b/google/api_core/universe.py index 77a91ec4..952d6fb3 100644 --- a/google/api_core/universe.py +++ b/google/api_core/universe.py @@ -24,7 +24,13 @@ ) -class _UniverseMismatchError(ValueError): +class EmptyUniverseError(ValueError): + def __init__(self): + message = "Universe Domain cannot be an empty string." + super().__init__(message) + + +class UniverseMismatchError(ValueError): def __init__(self, client_universe, credentials_universe): message = ( f"The configured universe domain ({client_universe}) does not match the universe domain " @@ -77,5 +83,5 @@ def compare_domains(client_universe: str, credentials: Any) -> bool: credentials_universe = getattr(credentials, "universe_domain", DEFAULT_UNIVERSE) if client_universe != credentials_universe: - raise _UniverseMismatchError(client_universe, credentials_universe) + raise UniverseMismatchError(client_universe, credentials_universe) return True diff --git a/tests/unit/test_universe.py b/tests/unit/test_universe.py index 51587411..cca6fd82 100644 --- a/tests/unit/test_universe.py +++ b/tests/unit/test_universe.py @@ -14,36 +14,44 @@ import pytest from google.api_core import universe -from google.auth import credentials + + +class _Fake_Credentials: + + def __init__(self, universe_domain=None): + if universe_domain: + self.universe_domain = universe_domain def test_determine_domain(): - client_universe_domain = "foo.com" - universe_domain_env = "bar.com" + domain_client = "foo.com" + domain_env = "bar.com" - assert ( - universe.determine_domain(client_universe_domain, universe_domain_env) - == client_universe_domain - ) - assert universe.determine_domain(None, universe_domain_env) == universe_domain_env + assert universe.determine_domain(domain_client, domain_env) == domain_client + assert universe.determine_domain(None, domain_env) == domain_env + assert universe.determine_domain(domain_client, None) == domain_client assert universe.determine_domain(None, None) == universe.DEFAULT_UNIVERSE with pytest.raises(ValueError) as excinfo: universe.determine_domain("", None) assert str(excinfo.value) == "Universe Domain cannot be an empty string." + with pytest.raises(ValueError) as excinfo: + universe.determine_domain(None, "") + assert str(excinfo.value) == "Universe Domain cannot be an empty string." -def test_compare_domains(): - ga_credentials = credentials.AnonymousCredentials() - mismatch_err_msg = ( - "The configured universe domain (foo.com) does not match the universe domain " - "found in the credentials (googleapis.com). " - "If you haven't configured the universe domain explicitly, " - "`googleapis.com` is the default." - ) - assert universe.compare_domains(universe.DEFAULT_UNIVERSE, ga_credentials) is True +def test_compare_domains(): + fake_domain = "foo.com" - with pytest.raises(ValueError) as excinfo: - universe.compare_domains("foo.com", ga_credentials) - assert str(excinfo.value) == mismatch_err_msg + assert ( + universe.compare_domains(universe.DEFAULT_UNIVERSE, _Fake_Credentials()) is True + ) + assert universe.compare_domains(fake_domain, _Fake_Credentials(fake_domain)) is True + + with pytest.raises(universe.UniverseMismatchError) as excinfo: + universe.compare_domains( + universe.DEFAULT_UNIVERSE, _Fake_Credentials(fake_domain) + ) + assert str(excinfo.value).find(universe.DEFAULT_UNIVERSE) >= 0 + assert str(excinfo.value).find(fake_domain) >= 0 From 44f5962b76b2b9863117437313f50fedc50eccfc Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Tue, 12 Mar 2024 22:55:17 +0000 Subject: [PATCH 14/18] =?UTF-8?q?=F0=9F=A6=89=20Updates=20from=20OwlBot=20?= =?UTF-8?q?post-processor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --- tests/unit/test_universe.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/unit/test_universe.py b/tests/unit/test_universe.py index cca6fd82..fc1fb0a8 100644 --- a/tests/unit/test_universe.py +++ b/tests/unit/test_universe.py @@ -17,7 +17,6 @@ class _Fake_Credentials: - def __init__(self, universe_domain=None): if universe_domain: self.universe_domain = universe_domain From 393d2e9d484c17c571f0f18a031880355495ffda Mon Sep 17 00:00:00 2001 From: ohmayr Date: Tue, 12 Mar 2024 23:02:57 +0000 Subject: [PATCH 15/18] use empty universe error in test cases --- google/api_core/universe.py | 2 +- tests/unit/test_universe.py | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/google/api_core/universe.py b/google/api_core/universe.py index 952d6fb3..962f5c1c 100644 --- a/google/api_core/universe.py +++ b/google/api_core/universe.py @@ -63,7 +63,7 @@ def determine_domain( elif universe_domain_env is not None: universe_domain = universe_domain_env if len(universe_domain.strip()) == 0: - raise ValueError("Universe Domain cannot be an empty string.") + raise EmptyUniverseError return universe_domain diff --git a/tests/unit/test_universe.py b/tests/unit/test_universe.py index cca6fd82..813229ba 100644 --- a/tests/unit/test_universe.py +++ b/tests/unit/test_universe.py @@ -32,13 +32,11 @@ def test_determine_domain(): assert universe.determine_domain(domain_client, None) == domain_client assert universe.determine_domain(None, None) == universe.DEFAULT_UNIVERSE - with pytest.raises(ValueError) as excinfo: + with pytest.raises(universe.EmptyUniverseError) as excinfo: universe.determine_domain("", None) - assert str(excinfo.value) == "Universe Domain cannot be an empty string." - with pytest.raises(ValueError) as excinfo: + with pytest.raises(universe.EmptyUniverseError) as excinfo: universe.determine_domain(None, "") - assert str(excinfo.value) == "Universe Domain cannot be an empty string." def test_compare_domains(): From f5bff28d6d2deda1a7316a065e3752cd0c5bed73 Mon Sep 17 00:00:00 2001 From: ohmayr Date: Thu, 14 Mar 2024 18:18:08 +0000 Subject: [PATCH 16/18] remove mtls error and add test cases --- google/api_core/universe.py | 5 ----- tests/unit/test_universe.py | 15 +++++++++++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/google/api_core/universe.py b/google/api_core/universe.py index 962f5c1c..35669642 100644 --- a/google/api_core/universe.py +++ b/google/api_core/universe.py @@ -14,15 +14,10 @@ """Helpers for universe domain.""" -from google.auth.exceptions import MutualTLSChannelError from typing import Any, Optional DEFAULT_UNIVERSE = "googleapis.com" -mTLS_Universe_Error = MutualTLSChannelError( - f"mTLS is not supported in any universe other than {DEFAULT_UNIVERSE}." -) - class EmptyUniverseError(ValueError): def __init__(self): diff --git a/tests/unit/test_universe.py b/tests/unit/test_universe.py index 814c024e..15a0ead7 100644 --- a/tests/unit/test_universe.py +++ b/tests/unit/test_universe.py @@ -40,6 +40,7 @@ def test_determine_domain(): def test_compare_domains(): fake_domain = "foo.com" + another_fake_domain = "bar.com" assert ( universe.compare_domains(universe.DEFAULT_UNIVERSE, _Fake_Credentials()) is True @@ -52,3 +53,17 @@ def test_compare_domains(): ) assert str(excinfo.value).find(universe.DEFAULT_UNIVERSE) >= 0 assert str(excinfo.value).find(fake_domain) >= 0 + + with pytest.raises(universe.UniverseMismatchError) as excinfo: + universe.compare_domains( + fake_domain, _Fake_Credentials() + ) + assert str(excinfo.value).find(fake_domain) >= 0 + assert str(excinfo.value).find(universe.DEFAULT_UNIVERSE) >= 0 + + with pytest.raises(universe.UniverseMismatchError) as excinfo: + universe.compare_domains( + fake_domain, _Fake_Credentials(another_fake_domain) + ) + assert str(excinfo.value).find(fake_domain) >= 0 + assert str(excinfo.value).find(another_fake_domain) >= 0 From 6cac3fdf6d8099f92aca49e360d8fc08f9a484c7 Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Thu, 14 Mar 2024 18:19:56 +0000 Subject: [PATCH 17/18] =?UTF-8?q?=F0=9F=A6=89=20Updates=20from=20OwlBot=20?= =?UTF-8?q?post-processor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --- tests/unit/test_universe.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/tests/unit/test_universe.py b/tests/unit/test_universe.py index 15a0ead7..9268d89f 100644 --- a/tests/unit/test_universe.py +++ b/tests/unit/test_universe.py @@ -55,15 +55,11 @@ def test_compare_domains(): assert str(excinfo.value).find(fake_domain) >= 0 with pytest.raises(universe.UniverseMismatchError) as excinfo: - universe.compare_domains( - fake_domain, _Fake_Credentials() - ) + universe.compare_domains(fake_domain, _Fake_Credentials()) assert str(excinfo.value).find(fake_domain) >= 0 assert str(excinfo.value).find(universe.DEFAULT_UNIVERSE) >= 0 with pytest.raises(universe.UniverseMismatchError) as excinfo: - universe.compare_domains( - fake_domain, _Fake_Credentials(another_fake_domain) - ) + universe.compare_domains(fake_domain, _Fake_Credentials(another_fake_domain)) assert str(excinfo.value).find(fake_domain) >= 0 assert str(excinfo.value).find(another_fake_domain) >= 0 From bc89e13166ed06fede91276b0455ba1d13adfb90 Mon Sep 17 00:00:00 2001 From: ohmayr Date: Thu, 14 Mar 2024 18:50:46 +0000 Subject: [PATCH 18/18] remove is True from test cases --- tests/unit/test_universe.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/unit/test_universe.py b/tests/unit/test_universe.py index 9268d89f..214e00ac 100644 --- a/tests/unit/test_universe.py +++ b/tests/unit/test_universe.py @@ -42,10 +42,8 @@ def test_compare_domains(): fake_domain = "foo.com" another_fake_domain = "bar.com" - assert ( - universe.compare_domains(universe.DEFAULT_UNIVERSE, _Fake_Credentials()) is True - ) - assert universe.compare_domains(fake_domain, _Fake_Credentials(fake_domain)) is True + assert universe.compare_domains(universe.DEFAULT_UNIVERSE, _Fake_Credentials()) + assert universe.compare_domains(fake_domain, _Fake_Credentials(fake_domain)) with pytest.raises(universe.UniverseMismatchError) as excinfo: universe.compare_domains(