Skip to content

Commit

Permalink
Refactor fix_tls function to avoid silent errors
Browse files Browse the repository at this point in the history
  • Loading branch information
martialblog committed Aug 1, 2023
1 parent 847f14b commit 12a13ff
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions check_vmware_nsxt.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,22 @@
}


def fix_tls_cert_store():
def fix_tls_cert_store(cafile_path):
"""
Ensure we are using the system certstore by default
See https://github.com/psf/requests/issues/2966
Inspired by https://github.com/psf/requests/issues/2966#issuecomment-614323746
"""

try:
system_ca_store = ssl.get_default_verify_paths().cafile
if os.stat(system_ca_store).st_size > 0:
requests.utils.DEFAULT_CA_BUNDLE_PATH = system_ca_store
requests.adapters.DEFAULT_CA_BUNDLE_PATH = system_ca_store
except:
pass
# Check if we got a CA file path
if not cafile_path:
return

# If CA file contains something, set as default
if os.stat(cafile_path).st_size > 0:
requests.utils.DEFAULT_CA_BUNDLE_PATH = cafile_path
requests.adapters.DEFAULT_CA_BUNDLE_PATH = cafile_path


class CriticalException(Exception):
Expand Down Expand Up @@ -406,7 +407,7 @@ def commandline(args):


def main(args):
fix_tls_cert_store()
fix_tls_cert_store(ssl.get_default_verify_paths().cafile)

if args.insecure:
urllib3.disable_warnings()
Expand Down

0 comments on commit 12a13ff

Please sign in to comment.