Skip to content

Commit 5eda9f3

Browse files
committed
refactor(api-core): remove deprecated fallback client cert helpers and environment parsing
1 parent c5d2644 commit 5eda9f3

3 files changed

Lines changed: 1 addition & 243 deletions

File tree

packages/google-api-core/google/api_core/gapic_v1/__init__.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,14 @@
2525
# Older Python versions safely ignore this variable.
2626
__lazy_modules__: Set[str] = {
2727
"google.api_core.gapic_v1.client_info",
28-
"google.api_core.gapic_v1.client_cert",
2928
"google.api_core.gapic_v1.client_utils",
30-
"google.api_core.gapic_v1.config_helpers",
3129
"google.api_core.gapic_v1.requests",
32-
"google.api_core.gapic_v1.client_utils",
3330
"google.api_core.gapic_v1.routing_header",
3431
}
3532
__all__ = [
3633
"client_info",
37-
"client_cert",
3834
"client_utils",
39-
"config_helpers",
4035
"requests",
41-
"client_utils",
4236
"routing_header",
4337
]
4438

@@ -54,11 +48,8 @@
5448

5549
from google.api_core.gapic_v1 import ( # noqa: E402
5650
client_info,
57-
client_cert,
5851
client_utils,
59-
config_helpers,
6052
requests,
61-
client_utils,
6253
routing_header,
6354
)
6455

packages/google-api-core/google/api_core/gapic_v1/client_utils.py

Lines changed: 1 addition & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@
1313
# limitations under the License.
1414
#
1515

16-
import os
17-
from typing import Any, Callable, Optional, Tuple
16+
from typing import Optional
1817
from urllib.parse import urlparse, urlunparse
1918

2019
from google.auth.exceptions import MutualTLSChannelError # type: ignore
21-
from google.auth.transport import mtls # type: ignore
2220

2321

2422
def get_default_mtls_endpoint(api_endpoint: Optional[str]) -> Optional[str]:
@@ -136,88 +134,3 @@ def get_universe_domain(
136134
return resolved
137135

138136

139-
def use_client_cert_effective() -> bool:
140-
"""Returns whether client certificate should be used for mTLS if the
141-
google-auth version supports should_use_client_cert automatic mTLS
142-
enablement.
143-
144-
Alternatively, read from the GOOGLE_API_USE_CLIENT_CERTIFICATE env var.
145-
146-
Returns:
147-
bool: whether client certificate should be used for mTLS
148-
Raises:
149-
ValueError: (If using a version of google-auth without
150-
should_use_client_cert and GOOGLE_API_USE_CLIENT_CERTIFICATE is
151-
set to an unexpected value.)
152-
"""
153-
# check if google-auth version supports should_use_client_cert for
154-
# automatic mTLS enablement
155-
if hasattr(mtls, "should_use_client_cert"): # pragma: NO COVER
156-
return mtls.should_use_client_cert()
157-
else: # pragma: NO COVER
158-
# if unsupported, fallback to reading from env var
159-
use_client_cert_str = os.getenv(
160-
"GOOGLE_API_USE_CLIENT_CERTIFICATE", "false"
161-
).lower()
162-
if use_client_cert_str not in ("true", "false"):
163-
raise ValueError(
164-
"Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` "
165-
"must be either `true` or `false`"
166-
)
167-
return use_client_cert_str == "true"
168-
169-
170-
def get_client_cert_source(
171-
provided_cert_source: Optional[Callable[[], Tuple[bytes, bytes]]],
172-
use_cert_flag: bool,
173-
) -> Optional[Callable[[], Tuple[bytes, bytes]]]:
174-
"""Return the client cert source to be used by the client.
175-
176-
Args:
177-
provided_cert_source (Callable[[], Tuple[bytes, bytes]]): The client certificate source provided.
178-
use_cert_flag (bool): A flag indicating whether to use the
179-
client certificate.
180-
181-
Returns:
182-
Callable[[], Tuple[bytes, bytes]] or None: The client cert source to be used by the client.
183-
"""
184-
if use_cert_flag:
185-
if provided_cert_source:
186-
return provided_cert_source
187-
elif (
188-
hasattr(mtls, "has_default_client_cert_source")
189-
and mtls.has_default_client_cert_source()
190-
):
191-
return mtls.default_client_cert_source()
192-
else:
193-
raise ValueError(
194-
"Client certificate is required for mTLS, but no client certificate source was provided or found."
195-
)
196-
return None
197-
198-
199-
def read_environment_variables() -> Tuple[bool, str, Optional[str]]:
200-
"""Returns the environment variables used by the client.
201-
202-
Returns:
203-
Tuple[bool, str, Optional[str]]: returns the
204-
GOOGLE_API_USE_CLIENT_CERTIFICATE, GOOGLE_API_USE_MTLS_ENDPOINT,
205-
and GOOGLE_CLOUD_UNIVERSE_DOMAIN environment variables.
206-
207-
Raises:
208-
ValueError: If GOOGLE_API_USE_CLIENT_CERTIFICATE is not
209-
any of ["true", "false"].
210-
google.auth.exceptions.MutualTLSChannelError: If
211-
GOOGLE_API_USE_MTLS_ENDPOINT is not any of
212-
["auto", "never", "always"].
213-
"""
214-
use_client_cert = use_client_cert_effective()
215-
use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower()
216-
universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN")
217-
if use_mtls_endpoint not in ("auto", "never", "always"):
218-
raise MutualTLSChannelError(
219-
"Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` "
220-
"must be `never`, `auto` or `always`"
221-
)
222-
return use_client_cert, use_mtls_endpoint, universe_domain_env
223-

packages/google-api-core/tests/unit/gapic/test_client_utils.py

Lines changed: 0 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@
2020

2121
from google.api_core.gapic_v1.client_utils import (
2222
get_api_endpoint,
23-
get_client_cert_source,
2423
get_default_mtls_endpoint,
2524
get_universe_domain,
26-
read_environment_variables,
27-
use_client_cert_effective,
2825
)
2926

3027

@@ -205,147 +202,4 @@ def test_get_universe_domain():
205202
assert str(excinfo.value) == "Universe Domain cannot be an empty string."
206203

207204

208-
@pytest.mark.parametrize(
209-
"has_method, method_val, env_val, expected",
210-
[
211-
# should_use_client_cert is available cases
212-
(True, True, None, "true"),
213-
(True, False, None, "false"),
214-
(True, False, "unsupported", "false"),
215-
# should_use_client_cert is unavailable cases
216-
(False, None, "true", "true"),
217-
(False, None, "false", "false"),
218-
(False, None, "True", "true"),
219-
(False, None, "False", "false"),
220-
(False, None, "TRUE", "true"),
221-
(False, None, "FALSE", "false"),
222-
(False, None, None, "false"),
223-
(False, None, "unsupported", "value_error"),
224-
],
225-
ids=[
226-
"google_auth_true",
227-
"google_auth_false",
228-
"google_auth_false_env_unsupported",
229-
"fallback_env_true_lowercase",
230-
"fallback_env_false_lowercase",
231-
"fallback_env_true_titlecase",
232-
"fallback_env_false_titlecase",
233-
"fallback_env_true_uppercase",
234-
"fallback_env_false_uppercase",
235-
"fallback_env_unset",
236-
"fallback_env_unsupported",
237-
],
238-
)
239-
def test_use_client_cert_effective(has_method, method_val, env_val, expected):
240-
# Mock hasattr to control whether should_use_client_cert exists
241-
original_hasattr = hasattr
242-
243-
def custom_hasattr(obj, name):
244-
if obj is mtls and name == "should_use_client_cert":
245-
return has_method
246-
return original_hasattr(obj, name)
247-
248-
with mock.patch("google.api_core.gapic_v1.client_utils.hasattr", custom_hasattr):
249-
with mock.patch(
250-
"google.auth.transport.mtls.should_use_client_cert",
251-
create=True,
252-
return_value=method_val,
253-
):
254-
env = {}
255-
if env_val is not None:
256-
env["GOOGLE_API_USE_CLIENT_CERTIFICATE"] = env_val
257-
with mock.patch.dict(os.environ, env, clear=True):
258-
if expected == "value_error":
259-
with pytest.raises(
260-
ValueError, match="must be either `true` or `false`"
261-
):
262-
use_client_cert_effective()
263-
else:
264-
assert use_client_cert_effective() is (expected == "true")
265-
266-
267-
@pytest.mark.parametrize(
268-
"provided, use_cert, has_default_avail, default_val, expected",
269-
[
270-
(None, False, True, b"default", None),
271-
(b"provided", False, True, b"default", None),
272-
(b"provided", True, True, b"default", b"provided"),
273-
(None, True, True, b"default", b"default"),
274-
(None, True, False, b"default", "value_error"),
275-
],
276-
ids=[
277-
"use_cert_false_no_provided",
278-
"use_cert_false_with_provided",
279-
"use_cert_true_with_provided",
280-
"use_cert_true_no_provided_default_avail",
281-
"use_cert_true_no_provided_default_unavail",
282-
],
283-
)
284-
def test_get_client_cert_source(
285-
provided, use_cert, has_default_avail, default_val, expected
286-
):
287-
original_hasattr = hasattr
288-
289-
def custom_hasattr(obj, name):
290-
if obj is mtls and name == "has_default_client_cert_source":
291-
return has_default_avail
292-
return original_hasattr(obj, name)
293-
294-
with mock.patch("google.api_core.gapic_v1.client_utils.hasattr", custom_hasattr):
295-
with mock.patch(
296-
"google.auth.transport.mtls.has_default_client_cert_source",
297-
create=True,
298-
return_value=has_default_avail,
299-
):
300-
with mock.patch(
301-
"google.auth.transport.mtls.default_client_cert_source",
302-
create=True,
303-
return_value=default_val,
304-
):
305-
if expected == "value_error":
306-
with pytest.raises(
307-
ValueError, match="Client certificate is required for mTLS"
308-
):
309-
get_client_cert_source(provided, use_cert)
310-
else:
311-
assert get_client_cert_source(provided, use_cert) == expected
312205

313-
314-
@pytest.mark.parametrize(
315-
"env, mock_cert_val, expected",
316-
[
317-
({}, False, (False, "auto", None)),
318-
({}, True, (True, "auto", None)),
319-
({"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}, False, (False, "never", None)),
320-
({"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}, False, (False, "always", None)),
321-
({"GOOGLE_API_USE_MTLS_ENDPOINT": "auto"}, False, (False, "auto", None)),
322-
({"GOOGLE_API_USE_MTLS_ENDPOINT": "invalid"}, False, "mutual_tls_error"),
323-
(
324-
{"GOOGLE_CLOUD_UNIVERSE_DOMAIN": "foo.com"},
325-
False,
326-
(False, "auto", "foo.com"),
327-
),
328-
],
329-
ids=[
330-
"default_env",
331-
"client_cert_true",
332-
"mtls_never",
333-
"mtls_always",
334-
"mtls_auto",
335-
"mtls_invalid",
336-
"universe_domain",
337-
],
338-
)
339-
def test_read_environment_variables(env, mock_cert_val, expected):
340-
with mock.patch(
341-
"google.api_core.gapic_v1.client_utils.use_client_cert_effective",
342-
return_value=mock_cert_val,
343-
):
344-
with mock.patch.dict(os.environ, env, clear=True):
345-
if expected == "mutual_tls_error":
346-
with pytest.raises(
347-
MutualTLSChannelError, match="must be `never`, `auto` or `always`"
348-
):
349-
read_environment_variables()
350-
else:
351-
assert read_environment_variables() == expected

0 commit comments

Comments
 (0)