1616
1717import json
1818import logging
19- from os import environ , path , getenv
19+ from os import environ , getenv , path
2020import re
2121import subprocess
2222
@@ -408,7 +408,8 @@ def client_cert_callback():
408408
409409
410410def 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"
0 commit comments