Skip to content

Commit

Permalink
fixed work with self-signed certificates
Browse files Browse the repository at this point in the history
  • Loading branch information
aiantsen committed Oct 26, 2023
1 parent 567649a commit f2e3e6e
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions zabbix_utils/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
# OTHER DEALINGS IN THE SOFTWARE.

import re
import ssl
import json
import base64
import logging
Expand Down Expand Up @@ -257,7 +258,7 @@ def __init__(self, url: Union[str, None] = None, token: Union[str, None] = None,
if kwargs.get('timeout'):
self.timeout = kwargs['timeout']

if kwargs.get('validate_certs'):
if 'validate_certs' in kwargs:
self.validate_certs = kwargs['validate_certs']

self._version = self.api_version()
Expand Down Expand Up @@ -482,13 +483,19 @@ def send_api_request(self, method: str, params: Union[dict, None] = None,
req.add_header('Content-Type', 'application/json-rpc')
req.add_header('User-Agent', f"{__name__}/{__version__}")
req.timeout = self.timeout
req.validate_certs = self.validate_certs

if not self.validate_certs:
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
else:
ctx = None

if self.use_basic:
req.add_header("Authorization", f"Basic {self.basic_cred}")

try:
resp = ul.urlopen(req)
resp = ul.urlopen(req, context=ctx)
resp_json = json.loads(resp.read().decode('utf-8'))
except URLError as err:
raise ProcessingException(f"Unable to connect to {self.url}:", err) from None
Expand Down

0 comments on commit f2e3e6e

Please sign in to comment.