Skip to content

Commit

Permalink
Zoneminder SSL fix (#16157)
Browse files Browse the repository at this point in the history
* Update zoneminder.py

Added a verify_ssl parameter for zoneminder

* PEP8 fixup

* PEP8 indenting fix

* Fix lint issue

* Remove whitespace
  • Loading branch information
djm300 authored and fabaff committed Aug 25, 2018
1 parent 6178026 commit f929c38
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions homeassistant/components/zoneminder.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@
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
Expand All @@ -30,12 +33,12 @@
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)

Expand All @@ -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

Expand All @@ -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")
Expand All @@ -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()
Expand All @@ -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):
Expand Down

0 comments on commit f929c38

Please sign in to comment.