From 4ca8ee49d0d49dd09cb2013b59bf11f09de025ff Mon Sep 17 00:00:00 2001 From: Heba Alazzeh Date: Fri, 31 Jul 2026 17:33:26 +0000 Subject: [PATCH 1/6] chore(generator): remove _use_client_cert_effective from gapic-generator --- .../%sub/services/%service/client.py.j2 | 30 +------ .../%name_%version/%sub/test_%service.py.j2 | 84 ------------------- .../asset_v1/services/asset_service/client.py | 30 +------ .../unit/gapic/asset_v1/test_asset_service.py | 84 ------------------- .../services/iam_credentials/client.py | 30 +------ .../credentials_v1/test_iam_credentials.py | 84 ------------------- .../eventarc_v1/services/eventarc/client.py | 30 +------ .../unit/gapic/eventarc_v1/test_eventarc.py | 84 ------------------- .../services/config_service_v2/client.py | 30 +------ .../services/logging_service_v2/client.py | 30 +------ .../services/metrics_service_v2/client.py | 30 +------ .../logging_v2/test_config_service_v2.py | 84 ------------------- .../logging_v2/test_logging_service_v2.py | 84 ------------------- .../logging_v2/test_metrics_service_v2.py | 84 ------------------- .../services/config_service_v2/client.py | 30 +------ .../services/logging_service_v2/client.py | 30 +------ .../services/metrics_service_v2/client.py | 30 +------ .../logging_v2/test_config_service_v2.py | 84 ------------------- .../logging_v2/test_logging_service_v2.py | 84 ------------------- .../logging_v2/test_metrics_service_v2.py | 84 ------------------- .../redis_v1/services/cloud_redis/client.py | 30 +------ .../unit/gapic/redis_v1/test_cloud_redis.py | 84 ------------------- .../redis_v1/services/cloud_redis/client.py | 30 +------ .../unit/gapic/redis_v1/test_cloud_redis.py | 84 ------------------- .../storage_batch_operations/client.py | 30 +------ .../test_storage_batch_operations.py | 84 ------------------- 26 files changed, 26 insertions(+), 1456 deletions(-) diff --git a/packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 b/packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 index 1920328f8f33..8ce5f0193ce5 100644 --- a/packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 +++ b/packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 @@ -156,32 +156,6 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta): _DEFAULT_UNIVERSE = "googleapis.com" - @staticmethod - def _use_client_cert_effective(): - """Returns whether client certificate should be used for mTLS if the - google-auth version supports should_use_client_cert automatic mTLS enablement. - - Alternatively, read from the GOOGLE_API_USE_CLIENT_CERTIFICATE env var. - - Returns: - bool: whether client certificate should be used for mTLS - Raises: - ValueError: (If using a version of google-auth without should_use_client_cert and - GOOGLE_API_USE_CLIENT_CERTIFICATE is set to an unexpected value.) - """ - # check if google-auth version supports should_use_client_cert for automatic mTLS enablement - if hasattr(mtls, "should_use_client_cert"): # pragma: NO COVER - return mtls.should_use_client_cert() - else: # pragma: NO COVER - # if unsupported, fallback to reading from env var - use_client_cert_str = os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false").lower() - if use_client_cert_str not in ("true", "false"): - raise ValueError( - "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be" - " either `true` or `false`" - ) - return use_client_cert_str == "true" - @classmethod def from_service_account_info(cls, info: dict, *args, **kwargs): """Creates an instance of this client using the provided credentials @@ -296,7 +270,7 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta): DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = {{ service.client_name }}._use_client_cert_effective() + use_client_cert = mtls.should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -333,7 +307,7 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = {{ service.client_name }}._use_client_cert_effective() + use_client_cert = mtls.should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 b/packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 index b83c1a0de021..e5c2105946c9 100644 --- a/packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 +++ b/packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 @@ -212,90 +212,6 @@ def test__read_environment_variables(): assert {{ service.client_name }}._read_environment_variables() == (False, "auto", "foo.com") -def test_use_client_cert_effective(): - # Test case 1: Test when `should_use_client_cert` returns True. - # We mock the `should_use_client_cert` function to simulate a scenario where - # the google-auth library supports automatic mTLS and determines that a - # client certificate should be used. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch("google.auth.transport.mtls.should_use_client_cert", return_value=True): - assert {{ service.client_name }}._use_client_cert_effective() is True - - # Test case 2: Test when `should_use_client_cert` returns False. - # We mock the `should_use_client_cert` function to simulate a scenario where - # the google-auth library supports automatic mTLS and determines that a - # client certificate should NOT be used. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch("google.auth.transport.mtls.should_use_client_cert", return_value=False): - assert {{ service.client_name }}._use_client_cert_effective() is False - - # Test case 3: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "true". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): - assert {{ service.client_name }}._use_client_cert_effective() is True - - # Test case 4: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "false". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}): - assert {{ service.client_name }}._use_client_cert_effective() is False - - # Test case 5: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "True". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "True"}): - assert {{ service.client_name }}._use_client_cert_effective() is True - - # Test case 6: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "False". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "False"}): - assert {{ service.client_name }}._use_client_cert_effective() is False - - # Test case 7: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "TRUE". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "TRUE"}): - assert {{ service.client_name }}._use_client_cert_effective() is True - - # Test case 8: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "FALSE". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "FALSE"}): - assert {{ service.client_name }}._use_client_cert_effective() is False - - # Test case 9: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not set. - # In this case, the method should return False, which is the default value. - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, clear=True): - assert {{ service.client_name }}._use_client_cert_effective() is False - - # Test case 10: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to an invalid value. - # The method should raise a ValueError as the environment variable must be either - # "true" or "false". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "unsupported"}): - with pytest.raises(ValueError): - {{ service.client_name }}._use_client_cert_effective() - - # Test case 11: Test when `should_use_client_cert` is available and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to an invalid value. - # The method should return False as the environment variable is set to an invalid value. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "unsupported"}): - assert {{ service.client_name }}._use_client_cert_effective() is False - - # Test case 12: Test when `should_use_client_cert` is available and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is unset. Also, - # the GOOGLE_API_CONFIG environment variable is unset. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": ""}): - with mock.patch.dict(os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": ""}): - assert {{ service.client_name }}._use_client_cert_effective() is False - def test__get_client_cert_source(): mock_provided_cert_source = mock.Mock() mock_default_cert_source = mock.Mock() diff --git a/packages/gapic-generator/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/client.py b/packages/gapic-generator/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/client.py index e96b1bc06fca..45aae2b977ee 100755 --- a/packages/gapic-generator/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/client.py +++ b/packages/gapic-generator/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/client.py @@ -108,32 +108,6 @@ class AssetServiceClient(metaclass=AssetServiceClientMeta): _DEFAULT_ENDPOINT_TEMPLATE = "cloudasset.{UNIVERSE_DOMAIN}" _DEFAULT_UNIVERSE = "googleapis.com" - @staticmethod - def _use_client_cert_effective(): - """Returns whether client certificate should be used for mTLS if the - google-auth version supports should_use_client_cert automatic mTLS enablement. - - Alternatively, read from the GOOGLE_API_USE_CLIENT_CERTIFICATE env var. - - Returns: - bool: whether client certificate should be used for mTLS - Raises: - ValueError: (If using a version of google-auth without should_use_client_cert and - GOOGLE_API_USE_CLIENT_CERTIFICATE is set to an unexpected value.) - """ - # check if google-auth version supports should_use_client_cert for automatic mTLS enablement - if hasattr(mtls, "should_use_client_cert"): # pragma: NO COVER - return mtls.should_use_client_cert() - else: # pragma: NO COVER - # if unsupported, fallback to reading from env var - use_client_cert_str = os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false").lower() - if use_client_cert_str not in ("true", "false"): - raise ValueError( - "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be" - " either `true` or `false`" - ) - return use_client_cert_str == "true" - @classmethod def from_service_account_info(cls, info: dict, *args, **kwargs): """Creates an instance of this client using the provided credentials @@ -351,7 +325,7 @@ def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_optio DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = AssetServiceClient._use_client_cert_effective() + use_client_cert = mtls.should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -388,7 +362,7 @@ def _read_environment_variables(): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = AssetServiceClient._use_client_cert_effective() + use_client_cert = mtls.should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py b/packages/gapic-generator/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py index 52913c13552b..670fa00bbb4c 100755 --- a/packages/gapic-generator/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py +++ b/packages/gapic-generator/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py @@ -176,90 +176,6 @@ def test__read_environment_variables(): assert AssetServiceClient._read_environment_variables() == (False, "auto", "foo.com") -def test_use_client_cert_effective(): - # Test case 1: Test when `should_use_client_cert` returns True. - # We mock the `should_use_client_cert` function to simulate a scenario where - # the google-auth library supports automatic mTLS and determines that a - # client certificate should be used. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch("google.auth.transport.mtls.should_use_client_cert", return_value=True): - assert AssetServiceClient._use_client_cert_effective() is True - - # Test case 2: Test when `should_use_client_cert` returns False. - # We mock the `should_use_client_cert` function to simulate a scenario where - # the google-auth library supports automatic mTLS and determines that a - # client certificate should NOT be used. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch("google.auth.transport.mtls.should_use_client_cert", return_value=False): - assert AssetServiceClient._use_client_cert_effective() is False - - # Test case 3: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "true". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): - assert AssetServiceClient._use_client_cert_effective() is True - - # Test case 4: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "false". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}): - assert AssetServiceClient._use_client_cert_effective() is False - - # Test case 5: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "True". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "True"}): - assert AssetServiceClient._use_client_cert_effective() is True - - # Test case 6: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "False". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "False"}): - assert AssetServiceClient._use_client_cert_effective() is False - - # Test case 7: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "TRUE". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "TRUE"}): - assert AssetServiceClient._use_client_cert_effective() is True - - # Test case 8: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "FALSE". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "FALSE"}): - assert AssetServiceClient._use_client_cert_effective() is False - - # Test case 9: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not set. - # In this case, the method should return False, which is the default value. - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, clear=True): - assert AssetServiceClient._use_client_cert_effective() is False - - # Test case 10: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to an invalid value. - # The method should raise a ValueError as the environment variable must be either - # "true" or "false". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "unsupported"}): - with pytest.raises(ValueError): - AssetServiceClient._use_client_cert_effective() - - # Test case 11: Test when `should_use_client_cert` is available and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to an invalid value. - # The method should return False as the environment variable is set to an invalid value. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "unsupported"}): - assert AssetServiceClient._use_client_cert_effective() is False - - # Test case 12: Test when `should_use_client_cert` is available and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is unset. Also, - # the GOOGLE_API_CONFIG environment variable is unset. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": ""}): - with mock.patch.dict(os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": ""}): - assert AssetServiceClient._use_client_cert_effective() is False - def test__get_client_cert_source(): mock_provided_cert_source = mock.Mock() mock_default_cert_source = mock.Mock() diff --git a/packages/gapic-generator/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/client.py b/packages/gapic-generator/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/client.py index 7b964aa0c9f3..264aa759e48d 100755 --- a/packages/gapic-generator/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/client.py +++ b/packages/gapic-generator/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/client.py @@ -111,32 +111,6 @@ class IAMCredentialsClient(metaclass=IAMCredentialsClientMeta): _DEFAULT_ENDPOINT_TEMPLATE = "iamcredentials.{UNIVERSE_DOMAIN}" _DEFAULT_UNIVERSE = "googleapis.com" - @staticmethod - def _use_client_cert_effective(): - """Returns whether client certificate should be used for mTLS if the - google-auth version supports should_use_client_cert automatic mTLS enablement. - - Alternatively, read from the GOOGLE_API_USE_CLIENT_CERTIFICATE env var. - - Returns: - bool: whether client certificate should be used for mTLS - Raises: - ValueError: (If using a version of google-auth without should_use_client_cert and - GOOGLE_API_USE_CLIENT_CERTIFICATE is set to an unexpected value.) - """ - # check if google-auth version supports should_use_client_cert for automatic mTLS enablement - if hasattr(mtls, "should_use_client_cert"): # pragma: NO COVER - return mtls.should_use_client_cert() - else: # pragma: NO COVER - # if unsupported, fallback to reading from env var - use_client_cert_str = os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false").lower() - if use_client_cert_str not in ("true", "false"): - raise ValueError( - "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be" - " either `true` or `false`" - ) - return use_client_cert_str == "true" - @classmethod def from_service_account_info(cls, info: dict, *args, **kwargs): """Creates an instance of this client using the provided credentials @@ -288,7 +262,7 @@ def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_optio DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = IAMCredentialsClient._use_client_cert_effective() + use_client_cert = mtls.should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -325,7 +299,7 @@ def _read_environment_variables(): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = IAMCredentialsClient._use_client_cert_effective() + use_client_cert = mtls.should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_iam_credentials.py b/packages/gapic-generator/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_iam_credentials.py index 50f68c77ce2d..cec7a8ecdaa3 100755 --- a/packages/gapic-generator/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_iam_credentials.py +++ b/packages/gapic-generator/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_iam_credentials.py @@ -167,90 +167,6 @@ def test__read_environment_variables(): assert IAMCredentialsClient._read_environment_variables() == (False, "auto", "foo.com") -def test_use_client_cert_effective(): - # Test case 1: Test when `should_use_client_cert` returns True. - # We mock the `should_use_client_cert` function to simulate a scenario where - # the google-auth library supports automatic mTLS and determines that a - # client certificate should be used. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch("google.auth.transport.mtls.should_use_client_cert", return_value=True): - assert IAMCredentialsClient._use_client_cert_effective() is True - - # Test case 2: Test when `should_use_client_cert` returns False. - # We mock the `should_use_client_cert` function to simulate a scenario where - # the google-auth library supports automatic mTLS and determines that a - # client certificate should NOT be used. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch("google.auth.transport.mtls.should_use_client_cert", return_value=False): - assert IAMCredentialsClient._use_client_cert_effective() is False - - # Test case 3: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "true". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): - assert IAMCredentialsClient._use_client_cert_effective() is True - - # Test case 4: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "false". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}): - assert IAMCredentialsClient._use_client_cert_effective() is False - - # Test case 5: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "True". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "True"}): - assert IAMCredentialsClient._use_client_cert_effective() is True - - # Test case 6: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "False". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "False"}): - assert IAMCredentialsClient._use_client_cert_effective() is False - - # Test case 7: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "TRUE". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "TRUE"}): - assert IAMCredentialsClient._use_client_cert_effective() is True - - # Test case 8: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "FALSE". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "FALSE"}): - assert IAMCredentialsClient._use_client_cert_effective() is False - - # Test case 9: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not set. - # In this case, the method should return False, which is the default value. - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, clear=True): - assert IAMCredentialsClient._use_client_cert_effective() is False - - # Test case 10: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to an invalid value. - # The method should raise a ValueError as the environment variable must be either - # "true" or "false". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "unsupported"}): - with pytest.raises(ValueError): - IAMCredentialsClient._use_client_cert_effective() - - # Test case 11: Test when `should_use_client_cert` is available and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to an invalid value. - # The method should return False as the environment variable is set to an invalid value. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "unsupported"}): - assert IAMCredentialsClient._use_client_cert_effective() is False - - # Test case 12: Test when `should_use_client_cert` is available and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is unset. Also, - # the GOOGLE_API_CONFIG environment variable is unset. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": ""}): - with mock.patch.dict(os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": ""}): - assert IAMCredentialsClient._use_client_cert_effective() is False - def test__get_client_cert_source(): mock_provided_cert_source = mock.Mock() mock_default_cert_source = mock.Mock() diff --git a/packages/gapic-generator/tests/integration/goldens/eventarc/google/cloud/eventarc_v1/services/eventarc/client.py b/packages/gapic-generator/tests/integration/goldens/eventarc/google/cloud/eventarc_v1/services/eventarc/client.py index b62f1b9c675d..9ffe6c128da3 100755 --- a/packages/gapic-generator/tests/integration/goldens/eventarc/google/cloud/eventarc_v1/services/eventarc/client.py +++ b/packages/gapic-generator/tests/integration/goldens/eventarc/google/cloud/eventarc_v1/services/eventarc/client.py @@ -129,32 +129,6 @@ class EventarcClient(metaclass=EventarcClientMeta): _DEFAULT_ENDPOINT_TEMPLATE = "eventarc.{UNIVERSE_DOMAIN}" _DEFAULT_UNIVERSE = "googleapis.com" - @staticmethod - def _use_client_cert_effective(): - """Returns whether client certificate should be used for mTLS if the - google-auth version supports should_use_client_cert automatic mTLS enablement. - - Alternatively, read from the GOOGLE_API_USE_CLIENT_CERTIFICATE env var. - - Returns: - bool: whether client certificate should be used for mTLS - Raises: - ValueError: (If using a version of google-auth without should_use_client_cert and - GOOGLE_API_USE_CLIENT_CERTIFICATE is set to an unexpected value.) - """ - # check if google-auth version supports should_use_client_cert for automatic mTLS enablement - if hasattr(mtls, "should_use_client_cert"): # pragma: NO COVER - return mtls.should_use_client_cert() - else: # pragma: NO COVER - # if unsupported, fallback to reading from env var - use_client_cert_str = os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false").lower() - if use_client_cert_str not in ("true", "false"): - raise ValueError( - "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be" - " either `true` or `false`" - ) - return use_client_cert_str == "true" - @classmethod def from_service_account_info(cls, info: dict, *args, **kwargs): """Creates an instance of this client using the provided credentials @@ -471,7 +445,7 @@ def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_optio DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = EventarcClient._use_client_cert_effective() + use_client_cert = mtls.should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -508,7 +482,7 @@ def _read_environment_variables(): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = EventarcClient._use_client_cert_effective() + use_client_cert = mtls.should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/tests/integration/goldens/eventarc/tests/unit/gapic/eventarc_v1/test_eventarc.py b/packages/gapic-generator/tests/integration/goldens/eventarc/tests/unit/gapic/eventarc_v1/test_eventarc.py index 277d0880e8cc..4fd019dbcb69 100755 --- a/packages/gapic-generator/tests/integration/goldens/eventarc/tests/unit/gapic/eventarc_v1/test_eventarc.py +++ b/packages/gapic-generator/tests/integration/goldens/eventarc/tests/unit/gapic/eventarc_v1/test_eventarc.py @@ -195,90 +195,6 @@ def test__read_environment_variables(): assert EventarcClient._read_environment_variables() == (False, "auto", "foo.com") -def test_use_client_cert_effective(): - # Test case 1: Test when `should_use_client_cert` returns True. - # We mock the `should_use_client_cert` function to simulate a scenario where - # the google-auth library supports automatic mTLS and determines that a - # client certificate should be used. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch("google.auth.transport.mtls.should_use_client_cert", return_value=True): - assert EventarcClient._use_client_cert_effective() is True - - # Test case 2: Test when `should_use_client_cert` returns False. - # We mock the `should_use_client_cert` function to simulate a scenario where - # the google-auth library supports automatic mTLS and determines that a - # client certificate should NOT be used. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch("google.auth.transport.mtls.should_use_client_cert", return_value=False): - assert EventarcClient._use_client_cert_effective() is False - - # Test case 3: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "true". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): - assert EventarcClient._use_client_cert_effective() is True - - # Test case 4: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "false". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}): - assert EventarcClient._use_client_cert_effective() is False - - # Test case 5: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "True". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "True"}): - assert EventarcClient._use_client_cert_effective() is True - - # Test case 6: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "False". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "False"}): - assert EventarcClient._use_client_cert_effective() is False - - # Test case 7: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "TRUE". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "TRUE"}): - assert EventarcClient._use_client_cert_effective() is True - - # Test case 8: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "FALSE". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "FALSE"}): - assert EventarcClient._use_client_cert_effective() is False - - # Test case 9: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not set. - # In this case, the method should return False, which is the default value. - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, clear=True): - assert EventarcClient._use_client_cert_effective() is False - - # Test case 10: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to an invalid value. - # The method should raise a ValueError as the environment variable must be either - # "true" or "false". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "unsupported"}): - with pytest.raises(ValueError): - EventarcClient._use_client_cert_effective() - - # Test case 11: Test when `should_use_client_cert` is available and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to an invalid value. - # The method should return False as the environment variable is set to an invalid value. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "unsupported"}): - assert EventarcClient._use_client_cert_effective() is False - - # Test case 12: Test when `should_use_client_cert` is available and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is unset. Also, - # the GOOGLE_API_CONFIG environment variable is unset. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": ""}): - with mock.patch.dict(os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": ""}): - assert EventarcClient._use_client_cert_effective() is False - def test__get_client_cert_source(): mock_provided_cert_source = mock.Mock() mock_default_cert_source = mock.Mock() diff --git a/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/client.py b/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/client.py index b559792ef617..6ec7c4dfa747 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/client.py +++ b/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/client.py @@ -104,32 +104,6 @@ class ConfigServiceV2Client(metaclass=ConfigServiceV2ClientMeta): _DEFAULT_ENDPOINT_TEMPLATE = "logging.{UNIVERSE_DOMAIN}" _DEFAULT_UNIVERSE = "googleapis.com" - @staticmethod - def _use_client_cert_effective(): - """Returns whether client certificate should be used for mTLS if the - google-auth version supports should_use_client_cert automatic mTLS enablement. - - Alternatively, read from the GOOGLE_API_USE_CLIENT_CERTIFICATE env var. - - Returns: - bool: whether client certificate should be used for mTLS - Raises: - ValueError: (If using a version of google-auth without should_use_client_cert and - GOOGLE_API_USE_CLIENT_CERTIFICATE is set to an unexpected value.) - """ - # check if google-auth version supports should_use_client_cert for automatic mTLS enablement - if hasattr(mtls, "should_use_client_cert"): # pragma: NO COVER - return mtls.should_use_client_cert() - else: # pragma: NO COVER - # if unsupported, fallback to reading from env var - use_client_cert_str = os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false").lower() - if use_client_cert_str not in ("true", "false"): - raise ValueError( - "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be" - " either `true` or `false`" - ) - return use_client_cert_str == "true" - @classmethod def from_service_account_info(cls, info: dict, *args, **kwargs): """Creates an instance of this client using the provided credentials @@ -347,7 +321,7 @@ def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_optio DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = ConfigServiceV2Client._use_client_cert_effective() + use_client_cert = mtls.should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -384,7 +358,7 @@ def _read_environment_variables(): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = ConfigServiceV2Client._use_client_cert_effective() + use_client_cert = mtls.should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/client.py b/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/client.py index 0c39745755ab..4f4fad32cd29 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/client.py +++ b/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/client.py @@ -101,32 +101,6 @@ class LoggingServiceV2Client(metaclass=LoggingServiceV2ClientMeta): _DEFAULT_ENDPOINT_TEMPLATE = "logging.{UNIVERSE_DOMAIN}" _DEFAULT_UNIVERSE = "googleapis.com" - @staticmethod - def _use_client_cert_effective(): - """Returns whether client certificate should be used for mTLS if the - google-auth version supports should_use_client_cert automatic mTLS enablement. - - Alternatively, read from the GOOGLE_API_USE_CLIENT_CERTIFICATE env var. - - Returns: - bool: whether client certificate should be used for mTLS - Raises: - ValueError: (If using a version of google-auth without should_use_client_cert and - GOOGLE_API_USE_CLIENT_CERTIFICATE is set to an unexpected value.) - """ - # check if google-auth version supports should_use_client_cert for automatic mTLS enablement - if hasattr(mtls, "should_use_client_cert"): # pragma: NO COVER - return mtls.should_use_client_cert() - else: # pragma: NO COVER - # if unsupported, fallback to reading from env var - use_client_cert_str = os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false").lower() - if use_client_cert_str not in ("true", "false"): - raise ValueError( - "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be" - " either `true` or `false`" - ) - return use_client_cert_str == "true" - @classmethod def from_service_account_info(cls, info: dict, *args, **kwargs): """Creates an instance of this client using the provided credentials @@ -278,7 +252,7 @@ def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_optio DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = LoggingServiceV2Client._use_client_cert_effective() + use_client_cert = mtls.should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -315,7 +289,7 @@ def _read_environment_variables(): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = LoggingServiceV2Client._use_client_cert_effective() + use_client_cert = mtls.should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/client.py b/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/client.py index 832d4633a4e4..6d3fd7b4b729 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/client.py +++ b/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/client.py @@ -102,32 +102,6 @@ class MetricsServiceV2Client(metaclass=MetricsServiceV2ClientMeta): _DEFAULT_ENDPOINT_TEMPLATE = "logging.{UNIVERSE_DOMAIN}" _DEFAULT_UNIVERSE = "googleapis.com" - @staticmethod - def _use_client_cert_effective(): - """Returns whether client certificate should be used for mTLS if the - google-auth version supports should_use_client_cert automatic mTLS enablement. - - Alternatively, read from the GOOGLE_API_USE_CLIENT_CERTIFICATE env var. - - Returns: - bool: whether client certificate should be used for mTLS - Raises: - ValueError: (If using a version of google-auth without should_use_client_cert and - GOOGLE_API_USE_CLIENT_CERTIFICATE is set to an unexpected value.) - """ - # check if google-auth version supports should_use_client_cert for automatic mTLS enablement - if hasattr(mtls, "should_use_client_cert"): # pragma: NO COVER - return mtls.should_use_client_cert() - else: # pragma: NO COVER - # if unsupported, fallback to reading from env var - use_client_cert_str = os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false").lower() - if use_client_cert_str not in ("true", "false"): - raise ValueError( - "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be" - " either `true` or `false`" - ) - return use_client_cert_str == "true" - @classmethod def from_service_account_info(cls, info: dict, *args, **kwargs): """Creates an instance of this client using the provided credentials @@ -279,7 +253,7 @@ def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_optio DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = MetricsServiceV2Client._use_client_cert_effective() + use_client_cert = mtls.should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -316,7 +290,7 @@ def _read_environment_variables(): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = MetricsServiceV2Client._use_client_cert_effective() + use_client_cert = mtls.should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_config_service_v2.py b/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_config_service_v2.py index 00171513e8fa..e8dfb610a2a3 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_config_service_v2.py +++ b/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_config_service_v2.py @@ -168,90 +168,6 @@ def test__read_environment_variables(): assert ConfigServiceV2Client._read_environment_variables() == (False, "auto", "foo.com") -def test_use_client_cert_effective(): - # Test case 1: Test when `should_use_client_cert` returns True. - # We mock the `should_use_client_cert` function to simulate a scenario where - # the google-auth library supports automatic mTLS and determines that a - # client certificate should be used. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch("google.auth.transport.mtls.should_use_client_cert", return_value=True): - assert ConfigServiceV2Client._use_client_cert_effective() is True - - # Test case 2: Test when `should_use_client_cert` returns False. - # We mock the `should_use_client_cert` function to simulate a scenario where - # the google-auth library supports automatic mTLS and determines that a - # client certificate should NOT be used. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch("google.auth.transport.mtls.should_use_client_cert", return_value=False): - assert ConfigServiceV2Client._use_client_cert_effective() is False - - # Test case 3: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "true". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): - assert ConfigServiceV2Client._use_client_cert_effective() is True - - # Test case 4: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "false". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}): - assert ConfigServiceV2Client._use_client_cert_effective() is False - - # Test case 5: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "True". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "True"}): - assert ConfigServiceV2Client._use_client_cert_effective() is True - - # Test case 6: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "False". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "False"}): - assert ConfigServiceV2Client._use_client_cert_effective() is False - - # Test case 7: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "TRUE". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "TRUE"}): - assert ConfigServiceV2Client._use_client_cert_effective() is True - - # Test case 8: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "FALSE". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "FALSE"}): - assert ConfigServiceV2Client._use_client_cert_effective() is False - - # Test case 9: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not set. - # In this case, the method should return False, which is the default value. - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, clear=True): - assert ConfigServiceV2Client._use_client_cert_effective() is False - - # Test case 10: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to an invalid value. - # The method should raise a ValueError as the environment variable must be either - # "true" or "false". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "unsupported"}): - with pytest.raises(ValueError): - ConfigServiceV2Client._use_client_cert_effective() - - # Test case 11: Test when `should_use_client_cert` is available and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to an invalid value. - # The method should return False as the environment variable is set to an invalid value. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "unsupported"}): - assert ConfigServiceV2Client._use_client_cert_effective() is False - - # Test case 12: Test when `should_use_client_cert` is available and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is unset. Also, - # the GOOGLE_API_CONFIG environment variable is unset. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": ""}): - with mock.patch.dict(os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": ""}): - assert ConfigServiceV2Client._use_client_cert_effective() is False - def test__get_client_cert_source(): mock_provided_cert_source = mock.Mock() mock_default_cert_source = mock.Mock() diff --git a/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_logging_service_v2.py b/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_logging_service_v2.py index 518a6c2f0b1c..b5a5a5425e4f 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_logging_service_v2.py +++ b/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_logging_service_v2.py @@ -163,90 +163,6 @@ def test__read_environment_variables(): assert LoggingServiceV2Client._read_environment_variables() == (False, "auto", "foo.com") -def test_use_client_cert_effective(): - # Test case 1: Test when `should_use_client_cert` returns True. - # We mock the `should_use_client_cert` function to simulate a scenario where - # the google-auth library supports automatic mTLS and determines that a - # client certificate should be used. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch("google.auth.transport.mtls.should_use_client_cert", return_value=True): - assert LoggingServiceV2Client._use_client_cert_effective() is True - - # Test case 2: Test when `should_use_client_cert` returns False. - # We mock the `should_use_client_cert` function to simulate a scenario where - # the google-auth library supports automatic mTLS and determines that a - # client certificate should NOT be used. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch("google.auth.transport.mtls.should_use_client_cert", return_value=False): - assert LoggingServiceV2Client._use_client_cert_effective() is False - - # Test case 3: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "true". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): - assert LoggingServiceV2Client._use_client_cert_effective() is True - - # Test case 4: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "false". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}): - assert LoggingServiceV2Client._use_client_cert_effective() is False - - # Test case 5: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "True". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "True"}): - assert LoggingServiceV2Client._use_client_cert_effective() is True - - # Test case 6: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "False". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "False"}): - assert LoggingServiceV2Client._use_client_cert_effective() is False - - # Test case 7: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "TRUE". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "TRUE"}): - assert LoggingServiceV2Client._use_client_cert_effective() is True - - # Test case 8: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "FALSE". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "FALSE"}): - assert LoggingServiceV2Client._use_client_cert_effective() is False - - # Test case 9: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not set. - # In this case, the method should return False, which is the default value. - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, clear=True): - assert LoggingServiceV2Client._use_client_cert_effective() is False - - # Test case 10: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to an invalid value. - # The method should raise a ValueError as the environment variable must be either - # "true" or "false". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "unsupported"}): - with pytest.raises(ValueError): - LoggingServiceV2Client._use_client_cert_effective() - - # Test case 11: Test when `should_use_client_cert` is available and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to an invalid value. - # The method should return False as the environment variable is set to an invalid value. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "unsupported"}): - assert LoggingServiceV2Client._use_client_cert_effective() is False - - # Test case 12: Test when `should_use_client_cert` is available and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is unset. Also, - # the GOOGLE_API_CONFIG environment variable is unset. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": ""}): - with mock.patch.dict(os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": ""}): - assert LoggingServiceV2Client._use_client_cert_effective() is False - def test__get_client_cert_source(): mock_provided_cert_source = mock.Mock() mock_default_cert_source = mock.Mock() diff --git a/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_metrics_service_v2.py b/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_metrics_service_v2.py index c8f980d04f43..846a214d4030 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_metrics_service_v2.py +++ b/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_metrics_service_v2.py @@ -164,90 +164,6 @@ def test__read_environment_variables(): assert MetricsServiceV2Client._read_environment_variables() == (False, "auto", "foo.com") -def test_use_client_cert_effective(): - # Test case 1: Test when `should_use_client_cert` returns True. - # We mock the `should_use_client_cert` function to simulate a scenario where - # the google-auth library supports automatic mTLS and determines that a - # client certificate should be used. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch("google.auth.transport.mtls.should_use_client_cert", return_value=True): - assert MetricsServiceV2Client._use_client_cert_effective() is True - - # Test case 2: Test when `should_use_client_cert` returns False. - # We mock the `should_use_client_cert` function to simulate a scenario where - # the google-auth library supports automatic mTLS and determines that a - # client certificate should NOT be used. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch("google.auth.transport.mtls.should_use_client_cert", return_value=False): - assert MetricsServiceV2Client._use_client_cert_effective() is False - - # Test case 3: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "true". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): - assert MetricsServiceV2Client._use_client_cert_effective() is True - - # Test case 4: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "false". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}): - assert MetricsServiceV2Client._use_client_cert_effective() is False - - # Test case 5: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "True". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "True"}): - assert MetricsServiceV2Client._use_client_cert_effective() is True - - # Test case 6: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "False". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "False"}): - assert MetricsServiceV2Client._use_client_cert_effective() is False - - # Test case 7: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "TRUE". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "TRUE"}): - assert MetricsServiceV2Client._use_client_cert_effective() is True - - # Test case 8: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "FALSE". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "FALSE"}): - assert MetricsServiceV2Client._use_client_cert_effective() is False - - # Test case 9: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not set. - # In this case, the method should return False, which is the default value. - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, clear=True): - assert MetricsServiceV2Client._use_client_cert_effective() is False - - # Test case 10: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to an invalid value. - # The method should raise a ValueError as the environment variable must be either - # "true" or "false". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "unsupported"}): - with pytest.raises(ValueError): - MetricsServiceV2Client._use_client_cert_effective() - - # Test case 11: Test when `should_use_client_cert` is available and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to an invalid value. - # The method should return False as the environment variable is set to an invalid value. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "unsupported"}): - assert MetricsServiceV2Client._use_client_cert_effective() is False - - # Test case 12: Test when `should_use_client_cert` is available and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is unset. Also, - # the GOOGLE_API_CONFIG environment variable is unset. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": ""}): - with mock.patch.dict(os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": ""}): - assert MetricsServiceV2Client._use_client_cert_effective() is False - def test__get_client_cert_source(): mock_provided_cert_source = mock.Mock() mock_default_cert_source = mock.Mock() diff --git a/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/services/config_service_v2/client.py b/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/services/config_service_v2/client.py index 5d540a5b4ff7..ae609991095d 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/services/config_service_v2/client.py +++ b/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/services/config_service_v2/client.py @@ -104,32 +104,6 @@ class BaseConfigServiceV2Client(metaclass=BaseConfigServiceV2ClientMeta): _DEFAULT_ENDPOINT_TEMPLATE = "logging.{UNIVERSE_DOMAIN}" _DEFAULT_UNIVERSE = "googleapis.com" - @staticmethod - def _use_client_cert_effective(): - """Returns whether client certificate should be used for mTLS if the - google-auth version supports should_use_client_cert automatic mTLS enablement. - - Alternatively, read from the GOOGLE_API_USE_CLIENT_CERTIFICATE env var. - - Returns: - bool: whether client certificate should be used for mTLS - Raises: - ValueError: (If using a version of google-auth without should_use_client_cert and - GOOGLE_API_USE_CLIENT_CERTIFICATE is set to an unexpected value.) - """ - # check if google-auth version supports should_use_client_cert for automatic mTLS enablement - if hasattr(mtls, "should_use_client_cert"): # pragma: NO COVER - return mtls.should_use_client_cert() - else: # pragma: NO COVER - # if unsupported, fallback to reading from env var - use_client_cert_str = os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false").lower() - if use_client_cert_str not in ("true", "false"): - raise ValueError( - "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be" - " either `true` or `false`" - ) - return use_client_cert_str == "true" - @classmethod def from_service_account_info(cls, info: dict, *args, **kwargs): """Creates an instance of this client using the provided credentials @@ -347,7 +321,7 @@ def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_optio DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = BaseConfigServiceV2Client._use_client_cert_effective() + use_client_cert = mtls.should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -384,7 +358,7 @@ def _read_environment_variables(): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = BaseConfigServiceV2Client._use_client_cert_effective() + use_client_cert = mtls.should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/services/logging_service_v2/client.py b/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/services/logging_service_v2/client.py index 0c39745755ab..4f4fad32cd29 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/services/logging_service_v2/client.py +++ b/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/services/logging_service_v2/client.py @@ -101,32 +101,6 @@ class LoggingServiceV2Client(metaclass=LoggingServiceV2ClientMeta): _DEFAULT_ENDPOINT_TEMPLATE = "logging.{UNIVERSE_DOMAIN}" _DEFAULT_UNIVERSE = "googleapis.com" - @staticmethod - def _use_client_cert_effective(): - """Returns whether client certificate should be used for mTLS if the - google-auth version supports should_use_client_cert automatic mTLS enablement. - - Alternatively, read from the GOOGLE_API_USE_CLIENT_CERTIFICATE env var. - - Returns: - bool: whether client certificate should be used for mTLS - Raises: - ValueError: (If using a version of google-auth without should_use_client_cert and - GOOGLE_API_USE_CLIENT_CERTIFICATE is set to an unexpected value.) - """ - # check if google-auth version supports should_use_client_cert for automatic mTLS enablement - if hasattr(mtls, "should_use_client_cert"): # pragma: NO COVER - return mtls.should_use_client_cert() - else: # pragma: NO COVER - # if unsupported, fallback to reading from env var - use_client_cert_str = os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false").lower() - if use_client_cert_str not in ("true", "false"): - raise ValueError( - "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be" - " either `true` or `false`" - ) - return use_client_cert_str == "true" - @classmethod def from_service_account_info(cls, info: dict, *args, **kwargs): """Creates an instance of this client using the provided credentials @@ -278,7 +252,7 @@ def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_optio DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = LoggingServiceV2Client._use_client_cert_effective() + use_client_cert = mtls.should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -315,7 +289,7 @@ def _read_environment_variables(): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = LoggingServiceV2Client._use_client_cert_effective() + use_client_cert = mtls.should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/services/metrics_service_v2/client.py b/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/services/metrics_service_v2/client.py index 00a69da4f3e8..b6cd4abe2227 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/services/metrics_service_v2/client.py +++ b/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/services/metrics_service_v2/client.py @@ -102,32 +102,6 @@ class BaseMetricsServiceV2Client(metaclass=BaseMetricsServiceV2ClientMeta): _DEFAULT_ENDPOINT_TEMPLATE = "logging.{UNIVERSE_DOMAIN}" _DEFAULT_UNIVERSE = "googleapis.com" - @staticmethod - def _use_client_cert_effective(): - """Returns whether client certificate should be used for mTLS if the - google-auth version supports should_use_client_cert automatic mTLS enablement. - - Alternatively, read from the GOOGLE_API_USE_CLIENT_CERTIFICATE env var. - - Returns: - bool: whether client certificate should be used for mTLS - Raises: - ValueError: (If using a version of google-auth without should_use_client_cert and - GOOGLE_API_USE_CLIENT_CERTIFICATE is set to an unexpected value.) - """ - # check if google-auth version supports should_use_client_cert for automatic mTLS enablement - if hasattr(mtls, "should_use_client_cert"): # pragma: NO COVER - return mtls.should_use_client_cert() - else: # pragma: NO COVER - # if unsupported, fallback to reading from env var - use_client_cert_str = os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false").lower() - if use_client_cert_str not in ("true", "false"): - raise ValueError( - "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be" - " either `true` or `false`" - ) - return use_client_cert_str == "true" - @classmethod def from_service_account_info(cls, info: dict, *args, **kwargs): """Creates an instance of this client using the provided credentials @@ -279,7 +253,7 @@ def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_optio DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = BaseMetricsServiceV2Client._use_client_cert_effective() + use_client_cert = mtls.should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -316,7 +290,7 @@ def _read_environment_variables(): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = BaseMetricsServiceV2Client._use_client_cert_effective() + use_client_cert = mtls.should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_config_service_v2.py b/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_config_service_v2.py index dddeb48d9ad3..0db9ffab1a50 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_config_service_v2.py +++ b/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_config_service_v2.py @@ -168,90 +168,6 @@ def test__read_environment_variables(): assert BaseConfigServiceV2Client._read_environment_variables() == (False, "auto", "foo.com") -def test_use_client_cert_effective(): - # Test case 1: Test when `should_use_client_cert` returns True. - # We mock the `should_use_client_cert` function to simulate a scenario where - # the google-auth library supports automatic mTLS and determines that a - # client certificate should be used. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch("google.auth.transport.mtls.should_use_client_cert", return_value=True): - assert BaseConfigServiceV2Client._use_client_cert_effective() is True - - # Test case 2: Test when `should_use_client_cert` returns False. - # We mock the `should_use_client_cert` function to simulate a scenario where - # the google-auth library supports automatic mTLS and determines that a - # client certificate should NOT be used. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch("google.auth.transport.mtls.should_use_client_cert", return_value=False): - assert BaseConfigServiceV2Client._use_client_cert_effective() is False - - # Test case 3: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "true". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): - assert BaseConfigServiceV2Client._use_client_cert_effective() is True - - # Test case 4: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "false". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}): - assert BaseConfigServiceV2Client._use_client_cert_effective() is False - - # Test case 5: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "True". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "True"}): - assert BaseConfigServiceV2Client._use_client_cert_effective() is True - - # Test case 6: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "False". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "False"}): - assert BaseConfigServiceV2Client._use_client_cert_effective() is False - - # Test case 7: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "TRUE". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "TRUE"}): - assert BaseConfigServiceV2Client._use_client_cert_effective() is True - - # Test case 8: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "FALSE". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "FALSE"}): - assert BaseConfigServiceV2Client._use_client_cert_effective() is False - - # Test case 9: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not set. - # In this case, the method should return False, which is the default value. - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, clear=True): - assert BaseConfigServiceV2Client._use_client_cert_effective() is False - - # Test case 10: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to an invalid value. - # The method should raise a ValueError as the environment variable must be either - # "true" or "false". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "unsupported"}): - with pytest.raises(ValueError): - BaseConfigServiceV2Client._use_client_cert_effective() - - # Test case 11: Test when `should_use_client_cert` is available and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to an invalid value. - # The method should return False as the environment variable is set to an invalid value. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "unsupported"}): - assert BaseConfigServiceV2Client._use_client_cert_effective() is False - - # Test case 12: Test when `should_use_client_cert` is available and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is unset. Also, - # the GOOGLE_API_CONFIG environment variable is unset. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": ""}): - with mock.patch.dict(os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": ""}): - assert BaseConfigServiceV2Client._use_client_cert_effective() is False - def test__get_client_cert_source(): mock_provided_cert_source = mock.Mock() mock_default_cert_source = mock.Mock() diff --git a/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_logging_service_v2.py b/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_logging_service_v2.py index 518a6c2f0b1c..b5a5a5425e4f 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_logging_service_v2.py +++ b/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_logging_service_v2.py @@ -163,90 +163,6 @@ def test__read_environment_variables(): assert LoggingServiceV2Client._read_environment_variables() == (False, "auto", "foo.com") -def test_use_client_cert_effective(): - # Test case 1: Test when `should_use_client_cert` returns True. - # We mock the `should_use_client_cert` function to simulate a scenario where - # the google-auth library supports automatic mTLS and determines that a - # client certificate should be used. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch("google.auth.transport.mtls.should_use_client_cert", return_value=True): - assert LoggingServiceV2Client._use_client_cert_effective() is True - - # Test case 2: Test when `should_use_client_cert` returns False. - # We mock the `should_use_client_cert` function to simulate a scenario where - # the google-auth library supports automatic mTLS and determines that a - # client certificate should NOT be used. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch("google.auth.transport.mtls.should_use_client_cert", return_value=False): - assert LoggingServiceV2Client._use_client_cert_effective() is False - - # Test case 3: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "true". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): - assert LoggingServiceV2Client._use_client_cert_effective() is True - - # Test case 4: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "false". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}): - assert LoggingServiceV2Client._use_client_cert_effective() is False - - # Test case 5: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "True". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "True"}): - assert LoggingServiceV2Client._use_client_cert_effective() is True - - # Test case 6: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "False". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "False"}): - assert LoggingServiceV2Client._use_client_cert_effective() is False - - # Test case 7: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "TRUE". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "TRUE"}): - assert LoggingServiceV2Client._use_client_cert_effective() is True - - # Test case 8: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "FALSE". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "FALSE"}): - assert LoggingServiceV2Client._use_client_cert_effective() is False - - # Test case 9: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not set. - # In this case, the method should return False, which is the default value. - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, clear=True): - assert LoggingServiceV2Client._use_client_cert_effective() is False - - # Test case 10: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to an invalid value. - # The method should raise a ValueError as the environment variable must be either - # "true" or "false". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "unsupported"}): - with pytest.raises(ValueError): - LoggingServiceV2Client._use_client_cert_effective() - - # Test case 11: Test when `should_use_client_cert` is available and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to an invalid value. - # The method should return False as the environment variable is set to an invalid value. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "unsupported"}): - assert LoggingServiceV2Client._use_client_cert_effective() is False - - # Test case 12: Test when `should_use_client_cert` is available and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is unset. Also, - # the GOOGLE_API_CONFIG environment variable is unset. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": ""}): - with mock.patch.dict(os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": ""}): - assert LoggingServiceV2Client._use_client_cert_effective() is False - def test__get_client_cert_source(): mock_provided_cert_source = mock.Mock() mock_default_cert_source = mock.Mock() diff --git a/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_metrics_service_v2.py b/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_metrics_service_v2.py index e5411f74542d..0c1208e48f03 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_metrics_service_v2.py +++ b/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_metrics_service_v2.py @@ -164,90 +164,6 @@ def test__read_environment_variables(): assert BaseMetricsServiceV2Client._read_environment_variables() == (False, "auto", "foo.com") -def test_use_client_cert_effective(): - # Test case 1: Test when `should_use_client_cert` returns True. - # We mock the `should_use_client_cert` function to simulate a scenario where - # the google-auth library supports automatic mTLS and determines that a - # client certificate should be used. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch("google.auth.transport.mtls.should_use_client_cert", return_value=True): - assert BaseMetricsServiceV2Client._use_client_cert_effective() is True - - # Test case 2: Test when `should_use_client_cert` returns False. - # We mock the `should_use_client_cert` function to simulate a scenario where - # the google-auth library supports automatic mTLS and determines that a - # client certificate should NOT be used. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch("google.auth.transport.mtls.should_use_client_cert", return_value=False): - assert BaseMetricsServiceV2Client._use_client_cert_effective() is False - - # Test case 3: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "true". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): - assert BaseMetricsServiceV2Client._use_client_cert_effective() is True - - # Test case 4: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "false". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}): - assert BaseMetricsServiceV2Client._use_client_cert_effective() is False - - # Test case 5: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "True". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "True"}): - assert BaseMetricsServiceV2Client._use_client_cert_effective() is True - - # Test case 6: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "False". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "False"}): - assert BaseMetricsServiceV2Client._use_client_cert_effective() is False - - # Test case 7: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "TRUE". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "TRUE"}): - assert BaseMetricsServiceV2Client._use_client_cert_effective() is True - - # Test case 8: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "FALSE". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "FALSE"}): - assert BaseMetricsServiceV2Client._use_client_cert_effective() is False - - # Test case 9: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not set. - # In this case, the method should return False, which is the default value. - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, clear=True): - assert BaseMetricsServiceV2Client._use_client_cert_effective() is False - - # Test case 10: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to an invalid value. - # The method should raise a ValueError as the environment variable must be either - # "true" or "false". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "unsupported"}): - with pytest.raises(ValueError): - BaseMetricsServiceV2Client._use_client_cert_effective() - - # Test case 11: Test when `should_use_client_cert` is available and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to an invalid value. - # The method should return False as the environment variable is set to an invalid value. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "unsupported"}): - assert BaseMetricsServiceV2Client._use_client_cert_effective() is False - - # Test case 12: Test when `should_use_client_cert` is available and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is unset. Also, - # the GOOGLE_API_CONFIG environment variable is unset. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": ""}): - with mock.patch.dict(os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": ""}): - assert BaseMetricsServiceV2Client._use_client_cert_effective() is False - def test__get_client_cert_source(): mock_provided_cert_source = mock.Mock() mock_default_cert_source = mock.Mock() diff --git a/packages/gapic-generator/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/client.py b/packages/gapic-generator/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/client.py index 786f445f2451..d4ca39f19d5c 100755 --- a/packages/gapic-generator/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/client.py +++ b/packages/gapic-generator/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/client.py @@ -139,32 +139,6 @@ class CloudRedisClient(metaclass=CloudRedisClientMeta): _DEFAULT_ENDPOINT_TEMPLATE = "redis.{UNIVERSE_DOMAIN}" _DEFAULT_UNIVERSE = "googleapis.com" - @staticmethod - def _use_client_cert_effective(): - """Returns whether client certificate should be used for mTLS if the - google-auth version supports should_use_client_cert automatic mTLS enablement. - - Alternatively, read from the GOOGLE_API_USE_CLIENT_CERTIFICATE env var. - - Returns: - bool: whether client certificate should be used for mTLS - Raises: - ValueError: (If using a version of google-auth without should_use_client_cert and - GOOGLE_API_USE_CLIENT_CERTIFICATE is set to an unexpected value.) - """ - # check if google-auth version supports should_use_client_cert for automatic mTLS enablement - if hasattr(mtls, "should_use_client_cert"): # pragma: NO COVER - return mtls.should_use_client_cert() - else: # pragma: NO COVER - # if unsupported, fallback to reading from env var - use_client_cert_str = os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false").lower() - if use_client_cert_str not in ("true", "false"): - raise ValueError( - "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be" - " either `true` or `false`" - ) - return use_client_cert_str == "true" - @classmethod def from_service_account_info(cls, info: dict, *args, **kwargs): """Creates an instance of this client using the provided credentials @@ -316,7 +290,7 @@ def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_optio DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = CloudRedisClient._use_client_cert_effective() + use_client_cert = mtls.should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -353,7 +327,7 @@ def _read_environment_variables(): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = CloudRedisClient._use_client_cert_effective() + use_client_cert = mtls.should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_cloud_redis.py b/packages/gapic-generator/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_cloud_redis.py index 89a2ea389ad4..567824103b37 100755 --- a/packages/gapic-generator/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_cloud_redis.py +++ b/packages/gapic-generator/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_cloud_redis.py @@ -182,90 +182,6 @@ def test__read_environment_variables(): assert CloudRedisClient._read_environment_variables() == (False, "auto", "foo.com") -def test_use_client_cert_effective(): - # Test case 1: Test when `should_use_client_cert` returns True. - # We mock the `should_use_client_cert` function to simulate a scenario where - # the google-auth library supports automatic mTLS and determines that a - # client certificate should be used. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch("google.auth.transport.mtls.should_use_client_cert", return_value=True): - assert CloudRedisClient._use_client_cert_effective() is True - - # Test case 2: Test when `should_use_client_cert` returns False. - # We mock the `should_use_client_cert` function to simulate a scenario where - # the google-auth library supports automatic mTLS and determines that a - # client certificate should NOT be used. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch("google.auth.transport.mtls.should_use_client_cert", return_value=False): - assert CloudRedisClient._use_client_cert_effective() is False - - # Test case 3: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "true". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): - assert CloudRedisClient._use_client_cert_effective() is True - - # Test case 4: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "false". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}): - assert CloudRedisClient._use_client_cert_effective() is False - - # Test case 5: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "True". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "True"}): - assert CloudRedisClient._use_client_cert_effective() is True - - # Test case 6: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "False". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "False"}): - assert CloudRedisClient._use_client_cert_effective() is False - - # Test case 7: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "TRUE". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "TRUE"}): - assert CloudRedisClient._use_client_cert_effective() is True - - # Test case 8: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "FALSE". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "FALSE"}): - assert CloudRedisClient._use_client_cert_effective() is False - - # Test case 9: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not set. - # In this case, the method should return False, which is the default value. - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, clear=True): - assert CloudRedisClient._use_client_cert_effective() is False - - # Test case 10: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to an invalid value. - # The method should raise a ValueError as the environment variable must be either - # "true" or "false". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "unsupported"}): - with pytest.raises(ValueError): - CloudRedisClient._use_client_cert_effective() - - # Test case 11: Test when `should_use_client_cert` is available and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to an invalid value. - # The method should return False as the environment variable is set to an invalid value. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "unsupported"}): - assert CloudRedisClient._use_client_cert_effective() is False - - # Test case 12: Test when `should_use_client_cert` is available and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is unset. Also, - # the GOOGLE_API_CONFIG environment variable is unset. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": ""}): - with mock.patch.dict(os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": ""}): - assert CloudRedisClient._use_client_cert_effective() is False - def test__get_client_cert_source(): mock_provided_cert_source = mock.Mock() mock_default_cert_source = mock.Mock() diff --git a/packages/gapic-generator/tests/integration/goldens/redis_selective/google/cloud/redis_v1/services/cloud_redis/client.py b/packages/gapic-generator/tests/integration/goldens/redis_selective/google/cloud/redis_v1/services/cloud_redis/client.py index 31475bca738f..de455739e52f 100755 --- a/packages/gapic-generator/tests/integration/goldens/redis_selective/google/cloud/redis_v1/services/cloud_redis/client.py +++ b/packages/gapic-generator/tests/integration/goldens/redis_selective/google/cloud/redis_v1/services/cloud_redis/client.py @@ -139,32 +139,6 @@ class CloudRedisClient(metaclass=CloudRedisClientMeta): _DEFAULT_ENDPOINT_TEMPLATE = "redis.{UNIVERSE_DOMAIN}" _DEFAULT_UNIVERSE = "googleapis.com" - @staticmethod - def _use_client_cert_effective(): - """Returns whether client certificate should be used for mTLS if the - google-auth version supports should_use_client_cert automatic mTLS enablement. - - Alternatively, read from the GOOGLE_API_USE_CLIENT_CERTIFICATE env var. - - Returns: - bool: whether client certificate should be used for mTLS - Raises: - ValueError: (If using a version of google-auth without should_use_client_cert and - GOOGLE_API_USE_CLIENT_CERTIFICATE is set to an unexpected value.) - """ - # check if google-auth version supports should_use_client_cert for automatic mTLS enablement - if hasattr(mtls, "should_use_client_cert"): # pragma: NO COVER - return mtls.should_use_client_cert() - else: # pragma: NO COVER - # if unsupported, fallback to reading from env var - use_client_cert_str = os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false").lower() - if use_client_cert_str not in ("true", "false"): - raise ValueError( - "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be" - " either `true` or `false`" - ) - return use_client_cert_str == "true" - @classmethod def from_service_account_info(cls, info: dict, *args, **kwargs): """Creates an instance of this client using the provided credentials @@ -316,7 +290,7 @@ def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_optio DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = CloudRedisClient._use_client_cert_effective() + use_client_cert = mtls.should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -353,7 +327,7 @@ def _read_environment_variables(): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = CloudRedisClient._use_client_cert_effective() + use_client_cert = mtls.should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/tests/integration/goldens/redis_selective/tests/unit/gapic/redis_v1/test_cloud_redis.py b/packages/gapic-generator/tests/integration/goldens/redis_selective/tests/unit/gapic/redis_v1/test_cloud_redis.py index 94a7b49589be..a86f631a7b1b 100755 --- a/packages/gapic-generator/tests/integration/goldens/redis_selective/tests/unit/gapic/redis_v1/test_cloud_redis.py +++ b/packages/gapic-generator/tests/integration/goldens/redis_selective/tests/unit/gapic/redis_v1/test_cloud_redis.py @@ -182,90 +182,6 @@ def test__read_environment_variables(): assert CloudRedisClient._read_environment_variables() == (False, "auto", "foo.com") -def test_use_client_cert_effective(): - # Test case 1: Test when `should_use_client_cert` returns True. - # We mock the `should_use_client_cert` function to simulate a scenario where - # the google-auth library supports automatic mTLS and determines that a - # client certificate should be used. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch("google.auth.transport.mtls.should_use_client_cert", return_value=True): - assert CloudRedisClient._use_client_cert_effective() is True - - # Test case 2: Test when `should_use_client_cert` returns False. - # We mock the `should_use_client_cert` function to simulate a scenario where - # the google-auth library supports automatic mTLS and determines that a - # client certificate should NOT be used. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch("google.auth.transport.mtls.should_use_client_cert", return_value=False): - assert CloudRedisClient._use_client_cert_effective() is False - - # Test case 3: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "true". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): - assert CloudRedisClient._use_client_cert_effective() is True - - # Test case 4: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "false". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}): - assert CloudRedisClient._use_client_cert_effective() is False - - # Test case 5: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "True". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "True"}): - assert CloudRedisClient._use_client_cert_effective() is True - - # Test case 6: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "False". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "False"}): - assert CloudRedisClient._use_client_cert_effective() is False - - # Test case 7: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "TRUE". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "TRUE"}): - assert CloudRedisClient._use_client_cert_effective() is True - - # Test case 8: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "FALSE". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "FALSE"}): - assert CloudRedisClient._use_client_cert_effective() is False - - # Test case 9: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not set. - # In this case, the method should return False, which is the default value. - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, clear=True): - assert CloudRedisClient._use_client_cert_effective() is False - - # Test case 10: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to an invalid value. - # The method should raise a ValueError as the environment variable must be either - # "true" or "false". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "unsupported"}): - with pytest.raises(ValueError): - CloudRedisClient._use_client_cert_effective() - - # Test case 11: Test when `should_use_client_cert` is available and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to an invalid value. - # The method should return False as the environment variable is set to an invalid value. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "unsupported"}): - assert CloudRedisClient._use_client_cert_effective() is False - - # Test case 12: Test when `should_use_client_cert` is available and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is unset. Also, - # the GOOGLE_API_CONFIG environment variable is unset. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": ""}): - with mock.patch.dict(os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": ""}): - assert CloudRedisClient._use_client_cert_effective() is False - def test__get_client_cert_source(): mock_provided_cert_source = mock.Mock() mock_default_cert_source = mock.Mock() diff --git a/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/services/storage_batch_operations/client.py b/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/services/storage_batch_operations/client.py index 4e647f0fdd96..c747d45f5d7d 100755 --- a/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/services/storage_batch_operations/client.py +++ b/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/services/storage_batch_operations/client.py @@ -113,32 +113,6 @@ class StorageBatchOperationsClient(metaclass=StorageBatchOperationsClientMeta): _DEFAULT_ENDPOINT_TEMPLATE = "storagebatchoperations.{UNIVERSE_DOMAIN}" _DEFAULT_UNIVERSE = "googleapis.com" - @staticmethod - def _use_client_cert_effective(): - """Returns whether client certificate should be used for mTLS if the - google-auth version supports should_use_client_cert automatic mTLS enablement. - - Alternatively, read from the GOOGLE_API_USE_CLIENT_CERTIFICATE env var. - - Returns: - bool: whether client certificate should be used for mTLS - Raises: - ValueError: (If using a version of google-auth without should_use_client_cert and - GOOGLE_API_USE_CLIENT_CERTIFICATE is set to an unexpected value.) - """ - # check if google-auth version supports should_use_client_cert for automatic mTLS enablement - if hasattr(mtls, "should_use_client_cert"): # pragma: NO COVER - return mtls.should_use_client_cert() - else: # pragma: NO COVER - # if unsupported, fallback to reading from env var - use_client_cert_str = os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false").lower() - if use_client_cert_str not in ("true", "false"): - raise ValueError( - "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be" - " either `true` or `false`" - ) - return use_client_cert_str == "true" - @classmethod def from_service_account_info(cls, info: dict, *args, **kwargs): """Creates an instance of this client using the provided credentials @@ -312,7 +286,7 @@ def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_optio DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = StorageBatchOperationsClient._use_client_cert_effective() + use_client_cert = mtls.should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -349,7 +323,7 @@ def _read_environment_variables(): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = StorageBatchOperationsClient._use_client_cert_effective() + use_client_cert = mtls.should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/tests/unit/gapic/storagebatchoperations_v1/test_storage_batch_operations.py b/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/tests/unit/gapic/storagebatchoperations_v1/test_storage_batch_operations.py index 33f696a4a132..ddc655f0c207 100755 --- a/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/tests/unit/gapic/storagebatchoperations_v1/test_storage_batch_operations.py +++ b/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/tests/unit/gapic/storagebatchoperations_v1/test_storage_batch_operations.py @@ -176,90 +176,6 @@ def test__read_environment_variables(): assert StorageBatchOperationsClient._read_environment_variables() == (False, "auto", "foo.com") -def test_use_client_cert_effective(): - # Test case 1: Test when `should_use_client_cert` returns True. - # We mock the `should_use_client_cert` function to simulate a scenario where - # the google-auth library supports automatic mTLS and determines that a - # client certificate should be used. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch("google.auth.transport.mtls.should_use_client_cert", return_value=True): - assert StorageBatchOperationsClient._use_client_cert_effective() is True - - # Test case 2: Test when `should_use_client_cert` returns False. - # We mock the `should_use_client_cert` function to simulate a scenario where - # the google-auth library supports automatic mTLS and determines that a - # client certificate should NOT be used. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch("google.auth.transport.mtls.should_use_client_cert", return_value=False): - assert StorageBatchOperationsClient._use_client_cert_effective() is False - - # Test case 3: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "true". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): - assert StorageBatchOperationsClient._use_client_cert_effective() is True - - # Test case 4: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "false". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}): - assert StorageBatchOperationsClient._use_client_cert_effective() is False - - # Test case 5: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "True". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "True"}): - assert StorageBatchOperationsClient._use_client_cert_effective() is True - - # Test case 6: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "False". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "False"}): - assert StorageBatchOperationsClient._use_client_cert_effective() is False - - # Test case 7: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "TRUE". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "TRUE"}): - assert StorageBatchOperationsClient._use_client_cert_effective() is True - - # Test case 8: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "FALSE". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "FALSE"}): - assert StorageBatchOperationsClient._use_client_cert_effective() is False - - # Test case 9: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not set. - # In this case, the method should return False, which is the default value. - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, clear=True): - assert StorageBatchOperationsClient._use_client_cert_effective() is False - - # Test case 10: Test when `should_use_client_cert` is unavailable and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to an invalid value. - # The method should raise a ValueError as the environment variable must be either - # "true" or "false". - if not hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "unsupported"}): - with pytest.raises(ValueError): - StorageBatchOperationsClient._use_client_cert_effective() - - # Test case 11: Test when `should_use_client_cert` is available and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to an invalid value. - # The method should return False as the environment variable is set to an invalid value. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "unsupported"}): - assert StorageBatchOperationsClient._use_client_cert_effective() is False - - # Test case 12: Test when `should_use_client_cert` is available and the - # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is unset. Also, - # the GOOGLE_API_CONFIG environment variable is unset. - if hasattr(google.auth.transport.mtls, "should_use_client_cert"): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": ""}): - with mock.patch.dict(os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": ""}): - assert StorageBatchOperationsClient._use_client_cert_effective() is False - def test__get_client_cert_source(): mock_provided_cert_source = mock.Mock() mock_default_cert_source = mock.Mock() From 74ec086a3a81572e3116046e3e9d1968b75c1bd1 Mon Sep 17 00:00:00 2001 From: Heba Alazzeh Date: Fri, 31 Jul 2026 18:19:07 +0000 Subject: [PATCH 2/6] update goldens --- .../%name_%version/%sub/_compat.py.j2 | 18 +++ .../%sub/services/%service/client.py.j2 | 6 +- .../%name_%version/%sub/test_compat.py.j2 | 106 ++++++++++++++++++ .../asset/google/cloud/asset_v1/_compat.py | 18 +++ .../asset_v1/services/asset_service/client.py | 6 +- .../tests/unit/gapic/asset_v1/test_compat.py | 106 ++++++++++++++++++ .../google/iam/credentials_v1/_compat.py | 18 +++ .../services/iam_credentials/client.py | 6 +- .../unit/gapic/credentials_v1/test_compat.py | 106 ++++++++++++++++++ .../google/cloud/eventarc_v1/_compat.py | 18 +++ .../eventarc_v1/services/eventarc/client.py | 6 +- .../unit/gapic/eventarc_v1/test_compat.py | 106 ++++++++++++++++++ .../google/cloud/logging_v2/_compat.py | 18 +++ .../services/config_service_v2/client.py | 6 +- .../services/logging_service_v2/client.py | 6 +- .../services/metrics_service_v2/client.py | 6 +- .../unit/gapic/logging_v2/test_compat.py | 106 ++++++++++++++++++ .../google/cloud/logging_v2/_compat.py | 18 +++ .../services/config_service_v2/client.py | 6 +- .../services/logging_service_v2/client.py | 6 +- .../services/metrics_service_v2/client.py | 6 +- .../unit/gapic/logging_v2/test_compat.py | 106 ++++++++++++++++++ .../redis/google/cloud/redis_v1/_compat.py | 18 +++ .../redis_v1/services/cloud_redis/client.py | 6 +- .../tests/unit/gapic/redis_v1/test_compat.py | 106 ++++++++++++++++++ .../google/cloud/redis_v1/_compat.py | 18 +++ .../redis_v1/services/cloud_redis/client.py | 6 +- .../tests/unit/gapic/redis_v1/test_compat.py | 106 ++++++++++++++++++ .../storagebatchoperations_v1/_compat.py | 18 +++ .../storage_batch_operations/client.py | 6 +- .../storagebatchoperations_v1/test_compat.py | 106 ++++++++++++++++++ 31 files changed, 1155 insertions(+), 39 deletions(-) diff --git a/packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/_compat.py.j2 b/packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/_compat.py.j2 index f9fa334ef284..a6164ad17637 100644 --- a/packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/_compat.py.j2 +++ b/packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/_compat.py.j2 @@ -13,10 +13,13 @@ falling back to the local implementation if not present. #} {# TODO(https://github.com/googleapis/google-cloud-python/issues/17883): Backfill compatibility functions being removed from the client layer. #} +import os + from typing import Optional from urllib.parse import urlparse, urlunparse from google.auth.exceptions import MutualTLSChannelError +from google.auth.transport import mtls from google.api_core.universe import EmptyUniverseError {% if has_auto_populated_fields %} @@ -142,6 +145,21 @@ def get_universe_domain( raise EmptyUniverseError() return resolved +def use_client_cert_effective() -> bool: + """Returns whether client certificate should be used for mTLS.""" + if hasattr(mtls, "should_use_client_cert"): + return mtls.should_use_client_cert() + else: + use_client_cert_str = os.getenv( + "GOOGLE_API_USE_CLIENT_CERTIFICATE", "false" + ).lower() + if use_client_cert_str not in ("true", "false"): + raise ValueError( + "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be" + " either `true` or `false`" + ) + return use_client_cert_str == "true" + {% if has_auto_populated_fields %} def setup_request_id( diff --git a/packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 b/packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 index 8ce5f0193ce5..b76f12308b19 100644 --- a/packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 +++ b/packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 @@ -30,7 +30,7 @@ from google.api_core import exceptions as core_exceptions from google.api_core import extended_operation {% endif %} from google.api_core import gapic_v1 -from {{package_path}}._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint +from {{package_path}}._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, use_client_cert_effective {% if has_auto_populated_fields %} from {{package_path}}._compat import setup_request_id {% endif %} @@ -270,7 +270,7 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta): DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = mtls.should_use_client_cert() + use_client_cert = use_client_cert_effective() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -307,7 +307,7 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = mtls.should_use_client_cert() + use_client_cert = use_client_cert_effective() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_compat.py.j2 b/packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_compat.py.j2 index aa45d0656c85..d1f28af6c4d3 100644 --- a/packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_compat.py.j2 +++ b/packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_compat.py.j2 @@ -12,6 +12,7 @@ Backfill compatibility functions tests being removed from the client layer. #} import re import pytest +import os {% set package_path = api.naming.module_namespace|join('.') + "." + api.naming.versioned_module_name %} from {{package_path}} import _compat as universe @@ -20,6 +21,7 @@ from {{package_path}}._compat import setup_request_id {% endif %} from google.auth.exceptions import MutualTLSChannelError +from unittest import mock def test_get_universe_domain(): @@ -218,6 +220,110 @@ def test_get_api_endpoint( ) +def test_use_client_cert_effective_true(): + mock_mtls = mock.Mock(spec=["should_use_client_cert"]) + mock_mtls.should_use_client_cert.return_value = True + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + assert config.use_client_cert_effective() is True + + +def test_use_client_cert_effective_false(): + mock_mtls = mock.Mock(spec=["should_use_client_cert"]) + mock_mtls.should_use_client_cert.return_value = False + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + assert config.use_client_cert_effective() is False + + +def test_use_client_cert_effective_fallback_env_true(): + mock_mtls = mock.Mock(spec=[]) + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): + assert config.use_client_cert_effective() is True + + +def test_use_client_cert_effective_fallback_env_false(): + mock_mtls = mock.Mock(spec=[]) + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"} + ): + assert config.use_client_cert_effective() is False + + +def test_use_client_cert_effective_fallback_env_invalid(): + mock_mtls = mock.Mock(spec=[]) + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"} + ): + with pytest.raises( + ValueError, + match="Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`", + ): + config.use_client_cert_effective() + + +def test_get_client_cert_source_provided(): + source = mock.Mock() + assert config.get_client_cert_source(source, True) == source + + +def test_get_client_cert_source_default(): + mock_mtls = mock.Mock( + spec=["has_default_client_cert_source", "default_client_cert_source"] + ) + mock_mtls.has_default_client_cert_source.return_value = True + mock_source = mock.Mock() + mock_mtls.default_client_cert_source.return_value = mock_source + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + assert config.get_client_cert_source(None, True) == mock_source + + +def test_get_client_cert_source_none(): + mock_mtls = mock.Mock( + spec=["has_default_client_cert_source", "default_client_cert_source"] + ) + mock_mtls.has_default_client_cert_source.return_value = False + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with pytest.raises( + ValueError, + match="Client certificate is required for mTLS, but no client certificate source was provided or found.", + ): + config.get_client_cert_source(None, True) + + +def test_get_client_cert_source_use_cert_flag_false(): + assert config.get_client_cert_source(None, False) is None + source = mock.Mock() + assert config.get_client_cert_source(source, False) is None + + +def test_read_environment_variables(): + with mock.patch( + "google.api_core.gapic_v1.config.use_client_cert_effective", return_value=True + ): + with mock.patch.dict( + os.environ, + { + "GOOGLE_API_USE_MTLS_ENDPOINT": "always", + "GOOGLE_CLOUD_UNIVERSE_DOMAIN": "my-universe.com", + }, + ): + use_cert, use_mtls, universe = config.read_environment_variables() + assert use_cert is True + assert use_mtls == "always" + assert universe == "my-universe.com" + + +def test_read_environment_variables_invalid_mtls(): + with mock.patch( + "google.api_core.gapic_v1.config.use_client_cert_effective", return_value=True + ): + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "invalid"}): + with pytest.raises(MutualTLSChannelError): + config.read_environment_variables() + + {% if has_auto_populated_fields %} class MockRequest: diff --git a/packages/gapic-generator/tests/integration/goldens/asset/google/cloud/asset_v1/_compat.py b/packages/gapic-generator/tests/integration/goldens/asset/google/cloud/asset_v1/_compat.py index cb118c8f3409..cce4dc784242 100755 --- a/packages/gapic-generator/tests/integration/goldens/asset/google/cloud/asset_v1/_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/asset/google/cloud/asset_v1/_compat.py @@ -15,10 +15,13 @@ # """A compatibility module for older versions of google-api-core.""" +import os + from typing import Optional from urllib.parse import urlparse, urlunparse from google.auth.exceptions import MutualTLSChannelError +from google.auth.transport import mtls from google.api_core.universe import EmptyUniverseError @@ -138,3 +141,18 @@ def get_universe_domain( if not resolved: raise EmptyUniverseError() return resolved + +def use_client_cert_effective() -> bool: + """Returns whether client certificate should be used for mTLS.""" + if hasattr(mtls, "should_use_client_cert"): + return mtls.should_use_client_cert() + else: + use_client_cert_str = os.getenv( + "GOOGLE_API_USE_CLIENT_CERTIFICATE", "false" + ).lower() + if use_client_cert_str not in ("true", "false"): + raise ValueError( + "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be" + " either `true` or `false`" + ) + return use_client_cert_str == "true" diff --git a/packages/gapic-generator/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/client.py b/packages/gapic-generator/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/client.py index 45aae2b977ee..9bb016e3744d 100755 --- a/packages/gapic-generator/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/client.py +++ b/packages/gapic-generator/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/client.py @@ -27,7 +27,7 @@ from google.api_core import client_options as client_options_lib from google.api_core import exceptions as core_exceptions from google.api_core import gapic_v1 -from google.cloud.asset_v1._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint +from google.cloud.asset_v1._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, use_client_cert_effective from google.api_core import retry as retries from google.auth import credentials as ga_credentials # type: ignore from google.auth.transport import mtls # type: ignore @@ -325,7 +325,7 @@ def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_optio DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = mtls.should_use_client_cert() + use_client_cert = use_client_cert_effective() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -362,7 +362,7 @@ def _read_environment_variables(): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = mtls.should_use_client_cert() + use_client_cert = use_client_cert_effective() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_compat.py b/packages/gapic-generator/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_compat.py index 17fbdd221973..131814f4f753 100755 --- a/packages/gapic-generator/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_compat.py @@ -17,10 +17,12 @@ import re import pytest +import os from google.cloud.asset_v1 import _compat as universe from google.auth.exceptions import MutualTLSChannelError +from unittest import mock def test_get_universe_domain(): @@ -217,3 +219,107 @@ def test_get_api_endpoint( ) == expected ) + + +def test_use_client_cert_effective_true(): + mock_mtls = mock.Mock(spec=["should_use_client_cert"]) + mock_mtls.should_use_client_cert.return_value = True + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + assert config.use_client_cert_effective() is True + + +def test_use_client_cert_effective_false(): + mock_mtls = mock.Mock(spec=["should_use_client_cert"]) + mock_mtls.should_use_client_cert.return_value = False + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + assert config.use_client_cert_effective() is False + + +def test_use_client_cert_effective_fallback_env_true(): + mock_mtls = mock.Mock(spec=[]) + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): + assert config.use_client_cert_effective() is True + + +def test_use_client_cert_effective_fallback_env_false(): + mock_mtls = mock.Mock(spec=[]) + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"} + ): + assert config.use_client_cert_effective() is False + + +def test_use_client_cert_effective_fallback_env_invalid(): + mock_mtls = mock.Mock(spec=[]) + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"} + ): + with pytest.raises( + ValueError, + match="Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`", + ): + config.use_client_cert_effective() + + +def test_get_client_cert_source_provided(): + source = mock.Mock() + assert config.get_client_cert_source(source, True) == source + + +def test_get_client_cert_source_default(): + mock_mtls = mock.Mock( + spec=["has_default_client_cert_source", "default_client_cert_source"] + ) + mock_mtls.has_default_client_cert_source.return_value = True + mock_source = mock.Mock() + mock_mtls.default_client_cert_source.return_value = mock_source + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + assert config.get_client_cert_source(None, True) == mock_source + + +def test_get_client_cert_source_none(): + mock_mtls = mock.Mock( + spec=["has_default_client_cert_source", "default_client_cert_source"] + ) + mock_mtls.has_default_client_cert_source.return_value = False + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with pytest.raises( + ValueError, + match="Client certificate is required for mTLS, but no client certificate source was provided or found.", + ): + config.get_client_cert_source(None, True) + + +def test_get_client_cert_source_use_cert_flag_false(): + assert config.get_client_cert_source(None, False) is None + source = mock.Mock() + assert config.get_client_cert_source(source, False) is None + + +def test_read_environment_variables(): + with mock.patch( + "google.api_core.gapic_v1.config.use_client_cert_effective", return_value=True + ): + with mock.patch.dict( + os.environ, + { + "GOOGLE_API_USE_MTLS_ENDPOINT": "always", + "GOOGLE_CLOUD_UNIVERSE_DOMAIN": "my-universe.com", + }, + ): + use_cert, use_mtls, universe = config.read_environment_variables() + assert use_cert is True + assert use_mtls == "always" + assert universe == "my-universe.com" + + +def test_read_environment_variables_invalid_mtls(): + with mock.patch( + "google.api_core.gapic_v1.config.use_client_cert_effective", return_value=True + ): + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "invalid"}): + with pytest.raises(MutualTLSChannelError): + config.read_environment_variables() diff --git a/packages/gapic-generator/tests/integration/goldens/credentials/google/iam/credentials_v1/_compat.py b/packages/gapic-generator/tests/integration/goldens/credentials/google/iam/credentials_v1/_compat.py index cb118c8f3409..cce4dc784242 100755 --- a/packages/gapic-generator/tests/integration/goldens/credentials/google/iam/credentials_v1/_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/credentials/google/iam/credentials_v1/_compat.py @@ -15,10 +15,13 @@ # """A compatibility module for older versions of google-api-core.""" +import os + from typing import Optional from urllib.parse import urlparse, urlunparse from google.auth.exceptions import MutualTLSChannelError +from google.auth.transport import mtls from google.api_core.universe import EmptyUniverseError @@ -138,3 +141,18 @@ def get_universe_domain( if not resolved: raise EmptyUniverseError() return resolved + +def use_client_cert_effective() -> bool: + """Returns whether client certificate should be used for mTLS.""" + if hasattr(mtls, "should_use_client_cert"): + return mtls.should_use_client_cert() + else: + use_client_cert_str = os.getenv( + "GOOGLE_API_USE_CLIENT_CERTIFICATE", "false" + ).lower() + if use_client_cert_str not in ("true", "false"): + raise ValueError( + "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be" + " either `true` or `false`" + ) + return use_client_cert_str == "true" diff --git a/packages/gapic-generator/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/client.py b/packages/gapic-generator/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/client.py index 264aa759e48d..813eab83813d 100755 --- a/packages/gapic-generator/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/client.py +++ b/packages/gapic-generator/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/client.py @@ -27,7 +27,7 @@ from google.api_core import client_options as client_options_lib from google.api_core import exceptions as core_exceptions from google.api_core import gapic_v1 -from google.iam.credentials_v1._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint +from google.iam.credentials_v1._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, use_client_cert_effective from google.api_core import retry as retries from google.auth import credentials as ga_credentials # type: ignore from google.auth.transport import mtls # type: ignore @@ -262,7 +262,7 @@ def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_optio DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = mtls.should_use_client_cert() + use_client_cert = use_client_cert_effective() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -299,7 +299,7 @@ def _read_environment_variables(): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = mtls.should_use_client_cert() + use_client_cert = use_client_cert_effective() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_compat.py b/packages/gapic-generator/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_compat.py index 05cb91c594eb..80bca06e578a 100755 --- a/packages/gapic-generator/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_compat.py @@ -17,10 +17,12 @@ import re import pytest +import os from google.iam.credentials_v1 import _compat as universe from google.auth.exceptions import MutualTLSChannelError +from unittest import mock def test_get_universe_domain(): @@ -217,3 +219,107 @@ def test_get_api_endpoint( ) == expected ) + + +def test_use_client_cert_effective_true(): + mock_mtls = mock.Mock(spec=["should_use_client_cert"]) + mock_mtls.should_use_client_cert.return_value = True + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + assert config.use_client_cert_effective() is True + + +def test_use_client_cert_effective_false(): + mock_mtls = mock.Mock(spec=["should_use_client_cert"]) + mock_mtls.should_use_client_cert.return_value = False + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + assert config.use_client_cert_effective() is False + + +def test_use_client_cert_effective_fallback_env_true(): + mock_mtls = mock.Mock(spec=[]) + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): + assert config.use_client_cert_effective() is True + + +def test_use_client_cert_effective_fallback_env_false(): + mock_mtls = mock.Mock(spec=[]) + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"} + ): + assert config.use_client_cert_effective() is False + + +def test_use_client_cert_effective_fallback_env_invalid(): + mock_mtls = mock.Mock(spec=[]) + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"} + ): + with pytest.raises( + ValueError, + match="Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`", + ): + config.use_client_cert_effective() + + +def test_get_client_cert_source_provided(): + source = mock.Mock() + assert config.get_client_cert_source(source, True) == source + + +def test_get_client_cert_source_default(): + mock_mtls = mock.Mock( + spec=["has_default_client_cert_source", "default_client_cert_source"] + ) + mock_mtls.has_default_client_cert_source.return_value = True + mock_source = mock.Mock() + mock_mtls.default_client_cert_source.return_value = mock_source + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + assert config.get_client_cert_source(None, True) == mock_source + + +def test_get_client_cert_source_none(): + mock_mtls = mock.Mock( + spec=["has_default_client_cert_source", "default_client_cert_source"] + ) + mock_mtls.has_default_client_cert_source.return_value = False + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with pytest.raises( + ValueError, + match="Client certificate is required for mTLS, but no client certificate source was provided or found.", + ): + config.get_client_cert_source(None, True) + + +def test_get_client_cert_source_use_cert_flag_false(): + assert config.get_client_cert_source(None, False) is None + source = mock.Mock() + assert config.get_client_cert_source(source, False) is None + + +def test_read_environment_variables(): + with mock.patch( + "google.api_core.gapic_v1.config.use_client_cert_effective", return_value=True + ): + with mock.patch.dict( + os.environ, + { + "GOOGLE_API_USE_MTLS_ENDPOINT": "always", + "GOOGLE_CLOUD_UNIVERSE_DOMAIN": "my-universe.com", + }, + ): + use_cert, use_mtls, universe = config.read_environment_variables() + assert use_cert is True + assert use_mtls == "always" + assert universe == "my-universe.com" + + +def test_read_environment_variables_invalid_mtls(): + with mock.patch( + "google.api_core.gapic_v1.config.use_client_cert_effective", return_value=True + ): + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "invalid"}): + with pytest.raises(MutualTLSChannelError): + config.read_environment_variables() diff --git a/packages/gapic-generator/tests/integration/goldens/eventarc/google/cloud/eventarc_v1/_compat.py b/packages/gapic-generator/tests/integration/goldens/eventarc/google/cloud/eventarc_v1/_compat.py index cb118c8f3409..cce4dc784242 100755 --- a/packages/gapic-generator/tests/integration/goldens/eventarc/google/cloud/eventarc_v1/_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/eventarc/google/cloud/eventarc_v1/_compat.py @@ -15,10 +15,13 @@ # """A compatibility module for older versions of google-api-core.""" +import os + from typing import Optional from urllib.parse import urlparse, urlunparse from google.auth.exceptions import MutualTLSChannelError +from google.auth.transport import mtls from google.api_core.universe import EmptyUniverseError @@ -138,3 +141,18 @@ def get_universe_domain( if not resolved: raise EmptyUniverseError() return resolved + +def use_client_cert_effective() -> bool: + """Returns whether client certificate should be used for mTLS.""" + if hasattr(mtls, "should_use_client_cert"): + return mtls.should_use_client_cert() + else: + use_client_cert_str = os.getenv( + "GOOGLE_API_USE_CLIENT_CERTIFICATE", "false" + ).lower() + if use_client_cert_str not in ("true", "false"): + raise ValueError( + "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be" + " either `true` or `false`" + ) + return use_client_cert_str == "true" diff --git a/packages/gapic-generator/tests/integration/goldens/eventarc/google/cloud/eventarc_v1/services/eventarc/client.py b/packages/gapic-generator/tests/integration/goldens/eventarc/google/cloud/eventarc_v1/services/eventarc/client.py index 9ffe6c128da3..75a06e71b67e 100755 --- a/packages/gapic-generator/tests/integration/goldens/eventarc/google/cloud/eventarc_v1/services/eventarc/client.py +++ b/packages/gapic-generator/tests/integration/goldens/eventarc/google/cloud/eventarc_v1/services/eventarc/client.py @@ -27,7 +27,7 @@ from google.api_core import client_options as client_options_lib from google.api_core import exceptions as core_exceptions from google.api_core import gapic_v1 -from google.cloud.eventarc_v1._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint +from google.cloud.eventarc_v1._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, use_client_cert_effective from google.api_core import retry as retries from google.auth import credentials as ga_credentials # type: ignore from google.auth.transport import mtls # type: ignore @@ -445,7 +445,7 @@ def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_optio DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = mtls.should_use_client_cert() + use_client_cert = use_client_cert_effective() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -482,7 +482,7 @@ def _read_environment_variables(): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = mtls.should_use_client_cert() + use_client_cert = use_client_cert_effective() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/tests/integration/goldens/eventarc/tests/unit/gapic/eventarc_v1/test_compat.py b/packages/gapic-generator/tests/integration/goldens/eventarc/tests/unit/gapic/eventarc_v1/test_compat.py index c6ba5691b2b3..0811b1e712cf 100755 --- a/packages/gapic-generator/tests/integration/goldens/eventarc/tests/unit/gapic/eventarc_v1/test_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/eventarc/tests/unit/gapic/eventarc_v1/test_compat.py @@ -17,10 +17,12 @@ import re import pytest +import os from google.cloud.eventarc_v1 import _compat as universe from google.auth.exceptions import MutualTLSChannelError +from unittest import mock def test_get_universe_domain(): @@ -217,3 +219,107 @@ def test_get_api_endpoint( ) == expected ) + + +def test_use_client_cert_effective_true(): + mock_mtls = mock.Mock(spec=["should_use_client_cert"]) + mock_mtls.should_use_client_cert.return_value = True + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + assert config.use_client_cert_effective() is True + + +def test_use_client_cert_effective_false(): + mock_mtls = mock.Mock(spec=["should_use_client_cert"]) + mock_mtls.should_use_client_cert.return_value = False + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + assert config.use_client_cert_effective() is False + + +def test_use_client_cert_effective_fallback_env_true(): + mock_mtls = mock.Mock(spec=[]) + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): + assert config.use_client_cert_effective() is True + + +def test_use_client_cert_effective_fallback_env_false(): + mock_mtls = mock.Mock(spec=[]) + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"} + ): + assert config.use_client_cert_effective() is False + + +def test_use_client_cert_effective_fallback_env_invalid(): + mock_mtls = mock.Mock(spec=[]) + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"} + ): + with pytest.raises( + ValueError, + match="Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`", + ): + config.use_client_cert_effective() + + +def test_get_client_cert_source_provided(): + source = mock.Mock() + assert config.get_client_cert_source(source, True) == source + + +def test_get_client_cert_source_default(): + mock_mtls = mock.Mock( + spec=["has_default_client_cert_source", "default_client_cert_source"] + ) + mock_mtls.has_default_client_cert_source.return_value = True + mock_source = mock.Mock() + mock_mtls.default_client_cert_source.return_value = mock_source + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + assert config.get_client_cert_source(None, True) == mock_source + + +def test_get_client_cert_source_none(): + mock_mtls = mock.Mock( + spec=["has_default_client_cert_source", "default_client_cert_source"] + ) + mock_mtls.has_default_client_cert_source.return_value = False + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with pytest.raises( + ValueError, + match="Client certificate is required for mTLS, but no client certificate source was provided or found.", + ): + config.get_client_cert_source(None, True) + + +def test_get_client_cert_source_use_cert_flag_false(): + assert config.get_client_cert_source(None, False) is None + source = mock.Mock() + assert config.get_client_cert_source(source, False) is None + + +def test_read_environment_variables(): + with mock.patch( + "google.api_core.gapic_v1.config.use_client_cert_effective", return_value=True + ): + with mock.patch.dict( + os.environ, + { + "GOOGLE_API_USE_MTLS_ENDPOINT": "always", + "GOOGLE_CLOUD_UNIVERSE_DOMAIN": "my-universe.com", + }, + ): + use_cert, use_mtls, universe = config.read_environment_variables() + assert use_cert is True + assert use_mtls == "always" + assert universe == "my-universe.com" + + +def test_read_environment_variables_invalid_mtls(): + with mock.patch( + "google.api_core.gapic_v1.config.use_client_cert_effective", return_value=True + ): + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "invalid"}): + with pytest.raises(MutualTLSChannelError): + config.read_environment_variables() diff --git a/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/_compat.py b/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/_compat.py index cb118c8f3409..cce4dc784242 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/_compat.py @@ -15,10 +15,13 @@ # """A compatibility module for older versions of google-api-core.""" +import os + from typing import Optional from urllib.parse import urlparse, urlunparse from google.auth.exceptions import MutualTLSChannelError +from google.auth.transport import mtls from google.api_core.universe import EmptyUniverseError @@ -138,3 +141,18 @@ def get_universe_domain( if not resolved: raise EmptyUniverseError() return resolved + +def use_client_cert_effective() -> bool: + """Returns whether client certificate should be used for mTLS.""" + if hasattr(mtls, "should_use_client_cert"): + return mtls.should_use_client_cert() + else: + use_client_cert_str = os.getenv( + "GOOGLE_API_USE_CLIENT_CERTIFICATE", "false" + ).lower() + if use_client_cert_str not in ("true", "false"): + raise ValueError( + "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be" + " either `true` or `false`" + ) + return use_client_cert_str == "true" diff --git a/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/client.py b/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/client.py index 6ec7c4dfa747..2240f5bff786 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/client.py +++ b/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/client.py @@ -27,7 +27,7 @@ from google.api_core import client_options as client_options_lib from google.api_core import exceptions as core_exceptions from google.api_core import gapic_v1 -from google.cloud.logging_v2._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint +from google.cloud.logging_v2._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, use_client_cert_effective from google.api_core import retry as retries from google.auth import credentials as ga_credentials # type: ignore from google.auth.transport import mtls # type: ignore @@ -321,7 +321,7 @@ def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_optio DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = mtls.should_use_client_cert() + use_client_cert = use_client_cert_effective() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -358,7 +358,7 @@ def _read_environment_variables(): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = mtls.should_use_client_cert() + use_client_cert = use_client_cert_effective() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/client.py b/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/client.py index 4f4fad32cd29..65fc8435e83c 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/client.py +++ b/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/client.py @@ -27,7 +27,7 @@ from google.api_core import client_options as client_options_lib from google.api_core import exceptions as core_exceptions from google.api_core import gapic_v1 -from google.cloud.logging_v2._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint +from google.cloud.logging_v2._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, use_client_cert_effective from google.api_core import retry as retries from google.auth import credentials as ga_credentials # type: ignore from google.auth.transport import mtls # type: ignore @@ -252,7 +252,7 @@ def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_optio DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = mtls.should_use_client_cert() + use_client_cert = use_client_cert_effective() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -289,7 +289,7 @@ def _read_environment_variables(): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = mtls.should_use_client_cert() + use_client_cert = use_client_cert_effective() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/client.py b/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/client.py index 6d3fd7b4b729..417c223f1cc0 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/client.py +++ b/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/client.py @@ -27,7 +27,7 @@ from google.api_core import client_options as client_options_lib from google.api_core import exceptions as core_exceptions from google.api_core import gapic_v1 -from google.cloud.logging_v2._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint +from google.cloud.logging_v2._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, use_client_cert_effective from google.api_core import retry as retries from google.auth import credentials as ga_credentials # type: ignore from google.auth.transport import mtls # type: ignore @@ -253,7 +253,7 @@ def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_optio DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = mtls.should_use_client_cert() + use_client_cert = use_client_cert_effective() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -290,7 +290,7 @@ def _read_environment_variables(): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = mtls.should_use_client_cert() + use_client_cert = use_client_cert_effective() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_compat.py b/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_compat.py index 4d2f8c2a01b2..49118be5d01b 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_compat.py @@ -17,10 +17,12 @@ import re import pytest +import os from google.cloud.logging_v2 import _compat as universe from google.auth.exceptions import MutualTLSChannelError +from unittest import mock def test_get_universe_domain(): @@ -217,3 +219,107 @@ def test_get_api_endpoint( ) == expected ) + + +def test_use_client_cert_effective_true(): + mock_mtls = mock.Mock(spec=["should_use_client_cert"]) + mock_mtls.should_use_client_cert.return_value = True + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + assert config.use_client_cert_effective() is True + + +def test_use_client_cert_effective_false(): + mock_mtls = mock.Mock(spec=["should_use_client_cert"]) + mock_mtls.should_use_client_cert.return_value = False + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + assert config.use_client_cert_effective() is False + + +def test_use_client_cert_effective_fallback_env_true(): + mock_mtls = mock.Mock(spec=[]) + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): + assert config.use_client_cert_effective() is True + + +def test_use_client_cert_effective_fallback_env_false(): + mock_mtls = mock.Mock(spec=[]) + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"} + ): + assert config.use_client_cert_effective() is False + + +def test_use_client_cert_effective_fallback_env_invalid(): + mock_mtls = mock.Mock(spec=[]) + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"} + ): + with pytest.raises( + ValueError, + match="Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`", + ): + config.use_client_cert_effective() + + +def test_get_client_cert_source_provided(): + source = mock.Mock() + assert config.get_client_cert_source(source, True) == source + + +def test_get_client_cert_source_default(): + mock_mtls = mock.Mock( + spec=["has_default_client_cert_source", "default_client_cert_source"] + ) + mock_mtls.has_default_client_cert_source.return_value = True + mock_source = mock.Mock() + mock_mtls.default_client_cert_source.return_value = mock_source + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + assert config.get_client_cert_source(None, True) == mock_source + + +def test_get_client_cert_source_none(): + mock_mtls = mock.Mock( + spec=["has_default_client_cert_source", "default_client_cert_source"] + ) + mock_mtls.has_default_client_cert_source.return_value = False + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with pytest.raises( + ValueError, + match="Client certificate is required for mTLS, but no client certificate source was provided or found.", + ): + config.get_client_cert_source(None, True) + + +def test_get_client_cert_source_use_cert_flag_false(): + assert config.get_client_cert_source(None, False) is None + source = mock.Mock() + assert config.get_client_cert_source(source, False) is None + + +def test_read_environment_variables(): + with mock.patch( + "google.api_core.gapic_v1.config.use_client_cert_effective", return_value=True + ): + with mock.patch.dict( + os.environ, + { + "GOOGLE_API_USE_MTLS_ENDPOINT": "always", + "GOOGLE_CLOUD_UNIVERSE_DOMAIN": "my-universe.com", + }, + ): + use_cert, use_mtls, universe = config.read_environment_variables() + assert use_cert is True + assert use_mtls == "always" + assert universe == "my-universe.com" + + +def test_read_environment_variables_invalid_mtls(): + with mock.patch( + "google.api_core.gapic_v1.config.use_client_cert_effective", return_value=True + ): + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "invalid"}): + with pytest.raises(MutualTLSChannelError): + config.read_environment_variables() diff --git a/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/_compat.py b/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/_compat.py index cb118c8f3409..cce4dc784242 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/_compat.py @@ -15,10 +15,13 @@ # """A compatibility module for older versions of google-api-core.""" +import os + from typing import Optional from urllib.parse import urlparse, urlunparse from google.auth.exceptions import MutualTLSChannelError +from google.auth.transport import mtls from google.api_core.universe import EmptyUniverseError @@ -138,3 +141,18 @@ def get_universe_domain( if not resolved: raise EmptyUniverseError() return resolved + +def use_client_cert_effective() -> bool: + """Returns whether client certificate should be used for mTLS.""" + if hasattr(mtls, "should_use_client_cert"): + return mtls.should_use_client_cert() + else: + use_client_cert_str = os.getenv( + "GOOGLE_API_USE_CLIENT_CERTIFICATE", "false" + ).lower() + if use_client_cert_str not in ("true", "false"): + raise ValueError( + "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be" + " either `true` or `false`" + ) + return use_client_cert_str == "true" diff --git a/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/services/config_service_v2/client.py b/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/services/config_service_v2/client.py index ae609991095d..57523bbf10cb 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/services/config_service_v2/client.py +++ b/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/services/config_service_v2/client.py @@ -27,7 +27,7 @@ from google.api_core import client_options as client_options_lib from google.api_core import exceptions as core_exceptions from google.api_core import gapic_v1 -from google.cloud.logging_v2._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint +from google.cloud.logging_v2._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, use_client_cert_effective from google.api_core import retry as retries from google.auth import credentials as ga_credentials # type: ignore from google.auth.transport import mtls # type: ignore @@ -321,7 +321,7 @@ def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_optio DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = mtls.should_use_client_cert() + use_client_cert = use_client_cert_effective() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -358,7 +358,7 @@ def _read_environment_variables(): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = mtls.should_use_client_cert() + use_client_cert = use_client_cert_effective() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/services/logging_service_v2/client.py b/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/services/logging_service_v2/client.py index 4f4fad32cd29..65fc8435e83c 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/services/logging_service_v2/client.py +++ b/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/services/logging_service_v2/client.py @@ -27,7 +27,7 @@ from google.api_core import client_options as client_options_lib from google.api_core import exceptions as core_exceptions from google.api_core import gapic_v1 -from google.cloud.logging_v2._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint +from google.cloud.logging_v2._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, use_client_cert_effective from google.api_core import retry as retries from google.auth import credentials as ga_credentials # type: ignore from google.auth.transport import mtls # type: ignore @@ -252,7 +252,7 @@ def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_optio DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = mtls.should_use_client_cert() + use_client_cert = use_client_cert_effective() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -289,7 +289,7 @@ def _read_environment_variables(): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = mtls.should_use_client_cert() + use_client_cert = use_client_cert_effective() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/services/metrics_service_v2/client.py b/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/services/metrics_service_v2/client.py index b6cd4abe2227..7e6ed1e87a54 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/services/metrics_service_v2/client.py +++ b/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/services/metrics_service_v2/client.py @@ -27,7 +27,7 @@ from google.api_core import client_options as client_options_lib from google.api_core import exceptions as core_exceptions from google.api_core import gapic_v1 -from google.cloud.logging_v2._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint +from google.cloud.logging_v2._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, use_client_cert_effective from google.api_core import retry as retries from google.auth import credentials as ga_credentials # type: ignore from google.auth.transport import mtls # type: ignore @@ -253,7 +253,7 @@ def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_optio DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = mtls.should_use_client_cert() + use_client_cert = use_client_cert_effective() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -290,7 +290,7 @@ def _read_environment_variables(): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = mtls.should_use_client_cert() + use_client_cert = use_client_cert_effective() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_compat.py b/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_compat.py index 4d2f8c2a01b2..49118be5d01b 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_compat.py @@ -17,10 +17,12 @@ import re import pytest +import os from google.cloud.logging_v2 import _compat as universe from google.auth.exceptions import MutualTLSChannelError +from unittest import mock def test_get_universe_domain(): @@ -217,3 +219,107 @@ def test_get_api_endpoint( ) == expected ) + + +def test_use_client_cert_effective_true(): + mock_mtls = mock.Mock(spec=["should_use_client_cert"]) + mock_mtls.should_use_client_cert.return_value = True + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + assert config.use_client_cert_effective() is True + + +def test_use_client_cert_effective_false(): + mock_mtls = mock.Mock(spec=["should_use_client_cert"]) + mock_mtls.should_use_client_cert.return_value = False + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + assert config.use_client_cert_effective() is False + + +def test_use_client_cert_effective_fallback_env_true(): + mock_mtls = mock.Mock(spec=[]) + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): + assert config.use_client_cert_effective() is True + + +def test_use_client_cert_effective_fallback_env_false(): + mock_mtls = mock.Mock(spec=[]) + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"} + ): + assert config.use_client_cert_effective() is False + + +def test_use_client_cert_effective_fallback_env_invalid(): + mock_mtls = mock.Mock(spec=[]) + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"} + ): + with pytest.raises( + ValueError, + match="Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`", + ): + config.use_client_cert_effective() + + +def test_get_client_cert_source_provided(): + source = mock.Mock() + assert config.get_client_cert_source(source, True) == source + + +def test_get_client_cert_source_default(): + mock_mtls = mock.Mock( + spec=["has_default_client_cert_source", "default_client_cert_source"] + ) + mock_mtls.has_default_client_cert_source.return_value = True + mock_source = mock.Mock() + mock_mtls.default_client_cert_source.return_value = mock_source + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + assert config.get_client_cert_source(None, True) == mock_source + + +def test_get_client_cert_source_none(): + mock_mtls = mock.Mock( + spec=["has_default_client_cert_source", "default_client_cert_source"] + ) + mock_mtls.has_default_client_cert_source.return_value = False + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with pytest.raises( + ValueError, + match="Client certificate is required for mTLS, but no client certificate source was provided or found.", + ): + config.get_client_cert_source(None, True) + + +def test_get_client_cert_source_use_cert_flag_false(): + assert config.get_client_cert_source(None, False) is None + source = mock.Mock() + assert config.get_client_cert_source(source, False) is None + + +def test_read_environment_variables(): + with mock.patch( + "google.api_core.gapic_v1.config.use_client_cert_effective", return_value=True + ): + with mock.patch.dict( + os.environ, + { + "GOOGLE_API_USE_MTLS_ENDPOINT": "always", + "GOOGLE_CLOUD_UNIVERSE_DOMAIN": "my-universe.com", + }, + ): + use_cert, use_mtls, universe = config.read_environment_variables() + assert use_cert is True + assert use_mtls == "always" + assert universe == "my-universe.com" + + +def test_read_environment_variables_invalid_mtls(): + with mock.patch( + "google.api_core.gapic_v1.config.use_client_cert_effective", return_value=True + ): + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "invalid"}): + with pytest.raises(MutualTLSChannelError): + config.read_environment_variables() diff --git a/packages/gapic-generator/tests/integration/goldens/redis/google/cloud/redis_v1/_compat.py b/packages/gapic-generator/tests/integration/goldens/redis/google/cloud/redis_v1/_compat.py index cb118c8f3409..cce4dc784242 100755 --- a/packages/gapic-generator/tests/integration/goldens/redis/google/cloud/redis_v1/_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/redis/google/cloud/redis_v1/_compat.py @@ -15,10 +15,13 @@ # """A compatibility module for older versions of google-api-core.""" +import os + from typing import Optional from urllib.parse import urlparse, urlunparse from google.auth.exceptions import MutualTLSChannelError +from google.auth.transport import mtls from google.api_core.universe import EmptyUniverseError @@ -138,3 +141,18 @@ def get_universe_domain( if not resolved: raise EmptyUniverseError() return resolved + +def use_client_cert_effective() -> bool: + """Returns whether client certificate should be used for mTLS.""" + if hasattr(mtls, "should_use_client_cert"): + return mtls.should_use_client_cert() + else: + use_client_cert_str = os.getenv( + "GOOGLE_API_USE_CLIENT_CERTIFICATE", "false" + ).lower() + if use_client_cert_str not in ("true", "false"): + raise ValueError( + "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be" + " either `true` or `false`" + ) + return use_client_cert_str == "true" diff --git a/packages/gapic-generator/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/client.py b/packages/gapic-generator/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/client.py index d4ca39f19d5c..c243e2c2eec5 100755 --- a/packages/gapic-generator/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/client.py +++ b/packages/gapic-generator/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/client.py @@ -27,7 +27,7 @@ from google.api_core import client_options as client_options_lib from google.api_core import exceptions as core_exceptions from google.api_core import gapic_v1 -from google.cloud.redis_v1._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint +from google.cloud.redis_v1._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, use_client_cert_effective from google.api_core import retry as retries from google.auth import credentials as ga_credentials # type: ignore from google.auth.transport import mtls # type: ignore @@ -290,7 +290,7 @@ def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_optio DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = mtls.should_use_client_cert() + use_client_cert = use_client_cert_effective() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -327,7 +327,7 @@ def _read_environment_variables(): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = mtls.should_use_client_cert() + use_client_cert = use_client_cert_effective() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_compat.py b/packages/gapic-generator/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_compat.py index 5caa250d85a1..a52eb1f9cc03 100755 --- a/packages/gapic-generator/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_compat.py @@ -17,10 +17,12 @@ import re import pytest +import os from google.cloud.redis_v1 import _compat as universe from google.auth.exceptions import MutualTLSChannelError +from unittest import mock def test_get_universe_domain(): @@ -217,3 +219,107 @@ def test_get_api_endpoint( ) == expected ) + + +def test_use_client_cert_effective_true(): + mock_mtls = mock.Mock(spec=["should_use_client_cert"]) + mock_mtls.should_use_client_cert.return_value = True + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + assert config.use_client_cert_effective() is True + + +def test_use_client_cert_effective_false(): + mock_mtls = mock.Mock(spec=["should_use_client_cert"]) + mock_mtls.should_use_client_cert.return_value = False + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + assert config.use_client_cert_effective() is False + + +def test_use_client_cert_effective_fallback_env_true(): + mock_mtls = mock.Mock(spec=[]) + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): + assert config.use_client_cert_effective() is True + + +def test_use_client_cert_effective_fallback_env_false(): + mock_mtls = mock.Mock(spec=[]) + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"} + ): + assert config.use_client_cert_effective() is False + + +def test_use_client_cert_effective_fallback_env_invalid(): + mock_mtls = mock.Mock(spec=[]) + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"} + ): + with pytest.raises( + ValueError, + match="Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`", + ): + config.use_client_cert_effective() + + +def test_get_client_cert_source_provided(): + source = mock.Mock() + assert config.get_client_cert_source(source, True) == source + + +def test_get_client_cert_source_default(): + mock_mtls = mock.Mock( + spec=["has_default_client_cert_source", "default_client_cert_source"] + ) + mock_mtls.has_default_client_cert_source.return_value = True + mock_source = mock.Mock() + mock_mtls.default_client_cert_source.return_value = mock_source + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + assert config.get_client_cert_source(None, True) == mock_source + + +def test_get_client_cert_source_none(): + mock_mtls = mock.Mock( + spec=["has_default_client_cert_source", "default_client_cert_source"] + ) + mock_mtls.has_default_client_cert_source.return_value = False + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with pytest.raises( + ValueError, + match="Client certificate is required for mTLS, but no client certificate source was provided or found.", + ): + config.get_client_cert_source(None, True) + + +def test_get_client_cert_source_use_cert_flag_false(): + assert config.get_client_cert_source(None, False) is None + source = mock.Mock() + assert config.get_client_cert_source(source, False) is None + + +def test_read_environment_variables(): + with mock.patch( + "google.api_core.gapic_v1.config.use_client_cert_effective", return_value=True + ): + with mock.patch.dict( + os.environ, + { + "GOOGLE_API_USE_MTLS_ENDPOINT": "always", + "GOOGLE_CLOUD_UNIVERSE_DOMAIN": "my-universe.com", + }, + ): + use_cert, use_mtls, universe = config.read_environment_variables() + assert use_cert is True + assert use_mtls == "always" + assert universe == "my-universe.com" + + +def test_read_environment_variables_invalid_mtls(): + with mock.patch( + "google.api_core.gapic_v1.config.use_client_cert_effective", return_value=True + ): + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "invalid"}): + with pytest.raises(MutualTLSChannelError): + config.read_environment_variables() diff --git a/packages/gapic-generator/tests/integration/goldens/redis_selective/google/cloud/redis_v1/_compat.py b/packages/gapic-generator/tests/integration/goldens/redis_selective/google/cloud/redis_v1/_compat.py index cb118c8f3409..cce4dc784242 100755 --- a/packages/gapic-generator/tests/integration/goldens/redis_selective/google/cloud/redis_v1/_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/redis_selective/google/cloud/redis_v1/_compat.py @@ -15,10 +15,13 @@ # """A compatibility module for older versions of google-api-core.""" +import os + from typing import Optional from urllib.parse import urlparse, urlunparse from google.auth.exceptions import MutualTLSChannelError +from google.auth.transport import mtls from google.api_core.universe import EmptyUniverseError @@ -138,3 +141,18 @@ def get_universe_domain( if not resolved: raise EmptyUniverseError() return resolved + +def use_client_cert_effective() -> bool: + """Returns whether client certificate should be used for mTLS.""" + if hasattr(mtls, "should_use_client_cert"): + return mtls.should_use_client_cert() + else: + use_client_cert_str = os.getenv( + "GOOGLE_API_USE_CLIENT_CERTIFICATE", "false" + ).lower() + if use_client_cert_str not in ("true", "false"): + raise ValueError( + "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be" + " either `true` or `false`" + ) + return use_client_cert_str == "true" diff --git a/packages/gapic-generator/tests/integration/goldens/redis_selective/google/cloud/redis_v1/services/cloud_redis/client.py b/packages/gapic-generator/tests/integration/goldens/redis_selective/google/cloud/redis_v1/services/cloud_redis/client.py index de455739e52f..b9c1bb1b144d 100755 --- a/packages/gapic-generator/tests/integration/goldens/redis_selective/google/cloud/redis_v1/services/cloud_redis/client.py +++ b/packages/gapic-generator/tests/integration/goldens/redis_selective/google/cloud/redis_v1/services/cloud_redis/client.py @@ -27,7 +27,7 @@ from google.api_core import client_options as client_options_lib from google.api_core import exceptions as core_exceptions from google.api_core import gapic_v1 -from google.cloud.redis_v1._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint +from google.cloud.redis_v1._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, use_client_cert_effective from google.api_core import retry as retries from google.auth import credentials as ga_credentials # type: ignore from google.auth.transport import mtls # type: ignore @@ -290,7 +290,7 @@ def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_optio DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = mtls.should_use_client_cert() + use_client_cert = use_client_cert_effective() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -327,7 +327,7 @@ def _read_environment_variables(): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = mtls.should_use_client_cert() + use_client_cert = use_client_cert_effective() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/tests/integration/goldens/redis_selective/tests/unit/gapic/redis_v1/test_compat.py b/packages/gapic-generator/tests/integration/goldens/redis_selective/tests/unit/gapic/redis_v1/test_compat.py index 5caa250d85a1..a52eb1f9cc03 100755 --- a/packages/gapic-generator/tests/integration/goldens/redis_selective/tests/unit/gapic/redis_v1/test_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/redis_selective/tests/unit/gapic/redis_v1/test_compat.py @@ -17,10 +17,12 @@ import re import pytest +import os from google.cloud.redis_v1 import _compat as universe from google.auth.exceptions import MutualTLSChannelError +from unittest import mock def test_get_universe_domain(): @@ -217,3 +219,107 @@ def test_get_api_endpoint( ) == expected ) + + +def test_use_client_cert_effective_true(): + mock_mtls = mock.Mock(spec=["should_use_client_cert"]) + mock_mtls.should_use_client_cert.return_value = True + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + assert config.use_client_cert_effective() is True + + +def test_use_client_cert_effective_false(): + mock_mtls = mock.Mock(spec=["should_use_client_cert"]) + mock_mtls.should_use_client_cert.return_value = False + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + assert config.use_client_cert_effective() is False + + +def test_use_client_cert_effective_fallback_env_true(): + mock_mtls = mock.Mock(spec=[]) + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): + assert config.use_client_cert_effective() is True + + +def test_use_client_cert_effective_fallback_env_false(): + mock_mtls = mock.Mock(spec=[]) + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"} + ): + assert config.use_client_cert_effective() is False + + +def test_use_client_cert_effective_fallback_env_invalid(): + mock_mtls = mock.Mock(spec=[]) + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"} + ): + with pytest.raises( + ValueError, + match="Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`", + ): + config.use_client_cert_effective() + + +def test_get_client_cert_source_provided(): + source = mock.Mock() + assert config.get_client_cert_source(source, True) == source + + +def test_get_client_cert_source_default(): + mock_mtls = mock.Mock( + spec=["has_default_client_cert_source", "default_client_cert_source"] + ) + mock_mtls.has_default_client_cert_source.return_value = True + mock_source = mock.Mock() + mock_mtls.default_client_cert_source.return_value = mock_source + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + assert config.get_client_cert_source(None, True) == mock_source + + +def test_get_client_cert_source_none(): + mock_mtls = mock.Mock( + spec=["has_default_client_cert_source", "default_client_cert_source"] + ) + mock_mtls.has_default_client_cert_source.return_value = False + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with pytest.raises( + ValueError, + match="Client certificate is required for mTLS, but no client certificate source was provided or found.", + ): + config.get_client_cert_source(None, True) + + +def test_get_client_cert_source_use_cert_flag_false(): + assert config.get_client_cert_source(None, False) is None + source = mock.Mock() + assert config.get_client_cert_source(source, False) is None + + +def test_read_environment_variables(): + with mock.patch( + "google.api_core.gapic_v1.config.use_client_cert_effective", return_value=True + ): + with mock.patch.dict( + os.environ, + { + "GOOGLE_API_USE_MTLS_ENDPOINT": "always", + "GOOGLE_CLOUD_UNIVERSE_DOMAIN": "my-universe.com", + }, + ): + use_cert, use_mtls, universe = config.read_environment_variables() + assert use_cert is True + assert use_mtls == "always" + assert universe == "my-universe.com" + + +def test_read_environment_variables_invalid_mtls(): + with mock.patch( + "google.api_core.gapic_v1.config.use_client_cert_effective", return_value=True + ): + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "invalid"}): + with pytest.raises(MutualTLSChannelError): + config.read_environment_variables() diff --git a/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/_compat.py b/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/_compat.py index c951a8a71847..02fc059ef85b 100755 --- a/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/_compat.py @@ -15,10 +15,13 @@ # """A compatibility module for older versions of google-api-core.""" +import os + from typing import Optional from urllib.parse import urlparse, urlunparse from google.auth.exceptions import MutualTLSChannelError +from google.auth.transport import mtls from google.api_core.universe import EmptyUniverseError import uuid @@ -142,6 +145,21 @@ def get_universe_domain( raise EmptyUniverseError() return resolved +def use_client_cert_effective() -> bool: + """Returns whether client certificate should be used for mTLS.""" + if hasattr(mtls, "should_use_client_cert"): + return mtls.should_use_client_cert() + else: + use_client_cert_str = os.getenv( + "GOOGLE_API_USE_CLIENT_CERTIFICATE", "false" + ).lower() + if use_client_cert_str not in ("true", "false"): + raise ValueError( + "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be" + " either `true` or `false`" + ) + return use_client_cert_str == "true" + def setup_request_id( request: Union[google.protobuf.message.Message, dict, None], diff --git a/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/services/storage_batch_operations/client.py b/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/services/storage_batch_operations/client.py index c747d45f5d7d..5a1903781f2a 100755 --- a/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/services/storage_batch_operations/client.py +++ b/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/services/storage_batch_operations/client.py @@ -28,7 +28,7 @@ from google.api_core import client_options as client_options_lib from google.api_core import exceptions as core_exceptions from google.api_core import gapic_v1 -from google.cloud.storagebatchoperations_v1._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint +from google.cloud.storagebatchoperations_v1._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, use_client_cert_effective from google.cloud.storagebatchoperations_v1._compat import setup_request_id from google.api_core import retry as retries from google.auth import credentials as ga_credentials # type: ignore @@ -286,7 +286,7 @@ def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_optio DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = mtls.should_use_client_cert() + use_client_cert = use_client_cert_effective() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -323,7 +323,7 @@ def _read_environment_variables(): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = mtls.should_use_client_cert() + use_client_cert = use_client_cert_effective() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/tests/unit/gapic/storagebatchoperations_v1/test_compat.py b/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/tests/unit/gapic/storagebatchoperations_v1/test_compat.py index 52b112caef3f..629bd4ad7f02 100755 --- a/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/tests/unit/gapic/storagebatchoperations_v1/test_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/tests/unit/gapic/storagebatchoperations_v1/test_compat.py @@ -17,11 +17,13 @@ import re import pytest +import os from google.cloud.storagebatchoperations_v1 import _compat as universe from google.cloud.storagebatchoperations_v1._compat import setup_request_id from google.auth.exceptions import MutualTLSChannelError +from unittest import mock def test_get_universe_domain(): @@ -220,6 +222,110 @@ def test_get_api_endpoint( ) +def test_use_client_cert_effective_true(): + mock_mtls = mock.Mock(spec=["should_use_client_cert"]) + mock_mtls.should_use_client_cert.return_value = True + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + assert config.use_client_cert_effective() is True + + +def test_use_client_cert_effective_false(): + mock_mtls = mock.Mock(spec=["should_use_client_cert"]) + mock_mtls.should_use_client_cert.return_value = False + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + assert config.use_client_cert_effective() is False + + +def test_use_client_cert_effective_fallback_env_true(): + mock_mtls = mock.Mock(spec=[]) + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): + assert config.use_client_cert_effective() is True + + +def test_use_client_cert_effective_fallback_env_false(): + mock_mtls = mock.Mock(spec=[]) + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"} + ): + assert config.use_client_cert_effective() is False + + +def test_use_client_cert_effective_fallback_env_invalid(): + mock_mtls = mock.Mock(spec=[]) + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"} + ): + with pytest.raises( + ValueError, + match="Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`", + ): + config.use_client_cert_effective() + + +def test_get_client_cert_source_provided(): + source = mock.Mock() + assert config.get_client_cert_source(source, True) == source + + +def test_get_client_cert_source_default(): + mock_mtls = mock.Mock( + spec=["has_default_client_cert_source", "default_client_cert_source"] + ) + mock_mtls.has_default_client_cert_source.return_value = True + mock_source = mock.Mock() + mock_mtls.default_client_cert_source.return_value = mock_source + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + assert config.get_client_cert_source(None, True) == mock_source + + +def test_get_client_cert_source_none(): + mock_mtls = mock.Mock( + spec=["has_default_client_cert_source", "default_client_cert_source"] + ) + mock_mtls.has_default_client_cert_source.return_value = False + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with pytest.raises( + ValueError, + match="Client certificate is required for mTLS, but no client certificate source was provided or found.", + ): + config.get_client_cert_source(None, True) + + +def test_get_client_cert_source_use_cert_flag_false(): + assert config.get_client_cert_source(None, False) is None + source = mock.Mock() + assert config.get_client_cert_source(source, False) is None + + +def test_read_environment_variables(): + with mock.patch( + "google.api_core.gapic_v1.config.use_client_cert_effective", return_value=True + ): + with mock.patch.dict( + os.environ, + { + "GOOGLE_API_USE_MTLS_ENDPOINT": "always", + "GOOGLE_CLOUD_UNIVERSE_DOMAIN": "my-universe.com", + }, + ): + use_cert, use_mtls, universe = config.read_environment_variables() + assert use_cert is True + assert use_mtls == "always" + assert universe == "my-universe.com" + + +def test_read_environment_variables_invalid_mtls(): + with mock.patch( + "google.api_core.gapic_v1.config.use_client_cert_effective", return_value=True + ): + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "invalid"}): + with pytest.raises(MutualTLSChannelError): + config.read_environment_variables() + + class MockRequest: def __init__(self, **kwargs): for k, v in kwargs.items(): From 0dcc129d6bcd2f41301b3c02ea4b9251ee9e35bc Mon Sep 17 00:00:00 2001 From: Heba Alazzeh Date: Fri, 31 Jul 2026 18:41:19 +0000 Subject: [PATCH 3/6] update goldens --- .../%name_%version/%sub/test_compat.py.j2 | 104 +++--------------- .../tests/unit/gapic/asset_v1/test_compat.py | 103 +++-------------- .../unit/gapic/credentials_v1/test_compat.py | 103 +++-------------- .../unit/gapic/eventarc_v1/test_compat.py | 103 +++-------------- .../unit/gapic/logging_v2/test_compat.py | 103 +++-------------- .../unit/gapic/logging_v2/test_compat.py | 103 +++-------------- .../tests/unit/gapic/redis_v1/test_compat.py | 103 +++-------------- .../tests/unit/gapic/redis_v1/test_compat.py | 103 +++-------------- .../storagebatchoperations_v1/test_compat.py | 103 +++-------------- 9 files changed, 126 insertions(+), 802 deletions(-) diff --git a/packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_compat.py.j2 b/packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_compat.py.j2 index d1f28af6c4d3..12aa48c2cf3d 100644 --- a/packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_compat.py.j2 +++ b/packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_compat.py.j2 @@ -21,6 +21,7 @@ from {{package_path}}._compat import setup_request_id {% endif %} from google.auth.exceptions import MutualTLSChannelError +import google.auth.transport.mtls from unittest import mock @@ -220,109 +221,32 @@ def test_get_api_endpoint( ) -def test_use_client_cert_effective_true(): - mock_mtls = mock.Mock(spec=["should_use_client_cert"]) +def test_use_client_cert_effective_with_should_use_client_cert(): + mock_mtls = mock.Mock() mock_mtls.should_use_client_cert.return_value = True - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - assert config.use_client_cert_effective() is True + with mock.patch(f"{universe.__name__}.mtls", mock_mtls): + assert universe.use_client_cert_effective() is True - -def test_use_client_cert_effective_false(): - mock_mtls = mock.Mock(spec=["should_use_client_cert"]) mock_mtls.should_use_client_cert.return_value = False - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - assert config.use_client_cert_effective() is False + with mock.patch(f"{universe.__name__}.mtls", mock_mtls): + assert universe.use_client_cert_effective() is False -def test_use_client_cert_effective_fallback_env_true(): +def test_use_client_cert_effective_fallback_env(): mock_mtls = mock.Mock(spec=[]) - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with mock.patch(f"{universe.__name__}.mtls", mock_mtls): with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): - assert config.use_client_cert_effective() is True - - -def test_use_client_cert_effective_fallback_env_false(): - mock_mtls = mock.Mock(spec=[]) - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - with mock.patch.dict( - os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"} - ): - assert config.use_client_cert_effective() is False + assert universe.use_client_cert_effective() is True + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}): + assert universe.use_client_cert_effective() is False -def test_use_client_cert_effective_fallback_env_invalid(): - mock_mtls = mock.Mock(spec=[]) - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - with mock.patch.dict( - os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"} - ): + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"}): with pytest.raises( ValueError, match="Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`", ): - config.use_client_cert_effective() - - -def test_get_client_cert_source_provided(): - source = mock.Mock() - assert config.get_client_cert_source(source, True) == source - - -def test_get_client_cert_source_default(): - mock_mtls = mock.Mock( - spec=["has_default_client_cert_source", "default_client_cert_source"] - ) - mock_mtls.has_default_client_cert_source.return_value = True - mock_source = mock.Mock() - mock_mtls.default_client_cert_source.return_value = mock_source - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - assert config.get_client_cert_source(None, True) == mock_source - - -def test_get_client_cert_source_none(): - mock_mtls = mock.Mock( - spec=["has_default_client_cert_source", "default_client_cert_source"] - ) - mock_mtls.has_default_client_cert_source.return_value = False - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - with pytest.raises( - ValueError, - match="Client certificate is required for mTLS, but no client certificate source was provided or found.", - ): - config.get_client_cert_source(None, True) - - -def test_get_client_cert_source_use_cert_flag_false(): - assert config.get_client_cert_source(None, False) is None - source = mock.Mock() - assert config.get_client_cert_source(source, False) is None - - -def test_read_environment_variables(): - with mock.patch( - "google.api_core.gapic_v1.config.use_client_cert_effective", return_value=True - ): - with mock.patch.dict( - os.environ, - { - "GOOGLE_API_USE_MTLS_ENDPOINT": "always", - "GOOGLE_CLOUD_UNIVERSE_DOMAIN": "my-universe.com", - }, - ): - use_cert, use_mtls, universe = config.read_environment_variables() - assert use_cert is True - assert use_mtls == "always" - assert universe == "my-universe.com" - - -def test_read_environment_variables_invalid_mtls(): - with mock.patch( - "google.api_core.gapic_v1.config.use_client_cert_effective", return_value=True - ): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "invalid"}): - with pytest.raises(MutualTLSChannelError): - config.read_environment_variables() - + universe.use_client_cert_effective() {% if has_auto_populated_fields %} diff --git a/packages/gapic-generator/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_compat.py b/packages/gapic-generator/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_compat.py index 131814f4f753..ec3899d1d033 100755 --- a/packages/gapic-generator/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_compat.py @@ -22,6 +22,7 @@ from google.cloud.asset_v1 import _compat as universe from google.auth.exceptions import MutualTLSChannelError +import google.auth.transport.mtls from unittest import mock @@ -221,105 +222,29 @@ def test_get_api_endpoint( ) -def test_use_client_cert_effective_true(): - mock_mtls = mock.Mock(spec=["should_use_client_cert"]) +def test_use_client_cert_effective_with_should_use_client_cert(): + mock_mtls = mock.Mock() mock_mtls.should_use_client_cert.return_value = True - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - assert config.use_client_cert_effective() is True + with mock.patch(f"{universe.__name__}.mtls", mock_mtls): + assert universe.use_client_cert_effective() is True - -def test_use_client_cert_effective_false(): - mock_mtls = mock.Mock(spec=["should_use_client_cert"]) mock_mtls.should_use_client_cert.return_value = False - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - assert config.use_client_cert_effective() is False + with mock.patch(f"{universe.__name__}.mtls", mock_mtls): + assert universe.use_client_cert_effective() is False -def test_use_client_cert_effective_fallback_env_true(): +def test_use_client_cert_effective_fallback_env(): mock_mtls = mock.Mock(spec=[]) - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with mock.patch(f"{universe.__name__}.mtls", mock_mtls): with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): - assert config.use_client_cert_effective() is True - + assert universe.use_client_cert_effective() is True -def test_use_client_cert_effective_fallback_env_false(): - mock_mtls = mock.Mock(spec=[]) - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - with mock.patch.dict( - os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"} - ): - assert config.use_client_cert_effective() is False + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}): + assert universe.use_client_cert_effective() is False - -def test_use_client_cert_effective_fallback_env_invalid(): - mock_mtls = mock.Mock(spec=[]) - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - with mock.patch.dict( - os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"} - ): + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"}): with pytest.raises( ValueError, match="Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`", ): - config.use_client_cert_effective() - - -def test_get_client_cert_source_provided(): - source = mock.Mock() - assert config.get_client_cert_source(source, True) == source - - -def test_get_client_cert_source_default(): - mock_mtls = mock.Mock( - spec=["has_default_client_cert_source", "default_client_cert_source"] - ) - mock_mtls.has_default_client_cert_source.return_value = True - mock_source = mock.Mock() - mock_mtls.default_client_cert_source.return_value = mock_source - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - assert config.get_client_cert_source(None, True) == mock_source - - -def test_get_client_cert_source_none(): - mock_mtls = mock.Mock( - spec=["has_default_client_cert_source", "default_client_cert_source"] - ) - mock_mtls.has_default_client_cert_source.return_value = False - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - with pytest.raises( - ValueError, - match="Client certificate is required for mTLS, but no client certificate source was provided or found.", - ): - config.get_client_cert_source(None, True) - - -def test_get_client_cert_source_use_cert_flag_false(): - assert config.get_client_cert_source(None, False) is None - source = mock.Mock() - assert config.get_client_cert_source(source, False) is None - - -def test_read_environment_variables(): - with mock.patch( - "google.api_core.gapic_v1.config.use_client_cert_effective", return_value=True - ): - with mock.patch.dict( - os.environ, - { - "GOOGLE_API_USE_MTLS_ENDPOINT": "always", - "GOOGLE_CLOUD_UNIVERSE_DOMAIN": "my-universe.com", - }, - ): - use_cert, use_mtls, universe = config.read_environment_variables() - assert use_cert is True - assert use_mtls == "always" - assert universe == "my-universe.com" - - -def test_read_environment_variables_invalid_mtls(): - with mock.patch( - "google.api_core.gapic_v1.config.use_client_cert_effective", return_value=True - ): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "invalid"}): - with pytest.raises(MutualTLSChannelError): - config.read_environment_variables() + universe.use_client_cert_effective() diff --git a/packages/gapic-generator/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_compat.py b/packages/gapic-generator/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_compat.py index 80bca06e578a..e7421e4c7129 100755 --- a/packages/gapic-generator/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_compat.py @@ -22,6 +22,7 @@ from google.iam.credentials_v1 import _compat as universe from google.auth.exceptions import MutualTLSChannelError +import google.auth.transport.mtls from unittest import mock @@ -221,105 +222,29 @@ def test_get_api_endpoint( ) -def test_use_client_cert_effective_true(): - mock_mtls = mock.Mock(spec=["should_use_client_cert"]) +def test_use_client_cert_effective_with_should_use_client_cert(): + mock_mtls = mock.Mock() mock_mtls.should_use_client_cert.return_value = True - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - assert config.use_client_cert_effective() is True + with mock.patch(f"{universe.__name__}.mtls", mock_mtls): + assert universe.use_client_cert_effective() is True - -def test_use_client_cert_effective_false(): - mock_mtls = mock.Mock(spec=["should_use_client_cert"]) mock_mtls.should_use_client_cert.return_value = False - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - assert config.use_client_cert_effective() is False + with mock.patch(f"{universe.__name__}.mtls", mock_mtls): + assert universe.use_client_cert_effective() is False -def test_use_client_cert_effective_fallback_env_true(): +def test_use_client_cert_effective_fallback_env(): mock_mtls = mock.Mock(spec=[]) - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with mock.patch(f"{universe.__name__}.mtls", mock_mtls): with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): - assert config.use_client_cert_effective() is True - + assert universe.use_client_cert_effective() is True -def test_use_client_cert_effective_fallback_env_false(): - mock_mtls = mock.Mock(spec=[]) - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - with mock.patch.dict( - os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"} - ): - assert config.use_client_cert_effective() is False + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}): + assert universe.use_client_cert_effective() is False - -def test_use_client_cert_effective_fallback_env_invalid(): - mock_mtls = mock.Mock(spec=[]) - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - with mock.patch.dict( - os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"} - ): + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"}): with pytest.raises( ValueError, match="Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`", ): - config.use_client_cert_effective() - - -def test_get_client_cert_source_provided(): - source = mock.Mock() - assert config.get_client_cert_source(source, True) == source - - -def test_get_client_cert_source_default(): - mock_mtls = mock.Mock( - spec=["has_default_client_cert_source", "default_client_cert_source"] - ) - mock_mtls.has_default_client_cert_source.return_value = True - mock_source = mock.Mock() - mock_mtls.default_client_cert_source.return_value = mock_source - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - assert config.get_client_cert_source(None, True) == mock_source - - -def test_get_client_cert_source_none(): - mock_mtls = mock.Mock( - spec=["has_default_client_cert_source", "default_client_cert_source"] - ) - mock_mtls.has_default_client_cert_source.return_value = False - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - with pytest.raises( - ValueError, - match="Client certificate is required for mTLS, but no client certificate source was provided or found.", - ): - config.get_client_cert_source(None, True) - - -def test_get_client_cert_source_use_cert_flag_false(): - assert config.get_client_cert_source(None, False) is None - source = mock.Mock() - assert config.get_client_cert_source(source, False) is None - - -def test_read_environment_variables(): - with mock.patch( - "google.api_core.gapic_v1.config.use_client_cert_effective", return_value=True - ): - with mock.patch.dict( - os.environ, - { - "GOOGLE_API_USE_MTLS_ENDPOINT": "always", - "GOOGLE_CLOUD_UNIVERSE_DOMAIN": "my-universe.com", - }, - ): - use_cert, use_mtls, universe = config.read_environment_variables() - assert use_cert is True - assert use_mtls == "always" - assert universe == "my-universe.com" - - -def test_read_environment_variables_invalid_mtls(): - with mock.patch( - "google.api_core.gapic_v1.config.use_client_cert_effective", return_value=True - ): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "invalid"}): - with pytest.raises(MutualTLSChannelError): - config.read_environment_variables() + universe.use_client_cert_effective() diff --git a/packages/gapic-generator/tests/integration/goldens/eventarc/tests/unit/gapic/eventarc_v1/test_compat.py b/packages/gapic-generator/tests/integration/goldens/eventarc/tests/unit/gapic/eventarc_v1/test_compat.py index 0811b1e712cf..388375dd2ff1 100755 --- a/packages/gapic-generator/tests/integration/goldens/eventarc/tests/unit/gapic/eventarc_v1/test_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/eventarc/tests/unit/gapic/eventarc_v1/test_compat.py @@ -22,6 +22,7 @@ from google.cloud.eventarc_v1 import _compat as universe from google.auth.exceptions import MutualTLSChannelError +import google.auth.transport.mtls from unittest import mock @@ -221,105 +222,29 @@ def test_get_api_endpoint( ) -def test_use_client_cert_effective_true(): - mock_mtls = mock.Mock(spec=["should_use_client_cert"]) +def test_use_client_cert_effective_with_should_use_client_cert(): + mock_mtls = mock.Mock() mock_mtls.should_use_client_cert.return_value = True - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - assert config.use_client_cert_effective() is True + with mock.patch(f"{universe.__name__}.mtls", mock_mtls): + assert universe.use_client_cert_effective() is True - -def test_use_client_cert_effective_false(): - mock_mtls = mock.Mock(spec=["should_use_client_cert"]) mock_mtls.should_use_client_cert.return_value = False - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - assert config.use_client_cert_effective() is False + with mock.patch(f"{universe.__name__}.mtls", mock_mtls): + assert universe.use_client_cert_effective() is False -def test_use_client_cert_effective_fallback_env_true(): +def test_use_client_cert_effective_fallback_env(): mock_mtls = mock.Mock(spec=[]) - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with mock.patch(f"{universe.__name__}.mtls", mock_mtls): with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): - assert config.use_client_cert_effective() is True - + assert universe.use_client_cert_effective() is True -def test_use_client_cert_effective_fallback_env_false(): - mock_mtls = mock.Mock(spec=[]) - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - with mock.patch.dict( - os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"} - ): - assert config.use_client_cert_effective() is False + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}): + assert universe.use_client_cert_effective() is False - -def test_use_client_cert_effective_fallback_env_invalid(): - mock_mtls = mock.Mock(spec=[]) - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - with mock.patch.dict( - os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"} - ): + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"}): with pytest.raises( ValueError, match="Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`", ): - config.use_client_cert_effective() - - -def test_get_client_cert_source_provided(): - source = mock.Mock() - assert config.get_client_cert_source(source, True) == source - - -def test_get_client_cert_source_default(): - mock_mtls = mock.Mock( - spec=["has_default_client_cert_source", "default_client_cert_source"] - ) - mock_mtls.has_default_client_cert_source.return_value = True - mock_source = mock.Mock() - mock_mtls.default_client_cert_source.return_value = mock_source - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - assert config.get_client_cert_source(None, True) == mock_source - - -def test_get_client_cert_source_none(): - mock_mtls = mock.Mock( - spec=["has_default_client_cert_source", "default_client_cert_source"] - ) - mock_mtls.has_default_client_cert_source.return_value = False - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - with pytest.raises( - ValueError, - match="Client certificate is required for mTLS, but no client certificate source was provided or found.", - ): - config.get_client_cert_source(None, True) - - -def test_get_client_cert_source_use_cert_flag_false(): - assert config.get_client_cert_source(None, False) is None - source = mock.Mock() - assert config.get_client_cert_source(source, False) is None - - -def test_read_environment_variables(): - with mock.patch( - "google.api_core.gapic_v1.config.use_client_cert_effective", return_value=True - ): - with mock.patch.dict( - os.environ, - { - "GOOGLE_API_USE_MTLS_ENDPOINT": "always", - "GOOGLE_CLOUD_UNIVERSE_DOMAIN": "my-universe.com", - }, - ): - use_cert, use_mtls, universe = config.read_environment_variables() - assert use_cert is True - assert use_mtls == "always" - assert universe == "my-universe.com" - - -def test_read_environment_variables_invalid_mtls(): - with mock.patch( - "google.api_core.gapic_v1.config.use_client_cert_effective", return_value=True - ): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "invalid"}): - with pytest.raises(MutualTLSChannelError): - config.read_environment_variables() + universe.use_client_cert_effective() diff --git a/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_compat.py b/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_compat.py index 49118be5d01b..4403fea60e29 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_compat.py @@ -22,6 +22,7 @@ from google.cloud.logging_v2 import _compat as universe from google.auth.exceptions import MutualTLSChannelError +import google.auth.transport.mtls from unittest import mock @@ -221,105 +222,29 @@ def test_get_api_endpoint( ) -def test_use_client_cert_effective_true(): - mock_mtls = mock.Mock(spec=["should_use_client_cert"]) +def test_use_client_cert_effective_with_should_use_client_cert(): + mock_mtls = mock.Mock() mock_mtls.should_use_client_cert.return_value = True - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - assert config.use_client_cert_effective() is True + with mock.patch(f"{universe.__name__}.mtls", mock_mtls): + assert universe.use_client_cert_effective() is True - -def test_use_client_cert_effective_false(): - mock_mtls = mock.Mock(spec=["should_use_client_cert"]) mock_mtls.should_use_client_cert.return_value = False - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - assert config.use_client_cert_effective() is False + with mock.patch(f"{universe.__name__}.mtls", mock_mtls): + assert universe.use_client_cert_effective() is False -def test_use_client_cert_effective_fallback_env_true(): +def test_use_client_cert_effective_fallback_env(): mock_mtls = mock.Mock(spec=[]) - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with mock.patch(f"{universe.__name__}.mtls", mock_mtls): with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): - assert config.use_client_cert_effective() is True - + assert universe.use_client_cert_effective() is True -def test_use_client_cert_effective_fallback_env_false(): - mock_mtls = mock.Mock(spec=[]) - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - with mock.patch.dict( - os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"} - ): - assert config.use_client_cert_effective() is False + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}): + assert universe.use_client_cert_effective() is False - -def test_use_client_cert_effective_fallback_env_invalid(): - mock_mtls = mock.Mock(spec=[]) - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - with mock.patch.dict( - os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"} - ): + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"}): with pytest.raises( ValueError, match="Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`", ): - config.use_client_cert_effective() - - -def test_get_client_cert_source_provided(): - source = mock.Mock() - assert config.get_client_cert_source(source, True) == source - - -def test_get_client_cert_source_default(): - mock_mtls = mock.Mock( - spec=["has_default_client_cert_source", "default_client_cert_source"] - ) - mock_mtls.has_default_client_cert_source.return_value = True - mock_source = mock.Mock() - mock_mtls.default_client_cert_source.return_value = mock_source - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - assert config.get_client_cert_source(None, True) == mock_source - - -def test_get_client_cert_source_none(): - mock_mtls = mock.Mock( - spec=["has_default_client_cert_source", "default_client_cert_source"] - ) - mock_mtls.has_default_client_cert_source.return_value = False - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - with pytest.raises( - ValueError, - match="Client certificate is required for mTLS, but no client certificate source was provided or found.", - ): - config.get_client_cert_source(None, True) - - -def test_get_client_cert_source_use_cert_flag_false(): - assert config.get_client_cert_source(None, False) is None - source = mock.Mock() - assert config.get_client_cert_source(source, False) is None - - -def test_read_environment_variables(): - with mock.patch( - "google.api_core.gapic_v1.config.use_client_cert_effective", return_value=True - ): - with mock.patch.dict( - os.environ, - { - "GOOGLE_API_USE_MTLS_ENDPOINT": "always", - "GOOGLE_CLOUD_UNIVERSE_DOMAIN": "my-universe.com", - }, - ): - use_cert, use_mtls, universe = config.read_environment_variables() - assert use_cert is True - assert use_mtls == "always" - assert universe == "my-universe.com" - - -def test_read_environment_variables_invalid_mtls(): - with mock.patch( - "google.api_core.gapic_v1.config.use_client_cert_effective", return_value=True - ): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "invalid"}): - with pytest.raises(MutualTLSChannelError): - config.read_environment_variables() + universe.use_client_cert_effective() diff --git a/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_compat.py b/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_compat.py index 49118be5d01b..4403fea60e29 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_compat.py @@ -22,6 +22,7 @@ from google.cloud.logging_v2 import _compat as universe from google.auth.exceptions import MutualTLSChannelError +import google.auth.transport.mtls from unittest import mock @@ -221,105 +222,29 @@ def test_get_api_endpoint( ) -def test_use_client_cert_effective_true(): - mock_mtls = mock.Mock(spec=["should_use_client_cert"]) +def test_use_client_cert_effective_with_should_use_client_cert(): + mock_mtls = mock.Mock() mock_mtls.should_use_client_cert.return_value = True - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - assert config.use_client_cert_effective() is True + with mock.patch(f"{universe.__name__}.mtls", mock_mtls): + assert universe.use_client_cert_effective() is True - -def test_use_client_cert_effective_false(): - mock_mtls = mock.Mock(spec=["should_use_client_cert"]) mock_mtls.should_use_client_cert.return_value = False - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - assert config.use_client_cert_effective() is False + with mock.patch(f"{universe.__name__}.mtls", mock_mtls): + assert universe.use_client_cert_effective() is False -def test_use_client_cert_effective_fallback_env_true(): +def test_use_client_cert_effective_fallback_env(): mock_mtls = mock.Mock(spec=[]) - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with mock.patch(f"{universe.__name__}.mtls", mock_mtls): with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): - assert config.use_client_cert_effective() is True - + assert universe.use_client_cert_effective() is True -def test_use_client_cert_effective_fallback_env_false(): - mock_mtls = mock.Mock(spec=[]) - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - with mock.patch.dict( - os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"} - ): - assert config.use_client_cert_effective() is False + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}): + assert universe.use_client_cert_effective() is False - -def test_use_client_cert_effective_fallback_env_invalid(): - mock_mtls = mock.Mock(spec=[]) - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - with mock.patch.dict( - os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"} - ): + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"}): with pytest.raises( ValueError, match="Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`", ): - config.use_client_cert_effective() - - -def test_get_client_cert_source_provided(): - source = mock.Mock() - assert config.get_client_cert_source(source, True) == source - - -def test_get_client_cert_source_default(): - mock_mtls = mock.Mock( - spec=["has_default_client_cert_source", "default_client_cert_source"] - ) - mock_mtls.has_default_client_cert_source.return_value = True - mock_source = mock.Mock() - mock_mtls.default_client_cert_source.return_value = mock_source - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - assert config.get_client_cert_source(None, True) == mock_source - - -def test_get_client_cert_source_none(): - mock_mtls = mock.Mock( - spec=["has_default_client_cert_source", "default_client_cert_source"] - ) - mock_mtls.has_default_client_cert_source.return_value = False - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - with pytest.raises( - ValueError, - match="Client certificate is required for mTLS, but no client certificate source was provided or found.", - ): - config.get_client_cert_source(None, True) - - -def test_get_client_cert_source_use_cert_flag_false(): - assert config.get_client_cert_source(None, False) is None - source = mock.Mock() - assert config.get_client_cert_source(source, False) is None - - -def test_read_environment_variables(): - with mock.patch( - "google.api_core.gapic_v1.config.use_client_cert_effective", return_value=True - ): - with mock.patch.dict( - os.environ, - { - "GOOGLE_API_USE_MTLS_ENDPOINT": "always", - "GOOGLE_CLOUD_UNIVERSE_DOMAIN": "my-universe.com", - }, - ): - use_cert, use_mtls, universe = config.read_environment_variables() - assert use_cert is True - assert use_mtls == "always" - assert universe == "my-universe.com" - - -def test_read_environment_variables_invalid_mtls(): - with mock.patch( - "google.api_core.gapic_v1.config.use_client_cert_effective", return_value=True - ): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "invalid"}): - with pytest.raises(MutualTLSChannelError): - config.read_environment_variables() + universe.use_client_cert_effective() diff --git a/packages/gapic-generator/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_compat.py b/packages/gapic-generator/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_compat.py index a52eb1f9cc03..94841cf763ab 100755 --- a/packages/gapic-generator/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_compat.py @@ -22,6 +22,7 @@ from google.cloud.redis_v1 import _compat as universe from google.auth.exceptions import MutualTLSChannelError +import google.auth.transport.mtls from unittest import mock @@ -221,105 +222,29 @@ def test_get_api_endpoint( ) -def test_use_client_cert_effective_true(): - mock_mtls = mock.Mock(spec=["should_use_client_cert"]) +def test_use_client_cert_effective_with_should_use_client_cert(): + mock_mtls = mock.Mock() mock_mtls.should_use_client_cert.return_value = True - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - assert config.use_client_cert_effective() is True + with mock.patch(f"{universe.__name__}.mtls", mock_mtls): + assert universe.use_client_cert_effective() is True - -def test_use_client_cert_effective_false(): - mock_mtls = mock.Mock(spec=["should_use_client_cert"]) mock_mtls.should_use_client_cert.return_value = False - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - assert config.use_client_cert_effective() is False + with mock.patch(f"{universe.__name__}.mtls", mock_mtls): + assert universe.use_client_cert_effective() is False -def test_use_client_cert_effective_fallback_env_true(): +def test_use_client_cert_effective_fallback_env(): mock_mtls = mock.Mock(spec=[]) - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with mock.patch(f"{universe.__name__}.mtls", mock_mtls): with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): - assert config.use_client_cert_effective() is True - + assert universe.use_client_cert_effective() is True -def test_use_client_cert_effective_fallback_env_false(): - mock_mtls = mock.Mock(spec=[]) - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - with mock.patch.dict( - os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"} - ): - assert config.use_client_cert_effective() is False + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}): + assert universe.use_client_cert_effective() is False - -def test_use_client_cert_effective_fallback_env_invalid(): - mock_mtls = mock.Mock(spec=[]) - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - with mock.patch.dict( - os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"} - ): + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"}): with pytest.raises( ValueError, match="Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`", ): - config.use_client_cert_effective() - - -def test_get_client_cert_source_provided(): - source = mock.Mock() - assert config.get_client_cert_source(source, True) == source - - -def test_get_client_cert_source_default(): - mock_mtls = mock.Mock( - spec=["has_default_client_cert_source", "default_client_cert_source"] - ) - mock_mtls.has_default_client_cert_source.return_value = True - mock_source = mock.Mock() - mock_mtls.default_client_cert_source.return_value = mock_source - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - assert config.get_client_cert_source(None, True) == mock_source - - -def test_get_client_cert_source_none(): - mock_mtls = mock.Mock( - spec=["has_default_client_cert_source", "default_client_cert_source"] - ) - mock_mtls.has_default_client_cert_source.return_value = False - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - with pytest.raises( - ValueError, - match="Client certificate is required for mTLS, but no client certificate source was provided or found.", - ): - config.get_client_cert_source(None, True) - - -def test_get_client_cert_source_use_cert_flag_false(): - assert config.get_client_cert_source(None, False) is None - source = mock.Mock() - assert config.get_client_cert_source(source, False) is None - - -def test_read_environment_variables(): - with mock.patch( - "google.api_core.gapic_v1.config.use_client_cert_effective", return_value=True - ): - with mock.patch.dict( - os.environ, - { - "GOOGLE_API_USE_MTLS_ENDPOINT": "always", - "GOOGLE_CLOUD_UNIVERSE_DOMAIN": "my-universe.com", - }, - ): - use_cert, use_mtls, universe = config.read_environment_variables() - assert use_cert is True - assert use_mtls == "always" - assert universe == "my-universe.com" - - -def test_read_environment_variables_invalid_mtls(): - with mock.patch( - "google.api_core.gapic_v1.config.use_client_cert_effective", return_value=True - ): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "invalid"}): - with pytest.raises(MutualTLSChannelError): - config.read_environment_variables() + universe.use_client_cert_effective() diff --git a/packages/gapic-generator/tests/integration/goldens/redis_selective/tests/unit/gapic/redis_v1/test_compat.py b/packages/gapic-generator/tests/integration/goldens/redis_selective/tests/unit/gapic/redis_v1/test_compat.py index a52eb1f9cc03..94841cf763ab 100755 --- a/packages/gapic-generator/tests/integration/goldens/redis_selective/tests/unit/gapic/redis_v1/test_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/redis_selective/tests/unit/gapic/redis_v1/test_compat.py @@ -22,6 +22,7 @@ from google.cloud.redis_v1 import _compat as universe from google.auth.exceptions import MutualTLSChannelError +import google.auth.transport.mtls from unittest import mock @@ -221,105 +222,29 @@ def test_get_api_endpoint( ) -def test_use_client_cert_effective_true(): - mock_mtls = mock.Mock(spec=["should_use_client_cert"]) +def test_use_client_cert_effective_with_should_use_client_cert(): + mock_mtls = mock.Mock() mock_mtls.should_use_client_cert.return_value = True - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - assert config.use_client_cert_effective() is True + with mock.patch(f"{universe.__name__}.mtls", mock_mtls): + assert universe.use_client_cert_effective() is True - -def test_use_client_cert_effective_false(): - mock_mtls = mock.Mock(spec=["should_use_client_cert"]) mock_mtls.should_use_client_cert.return_value = False - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - assert config.use_client_cert_effective() is False + with mock.patch(f"{universe.__name__}.mtls", mock_mtls): + assert universe.use_client_cert_effective() is False -def test_use_client_cert_effective_fallback_env_true(): +def test_use_client_cert_effective_fallback_env(): mock_mtls = mock.Mock(spec=[]) - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with mock.patch(f"{universe.__name__}.mtls", mock_mtls): with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): - assert config.use_client_cert_effective() is True - + assert universe.use_client_cert_effective() is True -def test_use_client_cert_effective_fallback_env_false(): - mock_mtls = mock.Mock(spec=[]) - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - with mock.patch.dict( - os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"} - ): - assert config.use_client_cert_effective() is False + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}): + assert universe.use_client_cert_effective() is False - -def test_use_client_cert_effective_fallback_env_invalid(): - mock_mtls = mock.Mock(spec=[]) - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - with mock.patch.dict( - os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"} - ): + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"}): with pytest.raises( ValueError, match="Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`", ): - config.use_client_cert_effective() - - -def test_get_client_cert_source_provided(): - source = mock.Mock() - assert config.get_client_cert_source(source, True) == source - - -def test_get_client_cert_source_default(): - mock_mtls = mock.Mock( - spec=["has_default_client_cert_source", "default_client_cert_source"] - ) - mock_mtls.has_default_client_cert_source.return_value = True - mock_source = mock.Mock() - mock_mtls.default_client_cert_source.return_value = mock_source - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - assert config.get_client_cert_source(None, True) == mock_source - - -def test_get_client_cert_source_none(): - mock_mtls = mock.Mock( - spec=["has_default_client_cert_source", "default_client_cert_source"] - ) - mock_mtls.has_default_client_cert_source.return_value = False - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - with pytest.raises( - ValueError, - match="Client certificate is required for mTLS, but no client certificate source was provided or found.", - ): - config.get_client_cert_source(None, True) - - -def test_get_client_cert_source_use_cert_flag_false(): - assert config.get_client_cert_source(None, False) is None - source = mock.Mock() - assert config.get_client_cert_source(source, False) is None - - -def test_read_environment_variables(): - with mock.patch( - "google.api_core.gapic_v1.config.use_client_cert_effective", return_value=True - ): - with mock.patch.dict( - os.environ, - { - "GOOGLE_API_USE_MTLS_ENDPOINT": "always", - "GOOGLE_CLOUD_UNIVERSE_DOMAIN": "my-universe.com", - }, - ): - use_cert, use_mtls, universe = config.read_environment_variables() - assert use_cert is True - assert use_mtls == "always" - assert universe == "my-universe.com" - - -def test_read_environment_variables_invalid_mtls(): - with mock.patch( - "google.api_core.gapic_v1.config.use_client_cert_effective", return_value=True - ): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "invalid"}): - with pytest.raises(MutualTLSChannelError): - config.read_environment_variables() + universe.use_client_cert_effective() diff --git a/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/tests/unit/gapic/storagebatchoperations_v1/test_compat.py b/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/tests/unit/gapic/storagebatchoperations_v1/test_compat.py index 629bd4ad7f02..ed8d786c2e51 100755 --- a/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/tests/unit/gapic/storagebatchoperations_v1/test_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/tests/unit/gapic/storagebatchoperations_v1/test_compat.py @@ -23,6 +23,7 @@ from google.cloud.storagebatchoperations_v1._compat import setup_request_id from google.auth.exceptions import MutualTLSChannelError +import google.auth.transport.mtls from unittest import mock @@ -222,108 +223,32 @@ def test_get_api_endpoint( ) -def test_use_client_cert_effective_true(): - mock_mtls = mock.Mock(spec=["should_use_client_cert"]) +def test_use_client_cert_effective_with_should_use_client_cert(): + mock_mtls = mock.Mock() mock_mtls.should_use_client_cert.return_value = True - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - assert config.use_client_cert_effective() is True + with mock.patch(f"{universe.__name__}.mtls", mock_mtls): + assert universe.use_client_cert_effective() is True - -def test_use_client_cert_effective_false(): - mock_mtls = mock.Mock(spec=["should_use_client_cert"]) mock_mtls.should_use_client_cert.return_value = False - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - assert config.use_client_cert_effective() is False + with mock.patch(f"{universe.__name__}.mtls", mock_mtls): + assert universe.use_client_cert_effective() is False -def test_use_client_cert_effective_fallback_env_true(): +def test_use_client_cert_effective_fallback_env(): mock_mtls = mock.Mock(spec=[]) - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): + with mock.patch(f"{universe.__name__}.mtls", mock_mtls): with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): - assert config.use_client_cert_effective() is True - - -def test_use_client_cert_effective_fallback_env_false(): - mock_mtls = mock.Mock(spec=[]) - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - with mock.patch.dict( - os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"} - ): - assert config.use_client_cert_effective() is False + assert universe.use_client_cert_effective() is True + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}): + assert universe.use_client_cert_effective() is False -def test_use_client_cert_effective_fallback_env_invalid(): - mock_mtls = mock.Mock(spec=[]) - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - with mock.patch.dict( - os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"} - ): + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"}): with pytest.raises( ValueError, match="Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`", ): - config.use_client_cert_effective() - - -def test_get_client_cert_source_provided(): - source = mock.Mock() - assert config.get_client_cert_source(source, True) == source - - -def test_get_client_cert_source_default(): - mock_mtls = mock.Mock( - spec=["has_default_client_cert_source", "default_client_cert_source"] - ) - mock_mtls.has_default_client_cert_source.return_value = True - mock_source = mock.Mock() - mock_mtls.default_client_cert_source.return_value = mock_source - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - assert config.get_client_cert_source(None, True) == mock_source - - -def test_get_client_cert_source_none(): - mock_mtls = mock.Mock( - spec=["has_default_client_cert_source", "default_client_cert_source"] - ) - mock_mtls.has_default_client_cert_source.return_value = False - with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): - with pytest.raises( - ValueError, - match="Client certificate is required for mTLS, but no client certificate source was provided or found.", - ): - config.get_client_cert_source(None, True) - - -def test_get_client_cert_source_use_cert_flag_false(): - assert config.get_client_cert_source(None, False) is None - source = mock.Mock() - assert config.get_client_cert_source(source, False) is None - - -def test_read_environment_variables(): - with mock.patch( - "google.api_core.gapic_v1.config.use_client_cert_effective", return_value=True - ): - with mock.patch.dict( - os.environ, - { - "GOOGLE_API_USE_MTLS_ENDPOINT": "always", - "GOOGLE_CLOUD_UNIVERSE_DOMAIN": "my-universe.com", - }, - ): - use_cert, use_mtls, universe = config.read_environment_variables() - assert use_cert is True - assert use_mtls == "always" - assert universe == "my-universe.com" - - -def test_read_environment_variables_invalid_mtls(): - with mock.patch( - "google.api_core.gapic_v1.config.use_client_cert_effective", return_value=True - ): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "invalid"}): - with pytest.raises(MutualTLSChannelError): - config.read_environment_variables() + universe.use_client_cert_effective() class MockRequest: From 285b8f5bd631821b42c5208f54fadc35b91d14fc Mon Sep 17 00:00:00 2001 From: Heba Alazzeh Date: Fri, 31 Jul 2026 21:28:06 +0000 Subject: [PATCH 4/6] update goldens & address feedback --- .../%name_%version/%sub/_compat.py.j2 | 18 ++++++------ .../%sub/services/%service/client.py.j2 | 6 ++-- .../%name_%version/%sub/test_compat.py.j2 | 28 ++++++++----------- .../asset/google/cloud/asset_v1/_compat.py | 18 ++++++------ .../asset_v1/services/asset_service/client.py | 6 ++-- .../tests/unit/gapic/asset_v1/test_compat.py | 28 ++++++++----------- .../google/iam/credentials_v1/_compat.py | 18 ++++++------ .../services/iam_credentials/client.py | 6 ++-- .../unit/gapic/credentials_v1/test_compat.py | 28 ++++++++----------- .../google/cloud/eventarc_v1/_compat.py | 18 ++++++------ .../eventarc_v1/services/eventarc/client.py | 6 ++-- .../unit/gapic/eventarc_v1/test_compat.py | 28 ++++++++----------- .../google/cloud/logging_v2/_compat.py | 18 ++++++------ .../services/config_service_v2/client.py | 6 ++-- .../services/logging_service_v2/client.py | 6 ++-- .../services/metrics_service_v2/client.py | 6 ++-- .../unit/gapic/logging_v2/test_compat.py | 28 ++++++++----------- .../google/cloud/logging_v2/_compat.py | 18 ++++++------ .../services/config_service_v2/client.py | 6 ++-- .../services/logging_service_v2/client.py | 6 ++-- .../services/metrics_service_v2/client.py | 6 ++-- .../unit/gapic/logging_v2/test_compat.py | 28 ++++++++----------- .../redis/google/cloud/redis_v1/_compat.py | 18 ++++++------ .../redis_v1/services/cloud_redis/client.py | 6 ++-- .../tests/unit/gapic/redis_v1/test_compat.py | 28 ++++++++----------- .../google/cloud/redis_v1/_compat.py | 18 ++++++------ .../redis_v1/services/cloud_redis/client.py | 6 ++-- .../tests/unit/gapic/redis_v1/test_compat.py | 28 ++++++++----------- .../storagebatchoperations_v1/_compat.py | 18 ++++++------ .../storage_batch_operations/client.py | 6 ++-- .../storagebatchoperations_v1/test_compat.py | 28 ++++++++----------- 31 files changed, 219 insertions(+), 273 deletions(-) diff --git a/packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/_compat.py.j2 b/packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/_compat.py.j2 index a6164ad17637..0200668bac5e 100644 --- a/packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/_compat.py.j2 +++ b/packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/_compat.py.j2 @@ -145,20 +145,18 @@ def get_universe_domain( raise EmptyUniverseError() return resolved -def use_client_cert_effective() -> bool: +def should_use_client_cert() -> bool: """Returns whether client certificate should be used for mTLS.""" if hasattr(mtls, "should_use_client_cert"): return mtls.should_use_client_cert() else: - use_client_cert_str = os.getenv( - "GOOGLE_API_USE_CLIENT_CERTIFICATE", "false" - ).lower() - if use_client_cert_str not in ("true", "false"): - raise ValueError( - "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be" - " either `true` or `false`" - ) - return use_client_cert_str == "true" + use_client_cert = os.getenv( + "GOOGLE_API_USE_CLIENT_CERTIFICATE" + ) or os.getenv("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE") + + if use_client_cert: + return use_client_cert.lower() == "true" + return None {% if has_auto_populated_fields %} diff --git a/packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 b/packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 index b76f12308b19..737c5e34e7bb 100644 --- a/packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 +++ b/packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 @@ -30,7 +30,7 @@ from google.api_core import exceptions as core_exceptions from google.api_core import extended_operation {% endif %} from google.api_core import gapic_v1 -from {{package_path}}._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, use_client_cert_effective +from {{package_path}}._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, should_use_client_cert {% if has_auto_populated_fields %} from {{package_path}}._compat import setup_request_id {% endif %} @@ -270,7 +270,7 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta): DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = use_client_cert_effective() + use_client_cert = should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -307,7 +307,7 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = use_client_cert_effective() + use_client_cert = should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_compat.py.j2 b/packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_compat.py.j2 index 12aa48c2cf3d..a92741d2c9c6 100644 --- a/packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_compat.py.j2 +++ b/packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_compat.py.j2 @@ -221,32 +221,28 @@ def test_get_api_endpoint( ) -def test_use_client_cert_effective_with_should_use_client_cert(): +def test_should_use_client_cert_with_should_use_client_cert(): mock_mtls = mock.Mock() mock_mtls.should_use_client_cert.return_value = True with mock.patch(f"{universe.__name__}.mtls", mock_mtls): - assert universe.use_client_cert_effective() is True + assert universe.should_use_client_cert() is True mock_mtls.should_use_client_cert.return_value = False with mock.patch(f"{universe.__name__}.mtls", mock_mtls): - assert universe.use_client_cert_effective() is False + assert universe.should_use_client_cert() is False -def test_use_client_cert_effective_fallback_env(): +def test_should_use_client_cert_fallback_env(): mock_mtls = mock.Mock(spec=[]) with mock.patch(f"{universe.__name__}.mtls", mock_mtls): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): - assert universe.use_client_cert_effective() is True - - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}): - assert universe.use_client_cert_effective() is False - - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"}): - with pytest.raises( - ValueError, - match="Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`", - ): - universe.use_client_cert_effective() + with mock.patch.dict(os.environ, {"CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE": "true"}, clear=True): + assert universe.should_use_client_cert() is True + + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}, clear=True): + assert universe.should_use_client_cert() is False + + with mock.patch.dict(os.environ, {}, clear=True): + assert universe.should_use_client_cert() is None {% if has_auto_populated_fields %} diff --git a/packages/gapic-generator/tests/integration/goldens/asset/google/cloud/asset_v1/_compat.py b/packages/gapic-generator/tests/integration/goldens/asset/google/cloud/asset_v1/_compat.py index cce4dc784242..94119c512980 100755 --- a/packages/gapic-generator/tests/integration/goldens/asset/google/cloud/asset_v1/_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/asset/google/cloud/asset_v1/_compat.py @@ -142,17 +142,15 @@ def get_universe_domain( raise EmptyUniverseError() return resolved -def use_client_cert_effective() -> bool: +def should_use_client_cert() -> bool: """Returns whether client certificate should be used for mTLS.""" if hasattr(mtls, "should_use_client_cert"): return mtls.should_use_client_cert() else: - use_client_cert_str = os.getenv( - "GOOGLE_API_USE_CLIENT_CERTIFICATE", "false" - ).lower() - if use_client_cert_str not in ("true", "false"): - raise ValueError( - "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be" - " either `true` or `false`" - ) - return use_client_cert_str == "true" + use_client_cert = os.getenv( + "GOOGLE_API_USE_CLIENT_CERTIFICATE" + ) or os.getenv("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE") + + if use_client_cert: + return use_client_cert.lower() == "true" + return None diff --git a/packages/gapic-generator/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/client.py b/packages/gapic-generator/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/client.py index 9bb016e3744d..182cfc6017bf 100755 --- a/packages/gapic-generator/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/client.py +++ b/packages/gapic-generator/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/client.py @@ -27,7 +27,7 @@ from google.api_core import client_options as client_options_lib from google.api_core import exceptions as core_exceptions from google.api_core import gapic_v1 -from google.cloud.asset_v1._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, use_client_cert_effective +from google.cloud.asset_v1._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, should_use_client_cert from google.api_core import retry as retries from google.auth import credentials as ga_credentials # type: ignore from google.auth.transport import mtls # type: ignore @@ -325,7 +325,7 @@ def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_optio DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = use_client_cert_effective() + use_client_cert = should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -362,7 +362,7 @@ def _read_environment_variables(): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = use_client_cert_effective() + use_client_cert = should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_compat.py b/packages/gapic-generator/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_compat.py index ec3899d1d033..9f64f5312c2d 100755 --- a/packages/gapic-generator/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_compat.py @@ -222,29 +222,25 @@ def test_get_api_endpoint( ) -def test_use_client_cert_effective_with_should_use_client_cert(): +def test_should_use_client_cert_with_should_use_client_cert(): mock_mtls = mock.Mock() mock_mtls.should_use_client_cert.return_value = True with mock.patch(f"{universe.__name__}.mtls", mock_mtls): - assert universe.use_client_cert_effective() is True + assert universe.should_use_client_cert() is True mock_mtls.should_use_client_cert.return_value = False with mock.patch(f"{universe.__name__}.mtls", mock_mtls): - assert universe.use_client_cert_effective() is False + assert universe.should_use_client_cert() is False -def test_use_client_cert_effective_fallback_env(): +def test_should_use_client_cert_fallback_env(): mock_mtls = mock.Mock(spec=[]) with mock.patch(f"{universe.__name__}.mtls", mock_mtls): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): - assert universe.use_client_cert_effective() is True - - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}): - assert universe.use_client_cert_effective() is False - - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"}): - with pytest.raises( - ValueError, - match="Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`", - ): - universe.use_client_cert_effective() + with mock.patch.dict(os.environ, {"CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE": "true"}, clear=True): + assert universe.should_use_client_cert() is True + + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}, clear=True): + assert universe.should_use_client_cert() is False + + with mock.patch.dict(os.environ, {}, clear=True): + assert universe.should_use_client_cert() is None diff --git a/packages/gapic-generator/tests/integration/goldens/credentials/google/iam/credentials_v1/_compat.py b/packages/gapic-generator/tests/integration/goldens/credentials/google/iam/credentials_v1/_compat.py index cce4dc784242..94119c512980 100755 --- a/packages/gapic-generator/tests/integration/goldens/credentials/google/iam/credentials_v1/_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/credentials/google/iam/credentials_v1/_compat.py @@ -142,17 +142,15 @@ def get_universe_domain( raise EmptyUniverseError() return resolved -def use_client_cert_effective() -> bool: +def should_use_client_cert() -> bool: """Returns whether client certificate should be used for mTLS.""" if hasattr(mtls, "should_use_client_cert"): return mtls.should_use_client_cert() else: - use_client_cert_str = os.getenv( - "GOOGLE_API_USE_CLIENT_CERTIFICATE", "false" - ).lower() - if use_client_cert_str not in ("true", "false"): - raise ValueError( - "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be" - " either `true` or `false`" - ) - return use_client_cert_str == "true" + use_client_cert = os.getenv( + "GOOGLE_API_USE_CLIENT_CERTIFICATE" + ) or os.getenv("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE") + + if use_client_cert: + return use_client_cert.lower() == "true" + return None diff --git a/packages/gapic-generator/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/client.py b/packages/gapic-generator/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/client.py index 813eab83813d..4a3cb7bad6c3 100755 --- a/packages/gapic-generator/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/client.py +++ b/packages/gapic-generator/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/client.py @@ -27,7 +27,7 @@ from google.api_core import client_options as client_options_lib from google.api_core import exceptions as core_exceptions from google.api_core import gapic_v1 -from google.iam.credentials_v1._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, use_client_cert_effective +from google.iam.credentials_v1._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, should_use_client_cert from google.api_core import retry as retries from google.auth import credentials as ga_credentials # type: ignore from google.auth.transport import mtls # type: ignore @@ -262,7 +262,7 @@ def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_optio DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = use_client_cert_effective() + use_client_cert = should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -299,7 +299,7 @@ def _read_environment_variables(): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = use_client_cert_effective() + use_client_cert = should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_compat.py b/packages/gapic-generator/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_compat.py index e7421e4c7129..a436d7e2fee4 100755 --- a/packages/gapic-generator/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_compat.py @@ -222,29 +222,25 @@ def test_get_api_endpoint( ) -def test_use_client_cert_effective_with_should_use_client_cert(): +def test_should_use_client_cert_with_should_use_client_cert(): mock_mtls = mock.Mock() mock_mtls.should_use_client_cert.return_value = True with mock.patch(f"{universe.__name__}.mtls", mock_mtls): - assert universe.use_client_cert_effective() is True + assert universe.should_use_client_cert() is True mock_mtls.should_use_client_cert.return_value = False with mock.patch(f"{universe.__name__}.mtls", mock_mtls): - assert universe.use_client_cert_effective() is False + assert universe.should_use_client_cert() is False -def test_use_client_cert_effective_fallback_env(): +def test_should_use_client_cert_fallback_env(): mock_mtls = mock.Mock(spec=[]) with mock.patch(f"{universe.__name__}.mtls", mock_mtls): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): - assert universe.use_client_cert_effective() is True - - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}): - assert universe.use_client_cert_effective() is False - - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"}): - with pytest.raises( - ValueError, - match="Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`", - ): - universe.use_client_cert_effective() + with mock.patch.dict(os.environ, {"CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE": "true"}, clear=True): + assert universe.should_use_client_cert() is True + + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}, clear=True): + assert universe.should_use_client_cert() is False + + with mock.patch.dict(os.environ, {}, clear=True): + assert universe.should_use_client_cert() is None diff --git a/packages/gapic-generator/tests/integration/goldens/eventarc/google/cloud/eventarc_v1/_compat.py b/packages/gapic-generator/tests/integration/goldens/eventarc/google/cloud/eventarc_v1/_compat.py index cce4dc784242..94119c512980 100755 --- a/packages/gapic-generator/tests/integration/goldens/eventarc/google/cloud/eventarc_v1/_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/eventarc/google/cloud/eventarc_v1/_compat.py @@ -142,17 +142,15 @@ def get_universe_domain( raise EmptyUniverseError() return resolved -def use_client_cert_effective() -> bool: +def should_use_client_cert() -> bool: """Returns whether client certificate should be used for mTLS.""" if hasattr(mtls, "should_use_client_cert"): return mtls.should_use_client_cert() else: - use_client_cert_str = os.getenv( - "GOOGLE_API_USE_CLIENT_CERTIFICATE", "false" - ).lower() - if use_client_cert_str not in ("true", "false"): - raise ValueError( - "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be" - " either `true` or `false`" - ) - return use_client_cert_str == "true" + use_client_cert = os.getenv( + "GOOGLE_API_USE_CLIENT_CERTIFICATE" + ) or os.getenv("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE") + + if use_client_cert: + return use_client_cert.lower() == "true" + return None diff --git a/packages/gapic-generator/tests/integration/goldens/eventarc/google/cloud/eventarc_v1/services/eventarc/client.py b/packages/gapic-generator/tests/integration/goldens/eventarc/google/cloud/eventarc_v1/services/eventarc/client.py index 75a06e71b67e..b015982530cc 100755 --- a/packages/gapic-generator/tests/integration/goldens/eventarc/google/cloud/eventarc_v1/services/eventarc/client.py +++ b/packages/gapic-generator/tests/integration/goldens/eventarc/google/cloud/eventarc_v1/services/eventarc/client.py @@ -27,7 +27,7 @@ from google.api_core import client_options as client_options_lib from google.api_core import exceptions as core_exceptions from google.api_core import gapic_v1 -from google.cloud.eventarc_v1._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, use_client_cert_effective +from google.cloud.eventarc_v1._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, should_use_client_cert from google.api_core import retry as retries from google.auth import credentials as ga_credentials # type: ignore from google.auth.transport import mtls # type: ignore @@ -445,7 +445,7 @@ def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_optio DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = use_client_cert_effective() + use_client_cert = should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -482,7 +482,7 @@ def _read_environment_variables(): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = use_client_cert_effective() + use_client_cert = should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/tests/integration/goldens/eventarc/tests/unit/gapic/eventarc_v1/test_compat.py b/packages/gapic-generator/tests/integration/goldens/eventarc/tests/unit/gapic/eventarc_v1/test_compat.py index 388375dd2ff1..c1546c606ed5 100755 --- a/packages/gapic-generator/tests/integration/goldens/eventarc/tests/unit/gapic/eventarc_v1/test_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/eventarc/tests/unit/gapic/eventarc_v1/test_compat.py @@ -222,29 +222,25 @@ def test_get_api_endpoint( ) -def test_use_client_cert_effective_with_should_use_client_cert(): +def test_should_use_client_cert_with_should_use_client_cert(): mock_mtls = mock.Mock() mock_mtls.should_use_client_cert.return_value = True with mock.patch(f"{universe.__name__}.mtls", mock_mtls): - assert universe.use_client_cert_effective() is True + assert universe.should_use_client_cert() is True mock_mtls.should_use_client_cert.return_value = False with mock.patch(f"{universe.__name__}.mtls", mock_mtls): - assert universe.use_client_cert_effective() is False + assert universe.should_use_client_cert() is False -def test_use_client_cert_effective_fallback_env(): +def test_should_use_client_cert_fallback_env(): mock_mtls = mock.Mock(spec=[]) with mock.patch(f"{universe.__name__}.mtls", mock_mtls): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): - assert universe.use_client_cert_effective() is True - - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}): - assert universe.use_client_cert_effective() is False - - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"}): - with pytest.raises( - ValueError, - match="Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`", - ): - universe.use_client_cert_effective() + with mock.patch.dict(os.environ, {"CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE": "true"}, clear=True): + assert universe.should_use_client_cert() is True + + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}, clear=True): + assert universe.should_use_client_cert() is False + + with mock.patch.dict(os.environ, {}, clear=True): + assert universe.should_use_client_cert() is None diff --git a/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/_compat.py b/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/_compat.py index cce4dc784242..94119c512980 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/_compat.py @@ -142,17 +142,15 @@ def get_universe_domain( raise EmptyUniverseError() return resolved -def use_client_cert_effective() -> bool: +def should_use_client_cert() -> bool: """Returns whether client certificate should be used for mTLS.""" if hasattr(mtls, "should_use_client_cert"): return mtls.should_use_client_cert() else: - use_client_cert_str = os.getenv( - "GOOGLE_API_USE_CLIENT_CERTIFICATE", "false" - ).lower() - if use_client_cert_str not in ("true", "false"): - raise ValueError( - "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be" - " either `true` or `false`" - ) - return use_client_cert_str == "true" + use_client_cert = os.getenv( + "GOOGLE_API_USE_CLIENT_CERTIFICATE" + ) or os.getenv("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE") + + if use_client_cert: + return use_client_cert.lower() == "true" + return None diff --git a/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/client.py b/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/client.py index 2240f5bff786..36468f3d26c7 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/client.py +++ b/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/client.py @@ -27,7 +27,7 @@ from google.api_core import client_options as client_options_lib from google.api_core import exceptions as core_exceptions from google.api_core import gapic_v1 -from google.cloud.logging_v2._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, use_client_cert_effective +from google.cloud.logging_v2._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, should_use_client_cert from google.api_core import retry as retries from google.auth import credentials as ga_credentials # type: ignore from google.auth.transport import mtls # type: ignore @@ -321,7 +321,7 @@ def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_optio DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = use_client_cert_effective() + use_client_cert = should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -358,7 +358,7 @@ def _read_environment_variables(): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = use_client_cert_effective() + use_client_cert = should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/client.py b/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/client.py index 65fc8435e83c..6009409d3685 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/client.py +++ b/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/client.py @@ -27,7 +27,7 @@ from google.api_core import client_options as client_options_lib from google.api_core import exceptions as core_exceptions from google.api_core import gapic_v1 -from google.cloud.logging_v2._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, use_client_cert_effective +from google.cloud.logging_v2._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, should_use_client_cert from google.api_core import retry as retries from google.auth import credentials as ga_credentials # type: ignore from google.auth.transport import mtls # type: ignore @@ -252,7 +252,7 @@ def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_optio DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = use_client_cert_effective() + use_client_cert = should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -289,7 +289,7 @@ def _read_environment_variables(): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = use_client_cert_effective() + use_client_cert = should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/client.py b/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/client.py index 417c223f1cc0..1c353672fae1 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/client.py +++ b/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/client.py @@ -27,7 +27,7 @@ from google.api_core import client_options as client_options_lib from google.api_core import exceptions as core_exceptions from google.api_core import gapic_v1 -from google.cloud.logging_v2._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, use_client_cert_effective +from google.cloud.logging_v2._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, should_use_client_cert from google.api_core import retry as retries from google.auth import credentials as ga_credentials # type: ignore from google.auth.transport import mtls # type: ignore @@ -253,7 +253,7 @@ def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_optio DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = use_client_cert_effective() + use_client_cert = should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -290,7 +290,7 @@ def _read_environment_variables(): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = use_client_cert_effective() + use_client_cert = should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_compat.py b/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_compat.py index 4403fea60e29..4819207e94bb 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_compat.py @@ -222,29 +222,25 @@ def test_get_api_endpoint( ) -def test_use_client_cert_effective_with_should_use_client_cert(): +def test_should_use_client_cert_with_should_use_client_cert(): mock_mtls = mock.Mock() mock_mtls.should_use_client_cert.return_value = True with mock.patch(f"{universe.__name__}.mtls", mock_mtls): - assert universe.use_client_cert_effective() is True + assert universe.should_use_client_cert() is True mock_mtls.should_use_client_cert.return_value = False with mock.patch(f"{universe.__name__}.mtls", mock_mtls): - assert universe.use_client_cert_effective() is False + assert universe.should_use_client_cert() is False -def test_use_client_cert_effective_fallback_env(): +def test_should_use_client_cert_fallback_env(): mock_mtls = mock.Mock(spec=[]) with mock.patch(f"{universe.__name__}.mtls", mock_mtls): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): - assert universe.use_client_cert_effective() is True - - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}): - assert universe.use_client_cert_effective() is False - - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"}): - with pytest.raises( - ValueError, - match="Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`", - ): - universe.use_client_cert_effective() + with mock.patch.dict(os.environ, {"CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE": "true"}, clear=True): + assert universe.should_use_client_cert() is True + + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}, clear=True): + assert universe.should_use_client_cert() is False + + with mock.patch.dict(os.environ, {}, clear=True): + assert universe.should_use_client_cert() is None diff --git a/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/_compat.py b/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/_compat.py index cce4dc784242..94119c512980 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/_compat.py @@ -142,17 +142,15 @@ def get_universe_domain( raise EmptyUniverseError() return resolved -def use_client_cert_effective() -> bool: +def should_use_client_cert() -> bool: """Returns whether client certificate should be used for mTLS.""" if hasattr(mtls, "should_use_client_cert"): return mtls.should_use_client_cert() else: - use_client_cert_str = os.getenv( - "GOOGLE_API_USE_CLIENT_CERTIFICATE", "false" - ).lower() - if use_client_cert_str not in ("true", "false"): - raise ValueError( - "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be" - " either `true` or `false`" - ) - return use_client_cert_str == "true" + use_client_cert = os.getenv( + "GOOGLE_API_USE_CLIENT_CERTIFICATE" + ) or os.getenv("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE") + + if use_client_cert: + return use_client_cert.lower() == "true" + return None diff --git a/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/services/config_service_v2/client.py b/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/services/config_service_v2/client.py index 57523bbf10cb..0b3b7c49baba 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/services/config_service_v2/client.py +++ b/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/services/config_service_v2/client.py @@ -27,7 +27,7 @@ from google.api_core import client_options as client_options_lib from google.api_core import exceptions as core_exceptions from google.api_core import gapic_v1 -from google.cloud.logging_v2._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, use_client_cert_effective +from google.cloud.logging_v2._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, should_use_client_cert from google.api_core import retry as retries from google.auth import credentials as ga_credentials # type: ignore from google.auth.transport import mtls # type: ignore @@ -321,7 +321,7 @@ def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_optio DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = use_client_cert_effective() + use_client_cert = should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -358,7 +358,7 @@ def _read_environment_variables(): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = use_client_cert_effective() + use_client_cert = should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/services/logging_service_v2/client.py b/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/services/logging_service_v2/client.py index 65fc8435e83c..6009409d3685 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/services/logging_service_v2/client.py +++ b/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/services/logging_service_v2/client.py @@ -27,7 +27,7 @@ from google.api_core import client_options as client_options_lib from google.api_core import exceptions as core_exceptions from google.api_core import gapic_v1 -from google.cloud.logging_v2._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, use_client_cert_effective +from google.cloud.logging_v2._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, should_use_client_cert from google.api_core import retry as retries from google.auth import credentials as ga_credentials # type: ignore from google.auth.transport import mtls # type: ignore @@ -252,7 +252,7 @@ def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_optio DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = use_client_cert_effective() + use_client_cert = should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -289,7 +289,7 @@ def _read_environment_variables(): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = use_client_cert_effective() + use_client_cert = should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/services/metrics_service_v2/client.py b/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/services/metrics_service_v2/client.py index 7e6ed1e87a54..7581350f8ec8 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/services/metrics_service_v2/client.py +++ b/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/services/metrics_service_v2/client.py @@ -27,7 +27,7 @@ from google.api_core import client_options as client_options_lib from google.api_core import exceptions as core_exceptions from google.api_core import gapic_v1 -from google.cloud.logging_v2._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, use_client_cert_effective +from google.cloud.logging_v2._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, should_use_client_cert from google.api_core import retry as retries from google.auth import credentials as ga_credentials # type: ignore from google.auth.transport import mtls # type: ignore @@ -253,7 +253,7 @@ def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_optio DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = use_client_cert_effective() + use_client_cert = should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -290,7 +290,7 @@ def _read_environment_variables(): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = use_client_cert_effective() + use_client_cert = should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_compat.py b/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_compat.py index 4403fea60e29..4819207e94bb 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_compat.py @@ -222,29 +222,25 @@ def test_get_api_endpoint( ) -def test_use_client_cert_effective_with_should_use_client_cert(): +def test_should_use_client_cert_with_should_use_client_cert(): mock_mtls = mock.Mock() mock_mtls.should_use_client_cert.return_value = True with mock.patch(f"{universe.__name__}.mtls", mock_mtls): - assert universe.use_client_cert_effective() is True + assert universe.should_use_client_cert() is True mock_mtls.should_use_client_cert.return_value = False with mock.patch(f"{universe.__name__}.mtls", mock_mtls): - assert universe.use_client_cert_effective() is False + assert universe.should_use_client_cert() is False -def test_use_client_cert_effective_fallback_env(): +def test_should_use_client_cert_fallback_env(): mock_mtls = mock.Mock(spec=[]) with mock.patch(f"{universe.__name__}.mtls", mock_mtls): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): - assert universe.use_client_cert_effective() is True - - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}): - assert universe.use_client_cert_effective() is False - - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"}): - with pytest.raises( - ValueError, - match="Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`", - ): - universe.use_client_cert_effective() + with mock.patch.dict(os.environ, {"CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE": "true"}, clear=True): + assert universe.should_use_client_cert() is True + + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}, clear=True): + assert universe.should_use_client_cert() is False + + with mock.patch.dict(os.environ, {}, clear=True): + assert universe.should_use_client_cert() is None diff --git a/packages/gapic-generator/tests/integration/goldens/redis/google/cloud/redis_v1/_compat.py b/packages/gapic-generator/tests/integration/goldens/redis/google/cloud/redis_v1/_compat.py index cce4dc784242..94119c512980 100755 --- a/packages/gapic-generator/tests/integration/goldens/redis/google/cloud/redis_v1/_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/redis/google/cloud/redis_v1/_compat.py @@ -142,17 +142,15 @@ def get_universe_domain( raise EmptyUniverseError() return resolved -def use_client_cert_effective() -> bool: +def should_use_client_cert() -> bool: """Returns whether client certificate should be used for mTLS.""" if hasattr(mtls, "should_use_client_cert"): return mtls.should_use_client_cert() else: - use_client_cert_str = os.getenv( - "GOOGLE_API_USE_CLIENT_CERTIFICATE", "false" - ).lower() - if use_client_cert_str not in ("true", "false"): - raise ValueError( - "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be" - " either `true` or `false`" - ) - return use_client_cert_str == "true" + use_client_cert = os.getenv( + "GOOGLE_API_USE_CLIENT_CERTIFICATE" + ) or os.getenv("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE") + + if use_client_cert: + return use_client_cert.lower() == "true" + return None diff --git a/packages/gapic-generator/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/client.py b/packages/gapic-generator/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/client.py index c243e2c2eec5..add3e3f67f59 100755 --- a/packages/gapic-generator/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/client.py +++ b/packages/gapic-generator/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/client.py @@ -27,7 +27,7 @@ from google.api_core import client_options as client_options_lib from google.api_core import exceptions as core_exceptions from google.api_core import gapic_v1 -from google.cloud.redis_v1._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, use_client_cert_effective +from google.cloud.redis_v1._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, should_use_client_cert from google.api_core import retry as retries from google.auth import credentials as ga_credentials # type: ignore from google.auth.transport import mtls # type: ignore @@ -290,7 +290,7 @@ def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_optio DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = use_client_cert_effective() + use_client_cert = should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -327,7 +327,7 @@ def _read_environment_variables(): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = use_client_cert_effective() + use_client_cert = should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_compat.py b/packages/gapic-generator/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_compat.py index 94841cf763ab..dfd7d0cb8901 100755 --- a/packages/gapic-generator/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_compat.py @@ -222,29 +222,25 @@ def test_get_api_endpoint( ) -def test_use_client_cert_effective_with_should_use_client_cert(): +def test_should_use_client_cert_with_should_use_client_cert(): mock_mtls = mock.Mock() mock_mtls.should_use_client_cert.return_value = True with mock.patch(f"{universe.__name__}.mtls", mock_mtls): - assert universe.use_client_cert_effective() is True + assert universe.should_use_client_cert() is True mock_mtls.should_use_client_cert.return_value = False with mock.patch(f"{universe.__name__}.mtls", mock_mtls): - assert universe.use_client_cert_effective() is False + assert universe.should_use_client_cert() is False -def test_use_client_cert_effective_fallback_env(): +def test_should_use_client_cert_fallback_env(): mock_mtls = mock.Mock(spec=[]) with mock.patch(f"{universe.__name__}.mtls", mock_mtls): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): - assert universe.use_client_cert_effective() is True - - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}): - assert universe.use_client_cert_effective() is False - - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"}): - with pytest.raises( - ValueError, - match="Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`", - ): - universe.use_client_cert_effective() + with mock.patch.dict(os.environ, {"CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE": "true"}, clear=True): + assert universe.should_use_client_cert() is True + + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}, clear=True): + assert universe.should_use_client_cert() is False + + with mock.patch.dict(os.environ, {}, clear=True): + assert universe.should_use_client_cert() is None diff --git a/packages/gapic-generator/tests/integration/goldens/redis_selective/google/cloud/redis_v1/_compat.py b/packages/gapic-generator/tests/integration/goldens/redis_selective/google/cloud/redis_v1/_compat.py index cce4dc784242..94119c512980 100755 --- a/packages/gapic-generator/tests/integration/goldens/redis_selective/google/cloud/redis_v1/_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/redis_selective/google/cloud/redis_v1/_compat.py @@ -142,17 +142,15 @@ def get_universe_domain( raise EmptyUniverseError() return resolved -def use_client_cert_effective() -> bool: +def should_use_client_cert() -> bool: """Returns whether client certificate should be used for mTLS.""" if hasattr(mtls, "should_use_client_cert"): return mtls.should_use_client_cert() else: - use_client_cert_str = os.getenv( - "GOOGLE_API_USE_CLIENT_CERTIFICATE", "false" - ).lower() - if use_client_cert_str not in ("true", "false"): - raise ValueError( - "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be" - " either `true` or `false`" - ) - return use_client_cert_str == "true" + use_client_cert = os.getenv( + "GOOGLE_API_USE_CLIENT_CERTIFICATE" + ) or os.getenv("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE") + + if use_client_cert: + return use_client_cert.lower() == "true" + return None diff --git a/packages/gapic-generator/tests/integration/goldens/redis_selective/google/cloud/redis_v1/services/cloud_redis/client.py b/packages/gapic-generator/tests/integration/goldens/redis_selective/google/cloud/redis_v1/services/cloud_redis/client.py index b9c1bb1b144d..1f6885be1999 100755 --- a/packages/gapic-generator/tests/integration/goldens/redis_selective/google/cloud/redis_v1/services/cloud_redis/client.py +++ b/packages/gapic-generator/tests/integration/goldens/redis_selective/google/cloud/redis_v1/services/cloud_redis/client.py @@ -27,7 +27,7 @@ from google.api_core import client_options as client_options_lib from google.api_core import exceptions as core_exceptions from google.api_core import gapic_v1 -from google.cloud.redis_v1._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, use_client_cert_effective +from google.cloud.redis_v1._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, should_use_client_cert from google.api_core import retry as retries from google.auth import credentials as ga_credentials # type: ignore from google.auth.transport import mtls # type: ignore @@ -290,7 +290,7 @@ def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_optio DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = use_client_cert_effective() + use_client_cert = should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -327,7 +327,7 @@ def _read_environment_variables(): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = use_client_cert_effective() + use_client_cert = should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/tests/integration/goldens/redis_selective/tests/unit/gapic/redis_v1/test_compat.py b/packages/gapic-generator/tests/integration/goldens/redis_selective/tests/unit/gapic/redis_v1/test_compat.py index 94841cf763ab..dfd7d0cb8901 100755 --- a/packages/gapic-generator/tests/integration/goldens/redis_selective/tests/unit/gapic/redis_v1/test_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/redis_selective/tests/unit/gapic/redis_v1/test_compat.py @@ -222,29 +222,25 @@ def test_get_api_endpoint( ) -def test_use_client_cert_effective_with_should_use_client_cert(): +def test_should_use_client_cert_with_should_use_client_cert(): mock_mtls = mock.Mock() mock_mtls.should_use_client_cert.return_value = True with mock.patch(f"{universe.__name__}.mtls", mock_mtls): - assert universe.use_client_cert_effective() is True + assert universe.should_use_client_cert() is True mock_mtls.should_use_client_cert.return_value = False with mock.patch(f"{universe.__name__}.mtls", mock_mtls): - assert universe.use_client_cert_effective() is False + assert universe.should_use_client_cert() is False -def test_use_client_cert_effective_fallback_env(): +def test_should_use_client_cert_fallback_env(): mock_mtls = mock.Mock(spec=[]) with mock.patch(f"{universe.__name__}.mtls", mock_mtls): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): - assert universe.use_client_cert_effective() is True - - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}): - assert universe.use_client_cert_effective() is False - - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"}): - with pytest.raises( - ValueError, - match="Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`", - ): - universe.use_client_cert_effective() + with mock.patch.dict(os.environ, {"CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE": "true"}, clear=True): + assert universe.should_use_client_cert() is True + + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}, clear=True): + assert universe.should_use_client_cert() is False + + with mock.patch.dict(os.environ, {}, clear=True): + assert universe.should_use_client_cert() is None diff --git a/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/_compat.py b/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/_compat.py index 02fc059ef85b..4fca57da8c57 100755 --- a/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/_compat.py @@ -145,20 +145,18 @@ def get_universe_domain( raise EmptyUniverseError() return resolved -def use_client_cert_effective() -> bool: +def should_use_client_cert() -> bool: """Returns whether client certificate should be used for mTLS.""" if hasattr(mtls, "should_use_client_cert"): return mtls.should_use_client_cert() else: - use_client_cert_str = os.getenv( - "GOOGLE_API_USE_CLIENT_CERTIFICATE", "false" - ).lower() - if use_client_cert_str not in ("true", "false"): - raise ValueError( - "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be" - " either `true` or `false`" - ) - return use_client_cert_str == "true" + use_client_cert = os.getenv( + "GOOGLE_API_USE_CLIENT_CERTIFICATE" + ) or os.getenv("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE") + + if use_client_cert: + return use_client_cert.lower() == "true" + return None def setup_request_id( diff --git a/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/services/storage_batch_operations/client.py b/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/services/storage_batch_operations/client.py index 5a1903781f2a..d8c6bd9009a7 100755 --- a/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/services/storage_batch_operations/client.py +++ b/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/services/storage_batch_operations/client.py @@ -28,7 +28,7 @@ from google.api_core import client_options as client_options_lib from google.api_core import exceptions as core_exceptions from google.api_core import gapic_v1 -from google.cloud.storagebatchoperations_v1._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, use_client_cert_effective +from google.cloud.storagebatchoperations_v1._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, should_use_client_cert from google.cloud.storagebatchoperations_v1._compat import setup_request_id from google.api_core import retry as retries from google.auth import credentials as ga_credentials # type: ignore @@ -286,7 +286,7 @@ def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_optio DeprecationWarning) if client_options is None: client_options = client_options_lib.ClientOptions() - use_client_cert = use_client_cert_effective() + use_client_cert = should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_endpoint not in ("auto", "never", "always"): raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`") @@ -323,7 +323,7 @@ def _read_environment_variables(): google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT is not any of ["auto", "never", "always"]. """ - use_client_cert = use_client_cert_effective() + use_client_cert = should_use_client_cert() use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") if use_mtls_endpoint not in ("auto", "never", "always"): diff --git a/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/tests/unit/gapic/storagebatchoperations_v1/test_compat.py b/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/tests/unit/gapic/storagebatchoperations_v1/test_compat.py index ed8d786c2e51..31d84bba564a 100755 --- a/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/tests/unit/gapic/storagebatchoperations_v1/test_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/tests/unit/gapic/storagebatchoperations_v1/test_compat.py @@ -223,32 +223,28 @@ def test_get_api_endpoint( ) -def test_use_client_cert_effective_with_should_use_client_cert(): +def test_should_use_client_cert_with_should_use_client_cert(): mock_mtls = mock.Mock() mock_mtls.should_use_client_cert.return_value = True with mock.patch(f"{universe.__name__}.mtls", mock_mtls): - assert universe.use_client_cert_effective() is True + assert universe.should_use_client_cert() is True mock_mtls.should_use_client_cert.return_value = False with mock.patch(f"{universe.__name__}.mtls", mock_mtls): - assert universe.use_client_cert_effective() is False + assert universe.should_use_client_cert() is False -def test_use_client_cert_effective_fallback_env(): +def test_should_use_client_cert_fallback_env(): mock_mtls = mock.Mock(spec=[]) with mock.patch(f"{universe.__name__}.mtls", mock_mtls): - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): - assert universe.use_client_cert_effective() is True - - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}): - assert universe.use_client_cert_effective() is False - - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"}): - with pytest.raises( - ValueError, - match="Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`", - ): - universe.use_client_cert_effective() + with mock.patch.dict(os.environ, {"CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE": "true"}, clear=True): + assert universe.should_use_client_cert() is True + + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}, clear=True): + assert universe.should_use_client_cert() is False + + with mock.patch.dict(os.environ, {}, clear=True): + assert universe.should_use_client_cert() is None class MockRequest: From fdb2ef8d04489bb85dbc7e0dac5311e1111c39e0 Mon Sep 17 00:00:00 2001 From: Heba Alazzeh Date: Fri, 31 Jul 2026 21:43:42 +0000 Subject: [PATCH 5/6] update goldens --- .../gapic/%name_%version/%sub/test_compat.py.j2 | 2 +- .../asset/google/cloud/asset_v1/_compat.py | 17 ++++------------- .../tests/unit/gapic/asset_v1/test_compat.py | 10 ++++------ .../google/iam/credentials_v1/_compat.py | 17 ++++------------- .../unit/gapic/credentials_v1/test_compat.py | 10 ++++------ .../google/cloud/eventarc_v1/_compat.py | 17 ++++------------- .../tests/unit/gapic/eventarc_v1/test_compat.py | 10 ++++------ .../logging/google/cloud/logging_v2/_compat.py | 17 ++++------------- .../tests/unit/gapic/logging_v2/test_compat.py | 10 ++++------ .../google/cloud/logging_v2/_compat.py | 17 ++++------------- .../tests/unit/gapic/logging_v2/test_compat.py | 10 ++++------ .../redis/google/cloud/redis_v1/_compat.py | 17 ++++------------- .../tests/unit/gapic/redis_v1/test_compat.py | 10 ++++------ .../google/cloud/redis_v1/_compat.py | 17 ++++------------- .../tests/unit/gapic/redis_v1/test_compat.py | 10 ++++------ .../cloud/storagebatchoperations_v1/_compat.py | 16 +++++----------- .../storagebatchoperations_v1/test_compat.py | 10 +++------- 17 files changed, 65 insertions(+), 152 deletions(-) diff --git a/packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_compat.py.j2 b/packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_compat.py.j2 index 23861d8eb510..d694a4e1f4dc 100644 --- a/packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_compat.py.j2 +++ b/packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_compat.py.j2 @@ -26,7 +26,7 @@ import google.auth.transport.mtls from {{package_path}}._compat import setup_request_id {% endif %} from {{package_path}}._compat import transcode_request -from {{package_path}}._compat import universe +from {{package_path}} import _compat as universe from google.protobuf import descriptor_pb2 from google.auth.exceptions import MutualTLSChannelError diff --git a/packages/gapic-generator/tests/integration/goldens/asset/google/cloud/asset_v1/_compat.py b/packages/gapic-generator/tests/integration/goldens/asset/google/cloud/asset_v1/_compat.py index 01323b572d31..437222809e1e 100755 --- a/packages/gapic-generator/tests/integration/goldens/asset/google/cloud/asset_v1/_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/asset/google/cloud/asset_v1/_compat.py @@ -15,24 +15,17 @@ # """A compatibility module for older versions of google-api-core.""" -<<<<<<< HEAD import os - -from typing import Optional -======= import json from typing import Any, Dict, List, Optional, Tuple from google.api_core import path_template -from google.protobuf import json_format ->>>>>>> main -from urllib.parse import urlparse, urlunparse - +from google.api_core.universe import EmptyUniverseError from google.auth.exceptions import MutualTLSChannelError from google.auth.transport import mtls -from google.api_core.universe import EmptyUniverseError - +from google.protobuf import json_format +from urllib.parse import urlparse, urlunparse DEFAULT_UNIVERSE = "googleapis.com" @@ -151,7 +144,6 @@ def get_universe_domain( raise EmptyUniverseError() return resolved -<<<<<<< HEAD def should_use_client_cert() -> bool: """Returns whether client certificate should be used for mTLS.""" if hasattr(mtls, "should_use_client_cert"): @@ -164,7 +156,7 @@ def should_use_client_cert() -> bool: if use_client_cert: return use_client_cert.lower() == "true" return None -======= + def transcode_request( http_options: List[Dict[str, str]], @@ -222,4 +214,3 @@ def transcode_request( query_params_json["$alt"] = "json;enum-encoding=int" return transcoded_request, body_json, query_params_json ->>>>>>> main diff --git a/packages/gapic-generator/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_compat.py b/packages/gapic-generator/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_compat.py index 091aa7f0a927..87c4e0cb6802 100755 --- a/packages/gapic-generator/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_compat.py @@ -21,14 +21,13 @@ from unittest import mock -from google.protobuf import descriptor_pb2 +import google.auth.transport.mtls from google.cloud.asset_v1._compat import transcode_request from google.cloud.asset_v1 import _compat as universe +from google.protobuf import descriptor_pb2 from google.auth.exceptions import MutualTLSChannelError -import google.auth.transport.mtls -from unittest import mock def test_get_universe_domain(): @@ -227,7 +226,6 @@ def test_get_api_endpoint( ) -<<<<<<< HEAD def test_should_use_client_cert_with_should_use_client_cert(): mock_mtls = mock.Mock() mock_mtls.should_use_client_cert.return_value = True @@ -250,7 +248,8 @@ def test_should_use_client_cert_fallback_env(): with mock.patch.dict(os.environ, {}, clear=True): assert universe.should_use_client_cert() is None -======= + + def test_transcode_basic(): # We use FieldDescriptorProto as it has standard primitive fields and nested messages. http_options = [ @@ -422,4 +421,3 @@ def test_transcode_request_proto_plus_wrapper(): transcoded, _, _ = transcode_request(http_options, mock_proto_plus) assert transcoded["uri"] == "/v1/test/proto-plus-field" ->>>>>>> main diff --git a/packages/gapic-generator/tests/integration/goldens/credentials/google/iam/credentials_v1/_compat.py b/packages/gapic-generator/tests/integration/goldens/credentials/google/iam/credentials_v1/_compat.py index 01323b572d31..437222809e1e 100755 --- a/packages/gapic-generator/tests/integration/goldens/credentials/google/iam/credentials_v1/_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/credentials/google/iam/credentials_v1/_compat.py @@ -15,24 +15,17 @@ # """A compatibility module for older versions of google-api-core.""" -<<<<<<< HEAD import os - -from typing import Optional -======= import json from typing import Any, Dict, List, Optional, Tuple from google.api_core import path_template -from google.protobuf import json_format ->>>>>>> main -from urllib.parse import urlparse, urlunparse - +from google.api_core.universe import EmptyUniverseError from google.auth.exceptions import MutualTLSChannelError from google.auth.transport import mtls -from google.api_core.universe import EmptyUniverseError - +from google.protobuf import json_format +from urllib.parse import urlparse, urlunparse DEFAULT_UNIVERSE = "googleapis.com" @@ -151,7 +144,6 @@ def get_universe_domain( raise EmptyUniverseError() return resolved -<<<<<<< HEAD def should_use_client_cert() -> bool: """Returns whether client certificate should be used for mTLS.""" if hasattr(mtls, "should_use_client_cert"): @@ -164,7 +156,7 @@ def should_use_client_cert() -> bool: if use_client_cert: return use_client_cert.lower() == "true" return None -======= + def transcode_request( http_options: List[Dict[str, str]], @@ -222,4 +214,3 @@ def transcode_request( query_params_json["$alt"] = "json;enum-encoding=int" return transcoded_request, body_json, query_params_json ->>>>>>> main diff --git a/packages/gapic-generator/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_compat.py b/packages/gapic-generator/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_compat.py index 25ed51843945..33883244ec8c 100755 --- a/packages/gapic-generator/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_compat.py @@ -21,14 +21,13 @@ from unittest import mock -from google.protobuf import descriptor_pb2 +import google.auth.transport.mtls from google.iam.credentials_v1._compat import transcode_request from google.iam.credentials_v1 import _compat as universe +from google.protobuf import descriptor_pb2 from google.auth.exceptions import MutualTLSChannelError -import google.auth.transport.mtls -from unittest import mock def test_get_universe_domain(): @@ -227,7 +226,6 @@ def test_get_api_endpoint( ) -<<<<<<< HEAD def test_should_use_client_cert_with_should_use_client_cert(): mock_mtls = mock.Mock() mock_mtls.should_use_client_cert.return_value = True @@ -250,7 +248,8 @@ def test_should_use_client_cert_fallback_env(): with mock.patch.dict(os.environ, {}, clear=True): assert universe.should_use_client_cert() is None -======= + + def test_transcode_basic(): # We use FieldDescriptorProto as it has standard primitive fields and nested messages. http_options = [ @@ -422,4 +421,3 @@ def test_transcode_request_proto_plus_wrapper(): transcoded, _, _ = transcode_request(http_options, mock_proto_plus) assert transcoded["uri"] == "/v1/test/proto-plus-field" ->>>>>>> main diff --git a/packages/gapic-generator/tests/integration/goldens/eventarc/google/cloud/eventarc_v1/_compat.py b/packages/gapic-generator/tests/integration/goldens/eventarc/google/cloud/eventarc_v1/_compat.py index 01323b572d31..437222809e1e 100755 --- a/packages/gapic-generator/tests/integration/goldens/eventarc/google/cloud/eventarc_v1/_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/eventarc/google/cloud/eventarc_v1/_compat.py @@ -15,24 +15,17 @@ # """A compatibility module for older versions of google-api-core.""" -<<<<<<< HEAD import os - -from typing import Optional -======= import json from typing import Any, Dict, List, Optional, Tuple from google.api_core import path_template -from google.protobuf import json_format ->>>>>>> main -from urllib.parse import urlparse, urlunparse - +from google.api_core.universe import EmptyUniverseError from google.auth.exceptions import MutualTLSChannelError from google.auth.transport import mtls -from google.api_core.universe import EmptyUniverseError - +from google.protobuf import json_format +from urllib.parse import urlparse, urlunparse DEFAULT_UNIVERSE = "googleapis.com" @@ -151,7 +144,6 @@ def get_universe_domain( raise EmptyUniverseError() return resolved -<<<<<<< HEAD def should_use_client_cert() -> bool: """Returns whether client certificate should be used for mTLS.""" if hasattr(mtls, "should_use_client_cert"): @@ -164,7 +156,7 @@ def should_use_client_cert() -> bool: if use_client_cert: return use_client_cert.lower() == "true" return None -======= + def transcode_request( http_options: List[Dict[str, str]], @@ -222,4 +214,3 @@ def transcode_request( query_params_json["$alt"] = "json;enum-encoding=int" return transcoded_request, body_json, query_params_json ->>>>>>> main diff --git a/packages/gapic-generator/tests/integration/goldens/eventarc/tests/unit/gapic/eventarc_v1/test_compat.py b/packages/gapic-generator/tests/integration/goldens/eventarc/tests/unit/gapic/eventarc_v1/test_compat.py index 190dcb1b36b5..4e5f86406a71 100755 --- a/packages/gapic-generator/tests/integration/goldens/eventarc/tests/unit/gapic/eventarc_v1/test_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/eventarc/tests/unit/gapic/eventarc_v1/test_compat.py @@ -21,14 +21,13 @@ from unittest import mock -from google.protobuf import descriptor_pb2 +import google.auth.transport.mtls from google.cloud.eventarc_v1._compat import transcode_request from google.cloud.eventarc_v1 import _compat as universe +from google.protobuf import descriptor_pb2 from google.auth.exceptions import MutualTLSChannelError -import google.auth.transport.mtls -from unittest import mock def test_get_universe_domain(): @@ -227,7 +226,6 @@ def test_get_api_endpoint( ) -<<<<<<< HEAD def test_should_use_client_cert_with_should_use_client_cert(): mock_mtls = mock.Mock() mock_mtls.should_use_client_cert.return_value = True @@ -250,7 +248,8 @@ def test_should_use_client_cert_fallback_env(): with mock.patch.dict(os.environ, {}, clear=True): assert universe.should_use_client_cert() is None -======= + + def test_transcode_basic(): # We use FieldDescriptorProto as it has standard primitive fields and nested messages. http_options = [ @@ -422,4 +421,3 @@ def test_transcode_request_proto_plus_wrapper(): transcoded, _, _ = transcode_request(http_options, mock_proto_plus) assert transcoded["uri"] == "/v1/test/proto-plus-field" ->>>>>>> main diff --git a/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/_compat.py b/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/_compat.py index 01323b572d31..437222809e1e 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/_compat.py @@ -15,24 +15,17 @@ # """A compatibility module for older versions of google-api-core.""" -<<<<<<< HEAD import os - -from typing import Optional -======= import json from typing import Any, Dict, List, Optional, Tuple from google.api_core import path_template -from google.protobuf import json_format ->>>>>>> main -from urllib.parse import urlparse, urlunparse - +from google.api_core.universe import EmptyUniverseError from google.auth.exceptions import MutualTLSChannelError from google.auth.transport import mtls -from google.api_core.universe import EmptyUniverseError - +from google.protobuf import json_format +from urllib.parse import urlparse, urlunparse DEFAULT_UNIVERSE = "googleapis.com" @@ -151,7 +144,6 @@ def get_universe_domain( raise EmptyUniverseError() return resolved -<<<<<<< HEAD def should_use_client_cert() -> bool: """Returns whether client certificate should be used for mTLS.""" if hasattr(mtls, "should_use_client_cert"): @@ -164,7 +156,7 @@ def should_use_client_cert() -> bool: if use_client_cert: return use_client_cert.lower() == "true" return None -======= + def transcode_request( http_options: List[Dict[str, str]], @@ -222,4 +214,3 @@ def transcode_request( query_params_json["$alt"] = "json;enum-encoding=int" return transcoded_request, body_json, query_params_json ->>>>>>> main diff --git a/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_compat.py b/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_compat.py index 1fa37acecfef..89a933d9ea27 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_compat.py @@ -21,14 +21,13 @@ from unittest import mock -from google.protobuf import descriptor_pb2 +import google.auth.transport.mtls from google.cloud.logging_v2._compat import transcode_request from google.cloud.logging_v2 import _compat as universe +from google.protobuf import descriptor_pb2 from google.auth.exceptions import MutualTLSChannelError -import google.auth.transport.mtls -from unittest import mock def test_get_universe_domain(): @@ -227,7 +226,6 @@ def test_get_api_endpoint( ) -<<<<<<< HEAD def test_should_use_client_cert_with_should_use_client_cert(): mock_mtls = mock.Mock() mock_mtls.should_use_client_cert.return_value = True @@ -250,7 +248,8 @@ def test_should_use_client_cert_fallback_env(): with mock.patch.dict(os.environ, {}, clear=True): assert universe.should_use_client_cert() is None -======= + + def test_transcode_basic(): # We use FieldDescriptorProto as it has standard primitive fields and nested messages. http_options = [ @@ -422,4 +421,3 @@ def test_transcode_request_proto_plus_wrapper(): transcoded, _, _ = transcode_request(http_options, mock_proto_plus) assert transcoded["uri"] == "/v1/test/proto-plus-field" ->>>>>>> main diff --git a/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/_compat.py b/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/_compat.py index 01323b572d31..437222809e1e 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/_compat.py @@ -15,24 +15,17 @@ # """A compatibility module for older versions of google-api-core.""" -<<<<<<< HEAD import os - -from typing import Optional -======= import json from typing import Any, Dict, List, Optional, Tuple from google.api_core import path_template -from google.protobuf import json_format ->>>>>>> main -from urllib.parse import urlparse, urlunparse - +from google.api_core.universe import EmptyUniverseError from google.auth.exceptions import MutualTLSChannelError from google.auth.transport import mtls -from google.api_core.universe import EmptyUniverseError - +from google.protobuf import json_format +from urllib.parse import urlparse, urlunparse DEFAULT_UNIVERSE = "googleapis.com" @@ -151,7 +144,6 @@ def get_universe_domain( raise EmptyUniverseError() return resolved -<<<<<<< HEAD def should_use_client_cert() -> bool: """Returns whether client certificate should be used for mTLS.""" if hasattr(mtls, "should_use_client_cert"): @@ -164,7 +156,7 @@ def should_use_client_cert() -> bool: if use_client_cert: return use_client_cert.lower() == "true" return None -======= + def transcode_request( http_options: List[Dict[str, str]], @@ -222,4 +214,3 @@ def transcode_request( query_params_json["$alt"] = "json;enum-encoding=int" return transcoded_request, body_json, query_params_json ->>>>>>> main diff --git a/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_compat.py b/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_compat.py index 1fa37acecfef..89a933d9ea27 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_compat.py @@ -21,14 +21,13 @@ from unittest import mock -from google.protobuf import descriptor_pb2 +import google.auth.transport.mtls from google.cloud.logging_v2._compat import transcode_request from google.cloud.logging_v2 import _compat as universe +from google.protobuf import descriptor_pb2 from google.auth.exceptions import MutualTLSChannelError -import google.auth.transport.mtls -from unittest import mock def test_get_universe_domain(): @@ -227,7 +226,6 @@ def test_get_api_endpoint( ) -<<<<<<< HEAD def test_should_use_client_cert_with_should_use_client_cert(): mock_mtls = mock.Mock() mock_mtls.should_use_client_cert.return_value = True @@ -250,7 +248,8 @@ def test_should_use_client_cert_fallback_env(): with mock.patch.dict(os.environ, {}, clear=True): assert universe.should_use_client_cert() is None -======= + + def test_transcode_basic(): # We use FieldDescriptorProto as it has standard primitive fields and nested messages. http_options = [ @@ -422,4 +421,3 @@ def test_transcode_request_proto_plus_wrapper(): transcoded, _, _ = transcode_request(http_options, mock_proto_plus) assert transcoded["uri"] == "/v1/test/proto-plus-field" ->>>>>>> main diff --git a/packages/gapic-generator/tests/integration/goldens/redis/google/cloud/redis_v1/_compat.py b/packages/gapic-generator/tests/integration/goldens/redis/google/cloud/redis_v1/_compat.py index 01323b572d31..437222809e1e 100755 --- a/packages/gapic-generator/tests/integration/goldens/redis/google/cloud/redis_v1/_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/redis/google/cloud/redis_v1/_compat.py @@ -15,24 +15,17 @@ # """A compatibility module for older versions of google-api-core.""" -<<<<<<< HEAD import os - -from typing import Optional -======= import json from typing import Any, Dict, List, Optional, Tuple from google.api_core import path_template -from google.protobuf import json_format ->>>>>>> main -from urllib.parse import urlparse, urlunparse - +from google.api_core.universe import EmptyUniverseError from google.auth.exceptions import MutualTLSChannelError from google.auth.transport import mtls -from google.api_core.universe import EmptyUniverseError - +from google.protobuf import json_format +from urllib.parse import urlparse, urlunparse DEFAULT_UNIVERSE = "googleapis.com" @@ -151,7 +144,6 @@ def get_universe_domain( raise EmptyUniverseError() return resolved -<<<<<<< HEAD def should_use_client_cert() -> bool: """Returns whether client certificate should be used for mTLS.""" if hasattr(mtls, "should_use_client_cert"): @@ -164,7 +156,7 @@ def should_use_client_cert() -> bool: if use_client_cert: return use_client_cert.lower() == "true" return None -======= + def transcode_request( http_options: List[Dict[str, str]], @@ -222,4 +214,3 @@ def transcode_request( query_params_json["$alt"] = "json;enum-encoding=int" return transcoded_request, body_json, query_params_json ->>>>>>> main diff --git a/packages/gapic-generator/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_compat.py b/packages/gapic-generator/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_compat.py index 7dabf1922195..e656dbae5d82 100755 --- a/packages/gapic-generator/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_compat.py @@ -21,14 +21,13 @@ from unittest import mock -from google.protobuf import descriptor_pb2 +import google.auth.transport.mtls from google.cloud.redis_v1._compat import transcode_request from google.cloud.redis_v1 import _compat as universe +from google.protobuf import descriptor_pb2 from google.auth.exceptions import MutualTLSChannelError -import google.auth.transport.mtls -from unittest import mock def test_get_universe_domain(): @@ -227,7 +226,6 @@ def test_get_api_endpoint( ) -<<<<<<< HEAD def test_should_use_client_cert_with_should_use_client_cert(): mock_mtls = mock.Mock() mock_mtls.should_use_client_cert.return_value = True @@ -250,7 +248,8 @@ def test_should_use_client_cert_fallback_env(): with mock.patch.dict(os.environ, {}, clear=True): assert universe.should_use_client_cert() is None -======= + + def test_transcode_basic(): # We use FieldDescriptorProto as it has standard primitive fields and nested messages. http_options = [ @@ -422,4 +421,3 @@ def test_transcode_request_proto_plus_wrapper(): transcoded, _, _ = transcode_request(http_options, mock_proto_plus) assert transcoded["uri"] == "/v1/test/proto-plus-field" ->>>>>>> main diff --git a/packages/gapic-generator/tests/integration/goldens/redis_selective/google/cloud/redis_v1/_compat.py b/packages/gapic-generator/tests/integration/goldens/redis_selective/google/cloud/redis_v1/_compat.py index 01323b572d31..437222809e1e 100755 --- a/packages/gapic-generator/tests/integration/goldens/redis_selective/google/cloud/redis_v1/_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/redis_selective/google/cloud/redis_v1/_compat.py @@ -15,24 +15,17 @@ # """A compatibility module for older versions of google-api-core.""" -<<<<<<< HEAD import os - -from typing import Optional -======= import json from typing import Any, Dict, List, Optional, Tuple from google.api_core import path_template -from google.protobuf import json_format ->>>>>>> main -from urllib.parse import urlparse, urlunparse - +from google.api_core.universe import EmptyUniverseError from google.auth.exceptions import MutualTLSChannelError from google.auth.transport import mtls -from google.api_core.universe import EmptyUniverseError - +from google.protobuf import json_format +from urllib.parse import urlparse, urlunparse DEFAULT_UNIVERSE = "googleapis.com" @@ -151,7 +144,6 @@ def get_universe_domain( raise EmptyUniverseError() return resolved -<<<<<<< HEAD def should_use_client_cert() -> bool: """Returns whether client certificate should be used for mTLS.""" if hasattr(mtls, "should_use_client_cert"): @@ -164,7 +156,7 @@ def should_use_client_cert() -> bool: if use_client_cert: return use_client_cert.lower() == "true" return None -======= + def transcode_request( http_options: List[Dict[str, str]], @@ -222,4 +214,3 @@ def transcode_request( query_params_json["$alt"] = "json;enum-encoding=int" return transcoded_request, body_json, query_params_json ->>>>>>> main diff --git a/packages/gapic-generator/tests/integration/goldens/redis_selective/tests/unit/gapic/redis_v1/test_compat.py b/packages/gapic-generator/tests/integration/goldens/redis_selective/tests/unit/gapic/redis_v1/test_compat.py index 7dabf1922195..e656dbae5d82 100755 --- a/packages/gapic-generator/tests/integration/goldens/redis_selective/tests/unit/gapic/redis_v1/test_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/redis_selective/tests/unit/gapic/redis_v1/test_compat.py @@ -21,14 +21,13 @@ from unittest import mock -from google.protobuf import descriptor_pb2 +import google.auth.transport.mtls from google.cloud.redis_v1._compat import transcode_request from google.cloud.redis_v1 import _compat as universe +from google.protobuf import descriptor_pb2 from google.auth.exceptions import MutualTLSChannelError -import google.auth.transport.mtls -from unittest import mock def test_get_universe_domain(): @@ -227,7 +226,6 @@ def test_get_api_endpoint( ) -<<<<<<< HEAD def test_should_use_client_cert_with_should_use_client_cert(): mock_mtls = mock.Mock() mock_mtls.should_use_client_cert.return_value = True @@ -250,7 +248,8 @@ def test_should_use_client_cert_fallback_env(): with mock.patch.dict(os.environ, {}, clear=True): assert universe.should_use_client_cert() is None -======= + + def test_transcode_basic(): # We use FieldDescriptorProto as it has standard primitive fields and nested messages. http_options = [ @@ -422,4 +421,3 @@ def test_transcode_request_proto_plus_wrapper(): transcoded, _, _ = transcode_request(http_options, mock_proto_plus) assert transcoded["uri"] == "/v1/test/proto-plus-field" ->>>>>>> main diff --git a/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/_compat.py b/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/_compat.py index 13eb8255ff3b..64f633eb0636 100755 --- a/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/_compat.py @@ -15,27 +15,21 @@ # """A compatibility module for older versions of google-api-core.""" -<<<<<<< HEAD import os - -from typing import Optional -======= import json import uuid from typing import Any, Dict, List, Optional, Tuple from typing import Union -from google.api_core import path_template -from google.protobuf import json_format ->>>>>>> main -from urllib.parse import urlparse, urlunparse +import google.protobuf.message +from google.api_core import path_template +from google.api_core.universe import EmptyUniverseError from google.auth.exceptions import MutualTLSChannelError from google.auth.transport import mtls -from google.api_core.universe import EmptyUniverseError - -import google.protobuf.message +from google.protobuf import json_format +from urllib.parse import urlparse, urlunparse DEFAULT_UNIVERSE = "googleapis.com" diff --git a/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/tests/unit/gapic/storagebatchoperations_v1/test_compat.py b/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/tests/unit/gapic/storagebatchoperations_v1/test_compat.py index 45b51b5627d9..f73d700d3f49 100755 --- a/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/tests/unit/gapic/storagebatchoperations_v1/test_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/tests/unit/gapic/storagebatchoperations_v1/test_compat.py @@ -17,23 +17,19 @@ import json import pytest -<<<<<<< HEAD import os -======= import re ->>>>>>> main from unittest import mock -from google.protobuf import descriptor_pb2 +import google.auth.transport.mtls +from google.cloud.storagebatchoperations_v1._compat import setup_request_id from google.cloud.storagebatchoperations_v1._compat import transcode_request from google.cloud.storagebatchoperations_v1 import _compat as universe -from google.cloud.storagebatchoperations_v1._compat import setup_request_id +from google.protobuf import descriptor_pb2 from google.auth.exceptions import MutualTLSChannelError -import google.auth.transport.mtls -from unittest import mock def test_get_universe_domain(): From 3279a6fd7f2af4ebca53dfa91137473d3c4804d6 Mon Sep 17 00:00:00 2001 From: Heba Alazzeh Date: Fri, 31 Jul 2026 21:58:16 +0000 Subject: [PATCH 6/6] update goldens --- .../templates/%namespace/%name_%version/%sub/_compat.py.j2 | 2 +- .../integration/goldens/asset/google/cloud/asset_v1/_compat.py | 2 +- .../goldens/credentials/google/iam/credentials_v1/_compat.py | 2 +- .../goldens/eventarc/google/cloud/eventarc_v1/_compat.py | 2 +- .../goldens/logging/google/cloud/logging_v2/_compat.py | 2 +- .../goldens/logging_internal/google/cloud/logging_v2/_compat.py | 2 +- .../integration/goldens/redis/google/cloud/redis_v1/_compat.py | 2 +- .../goldens/redis_selective/google/cloud/redis_v1/_compat.py | 2 +- .../google/cloud/storagebatchoperations_v1/_compat.py | 2 +- 9 files changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/_compat.py.j2 b/packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/_compat.py.j2 index e30e314af7c2..c92d8e405e84 100644 --- a/packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/_compat.py.j2 +++ b/packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/_compat.py.j2 @@ -152,7 +152,7 @@ def get_universe_domain( raise EmptyUniverseError() return resolved -def should_use_client_cert() -> bool: +def should_use_client_cert() -> Optional[bool]: """Returns whether client certificate should be used for mTLS.""" if hasattr(mtls, "should_use_client_cert"): return mtls.should_use_client_cert() diff --git a/packages/gapic-generator/tests/integration/goldens/asset/google/cloud/asset_v1/_compat.py b/packages/gapic-generator/tests/integration/goldens/asset/google/cloud/asset_v1/_compat.py index 437222809e1e..83d8170f1257 100755 --- a/packages/gapic-generator/tests/integration/goldens/asset/google/cloud/asset_v1/_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/asset/google/cloud/asset_v1/_compat.py @@ -144,7 +144,7 @@ def get_universe_domain( raise EmptyUniverseError() return resolved -def should_use_client_cert() -> bool: +def should_use_client_cert() -> Optional[bool]: """Returns whether client certificate should be used for mTLS.""" if hasattr(mtls, "should_use_client_cert"): return mtls.should_use_client_cert() diff --git a/packages/gapic-generator/tests/integration/goldens/credentials/google/iam/credentials_v1/_compat.py b/packages/gapic-generator/tests/integration/goldens/credentials/google/iam/credentials_v1/_compat.py index 437222809e1e..83d8170f1257 100755 --- a/packages/gapic-generator/tests/integration/goldens/credentials/google/iam/credentials_v1/_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/credentials/google/iam/credentials_v1/_compat.py @@ -144,7 +144,7 @@ def get_universe_domain( raise EmptyUniverseError() return resolved -def should_use_client_cert() -> bool: +def should_use_client_cert() -> Optional[bool]: """Returns whether client certificate should be used for mTLS.""" if hasattr(mtls, "should_use_client_cert"): return mtls.should_use_client_cert() diff --git a/packages/gapic-generator/tests/integration/goldens/eventarc/google/cloud/eventarc_v1/_compat.py b/packages/gapic-generator/tests/integration/goldens/eventarc/google/cloud/eventarc_v1/_compat.py index 437222809e1e..83d8170f1257 100755 --- a/packages/gapic-generator/tests/integration/goldens/eventarc/google/cloud/eventarc_v1/_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/eventarc/google/cloud/eventarc_v1/_compat.py @@ -144,7 +144,7 @@ def get_universe_domain( raise EmptyUniverseError() return resolved -def should_use_client_cert() -> bool: +def should_use_client_cert() -> Optional[bool]: """Returns whether client certificate should be used for mTLS.""" if hasattr(mtls, "should_use_client_cert"): return mtls.should_use_client_cert() diff --git a/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/_compat.py b/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/_compat.py index 437222809e1e..83d8170f1257 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/logging/google/cloud/logging_v2/_compat.py @@ -144,7 +144,7 @@ def get_universe_domain( raise EmptyUniverseError() return resolved -def should_use_client_cert() -> bool: +def should_use_client_cert() -> Optional[bool]: """Returns whether client certificate should be used for mTLS.""" if hasattr(mtls, "should_use_client_cert"): return mtls.should_use_client_cert() diff --git a/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/_compat.py b/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/_compat.py index 437222809e1e..83d8170f1257 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/logging_internal/google/cloud/logging_v2/_compat.py @@ -144,7 +144,7 @@ def get_universe_domain( raise EmptyUniverseError() return resolved -def should_use_client_cert() -> bool: +def should_use_client_cert() -> Optional[bool]: """Returns whether client certificate should be used for mTLS.""" if hasattr(mtls, "should_use_client_cert"): return mtls.should_use_client_cert() diff --git a/packages/gapic-generator/tests/integration/goldens/redis/google/cloud/redis_v1/_compat.py b/packages/gapic-generator/tests/integration/goldens/redis/google/cloud/redis_v1/_compat.py index 437222809e1e..83d8170f1257 100755 --- a/packages/gapic-generator/tests/integration/goldens/redis/google/cloud/redis_v1/_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/redis/google/cloud/redis_v1/_compat.py @@ -144,7 +144,7 @@ def get_universe_domain( raise EmptyUniverseError() return resolved -def should_use_client_cert() -> bool: +def should_use_client_cert() -> Optional[bool]: """Returns whether client certificate should be used for mTLS.""" if hasattr(mtls, "should_use_client_cert"): return mtls.should_use_client_cert() diff --git a/packages/gapic-generator/tests/integration/goldens/redis_selective/google/cloud/redis_v1/_compat.py b/packages/gapic-generator/tests/integration/goldens/redis_selective/google/cloud/redis_v1/_compat.py index 437222809e1e..83d8170f1257 100755 --- a/packages/gapic-generator/tests/integration/goldens/redis_selective/google/cloud/redis_v1/_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/redis_selective/google/cloud/redis_v1/_compat.py @@ -144,7 +144,7 @@ def get_universe_domain( raise EmptyUniverseError() return resolved -def should_use_client_cert() -> bool: +def should_use_client_cert() -> Optional[bool]: """Returns whether client certificate should be used for mTLS.""" if hasattr(mtls, "should_use_client_cert"): return mtls.should_use_client_cert() diff --git a/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/_compat.py b/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/_compat.py index 64f633eb0636..ddaeb608a9aa 100755 --- a/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/_compat.py +++ b/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/_compat.py @@ -148,7 +148,7 @@ def get_universe_domain( raise EmptyUniverseError() return resolved -def should_use_client_cert() -> bool: +def should_use_client_cert() -> Optional[bool]: """Returns whether client certificate should be used for mTLS.""" if hasattr(mtls, "should_use_client_cert"): return mtls.should_use_client_cert()