From 1345a9d89e5109e6691b53cc8b948bab34945bab Mon Sep 17 00:00:00 2001 From: "Daniel W. Anner" Date: Fri, 3 Mar 2023 12:25:25 -0500 Subject: [PATCH] Added try except block to catch ssl errors. (#74) If IGNORE_SSL_ERRORS is true, then silence SSL verification & continue. If IGNORE_SSL_ERRORS is false, then raise the error with custom message. Added verbose option to assist with reducing output and allowing for verbose logging if desired. If IGNORE_SSL_ERRORS is false, and --verbose is specified, then print the exception. --- .gitignore | 3 ++- nb-dt-import.py | 36 +++++++++++++++++++++--------------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index 6d362a2..825e176 100644 --- a/.gitignore +++ b/.gitignore @@ -102,7 +102,8 @@ celerybeat.pid *.sage.py # Environments -.env +.env* +!.env.example .venv env/ venv/ diff --git a/nb-dt-import.py b/nb-dt-import.py index 85ff7f7..f0ef8e1 100755 --- a/nb-dt-import.py +++ b/nb-dt-import.py @@ -10,6 +10,7 @@ import os import settings import sys import re +import requests counter = Counter( @@ -768,21 +769,7 @@ def main(): cwd = os.getcwd() startTime = datetime.now() - - nbUrl = settings.NETBOX_URL - nbToken = settings.NETBOX_TOKEN - nb = pynetbox.api(nbUrl, token=nbToken) - - determine_features(nb) - - if settings.IGNORE_SSL_ERRORS: - import requests - requests.packages.urllib3.disable_warnings() - session = requests.Session() - session.verify = False - nb.http_session = session - - + VENDORS = settings.VENDORS REPO_URL = settings.REPO_URL @@ -798,8 +785,27 @@ def main(): help="List of device-type slugs to import eg. ap4431 ws-c3850-24t-l") parser.add_argument('--branch', default=REPO_BRANCH, help="Git branch to use from repo") + parser.add_argument('--verbose', action='store_true', + help="Print verbose output") args = parser.parse_args() + nbUrl = settings.NETBOX_URL + nbToken = settings.NETBOX_TOKEN + nb = pynetbox.api(nbUrl, token=nbToken) + + try: + determine_features(nb) + except requests.exceptions.SSLError as e: + if args.verbose: + print(e) + if not settings.IGNORE_SSL_ERRORS: + print("IGNORE_SSL_ERRORS is False. SSL verification failed, exiting.") + sys.exit(1) + print("IGNORE_SSL_ERRORS is True, catching exception and disabling SSL verification.") + requests.packages.urllib3.disable_warnings() + nb.http_session.verify = False + determine_features(nb) + try: if os.path.isdir('./repo'): print(f"Package devicetype-library is already installed, "