Skip to content

Commit 2a0a71a

Browse files
fix: Update the dosctring and refine the try-catch blocks
Signed-off-by: Radhika Agrawal <agrawalradhika@google.com>
1 parent 09b6a1e commit 2a0a71a

File tree

3 files changed

+18
-25
lines changed

3 files changed

+18
-25
lines changed

google/auth/transport/_mtls_helper.py

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import json
1818
import logging
19-
from os import environ, path, getenv
19+
from os import environ, getenv, path
2020
import re
2121
import subprocess
2222

@@ -408,7 +408,8 @@ def client_cert_callback():
408408

409409

410410
def check_use_client_cert():
411-
"""Returns whether the client certificate should to be used for mTLS.
411+
"""Returns the value of the GOOGLE_API_USE_CLIENT_CERTIFICATE variable,
412+
or an inferred 'true' or 'false' value if unset.
412413
413414
The function checks the value of GOOGLE_API_USE_CLIENT_CERTIFICATE
414415
environment variable, and GOOGLE_API_CERTIFICATE_CONFIG environment variable
@@ -419,8 +420,9 @@ def check_use_client_cert():
419420
"workload" section and "false" otherwise.
420421
421422
Returns:
422-
str: A string("true" or "false") indicating if client certificate should
423-
be used.
423+
str: A string("true" or "false" or value of the
424+
GOOGLE_API_USE_CLIENT_CERTIFICATE variable set) indicating if client
425+
certificate should be used.
424426
"""
425427
use_client_cert = getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE")
426428
# Check if the value of GOOGLE_API_USE_CLIENT_CERTIFICATE is set.
@@ -433,25 +435,16 @@ def check_use_client_cert():
433435
try:
434436
with open(cert_path, "r") as f:
435437
content = json.load(f)
436-
except json.JSONDecodeError:
437-
_LOGGER.debug("JSON decode error.")
438-
return "false"
439-
except FileNotFoundError:
440-
_LOGGER.debug("Certificate config file not found.")
441-
return "false"
442-
except OSError:
443-
_LOGGER.debug("OS error.")
444-
return "false"
445-
try:
446-
if content["cert_configs"]["workload"]:
438+
# verify json has workload key
439+
content["cert_configs"]["workload"]
447440
return "true"
448-
except KeyError:
449-
_LOGGER.debug(
450-
"Certificate config file content does not contain 'workload'"
451-
" section in 'cert_configs'."
452-
)
453-
return "false"
454-
except TypeError:
455-
_LOGGER.debug("Certificate config file content is not a JSON object.")
441+
except (
442+
FileNotFoundError,
443+
OSError,
444+
KeyError,
445+
TypeError,
446+
json.JSONDecodeError,
447+
) as e:
448+
_LOGGER.debug("error decoding certificate: %s", e)
456449
return "false"
457450
return "false"

google/auth/transport/requests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ 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 = _mtls_helper.check_use_client_cert()
447+
use_client_cert = google.auth.transport._mtls_helper.check_use_client_cert()
448448
if use_client_cert != "true":
449449
self._is_mtls = False
450450
return

google/auth/transport/urllib3.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ 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 = _mtls_helper.check_use_client_cert()
338+
use_client_cert = transport._mtls_helper.check_use_client_cert()
339339
if use_client_cert != "true":
340340
return False
341341
try:

0 commit comments

Comments
 (0)