Skip to content

Commit

Permalink
Fix SSL verify (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
pszulczewski committed Oct 17, 2023
1 parent 1c5fe6d commit 17e6bdd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
8 changes: 2 additions & 6 deletions docs/advanced/session.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ A few examples are provided below:
* :ref:`SSL Verification`
* :ref:`Timeouts`


Headers
-------

Expand All @@ -31,11 +30,9 @@ The example below shows how to update a Token if it has been cycled.
# Update Session object with new header
nautobot.http_session.headers["Authorization"] = new_token
SSL Verification
----------------

Handling SSL Verification is documented `here <https://requests.readthedocs.io/en/stable/user/advanced/#ssl-cert-verification>`_.
The below example shows how to disable SSL verification.

.. code-block:: python
Expand All @@ -44,10 +41,9 @@ The below example shows how to disable SSL verification.
from pynautobot import api
nautobot = api(
url='https://localhost:8000',
token=os.environ["NAUTOBOT_TOKEN"]
token=os.environ["NAUTOBOT_TOKEN"],
verify=False
)
nautobot.http_session.verify = False
Timeouts
--------
Expand Down
3 changes: 3 additions & 0 deletions pynautobot/core/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class Api(object):
for all requests.
:param int,optional retries: Number of retries, for HTTP codes 429, 500, 502, 503, 504,
this client will try before dropping.
:param bool,optional verify: SSL cert verification.
:raises AttributeError: If app doesn't exist.
:Examples:
Expand All @@ -80,12 +81,14 @@ def __init__(
max_workers=4,
api_version=None,
retries=0,
verify=True,
):
base_url = "{}/api".format(url if url[-1] != "/" else url[:-1])
self.token = token
self.headers = {"Authorization": f"Token {self.token}"}
self.base_url = base_url
self.http_session = requests.Session()
self.http_session.verify = verify
if retries:
_adapter = HTTPAdapter(
max_retries=Retry(
Expand Down
12 changes: 12 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ def test_sanitize_url(self, *_):
self.assertTrue(api)
self.assertEqual(api.base_url, "http://localhost:8000/api")

@patch("pynautobot.api.version", "1.999")
def test_verify_true(self, *_):
api = pynautobot.api("http://localhost:8000/", **def_kwargs)
self.assertTrue(api)
self.assertTrue(api.http_session.verify)

@patch("pynautobot.api.version", "1.999")
def test_verify_false(self, *_):
api = pynautobot.api("http://localhost:8000/", verify=False, **def_kwargs)
self.assertTrue(api)
self.assertFalse(api.http_session.verify)


class ApiVersionTestCase(unittest.TestCase):
class ResponseHeadersWithVersion:
Expand Down

0 comments on commit 17e6bdd

Please sign in to comment.