Module: check_mk
Branch: master
Commit: 3e72fbcb6ad07eff4464e940f23631601b5486c6
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=3e72fbcb6ad07e…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Mon May 14 17:14:39 2012 +0200
Better error handling in D/WATO Login
---
web/htdocs/htmllib.py | 11 +++++++++++
web/htdocs/index.py | 38 +++++++++++++++++++++++++++++---------
web/htdocs/login.py | 9 ++++-----
web/htdocs/wato.py | 28 ++++++++++++++++++++--------
4 files changed, 64 insertions(+), 22 deletions(-)
diff --git a/web/htdocs/htmllib.py b/web/htdocs/htmllib.py
index 5a130c3..1106a4c 100644
--- a/web/htdocs/htmllib.py
+++ b/web/htdocs/htmllib.py
@@ -154,6 +154,17 @@ def strip_tags(ht):
ht = ht[0:x] + ht[y+1:]
return ht
+def strip_scripts(ht):
+ while True:
+ x = ht.find('<script')
+ if x == -1:
+ break
+ y = ht.find('</script>')
+ if y == -1:
+ break
+ ht = ht[0:x] + ht[y+9:]
+ return ht
+
class html:
def __init__(self, req):
diff --git a/web/htdocs/index.py b/web/htdocs/index.py
index 19f6074..26e991e 100644
--- a/web/htdocs/index.py
+++ b/web/htdocs/index.py
@@ -206,6 +206,10 @@ def handler(req, profiling = True):
# just a plain server result code of 500
fail_silently = html.has_var("_ajaxid")
+ # Webservice functions may decide to get a normal result code
+ # but a text with an error message in case of an error
+ plain_error = html.has_var("_plain_error")
+
config.load_config() # load multisite.mk
if html.var("debug"): # Debug flag may be set via URL
config.debug = True
@@ -268,7 +272,7 @@ def handler(req, profiling = True):
load_language(html.var("lang", config.get_language()))
# After auth check the regular page can be shown
- result = login.page_login()
+ result = login.page_login(plain_error)
if type(result) == tuple:
# This is the redirect to the requested page directly after
successful login
req.user = result[0]
@@ -312,40 +316,52 @@ def handler(req, profiling = True):
handler()
except MKUserError, e:
- if not fail_silently:
+ if plain_error:
+ html.write(_("User error") + ": %s\n" % e)
+ elif not fail_silently:
html.header("Invalid User Input")
html.show_error(str(e))
html.footer()
except MKAuthException, e:
- if not fail_silently:
+ if plain_error:
+ html.write(_("Authentication error") + ": %s\n" % e)
+ elif not fail_silently:
html.header(_("Permission denied"))
html.show_error(str(e))
html.footer()
except MKUnauthenticatedException, e:
- if not fail_silently:
+ if plain_error:
+ html.write(_("Missing authentication credentials") + ":
%s\n" % e)
+ elif not fail_silently:
html.header(_("Not authenticated"))
html.show_error(str(e))
html.footer()
response_code = apache.HTTP_UNAUTHORIZED
except MKConfigError, e:
- if not fail_silently:
+ if plain_error:
+ html.write(_("Configuration error") + ": %s\n" % e)
+ elif not fail_silently:
html.header(_("Configuration Error"))
html.show_error(str(e))
html.footer()
apache.log_error(_("Configuration error: %s") % (e,),
apache.APLOG_ERR)
except MKGeneralException, e:
- if not fail_silently:
+ if plain_error:
+ html.write(_("General error") + ": %s\n" % e)
+ elif not fail_silently:
html.header(_("Error"))
html.show_error(str(e))
html.footer()
apache.log_error(_("Error: %s") % (e,), apache.APLOG_ERR)
except livestatus.MKLivestatusNotFoundError, e:
- if not fail_silently:
+ if plain_error:
+ html.write(_("Livestatus-data not found") + ": %s\n" %
e)
+ elif not fail_silently:
html.header(_("Data not found"))
html.show_error(_("The following query produced no
output:\n<pre>\n%s</pre>\n") % \
e.query)
@@ -353,7 +369,9 @@ def handler(req, profiling = True):
response_code = apache.HTTP_NOT_FOUND
except livestatus.MKLivestatusException, e:
- if not fail_silently:
+ if plain_error:
+ html.write(_("Livestatus problem") + ": %s\n" % e)
+ elif not fail_silently:
html.header(_("Livestatus problem"))
html.show_error(_("Livestatus problem: %s") % e)
html.footer()
@@ -364,7 +382,9 @@ def handler(req, profiling = True):
raise
except Exception, e:
- if not fail_silently:
+ if plain_error:
+ html.write(_("Internal error") + ": %s\n" % e)
+ elif not fail_silently:
html.header(_("Internal error"))
if config.debug:
html.show_error("%s: %s<pre>%s</pre>" %
diff --git a/web/htdocs/login.py b/web/htdocs/login.py
index 0035894..fb9eacb 100644
--- a/web/htdocs/login.py
+++ b/web/htdocs/login.py
@@ -29,9 +29,6 @@ from lib import *
from mod_python import apache
import os, md5, md5crypt, crypt, time
-class MKAuthException(MKGeneralException):
- pass
-
def site_cookie_name(site_id = None):
if not site_id:
url_prefix = defaults.url_prefix
@@ -193,10 +190,12 @@ def do_login():
html.add_user_error(e.varname, e.message)
return e.message
-def page_login():
+def page_login(no_html_output = False):
result = do_login()
if type(result) == tuple:
- return result # Successfull login
+ return result # Successful login
+ elif no_html_output:
+ raise MKAuthException(_("Invalid login credentials."))
if html.mobile:
import mobile
diff --git a/web/htdocs/wato.py b/web/htdocs/wato.py
index 8e96ce8..a5c7731 100644
--- a/web/htdocs/wato.py
+++ b/web/htdocs/wato.py
@@ -6718,14 +6718,26 @@ def do_site_login(site_id, name, password):
raise MKUserError("_passwd",
_("Please specify your password."))
- # Trying basic auth AND form based auth to ensure the site login works
- response = get_url(site["multisiteurl"] +
'automation_login.py?_login=1'
-
'&_username=%s&_password=%s&_origtarget=automation_login.py' %
- (name, password),
site.get('insecure', False), name, password)
- try:
- return eval(response)
- except:
- raise MKAutomationException(response)
+ # Trying basic auth AND form based auth to ensure the site login works.
+ # Adding _ajaxid makes the web service fail silently with an HTTP code and
+ # not output HTML code for an error screen.
+ url = site["multisiteurl"] + 'automation_login.py?_login=1' \
+
'&_username=%s&_password=%s&_origtarget=automation_login.py&_plain_error=1'
% \
+ (name, password)
+ response = get_url(url, site.get('insecure', False), name, password).strip()
+ if '<html>' in response.lower():
+ message = _("Authentication to web service
failed.<br>Message:<br>%s") % \
+ htmllib.strip_tags(htmllib.strip_scripts(response))
+ if config.debug:
+ message += "<br>Automation URL:
<tt>%s</tt><br>" % url
+ raise MKAutomationException(message)
+ elif not response:
+ raise MKAutomationException(_("Empty response from web service"))
+ else:
+ try:
+ return eval(response)
+ except:
+ raise MKAutomationException(response)
def upload_file(url, file_path, insecure):
return get_url(url, insecure, params = ' -F snapshot=@%s' % file_path)