Skip to content

Commit 3158f86

Browse files
committed
refactor(api-core): remove deprecated fallback client cert helpers and environment parsing
1 parent a52129a commit 3158f86

2 files changed

Lines changed: 0 additions & 231 deletions

File tree

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

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -127,88 +127,3 @@ def get_universe_domain(
127127
raise ValueError("Universe Domain cannot be an empty string.")
128128
return universe_domain
129129

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

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
@@ -22,11 +22,8 @@
2222

2323
from google.api_core.gapic_v1.client_utils import (
2424
get_api_endpoint,
25-
get_client_cert_source,
2625
get_default_mtls_endpoint,
2726
get_universe_domain,
28-
read_environment_variables,
29-
use_client_cert_effective,
3027
)
3128

3229

@@ -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)