From f929c38e980d728bc74055043310bdc4c24211a1 Mon Sep 17 00:00:00 2001 From: djm300 Date: Sat, 25 Aug 2018 11:21:57 +0200 Subject: [PATCH] Zoneminder SSL fix (#16157) * Update zoneminder.py Added a verify_ssl parameter for zoneminder * PEP8 fixup * PEP8 indenting fix * Fix lint issue * Remove whitespace --- homeassistant/components/zoneminder.py | 28 +++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/zoneminder.py b/homeassistant/components/zoneminder.py index 471c1c6e82c..5c045544456 100644 --- a/homeassistant/components/zoneminder.py +++ b/homeassistant/components/zoneminder.py @@ -11,16 +11,19 @@ import requests import voluptuous as vol from homeassistant.const import ( - CONF_PATH, CONF_HOST, CONF_SSL, CONF_PASSWORD, CONF_USERNAME) + CONF_HOST, CONF_PASSWORD, CONF_PATH, CONF_SSL, CONF_USERNAME, + CONF_VERIFY_SSL) import homeassistant.helpers.config_validation as cv _LOGGER = logging.getLogger(__name__) CONF_PATH_ZMS = 'path_zms' + DEFAULT_PATH = '/zm/' DEFAULT_PATH_ZMS = '/zm/cgi-bin/nph-zms' DEFAULT_SSL = False DEFAULT_TIMEOUT = 10 +DEFAULT_VERIFY_SSL = True DOMAIN = 'zoneminder' LOGIN_RETRIES = 2 @@ -30,12 +33,12 @@ ZM = {} CONFIG_SCHEMA = vol.Schema({ DOMAIN: vol.Schema({ vol.Required(CONF_HOST): cv.string, - vol.Optional(CONF_SSL, default=DEFAULT_SSL): cv.boolean, + vol.Optional(CONF_PASSWORD): cv.string, vol.Optional(CONF_PATH, default=DEFAULT_PATH): cv.string, - # This should match PATH_ZMS in ZoneMinder settings. vol.Optional(CONF_PATH_ZMS, default=DEFAULT_PATH_ZMS): cv.string, + vol.Optional(CONF_SSL, default=DEFAULT_SSL): cv.boolean, vol.Optional(CONF_USERNAME): cv.string, - vol.Optional(CONF_PASSWORD): cv.string + vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean, }) }, extra=vol.ALLOW_EXTRA) @@ -56,11 +59,14 @@ def setup(hass, config): username = conf.get(CONF_USERNAME, None) password = conf.get(CONF_PASSWORD, None) + ssl_verification = conf.get(CONF_VERIFY_SSL) + ZM['server_origin'] = server_origin ZM['url'] = url ZM['username'] = username ZM['password'] = password ZM['path_zms'] = conf.get(CONF_PATH_ZMS) + ZM['ssl_verification'] = ssl_verification hass.data[DOMAIN] = ZM @@ -77,14 +83,16 @@ def login(): if ZM['password']: login_post['password'] = ZM['password'] - req = requests.post(ZM['url'] + '/index.php', data=login_post) + req = requests.post(ZM['url'] + '/index.php', data=login_post, + verify=ZM['ssl_verification'], timeout=DEFAULT_TIMEOUT) + ZM['cookies'] = req.cookies # Login calls returns a 200 response on both failure and success. # The only way to tell if you logged in correctly is to issue an api call. req = requests.get( ZM['url'] + 'api/host/getVersion.json', cookies=ZM['cookies'], - timeout=DEFAULT_TIMEOUT) + timeout=DEFAULT_TIMEOUT, verify=ZM['ssl_verification']) if not req.ok: _LOGGER.error("Connection error logging into ZoneMinder") @@ -100,7 +108,8 @@ def _zm_request(method, api_url, data=None): for _ in range(LOGIN_RETRIES): req = requests.request( method, urljoin(ZM['url'], api_url), data=data, - cookies=ZM['cookies'], timeout=DEFAULT_TIMEOUT) + cookies=ZM['cookies'], timeout=DEFAULT_TIMEOUT, + verify=ZM['ssl_verification']) if not req.ok: login() @@ -113,8 +122,9 @@ def _zm_request(method, api_url, data=None): try: return req.json() except ValueError: - _LOGGER.exception('JSON decode exception caught while attempting to ' - 'decode "%s"', req.text) + _LOGGER.exception( + "JSON decode exception caught while attempting to decode: %s", + req.text) def get_state(api_url):