Skip to content
This repository was archived by the owner on Mar 6, 2026. It is now read-only.

Commit 1efca42

Browse files
feat: Update the helper for use_client_cert and add support for grpc.py
Signed-off-by: Radhika Agrawal <agrawalradhika@google.com>
1 parent f86ca96 commit 1efca42

5 files changed

Lines changed: 51 additions & 47 deletions

File tree

google/auth/transport/_mtls_helper.py

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -407,13 +407,42 @@ def client_cert_callback():
407407
# Then dump the decrypted key bytes
408408
return crypto.dump_privatekey(crypto.FILETYPE_PEM, pkey)
409409

410-
def check_use_client_cert_for_workload(use_client_cert):
411-
"""Checks if the workload should use client cert for mutual TLS."""
412-
if use_client_cert == "":
410+
def check_use_client_cert():
411+
"""Returns the effective value of use_client_cert to be used.
412+
413+
Returns:
414+
str:
415+
A boolean indicating if client certificate should be used.
416+
The value is "true" or "false" or unset.
417+
If unset, the function checks if the GOOGLE_API_CERTIFICATE_CONFIG
418+
environment variable is set. If it is set, the function returns
419+
"true" if the certificate config file contains "workload" section
420+
and "false" otherwise.
421+
422+
Raises:
423+
ValueError: if GOOGLE_API_USE_CLIENT_CERTIFICATE is set to unsupported
424+
value, which is not "true" or "false".
425+
"""
426+
use_client_cert = os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE")
427+
### Check if the value of GOOGLE_API_USE_CLIENT_CERTIFICATE is unset.
428+
if use_client_cert == "" or use_client_cert is None:
413429
cert_path = os.getenv("GOOGLE_API_CERTIFICATE_CONFIG")
414430
if cert_path:
415431
with open(cert_path, "r") as f:
416432
content = f.read()
417433
if "workload" in content:
418-
return True
419-
return False
434+
return "true"
435+
return "false"
436+
else:
437+
### Check if the value of GOOGLE_API_USE_CLIENT_CERTIFICATE is set but to an
438+
### invalid value.
439+
use_client_cert = use_client_cert.lower()
440+
if use_client_cert not in ("true", "false"):
441+
raise ValueError(
442+
"Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be"
443+
" either `true` or `false`"
444+
)
445+
else:
446+
### Return the value of GOOGLE_API_USE_CLIENT_CERTIFICATE which is set.
447+
return use_client_cert
448+

google/auth/transport/grpc.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,7 @@ def my_client_cert_callback():
256256

257257
# If SSL credentials are not explicitly set, try client_cert_callback and ADC.
258258
if not ssl_credentials:
259-
use_client_cert = os.getenv(
260-
environment_vars.GOOGLE_API_USE_CLIENT_CERTIFICATE, "false"
259+
use_client_cert = _mtls_helper.check_use_client_cert()
261260
)
262261
if use_client_cert == "true" and client_cert_callback:
263262
# Use the callback if provided.
@@ -295,8 +294,7 @@ class SslCredentials:
295294
"""
296295

297296
def __init__(self):
298-
use_client_cert = os.getenv(
299-
environment_vars.GOOGLE_API_USE_CLIENT_CERTIFICATE, "false"
297+
use_client_cert = _mtls_helper.check_use_client_cert()
300298
)
301299
if use_client_cert != "true":
302300
self._is_mtls = False

google/auth/transport/requests.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -444,21 +444,10 @@ def configure_mtls_channel(self, client_cert_callback=None):
444444
google.auth.exceptions.MutualTLSChannelError: If mutual TLS channel
445445
creation failed for any reason.
446446
"""
447-
use_client_cert = os.getenv(
448-
environment_vars.GOOGLE_API_USE_CLIENT_CERTIFICATE)
449-
if use_client_cert != "true":
450-
## Checking if the GOOGLE_API_USE_CLIENT_CERTIFICATE is unset.
451-
if _mtls_helper.check_use_client_cert_for_workload(
452-
use_client_cert
453-
):
454-
os.putenv(
455-
environment_vars.GOOGLE_API_USE_CLIENT_CERTIFICATE, "true"
456-
)
457-
use_client_cert = "true"
458-
else:
459-
use_client_cert = "false"
460-
self._is_mtls = False
461-
return
447+
use_client_cert = _mtls_helper.check_use_client_cert()
448+
if use_client_cert != "true":
449+
self._is_mtls = False
450+
return
462451
try:
463452
import OpenSSL
464453
except ImportError as caught_exc:

google/auth/transport/urllib3.py

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -335,22 +335,9 @@ def configure_mtls_channel(self, client_cert_callback=None):
335335
google.auth.exceptions.MutualTLSChannelError: If mutual TLS channel
336336
creation failed for any reason.
337337
"""
338-
use_client_cert = os.getenv(
339-
environment_vars.GOOGLE_API_USE_CLIENT_CERTIFICATE)
340-
if use_client_cert != "true":
341-
## Check if workload is present in the certificate config file
342-
## and GOOGLE_API_USE_CLIENT_CERTIFICATE is unset.
343-
if _mtls_helper.check_use_client_cert_for_workload(
344-
use_client_cert
345-
):
346-
os.putenv(
347-
environment_vars.GOOGLE_API_USE_CLIENT_CERTIFICATE, "true"
348-
)
349-
use_client_cert = "true"
350-
else:
351-
use_client_cert = "false"
352-
return False
353-
338+
use_client_cert = _mtls_helper.check_use_client_cert()
339+
if use_client_cert != "true":
340+
return False
354341
try:
355342
import OpenSSL
356343
except ImportError as caught_exc:

tests/transport/test__mtls_helper.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -640,9 +640,10 @@ def test_crypto_error(self):
640640
ENCRYPTED_EC_PRIVATE_KEY, b"wrong_password"
641641
)
642642

643-
def test_check_use_client_cert_for_workload(self):
644-
use_client_cert = _mtls_helper.check_use_client_cert_for_workload("")
645-
assert use_client_cert == False
643+
def test_check_use_client_cert(self):
644+
os.environ["GOOGLE_API_USE_CLIENT_CERTIFICATE"] = "true"
645+
use_client_cert = _mtls_helper.check_use_client_cert()
646+
assert use_client_cert == "true"
646647

647648
def test_check_use_client_cert_for_workload_with_config_file(self):
648649
config_data = {
@@ -660,7 +661,7 @@ def test_check_use_client_cert_for_workload_with_config_file(self):
660661
m = mock.mock_open(read_data=config_file_content)
661662
with mock.patch("builtins.open", m):
662663
os.environ["GOOGLE_API_CERTIFICATE_CONFIG"] = config_filename
663-
use_client_cert = _mtls_helper.check_use_client_cert_for_workload(
664-
""
665-
)
666-
assert use_client_cert == True
664+
os.environ["GOOGLE_API_USE_CLIENT_CERTIFICATE"] = ""
665+
use_client_cert = _mtls_helper.check_use_client_cert()
666+
assert use_client_cert == "true"
667+

0 commit comments

Comments
 (0)