Skip to content

Commit

Permalink
Merge pull request #20 from evgkrsk/validate_certs
Browse files Browse the repository at this point in the history
Add support for unverified/selfsigned SSL certificates
  • Loading branch information
gescheit committed Nov 19, 2015
2 parents 2148ac9 + 4456158 commit 2a5e35c
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions zabbix/zabbix_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@
import re
from collections import deque

try:
from ssl import _create_unverified_context
HAS_SSLCONTEXT = True
except ImportError:
HAS_SSLCONTEXT = False


default_log_handler = logging.StreamHandler(sys.stdout)
__logger = logging.getLogger("zabbix_api")
__logger.addHandler(default_log_handler)
Expand Down Expand Up @@ -108,6 +115,7 @@ class ZabbixAPI(object):
httpuser = None
httppasswd = None
timeout = 10
validate_certs = None
# sub-class instances.
# Constructor Params:
# server: Server to connect to
Expand All @@ -122,7 +130,7 @@ class ZabbixAPI(object):
# **kwargs: Data to pass to each api module

def __init__(self, server='http://localhost/zabbix', user=httpuser, passwd=httppasswd,
log_level=logging.WARNING, timeout=10, r_query_len=10, **kwargs):
log_level=logging.WARNING, timeout=10, r_query_len=10, validate_certs=True, **kwargs):
""" Create an API object. """
self._setuplogging()
self.set_log_level(log_level)
Expand All @@ -136,6 +144,7 @@ def __init__(self, server='http://localhost/zabbix', user=httpuser, passwd=httpp
self.kwargs = kwargs
self.id = 0
self.r_query = deque([], maxlen=r_query_len)
self.validate_certs = validate_certs
self.debug(logging.INFO, "url: " + self.url)

def _setuplogging(self):
Expand Down Expand Up @@ -221,7 +230,10 @@ def do_request(self, json_obj):

request = urllib2.Request(url=self.url, data=json_obj.encode('utf-8'), headers=headers)
if self.proto == "https":
https_handler = urllib2.HTTPSHandler(debuglevel=0)
if HAS_SSLCONTEXT and not self.validate_certs:
https_handler = urllib2.HTTPSHandler(debuglevel=0,context=_create_unverified_context())
else:
https_handler = urllib2.HTTPSHandler(debuglevel=0)
opener = urllib2.build_opener(https_handler)
elif self.proto == "http":
http_handler = urllib2.HTTPHandler(debuglevel=0)
Expand Down

0 comments on commit 2a5e35c

Please sign in to comment.