Improve logging system: add a uuid and use a specific Mandaye logger

* README.rst: typo
 * mandaye/__init__.py: move logging part to log.py
 * mandaye/auth/authform.py: replace logging by mandaye logger and
   check if pycrpto is installed
 * mandaye/auth/vincennes.py: replace logging by mandaye logger
 * mandaye/config.py: improve comment on encryption
 * mandaye/dispatcher.py: replace logging by mandaye logger and
   add new debug
 * mandaye/emails.py: replace logging by mandaye logger
 * mandaye/filters/vincennes.py: replace logging by mandaye logger
 * mandaye/http.py: replace logging by mandaye logger
 * mandaye/log.py: new file to manage logging
 * mandaye/response.py: replace logging by mandaye logger and
   add debug informations
 * mandaye/server.py: replace logging by mandaye logger and
    add an uuid for the log
 * mandaye_admin.py: replace logging by mandaye logger
 * mandaye_server.py: replace logging by mandaye logger and fix Python
   path
This commit is contained in:
Jérôme Schneider 2011-12-23 11:01:03 +01:00
parent 417b837019
commit 0aaa601bbb
14 changed files with 149 additions and 104 deletions

View File

@ -98,7 +98,7 @@ Launch mandaye server:::
mandaye_server.py use gunicorn and gunicorn options (please read http://gunicorn.org/configure.html)
You could alse use gunicorn.conf.py-sample (in the mandaye files)::
You could also use gunicorn.conf.py-sample (in the mandaye files)::
$ mandaye_server.py -c PATH_TO_THE_FILE/gunicorn.conf.py

View File

@ -1,28 +1 @@
VERSION=0.2
import logging
from logging import FileHandler
from logging.handlers import SysLogHandler
from mandaye.config import log_level, syslog, log_file
logger = logging.getLogger()
logger.setLevel(log_level)
# Stream logging
sh = logging.StreamHandler()
sh.setFormatter(logging.Formatter('%(levelname)s %(message)s'))
logger.addHandler(sh)
# Syslog logging
if syslog:
syslog_handler = SysLogHandler(address='/dev/log')
syslog_handler.setFormatter(logging.Formatter('mandaye: %(levelname)s %(message)s'))
logger.addHandler(syslog_handler)
# File logging
if log_file:
filehandler = FileHandler(log_file)
filehandler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'))
logger.addHandler(filehandler)

View File

@ -3,7 +3,6 @@ Dispatcher for basic auth form authentifications
"""
import Cookie
import base64
import logging
import re
import traceback
import urllib
@ -11,7 +10,6 @@ import urllib
import mandaye
from cookielib import CookieJar
from Crypto.Cipher import AES
from datetime import datetime
from lxml.html import fromstring
from urlparse import parse_qs
@ -19,11 +17,16 @@ from urlparse import parse_qs
from mandaye import config
from mandaye.db import sql_session
from mandaye.models import Site, ExtUser, LocalUser
from mandaye.log import logger
from mandaye.http import HTTPResponse, HTTPHeader, HTTPRequest
from mandaye.response import _500, _302, _401
from mandaye.response import template_response
from mandaye.server import get_response
try:
from Crypto.Cipher import AES
except ImportError:
config.encrypt_ext_password = False
class AuthForm(object):
@ -49,7 +52,7 @@ class AuthForm(object):
not form_values.has_key('form_attrs') or \
not form_values.has_key('username_field') or \
not form_values.has_key('password_field'):
logging.critical("Bad configuration: AuthForm form_values dict must have \
logger.critical("Bad configuration: AuthForm form_values dict must have \
this keys: form_url, form_attrs, username_field and password_field")
# TODO: manage Mandaye exceptions
raise BaseException, 'AuthForm bad configuration'
@ -65,7 +68,7 @@ this keys: form_url, form_attrs, username_field and password_field")
pwd: the password you want to encrypt
return None if encryption failed
"""
logging.debug("Encrypt password")
logger.debug("Encrypt password")
enc_pwd = pwd
if config.encrypt_secret:
try:
@ -75,9 +78,9 @@ this keys: form_url, form_attrs, username_field and password_field")
except Exception, e:
if config.debug:
traceback.print_exc()
logging.warning('Password encrypting failed %s' % e)
logger.warning('Password encrypting failed %s' % e)
else:
logging.warning("You must set a secret to use pwd encryption")
logger.warning("You must set a secret to use pwd encryption")
return enc_pwd
def _decrypt_pwd(self, enc_pwd):
@ -88,7 +91,7 @@ this keys: form_url, form_attrs, username_field and password_field")
enc_pwd: your encoded password
return None if encryption failed
"""
logging.debug("Decrypt password")
logger.debug("Decrypt password")
pwd = enc_pwd
if config.encrypt_secret:
try:
@ -98,9 +101,9 @@ this keys: form_url, form_attrs, username_field and password_field")
except Exception, e:
if config.debug:
traceback.print_exc()
logging.warning('Decrypting password failed: %s' % e)
logger.warning('Decrypting password failed: %s' % e)
else:
logging.warning("You must set a secret to use pwd decryption")
logger.warning("You must set a secret to use pwd decryption")
return pwd
def replay(self, env, username, password, extra_values={}):
@ -127,13 +130,13 @@ this keys: form_url, form_attrs, username_field and password_field")
break
if auth_form == None:
logging.critical("%s %s: can't find login form on %s" %
logger.critical("%s %s: can't find login form on %s" %
(env['HTTP_HOST'], env['PATH_INFO'], self.form_url))
return _500(env['PATH_INFO'], "Replay: Can't find login form")
if not self.form_values.has_key('from_action'):
if not auth_form.action:
logging.critical("%s %s: don't find form action on %s" %
logger.critical("%s %s: don't find form action on %s" %
(env['HTTP_HOST'], env['PATH_INFO'], self.form_url))
return _500(env['PATH_INFO'], 'Replay: form action not found')
action = auth_form.action
@ -171,13 +174,13 @@ this keys: form_url, form_attrs, username_field and password_field")
site = sql_session().query(Site).\
filter_by(name=self.site_name).first()
if not site:
logging.info('Add %s site in the database' % self.site_name)
logger.info('Add %s site in the database' % self.site_name)
site = Site(self.site_name)
sql_session().add(site)
local_user = sql_session().query(LocalUser).\
filter_by(login=local_login).first()
if not local_user:
logging.debug('Add user %s in the database' % local_login)
logger.debug('Add user %s in the database' % local_login)
local_user = LocalUser(login=local_login)
sql_session().add(local_user)
ext_user = sql_session().query(ExtUser).\
@ -188,7 +191,7 @@ this keys: form_url, form_attrs, username_field and password_field")
if not ext_user:
ext_user = ExtUser()
sql_session().add(ext_user)
logging.info('New association: %s with %s on site %s' % \
logger.info('New association: %s with %s on site %s' % \
(ext_username, local_login, self.site_name))
ext_user.login = ext_username
if config.encrypt_ext_password:
@ -208,18 +211,18 @@ this keys: form_url, form_attrs, username_field and password_field")
def associate_submit(self, env, values, condition, request, response):
""" Associate your login / password into your database
"""
logging.debug("Trying to associate a user")
logger.debug("Trying to associate a user")
login = env['beaker.session'].get('login')
if request.msg:
if not login:
logging.warning("Association failed: user isn't login on Mandaye")
logger.warning("Association failed: user isn't login on Mandaye")
return _302(values.get('connection_url'))
post = parse_qs(request.msg.read(), request)
qs = parse_qs(env['QUERY_STRING'])
for key, value in qs.iteritems():
qs[key] = value[0]
if not post.has_key('username') or not post.has_key('password'):
logging.info('Association auth failed: form not correctly filled')
logger.info('Association auth failed: form not correctly filled')
qs['type'] = 'badlogin'
return _302(values.get('associate_url') + "?%s" % urllib.urlencode(qs))
username = post['username'][0]
@ -234,12 +237,12 @@ this keys: form_url, form_attrs, username_field and password_field")
response = self.replay(env, username,
post['password'][0], extra_values)
if eval(condition):
logging.debug("Replay works: save the association")
logger.debug("Replay works: save the association")
self._save_association(env, login, username, post['password'][0], birthdate)
if qs.has_key('next_url'):
return _302(qs['next_url'], response.cookies)
return response
logging.info('Auth failed: Bad password or login for %s on %s' % \
logger.info('Auth failed: Bad password or login for %s on %s' % \
(post['username'][0], self.site_name))
qs['type'] = 'badlogin'
return _302(values.get('associate_url') + "?%s" % urllib.urlencode(qs))
@ -272,7 +275,7 @@ this keys: form_url, form_attrs, username_field and password_field")
def login(self, env, values, condition, request, response):
""" Automatic login on a site with a form
"""
logging.debug('Trying to login on Mandaye')
logger.debug('Trying to login on Mandaye')
login = self.get_current_login(env)
if not login:
return _401('Access denied: invalid token')
@ -283,7 +286,7 @@ this keys: form_url, form_attrs, username_field and password_field")
env['beaker.session']['login'] = login
env['beaker.session'].save()
logging.debug('User %s successfully login' % env['beaker.session']['login'])
logger.debug('User %s successfully login' % env['beaker.session']['login'])
ext_user = sql_session().query(ExtUser).\
join(LocalUser).\
@ -293,14 +296,14 @@ this keys: form_url, form_attrs, username_field and password_field")
order_by(ExtUser.last_connection.desc()).\
first()
if not ext_user:
logging.debug('User %s is not associate' % env['beaker.session']['login'])
logger.debug('User %s is not associate' % env['beaker.session']['login'])
return _302(values.get('associate_url') + "?type=first")
return self._login_ext_user(ext_user, env, condition, values)
def logout(self, env, values, request, response):
""" Destroy the Beaker session
"""
logging.debug('Logout from Mandaye')
logger.debug('Logout from Mandaye')
env['beaker.session'].delete()
return response
@ -345,7 +348,7 @@ this keys: form_url, form_attrs, username_field and password_field")
filter(ExtUser.id==id).\
first()
if ext_user:
logging.debug('Disassociate account %s' % ext_user.login)
logger.debug('Disassociate account %s' % ext_user.login)
sql_session().delete(ext_user)
sql_session().commit()
if qs.has_key('logout'):

View File

@ -1,7 +1,6 @@
""" Vincennes authentifications
"""
import base64
import logging
import traceback
from Crypto.Cipher import AES
@ -9,6 +8,7 @@ from datetime import datetime, timedelta
from urlparse import parse_qs
from mandaye.auth.authform import AuthForm
from mandaye.log import logger
from mandaye.models import Site, ExtUser, LocalUser
from mandaye.db import sql_session
from mandaye.response import _502, _302
@ -54,7 +54,7 @@ class VincennesAuth(AuthForm):
except Exception, e:
if config.debug:
traceback.print_exc()
logging.info("Invalid token %s" % e)
logger.info("Invalid token %s" % e)
return None
else:
info = eval(decode[16:])
@ -64,7 +64,7 @@ class VincennesAuth(AuthForm):
delta = datetime.utcnow() - token_date
timeout = timedelta(seconds=config.token_timeout)
if abs(delta) > timeout:
logging.warning('Vincennes token timeout (delta of %s seconds)' \
logger.warning('Vincennes token timeout (delta of %s seconds)' \
% abs(delta))
return None
return info['pseudo']
@ -82,15 +82,15 @@ class VincennesAuth(AuthForm):
def auto_login(self, env, values, condition, request, response):
""" Try to auto login the user if he is already login on www.vincennes.fr
"""
logging.debug('Trying to auto log user on %s' % self.site_name)
logger.debug('Trying to auto log user on %s' % self.site_name)
login = self.get_current_login(env)
if env['beaker.session'].has_key('next_url'):
path = env['beaker.session']['next_url']
else:
logging.warning('Auto login without mandaye_next_url automatically redirect to /')
logger.warning('Auto login without mandaye_next_url automatically redirect to /')
path = '/'
if not login:
logging.debug('Auto login failed because the user is not connected on vincennes.fr')
logger.debug('Auto login failed because the user is not connected on vincennes.fr')
return _302(path, request.cookies)
env['beaker.session']['login'] = login
env['beaker.session'].save()
@ -102,12 +102,12 @@ class VincennesAuth(AuthForm):
order_by(ExtUser.last_connection.desc()).\
first()
if not ext_user:
logging.debug("No association found redirect to the association page %s" % values.get('associate_url'))
logger.debug("No association found redirect to the association page %s" % values.get('associate_url'))
# TODO: urllencode path
return _302(values.get('associate_url') + "?type=first&next_url=%s" % path)
else:
response = self._login_ext_user(ext_user, env, condition, values)
logging.info("User %s has been successfully auto login on %s" % (login, self.site_name))
logger.info("User %s has been successfully auto login on %s" % (login, self.site_name))
return _302(path, response.cookies)
def auto_connection(self, env, values, request, response):
@ -129,10 +129,10 @@ class VincennesAuth(AuthForm):
else:
env['beaker.session']['next_url'] = env['PATH_INFO']
env['beaker.session'].save()
logging.debug('Auto connection call %s' % target)
logger.debug('Auto connection call %s' % target)
return _302(target)
if env['beaker.session'].get('next_url'):
logging.debug('Disable next_url in the session')
logger.debug('Disable next_url in the session')
env['beaker.session']['next_url'] = None
env['beaker.session'].save()
return response

View File

@ -29,6 +29,7 @@ email_from = 'traceback@entrouvert.com'
email_to = ['admin@localhost']
# Encrypt external passwords with a secret
# You should install pycypto to use this feature
encrypt_ext_password = False
# Must be a 16, 24, or 32 bytes long
encrypt_secret = ''

View File

@ -1,9 +1,9 @@
import logging
import re
from urlparse import urlparse
from importlib import import_module
from mandaye.log import logger
from mandaye.filters.default import MandayeFilter
from mandaye.response import _500, _302
from mandaye.exceptions import ImproperlyConfigured
@ -83,7 +83,7 @@ class Dispatcher(object):
if re.match(path, self.env['PATH_INFO']):
req_mapping = self.__get_mappings_hooks(mapper, req_mapping)
else:
logging.warning('Config error: you need to specify paths in your mapping')
logger.warning('Config error: you need to specify paths in your mapping')
return req_mapping
def _call_hook(self, hook, *args):
@ -96,7 +96,7 @@ class Dispatcher(object):
else:
return hook['filter'](self.env, values, *args)
else:
logging.warning("%s hook failed (no filter option)" % self.env['PATH_INFO'])
logger.warning("%s hook failed (no filter option)" % self.env['PATH_INFO'])
return None
def get_target_url(self):
@ -126,12 +126,13 @@ class Dispatcher(object):
if self.req_mapping['redirect']:
return _302(self.req_mapping['redirect'])
response = None
logger.debug("Loading response hook(s)")
for hook in self.req_mapping['response']:
new_response = self._call_hook(hook, request, response)
if new_response:
response = new_response
else:
logging.warning("%s Response hook %s failed" % (self.env['PATH_INFO'],
logger.warning("%s Response hook %s failed" % (self.env['PATH_INFO'],
self.req_mapping['response']['filter']))
if not response:
return _500(self.env["PATH_INFO"], "The response hook failed")
@ -152,7 +153,7 @@ class Dispatcher(object):
if new_request:
request = new_request
else:
logging.warning("%s On_request hook %s failed (empty request)" % \
logger.warning("%s On_request hook %s failed (empty request)" % \
(self.env['PATH_INFO'], hook['filter']))
return request
@ -171,7 +172,7 @@ class Dispatcher(object):
if new_response:
response = new_response
else:
logging.warning("%s On_response hook %s failed (empty answer)" % \
logger.warning("%s On_response hook %s failed (empty answer)" % \
(self.env['PATH_INFO'], hook['filter']))
return response

View File

@ -1,9 +1,9 @@
import logging
import smtplib
from email.mime.text import MIMEText
from mandaye import config
from mandaye.log import logger
class Email:
@ -13,7 +13,7 @@ class Email:
self.config_ok = True
if not config.smtp_host or not config.smtp_port or \
not config.email_from or not config.email_to:
logging.warning('[Config] Bad email notification configuration')
logger.warning('[Config] Bad email notification configuration')
self.config_ok = False
def sent(self, subject, msg, type='plain'):
@ -22,7 +22,7 @@ class Email:
subject; email subject
"""
if not self.config_ok:
logging.warning('[Config] Send email %s failed: bad configuration' % msg)
logger.warning('[Config] Send email %s failed: bad configuration' % msg)
# build email
msg = MIMEText(msg, type)
msg['Subject'] = subject

View File

@ -1,5 +1,4 @@
import Cookie
import logging
import mandaye
import re
@ -8,6 +7,7 @@ from BeautifulSoup import BeautifulSoup
import lxml.html
from mandaye.db import sql_session
from mandaye.log import logger
from mandaye.models import Site, ExtUser, LocalUser
from mandaye.response import serve_template
from mandaye.response import _302, _401
@ -81,7 +81,7 @@ class Biblio:
response.msg)
response.msg = sub[0]
if sub[1] != 1:
logging.warning('Filter Biblio.resp_html_login_page failed !')
logger.warning('Filter Biblio.resp_html_login_page failed !')
return response
def resp_html(self, env, values, request, response):
@ -110,7 +110,7 @@ class Biblio:
sub = re.subn(r, form, response.msg)
response.msg = sub[0]
if sub[1] != 1:
logging.warning('Filter Biblio.resp_associate failed !')
logger.warning('Filter Biblio.resp_associate failed !')
return response
def resp_multicompte_html(self, env, values, request, response):
@ -128,7 +128,7 @@ class Biblio:
response.msg)
response.msg = sub[0]
if sub[1] != 1:
logging.warning('Filter Biblio.resp_multicompte_html: add card number in account information failed')
logger.warning('Filter Biblio.resp_multicompte_html: add card number in account information failed')
desassociate = serve_template('biblio/disassociate.html',
account=current_account, **values)
sub = re.subn(r'<div class="link">', '<div class="link" style="margin-bottom: 10px">%s'\
@ -136,7 +136,7 @@ class Biblio:
response.msg)
response.msg = sub[0]
if sub[1] != 1:
logging.warning('Filter Biblio.resp_multicompte_html: add disassociate link failed !')
logger.warning('Filter Biblio.resp_multicompte_html: add disassociate link failed !')
else:
template = serve_template(values.get('nosso_template'), **values)
regexp = re.compile(r'(<div id="opacaccount" class="summary">.*connecter</a></div></div>)',
@ -144,7 +144,7 @@ class Biblio:
sub = re.subn(regexp, r"\1 %s" % template.encode('utf-8'), response.msg)
response.msg = sub[0]
if sub[1] != 1:
logging.warning('Filter Biblio.resp_multicompte_html: add multiaccount informations failed !')
logger.warning('Filter Biblio.resp_multicompte_html: add multiaccount informations failed !')
return response
@ -162,7 +162,7 @@ class EspaceFamille:
r"\1%s" % login.encode('utf-8'), response.msg)
response.msg = sub[0]
if sub[1] != 1:
logging.warning('Filter EspaceFamille.resp_login_page failed !')
logger.warning('Filter EspaceFamille.resp_login_page failed !')
return response
def resp_associate(self, env, values, request, response):
@ -176,7 +176,7 @@ class EspaceFamille:
div.replaceWith(form)
response.msg = str(soup)
else:
logging.warning('Filter EspaceFamille.resp_associate failed !')
logger.warning('Filter EspaceFamille.resp_associate failed !')
return response
def resp_disassociate(self, env, values, resquest, response):
@ -194,7 +194,7 @@ class EspaceFamille:
r"\1%s" % disassociate.encode('iso8859-15'), response.msg)
response.msg = sub[0]
if sub[1] > 1:
logging.warning('Filter EspaceFamille.disassociate failed !')
logger.warning('Filter EspaceFamille.disassociate failed !')
return response
@ -212,7 +212,7 @@ class Duonet:
"</table>%s" % login.encode('utf-8'), response.msg)
response.msg = sub[0]
if sub[1] > 1:
logging.warning('Filter Duonet.resp_login_page failed !')
logger.warning('Filter Duonet.resp_login_page failed !')
return response
def resp_associate(self, env, values, request, response):
@ -258,7 +258,7 @@ class Duonet:
for cookie in request.cookies.itervalues():
cookie['expires'] = 'Tue, 02-Dec-2003 08:19:12 GMT'
cookie['Path'] = '/'
logging.debug('Logout from Mandaye')
logger.debug('Logout from Mandaye')
env['beaker.session'].delete()
return _302(values.get('index_url'), request.cookies)

View File

@ -4,7 +4,6 @@ Mandaye HTTP object: Response, Request and Headers
import copy
import Cookie
import logging
class HTTPHeader(dict):
""" Mandaye class to manage HTTP header

43
mandaye/log.py Normal file
View File

@ -0,0 +1,43 @@
import logging
from logging import FileHandler
from logging.handlers import SysLogHandler
from mandaye.config import log_level, syslog, log_file
logger = logging.getLogger('mandaye')
logger.setLevel(log_level)
# Syslog logging
if syslog:
syslog_handler = SysLogHandler(address='/dev/log')
logger.addHandler(syslog_handler)
else:
syslog_handler = None
# File logging
if log_file:
filehandler = FileHandler(log_file)
logger.addHandler(filehandler)
else:
filehandler = None
# Stream logging
sh = logging.StreamHandler()
sh.setFormatter(logging.Formatter('%(levelname)s %(message)s'))
logger.addHandler(sh)
def format_logging_handlers(uuid):
""" uuid: unique id to trace a request
Format logging handlers
"""
if filehandler:
filehandler.setFormatter(
logging.Formatter('%(asctime)s %(levelname)s [' +\
uuid + '] %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
)
if syslog_handler:
syslog_handler.setFormatter(
logging.Formatter('%(levelname)s [' + uuid + '] %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
)

View File

@ -1,10 +1,10 @@
import logging
import traceback
import pprint
import sys
from cgi import escape
from mandaye.log import logger
from mandaye.http import HTTPResponse, HTTPHeader
from mandaye.config import debug, email_notification
from mandaye.emails import Email
@ -28,17 +28,18 @@ def _302(location, cookies=None):
headers = HTTPHeader({'Location': [location],
'Content-Type': ['text/html']})
msg = "Reload the page to get source for: %s" % location
logger.info('302 redirect to %s' % location)
return HTTPResponse(302, 'Found', headers, msg, cookies)
def _401(msg):
title='Unauthorized'
logging.warning('401 %s' % msg)
logger.warning('401 %s' % msg)
headers = HTTPHeader({'Content-Type': ['text/html']})
return HTTPResponse(401, 'Unauthorized', headers,
serve_template("response.html", title=title, body=msg))
def _404(path):
logging.warning("404 %s not found" % path)
logger.warning("404 %s not found" % path)
title = "Not Found"
body = "The requested URL %s was not found on this server." % path
headers = HTTPHeader({'Content-Type': ['text/html']})
@ -56,10 +57,10 @@ def _500(path, msg, exception=None, env=None):
email.sent('Mandaye internal server error',
_get_html_traceback(tb_str, path, env=env), 'html')
except Exception as detail:
logging.warning('Sent mail failed with error: %s' % detail)
logging.exception("500 %s: %s, %s, env: %r" % (path, msg, tb_str, env))
logger.warning('Sent mail failed with error: %s' % detail)
logger.exception("500 %s: %s, %s, env: %r" % (path, msg, tb_str, env))
else:
logging.error("500 %s: %s, env: %r" % (path, msg, env))
logger.error("500 %s: %s, env: %r" % (path, msg, env))
title = 'Internal Server Error'
headers = HTTPHeader({'Content-Type': ['text/html']})
if debug:
@ -79,7 +80,7 @@ def _502(path, target_hostname, exception=None):
""" Return a 502 error
path: the path of this error
exception: this exception instance if any """
logging.critical("[502] error while reading %s:\n" % path)
logger.error("502 error while reading %s" % path)
title = 'Bad Gateway'
headers = HTTPHeader({'Content-Type': ['text/html']})
msg = "The Manaye server can't join the target server or misconfiguration \

View File

@ -1,21 +1,26 @@
import Cookie
import config
import logging
import urllib
import urllib2
import re
import sys
import traceback
import poster.streaminghttp
import random
import re
import os
import sys
import time
import traceback
from beaker.middleware import SessionMiddleware
from cgi import escape
from md5 import md5
from static import Cling
import mandaye
from mandaye.config import debug
from mandaye.dispatcher import Dispatcher
from mandaye.log import logger, format_logging_handlers
from mandaye.handlers.default import MandayeRedirectHandler, MandayeErrorHandler
from mandaye.http import HTTPHeader, HTTPRequest, HTTPResponse
from mandaye.response import _404, _502, _500
@ -31,8 +36,11 @@ def get_response(env, request, url, cookiejar=None):
url = re.sub('(?<!:)/+', '/', url)
if request.req_method == 'POST':
req = urllib2.Request(url, request.msg, request.headers.getheaders())
logger.info('Mandaye POST %s' % url)
logger.debug('POST content %s' % request.msg)
else:
req = urllib2.Request(url, headers=request.headers.getheaders())
logger.info('Mandaye GET %s' % url)
# Load the cookies
if request.cookies:
req.add_header('cookie', request.cookies.output(attrs=[], sep=';', header=''))
@ -75,6 +83,7 @@ class MandayeApp(object):
self.env = None
self.dispatcher = None
def __call__(self, env, start_response):
""" called by the WSGI server
env: standard WSGI env
@ -87,9 +96,15 @@ class MandayeApp(object):
self.env['mandaye.scheme'] = env['HTTP_X_FORWARDED_SCHEME']
else:
self.env['mandaye.scheme'] = env['wsgi.url_scheme']
self.env['mandaye.uuid'] = self._get_uuid()
self.dispatcher = None
local_host = env['HTTP_HOST']
path_info = env['PATH_INFO']
format_logging_handlers(self.env['mandaye.uuid'])
logger.info("Client %s - %s %s://%s%s" %\
(self.env['REMOTE_ADDR'], self.env['REQUEST_METHOD'],
self.env['wsgi.url_scheme'], self.env['HTTP_HOST'],
self.env['PATH_INFO']))
if config.hosts.has_key(local_host):
for mapper in config.hosts[local_host]:
if re.match(mapper.get('path'), path_info):
@ -116,6 +131,15 @@ class MandayeApp(object):
sql_session.close()
return response
def _get_uuid(self):
id_str = "%f%s%f%s" % (
time.time(),
id({}),
random.random(),
os.getpid
)
return md5(md5(id_str).hexdigest()).hexdigest()
def _get_request(self):
""" Return a Mandaye HTTP Request

View File

@ -5,12 +5,11 @@
"""
import base64
import logging
from optparse import OptionParser
from Crypto.Cipher import AES
from mandaye import config
from mandaye.log import logger
from mandaye.models import ExtUser
from mandaye.db import sql_session
@ -36,7 +35,8 @@ def get_cmd_options():
return options
def encrypt_pwd(pwd):
logging.debug("Encrypt password")
from Crypto.Cipher import AES
logger.debug("Encrypt password")
enc_pwd = pwd
if config.encrypt_secret:
try:
@ -46,21 +46,21 @@ def encrypt_pwd(pwd):
except Exception, e:
if config.debug:
traceback.print_exc()
logging.warning('Password encrypting failed %s' % e)
logger.warning('Password encrypting failed %s' % e)
else:
logging.warning("You must set a secret to use pwd encryption")
logger.warning("You must set a secret to use pwd encryption")
return enc_pwd
def main():
options = get_cmd_options()
if options.createdb:
logging.info("Creating database...")
logger.info("Creating database...")
if config.db_url:
from mandaye.models import Base
from sqlalchemy import create_engine
engine = create_engine(config.db_url)
Base.metadata.create_all(engine)
logging.info("Database created")
logger.info("Database created")
if options.cryptpwd:
for user in sql_session().query(ExtUser).all():
user.password = encrypt_pwd(user.password)

View File

@ -1,13 +1,13 @@
#!/home/jschneider/temp/test/bin/python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" Script to launch mandaye with gunicorn server
"""
import logging
import sys
import os
from mandaye.log import logger
from gunicorn.app.wsgiapp import WSGIApplication
class WSGIApplication(WSGIApplication):
@ -22,7 +22,7 @@ def main():
""" The ``gunicorn`` command line runner for launcing Gunicorn with
generic WSGI applications.
"""
logging.info('Launching Mandaye ...')
logger.info('Launching Mandaye ...')
WSGIApplication("%prog [OPTIONS]").run()
if __name__ == "__main__":